2021-06-30 11:57:43 -07:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2021-10-26 14:15:33 -05:00
|
|
|
import type { BrowserWindow } from 'electron';
|
|
|
|
import { ipcMain as ipc, nativeTheme } from 'electron';
|
2021-06-30 11:57:43 -07:00
|
|
|
|
2021-10-26 14:15:33 -05:00
|
|
|
import type { NativeThemeState } from '../types/NativeThemeNotifier.d';
|
2021-06-30 11:57:43 -07:00
|
|
|
|
|
|
|
function getState(): NativeThemeState {
|
|
|
|
return {
|
|
|
|
shouldUseDarkColors: nativeTheme.shouldUseDarkColors,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export class NativeThemeNotifier {
|
2025-01-14 11:11:52 -08:00
|
|
|
readonly #listeners = new Set<BrowserWindow>();
|
2021-06-30 11:57:43 -07:00
|
|
|
|
|
|
|
public initialize(): void {
|
|
|
|
nativeTheme.on('updated', () => {
|
2025-01-14 11:11:52 -08:00
|
|
|
this.#notifyListeners();
|
2021-06-30 11:57:43 -07:00
|
|
|
});
|
|
|
|
|
|
|
|
ipc.on('native-theme:init', event => {
|
|
|
|
// eslint-disable-next-line no-param-reassign
|
|
|
|
event.returnValue = getState();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public addWindow(window: BrowserWindow): void {
|
2025-01-14 11:11:52 -08:00
|
|
|
if (this.#listeners.has(window)) {
|
2021-06-30 11:57:43 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2025-01-14 11:11:52 -08:00
|
|
|
this.#listeners.add(window);
|
2021-06-30 11:57:43 -07:00
|
|
|
|
|
|
|
window.once('closed', () => {
|
2025-01-14 11:11:52 -08:00
|
|
|
this.#listeners.delete(window);
|
2021-06-30 11:57:43 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2025-01-14 11:11:52 -08:00
|
|
|
#notifyListeners(): void {
|
|
|
|
for (const window of this.#listeners) {
|
2021-06-30 11:57:43 -07:00
|
|
|
window.webContents.send('native-theme:changed', getState());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|