1
1
// Modules to control application life and create native browser window
2
- const { app, BrowserWindow, ipcMain, Menu } = require ( 'electron' )
3
- const path = require ( 'path' )
4
- require ( './titlebar-events' )
2
+ const { app, BrowserWindow, Menu } = require ( 'electron' ) ;
3
+ const path = require ( 'path' ) ;
4
+ const { setupTitlebar, attachTitlebarToWindow } = require ( 'custom-electron-titlebar/main' ) ;
5
+ // setup the titlebar main process
6
+ setupTitlebar ( ) ;
5
7
6
8
createWindow = ( ) => {
7
9
// Create the browser window.
8
10
const mainWindow = new BrowserWindow ( {
9
11
width : 800 ,
10
12
height : 600 ,
11
13
titleBarStyle : 'hidden' ,
14
+ //frame: false, // needed if process.versions.electron < 14
12
15
webPreferences : {
13
16
preload : path . join ( __dirname , 'preload.js' )
14
17
}
15
- } )
18
+ } ) ;
16
19
17
- // and load the index.html of the app.
18
- mainWindow . loadFile ( 'index.html' )
20
+ const menu = Menu . buildFromTemplate ( exampleMenuTemplate ( ) ) ;
21
+ Menu . setApplicationMenu ( menu ) ;
19
22
20
- // Open the DevTools.
21
- // mainWindow.webContents.openDevTools()
22
23
23
- mainWindow . on ( 'enter-full-screen' , ( ) => {
24
- mainWindow . webContents . send ( 'window-fullscreen' , true )
25
- } )
26
24
27
- mainWindow . on ( 'leave-full-screen' , ( ) => {
28
- mainWindow . webContents . send ( 'window-fullscreen' , false )
29
- } )
25
+ // and load the index.html of the app.
26
+ mainWindow . loadFile ( 'index.html' ) ;
30
27
31
- mainWindow . on ( 'focus' , ( ) => {
32
- mainWindow . webContents . send ( 'window-focus' , true )
33
- } )
28
+ // Open the DevTools.
29
+ // mainWindow.webContents.openDevTools()
34
30
35
- mainWindow . on ( 'blur' , ( ) => {
36
- mainWindow . webContents . send ( 'window-focus' , false )
37
- } )
31
+ //attach fullscreen(f11 and not 'maximized') && focus listeners
32
+ attachTitlebarToWindow ( mainWindow ) ;
38
33
}
39
34
40
35
// This method will be called when Electron has finished
@@ -46,16 +41,152 @@ app.whenReady().then(() => {
46
41
app . on ( 'activate' , function ( ) {
47
42
// On macOS it's common to re-create a window in the app when the
48
43
// dock icon is clicked and there are no other windows open.
49
- if ( BrowserWindow . getAllWindows ( ) . length === 0 ) createWindow ( )
44
+ if ( BrowserWindow . getAllWindows ( ) . length === 0 ) createWindow ( ) ;
50
45
} )
51
46
} )
52
47
53
48
// Quit when all windows are closed, except on macOS. There, it's common
54
49
// for applications and their menu bar to stay active until the user quits
55
50
// explicitly with Cmd + Q.
56
51
app . on ( 'window-all-closed' , function ( ) {
57
- if ( process . platform !== 'darwin' ) app . quit ( )
52
+ if ( process . platform !== 'darwin' ) app . quit ( ) ;
58
53
} )
59
54
60
- // In this file you can include the rest of your app's specific main process
61
- // code. You can also put them in separate files and require them here.
55
+ const exampleMenuTemplate = ( ) => [
56
+ {
57
+ label : "Simple Options" ,
58
+ submenu : [
59
+ {
60
+ label : "Quit" ,
61
+ click : ( ) => app . quit ( )
62
+ } ,
63
+ {
64
+ label : "Radio1" ,
65
+ type : "radio" ,
66
+ checked : true
67
+ } ,
68
+ {
69
+ label : "Radio2" ,
70
+ type : "radio" ,
71
+ } ,
72
+ {
73
+ label : "Checkbox1" ,
74
+ type : "checkbox" ,
75
+ checked : true ,
76
+ click : ( item ) => {
77
+ console . log ( "item is checked? " + item . checked ) ;
78
+ }
79
+ } ,
80
+ { type : "separator" } ,
81
+ {
82
+ label : "Checkbox2" ,
83
+ type : "checkbox" ,
84
+ checked : false ,
85
+ click : ( item ) => {
86
+ console . log ( "item is checked? " + item . checked ) ;
87
+ }
88
+ }
89
+ ]
90
+ } ,
91
+ {
92
+ label : "Advanced Options" ,
93
+ submenu : [
94
+ {
95
+ label : "Quit" ,
96
+ click : ( ) => app . quit ( )
97
+ } ,
98
+ {
99
+ label : "Radio1" ,
100
+ type : "radio" ,
101
+ checked : true
102
+ } ,
103
+ {
104
+ label : "Radio2" ,
105
+ type : "radio" ,
106
+ } ,
107
+ {
108
+ label : "Checkbox1" ,
109
+ type : "checkbox" ,
110
+ checked : true ,
111
+ click : ( item ) => {
112
+ console . log ( "item is checked? " + item . checked ) ;
113
+ }
114
+ } ,
115
+ { type : "separator" } ,
116
+ {
117
+ label : "Checkbox2" ,
118
+ type : "checkbox" ,
119
+ checked : false ,
120
+ click : ( item ) => {
121
+ console . log ( "item is checked? " + item . checked ) ;
122
+ }
123
+ } ,
124
+ {
125
+ label : "Radio Test" ,
126
+ submenu : [
127
+ {
128
+ label : "Sample Checkbox" ,
129
+ type : "checkbox" ,
130
+ checked : true
131
+ } ,
132
+ {
133
+ label : "Radio1" ,
134
+ checked : true ,
135
+ type : "radio"
136
+ } ,
137
+ {
138
+ label : "Radio2" ,
139
+ type : "radio"
140
+ } ,
141
+ {
142
+ label : "Radio3" ,
143
+ type : "radio"
144
+ } ,
145
+ { type : "separator" } ,
146
+ {
147
+ label : "Radio1" ,
148
+ checked : true ,
149
+ type : "radio"
150
+ } ,
151
+ {
152
+ label : "Radio2" ,
153
+ type : "radio"
154
+ } ,
155
+ {
156
+ label : "Radio3" ,
157
+ type : "radio"
158
+ }
159
+ ]
160
+ } ,
161
+ {
162
+ label : "zoomIn" ,
163
+ role : "zoomIn"
164
+ } ,
165
+ {
166
+ label : "zoomOut" ,
167
+ role : "zoomOut"
168
+ } ,
169
+ {
170
+ label : "Radio1" ,
171
+ type : "radio"
172
+ } ,
173
+ {
174
+ label : "Radio2" ,
175
+ checked : true ,
176
+ type : "radio"
177
+ } ,
178
+ ]
179
+ } ,
180
+ {
181
+ label : "View" ,
182
+ submenu : [
183
+ { role : "reload" } ,
184
+ { role : "forceReload" } ,
185
+ { type : "separator" } ,
186
+ { role : "zoomIn" } ,
187
+ { role : "zoomOut" } ,
188
+ { role : "resetZoom" } ,
189
+ { role : "toggleDevTools" }
190
+ ] ,
191
+ }
192
+ ] ;
0 commit comments