diff options
author | Markus Hatvan <markus_hatvan@aon.at> | 2021-09-14 10:34:04 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-14 10:34:04 +0200 |
commit | 979ec02c9a1019152be08705986337e470eabb57 (patch) | |
tree | 6021179ad8649112717a499780f475275af487f2 /src/stores/UIStore.ts | |
parent | refactor: defensive programming to avoid javascript error for unread badges (diff) | |
download | ferdium-app-979ec02c9a1019152be08705986337e470eabb57.tar.gz ferdium-app-979ec02c9a1019152be08705986337e470eabb57.tar.zst ferdium-app-979ec02c9a1019152be08705986337e470eabb57.zip |
chore: codebase improvements (#1930)
Diffstat (limited to 'src/stores/UIStore.ts')
-rw-r--r-- | src/stores/UIStore.ts | 117 |
1 files changed, 117 insertions, 0 deletions
diff --git a/src/stores/UIStore.ts b/src/stores/UIStore.ts new file mode 100644 index 000000000..ec48eeb40 --- /dev/null +++ b/src/stores/UIStore.ts | |||
@@ -0,0 +1,117 @@ | |||
1 | import { action, observable, computed, reaction } from 'mobx'; | ||
2 | import { theme, ThemeType } from '@meetfranz/theme'; | ||
3 | import { nativeTheme, systemPreferences } from '@electron/remote'; | ||
4 | |||
5 | import Store from './lib/Store'; | ||
6 | import { isMac, isWindows } from '../environment'; | ||
7 | |||
8 | export default class UIStore extends Store { | ||
9 | actions: any; | ||
10 | |||
11 | stores: any; | ||
12 | |||
13 | @observable showServicesUpdatedInfoBar = false; | ||
14 | |||
15 | @observable isOsDarkThemeActive = nativeTheme.shouldUseDarkColors; | ||
16 | |||
17 | constructor(...args) { | ||
18 | super(...args); | ||
19 | |||
20 | // Register action handlers | ||
21 | this.actions.ui.openSettings.listen(this._openSettings.bind(this)); | ||
22 | this.actions.ui.closeSettings.listen(this._closeSettings.bind(this)); | ||
23 | this.actions.ui.toggleServiceUpdatedInfoBar.listen( | ||
24 | this._toggleServiceUpdatedInfoBar.bind(this), | ||
25 | ); | ||
26 | |||
27 | // Listen for theme change on MacOS | ||
28 | if (isMac) { | ||
29 | systemPreferences.subscribeNotification( | ||
30 | 'AppleInterfaceThemeChangedNotification', | ||
31 | () => { | ||
32 | this.isOsDarkThemeActive = nativeTheme.shouldUseDarkColors; | ||
33 | this.actions.service.shareSettingsWithServiceProcess(); | ||
34 | }, | ||
35 | ); | ||
36 | } | ||
37 | |||
38 | if (isWindows) { | ||
39 | nativeTheme.on('updated', () => { | ||
40 | this.isOsDarkThemeActive = nativeTheme.shouldUseDarkColors; | ||
41 | this.actions.service.shareSettingsWithServiceProcess(); | ||
42 | }); | ||
43 | } | ||
44 | } | ||
45 | |||
46 | setup() { | ||
47 | reaction( | ||
48 | () => this.isDarkThemeActive, | ||
49 | () => { | ||
50 | this._setupThemeInDOM(); | ||
51 | }, | ||
52 | { fireImmediately: true }, | ||
53 | ); | ||
54 | } | ||
55 | |||
56 | @computed get showMessageBadgesEvenWhenMuted() { | ||
57 | const settings = this.stores.settings.all; | ||
58 | |||
59 | return ( | ||
60 | (settings.app.isAppMuted && settings.app.showMessageBadgeWhenMuted) || | ||
61 | !settings.app.isAppMuted | ||
62 | ); | ||
63 | } | ||
64 | |||
65 | @computed get isDarkThemeActive() { | ||
66 | const isWithAdaptableInDarkMode = | ||
67 | this.stores.settings.all.app.adaptableDarkMode && | ||
68 | this.isOsDarkThemeActive; | ||
69 | const isWithoutAdaptableInDarkMode = | ||
70 | this.stores.settings.all.app.darkMode && | ||
71 | !this.stores.settings.all.app.adaptableDarkMode; | ||
72 | const isInDarkMode = this.stores.settings.all.app.darkMode; | ||
73 | return !!( | ||
74 | isWithAdaptableInDarkMode || | ||
75 | isWithoutAdaptableInDarkMode || | ||
76 | isInDarkMode | ||
77 | ); | ||
78 | } | ||
79 | |||
80 | @computed get theme() { | ||
81 | const themeId = | ||
82 | this.isDarkThemeActive || this.stores.settings.app.darkMode | ||
83 | ? ThemeType.dark | ||
84 | : ThemeType.default; | ||
85 | const { accentColor } = this.stores.settings.app; | ||
86 | return theme(themeId, accentColor); | ||
87 | } | ||
88 | |||
89 | // Actions | ||
90 | @action _openSettings({ path = '/settings' }) { | ||
91 | const settingsPath = path !== '/settings' ? `/settings/${path}` : path; | ||
92 | this.stores.router.push(settingsPath); | ||
93 | } | ||
94 | |||
95 | @action _closeSettings() { | ||
96 | this.stores.router.push('/'); | ||
97 | } | ||
98 | |||
99 | @action _toggleServiceUpdatedInfoBar({ visible }) { | ||
100 | let visibility = visible; | ||
101 | if (visibility === null) { | ||
102 | visibility = !this.showServicesUpdatedInfoBar; | ||
103 | } | ||
104 | this.showServicesUpdatedInfoBar = visibility; | ||
105 | } | ||
106 | |||
107 | // Reactions | ||
108 | _setupThemeInDOM() { | ||
109 | const body = document.querySelector('body'); | ||
110 | |||
111 | if (!this.isDarkThemeActive) { | ||
112 | body?.classList.remove('theme__dark'); | ||
113 | } else { | ||
114 | body?.classList.add('theme__dark'); | ||
115 | } | ||
116 | } | ||
117 | } | ||