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

Sync theia dark/light theme with electron nativeTheme setting #15037

Merged
Show file tree
Hide file tree
Changes from all commits
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
6 changes: 6 additions & 0 deletions packages/core/src/common/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import { URI } from 'vscode-uri';

export type ThemeType = 'light' | 'dark' | 'hc' | 'hcLight';

export type ThemeMode = 'light' | 'dark';

export interface Theme {
readonly id: string;
readonly type: ThemeType;
Expand All @@ -32,6 +34,10 @@ export function isHighContrast(scheme: ThemeType): boolean {
return scheme === 'hc' || scheme === 'hcLight';
}

export function getThemeMode(type: ThemeType): ThemeMode {
return (type === 'hc' || type === 'dark') ? 'dark' : 'light';
}

export interface ThemeChangeEvent {
readonly newTheme: Theme;
readonly oldTheme?: Theme;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { WindowTitleService } from '../../browser/window/window-title-service';

import '../../../src/electron-browser/menu/electron-menu-style.css';
import { ThemeService } from '../../browser/theming';
import { ThemeChangeEvent } from '../../common/theme';
import { getThemeMode, ThemeChangeEvent } from '../../common/theme';

export namespace ElectronCommands {
export const TOGGLE_DEVELOPER_TOOLS = Command.toDefaultLocalizedCommand({
Expand Down Expand Up @@ -424,6 +424,7 @@ export class ElectronMenuContribution extends BrowserMenuBarContribution impleme
protected handleThemeChange(e: ThemeChangeEvent): void {
const backgroundColor = window.getComputedStyle(document.body).backgroundColor;
window.electronTheiaCore.setBackgroundColor(backgroundColor);
window.electronTheiaCore.setTheme(getThemeMode(e.newTheme.type));
}

}
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/electron-browser/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import {
CHANNEL_REQUEST_RELOAD, CHANNEL_APP_STATE_CHANGED, CHANNEL_SHOW_ITEM_IN_FOLDER, CHANNEL_READ_CLIPBOARD, CHANNEL_WRITE_CLIPBOARD,
CHANNEL_KEYBOARD_LAYOUT_CHANGED, CHANNEL_IPC_CONNECTION, InternalMenuDto, CHANNEL_REQUEST_SECONDARY_CLOSE, CHANNEL_SET_BACKGROUND_COLOR,
CHANNEL_WC_METADATA, CHANNEL_ABOUT_TO_CLOSE, CHANNEL_OPEN_WITH_SYSTEM_APP,
CHANNEL_OPEN_URL
CHANNEL_OPEN_URL, CHANNEL_SET_THEME
} from '../electron-common/electron-api';

// eslint-disable-next-line import/no-extraneous-dependencies
Expand Down Expand Up @@ -120,6 +120,9 @@ const api: TheiaCoreAPI = {
setBackgroundColor: function (backgroundColor): void {
ipcRenderer.send(CHANNEL_SET_BACKGROUND_COLOR, backgroundColor);
},
setTheme: function (theme): void {
ipcRenderer.send(CHANNEL_SET_THEME, theme);
},
minimize: function (): void {
ipcRenderer.send(CHANNEL_MINIMIZE);
},
Expand Down
3 changes: 3 additions & 0 deletions packages/core/src/electron-common/electron-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import { NativeKeyboardLayout } from '../common/keyboard/keyboard-layout-provider';
import { Disposable } from '../common';
import { FrontendApplicationState, StopReason } from '../common/frontend-application-state';
import { ThemeMode } from '../common/theme';

export type MenuRole = ('undo' | 'redo' | 'cut' | 'copy' | 'paste' | 'selectAll' | 'about' | 'services' | 'hide' | 'hideOthers' | 'unhide' | 'quit');

Expand Down Expand Up @@ -65,6 +66,7 @@ export interface TheiaCoreAPI {
getTitleBarStyleAtStartup(): Promise<string>;
setTitleBarStyle(style: string): void;
setBackgroundColor(backgroundColor: string): void;
setTheme(theme: ThemeMode): void;
minimize(): void;
isMaximized(): boolean; // TODO: this should really be async, since it blocks the renderer process
maximize(): void;
Expand Down Expand Up @@ -125,6 +127,7 @@ export const CHANNEL_ATTACH_SECURITY_TOKEN = 'AttachSecurityToken';
export const CHANNEL_GET_TITLE_STYLE_AT_STARTUP = 'GetTitleStyleAtStartup';
export const CHANNEL_SET_TITLE_STYLE = 'SetTitleStyle';
export const CHANNEL_SET_BACKGROUND_COLOR = 'SetBackgroundColor';
export const CHANNEL_SET_THEME = 'SetTheme';
export const CHANNEL_CLOSE = 'Close';
export const CHANNEL_MINIMIZE = 'Minimize';
export const CHANNEL_MAXIMIZE = 'Maximize';
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/electron-main/electron-api-main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ import {
CHANNEL_WC_METADATA,
CHANNEL_ABOUT_TO_CLOSE,
CHANNEL_OPEN_WITH_SYSTEM_APP,
CHANNEL_OPEN_URL
CHANNEL_OPEN_URL,
CHANNEL_SET_THEME
} from '../electron-common/electron-api';
import { ElectronMainApplication, ElectronMainApplicationContribution } from './electron-main-application';
import { Disposable, DisposableCollection, isOSX, MaybePromise } from '../common';
Expand Down Expand Up @@ -176,6 +177,8 @@ export class TheiaMainApi implements ElectronMainApplicationContribution {

ipcMain.on(CHANNEL_SET_BACKGROUND_COLOR, (event, backgroundColor) => application.setBackgroundColor(event.sender, backgroundColor));

ipcMain.on(CHANNEL_SET_THEME, (event, theme) => application.setTheme(theme));

ipcMain.on(CHANNEL_MINIMIZE, event => {
BrowserWindow.fromWebContents(event.sender)?.minimize();
});
Expand Down
5 changes: 5 additions & 0 deletions packages/core/src/electron-main/electron-main-application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import { createDisposableListener } from './event-utils';
import { TheiaRendererAPI } from './electron-api-main';
import { StopReason } from '../common/frontend-application-state';
import { dynamicRequire } from '../node/dynamic-require';
import { ThemeMode } from '../common/theme';

export { ElectronMainApplicationGlobals };

Expand Down Expand Up @@ -277,6 +278,10 @@ export class ElectronMainApplication {
this.saveState(webContents);
}

public setTheme(theme: ThemeMode): void {
nativeTheme.themeSource = theme;
}

protected saveState(webContents: Electron.WebContents): void {
const browserWindow = BrowserWindow.fromWebContents(webContents);
if (browserWindow) {
Expand Down
Loading