From d07e7b834831230b53860d0919a68edc2d36193d Mon Sep 17 00:00:00 2001 From: Kristóf Marussy Date: Sat, 8 Jan 2022 21:36:43 +0100 Subject: build: Eslint fixes for multi-module project MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Kristóf Marussy --- .../main/src/controllers/__tests__/config.spec.ts | 185 --------------------- .../src/controllers/__tests__/initConfig.spec.ts | 185 +++++++++++++++++++++ .../controllers/__tests__/initNativeTheme.spec.ts | 71 ++++++++ .../src/controllers/__tests__/nativeTheme.spec.ts | 71 -------- packages/main/src/controllers/config.ts | 90 ---------- packages/main/src/controllers/initConfig.ts | 90 ++++++++++ packages/main/src/controllers/initNativeTheme.ts | 50 ++++++ packages/main/src/controllers/nativeTheme.ts | 50 ------ 8 files changed, 396 insertions(+), 396 deletions(-) delete mode 100644 packages/main/src/controllers/__tests__/config.spec.ts create mode 100644 packages/main/src/controllers/__tests__/initConfig.spec.ts create mode 100644 packages/main/src/controllers/__tests__/initNativeTheme.spec.ts delete mode 100644 packages/main/src/controllers/__tests__/nativeTheme.spec.ts delete mode 100644 packages/main/src/controllers/config.ts create mode 100644 packages/main/src/controllers/initConfig.ts create mode 100644 packages/main/src/controllers/initNativeTheme.ts delete mode 100644 packages/main/src/controllers/nativeTheme.ts (limited to 'packages/main/src/controllers') diff --git a/packages/main/src/controllers/__tests__/config.spec.ts b/packages/main/src/controllers/__tests__/config.spec.ts deleted file mode 100644 index eb67df0..0000000 --- a/packages/main/src/controllers/__tests__/config.spec.ts +++ /dev/null @@ -1,185 +0,0 @@ -/* - * Copyright (C) 2021-2022 Kristóf Marussy - * - * This file is part of Sophie. - * - * Sophie is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, version 3. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - * SPDX-License-Identifier: AGPL-3.0-only - */ - -import { jest } from '@jest/globals'; -import { mocked } from 'jest-mock'; -import ms from 'ms'; - -import { initConfig } from '../config'; -import type { ConfigPersistenceService } from '../../services/ConfigPersistenceService'; -import { Config, config as configModel } from '../../stores/Config'; -import { Disposer } from '../../utils/disposer'; -import { silenceLogger } from '../../utils/logging'; - -let config: Config; -let persistenceService: ConfigPersistenceService = { - readConfig: jest.fn(), - writeConfig: jest.fn(), - watchConfig: jest.fn(), -}; -let lessThanThrottleMs = ms('0.1s'); -let throttleMs = ms('1s'); - -beforeAll(() => { - jest.useFakeTimers(); - silenceLogger(); -}); - -beforeEach(() => { - config = configModel.create(); -}); - -describe('when initializing', () => { - describe('when there is no config file', () => { - beforeEach(() => { - mocked(persistenceService.readConfig).mockResolvedValueOnce({ - found: false, - }); - }); - - it('should create a new config file', async () => { - await initConfig(config, persistenceService); - expect(persistenceService.writeConfig).toBeCalledTimes(1); - }); - - it('should bail if there is an an error creating the config file', async () => { - mocked(persistenceService.writeConfig).mockRejectedValue(new Error('boo')); - await expect(() => initConfig(config, persistenceService)).rejects.toBeInstanceOf(Error); - }); - }); - - describe('when there is a valid config file', () => { - beforeEach(() => { - mocked(persistenceService.readConfig).mockResolvedValueOnce({ - found: true, - data: { - themeSource: 'dark', - }, - }); - }); - - it('should read the existing config file is there is one', async () => { - await initConfig(config, persistenceService); - expect(persistenceService.writeConfig).not.toBeCalled(); - expect(config.themeSource).toBe('dark'); - }); - - it('should bail if it cannot set up a watcher', async () => { - mocked(persistenceService.watchConfig).mockImplementationOnce(() => { - throw new Error('boo'); - }); - await expect(() => initConfig(config, persistenceService)).rejects.toBeInstanceOf(Error); - }); - }); - - it('should not apply an invalid config file', async () => { - mocked(persistenceService.readConfig).mockResolvedValueOnce({ - found: true, - data: { - themeSource: -1, - }, - }); - await initConfig(config, persistenceService); - expect(config.themeSource).not.toBe(-1); - }); - - it('should bail if it cannot determine whether there is a config file', async () => { - mocked(persistenceService.readConfig).mockRejectedValue(new Error('boo')); - await expect(() => initConfig(config, persistenceService)).rejects.toBeInstanceOf(Error); - }); -}); - -describe('when it has loaded the config', () => { - let sutDisposer: Disposer; - let watcherDisposer: Disposer = jest.fn(); - let configChangedCallback: () => Promise; - - beforeEach(async () => { - mocked(persistenceService.readConfig).mockResolvedValueOnce({ - found: true, - data: {}, - }); - mocked(persistenceService.watchConfig).mockReturnValueOnce(watcherDisposer); - sutDisposer = await initConfig(config, persistenceService, throttleMs); - configChangedCallback = mocked(persistenceService.watchConfig).mock.calls[0][0]; - jest.resetAllMocks(); - }); - - it('should throttle saving changes to the config file', () => { - mocked(persistenceService.writeConfig).mockResolvedValue(undefined); - config.setThemeSource('dark'); - jest.advanceTimersByTime(lessThanThrottleMs); - config.setThemeSource('light'); - jest.advanceTimersByTime(throttleMs); - expect(persistenceService.writeConfig).toBeCalledTimes(1); - }); - - it('should handle config writing errors gracefully', () => { - mocked(persistenceService.writeConfig).mockRejectedValue(new Error('boo')); - config.setThemeSource('dark'); - jest.advanceTimersByTime(throttleMs); - expect(persistenceService.writeConfig).toBeCalledTimes(1); - }); - - it('should read the config file when it has changed', async () => { - mocked(persistenceService.readConfig).mockResolvedValueOnce({ - found: true, - data: { - themeSource: 'dark', - }, - }); - await configChangedCallback(); - // Do not write back the changes we have just read. - expect(persistenceService.writeConfig).not.toBeCalled(); - expect(config.themeSource).toBe('dark'); - }); - - it('should not apply an invalid config file when it has changed', async () => { - mocked(persistenceService.readConfig).mockResolvedValueOnce({ - found: true, - data: { - themeSource: -1, - }, - }); - await configChangedCallback(); - expect(config.themeSource).not.toBe(-1); - }); - - it('should handle config writing errors gracefully', async () => { - mocked(persistenceService.readConfig).mockRejectedValue(new Error('boo')); - await configChangedCallback(); - }); - - describe('when it was disposed', () => { - beforeEach(() => { - sutDisposer(); - }); - - it('should dispose the watcher', () => { - expect(watcherDisposer).toBeCalled(); - }); - - it('should not listen to store changes any more', () => { - config.setThemeSource('dark'); - jest.advanceTimersByTime(2 * throttleMs); - expect(persistenceService.writeConfig).not.toBeCalled(); - }); - }); -}); diff --git a/packages/main/src/controllers/__tests__/initConfig.spec.ts b/packages/main/src/controllers/__tests__/initConfig.spec.ts new file mode 100644 index 0000000..e386a07 --- /dev/null +++ b/packages/main/src/controllers/__tests__/initConfig.spec.ts @@ -0,0 +1,185 @@ +/* + * Copyright (C) 2021-2022 Kristóf Marussy + * + * This file is part of Sophie. + * + * Sophie is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + * SPDX-License-Identifier: AGPL-3.0-only + */ + +import { jest } from '@jest/globals'; +import { mocked } from 'jest-mock'; +import ms from 'ms'; + +import type ConfigPersistenceService from '../../services/ConfigPersistenceService'; +import { Config, config as configModel } from '../../stores/Config'; +import type Disposer from '../../utils/Disposer'; +import { silenceLogger } from '../../utils/log'; +import initConfig from '../initConfig'; + +let config: Config; +const persistenceService: ConfigPersistenceService = { + readConfig: jest.fn(), + writeConfig: jest.fn(), + watchConfig: jest.fn(), +}; +const lessThanThrottleMs = ms('0.1s'); +const throttleMs = ms('1s'); + +beforeAll(() => { + jest.useFakeTimers(); + silenceLogger(); +}); + +beforeEach(() => { + config = configModel.create(); +}); + +describe('when initializing', () => { + describe('when there is no config file', () => { + beforeEach(() => { + mocked(persistenceService.readConfig).mockResolvedValueOnce({ + found: false, + }); + }); + + it('should create a new config file', async () => { + await initConfig(config, persistenceService); + expect(persistenceService.writeConfig).toBeCalledTimes(1); + }); + + it('should bail if there is an an error creating the config file', async () => { + mocked(persistenceService.writeConfig).mockRejectedValue(new Error('boo')); + await expect(() => initConfig(config, persistenceService)).rejects.toBeInstanceOf(Error); + }); + }); + + describe('when there is a valid config file', () => { + beforeEach(() => { + mocked(persistenceService.readConfig).mockResolvedValueOnce({ + found: true, + data: { + themeSource: 'dark', + }, + }); + }); + + it('should read the existing config file is there is one', async () => { + await initConfig(config, persistenceService); + expect(persistenceService.writeConfig).not.toBeCalled(); + expect(config.themeSource).toBe('dark'); + }); + + it('should bail if it cannot set up a watcher', async () => { + mocked(persistenceService.watchConfig).mockImplementationOnce(() => { + throw new Error('boo'); + }); + await expect(() => initConfig(config, persistenceService)).rejects.toBeInstanceOf(Error); + }); + }); + + it('should not apply an invalid config file', async () => { + mocked(persistenceService.readConfig).mockResolvedValueOnce({ + found: true, + data: { + themeSource: -1, + }, + }); + await initConfig(config, persistenceService); + expect(config.themeSource).not.toBe(-1); + }); + + it('should bail if it cannot determine whether there is a config file', async () => { + mocked(persistenceService.readConfig).mockRejectedValue(new Error('boo')); + await expect(() => initConfig(config, persistenceService)).rejects.toBeInstanceOf(Error); + }); +}); + +describe('when it has loaded the config', () => { + let sutDisposer: Disposer; + const watcherDisposer: Disposer = jest.fn(); + let configChangedCallback: () => Promise; + + beforeEach(async () => { + mocked(persistenceService.readConfig).mockResolvedValueOnce({ + found: true, + data: {}, + }); + mocked(persistenceService.watchConfig).mockReturnValueOnce(watcherDisposer); + sutDisposer = await initConfig(config, persistenceService, throttleMs); + [[configChangedCallback]] = mocked(persistenceService.watchConfig).mock.calls; + jest.resetAllMocks(); + }); + + it('should throttle saving changes to the config file', () => { + mocked(persistenceService.writeConfig).mockResolvedValue(undefined); + config.setThemeSource('dark'); + jest.advanceTimersByTime(lessThanThrottleMs); + config.setThemeSource('light'); + jest.advanceTimersByTime(throttleMs); + expect(persistenceService.writeConfig).toBeCalledTimes(1); + }); + + it('should handle config writing errors gracefully', () => { + mocked(persistenceService.writeConfig).mockRejectedValue(new Error('boo')); + config.setThemeSource('dark'); + jest.advanceTimersByTime(throttleMs); + expect(persistenceService.writeConfig).toBeCalledTimes(1); + }); + + it('should read the config file when it has changed', async () => { + mocked(persistenceService.readConfig).mockResolvedValueOnce({ + found: true, + data: { + themeSource: 'dark', + }, + }); + await configChangedCallback(); + // Do not write back the changes we have just read. + expect(persistenceService.writeConfig).not.toBeCalled(); + expect(config.themeSource).toBe('dark'); + }); + + it('should not apply an invalid config file when it has changed', async () => { + mocked(persistenceService.readConfig).mockResolvedValueOnce({ + found: true, + data: { + themeSource: -1, + }, + }); + await configChangedCallback(); + expect(config.themeSource).not.toBe(-1); + }); + + it('should handle config writing errors gracefully', async () => { + mocked(persistenceService.readConfig).mockRejectedValue(new Error('boo')); + await configChangedCallback(); + }); + + describe('when it was disposed', () => { + beforeEach(() => { + sutDisposer(); + }); + + it('should dispose the watcher', () => { + expect(watcherDisposer).toBeCalled(); + }); + + it('should not listen to store changes any more', () => { + config.setThemeSource('dark'); + jest.advanceTimersByTime(2 * throttleMs); + expect(persistenceService.writeConfig).not.toBeCalled(); + }); + }); +}); diff --git a/packages/main/src/controllers/__tests__/initNativeTheme.spec.ts b/packages/main/src/controllers/__tests__/initNativeTheme.spec.ts new file mode 100644 index 0000000..bd33f48 --- /dev/null +++ b/packages/main/src/controllers/__tests__/initNativeTheme.spec.ts @@ -0,0 +1,71 @@ +/* + * Copyright (C) 2021-2022 Kristóf Marussy + * + * This file is part of Sophie. + * + * Sophie is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + * SPDX-License-Identifier: AGPL-3.0-only + */ + +import { jest } from '@jest/globals'; +import { mocked } from 'jest-mock'; + +import { createMainStore, MainStore } from '../../stores/MainStore'; +import type Disposer from '../../utils/Disposer'; + +let shouldUseDarkColors = false; + +jest.unstable_mockModule('electron', () => ({ + nativeTheme: { + themeSource: 'system', + get shouldUseDarkColors() { + return shouldUseDarkColors; + }, + on: jest.fn(), + off: jest.fn(), + }, +})); + +const { nativeTheme } = await import('electron'); +const { default: initNativeTheme } = await import('../initNativeTheme'); + +let store: MainStore; +let disposeSut: Disposer; + +beforeEach(() => { + store = createMainStore(); + disposeSut = initNativeTheme(store); +}); + +it('should register a nativeTheme updated listener', () => { + expect(nativeTheme.on).toBeCalledWith('updated', expect.anything()); +}); + +it('should synchronize themeSource changes to the nativeTheme', () => { + store.config.setThemeSource('dark'); + expect(nativeTheme.themeSource).toBe('dark'); +}); + +it('should synchronize shouldUseDarkColors changes to the store', () => { + const listener = mocked(nativeTheme.on).mock.calls.find(([event]) => event === 'updated')![1]; + shouldUseDarkColors = true; + listener(); + expect(store.shared.shouldUseDarkColors).toBe(true); +}); + +it('should remove the listener on dispose', () => { + const listener = mocked(nativeTheme.on).mock.calls.find(([event]) => event === 'updated')![1]; + disposeSut(); + expect(nativeTheme.off).toBeCalledWith('updated', listener); +}); diff --git a/packages/main/src/controllers/__tests__/nativeTheme.spec.ts b/packages/main/src/controllers/__tests__/nativeTheme.spec.ts deleted file mode 100644 index 85d6dd2..0000000 --- a/packages/main/src/controllers/__tests__/nativeTheme.spec.ts +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Copyright (C) 2021-2022 Kristóf Marussy - * - * This file is part of Sophie. - * - * Sophie is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, version 3. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - * SPDX-License-Identifier: AGPL-3.0-only - */ - -import { jest } from '@jest/globals'; -import { mocked } from 'jest-mock'; - -import { createMainStore, MainStore } from '../../stores/MainStore'; -import { Disposer } from '../../utils/disposer'; - -let shouldUseDarkColors = false; - -jest.unstable_mockModule('electron', () => ({ - nativeTheme: { - themeSource: 'system', - get shouldUseDarkColors() { - return shouldUseDarkColors; - }, - on: jest.fn(), - off: jest.fn(), - }, -})); - -const { nativeTheme } = await import('electron'); -const { initNativeTheme } = await import('../nativeTheme'); - -let store: MainStore; -let disposeSut: Disposer; - -beforeEach(() => { - store = createMainStore(); - disposeSut = initNativeTheme(store); -}); - -it('should register a nativeTheme updated listener', () => { - expect(nativeTheme.on).toBeCalledWith('updated', expect.anything()); -}); - -it('should synchronize themeSource changes to the nativeTheme', () => { - store.config.setThemeSource('dark'); - expect(nativeTheme.themeSource).toBe('dark'); -}); - -it('should synchronize shouldUseDarkColors changes to the store', () => { - const listener = mocked(nativeTheme.on).mock.calls.find(([event]) => event === 'updated')![1]; - shouldUseDarkColors = true; - listener(); - expect(store.shared.shouldUseDarkColors).toBe(true); -}); - -it('should remove the listener on dispose', () => { - const listener = mocked(nativeTheme.on).mock.calls.find(([event]) => event === 'updated')![1]; - disposeSut(); - expect(nativeTheme.off).toBeCalledWith('updated', listener); -}); diff --git a/packages/main/src/controllers/config.ts b/packages/main/src/controllers/config.ts deleted file mode 100644 index deaeac2..0000000 --- a/packages/main/src/controllers/config.ts +++ /dev/null @@ -1,90 +0,0 @@ -/* - * Copyright (C) 2021-2022 Kristóf Marussy - * - * This file is part of Sophie. - * - * Sophie is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, version 3. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - * SPDX-License-Identifier: AGPL-3.0-only - */ - -import { debounce } from 'lodash-es'; -import ms from 'ms'; -import { applySnapshot, getSnapshot, onSnapshot } from 'mobx-state-tree'; - -import type { ConfigPersistenceService } from '../services/ConfigPersistenceService.js'; -import type { Config, ConfigSnapshotOut } from '../stores/Config.js'; -import { Disposer } from '../utils/disposer'; -import { getLogger } from '../utils/logging'; - -const DEFAULT_CONFIG_DEBOUNCE_TIME = ms('1s'); - -const log = getLogger('config'); - -export async function initConfig( - config: Config, - persistenceService: ConfigPersistenceService, - debounceTime: number = DEFAULT_CONFIG_DEBOUNCE_TIME, -): Promise { - log.trace('Initializing config controller'); - - let lastSnapshotOnDisk: ConfigSnapshotOut | null = null; - - async function readConfig(): Promise { - const result = await persistenceService.readConfig(); - if (result.found) { - try { - applySnapshot(config, result.data); - lastSnapshotOnDisk = getSnapshot(config); - } catch (err) { - log.error('Failed to apply config snapshot', result.data, err); - } - } - return result.found; - } - - async function writeConfig(): Promise { - const snapshot = getSnapshot(config); - await persistenceService.writeConfig(snapshot); - lastSnapshotOnDisk = snapshot; - } - - if (!await readConfig()) { - log.info('Config file was not found'); - await writeConfig(); - log.info('Created config file'); - } - - const disposeOnSnapshot = onSnapshot(config, debounce((snapshot) => { - // We can compare snapshots by reference, since it is only recreated on store changes. - if (lastSnapshotOnDisk !== snapshot) { - writeConfig().catch((err) => { - log.error('Failed to write config on config change', err); - }); - } - }, debounceTime)); - - const disposeWatcher = persistenceService.watchConfig(async () => { - try { - await readConfig(); - } catch (err) { - log.error('Failed to read config', err); - } - }, debounceTime); - - return () => { - log.trace('Disposing config controller'); - disposeWatcher(); - disposeOnSnapshot(); - }; -} diff --git a/packages/main/src/controllers/initConfig.ts b/packages/main/src/controllers/initConfig.ts new file mode 100644 index 0000000..1d40762 --- /dev/null +++ b/packages/main/src/controllers/initConfig.ts @@ -0,0 +1,90 @@ +/* + * Copyright (C) 2021-2022 Kristóf Marussy + * + * This file is part of Sophie. + * + * Sophie is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + * SPDX-License-Identifier: AGPL-3.0-only + */ + +import { debounce } from 'lodash-es'; +import { applySnapshot, getSnapshot, onSnapshot } from 'mobx-state-tree'; +import ms from 'ms'; + +import type ConfigPersistenceService from '../services/ConfigPersistenceService'; +import type { Config, ConfigSnapshotOut } from '../stores/Config'; +import type Disposer from '../utils/Disposer'; +import { getLogger } from '../utils/log'; + +const DEFAULT_CONFIG_DEBOUNCE_TIME = ms('1s'); + +const log = getLogger('config'); + +export default async function initConfig( + config: Config, + persistenceService: ConfigPersistenceService, + debounceTime: number = DEFAULT_CONFIG_DEBOUNCE_TIME, +): Promise { + log.trace('Initializing config controller'); + + let lastSnapshotOnDisk: ConfigSnapshotOut | null = null; + + async function readConfig(): Promise { + const result = await persistenceService.readConfig(); + if (result.found) { + try { + applySnapshot(config, result.data); + lastSnapshotOnDisk = getSnapshot(config); + } catch (err) { + log.error('Failed to apply config snapshot', result.data, err); + } + } + return result.found; + } + + async function writeConfig(): Promise { + const snapshot = getSnapshot(config); + await persistenceService.writeConfig(snapshot); + lastSnapshotOnDisk = snapshot; + } + + if (!await readConfig()) { + log.info('Config file was not found'); + await writeConfig(); + log.info('Created config file'); + } + + const disposeOnSnapshot = onSnapshot(config, debounce((snapshot) => { + // We can compare snapshots by reference, since it is only recreated on store changes. + if (lastSnapshotOnDisk !== snapshot) { + writeConfig().catch((err) => { + log.error('Failed to write config on config change', err); + }); + } + }, debounceTime)); + + const disposeWatcher = persistenceService.watchConfig(async () => { + try { + await readConfig(); + } catch (err) { + log.error('Failed to read config', err); + } + }, debounceTime); + + return () => { + log.trace('Disposing config controller'); + disposeWatcher(); + disposeOnSnapshot(); + }; +} diff --git a/packages/main/src/controllers/initNativeTheme.ts b/packages/main/src/controllers/initNativeTheme.ts new file mode 100644 index 0000000..d2074ab --- /dev/null +++ b/packages/main/src/controllers/initNativeTheme.ts @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2021-2022 Kristóf Marussy + * + * This file is part of Sophie. + * + * Sophie is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, version 3. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + * SPDX-License-Identifier: AGPL-3.0-only + */ + +import { nativeTheme } from 'electron'; +import { autorun } from 'mobx'; + +import type { MainStore } from '../stores/MainStore'; +import type Disposer from '../utils/Disposer'; +import { getLogger } from '../utils/log'; + +const log = getLogger('nativeTheme'); + +export default function initNativeTheme(store: MainStore): Disposer { + log.trace('Initializing nativeTheme controller'); + + const disposeThemeSourceReaction = autorun(() => { + nativeTheme.themeSource = store.config.themeSource; + log.debug('Set theme source:', store.config.themeSource); + }); + + store.setShouldUseDarkColors(nativeTheme.shouldUseDarkColors); + const shouldUseDarkColorsListener = () => { + store.setShouldUseDarkColors(nativeTheme.shouldUseDarkColors); + log.debug('Set should use dark colors:', nativeTheme.shouldUseDarkColors); + }; + nativeTheme.on('updated', shouldUseDarkColorsListener); + + return () => { + log.trace('Disposing nativeTheme controller'); + nativeTheme.off('updated', shouldUseDarkColorsListener); + disposeThemeSourceReaction(); + }; +} diff --git a/packages/main/src/controllers/nativeTheme.ts b/packages/main/src/controllers/nativeTheme.ts deleted file mode 100644 index ccd12d8..0000000 --- a/packages/main/src/controllers/nativeTheme.ts +++ /dev/null @@ -1,50 +0,0 @@ -/* - * Copyright (C) 2021-2022 Kristóf Marussy - * - * This file is part of Sophie. - * - * Sophie is free software: you can redistribute it and/or modify - * it under the terms of the GNU Affero General Public License as - * published by the Free Software Foundation, version 3. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Affero General Public License for more details. - * - * You should have received a copy of the GNU Affero General Public License - * along with this program. If not, see . - * - * SPDX-License-Identifier: AGPL-3.0-only - */ - -import { nativeTheme } from 'electron'; -import { autorun } from 'mobx'; - -import type { MainStore } from '../stores/MainStore.js'; -import { Disposer } from '../utils/disposer'; -import { getLogger } from '../utils/logging'; - -const log = getLogger('nativeTheme'); - -export function initNativeTheme(store: MainStore): Disposer { - log.trace('Initializing nativeTheme controller'); - - const disposeThemeSourceReaction = autorun(() => { - nativeTheme.themeSource = store.config.themeSource; - log.debug('Set theme source:', store.config.themeSource); - }); - - store.setShouldUseDarkColors(nativeTheme.shouldUseDarkColors); - const shouldUseDarkColorsListener = () => { - store.setShouldUseDarkColors(nativeTheme.shouldUseDarkColors); - log.debug('Set should use dark colors:', nativeTheme.shouldUseDarkColors); - }; - nativeTheme.on('updated', shouldUseDarkColorsListener); - - return () => { - log.trace('Disposing nativeTheme controller'); - nativeTheme.off('updated', shouldUseDarkColorsListener); - disposeThemeSourceReaction(); - }; -} -- cgit v1.2.3-54-g00ecf