From 1fc3ffa6935339b49c9ec8cd4264af6d954ff72b Mon Sep 17 00:00:00 2001 From: LudoO Date: Sun, 23 Feb 2025 15:21:51 +0100 Subject: [PATCH] feat: review tray options --- src/main/MainRunner.ts | 46 +++++++++++++++-------------- src/main/desktop.ts | 17 ----------- src/main/tray.ts | 58 ++++++++++++++----------------------- src/main/utils/Constants.ts | 20 +++++++++++++ 4 files changed, 67 insertions(+), 74 deletions(-) delete mode 100644 src/main/desktop.ts diff --git a/src/main/MainRunner.ts b/src/main/MainRunner.ts index 8477c5b..3667661 100644 --- a/src/main/MainRunner.ts +++ b/src/main/MainRunner.ts @@ -1,20 +1,15 @@ import { app, BrowserWindow, RenderProcessGoneDetails, BrowserWindowConstructorOptions } from 'electron' -import Constants from './utils/Constants' +import Constants, { TrayOptions } from './utils/Constants' import IPCs from './IPCs' -import { createTray, setWindowAutoHide, showWindow } from './tray.ts' +import { createTray, hideWindow, showWindow } from './tray.ts' const options = { - tray: true, - trayWindow: false, // true, to use a tray floating window attached to top try icon width: Constants.IS_DEV_ENV ? 1500 : 1200, height: 650, - trayOptions: { - menu: false, - tooltip: 'Vutron App', - margin: {x:0, y:0}, - width: Constants.IS_DEV_ENV ? 800 : 600, - height: 650, - showAtStartup: false + tray: { + // all optional values from DEFAULT_TRAY_OPTIONS can de defined here + enabled: true, + trayWindow: false // true, to use a tray floating window attached to top try icon } } @@ -36,13 +31,22 @@ export const createMainWindow = async (): Promise => { webPreferences: Constants.DEFAULT_WEB_PREFERENCES, frame: true } - if (options.trayWindow){ + const trayOptions: TrayOptions = (options.tray?.enabled) ? { + ...Constants.DEFAULT_TRAY_OPTIONS, + ...options.tray + }: { + ...Constants.DEFAULT_TRAY_OPTIONS, + enabled: false + } + + // trayWindow requires tray.enabled=true + if (trayOptions.enabled && trayOptions.trayWindow){ opt = { ...opt, - width: options.trayOptions.width , - height: options.trayOptions.height, - maxWidth: options.trayOptions.width, - maxHeight: options.trayOptions.height, + width: options.width , + height: options.height, + maxWidth: options.width, + maxHeight: options.height, show: false, frame: false, fullscreenable: false, @@ -71,13 +75,13 @@ export const createMainWindow = async (): Promise => { } }) - if (options.tray) { - createTray(mainWindow, options.trayWindow, options.trayOptions); + if (trayOptions.enabled) { + createTray(mainWindow, trayOptions); } - if (options.trayWindow) { - setWindowAutoHide(mainWindow); - if (options.trayOptions?.showAtStartup){ + if (trayOptions.enabled && trayOptions.trayWindow) { + hideWindow(mainWindow); + if (trayOptions.showAtStartup){ showWindow(mainWindow) } }else{ diff --git a/src/main/desktop.ts b/src/main/desktop.ts deleted file mode 100644 index f23b00f..0000000 --- a/src/main/desktop.ts +++ /dev/null @@ -1,17 +0,0 @@ -import { screen } from 'electron' - -// To detect proper desktop/workspace, find the display where the mouse cursor is -export function getDisplayNearestPoint(){ - return screen.getDisplayNearestPoint(screen.getCursorScreenPoint()); -} - -export function moveToCurrentDesktop(mainWindow){ - const currentDisplay = getDisplayNearestPoint(); -// Center window relatively to that display - const centerX = Math.round((currentDisplay.workArea.width - mainWindow.getBounds().width) / 2); - const centerY = Math.round((currentDisplay.workArea.height - mainWindow.getBounds().height) / 2); - mainWindow.setPosition(currentDisplay.workArea.x + centerX, currentDisplay.workArea.y + centerY); -} - - - diff --git a/src/main/tray.ts b/src/main/tray.ts index b035865..b483565 100644 --- a/src/main/tray.ts +++ b/src/main/tray.ts @@ -1,29 +1,23 @@ -import { app, screen, Menu, Tray, BaseWindow, BrowserWindow } from 'electron' +import { app, screen, Menu, Tray, BrowserWindow } from 'electron' import Constants from './utils/Constants.ts' -import { getDisplayNearestPoint } from './desktop.ts' - let tray; let trayOptions; -const DEFAULT_OPTIONS = { - menu: false, - tooltip: 'Vutron App', - margin: {x:0, y:0}, - width: Constants.IS_DEV_ENV ? 1500 : 1200, - height: 650, -} -export function createTray(window: BaseWindow, trayWindow: boolean, options) { - trayOptions = options || DEFAULT_OPTIONS; - trayOptions.trayWindow=trayWindow; +export function createTray(window: BrowserWindow, options) { + trayOptions = options || Constants.DEFAULT_TRAY_OPTIONS; + tray = new Tray('buildAssets/icons/icon16.png'); - // tray.on('double-click', function (event) { - // toggleWindow(window) - // }); + tray.on('double-click', function (event) { + console.log('double-click') + toggleWindow(window) + }); tray.on('right-click', function (event) { + console.log('right-click') toggleWindow(window) }); tray.on('click', function (event) { + console.log('click') toggleWindow(window) }); tray.setToolTip(trayOptions.tooltip); @@ -60,34 +54,33 @@ export function createTray(window: BaseWindow, trayWindow: boolean, options) { return tray } -export function setWindowAutoHide(window: BrowserWindow) { +export function hideWindow(window: BrowserWindow) { + console.log('hide window'); window.hide(); window.on("blur", () => { // dont close if devtools if (!window.webContents.isDevToolsOpened()) { window.hide(); - // ipcMain.emit("tray-window-hidden", { window: window, tray: tray }); } }); } -export function toggleWindow(window: BaseWindow) { +export function toggleWindow(window: BrowserWindow) { if (window.isVisible()) { - window.hide(); - // ipcMain.emit("tray-window-hidden", { window: window, tray: tray }); - return; + hideWindow(window); + }else{ + showWindow(window); } - - showWindow(window); - // ipcMain.emit("tray-window-visible", { window: window, tray: tray }); } -export function showWindow(window: BaseWindow) { +export function showWindow(window: BrowserWindow) { + console.log('show window'); window.show() + alignWindow(window) } -export function alignWindow(window: BaseWindow) { +export function alignWindow(window: BrowserWindow) { if (!trayOptions.trayWindow) return; const position = calculateWindowPosition(); @@ -101,15 +94,8 @@ export function alignWindow(window: BaseWindow) { } function calculateWindowPosition() { - // // To detect proper desktop/workspace, find the display where the mouse cursor is - // const currentDisplay = getDisplayNearestPoint(); - // console.log("currentDisplay.workArea=", currentDisplay.workArea); - - const margin = { - x: trayOptions.margin.x, // + currentDisplay.workArea.width, - y: trayOptions.margin.y // + currentDisplay.workArea.height - }; - const b = trayOptions + const margin = trayOptions.margin; + const b = trayOptions; const screenBounds = screen.getPrimaryDisplay().size; const trayBounds = tray.getBounds(); diff --git a/src/main/utils/Constants.ts b/src/main/utils/Constants.ts index 5b19818..43f029f 100644 --- a/src/main/utils/Constants.ts +++ b/src/main/utils/Constants.ts @@ -4,6 +4,15 @@ import { fileURLToPath } from 'url' const __dirname = dirname(fileURLToPath(import.meta.url)) +export interface TrayOptions { + enabled: boolean, + trayWindow: boolean, + menu: boolean, + tooltip: string, + margin: {x: number, y: number}, + showAtStartup: boolean +} + export default class Constants { // Display app name (uppercase first letter) static APP_NAME = name.charAt(0).toUpperCase() + name.slice(1) @@ -25,6 +34,17 @@ export default class Constants { preload: join(__dirname, '../preload/index.js') } + static DEFAULT_TRAY_OPTIONS : TrayOptions = { + enabled: false, + trayWindow: false, + menu: false, + tooltip: 'Vutron App', + margin: {x:0, y:0}, + showAtStartup: false + } + static APP_INDEX_URL_DEV = `${debug.env.VITE_DEV_SERVER_URL}/index.html` static APP_INDEX_URL_PROD = join(__dirname, '../index.html') } + +