From bb0812349b6bea8220408cf87b770089ec625458 Mon Sep 17 00:00:00 2001 From: LudoO Date: Sun, 16 Feb 2025 12:47:04 +0100 Subject: [PATCH] feat: review options tray and trayWindow --- src/main/MainRunner.ts | 16 ++++++++-------- src/main/index.ts | 4 ++-- src/main/tray.ts | 16 +++++++--------- src/main/utils/Constants.ts | 3 ++- 4 files changed, 19 insertions(+), 20 deletions(-) diff --git a/src/main/MainRunner.ts b/src/main/MainRunner.ts index ea0f600..945ec18 100644 --- a/src/main/MainRunner.ts +++ b/src/main/MainRunner.ts @@ -1,8 +1,7 @@ import { app, BrowserWindow, RenderProcessGoneDetails, BrowserWindowConstructorOptions } from 'electron' import Constants from './utils/Constants' import IPCs from './IPCs' -import { createTray, setWindowAutoHide } from './tray.ts' -import { moveToCurrentDesktop } from './desktop.ts' +import { createTray, setWindowAutoHide, showWindow } from './tray.ts' const options = { tray: true, @@ -13,8 +12,9 @@ const options = { menu: false, tooltip: 'Vutron App', margin: {x:0, y:0}, - width: Constants.IS_DEV_ENV ? 1500 : 1200, + width: Constants.IS_DEV_ENV ? 800 : 600, height: 650, + showAtStartup: false } } @@ -34,7 +34,7 @@ export const createMainWindow = async (): Promise => { height: options.height, useContentSize: true, webPreferences: Constants.DEFAULT_WEB_PREFERENCES, - frame: !options.trayWindow, + frame: true } if (options.trayWindow){ opt = { @@ -72,20 +72,20 @@ export const createMainWindow = async (): Promise => { }) if (options.tray) { - createTray(mainWindow, options.trayOptions); + createTray(mainWindow, options.trayWindow, options.trayOptions); } if (options.trayWindow) { setWindowAutoHide(mainWindow); - // showWindow(mainWindow) + if (options.trayOptions?.showAtStartup){ + showWindow(mainWindow) + } }else{ mainWindow.once('ready-to-show', (): void => { mainWindow.setAlwaysOnTop(true) mainWindow.show() mainWindow.focus() mainWindow.setAlwaysOnTop(false) - - // moveToCurrentDesktop(mainWindow) }) } diff --git a/src/main/index.ts b/src/main/index.ts index 8dc301c..b3a1666 100644 --- a/src/main/index.ts +++ b/src/main/index.ts @@ -18,12 +18,12 @@ app.on('ready', async () => { } */ - mainWindow = await createMainWindow(mainWindow) + mainWindow = await createMainWindow() }) app.on('activate', async () => { if (!mainWindow) { - mainWindow = await createMainWindow(mainWindow) + mainWindow = await createMainWindow() } }) diff --git a/src/main/tray.ts b/src/main/tray.ts index 4209031..7033a85 100644 --- a/src/main/tray.ts +++ b/src/main/tray.ts @@ -2,11 +2,6 @@ import { app, screen, Menu, Tray, BaseWindow, BrowserWindow } from 'electron' import Constants from './utils/Constants.ts' import { getDisplayNearestPoint } from './desktop.ts' -// defaults -// const width = 800; -// const height = 500; -// const DEFAULT_MARGIN = {x: 0, y: 0} ; - let tray; let trayOptions; const DEFAULT_OPTIONS = { @@ -17,13 +12,14 @@ const DEFAULT_OPTIONS = { height: 650, } -export function createTray(window: BaseWindow, options) { +export function createTray(window: BaseWindow, trayWindow: boolean, options) { trayOptions = options || DEFAULT_OPTIONS; + trayOptions.trayWindow=trayWindow; tray = new Tray('buildAssets/icons/icon16.png'); - tray.on('double-click', function (event) { - toggleWindow(window) - }); + // tray.on('double-click', function (event) { + // toggleWindow(window) + // }); tray.on('right-click', function (event) { toggleWindow(window) }); @@ -92,6 +88,8 @@ export function showWindow(window: BaseWindow) { } export function alignWindow(window: BaseWindow) { + if (!trayOptions.trayWindow) return; + const position = calculateWindowPosition(); const b = window.getBounds() window.setBounds({ diff --git a/src/main/utils/Constants.ts b/src/main/utils/Constants.ts index a97fbc7..5b19818 100644 --- a/src/main/utils/Constants.ts +++ b/src/main/utils/Constants.ts @@ -12,7 +12,8 @@ export default class Constants { static IS_DEV_ENV = process.env.NODE_ENV === 'development' - // required IS_DEV_ENV + // To show devtools at startup. It requires IS_DEV_ENV=true. + // Note: For debugging purpose, window won't be closed if click elsewhere, if devtools is open. static IS_DEVTOOLS = true static IS_MAC = process.platform === 'darwin'