Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash when changing os theme kind #5354

Merged
merged 2 commits into from
May 11, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Fix crash when changing os theme kind
Signed-off-by: Sebastian Malton <sebastian@malton.name>
  • Loading branch information
Nokel81 committed May 9, 2022
commit 0a02760734dcf4f427a2099ee7ef571ff773c316
51 changes: 22 additions & 29 deletions src/renderer/theme.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,33 @@ export class ThemeStore extends Singleton {
"lens-light": lensLightThemeJson as Theme,
});

@observable osNativeTheme: "dark" | "light" | undefined;
@observable osNativeThemeType: "dark" | "light" | undefined;

@computed get activeThemeId(): ThemeId {
@computed private get colorThemePreference(): ThemeId | "system" {
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;
}

@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;

return Object
Expand All @@ -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() {
super();

Expand All @@ -93,8 +97,10 @@ export class ThemeStore extends Singleton {
}

async init() {
await this.setNativeTheme();
this.bindNativeThemeUpdateEvent();
this.osNativeThemeType = await ipcRenderer.invoke(getNativeThemeChannel);
ipcRenderer.on(setNativeThemeChannel, (event, theme: "dark" | "light") => {
this.osNativeThemeType = theme;
});

// auto-apply active theme
reaction(() => ({
Expand All @@ -113,25 +119,12 @@ export class ThemeStore extends Singleton {
});
}

bindNativeThemeUpdateEvent() {
ipcRenderer.on(setNativeThemeChannel, (event, theme: "dark" | "light") => {
this.osNativeTheme = theme;
this.applyTheme(theme);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This call was the main source of the crash because it treated "dark" as a ThemeId

});
}

async setNativeTheme() {
const theme: "dark" | "light" = await ipcRenderer.invoke(getNativeThemeChannel);

this.osNativeTheme = theme;
}

getThemeById(themeId: ThemeId): Theme {
return this.themes.get(themeId);
}

protected applyTheme(themeId: ThemeId) {
const theme = this.systemTheme ?? this.getThemeById(themeId);
const theme = this.getThemeById(themeId);

const colors = Object.entries({
...theme.colors,
Expand Down