1
1
'use strict'
2
2
3
3
import { app , protocol , BrowserWindow } from 'electron'
4
- import * as path from 'path'
5
- import { format as formatUrl } from 'url'
6
4
import {
7
5
createProtocol ,
8
6
installVueDevtools
@@ -13,64 +11,55 @@ if (isDevelopment) {
13
11
require ( 'module' ) . globalPaths . push ( process . env . NODE_MODULES_PATH )
14
12
}
15
13
16
- // global reference to mainWindow (necessary to prevent window from being garbage collected)
17
- let mainWindow
14
+ // Keep a global reference of the window object, if you don't, the window will
15
+ // be closed automatically when the JavaScript object is garbage collected.
16
+ let win
18
17
19
18
// Standard scheme must be registered before the app is ready
20
19
protocol . registerStandardSchemes ( [ 'app' ] , { secure : true } )
21
- function createMainWindow ( ) {
22
- const window = new BrowserWindow ( )
20
+ function createWindow ( ) {
21
+ // Create the browser window.
22
+ win = new BrowserWindow ( { width : 800 , height : 600 } )
23
23
24
24
if ( isDevelopment ) {
25
25
// Load the url of the dev server if in development mode
26
- window . loadURL ( process . env . WEBPACK_DEV_SERVER_URL )
27
- if ( ! process . env . IS_TEST ) window . webContents . openDevTools ( )
26
+ win . loadURL ( process . env . WEBPACK_DEV_SERVER_URL )
27
+ if ( ! process . env . IS_TEST ) win . webContents . openDevTools ( )
28
28
} else {
29
29
createProtocol ( 'app' )
30
- // Load the index.html when not in development
31
- window . loadURL (
32
- formatUrl ( {
33
- pathname : path . join ( __dirname , 'index.html' ) ,
34
- protocol : 'file' ,
35
- slashes : true
36
- } )
37
- )
30
+ // Load the index.html when not in development
31
+ win . loadFile ( 'index.html' )
38
32
}
39
33
40
- window . on ( 'closed' , ( ) => {
41
- mainWindow = null
34
+ win . on ( 'closed' , ( ) => {
35
+ win = null
42
36
} )
43
-
44
- window . webContents . on ( 'devtools-opened' , ( ) => {
45
- window . focus ( )
46
- setImmediate ( ( ) => {
47
- window . focus ( )
48
- } )
49
- } )
50
-
51
- return window
52
37
}
53
38
54
- // quit application when all windows are closed
39
+ // Quit when all windows are closed.
55
40
app . on ( 'window-all-closed' , ( ) => {
56
- // on macOS it is common for applications to stay open until the user explicitly quits
41
+ // On macOS it is common for applications and their menu bar
42
+ // to stay active until the user quits explicitly with Cmd + Q
57
43
if ( process . platform !== 'darwin' ) {
58
44
app . quit ( )
59
45
}
60
46
} )
61
47
62
48
app . on ( 'activate' , ( ) => {
63
- // on macOS it is common to re-create a window even after all windows have been closed
64
- if ( mainWindow === null ) {
65
- mainWindow = createMainWindow ( )
49
+ // On macOS it's common to re-create a window in the app when the
50
+ // dock icon is clicked and there are no other windows open.
51
+ if ( win === null ) {
52
+ createWindow ( )
66
53
}
67
54
} )
68
55
69
- // create main BrowserWindow when electron is ready
56
+ // This method will be called when Electron has finished
57
+ // initialization and is ready to create browser windows.
58
+ // Some APIs can only be used after this event occurs.
70
59
app . on ( 'ready' , async ( ) => {
71
60
if ( isDevelopment && ! process . env . IS_TEST ) {
72
61
// Install Vue Devtools
73
62
await installVueDevtools ( )
74
63
}
75
- mainWindow = createMainWindow ( )
64
+ createWindow ( )
76
65
} )
0 commit comments