project-client/src/main/mainWindow.ts

100 lines
2.2 KiB
TypeScript
Raw Normal View History

import { BrowserWindow, Menu, Tray, app, shell } from "electron";
2023-03-30 08:02:30 +09:00
import { join } from "path";
2023-04-04 08:35:37 +09:00
import { ICON_PATH } from "../shared/paths";
2023-03-30 08:02:30 +09:00
let isQuitting = false;
2023-04-04 07:41:52 +09:00
app.on("before-quit", () => {
isQuitting = true;
});
function initWindowOpenHandler(win: BrowserWindow) {
win.webContents.setWindowOpenHandler(({ url }) => {
switch (url) {
case "about:blank":
case "https://discord.com/popout":
return { action: "allow" };
}
try {
var protocol = new URL(url).protocol;
} catch {
return { action: "deny" };
}
switch (protocol) {
case "http:":
case "https:":
case "mailto:":
case "steam:":
case "spotify:":
shell.openExternal(url);
}
return { action: "deny" };
2023-04-04 07:41:52 +09:00
});
}
2023-04-04 07:41:52 +09:00
function initTray(win: BrowserWindow) {
2023-04-04 08:35:37 +09:00
const trayMenu = Menu.buildFromTemplate([
{
label: "Open",
click() {
win.show();
},
enabled: false
},
{
label: "Quit Vencord Desktop",
click() {
isQuitting = true;
app.quit();
}
}
]);
const tray = new Tray(ICON_PATH);
tray.setToolTip("Vencord Desktop");
tray.setContextMenu(trayMenu);
tray.on("click", () => win.show());
win.on("show", () => {
trayMenu.items[0].enabled = false;
});
win.on("hide", () => {
trayMenu.items[0].enabled = true;
});
}
export function createMainWindow() {
const win = new BrowserWindow({
show: false,
autoHideMenuBar: true,
webPreferences: {
nodeIntegration: false,
sandbox: false,
contextIsolation: true,
devTools: true,
preload: join(__dirname, "preload.js")
},
icon: ICON_PATH
2023-04-04 07:41:52 +09:00
});
win.on("close", e => {
if (isQuitting) return;
e.preventDefault();
win.hide();
return false;
2023-03-30 08:02:30 +09:00
});
initTray(win);
initWindowOpenHandler(win);
2023-04-04 07:41:52 +09:00
2023-03-30 08:02:30 +09:00
win.loadURL("https://discord.com/app");
return win;
}