From cbfb5688e9dca15bb524c0d3184f4210d4b4f46f Mon Sep 17 00:00:00 2001 From: Emery Date: Fri, 25 Mar 2022 00:45:02 +0800 Subject: [PATCH] Add Linux tray menu; Fix AppImage APP icon --- src/main/ui/tray/index.ts | 107 +++++++++++++++++++++++++++++-------- src/main/ui/tray/window.ts | 8 +++ 2 files changed, 92 insertions(+), 23 deletions(-) diff --git a/src/main/ui/tray/index.ts b/src/main/ui/tray/index.ts index 303af9423..d29c6b893 100644 --- a/src/main/ui/tray/index.ts +++ b/src/main/ui/tray/index.ts @@ -36,33 +36,50 @@ const makeTray = async () => { tray.setToolTip('SwitchHosts') - tray.on('click', () => { - if (!win) { - makeWindow() - return - } + let locale = await configGet('locale') + if (process.platform === 'linux') { + locale = global.system_locale // configGet() always get undefined on Linux + } + const i18n = new I18N(locale) + const { lang } = i18n - if (win.isVisible()) { - if (win.isFocused()) { - win.hide() - } else { - show() - win.focus() - } - } else { - show() - } - }) + const ver = version.slice(0, 3).join('.') + ` (${version[3]})` - tray.on('double-click', () => broadcast(events.active_main_window)) + if (process.platform === 'linux') { + const menu = Menu.buildFromTemplate([ + { + label: lang.click_to_open, + click: () => window(), + }, + { type: 'separator' }, + { + label: lang._app_name, + toolTip: lang.show_main_window, + click: () => { + broadcast(events.active_main_window) + }, + }, + { + label: `v${ver}`, + enabled: false, + }, + { type: 'separator' }, + { + label: lang.quit, + role: 'quit', + }, + ]) - tray.on('right-click', async () => { - let locale = await configGet('locale') - const i18n = new I18N(locale) - const { lang } = i18n + // Linux requires setContextMenu to be called in order for the context menu to populate correctly + tray.setContextMenu(menu) + return + } - const ver = version.slice(0, 3).join('.') + ` (${version[3]})` + tray.on('click', () => window()) + tray.on('double-click', () => broadcast(events.active_main_window)) + + tray.on('right-click', async () => { const menu = Menu.buildFromTemplate([ { label: lang._app_name, @@ -139,8 +156,52 @@ const getPosition = () => { return { x, y } } +const getLinuxPosition = () => { + const window_bounds = win.getBounds() + const point = screen.getCursorScreenPoint() + const screen_bounds0 = screen.getDisplayNearestPoint(point).bounds + const screen_bounds = screen.getDisplayNearestPoint(point).workAreaSize + + let x: number + let y: number + + if (point.x - screen_bounds0.x > screen_bounds.width / 2) { // display on the right of the active screen + x = screen_bounds0.x + screen_bounds0.width - window_bounds.width + } else { + x = 0 + } + if (point.y < screen_bounds.height / 2) { // display on the top of the active screen + y = 0 + } else { + y = screen_bounds.height - window_bounds.height + } + + x = Math.round(x) + y = Math.round(y) + + return {x, y} +} + +const window = () => { + if (!win) { + makeWindow() + return + } + + if (win.isVisible()) { + if (win.isFocused()) { + win.hide() + } else { + show() + win.focus() + } + } else { + show() + } +} + const show = () => { - const { x, y } = getPosition() + let {x, y} = process.platform === 'linux' ? getLinuxPosition(): getPosition() win.setPosition(x, y, true) win.show() // win.focus() diff --git a/src/main/ui/tray/window.ts b/src/main/ui/tray/window.ts index 8192a6d55..91d0fd670 100644 --- a/src/main/ui/tray/window.ts +++ b/src/main/ui/tray/window.ts @@ -11,6 +11,13 @@ import path from 'path' const makeWindow = () => { let win: BrowserWindow | null + // Linux AppImage APP can't automatically recognize dock icon, requires special configuration to display correctly + let linux_icon = {} + if (process.platform === 'linux') { + linux_icon = { + icon: path.join(__dirname, '/assets/icon.png'), + } + } win = new BrowserWindow({ frame: false, // titleBarStyle: 'hidden', @@ -30,6 +37,7 @@ const makeWindow = () => { preload: path.join(__dirname, 'preload.js'), spellcheck: true, }, + ...linux_icon, }) win.setVisibleOnAllWorkspaces(true, {