Skip to content

Commit

Permalink
feat: set window background to dark
Browse files Browse the repository at this point in the history
  • Loading branch information
linonetwo committed Apr 13, 2022
1 parent 38026e1 commit 1a71d68
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 5 deletions.
2 changes: 0 additions & 2 deletions src/services/theme/defaultTheme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,3 @@ export const darkTheme = merge(
}),
),
) as Theme;
// DEBUG: console
console.warn(`darkTheme`, JSON.stringify(darkTheme, null, ' '));
11 changes: 8 additions & 3 deletions src/services/theme/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable @typescript-eslint/require-await */
import { BehaviorSubject } from 'rxjs';
import { nativeTheme } from 'electron';
import { injectable } from 'inversify';
Expand All @@ -14,7 +15,7 @@ export class ThemeService implements IThemeService {

constructor() {
void this.init();
this.theme$ = new BehaviorSubject<ITheme>({ shouldUseDarkColors: this.shouldUseDarkColors() });
this.theme$ = new BehaviorSubject<ITheme>({ shouldUseDarkColors: this.shouldUseDarkColorsSync() });
}

private updateThemeSubject(newTheme: ITheme): void {
Expand All @@ -26,11 +27,15 @@ export class ThemeService implements IThemeService {
// apply theme
nativeTheme.themeSource = themeSource;
nativeTheme.addListener('updated', () => {
this.updateThemeSubject({ shouldUseDarkColors: this.shouldUseDarkColors() });
this.updateThemeSubject({ shouldUseDarkColors: this.shouldUseDarkColorsSync() });
});
}

private shouldUseDarkColors(): boolean {
private shouldUseDarkColorsSync(): boolean {
return nativeTheme.shouldUseDarkColors;
}

public async shouldUseDarkColors(): Promise<boolean> {
return this.shouldUseDarkColorsSync();
}
}
1 change: 1 addition & 0 deletions src/services/theme/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface ITheme {
* Wrap call to electron api, so we won't need remote module in renderer process
*/
export interface IThemeService {
shouldUseDarkColors(): Promise<boolean>;
theme$: BehaviorSubject<ITheme>;
}
export const ThemeServiceIPCDescriptor = {
Expand Down
9 changes: 9 additions & 0 deletions src/services/windows/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { isTest } from '@/constants/environment';
import { MENUBAR_ICON_PATH } from '@/constants/paths';
import { getLocalHostUrlWithActualIP } from '@services/libs/url';
import { SETTINGS_FOLDER } from '@/constants/appPaths';
import { IThemeService } from '@services/theme/interface';

@injectable()
export class Window implements IWindowService {
Expand All @@ -37,6 +38,7 @@ export class Window implements IWindowService {
@lazyInject(serviceIdentifier.Workspace) private readonly workspaceService!: IWorkspaceService;
@lazyInject(serviceIdentifier.WorkspaceView) private readonly workspaceViewService!: IWorkspaceViewService;
@lazyInject(serviceIdentifier.MenuService) private readonly menuService!: IMenuService;
@lazyInject(serviceIdentifier.ThemeService) private readonly themeService!: IThemeService;

constructor() {
void this.registerMenu();
Expand Down Expand Up @@ -212,6 +214,7 @@ export class Window implements IWindowService {
});
});
}
await this.updateWindowBackground(newWindow);
// This loading will wait for a while
await newWindow.loadURL(MAIN_WINDOW_WEBPACK_ENTRY);
await webContentLoadingPromise;
Expand Down Expand Up @@ -599,4 +602,10 @@ export class Window implements IWindowService {
});
});
}

private async updateWindowBackground(newWindow: BrowserWindow): Promise<void> {
if (await this.themeService.shouldUseDarkColors()) {
newWindow.setBackgroundColor('#000000');
}
}
}

0 comments on commit 1a71d68

Please sign in to comment.