mirror of
https://github.com/lensapp/lens.git
synced 2025-05-20 05:10:56 +00:00
Improve tray initialization and logging (#2048)
- Remove all async click handlers as the typing of the expected method is that of `void` so it doesn't know how to handle `Promise<void>` - Replace the above with promise chains with correctly handle any errors by logging them - Fix the tray menu update logic to actually produce a new menu when the reaction is run - Improve the Clusters submenu generation to involve less work and to utilize the built-in checkmark rendering Signed-off-by: Sebastian Malton <sebastian@malton.name>
This commit is contained in:
parent
503a49261e
commit
0e307e65b6
120
src/main/tray.ts
120
src/main/tray.ts
@ -1,6 +1,6 @@
|
|||||||
import path from "path";
|
import path from "path";
|
||||||
import packageInfo from "../../package.json";
|
import packageInfo from "../../package.json";
|
||||||
import { Menu, NativeImage, Tray } from "electron";
|
import { Menu, Tray } from "electron";
|
||||||
import { autorun } from "mobx";
|
import { autorun } from "mobx";
|
||||||
import { showAbout } from "./menu";
|
import { showAbout } from "./menu";
|
||||||
import { checkForUpdates } from "./app-updater";
|
import { checkForUpdates } from "./app-updater";
|
||||||
@ -13,6 +13,8 @@ import logger from "./logger";
|
|||||||
import { isDevelopment, isWindows } from "../common/vars";
|
import { isDevelopment, isWindows } from "../common/vars";
|
||||||
import { exitApp } from "./exit-app";
|
import { exitApp } from "./exit-app";
|
||||||
|
|
||||||
|
const TRAY_LOG_PREFIX = "[TRAY]";
|
||||||
|
|
||||||
// note: instance of Tray should be saved somewhere, otherwise it disappears
|
// note: instance of Tray should be saved somewhere, otherwise it disappears
|
||||||
export let tray: Tray;
|
export let tray: Tray;
|
||||||
|
|
||||||
@ -25,94 +27,92 @@ export function getTrayIcon(): string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function initTray(windowManager: WindowManager) {
|
export function initTray(windowManager: WindowManager) {
|
||||||
const dispose = autorun(() => {
|
const icon = getTrayIcon();
|
||||||
try {
|
|
||||||
const menu = createTrayMenu(windowManager);
|
|
||||||
|
|
||||||
buildTray(getTrayIcon(), menu, windowManager);
|
tray = new Tray(icon);
|
||||||
} catch (err) {
|
tray.setToolTip(packageInfo.description);
|
||||||
logger.error(`[TRAY]: building failed: ${err}`);
|
tray.setIgnoreDoubleClickEvents(true);
|
||||||
}
|
|
||||||
});
|
if (isWindows) {
|
||||||
|
tray.on("click", () => {
|
||||||
|
windowManager
|
||||||
|
.ensureMainWindow()
|
||||||
|
.catch(error => logger.error(`${TRAY_LOG_PREFIX}: Failed to open lens`, { error }));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const disposers = [
|
||||||
|
autorun(() => {
|
||||||
|
try {
|
||||||
|
const menu = createTrayMenu(windowManager);
|
||||||
|
|
||||||
|
tray.setContextMenu(menu);
|
||||||
|
} catch (error) {
|
||||||
|
logger.error(`${TRAY_LOG_PREFIX}: building failed`, { error });
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
];
|
||||||
|
|
||||||
return () => {
|
return () => {
|
||||||
dispose();
|
disposers.forEach(disposer => disposer());
|
||||||
tray?.destroy();
|
tray?.destroy();
|
||||||
tray = null;
|
tray = null;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
function buildTray(icon: string | NativeImage, menu: Menu, windowManager: WindowManager) {
|
|
||||||
if (!tray) {
|
|
||||||
tray = new Tray(icon);
|
|
||||||
tray.setToolTip(packageInfo.description);
|
|
||||||
tray.setIgnoreDoubleClickEvents(true);
|
|
||||||
tray.setImage(icon);
|
|
||||||
tray.setContextMenu(menu);
|
|
||||||
|
|
||||||
if (isWindows) {
|
|
||||||
tray.on("click", () => {
|
|
||||||
windowManager.ensureMainWindow();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return tray;
|
|
||||||
}
|
|
||||||
|
|
||||||
function createTrayMenu(windowManager: WindowManager): Menu {
|
function createTrayMenu(windowManager: WindowManager): Menu {
|
||||||
return Menu.buildFromTemplate([
|
return Menu.buildFromTemplate([
|
||||||
{
|
{
|
||||||
label: "Open Lens",
|
label: "Open Lens",
|
||||||
async click() {
|
click() {
|
||||||
await windowManager.ensureMainWindow();
|
windowManager
|
||||||
|
.ensureMainWindow()
|
||||||
|
.catch(error => logger.error(`${TRAY_LOG_PREFIX}: Failed to open lens`, { error }));
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
label: "Preferences",
|
label: "Preferences",
|
||||||
click() {
|
click() {
|
||||||
windowManager.navigate(preferencesURL());
|
windowManager
|
||||||
|
.navigate(preferencesURL())
|
||||||
|
.catch(error => logger.error(`${TRAY_LOG_PREFIX}: Failed to nativate to Preferences`, { error }));
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
label: "Clusters",
|
label: "Clusters",
|
||||||
submenu: workspaceStore.enabledWorkspacesList
|
submenu: workspaceStore.enabledWorkspacesList
|
||||||
.filter(workspace => clusterStore.getByWorkspaceId(workspace.id).length > 0) // hide empty workspaces
|
.map(workspace => [workspace, clusterStore.getByWorkspaceId(workspace.id)] as const)
|
||||||
.map(workspace => {
|
.map(([workspace, clusters]) => ({
|
||||||
const clusters = clusterStore.getByWorkspaceId(workspace.id);
|
label: workspace.name,
|
||||||
|
toolTip: workspace.description,
|
||||||
return {
|
enabled: clusters.length > 0,
|
||||||
label: workspace.name,
|
submenu: clusters.map(({ id: clusterId, name: label, online, workspace }) => ({
|
||||||
toolTip: workspace.description,
|
checked: online,
|
||||||
submenu: clusters.map(cluster => {
|
type: "checkbox",
|
||||||
const { id: clusterId, name: label, online, workspace } = cluster;
|
label,
|
||||||
|
toolTip: clusterId,
|
||||||
return {
|
click() {
|
||||||
label: `${online ? "✓" : "\x20".repeat(3)/*offset*/}${label}`,
|
workspaceStore.setActive(workspace);
|
||||||
toolTip: clusterId,
|
windowManager
|
||||||
async click() {
|
.navigate(clusterViewURL({ params: { clusterId } }))
|
||||||
workspaceStore.setActive(workspace);
|
.catch(error => logger.error(`${TRAY_LOG_PREFIX}: Failed to nativate to cluster`, { clusterId, error }));
|
||||||
windowManager.navigate(clusterViewURL({ params: { clusterId } }));
|
}
|
||||||
}
|
}))
|
||||||
};
|
})),
|
||||||
})
|
|
||||||
};
|
|
||||||
}),
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
label: "Check for updates",
|
label: "Check for updates",
|
||||||
async click() {
|
click() {
|
||||||
await checkForUpdates();
|
checkForUpdates()
|
||||||
await windowManager.ensureMainWindow();
|
.then(() => windowManager.ensureMainWindow());
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
label: "About Lens",
|
label: "About Lens",
|
||||||
async click() {
|
click() {
|
||||||
// note: argument[1] (browserWindow) not available when app is not focused / hidden
|
windowManager.ensureMainWindow()
|
||||||
const browserWindow = await windowManager.ensureMainWindow();
|
.then(showAbout)
|
||||||
|
.catch(error => logger.error(`${TRAY_LOG_PREFIX}: Failed to show Lens About view`, { error }));
|
||||||
showAbout(browserWindow);
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{ type: "separator" },
|
{ type: "separator" },
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user