1- const { app, BrowserWindow, globalShortcut } = require ( 'electron/main' ) ;
1+ const { app, BrowserWindow, Tray , Menu , globalShortcut } = require ( 'electron/main' ) ;
22const path = require ( 'node:path' ) ;
33const { ipcMain} = require ( 'electron' )
44const http = require ( 'http' ) ;
55const xml = require ( "xml2js" ) ;
66const net = require ( 'net' ) ;
77
8+ let tray ;
89let s_mainWindow ;
910let msgbacklog = [ ] ;
1011var WServer ;
@@ -50,7 +51,7 @@ function createWindow () {
5051 }
5152 } ) ;
5253 if ( app . isPackaged ) {
53- mainWindow . setMenu ( null ) ;
54+ mainWindow . setMenu ( null ) ;
5455 }
5556
5657
@@ -133,6 +134,7 @@ ipcMain.on("setCAT", async (event,arg) => {
133134} ) ;
134135
135136ipcMain . on ( "quit" , async ( event , arg ) => {
137+ app . isQuitting = true ;
136138 app . quit ( ) ;
137139 event . returnValue = true ;
138140} ) ;
@@ -157,6 +159,12 @@ ipcMain.on("test", async (event,arg) => {
157159 }
158160} ) ;
159161
162+ app . on ( 'before-quit' , ( ) => {
163+ if ( tray ) {
164+ tray . destroy ( ) ;
165+ }
166+ } ) ;
167+
160168app . whenReady ( ) . then ( ( ) => {
161169 s_mainWindow = createWindow ( ) ;
162170 createAdvancedWindow ( s_mainWindow ) ;
@@ -169,10 +177,40 @@ app.whenReady().then(() => {
169177 s_mainWindow . webContents . send ( 'updateMsg' , msgbacklog . pop ( ) ) ;
170178 }
171179 } ) ;
180+
181+ // Create the tray icon
182+ tray = new Tray ( 'icon1616.png' ) ;
183+
184+ const contextMenu = Menu . buildFromTemplate ( [
185+ { label : 'Show App' , click : ( ) => s_mainWindow . show ( ) } ,
186+ { label : 'Quit' , click : ( ) => {
187+ console . log ( "Exiting" ) ;
188+ app . isQuitting = true ;
189+ app . quit ( ) ;
190+ }
191+ } ,
192+ ] ) ;
193+
194+ tray . setContextMenu ( contextMenu ) ;
195+ tray . setToolTip ( require ( './package.json' ) . name + " V" + require ( './package.json' ) . version ) ;
196+
197+ s_mainWindow . on ( 'minimize' , ( event ) => {
198+ event . preventDefault ( ) ;
199+ s_mainWindow . hide ( ) ; // Hides the window instead of minimizing it to the taskbar
200+ } ) ;
201+
202+ s_mainWindow . on ( 'close' , ( event ) => {
203+ if ( ! app . isQuitting ) {
204+ event . preventDefault ( ) ;
205+ s_mainWindow . hide ( ) ;
206+ }
207+ } ) ;
208+
209+ app . dock . hide ( ) ;
172210} )
173211
174212app . on ( 'window-all-closed' , function ( ) {
175- if ( process . platform !== 'darwin' ) app . quit ( )
213+ if ( process . platform !== 'darwin' ) app . quit ( ) ;
176214 app . quit ( ) ;
177215} )
178216
0 commit comments