-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
218 lines (191 loc) · 6.54 KB
/
Copy pathmain.js
File metadata and controls
218 lines (191 loc) · 6.54 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
const {
app,
BrowserWindow,
ipcMain,
Menu,
Tray,
contentTracing,
dialog,
} = require("electron"); // Import Electron modules
const path = require("path"); // Import Node.js path module
const {
getApps,
saveData,
loadData,
ifImageExists,
} = require("./getActiveApps.js"); // Import functions from getActiveApps.js
const {
toggleRunOnStartup,
toggleStartMinimised,
initializeSettings,
toggleCloseToTray,
exportSettings,
importSettings,
clearIconCache,
factoryReset,
clearHistory,
} = require("./settingsScripts.js"); // Import functions from settingsScripts.js
const Store = require("electron-store"); // Import electron-store module
let win = null; // Initialize the main window variable
// Ensure only a single instance of the application is running
const gotTheLock = app.requestSingleInstanceLock();
if (!gotTheLock) {
app.quit(); // Quit if another instance is already running
} else {
app.on("second-instance", () => {
if (win) {
if (win.isMinimized()) {
win.restore(); // Restore the window if it is minimized
}
win.show(); // Show the window
win.focus(); // Focus the window
}
});
}
// Function to create the main application window
function createWindow() {
win = new BrowserWindow({
width: 1280,
height: 720,
webPreferences: {
preload: path.join(__dirname, "preload.js"), // Preload script
contextIsolation: true, // Enable context isolation
enableRemoteModule: false, // Disable remote module
nodeIntegration: false, // Disable Node.js integration
devTools: false, // Disable developer tools
},
titleBarStyle: "hidden", // Hide the title bar
titleBarOverlay: {
color: "#ececec", // Set the title bar overlay color
height: 40, // Set the title bar overlay height
},
});
// Create a system tray icon
let tray = new Tray(path.join(__dirname, "/assets/icon.png"));
const contextMenu = Menu.buildFromTemplate([
{
label: "Show Screen Diary",
click: () => {
win.show(); // Show the window
win.focus(); // Focus the window
},
},
{ type: "separator" },
{
label: "Quit",
click: () => {
dialog
.showMessageBox(win, {
type: "question",
buttons: [
"Quit Screen Diary",
"Run Screen Diary in the Background",
"Cancel",
],
title: "Quit Screen Diary",
message: "Are you sure you want to quit Screen Diary?",
detail:
"If you quit Screen Diary, you will not be able to track your usage. Running Screen Diary in the background will continue to track your usage.\n\nYour data will be saved before quitting.",
detailType: "info",
icon: path.join(__dirname, "/assets/icon.png"),
})
.then((result) => {
if (result.response === 0) {
win.webContents.send("request-variable-from-renderer"); // Request variable from renderer
win.hide();
setTimeout(() => {
saveData(currentAppData); // Save the current app data
win.destroy(); // Destroy the window
app.quit(); // Quit the application
}, 1000); // Delay to ensure data is saved
} else if (result.response === 1) {
const win = BrowserWindow.getAllWindows()[0]; // Get the focused window
win.hide(); // Hide the window
} else {
return; // Do nothing if the user cancels
}
});
},
},
]);
tray.setToolTip("Screen Diary"); // Set the tray tooltip
tray.setContextMenu(contextMenu); // Set the tray context menu
tray.on("click", () => {
win.show(); // Show the window on tray icon click
win.focus(); // Focus the window on tray icon click
});
win.setMenuBarVisibility(false); // Hide the menu bar
win.loadFile("./index.html"); // Load the main HTML file
// Handle window close event for non-macOS platforms
win.on("close", () => {
if (process.platform !== "darwin") {
win.webContents.send("request-variable-from-renderer"); // Request variable from renderer
}
});
}
const store = new Store(); // Initialize electron-store
// When the application is ready, create the main window
app.whenReady().then(() => {
createWindow();
if (store.get("startMinimised") == true) {
win.hide(); // Hide the window if startMinimised setting is true
}
});
// Quit the application when all windows are closed, except on macOS
app.on("window-all-closed", () => {
if (process.platform !== "darwin") {
app.quit(); // Quit the application
}
});
// Handle IPC requests from the renderer process
ipcMain.handle("get-active-apps", async () => {
return await getApps(); // Return the active apps
});
ipcMain.handle("save-data-with-data", (event, processData) => {
return saveData(processData); // Save the provided data
});
ipcMain.handle("load-data", () => {
return loadData(); // Load the saved data
});
ipcMain.handle("image-exists", (event, path) => {
return ifImageExists(path); // Check if the image exists
});
let currentAppData = null; // Initialize the current app data variable
ipcMain.on("send-variable-to-main", (event, variable) => {
currentAppData = variable; // Update the current app data
});
ipcMain.handle("toggle-run-on-startup", async () => {
return await toggleRunOnStartup(); // Toggle the run on startup setting
});
ipcMain.handle("toggle-start-minimised", () => {
toggleStartMinimised(); // Toggle the start minimized setting
});
ipcMain.handle("toggle-close-to-tray", () => {
toggleCloseToTray(); // Toggle the close to tray setting
});
ipcMain.handle("get-settings", async () => {
return new Promise((resolve, reject) => {
resolve(initializeSettings()); // Initialize and return the settings
});
});
ipcMain.handle("set-accent-colour", async (event, colour) => {
store.set("accentColour", colour); // Set the accent colour
});
ipcMain.handle("get-store-value", async (event, key) => {
return store.get(key); // Get the value from the store
});
ipcMain.handle("export-settings", () => {
return exportSettings(); // Export the settings
});
ipcMain.handle("import-settings", () => {
return importSettings(); // Import the settings
});
ipcMain.handle("clear-icon-cache", () => {
return clearIconCache(); // Export the settings
});
ipcMain.handle("factory-reset", () => {
return factoryReset(); // Factory reset the application
});
ipcMain.handle("clear-history", () => {
return clearHistory(); // Clear the history
});