diff --git a/src/main/MainRunner.ts b/src/main/MainRunner.ts index 49f1334..04b1456 100644 --- a/src/main/MainRunner.ts +++ b/src/main/MainRunner.ts @@ -1,4 +1,9 @@ -import { app, BrowserWindow, RenderProcessGoneDetails, BrowserWindowConstructorOptions } from 'electron' +import { + app, + BrowserWindow, + RenderProcessGoneDetails, + BrowserWindowConstructorOptions +} from 'electron' import Constants, { TrayOptions } from './utils/Constants' import IPCs from './IPCs' import { createTray, hideWindow, showWindow } from './tray.ts' @@ -32,19 +37,21 @@ export const createMainWindow = async (): Promise => { webPreferences: Constants.DEFAULT_WEB_PREFERENCES, frame: true } - const trayOptions: TrayOptions = (options.tray?.enabled) ? { - ...Constants.DEFAULT_TRAY_OPTIONS, - ...options.tray - }: { - ...Constants.DEFAULT_TRAY_OPTIONS, - enabled: false - } + 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){ + if (trayOptions.enabled && trayOptions.trayWindow) { opt = { ...opt, - width: options.width , + width: options.width, height: options.height, maxWidth: options.width, maxHeight: options.height, @@ -77,15 +84,15 @@ export const createMainWindow = async (): Promise => { }) if (trayOptions.enabled) { - createTray(mainWindow, trayOptions); + createTray(mainWindow, trayOptions) } if (trayOptions.enabled && trayOptions.trayWindow) { - hideWindow(mainWindow); - if (trayOptions.showAtStartup){ - showWindow(mainWindow) + hideWindow(mainWindow) + if (trayOptions.showAtStartup) { + showWindow(mainWindow) } - }else{ + } else { mainWindow.once('ready-to-show', (): void => { mainWindow.setAlwaysOnTop(true) mainWindow.show() diff --git a/src/main/tray.ts b/src/main/tray.ts index f416218..97a067f 100644 --- a/src/main/tray.ts +++ b/src/main/tray.ts @@ -2,17 +2,17 @@ import { app, screen, Menu, Tray, BrowserWindow } from 'electron' import Constants from './utils/Constants.ts' import { debounce } from './utils/Util.ts' let tray -let trayOptions; +let trayOptions export function createTray(window: BrowserWindow, options) { - trayOptions = options || Constants.DEFAULT_TRAY_OPTIONS; + trayOptions = options || Constants.DEFAULT_TRAY_OPTIONS // menu or trayWindow, you need to choose - if (trayOptions.trayWindow){ + if (trayOptions.trayWindow) { trayOptions.menu = false } - tray = new Tray('buildAssets/icons/icon16.png'); - tray.setToolTip(trayOptions.tooltip); + tray = new Tray('buildAssets/icons/icon16.png') + tray.setToolTip(trayOptions.tooltip) if (trayOptions.menu) { const contextMenu = Menu.buildFromTemplate([ { @@ -39,11 +39,11 @@ export function createTray(window: BrowserWindow, options) { } else { // handle click on tray icon tray.on('right-click', function (event) { - debounce(() => toggleWindow(window)); - }); + debounce(() => toggleWindow(window)) + }) tray.on('click', function (event) { - debounce(() => toggleWindow(window)); - }); + debounce(() => toggleWindow(window)) + }) // no menu for tray window window.setMenu(null) tray.setContextMenu(null) @@ -54,22 +54,22 @@ export function createTray(window: BrowserWindow, options) { } export function hideWindow(window: BrowserWindow) { - window.hide(); + window.hide() // if (!trayOptions.trayWindow) return; // hide window when click elsewhere on screen - window.on("blur", () => { + window.on('blur', () => { // dont close if devtools if (!window.webContents.isDevToolsOpened()) { - window.hide(); + window.hide() } - }); + }) } export function toggleWindow(window: BrowserWindow) { if (window.isVisible()) { - hideWindow(window); - }else{ - showWindow(window); + hideWindow(window) + } else { + showWindow(window) } } @@ -79,33 +79,30 @@ export function showWindow(window: BrowserWindow) { } export function alignWindow(window: BrowserWindow) { - if (!trayOptions.trayWindow) return; + if (!trayOptions.trayWindow) return const b = window.getBounds() - const position = calculateWindowPosition(b); + const position = calculateWindowPosition(b) window.setBounds({ width: b.width, height: b.height, x: position.x, y: position.y - }); + }) } function calculateWindowPosition(b) { - const margin = trayOptions.margin; - const screenBounds = screen.getPrimaryDisplay().size; - const trayBounds = tray.getBounds(); - const bottom = trayBounds.y > screenBounds.height / 2 ; - const x = Math.floor(trayBounds.x - b.width/2 - margin.x + trayBounds.width / 2); - const y = bottom ? - Math.floor(trayBounds.y - b.height - margin.y + trayBounds.height / 2) - : - Math.floor(trayBounds.y + margin.y + trayBounds.height / 2) - ; - + const margin = trayOptions.margin + const screenBounds = screen.getPrimaryDisplay().size + const trayBounds = tray.getBounds() + const bottom = trayBounds.y > screenBounds.height / 2 + const x = Math.floor(trayBounds.x - b.width / 2 - margin.x + trayBounds.width / 2) + const y = bottom + ? Math.floor(trayBounds.y - b.height - margin.y + trayBounds.height / 2) + : Math.floor(trayBounds.y + margin.y + trayBounds.height / 2) // constraint into screen return { x: Math.max(0, Math.min(screenBounds.width - b.width, x)), y: Math.max(0, Math.min(screenBounds.height - b.height, y)) - }; + } } diff --git a/src/main/utils/Constants.ts b/src/main/utils/Constants.ts index 43f029f..6afb0ba 100644 --- a/src/main/utils/Constants.ts +++ b/src/main/utils/Constants.ts @@ -5,11 +5,11 @@ 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}, + enabled: boolean + trayWindow: boolean + menu: boolean + tooltip: string + margin: { x: number; y: number } showAtStartup: boolean } @@ -34,17 +34,15 @@ export default class Constants { preload: join(__dirname, '../preload/index.js') } - static DEFAULT_TRAY_OPTIONS : TrayOptions = { + static DEFAULT_TRAY_OPTIONS: TrayOptions = { enabled: false, trayWindow: false, menu: false, tooltip: 'Vutron App', - margin: {x:0, y:0}, + 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') } - - diff --git a/src/main/utils/Util.ts b/src/main/utils/Util.ts index 3cb030e..894c75c 100644 --- a/src/main/utils/Util.ts +++ b/src/main/utils/Util.ts @@ -1,14 +1,12 @@ - -export function debounce(func, timeout = 200){ - let timer; - return function(...args) { +export function debounce(func, timeout = 200) { + let timer + return (function (...args) { if (!timer) { - func.apply(this, args); + func.apply(this, args) } - clearTimeout(timer); + clearTimeout(timer) timer = setTimeout(() => { - timer = undefined; - }, timeout); - }(); + timer = undefined + }, timeout) + })() } -