-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.js
More file actions
224 lines (202 loc) · 6.22 KB
/
main.js
File metadata and controls
224 lines (202 loc) · 6.22 KB
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
/**
* Main entry point for the Electron application.
* This file sets up the main process, creates windows, and handles app lifecycle.
*/
// Import required modules
const { app, BrowserWindow, session, nativeImage, Menu, globalShortcut } = require('electron');
const path = require('node:path');
const { ipcMain } = require('electron');
const fs = require('node:fs');
const os = require('os');
const { loadPreferences, savePreferences } = require("./electronUtils/preferences");
const { loadIpcFunctions } = require("./electronUtils/ipcUtils");
const package = require('./package.json');
// Set development mode flag
const isDev = process.env.NODE_ENV === 'development';
// Initialize window management
const windows = [];
let splashWindow = null;
// Check if running on macOS
const isMac = process.platform === 'darwin';
// Set up file paths
const documentsFolderPath = path.join(os.homedir(), 'Documents', 'Processing Collaborative Sketches');
// Load user preferences
const userPreferences = loadPreferences();
const version = package.version;
/**
* Define menu template for the application
* @type {Electron.MenuItemConstructorOptions[]}
*/
const template = [
{
label: app.name,
role: 'appMenu',
submenu: [
{ label: 'About ' + app.name, role: 'about' },
{ type: 'separator' },
{ role: 'quit' }
]
},
{
label: 'File',
submenu: [
{ label: 'New window', accelerator: 'CmdOrCtrl+N', click: () => createWindow('') },
{ type: 'separator' },
{ label: 'Exit', role: 'quit' }
]
},
{
label: 'Edit',
submenu: [
{ label: 'Undo', role: 'undo' },
{ label: 'Redo', role: 'redo' },
{ type: 'separator' },
{ label: 'Cut', role: 'cut' },
{ label: 'Copy', role: 'copy' },
{ label: 'Paste', role: 'paste' }
]
},
{
label: 'View',
submenu: [
{
label: 'Enable Dark Mode',
type: 'checkbox',
checked: userPreferences.theme === 'dark',
click: () => {
savePreferences({theme: userPreferences.theme === 'dark' ? 'light' : 'dark'});
// Relaunch the app
app.relaunch();
app.exit(0); // Exit the current instance with a status code of 0 (success)
},
},
{ role: 'reload' },
{ role: 'toggledevtools' }
]
}
];
/**
* Creates the sketch folder in the user's Documents directory
*/
function createSketchFolder() {
try {
if (!fs.existsSync(documentsFolderPath)) {
fs.mkdirSync(documentsFolderPath, { recursive: true });
}
console.log('Access granted to Documents folder');
} catch (error) {
console.error('No permission to access Documents folder:', error);
}
}
/**
* Creates a new application window
* @param {string} urlPath - The URL path to load in the new window
*/
const createWindow = (urlPath = '') => {
let options = {
width: 1000,
height: 800,
minWidth: 800,
minHeight: 600,
webPreferences: {
preload: path.join(__dirname, 'electronUtils/preload.js'),
nodeIntegration: true,
contextIsolation: true,
},
titleBarStyle: 'hidden',
};
if (BrowserWindow.getFocusedWindow()) {
const focusedWindow = BrowserWindow.getFocusedWindow();
const pos = focusedWindow.getPosition();
options = {
...options,
x: pos[0] + 20,
y: pos[1] + 20,
}
}
const newWindow = new BrowserWindow(options);
windows.push(newWindow);
loadWindow(newWindow, urlPath);
}
/**
* Loads the specified URL in the given window
* @param {Electron.BrowserWindow} window - The window to load the URL in
* @param {string} urlPath - The URL path to load
*/
const loadWindow = (window, urlPath = '') => {
const windowUrl = isDev ?
`http://localhost:5173#/theme/${userPreferences.theme}/${urlPath}`
:
`file://${path.join(__dirname, 'build', 'index.html')}#theme/${userPreferences.theme}/${urlPath}`;
window.loadURL(windowUrl);
window.hide();
window.once('ready-to-show', () => {
if (splashWindow) {
setTimeout(() => {
splashWindow.close();
splashWindow = null;
window.show();
}, 2000);
} else {
window.show();
}
});
}
/**
* Loads the splash window
*/
const loadSplashWindow = () => {
splashWindow = new BrowserWindow({
width: 800,
height: 600,
frame: false,
alwaysOnTop: true,
transparent: true,
});
splashWindow.loadFile(path.join(__dirname, 'splash.html'));
}
// App ready event handler
app.whenReady().then(async () => {
loadSplashWindow();
createSketchFolder();
const menu = Menu.buildFromTemplate(template)
Menu.setApplicationMenu(menu);
createWindow();
});
// Window creation event handler
app.on('browser-window-created', () => {
// Add any necessary logic for new window creation
})
// Window close event handler
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') app.quit();
});
// Window focus event handler
app.on('browser-window-focus', function () {
// globalShortcut.register("CommandOrControl+R", () => {
// console.log("CommandOrControl+R is pressed: Shortcut Disabled");
// });
globalShortcut.register("F5", () => {
console.log("F5 is pressed: Shortcut Disabled");
});
});
// Window blur event handler
app.on('browser-window-blur', function () {
globalShortcut.unregister('CommandOrControl+R');
globalShortcut.unregister('F5');
});
// Set about panel options
app.setAboutPanelOptions({
iconPath: '/assets/PCE_icon_48x48.png',
applicationName: "Processing Collaborative Editor",
applicationVersion: "App Version",
version: version,
credits: "Dora Do",
copyright: "Copyright"
});
// Load IPC functions
loadIpcFunctions();
// IPC handler for opening a new window
ipcMain.handle('open-new-window', (event, urlPath) => {
createWindow(urlPath);
});