-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.ts
151 lines (137 loc) · 5 KB
/
main.ts
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
import { app, BrowserWindow, screen, ipcMain, dialog } from 'electron'; //electron modules
import log from 'electron-log' // helps with logging to file for main process
console.log = log.log; //Logs all console messages in the main process to a file for debug purposes.
import { autoUpdater } from 'electron-updater'; //handles updates
import isDev = require("electron-is-dev"); // detects if we are in dev mode
import windowStateKeeper from 'electron-window-state';
ipcMain.on('app_version', (event) => {
console.log(`Glimboi dev mode: ${isDev}`);
console.log("The current version is " + app.getVersion());
event.sender.send('app_version', { version: app.getVersion() });
if (isDev) {
return
}
autoUpdater.logger = log
autoUpdater.autoDownload = true;
autoUpdater.autoInstallOnAppQuit = true;
console.log(autoUpdater.currentVersion);
autoUpdater.checkForUpdates();
});
autoUpdater.on('update-available', () => {
win.webContents.send('update_available');
console.log("avaible update!")
});
autoUpdater.on('update-downloaded', () => {
win.webContents.send('update_downloaded');
console.log("downloaded the update!")
});
ipcMain.on('restart_app', () => {
autoUpdater.quitAndInstall();
});
let win: BrowserWindow; // The main window
function createWindow() {
const { width, height } = screen.getPrimaryDisplay().workAreaSize;
let mainWindowState = windowStateKeeper({
defaultWidth: width,
defaultHeight: height
});
win = new BrowserWindow({
width: mainWindowState.width,
height: mainWindowState.height,
x: mainWindowState.x,
y: mainWindowState.y,
backgroundColor: "#060818",
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
backgroundThrottling: false,
},
frame: false
})
win.loadFile(__dirname + '\\../src/index.html');
win.setIcon('resources/Icons/icon.png');
mainWindowState.manage(win);
}
app.whenReady().then(createWindow);
// send info to the renderer so it can use glimboi modules. This MUST come first and it needs to be very fast.
ipcMain.on("appDataRequest", (event) => {
event.returnValue = [__dirname, app.getPath("userData"), isDev]
})
ipcMain.on("window", async (event, arg: "close" | "import" | "maximize" | "minimize" | "refresh", path?: string, files?: { name: string, copy: boolean }[]) => {
switch (arg) {
case "close":
win.close();
console.log("Recieved close request, closing.");
break;
case "maximize":
win.maximize();
console.log("Maximizing window");
break;
case "minimize":
win.minimize();
console.log("Minimizing Window");
break;
case "refresh":
win.reload();
console.log("Reloading Window");
break;
case "import":
console.log("Importing new data, closing window");
let { copyFile } = require("fs");
// do file stuff
for (let file of files) {
try {
await copyFile(`${path}/${file.name}.db`, `${app.getPath("userData")}/data/${file.name}.db`, 0, () => {});
} catch (e) {
console.error(e);
dialog.showMessageBoxSync(null, {
message: "Error importing data file. Please restart Glimboi.",
type: "error",
title: "Error",
})
app.quit();
break;
}
}
dialog.showMessageBoxSync(null, {message: "Data imported successfully! Glimboi will now restart.", type: "info", title: "Success"});
win.reload();
}
});
ipcMain.handle("getLogLocation", async (event) => {
let fileSelection = await dialog.showSaveDialog(win, {
title: "Create Chat Log:", defaultPath: app.getPath("logs"), buttonLabel: "Create/Write",
properties: ['showOverwriteConfirmation'], filters: [{ name: "Chat Logs", extensions: ["txt"] }]
})
return fileSelection;
})
ipcMain.handle("backup", async (event) => {
let folderSelection = await dialog.showOpenDialog(win, {
title: "Select Backup Folder:", defaultPath: app.getPath("desktop"), buttonLabel: "Select",
properties: ['openDirectory']
});
if (folderSelection.canceled) {
return null
}
return folderSelection;
})
ipcMain.handle("openPath", (event, path: string, location: "internal" | "external") => {
let shell = require("electron").shell;
if (location == "internal") {
shell.openPath(path);
} else {
shell.openExternal(path, {activate: true});
}
return;
})
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit();
} else {
console.log("Closed");
}
})
app.on('activate', () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
})