feat: rewrite minimize to tray

This commit is contained in:
smartfrigde 2024-06-16 12:59:30 +02:00
parent a50f4ca297
commit ac1d090ead
4 changed files with 30 additions and 33 deletions

5
src/common/forceQuit.ts Normal file
View file

@ -0,0 +1,5 @@
export let forceQuit = false;
export function setForceQuit(e: boolean): void {
forceQuit = e;
}

View file

@ -1,6 +1,7 @@
import {BrowserWindow, Menu, app} from "electron"; import {BrowserWindow, Menu, app} from "electron";
import {mainWindows} from "./window.js"; import {mainWindows} from "./window.js";
import {createSettingsWindow} from "../settings/main.js"; import {createSettingsWindow} from "../settings/main.js";
import {setForceQuit} from "../common/forceQuit.js";
export function setMenu(): void { export function setMenu(): void {
const template: Electron.MenuItemConstructorOptions[] = [ const template: Electron.MenuItemConstructorOptions[] = [
@ -48,6 +49,7 @@ export function setMenu(): void {
label: "Quit", label: "Quit",
accelerator: "CmdOrCtrl+Q", accelerator: "CmdOrCtrl+Q",
click() { click() {
setForceQuit(true);
app.quit(); app.quit();
} }
} }

View file

@ -12,13 +12,14 @@ import * as fs from "fs";
import contextMenu from "electron-context-menu"; import contextMenu from "electron-context-menu";
import os from "os"; import os from "os";
import RPCServer from "arrpc"; import RPCServer from "arrpc";
import {isQuitting, tray} from "../tray.js"; import {tray} from "../tray.js";
import {iconPath, init} from "../main.js"; import {iconPath, init} from "../main.js";
import {getConfig, setConfig, firstRun} from "../common/config.js"; import {getConfig, setConfig, firstRun} from "../common/config.js";
import {getWindowState, setWindowState} from "../common/windowState.js"; import {getWindowState, setWindowState} from "../common/windowState.js";
import {forceQuit, setForceQuit} from "../common/forceQuit.js";
export let mainWindows: BrowserWindow[] = []; export let mainWindows: BrowserWindow[] = [];
export let inviteWindow: BrowserWindow; export let inviteWindow: BrowserWindow;
export let forceQuit = false;
let osType = os.type(); let osType = os.type();
contextMenu({ contextMenu({
showSaveImageAs: true, showSaveImageAs: true,
@ -238,12 +239,18 @@ function doAfterDefiningTheWindow(passedWindow: BrowserWindow): void {
}); });
setMenu(); setMenu();
passedWindow.on("close", (e) => { passedWindow.on("close", (e) => {
if (process.platform === "darwin" && forceQuit) { if (mainWindows.length > 1) {
passedWindow.close();
} else if (mainWindows.length > 1) {
mainWindows = mainWindows.filter((mainWindow) => mainWindow.id != passedWindow.id); mainWindows = mainWindows.filter((mainWindow) => mainWindow.id != passedWindow.id);
passedWindow.destroy(); passedWindow.destroy();
} else { }
if (getConfig("minimizeToTray") && !forceQuit) {
e.preventDefault();
passedWindow.hide();
} else if (!getConfig("minimizeToTray")) {
app.quit();
}
});
app.on("before-quit", () => {
const [width, height] = passedWindow.getSize(); const [width, height] = passedWindow.getSize();
setWindowState({ setWindowState({
width, width,
@ -252,25 +259,8 @@ function doAfterDefiningTheWindow(passedWindow: BrowserWindow): void {
x: passedWindow.getPosition()[0], x: passedWindow.getPosition()[0],
y: passedWindow.getPosition()[1] y: passedWindow.getPosition()[1]
}); });
if (getConfig("minimizeToTray") && !isQuitting) { setForceQuit(true);
e.preventDefault();
passedWindow.hide();
} else if (!getConfig("minimizeToTray")) {
e.preventDefault();
app.quit();
}
}
}); });
if (process.platform === "darwin") {
app.on("before-quit", function (event) {
if (!forceQuit) {
event.preventDefault();
forceQuit = true;
app.quit();
}
});
}
// REVIEW - Awaiting javascript execution is silly // REVIEW - Awaiting javascript execution is silly
passedWindow.on("focus", () => { passedWindow.on("focus", () => {
void passedWindow.webContents.executeJavaScript(`document.body.removeAttribute("unFocused");`); void passedWindow.webContents.executeJavaScript(`document.body.removeAttribute("unFocused");`);

View file

@ -5,8 +5,9 @@ import path from "path";
import {createSettingsWindow} from "./settings/main.js"; import {createSettingsWindow} from "./settings/main.js";
import {getConfig, getConfigLocation, setConfig} from "./common/config.js"; import {getConfig, getConfigLocation, setConfig} from "./common/config.js";
import {getDisplayVersion} from "./common/version.js"; import {getDisplayVersion} from "./common/version.js";
import {setForceQuit} from "./common/forceQuit.js";
export let tray: Tray; export let tray: Tray;
export let isQuitting = false;
let trayIcon = "ac_plug_colored"; let trayIcon = "ac_plug_colored";
void app.whenReady().then(async () => { void app.whenReady().then(async () => {
// REVIEW - app will hang at startup if line above is awaited. // REVIEW - app will hang at startup if line above is awaited.
@ -86,8 +87,7 @@ void app.whenReady().then(async () => {
{ {
label: `Quit ${clientName}`, label: `Quit ${clientName}`,
click() { click() {
//NOTE - It would be better to unify isQuitting with forceQuit in window.ts that's used on macOS setForceQuit(true);
isQuitting = true;
app.quit(); app.quit();
} }
} }