-
Notifications
You must be signed in to change notification settings - Fork 0
/
electron.js
236 lines (204 loc) · 6.32 KB
/
electron.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// dialog isn't used, but not requiring it throws an error.
const {app, BrowserWindow, ipcMain, shell, dialog, Menu} = require('electron');
const path = require('path');
const url = require('url');
const fs = require('fs');
const os = require('os');
// 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.
let mainWindow,
appName = 'Moodle Desktop', // Default value.
isReady = false,
configRead = false;
function createWindow() {
// Create the browser window.
var width = 800,
height = 600;
const screen = require('electron').screen;
if (screen) {
const display = screen.getPrimaryDisplay();
if (display && display.workArea) {
width = display.workArea.width || width;
height = display.workArea.height || height;
}
}
mainWindow = new BrowserWindow({
width: width,
height: height,
minWidth: 400,
minHeight: 400,
textAreasAreResizable: false,
plugins: true,
show: false // Don't show it until it's ready to prevent showing a blank screen.
});
// And load the index.html of the app.
mainWindow.loadURL(url.format({
pathname: path.join(__dirname, 'index.html'),
protocol: 'file:',
slashes: true
}));
mainWindow.once('ready-to-show', () => {
mainWindow.show();
mainWindow.maximize();
});
// Emitted when the window is closed.
mainWindow.on('closed', () => {
// Dereference the window object.
mainWindow = null
});
}
// 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.
app.on('ready', function() {
isReady = true;
createWindow();
if (configRead) {
setAppMenu();
}
});
// Quit when all windows are closed.
app.on('window-all-closed', () => {
app.exit();
});
app.on('activate', () => {
// On macOS it's common to re-create a window in the app when the dock icon is clicked and there are no other windows open.
if (mainWindow === null) {
createWindow();
}
});
// Read the config.json file.
fs.readFile(path.join(__dirname, 'config.json'), 'utf8', (err, data) => {
configRead = true;
var ssoScheme = 'moodlemobile'; // Default value.
if (!err) {
try {
data = JSON.parse(data);
ssoScheme = data.customurlscheme;
appName = data.desktopappname;
} catch(ex) {}
}
// Set default protocol (custom URL scheme).
app.setAsDefaultProtocolClient(ssoScheme);
if (isReady) {
setAppMenu();
}
});
// Make sure that only a single instance of the app is running.
var shouldQuit = app.makeSingleInstance((argv, workingDirectory) => {
// Another instance was launched. If it was launched with a URL, it should be in the second param.
if (argv && argv[1]) {
appLaunched(argv[1]);
} else {
focusApp();
}
});
// For some reason, shouldQuit is always true in signed Mac apps so we should ingore it.
if (shouldQuit && os.platform().indexOf('darwin') == -1) {
// It's not the main instance of the app, kill it.
app.exit();
return;
}
// Listen for open-url events (Mac OS only).
app.on('open-url', (event, url) => {
event.preventDefault();
appLaunched(url);
});
function appLaunched(url) {
// App was launched again with a URL. Focus the main window and send an event to treat the URL.
if (mainWindow) {
focusApp();
mainWindow.webContents.send('mmAppLaunched', url); // Send an event to the main window.
}
}
function focusApp() {
if (mainWindow) {
if (mainWindow.isMinimized()) {
mainWindow.restore();
}
mainWindow.focus();
}
}
// Listen for events sent by the renderer processes (windows).
ipcMain.on('openItem', (event, path) => {
var result;
// Add file:// protocol if it isn't there.
if (path.indexOf('file://') == -1) {
path = 'file://' + path;
}
if (os.platform().indexOf('darwin') > -1) {
// Use openExternal in MacOS because openItem doesn't work in sandboxed apps.
// https://github.com/electron/electron/issues/9005
result = shell.openExternal(path);
} else {
result = shell.openItem(path);
}
if (!result) {
// Cannot open file, probably no app to handle it. Open the folder.
result = shell.showItemInFolder(path.replace('file://', ''));
}
event.returnValue = result;
});
ipcMain.on('closeSecondaryWindows', () => {
var windows = BrowserWindow.getAllWindows();
for (var i = 0; i < windows.length; i++) {
if (!mainWindow || windows[i].id != mainWindow.id) {
windows[i].close();
}
}
});
ipcMain.on('focusApp', focusApp);
// Configure the app's menu.
function setAppMenu() {
let menuTemplate = [
{
label: appName,
role: 'window',
submenu: [
{
label: 'Quit',
accelerator: 'CmdorCtrl+Q',
role: 'close'
}
]
},
{
label: 'Edit',
submenu: [
{
label: 'Cut',
accelerator: 'CmdOrCtrl+X',
role: 'cut'
},
{
label: 'Copy',
accelerator: 'CmdOrCtrl+C',
role: 'copy'
},
{
label: 'Paste',
accelerator: 'CmdOrCtrl+V',
role: 'paste'
},
{
label: 'Select All',
accelerator: 'CmdOrCtrl+A',
role: 'selectall'
}
]
},
{
label: 'Help',
role: 'help',
submenu: [
{
label: 'Docs',
accelerator: 'CmdOrCtrl+H',
click() {
shell.openExternal('https://docs.moodle.org/en/Moodle_Mobile');
}
}
]
}
];
Menu.setApplicationMenu(Menu.buildFromTemplate(menuTemplate));
}