You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Keep a global reference of the window object, if you don't, the window will
40
-
// be closed automatically when the JavaScript object is garbage collected.
33
+
// Keep a global reference of the window object, if you don't, the window will be closed automatically when the JavaScript object is garbage collected.
34
+
// We'll be working with one Window (mainWindow) in our application
41
35
letmainWindow;
42
36
43
-
// Open image file
44
-
functionopenFile(){
45
-
// Opens file dialog looking for markdown
46
-
constfiles=dialog.showOpenDialog(mainWindow,{
47
-
// dialog.showOpenDialog - allows you to open files / folders
// Creates the browser window. This is triggered in an event listener that ensures that the content is fully loaded before creating the Window.
39
+
const{ width, height }=require('electron').
40
+
screen.getPrimaryDisplay().size;
41
+
// screen is an event emitter in electron. It can grant you information about the user's screen (or screens in the case that they have external monitors)
42
+
// getPrimaryDisplay() - returns the display object which contains properties like size (which gives us the size of the screen)
43
+
44
+
// create a new BrowserWindow within the createWindow function
89
45
mainWindow=newBrowserWindow({
46
+
// width & height have been extracted from the call to screen.getPrimaryDisplay() so that they match the full size of the users screen
90
47
width,
91
48
height,
92
49
webPreferences: {
93
-
zoomFactor: 0.7,
94
-
'node-Integration': false
50
+
// webFactor: default is 1.0 which is 100%
51
+
zoomFactor: 0.8,
95
52
},
53
+
// if show is set to true, a blank window will appear at first until the rest of the application has loaded.
// Menu.buildFromTemplate adds to the default Menu using properties from the template passed into it
169
+
// the default menu props: File, Edit, View, Window and Help
218
170
constmenu=Menu.buildFromTemplate(template);
171
+
// Menu.setApplicationMenu - Sets menu as the application menu on macOS. On Windows and Linux, the menu will be set as each window's top menu.
219
172
Menu.setApplicationMenu(menu);
220
173
221
-
// Emitted when the window is closed.
222
174
mainWindow.on('closed',()=>{
223
-
// Dereference the window object, usually you would store windows
224
-
// in an array if your app supports multi windows, this is the time
225
-
// when you should delete the corresponding element.
175
+
// Emitted when the window is going to be closed. It's emitted before the beforeunload and unload event of the DOM.
226
176
mainWindow=null;
227
177
});
178
+
228
179
};
229
180
230
-
// This method will be called when Electron has finished
231
-
// initialization and is ready to create browser windows.
232
-
// Some APIs can only be used after this event occurs.
181
+
// Open local image file for prototyping
182
+
functionopenFile(){
183
+
// Opens file dialog looking for markdown
184
+
constfiles=dialog.showOpenDialog(mainWindow,{
185
+
// dialog.showOpenDialog - allows you to open files / folders
186
+
// properties: ['openFile'] - allows files to be selected
187
+
properties: ['openFile'],
188
+
filters: [
189
+
{
190
+
// ensure only image files with specific extensions can be selected and uploaded into the app
191
+
name: 'Images',
192
+
extensions: ['jpeg','jpg','png','gif','pdf']
193
+
}
194
+
]
195
+
});
196
+
197
+
// if no files
198
+
if(!files)return;
199
+
constfile=files[0];
200
+
201
+
// Send fileContent to renderer
202
+
mainWindow.webContents.send('new-file',file);
203
+
// webContents is an EventEmitter. It is responsible for rendering and controlling a web page and is a property of the BrowserWindow object
204
+
}
205
+
206
+
// Update file
207
+
ipcMain.on('update-file',()=>{
208
+
// openFile defined above - allows user to open a file from their local file system
209
+
openFile();
210
+
});
211
+
212
+
// Choose directory to save / export your project
213
+
ipcMain.on('choose_app_dir',(event)=>{
214
+
constdirectory=dialog.showOpenDialog(mainWindow,{
215
+
// openDirectory allows directories to be opened - 'Export' button appears from the directory selection pop-up after the user has clicked `export project` and selected their preferred export type (`Export components` or `Export components with application files`)
216
+
properties: ['openDirectory'],
217
+
buttonLabel: 'Export'
218
+
});
219
+
// if no directory is selected, do not export the files
220
+
if(!directory)return;
221
+
// event.sender.send sends to the main frame. Allows user to send the file to their file system / save it locally
// shell.openItem - opens the given file in the desktop's default manner
227
+
shell.openItem(appDir);
228
+
});
229
+
230
+
231
+
// This method will be called when Electron has finished initialization and is ready to create browser windows. Some APIs can only be used after this event occurs.
// if not working in dev, we still want to create our window
247
248
createWindow();
248
249
}
249
250
});
250
251
251
-
// Quit when all windows are closed.
252
-
app.on('window-all-closed',()=>{
253
-
// On OS X it is common for applications and their menu bar
254
-
// to stay active until the user quits explicitly with Cmd + Q
255
-
if(process.platform!=='darwin'){
256
-
app.quit();
257
-
}
258
-
});
259
252
260
253
app.on('activate',()=>{
261
-
// On OS X it's common to re-create a window in the app when the
262
-
// dock icon is clicked and there are no other windows open.
254
+
// activate can be triggered when launching the application for the first time, attempting to re-launch the application when it's already running, or clicking on the application's dock or taskbar icon.
263
255
if(mainWindow===null){
256
+
// we're only working with one main window in our application, so if that is set to null it means the window is not open. We want to open it when the user clicks on the icon in the dock.
264
257
createWindow();
265
258
}
266
259
});
260
+
261
+
// Quit when all windows are closed.
262
+
app.on('window-all-closed',()=>{
263
+
// On OS X it is common for applications and their menu bar to stay active until the user quits explicitly with Cmd + Q
0 commit comments