From beba2c0068e0ae87f79f7b276f5255dfbcc3bb39 Mon Sep 17 00:00:00 2001 From: Alex Andreev Date: Sat, 5 Mar 2022 08:49:17 +0300 Subject: [PATCH] Set nativeTheme ipc handlers and callers Signed-off-by: Alex Andreev --- src/common/ipc/native-theme.ts | 8 ++++++++ src/main/index.ts | 3 +++ .../init-ipc-main-handlers.ts | 8 +++++++- src/main/native-theme.ts | 18 ++++++++++++++++++ 4 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 src/common/ipc/native-theme.ts create mode 100644 src/main/native-theme.ts diff --git a/src/common/ipc/native-theme.ts b/src/common/ipc/native-theme.ts new file mode 100644 index 0000000000..4708a3c9b3 --- /dev/null +++ b/src/common/ipc/native-theme.ts @@ -0,0 +1,8 @@ +/** + * Copyright (c) OpenLens Authors. All rights reserved. + * Licensed under MIT License. See LICENSE in root directory for more information. + */ + + +export const setNativeThemeChannel = "theme:set-native-theme"; +export const getNativeThemeChannel = "theme:get-native-theme"; diff --git a/src/main/index.ts b/src/main/index.ts index bbd8e73599..adb9405eb7 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -55,6 +55,7 @@ import routerInjectable from "./router/router.injectable"; import shellApiRequestInjectable from "./proxy-functions/shell-api-request/shell-api-request.injectable"; import userStoreInjectable from "../common/user-store/user-store.injectable"; import trayMenuItemsInjectable from "./tray/tray-menu-items.injectable"; +import { broadcastNativeThemeOnUpdate } from "./native-theme"; const di = getDi(); @@ -108,6 +109,8 @@ di.runSetups().then(() => { } } + broadcastNativeThemeOnUpdate(); + app.on("second-instance", (event, argv) => { logger.debug("second-instance message"); diff --git a/src/main/initializers/init-ipc-main-handlers/init-ipc-main-handlers.ts b/src/main/initializers/init-ipc-main-handlers/init-ipc-main-handlers.ts index c5b2d1f975..5a907f2449 100644 --- a/src/main/initializers/init-ipc-main-handlers/init-ipc-main-handlers.ts +++ b/src/main/initializers/init-ipc-main-handlers/init-ipc-main-handlers.ts @@ -3,7 +3,7 @@ * Licensed under MIT License. See LICENSE in root directory for more information. */ -import { BrowserWindow, IpcMainInvokeEvent, Menu } from "electron"; +import { BrowserWindow, IpcMainInvokeEvent, Menu, nativeTheme } from "electron"; import { clusterFrameMap } from "../../../common/cluster-frames"; import { clusterActivateHandler, clusterSetFrameIdHandler, clusterVisibilityHandler, clusterRefreshHandler, clusterDisconnectHandler, clusterKubectlApplyAllHandler, clusterKubectlDeleteAllHandler, clusterDeleteHandler, clusterSetDeletingHandler, clusterClearDeletingHandler } from "../../../common/ipc/cluster"; import type { ClusterId } from "../../../common/cluster-types"; @@ -24,6 +24,8 @@ import { onLocationChange, handleWindowAction } from "../../ipc/window"; import { openFilePickingDialogChannel } from "../../../common/ipc/dialog"; import { showOpenDialog } from "../../ipc/dialog"; import { windowActionHandleChannel, windowLocationChangedChannel, windowOpenAppMenuAsContextMenuChannel } from "../../../common/ipc/window"; +import { getNativeColorTheme } from "../../native-theme"; +import { getNativeThemeChannel } from "../../../common/ipc/native-theme"; interface Dependencies { electronMenuItems: IComputedValue; @@ -158,4 +160,8 @@ export const initIpcMainHandlers = ({ electronMenuItems, directoryForLensLocalSt y: 20, }); }); + + ipcMainHandle(getNativeThemeChannel, () => { + return getNativeColorTheme(); + }); }; diff --git a/src/main/native-theme.ts b/src/main/native-theme.ts new file mode 100644 index 0000000000..e729245666 --- /dev/null +++ b/src/main/native-theme.ts @@ -0,0 +1,18 @@ +/** + * Copyright (c) OpenLens Authors. All rights reserved. + * Licensed under MIT License. See LICENSE in root directory for more information. + */ + +import { nativeTheme } from "electron"; +import { broadcastMessage } from "../common/ipc"; +import { setNativeThemeChannel } from "../common/ipc/native-theme"; + +export function broadcastNativeThemeOnUpdate() { + nativeTheme.on("updated", () => { + broadcastMessage(setNativeThemeChannel, getNativeColorTheme()); + }); +} + +export function getNativeColorTheme() { + return nativeTheme.shouldUseDarkColors ? "dark" : "light"; +}