Skip to content

Build your personal knowledge base with Trilium Notes

server-ts: Port services/tray

Elian Doran3ea4b7a72bda
diff --git a/electron.js b/electron.js
index f8a73d7905..69f403dd83 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 6f7f9f598e..78e8645cdf 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
}