1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00
lens/src/main/tray/tray-icon-updater.injectable.ts
Sebastian Malton 60498bbd4a Make initTray injectable
Signed-off-by: Sebastian Malton <sebastian@malton.name>
2022-05-03 14:41:43 -04:00

45 lines
1.5 KiB
TypeScript

/**
* Copyright (c) OpenLens Authors. All rights reserved.
* Licensed under MIT License. See LICENSE in root directory for more information.
*/
import { getInjectable } from "@ogre-tools/injectable";
import type { Tray } from "electron";
import type { Disposer } from "../../common/utils";
import nativeThemeInjectable from "../electron/native-theme.injectable";
import createCurrentTrayIconInjectable from "./create-current-tray-icon.injectable";
export type TrayIconUpdater = (tray: Tray) => Disposer;
const trayIconUpdaterInjectable = getInjectable({
id: "tray-icon-updater",
instantiate: (di): TrayIconUpdater => {
const nativeTheme = di.inject(nativeThemeInjectable);
const createCurrentTrayIcon = di.inject(createCurrentTrayIconInjectable);
return (tray) => {
let prevShouldUseDarkColors = nativeTheme.shouldUseDarkColors;
const onUpdated = () => {
if (prevShouldUseDarkColors !== nativeTheme.shouldUseDarkColors) {
const localShouldUseDarkColors = prevShouldUseDarkColors = nativeTheme.shouldUseDarkColors;
createCurrentTrayIcon()
.then(img => {
// This guards against rapid changes back and forth
if (localShouldUseDarkColors === prevShouldUseDarkColors) {
tray.setImage(img);
}
});
}
};
nativeTheme.on("updated", onUpdated);
return () => {
nativeTheme.off("updated", onUpdated);
};
};
},
});
export default trayIconUpdaterInjectable;