-
Notifications
You must be signed in to change notification settings - Fork 5
/
trayIcon.ts
101 lines (86 loc) · 2.61 KB
/
trayIcon.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/* eslint-disable @typescript-eslint/no-explicit-any */
import { join } from 'path';
import { app, BrowserWindow, Menu, Tray } from 'electron';
let tray: Tray | null = null;
let trayContextMenu = null;
export function createTrayIcon(
getMainWindow: () => BrowserWindow | null
): Tray {
// keep the duplicated part to allow for search and find
const iconFile =
process.platform === 'darwin'
? '256x256.png'
: '512x512.png';
const icon = join(__dirname, '../', 'images', iconFile);
tray = new Tray(icon);
(tray as any).forceOnTop = (mainWindow: BrowserWindow) => {
if (mainWindow) {
// On some versions of GNOME the window may not be on top when restored.
// This trick should fix it.
// Thanks to: https://github.com/Enrico204/Whatsapp-Desktop/commit/6b0dc86b64e481b455f8fce9b4d797e86d000dc1
mainWindow.setAlwaysOnTop(true);
mainWindow.focus();
mainWindow.setAlwaysOnTop(false);
}
};
(tray as any).closeApp = () => {
const mainWindow = getMainWindow();
if (!mainWindow) {
return;
}
app.quit()
};
(tray as any).toggleWindowVisibility = () => {
const mainWindow = getMainWindow();
if (mainWindow) {
if (mainWindow.isVisible()) {
mainWindow.hide();
} else {
mainWindow.show();
(tray as any).forceOnTop(mainWindow);
}
}
(tray as any).updateContextMenu();
};
(tray as any).showWindow = () => {
const mainWindow = getMainWindow();
if (mainWindow) {
if (!mainWindow.isVisible()) {
mainWindow.show();
}
(tray as any).forceOnTop(mainWindow);
}
(tray as any).updateContextMenu();
};
(tray as any).updateContextMenu = () => {
const mainWindow = getMainWindow();
if (!mainWindow) {
return;
}
// NOTE: we want to have the show/hide entry available in the tray icon
// context menu, since the 'click' event may not work on all platforms.
// For details please refer to:
// https://github.com/electron/electron/blob/master/docs/api/tray.md.
trayContextMenu = Menu.buildFromTemplate([
{
id: 'toggleWindowVisibility',
label: mainWindow.isVisible() ? 'Hide' : 'Show',
click: (tray as any).toggleWindowVisibility
},
{
id: 'quit',
label: 'Quit',
click: () => {
app.quit()
// mainWindow.destroy();
// app.quit.bind(app);
}
}
]);
(tray as any).setContextMenu(trayContextMenu);
};
tray.on('click', (tray as any).showWindow);
tray.setToolTip('Belnet GUI');
(tray as any).updateContextMenu();
return tray;
}