-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
160 lines (137 loc) · 3.84 KB
/
main.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
const {
app,
BrowserWindow,
ipcMain,
Notification,
Menu,
Tray,
dialog,
} = require("electron");
const { autoUpdater } = require("electron-updater");
const path = require("path");
const isDev = !app.isPackaged;
let updateInterval = null;
let mainApp = null;
const dockIcon = path.join(__dirname, "assets", "images", "logo1.png");
const trayIcon = path.join(__dirname, "assets", "images", "logo2.png");
function createSplashWindow() {
const win = new BrowserWindow({
width: 400,
height: 200,
frame: false,
transparent: true,
webPreferences: {
nodeIntegration: false,
worldSafeExecuteJavaScript: true,
contextIsolation: true,
},
});
win.loadFile("splash.html");
return win;
}
const createWindow = () => {
const win = new BrowserWindow({
width: 800,
height: 1280,
fullscreen: !isDev,
autoHideMenuBar: !isDev,
backgroundColor: "white",
webPreferences: {
nodeIntegration: false,
worldSafeExecuteJavascript: true,
contextIsolation: true,
preload: path.join(__dirname, "preload.js"),
},
});
win.loadFile("index.html");
isDev && win.webContents.openDevTools();
return win;
};
if (isDev) {
require("electron-reload")(__dirname, {
electron: path.join(__dirname, "node_modules", ".bin", "electron"),
hardResetMethod: "exit",
});
}
if (process.platform === "darwin") {
app.dock.setIcon(dockIcon);
}
let tray = null;
app.whenReady().then(() => {
const template = require("./utils/Menu").createTemplate(app);
const menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(menu);
tray = new Tray(trayIcon);
tray.setContextMenu(menu);
const splash = createSplashWindow();
mainApp = createWindow();
mainApp.once("ready-to-show", () => {
// splash.destroy();
// mainApp.show();
setTimeout(() => {
splash.destroy();
mainApp.show();
}, 2000);
// Update app only on startup
autoUpdater.checkForUpdates();
});
app.on("activate", () => {
if (BrowserWindow.getAllWindows().length === 0) createWindow();
});
// Update app at interval
// updateInterval = setInterval(() => autoUpdater.checkForUpdates(), 300000);
});
// Update app
autoUpdater.on("update-available", (_event, releaseNotes, releaseName) => {
const dialogOpts = {
type: "info",
buttons: ["Ok"],
title: "Mise à jour disponible",
message: process.platform === "win32" ? releaseNotes : releaseName,
detail:
"Une nouvelle version est disponible, elle est en cours de téléchargement.",
};
dialog.showMessageBox(dialogOpts);
updateInterval = null;
});
// Update app
autoUpdater.on("update-downloaded", (_event, releaseNotes, releaseName) => {
const dialogOpts = {
type: "info",
buttons: ["Redémarrer", "Plus tard"],
title: "Installation requise",
message: process.platform === "win32" ? releaseNotes : releaseName,
detail:
"Une mise à jour a été téléchargée. Redémarrez l'application pour l'installer.",
};
dialog.showMessageBox(dialogOpts).then((returnValue) => {
if (returnValue.response === 0) autoUpdater.quitAndInstall();
});
});
// Clear cache
ipcMain.handle('clear-cache', async () => {
try {
await mainApp.webContents.session.clearCache();
await mainApp.webContents.session.clearStorageData({
storages: ['cookies', 'indexeddb', 'websql']
});
return { success: true };
} catch (error) {
console.error('Error clearing cache:', error);
return { success: false, error };
}
});
// Notify
ipcMain.on("notify", (_, message) => {
new Notification({ title: "Notification", body: message }).show();
});
// App version
ipcMain.handle('get-app-version', () => {
return app.getVersion();
});
ipcMain.on("app-quit", () => {
app.quit();
});
app.on("window-all-closed", () => {
if (process.platform !== "darwin") app.quit();
});