Skip to content

Commit d6888aa

Browse files
committed
imrpoved Electron code in main.js and added comments
1 parent b82bc33 commit d6888aa

File tree

6 files changed

+149
-136
lines changed

6 files changed

+149
-136
lines changed

main.js

Lines changed: 125 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
11
const path = require('path');
2-
//sfsdfsdfsdfasdasd
3-
//sdfasdfsadf
4-
5-
// ** electron notes:
6-
// IPCMain and IPCRenderer are used to send messages between main and renderer processes.
7-
// IPCMain - The main process takes care of starting and running your app. It is the entry point for an application.
8-
// IPCRenderer - The renderer process takes care of showing your app in the Chromium browser
92

103
const {
11-
// ** look into how this is working - the code is the same here as in react proto **
124
app,
13-
//app - the lifecycle of the application is managed through electron.app
5+
// The app module controls the application lifecycle (like reacting to the ready state of the application).
146
BrowserWindow,
15-
// BrowserWindow - windows can be created using the electron.BrowserWindow class
7+
// BrowserWindow - allows for window creation via Electron. This is what actually shows something for the user. T
168
Menu,
179
// Menu - allows you to create native application menus and context menus.
1810
shell,
@@ -21,123 +13,98 @@ const {
2113
// dialog - Display native system dialogs for opening and saving files, alerting, etc
2214
ipcMain
2315
// ipcMain - Communicate asynchronously from the main process to renderer processes. It is an event emitter.
16+
// IPC stands for Inter-Process Communication. Electron uses IPC to send serialized JSON messages between the main and renderer processes.
2417
} = require('electron');
2518

2619
// ** Uncomment below for hot reloading during development **
2720

28-
// below hard-resets the entire electron process so is more of a catch-all in terms of dev mode
29-
require('electron-reload')(__dirname, {
30-
electron: path.join(__dirname, 'node_modules', '.bin', 'electron')
31-
});
21+
// below hard-resets the entire electron process so is more of a catch-all in terms of dev mode:
22+
// require('electron-reload')(__dirname, {
23+
// electron: path.join(__dirname, 'node_modules', '.bin', 'electron')
24+
// });
3225

33-
// below loads contents of all active BrowserWindows within electron when the source files are changed:
26+
// below loads contents of all active BrowserWindows within electron when the source files are changed.
27+
// ** You'll want to use this one when working in most files other than main.js
3428
// require('electron-reload')(__dirname);
3529

3630
const isDev =
3731
process.env.NODE_ENV === 'development' || process.env.NODE_ENV === 'test';
3832

39-
// 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
4135
let mainWindow;
4236

43-
// Open image file
44-
function openFile() {
45-
// Opens file dialog looking for markdown
46-
const files = dialog.showOpenDialog(mainWindow, {
47-
// dialog.showOpenDialog - allows you to open files / folders
48-
properties: ['openFile'],
49-
filters: [
50-
{
51-
name: 'Images',
52-
extensions: ['jpeg', 'jpg', 'png', 'gif', 'pdf']
53-
}
54-
]
55-
});
56-
57-
// if no files
58-
if (!files) return;
59-
const file = files[0];
60-
61-
// Send fileContent to renderer
62-
mainWindow.webContents.send('new-file', file);
63-
}
64-
65-
// Choose directory
66-
ipcMain.on('choose_app_dir', (event) => {
67-
const directory = dialog.showOpenDialog(mainWindow, {
68-
properties: ['openDirectory'],
69-
buttonLabel: 'Export'
70-
});
71-
72-
if (!directory) return;
73-
event.sender.send('app_dir_selected', directory[0]);
74-
});
75-
76-
ipcMain.on('view_app_dir', (event, appDir) => {
77-
shell.openItem(appDir);
78-
});
79-
80-
// Update file
81-
ipcMain.on('update-file', () => {
82-
openFile();
83-
});
84-
8537
const createWindow = () => {
86-
// Create the browser window.
87-
// eslint-disable-next-line
88-
const { width, height } = require('electron').screen.getPrimaryDisplay().size;
38+
// 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
8945
mainWindow = new BrowserWindow({
46+
// width & height have been extracted from the call to screen.getPrimaryDisplay() so that they match the full size of the users screen
9047
width,
9148
height,
9249
webPreferences: {
93-
zoomFactor: 0.7,
94-
'node-Integration': false
50+
// webFactor: default is 1.0 which is 100%
51+
zoomFactor: 0.8,
9552
},
53+
// if show is set to true, a blank window will appear at first until the rest of the application has loaded.
9654
show: false,
55+
9756
icon: path.join(__dirname, '/src/public/icons/mac/icon.icns'),
9857
win: {
9958
icon: path.join(__dirname, '/src/public/icons/win/icon.ico'),
10059
target: ['portable']
10160
}
10261
});
10362

104-
// and load the index.html of the app.
63+
64+
// mainWindow.loadURL returns a promise that then loads the local index.html file once the page has finished loading
10565
mainWindow.loadURL(`file://${__dirname}/build/index.html`);
106-
// load page once window is loaded
66+
// mainWindow.once adds a one time listener function for the 'ready-to-show' event. The event listener is removed after it is invoked once.
10767
mainWindow.once('ready-to-show', () => {
68+
// .show - shows and gives focus to the window it's called on.
10869
mainWindow.show();
10970
});
11071

72+
// this template will be used when creating the menu for our app - the template allows us to add additional menu items to the default menu item set
11173
const template = [
74+
{
75+
// label appears along the top of the menu on mac
76+
label: 'About',
77+
submenu: [
78+
{
79+
label: 'ReacType Info',
80+
// click() executes a function you've defined when the user clicks on the specific submenu item
81+
click() {
82+
// shell.openExternal - returns a promise and then opens an external link in the user's default browser
83+
shell.openExternal('https://github.com/oslabs-beta/ReacType');
84+
}
85+
}
86+
]
87+
},
11288
{
11389
label: 'File',
11490
submenu: [
11591
{
11692
label: 'Open File',
93+
// The process.platform property returns a string identifying the operating system platform on which the Node.js process is running.
94+
// accelerator is used to define keyboard shortcuts
11795
accelerator: process.platform === 'darwin' ? 'Cmd+O' : 'Ctrl+Shift+O',
96+
// click() allows you to define the functionality that will occur when the user clicks on an item in the submenu
11897
click() {
98+
11999
openFile();
120100
}
121101
}
122102
]
123103
},
124-
// {
125-
// label: 'Edit',
126-
// submenu: [
127-
// { role: 'undo' },
128-
// { role: 'redo' },
129-
// { type: 'separator' },
130-
// { role: 'cut' },
131-
// { role: 'copy' },
132-
// { role: 'paste' },
133-
// { role: 'pasteandmatchstyle' },
134-
// { role: 'delete' },
135-
// { role: 'selectall' },
136-
// ],
137-
// },
138104
{
139105
label: 'View',
140106
submenu: [
107+
// role can be set to many different strings: https://www.electronjs.org/docs/api/menu-item
141108
{ role: 'reload' },
142109
{ role: 'forcereload' },
143110
{ type: 'separator' },
@@ -164,13 +131,14 @@ const createWindow = () => {
164131
]
165132
},
166133
{
167-
label: 'Developer',
134+
label: 'Developer Tools',
168135
submenu: [
169136
{
170137
label: 'Toggle Developer Tools',
171138
accelerator:
172139
process.platform === 'darwin' ? 'Alt+Command+I' : 'Ctrl+Shift+I',
173140
click() {
141+
// open the console in the right side of the window
174142
mainWindow.webContents.toggleDevTools();
175143
}
176144
}
@@ -179,7 +147,9 @@ const createWindow = () => {
179147
];
180148

181149
if (process.platform === 'darwin') {
150+
// if this isn't triggered, the app won't properly load on Mac but I haven't found a solid answer as to why that is.
182151
template.unshift({
152+
// app.getName() - returns a string that is the current application's name as found in the package.json file
183153
label: app.getName(),
184154
submenu: [
185155
{ role: 'about' },
@@ -193,74 +163,105 @@ const createWindow = () => {
193163
{ role: 'quit' }
194164
]
195165
});
196-
197-
// Edit menu
198-
template[2].submenu.push(
199-
{
200-
type: 'separator'
201-
},
202-
{
203-
label: 'Speech',
204-
submenu: [{ role: 'startspeaking' }, { role: 'stopspeaking' }]
205-
}
206-
);
207-
208-
// Window menu
209-
template[4].submenu = [
210-
{ role: 'close' },
211-
{ role: 'minimize' },
212-
{ role: 'zoom' },
213-
{ type: 'separator' },
214-
{ role: 'front' }
215-
];
216166
}
217167

168+
// 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
218170
const menu = 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.
219172
Menu.setApplicationMenu(menu);
220173

221-
// Emitted when the window is closed.
222174
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.
226176
mainWindow = null;
227177
});
178+
228179
};
229180

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+
function openFile() {
183+
// Opens file dialog looking for markdown
184+
const files = 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+
const file = 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+
const directory = 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
222+
event.sender.send('app_dir_selected', directory[0]);
223+
});
224+
225+
ipcMain.on('view_app_dir', (event, appDir) => {
226+
// 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.
233232
app.on('ready', () => {
234233
if (isDev) {
235234
const {
236235
default: installExtension,
237236
REACT_DEVELOPER_TOOLS,
238237
REDUX_DEVTOOLS
239238
} = require('electron-devtools-installer');
240-
241-
installExtension([REACT_DEVELOPER_TOOLS, REDUX_DEVTOOLS])
242-
.then(() => {
243-
createWindow();
244-
})
245-
.catch((err) => err);
239+
[REACT_DEVELOPER_TOOLS, REDUX_DEVTOOLS].forEach(extension => {
240+
installExtension(extension)
241+
// uncomment code below to test whether extensions have been installed correctly
242+
.then(/*(name) => console.log(`Added Extension: ${name}`)*/)
243+
.catch((err) => console.log('An error occurred: ', err));
244+
});
245+
createWindow();
246246
} else {
247+
// if not working in dev, we still want to create our window
247248
createWindow();
248249
}
249250
});
250251

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-
});
259252

260253
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.
263255
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.
264257
createWindow();
265258
}
266259
});
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
264+
if (process.platform !== 'darwin') {
265+
app.quit();
266+
}
267+
});

0 commit comments

Comments
 (0)