Get native theme through IPC not remote
This commit is contained in:
parent
d2cc8e5aa9
commit
71572db7a9
16 changed files with 227 additions and 60 deletions
48
ts/context/NativeThemeListener.ts
Normal file
48
ts/context/NativeThemeListener.ts
Normal file
|
@ -0,0 +1,48 @@
|
|||
// Copyright 2021 Signal Messenger, LLC
|
||||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
/* eslint-disable no-restricted-syntax */
|
||||
|
||||
import { NativeThemeState } from '../types/NativeThemeNotifier.d';
|
||||
|
||||
export type Callback = (change: NativeThemeState) => void;
|
||||
|
||||
export interface MinimalIPC {
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
sendSync(channel: string): any;
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
on(channel: string, listener: (event: unknown, ...args: any[]) => void): this;
|
||||
}
|
||||
|
||||
export type SystemThemeHolder = { systemTheme: 'dark' | 'light' };
|
||||
|
||||
export class NativeThemeListener {
|
||||
private readonly subscribers = new Array<Callback>();
|
||||
|
||||
public theme: NativeThemeState;
|
||||
|
||||
constructor(ipc: MinimalIPC, private readonly holder: SystemThemeHolder) {
|
||||
this.theme = ipc.sendSync('native-theme:init');
|
||||
this.update();
|
||||
|
||||
ipc.on(
|
||||
'native-theme:changed',
|
||||
(_event: unknown, change: NativeThemeState) => {
|
||||
this.theme = change;
|
||||
this.update();
|
||||
|
||||
for (const fn of this.subscribers) {
|
||||
fn(change);
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
public subscribe(fn: Callback): void {
|
||||
this.subscribers.push(fn);
|
||||
}
|
||||
|
||||
private update(): void {
|
||||
this.holder.systemTheme = this.theme.shouldUseDarkColors ? 'dark' : 'light';
|
||||
}
|
||||
}
|
|
@ -2,7 +2,14 @@
|
|||
// SPDX-License-Identifier: AGPL-3.0-only
|
||||
|
||||
import { Bytes } from './Bytes';
|
||||
import { NativeThemeListener, MinimalIPC } from './NativeThemeListener';
|
||||
|
||||
export class Context {
|
||||
public readonly bytes = new Bytes();
|
||||
|
||||
public readonly nativeThemeListener;
|
||||
|
||||
constructor(ipc: MinimalIPC) {
|
||||
this.nativeThemeListener = new NativeThemeListener(ipc, window);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue