1
+ 'use strict'
2
+
3
+ const { app, ipcMain, BrowserWindow, Menu, Tray } = require ( "electron" )
4
+ const fs = require ( "fs" )
5
+ const path = require ( "path" )
6
+
7
+ // Module to control application life.
8
+ // Module to create native browser window.
9
+
10
+ // Keep a global reference of the window object, if you don't, the window will
11
+ // be closed automatically when the JavaScript object is garbage collected.
12
+ let mainWindow ;
13
+
14
+ let tray = null
15
+
16
+ function createWindow ( ) {
17
+ if ( process . platform === "linux" && process . env . APPDIR != null ) {
18
+ tray = new Tray ( path . join ( process . env . APPDIR , "testapp.png" ) )
19
+ const contextMenu = Menu . buildFromTemplate ( [
20
+ { label : 'Item1' , type : 'radio' } ,
21
+ { label : 'Item2' , type : 'radio' } ,
22
+ { label : 'Item3' , type : 'radio' , checked : true } ,
23
+ { label : 'Item4' , type : 'radio' }
24
+ ] )
25
+ tray . setToolTip ( 'This is my application.' )
26
+ tray . setContextMenu ( contextMenu )
27
+ }
28
+
29
+ // Create the browser window.
30
+ mainWindow = new BrowserWindow ( { width : 800 , height : 600 } ) ;
31
+
32
+ // and load the index.html of the app.
33
+ mainWindow . loadURL ( 'file://' + __dirname + '/index.html' ) ;
34
+
35
+ // Open the DevTools.
36
+ mainWindow . webContents . openDevTools ( ) ;
37
+
38
+ mainWindow . webContents . executeJavaScript ( `console.log("appData: ${ app . getPath ( "appData" ) . replace ( / \\ / g, "\\\\" ) } ")` )
39
+ mainWindow . webContents . executeJavaScript ( `console.log("userData: ${ app . getPath ( "userData" ) . replace ( / \\ / g, "\\\\" ) } ")` )
40
+
41
+ // Emitted when the window is closed.
42
+ mainWindow . on ( 'closed' , function ( ) {
43
+ // Dereference the window object, usually you would store windows
44
+ // in an array if your app supports multi windows, this is the time
45
+ // when you should delete the corresponding element.
46
+ mainWindow = null ;
47
+ } ) ;
48
+ }
49
+
50
+ // This method will be called when Electron has finished
51
+ // initialization and is ready to create browser windows.
52
+ app . on ( 'ready' , createWindow ) ;
53
+
54
+ // Quit when all windows are closed.
55
+ app . on ( 'window-all-closed' , function ( ) {
56
+ // On MacOS it is common for applications and their menu bar
57
+ // to stay active until the user quits explicitly with Cmd + Q
58
+ if ( process . platform !== 'darwin' ) {
59
+ app . quit ( ) ;
60
+ }
61
+ } ) ;
62
+
63
+ app . on ( "activate" , function ( ) {
64
+ if ( mainWindow === null ) {
65
+ createWindow ( )
66
+ }
67
+ } )
68
+
69
+ ipcMain . on ( "saveAppData" , ( ) => {
70
+ try {
71
+ // electron doesn't escape / in the product name
72
+ fs . writeFileSync ( path . join ( app . getPath ( "appData" ) , "Test App ßW" , "testFile" ) , "test" )
73
+ }
74
+ catch ( e ) {
75
+ mainWindow . webContents . executeJavaScript ( `console.log(\`userData: ${ e } \`)` )
76
+ }
77
+ } )
0 commit comments