Skip to content

Commit f38fef2

Browse files
committed
ThemeManager Created
1 parent 8be0ea6 commit f38fef2

File tree

4 files changed

+62
-23
lines changed

4 files changed

+62
-23
lines changed

browser/main/Main.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import store from 'browser/main/store'
1717
import i18n from 'browser/lib/i18n'
1818
import { getLocales } from 'browser/lib/Languages'
1919
import applyShortcuts from 'browser/main/lib/shortcutManager'
20+
import theme from 'browser/main/lib/ThemeManager'
2021
const path = require('path')
2122
const electron = require('electron')
2223
const { remote } = electron
@@ -140,13 +141,12 @@ class Main extends React.Component {
140141
componentDidMount () {
141142
const { dispatch, config } = this.props
142143

143-
const supportedThemes = ['dark', 'white', 'solarized-dark', 'monokai', 'dracula']
144+
this.refreshTheme = setInterval(() => {
145+
theme.choose(ConfigManager.get().ui)
146+
}, 5 * 1000)
144147

145-
if (supportedThemes.indexOf(config.ui.theme) !== -1) {
146-
document.body.setAttribute('data-theme', config.ui.theme)
147-
} else {
148-
document.body.setAttribute('data-theme', 'default')
149-
}
148+
theme.choose(config.ui)
149+
theme.apply(config.ui.theme)
150150

151151
if (getLocales().indexOf(config.ui.language) !== -1) {
152152
i18n.setLocale(config.ui.language)

browser/main/lib/ConfigManager.js

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import _ from 'lodash'
22
import RcParser from 'browser/lib/RcParser'
33
import i18n from 'browser/lib/i18n'
44
import ee from 'browser/main/lib/eventEmitter'
5+
import theme from 'browser/main/lib/ThemeManager'
56

67
const OSX = global.process.platform === 'darwin'
78
const win = global.process.platform === 'win32'
@@ -30,8 +31,9 @@ export const DEFAULT_CONFIG = {
3031
ui: {
3132
language: 'en',
3233
theme: 'default',
34+
defaultTheme: 'default',
3335
enableScheduleTheme: false,
34-
scheduledTheme: 'Monokai',
36+
scheduledTheme: 'monokai',
3537
scheduleStart: 1200,
3638
scheduleEnd: 360,
3739
showCopyNotification: true,
@@ -149,19 +151,8 @@ function set (updates) {
149151
if (!validate(newConfig)) throw new Error('INVALID CONFIG')
150152
_save(newConfig)
151153

152-
if (newConfig.ui.theme === 'dark') {
153-
document.body.setAttribute('data-theme', 'dark')
154-
} else if (newConfig.ui.theme === 'white') {
155-
document.body.setAttribute('data-theme', 'white')
156-
} else if (newConfig.ui.theme === 'solarized-dark') {
157-
document.body.setAttribute('data-theme', 'solarized-dark')
158-
} else if (newConfig.ui.theme === 'monokai') {
159-
document.body.setAttribute('data-theme', 'monokai')
160-
} else if (newConfig.ui.theme === 'dracula') {
161-
document.body.setAttribute('data-theme', 'dracula')
162-
} else {
163-
document.body.setAttribute('data-theme', 'default')
164-
}
154+
theme.choose(newConfig.ui)
155+
theme.apply(newConfig.ui.theme)
165156

166157
i18n.setLocale(newConfig.ui.language)
167158

browser/main/lib/ThemeManager.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
function choose (ui) {
2+
console.log(ui.enableScheduleTheme)
3+
if (ui.enableScheduleTheme !== 'on') {
4+
return
5+
}
6+
7+
const start = parseInt(ui.scheduleStart)
8+
const end = parseInt(ui.scheduleEnd)
9+
10+
const now = new Date()
11+
const minutes = now.getHours() * 60 + now.getMinutes()
12+
13+
console.log(ui.scheduleStart, minutes, ui.scheduleEnd)
14+
15+
if ((end > start && minutes >= start && minutes <= end) ||
16+
(start > end && (minutes >= start || minutes <= end))) {
17+
console.log('SC', ui.theme, ui.scheduledTheme)
18+
if (ui.theme !== ui.scheduledTheme) {
19+
ui.defaultTheme = ui.theme
20+
ui.theme = ui.scheduledTheme
21+
apply(ui.theme)
22+
}
23+
24+
console.log(ui.defaultTheme, ui.theme)
25+
} else {
26+
console.log('TH', ui.theme, ui.defaultTheme)
27+
if (ui.theme !== ui.defaultTheme) {
28+
ui.theme = ui.defaultTheme
29+
apply(ui.theme)
30+
}
31+
32+
console.log(ui.theme)
33+
}
34+
}
35+
36+
function apply (theme) {
37+
console.log('Apply ', theme)
38+
const supportedThemes = ['dark', 'white', 'solarized-dark', 'monokai', 'dracula']
39+
if (supportedThemes.indexOf(theme) !== -1) {
40+
document.body.setAttribute('data-theme', theme)
41+
} else {
42+
document.body.setAttribute('data-theme', 'default')
43+
}
44+
}
45+
46+
export default {
47+
choose,
48+
apply
49+
}

browser/main/modals/PreferencesModal/UiTab.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ class UiTab extends React.Component {
6868
const newConfig = {
6969
ui: {
7070
theme: this.refs.uiTheme.value,
71+
defaultTheme: this.refs.uiTheme.value,
7172
enableScheduleTheme: this.refs.enableScheduleTheme.value,
7273
scheduledTheme: this.refs.uiScheduledTheme.value,
7374
scheduleStart: this.refs.scheduleStart.value,
@@ -203,8 +204,6 @@ class UiTab extends React.Component {
203204

204205
if (e) {
205206
this.handleUIChange(e)
206-
} else {
207-
console.log('HEY')
208207
}
209208
}
210209

@@ -231,7 +230,7 @@ class UiTab extends React.Component {
231230
{i18n.__('Interface Theme')}
232231
</div>
233232
<div styleName='group-section-control'>
234-
<select value={config.ui.theme}
233+
<select value={config.ui.defaultTheme}
235234
onChange={(e) => this.handleUIChange(e)}
236235
ref='uiTheme'
237236
>

0 commit comments

Comments
 (0)