diff options
author | Kristóf Marussy <kristof@marussy.com> | 2021-12-26 01:59:17 +0100 |
---|---|---|
committer | Kristóf Marussy <kristof@marussy.com> | 2021-12-26 01:59:17 +0100 |
commit | cce40ab1d2ad8cc1b60fabb4cc8281ea0490a123 (patch) | |
tree | bb96df2676934a9cd4ef2bc5a48957e8b32fb646 /packages/main/src | |
parent | refactor: Simplify browserViewBounds handling (diff) | |
download | sophie-cce40ab1d2ad8cc1b60fabb4cc8281ea0490a123.tar.gz sophie-cce40ab1d2ad8cc1b60fabb4cc8281ea0490a123.tar.zst sophie-cce40ab1d2ad8cc1b60fabb4cc8281ea0490a123.zip |
feat: Set nativeTheme theme source on dark mode
Diffstat (limited to 'packages/main/src')
-rw-r--r-- | packages/main/src/index.ts | 16 | ||||
-rw-r--r-- | packages/main/src/stores/Config.ts | 32 | ||||
-rw-r--r-- | packages/main/src/stores/RootStore.ts | 21 | ||||
-rw-r--r-- | packages/main/src/stores/SharedStore.ts | 32 |
4 files changed, 90 insertions, 11 deletions
diff --git a/packages/main/src/index.ts b/packages/main/src/index.ts index a135902..6a8a74e 100644 --- a/packages/main/src/index.ts +++ b/packages/main/src/index.ts | |||
@@ -23,6 +23,7 @@ import { | |||
23 | BrowserView, | 23 | BrowserView, |
24 | BrowserWindow, | 24 | BrowserWindow, |
25 | ipcMain, | 25 | ipcMain, |
26 | nativeTheme, | ||
26 | } from 'electron'; | 27 | } from 'electron'; |
27 | import { readFile, readFileSync } from 'fs'; | 28 | import { readFile, readFileSync } from 'fs'; |
28 | import { autorun } from 'mobx'; | 29 | import { autorun } from 'mobx'; |
@@ -36,7 +37,7 @@ import { | |||
36 | import { | 37 | import { |
37 | browserViewBounds, | 38 | browserViewBounds, |
38 | MainToRendererIpcMessage, | 39 | MainToRendererIpcMessage, |
39 | paletteMode, | 40 | themeSource, |
40 | RendererToMainIpcMessage, | 41 | RendererToMainIpcMessage, |
41 | } from '@sophie/shared'; | 42 | } from '@sophie/shared'; |
42 | import { URL } from 'url'; | 43 | import { URL } from 'url'; |
@@ -104,6 +105,15 @@ let mainWindow: BrowserWindow | null = null; | |||
104 | 105 | ||
105 | const store = createRootStore(); | 106 | const store = createRootStore(); |
106 | 107 | ||
108 | autorun(() => { | ||
109 | nativeTheme.themeSource = store.config.themeSource; | ||
110 | }); | ||
111 | |||
112 | store.setShouldUseDarkColors(nativeTheme.shouldUseDarkColors); | ||
113 | nativeTheme.on('updated', () => { | ||
114 | store.setShouldUseDarkColors(nativeTheme.shouldUseDarkColors); | ||
115 | }); | ||
116 | |||
107 | const rendererBaseUrl = getResourceUrl('../renderer/'); | 117 | const rendererBaseUrl = getResourceUrl('../renderer/'); |
108 | function shouldCancelMainWindowRequest(url: string, method: string): boolean { | 118 | function shouldCancelMainWindowRequest(url: string, method: string): boolean { |
109 | if (method !== 'GET') { | 119 | if (method !== 'GET') { |
@@ -191,8 +201,8 @@ function createWindow(): Promise<unknown> { | |||
191 | case RendererToMainIpcMessage.SetBrowserViewBounds: | 201 | case RendererToMainIpcMessage.SetBrowserViewBounds: |
192 | store.setBrowserViewBounds(browserViewBounds.parse(args[0])); | 202 | store.setBrowserViewBounds(browserViewBounds.parse(args[0])); |
193 | break; | 203 | break; |
194 | case RendererToMainIpcMessage.SetPaletteMode: | 204 | case RendererToMainIpcMessage.SetThemeSource: |
195 | store.setPaletteMode(paletteMode.parse(args[0])) | 205 | store.config.setThemeSource(themeSource.parse(args[0])) |
196 | break; | 206 | break; |
197 | case RendererToMainIpcMessage.ReloadAllServices: | 207 | case RendererToMainIpcMessage.ReloadAllServices: |
198 | readFile(serviceInjectPath, 'utf8', (err, data) => { | 208 | readFile(serviceInjectPath, 'utf8', (err, data) => { |
diff --git a/packages/main/src/stores/Config.ts b/packages/main/src/stores/Config.ts new file mode 100644 index 0000000..483a491 --- /dev/null +++ b/packages/main/src/stores/Config.ts | |||
@@ -0,0 +1,32 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2021-2022 Kristóf Marussy <kristof@marussy.com> | ||
3 | * | ||
4 | * This file is part of Sophie. | ||
5 | * | ||
6 | * Sophie is free software: you can redistribute it and/or modify | ||
7 | * it under the terms of the GNU Affero General Public License as | ||
8 | * published by the Free Software Foundation, version 3. | ||
9 | * | ||
10 | * This program is distributed in the hope that it will be useful, | ||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | * GNU Affero General Public License for more details. | ||
14 | * | ||
15 | * You should have received a copy of the GNU Affero General Public License | ||
16 | * along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
17 | * | ||
18 | * SPDX-License-Identifier: AGPL-3.0-only | ||
19 | */ | ||
20 | |||
21 | import type { Instance } from 'mobx-state-tree'; | ||
22 | import { config as originalConfig, ThemeSource } from '@sophie/shared'; | ||
23 | |||
24 | export type { ConfigSnapshotIn, ConfigSnapshotOut } from '@sophie/shared'; | ||
25 | |||
26 | export const config = originalConfig.actions((self) => ({ | ||
27 | setThemeSource(mode: ThemeSource) { | ||
28 | self.themeSource = mode; | ||
29 | }, | ||
30 | })); | ||
31 | |||
32 | export interface Config extends Instance<typeof config> {} | ||
diff --git a/packages/main/src/stores/RootStore.ts b/packages/main/src/stores/RootStore.ts index c09cd4a..31e2b71 100644 --- a/packages/main/src/stores/RootStore.ts +++ b/packages/main/src/stores/RootStore.ts | |||
@@ -22,25 +22,30 @@ import { applySnapshot, Instance, types } from 'mobx-state-tree'; | |||
22 | import { | 22 | import { |
23 | BrowserViewBounds, | 23 | BrowserViewBounds, |
24 | emptySharedStore, | 24 | emptySharedStore, |
25 | PaletteMode, | ||
26 | sharedStore, | ||
27 | } from '@sophie/shared'; | 25 | } from '@sophie/shared'; |
28 | 26 | ||
27 | import type { Config } from './Config'; | ||
28 | import { sharedStore } from './SharedStore'; | ||
29 | |||
29 | export const rootStore = types.model('RootStore', { | 30 | export const rootStore = types.model('RootStore', { |
30 | browserViewBounds: types.model("BrowserViewBoundsStore", { | 31 | browserViewBounds: types.model('BrowserViewBounds', { |
31 | x: 0, | 32 | x: 0, |
32 | y: 0, | 33 | y: 0, |
33 | width: 0, | 34 | width: 0, |
34 | height: 0, | 35 | height: 0, |
35 | }), | 36 | }), |
36 | shared: sharedStore, | 37 | shared: sharedStore, |
37 | }).actions((self) => ({ | 38 | }).views((self) => ({ |
38 | setBrowserViewBounds(bounds: BrowserViewBounds) { | 39 | get config(): Config { |
39 | applySnapshot(self.browserViewBounds, bounds); | 40 | return self.shared.config; |
40 | }, | 41 | }, |
41 | setPaletteMode(mode: PaletteMode) { | 42 | })).actions((self) => ({ |
42 | self.shared.shouldUseDarkColors = mode === 'dark'; | 43 | setBrowserViewBounds(bounds: BrowserViewBounds): void { |
44 | applySnapshot(self.browserViewBounds, bounds); | ||
43 | }, | 45 | }, |
46 | setShouldUseDarkColors(shouldUseDarkColors: boolean): void { | ||
47 | self.shared.shouldUseDarkColors = shouldUseDarkColors; | ||
48 | } | ||
44 | })); | 49 | })); |
45 | 50 | ||
46 | export interface RootStore extends Instance<typeof rootStore> {} | 51 | export interface RootStore extends Instance<typeof rootStore> {} |
diff --git a/packages/main/src/stores/SharedStore.ts b/packages/main/src/stores/SharedStore.ts new file mode 100644 index 0000000..04dda32 --- /dev/null +++ b/packages/main/src/stores/SharedStore.ts | |||
@@ -0,0 +1,32 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2021-2022 Kristóf Marussy <kristof@marussy.com> | ||
3 | * | ||
4 | * This file is part of Sophie. | ||
5 | * | ||
6 | * Sophie is free software: you can redistribute it and/or modify | ||
7 | * it under the terms of the GNU Affero General Public License as | ||
8 | * published by the Free Software Foundation, version 3. | ||
9 | * | ||
10 | * This program is distributed in the hope that it will be useful, | ||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | * GNU Affero General Public License for more details. | ||
14 | * | ||
15 | * You should have received a copy of the GNU Affero General Public License | ||
16 | * along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
17 | * | ||
18 | * SPDX-License-Identifier: AGPL-3.0-only | ||
19 | */ | ||
20 | |||
21 | import { Instance } from 'mobx-state-tree'; | ||
22 | import { sharedStore as originalSharedStore } from '@sophie/shared'; | ||
23 | |||
24 | import { config } from './Config'; | ||
25 | |||
26 | export type { SharedStoreSnapshotIn, SharedStoreSnapshotOut } from '@sophie/shared'; | ||
27 | |||
28 | export const sharedStore = originalSharedStore.props({ | ||
29 | config, | ||
30 | }); | ||
31 | |||
32 | export interface SharedStore extends Instance<typeof sharedStore> {} | ||