1
- import { BrowserWindow , Menu , app , dialog } from 'electron' ;
1
+ import { BrowserWindow , Menu , app , dialog , ipcMain } from 'electron' ;
2
2
import * as path from 'path' ;
3
3
import { format as formatUrl } from 'url' ;
4
4
import { getFilterForExtension } from './FileFilters' ;
@@ -13,20 +13,17 @@ const defaultSize = {width: 1280, height: 800}; // good for MAS screenshots
13
13
14
14
const isDevelopment = process . env . NODE_ENV !== 'production' ;
15
15
16
- const createMainWindow = ( ) => {
16
+ // global window references prevent them from being garbage-collected
17
+ const _windows = { } ;
18
+
19
+ const createWindow = ( { url, ...browserWindowOptions } ) => {
17
20
const window = new BrowserWindow ( {
18
- width : defaultSize . width ,
19
- height : defaultSize . height ,
20
21
useContentSize : true ,
21
- show : false
22
+ show : false ,
23
+ ...browserWindowOptions
22
24
} ) ;
23
25
const webContents = window . webContents ;
24
26
25
- if ( process . platform === 'darwin' ) {
26
- const osxMenu = Menu . buildFromTemplate ( MacOSMenu ( app ) ) ;
27
- Menu . setApplicationMenu ( osxMenu ) ;
28
- }
29
-
30
27
if ( isDevelopment ) {
31
28
webContents . openDevTools ( ) ;
32
29
import ( 'electron-devtools-installer' ) . then ( importedModule => {
@@ -46,15 +43,38 @@ const createMainWindow = () => {
46
43
} ) ;
47
44
48
45
if ( isDevelopment ) {
49
- window . loadURL ( `http://localhost:${ process . env . ELECTRON_WEBPACK_WDS_PORT } ` ) ;
46
+ window . loadURL ( `http://localhost:${ process . env . ELECTRON_WEBPACK_WDS_PORT } / ${ url } ` ) ;
50
47
} else {
51
48
window . loadURL ( formatUrl ( {
52
- pathname : path . join ( __dirname , 'index.html' ) ,
49
+ pathname : path . join ( __dirname , url ) ,
53
50
protocol : 'file' ,
54
51
slashes : true
55
52
} ) ) ;
56
53
}
57
54
55
+ return window ;
56
+ } ;
57
+
58
+ const createAboutWindow = ( ) => {
59
+ const window = createWindow ( {
60
+ width : 400 ,
61
+ height : 400 ,
62
+ parent : _windows . main ,
63
+ title : 'About Scratch Desktop' ,
64
+ url : 'index.html?route=about'
65
+ } ) ;
66
+ return window ;
67
+ } ;
68
+
69
+ const createMainWindow = ( ) => {
70
+ const window = createWindow ( {
71
+ width : defaultSize . width ,
72
+ height : defaultSize . height ,
73
+ title : 'Scratch Desktop' ,
74
+ url : 'index.html'
75
+ } ) ;
76
+ const webContents = window . webContents ;
77
+
58
78
webContents . session . on ( 'will-download' , ( ev , item ) => {
59
79
const itemPath = item . getFilename ( ) ;
60
80
const baseName = path . basename ( itemPath ) ;
@@ -104,6 +124,11 @@ const createMainWindow = () => {
104
124
return window ;
105
125
} ;
106
126
127
+ if ( process . platform === 'darwin' ) {
128
+ const osxMenu = Menu . buildFromTemplate ( MacOSMenu ( app ) ) ;
129
+ Menu . setApplicationMenu ( osxMenu ) ;
130
+ }
131
+
107
132
// quit application when all windows are closed
108
133
app . on ( 'window-all-closed' , ( ) => {
109
134
app . quit ( ) ;
@@ -113,13 +138,19 @@ app.on('will-quit', () => {
113
138
telemetry . appWillClose ( ) ;
114
139
} ) ;
115
140
116
- // global reference to mainWindow (necessary to prevent window from being garbage collected)
117
- let _mainWindow ;
118
-
119
141
// create main BrowserWindow when electron is ready
120
142
app . on ( 'ready' , ( ) => {
121
- _mainWindow = createMainWindow ( ) ;
122
- _mainWindow . on ( 'closed' , ( ) => {
123
- _mainWindow = null ;
143
+ _windows . main = createMainWindow ( ) ;
144
+ _windows . main . on ( 'closed' , ( ) => {
145
+ delete _windows . main ;
124
146
} ) ;
147
+ _windows . about = createAboutWindow ( ) ;
148
+ _windows . about . on ( 'close' , event => {
149
+ event . preventDefault ( ) ;
150
+ _windows . about . hide ( ) ;
151
+ } ) ;
152
+ } ) ;
153
+
154
+ ipcMain . on ( 'open-about-window' , ( ) => {
155
+ _windows . about . show ( ) ;
125
156
} ) ;
0 commit comments