1+ const { app, BrowserWindow, dialog, Menu } = require ( 'electron' ) ;
2+ const fs = require ( 'fs' ) ;
3+
4+
5+ // Keep a global reference of the window object, if you don't, the window will
6+ // be closed automatically when the JavaScript object is garbage collected.
7+ let mainWindow
8+
9+ function createWindow ( ) {
10+ // Create the browser window.
11+ mainWindow = new BrowserWindow ( {
12+ width : 1000 ,
13+ height : 900 ,
14+ } ) ;
15+
16+ // and load the index.html of the app.
17+ console . log ( __dirname ) ;
18+ mainWindow . loadURL ( `file://${ __dirname } /../dist/index.html` ) ;
19+
20+
21+ const { app, Menu } = require ( 'electron' )
22+
23+ const template = [
24+ {
25+ label : 'File' ,
26+ submenu : [
27+ {
28+ label : 'Save File' ,
29+ accelerator : 'CmdOrCtrl+S' ,
30+ click ( ) {
31+ mainWindow . webContents . send ( 'save-file' ) ;
32+ }
33+ } ,
34+ ]
35+ } ,
36+ {
37+ label : 'Edit' ,
38+ submenu : [
39+ { role : 'undo' } ,
40+ { role : 'redo' } ,
41+ { type : 'separator' } ,
42+ { role : 'cut' } ,
43+ { role : 'copy' } ,
44+ { role : 'paste' } ,
45+ { role : 'pasteandmatchstyle' } ,
46+ { role : 'delete' } ,
47+ { role : 'selectall' }
48+ ]
49+ } ,
50+ {
51+ label : 'View' ,
52+ submenu : [
53+ { role : 'reload' } ,
54+ { role : 'forcereload' } ,
55+ { role : 'toggledevtools' } ,
56+ { type : 'separator' } ,
57+ { role : 'resetzoom' } ,
58+ { role : 'zoomin' } ,
59+ { role : 'zoomout' } ,
60+ { type : 'separator' } ,
61+ { role : 'togglefullscreen' }
62+ ]
63+ } ,
64+ {
65+ role : 'window' ,
66+ submenu : [
67+ { role : 'minimize' } ,
68+ { role : 'close' }
69+ ]
70+ } ,
71+ {
72+ role : 'help' ,
73+ submenu : [
74+ {
75+ label : 'Learn More' ,
76+ click ( ) { require ( 'electron' ) . shell . openExternal ( 'https://electronjs.org' ) }
77+ }
78+ ]
79+ } ,
80+ {
81+ label : 'Developer' ,
82+ submenu : [
83+ {
84+ label : 'Toggle Developer Tools' ,
85+ accelerator : process . platform === 'darwin' ? 'Alt+Command+I' : 'Ctrl+Shift+I' ,
86+ click ( ) {
87+ mainWindow . webContents . toggleDevTools ( ) ;
88+ }
89+ }
90+ ]
91+ }
92+ ]
93+
94+ if ( process . platform === 'darwin' ) {
95+ template . unshift ( {
96+ label : app . getName ( ) ,
97+ submenu : [
98+ { role : 'about' } ,
99+ { type : 'separator' } ,
100+ { role : 'services' , submenu : [ ] } ,
101+ { type : 'separator' } ,
102+ { role : 'hide' } ,
103+ { role : 'hideothers' } ,
104+ { role : 'unhide' } ,
105+ { type : 'separator' } ,
106+ { role : 'quit' }
107+ ]
108+ } )
109+
110+ // Edit menu
111+ template [ 2 ] . submenu . push (
112+ { type : 'separator' } ,
113+ {
114+ label : 'Speech' ,
115+ submenu : [
116+ { role : 'startspeaking' } ,
117+ { role : 'stopspeaking' }
118+ ]
119+ }
120+ )
121+
122+ // Window menu
123+ template [ 4 ] . submenu = [
124+ { role : 'close' } ,
125+ { role : 'minimize' } ,
126+ { role : 'zoom' } ,
127+ { type : 'separator' } ,
128+ { role : 'front' }
129+ ]
130+ }
131+
132+ const menu = Menu . buildFromTemplate ( template ) ;
133+ Menu . setApplicationMenu ( menu ) ;
134+
135+
136+
137+ // Open the DevTools.
138+ // mainWindow.webContents.openDevTools()
139+
140+ // Emitted when the window is closed.
141+ mainWindow . on ( 'closed' , function ( ) {
142+ // Dereference the window object, usually you would store windows
143+ // in an array if your app supports multi windows, this is the time
144+ // when you should delete the corresponding element.
145+ mainWindow = null
146+ } )
147+ }
148+
149+ // This method will be called when Electron has finished
150+ // initialization and is ready to create browser windows.
151+ // Some APIs can only be used after this event occurs.
152+ app . on ( 'ready' , createWindow )
153+
154+ // Quit when all windows are closed.
155+ app . on ( 'window-all-closed' , function ( ) {
156+ // On OS X it is common for applications and their menu bar
157+ // to stay active until the user quits explicitly with Cmd + Q
158+ if ( process . platform !== 'darwin' ) {
159+ app . quit ( )
160+ }
161+ } )
162+
163+ app . on ( 'activate' , function ( ) {
164+ // On OS X it's common to re-create a window in the app when the
165+ // dock icon is clicked and there are no other windows open.
166+ if ( mainWindow === null ) {
167+ createWindow ( )
168+ }
169+ } )
0 commit comments