1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

Fix crash when changing os theme kind (#5354)

This commit is contained in:
Sebastian Malton 2022-05-11 09:07:51 -04:00 committed by GitHub
parent 7a8a734957
commit 42e7daf057
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -36,21 +36,33 @@ export class ThemeStore extends Singleton {
"lens-light": lensLightThemeJson as Theme, "lens-light": lensLightThemeJson as Theme,
}); });
@observable osNativeTheme: "dark" | "light" | undefined; @observable private osNativeThemeType: "dark" | "light" | undefined;
@computed get activeThemeId(): ThemeId { @computed private get colorThemePreference(): ThemeId | "system" {
return UserStore.getInstance().colorTheme; return UserStore.getInstance().colorTheme;
} }
@computed get terminalThemeId(): ThemeId { @computed private get activeThemeId(): ThemeId {
if (this.colorThemePreference === "system") {
if (this.osNativeThemeType) {
return `lens-${this.osNativeThemeType}`;
} else {
return defaultTheme;
}
} else {
return this.colorThemePreference;
}
}
@computed private get terminalThemeId(): ThemeId {
return UserStore.getInstance().terminalTheme; return UserStore.getInstance().terminalTheme;
} }
@computed get activeTheme(): Theme { @computed get activeTheme(): Theme {
return this.systemTheme ?? this.themes.get(this.activeThemeId) ?? this.themes.get(defaultTheme); return this.themes.get(this.activeThemeId) ?? this.themes.get(defaultTheme);
} }
@computed get terminalColors(): [string, string][] { @computed private get terminalColors(): [string, string][] {
const theme = this.themes.get(this.terminalThemeId) ?? this.activeTheme; const theme = this.themes.get(this.terminalThemeId) ?? this.activeTheme;
return Object return Object
@ -76,14 +88,6 @@ export class ThemeStore extends Singleton {
})); }));
} }
@computed get systemTheme() {
if (this.activeThemeId == "system" && this.osNativeTheme) {
return this.themes.get(`lens-${this.osNativeTheme}`);
}
return null;
}
constructor() { constructor() {
super(); super();
@ -93,8 +97,10 @@ export class ThemeStore extends Singleton {
} }
async init() { async init() {
await this.setNativeTheme(); this.osNativeThemeType = await ipcRenderer.invoke(getNativeThemeChannel);
this.bindNativeThemeUpdateEvent(); ipcRenderer.on(setNativeThemeChannel, (event, theme: "dark" | "light") => {
this.osNativeThemeType = theme;
});
// auto-apply active theme // auto-apply active theme
reaction(() => ({ reaction(() => ({
@ -113,25 +119,12 @@ export class ThemeStore extends Singleton {
}); });
} }
bindNativeThemeUpdateEvent() {
ipcRenderer.on(setNativeThemeChannel, (event, theme: "dark" | "light") => {
this.osNativeTheme = theme;
this.applyTheme(theme);
});
}
async setNativeTheme() {
const theme: "dark" | "light" = await ipcRenderer.invoke(getNativeThemeChannel);
this.osNativeTheme = theme;
}
getThemeById(themeId: ThemeId): Theme { getThemeById(themeId: ThemeId): Theme {
return this.themes.get(themeId); return this.themes.get(themeId);
} }
protected applyTheme(themeId: ThemeId) { protected applyTheme(themeId: ThemeId) {
const theme = this.systemTheme ?? this.getThemeById(themeId); const theme = this.getThemeById(themeId);
const colors = Object.entries({ const colors = Object.entries({
...theme.colors, ...theme.colors,