diff options
Diffstat (limited to 'packages/main/src/controllers/NativeThemeController.ts')
-rw-r--r-- | packages/main/src/controllers/NativeThemeController.ts | 37 |
1 files changed, 24 insertions, 13 deletions
diff --git a/packages/main/src/controllers/NativeThemeController.ts b/packages/main/src/controllers/NativeThemeController.ts index 07a3292..a50d41e 100644 --- a/packages/main/src/controllers/NativeThemeController.ts +++ b/packages/main/src/controllers/NativeThemeController.ts | |||
@@ -18,21 +18,32 @@ | |||
18 | * SPDX-License-Identifier: AGPL-3.0-only | 18 | * SPDX-License-Identifier: AGPL-3.0-only |
19 | */ | 19 | */ |
20 | 20 | ||
21 | import { nativeTheme } from 'electron'; | ||
21 | import { autorun } from 'mobx'; | 22 | import { autorun } from 'mobx'; |
22 | import type { IDisposer } from 'mobx-state-tree'; | 23 | import { IDisposer } from 'mobx-state-tree'; |
23 | 24 | ||
24 | import type { NativeThemeService } from '../services/NativeThemeService'; | ||
25 | import type { MainStore } from '../stores/MainStore'; | 25 | import type { MainStore } from '../stores/MainStore'; |
26 | 26 | ||
27 | export function initNativeTheme(store: MainStore, service: NativeThemeService): IDisposer { | 27 | export class NativeThemeController { |
28 | const themeSourceReactionDisposer = autorun(() => { | 28 | private autorunDisposer: IDisposer | null = null; |
29 | service.setThemeSource(store.config.themeSource); | 29 | |
30 | }); | 30 | private shouldUseDarkColorsListener: (() => void) | null = null; |
31 | const onShouldUseDarkColorsUpdatedDisposer = service.onShouldUseDarkColorsUpdated( | 31 | |
32 | store.setShouldUseDarkColors, | 32 | connect(store: MainStore): void { |
33 | ); | 33 | this.autorunDisposer = autorun(() => { |
34 | return () => { | 34 | nativeTheme.themeSource = store.config.themeSource; |
35 | onShouldUseDarkColorsUpdatedDisposer(); | 35 | }); |
36 | themeSourceReactionDisposer(); | 36 | store.setShouldUseDarkColors(nativeTheme.shouldUseDarkColors); |
37 | }; | 37 | this.shouldUseDarkColorsListener = () => { |
38 | store.setShouldUseDarkColors(nativeTheme.shouldUseDarkColors); | ||
39 | }; | ||
40 | nativeTheme.on('updated', this.shouldUseDarkColorsListener); | ||
41 | } | ||
42 | |||
43 | dispose(): void { | ||
44 | if (this.shouldUseDarkColorsListener !== null) { | ||
45 | nativeTheme.off('updated', this.shouldUseDarkColorsListener); | ||
46 | } | ||
47 | this.autorunDisposer?.(); | ||
48 | } | ||
38 | } | 49 | } |