From 3ea4b7a72bda026ce2b0da3474740c58f7148e1b Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Sun, 18 Feb 2024 12:28:32 +0200 Subject: [PATCH] server-ts: Port services/tray --- electron.js | 2 +- src/services/{tray.js => tray.ts} | 21 ++++++++++++--------- 2 files changed, 13 insertions(+), 10 deletions(-) rename src/services/{tray.js => tray.ts} (85%) diff --git a/electron.js b/electron.js index f8a73d790..69f403dd8 100644 --- a/electron.js +++ b/electron.js @@ -4,7 +4,7 @@ const {app, globalShortcut, BrowserWindow} = require('electron'); const sqlInit = require('./src/services/sql_init'); const appIconService = require('./src/services/app_icon.js'); const windowService = require('./src/services/window'); -const tray = require('./src/services/tray.js'); +const tray = require('./src/services/tray'); // Adds debug features like hotkeys for triggering dev tools and reload require('electron-debug')(); diff --git a/src/services/tray.js b/src/services/tray.ts similarity index 85% rename from src/services/tray.js rename to src/services/tray.ts index 6f7f9f598..78e8645cd 100644 --- a/src/services/tray.js +++ b/src/services/tray.ts @@ -1,13 +1,13 @@ -const { Menu, Tray } = require('electron'); -const path = require('path'); -const windowService = require('./window'); -const optionService = require('./options'); +import { Menu, Tray } from 'electron'; +import path = require('path'); +import windowService = require('./window'); +import optionService = require('./options'); const UPDATE_TRAY_EVENTS = [ 'minimize', 'maximize', 'show', 'hide' -] +] as const; -let tray = null; +let tray: Tray | null = null; // `mainWindow.isVisible` doesn't work with `mainWindow.show` and `mainWindow.hide` - it returns `false` when the window // is minimized let isVisible = true; @@ -37,6 +37,7 @@ const getIconPath = () => { } const registerVisibilityListener = () => { const mainWindow = windowService.getMainWindow(); + if (!mainWindow) { return; } // They need to be registered before the tray updater is registered mainWindow.on('show', () => { @@ -46,13 +47,14 @@ const registerVisibilityListener = () => { isVisible = false; }); - UPDATE_TRAY_EVENTS.forEach(eventName => { - mainWindow.on(eventName, updateTrayMenu) + UPDATE_TRAY_EVENTS.forEach((eventName) => { + mainWindow.on(eventName as any, updateTrayMenu) }); } const updateTrayMenu = () => { const mainWindow = windowService.getMainWindow(); + if (!mainWindow) { return; } const contextMenu = Menu.buildFromTemplate([ { @@ -83,6 +85,7 @@ const updateTrayMenu = () => { } const changeVisibility = () => { const window = windowService.getMainWindow(); + if (!window) { return; } if (isVisible) { window.hide(); @@ -106,6 +109,6 @@ function createTray() { registerVisibilityListener(); } -module.exports = { +export = { createTray }