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

View file

@ -12,13 +12,14 @@ import * as fs from "fs";
import contextMenu from "electron-context-menu";
import os from "os";
import RPCServer from "arrpc";
import {isQuitting, tray} from "../tray.js";
import {tray} from "../tray.js";
import {iconPath, init} from "../main.js";
import {getConfig, setConfig, firstRun} from "../common/config.js";
import {getWindowState, setWindowState} from "../common/windowState.js";
import {forceQuit, setForceQuit} from "../common/forceQuit.js";
export let mainWindows: BrowserWindow[] = [];
export let inviteWindow: BrowserWindow;
export let forceQuit = false;
let osType = os.type();
contextMenu({
showSaveImageAs: true,
@ -238,39 +239,28 @@ function doAfterDefiningTheWindow(passedWindow: BrowserWindow): void {
});
setMenu();
passedWindow.on("close", (e) => {
if (process.platform === "darwin" && forceQuit) {
passedWindow.close();
} else if (mainWindows.length > 1) {
if (mainWindows.length > 1) {
mainWindows = mainWindows.filter((mainWindow) => mainWindow.id != passedWindow.id);
passedWindow.destroy();
} else {
const [width, height] = passedWindow.getSize();
setWindowState({
width,
height,
isMaximized: passedWindow.isMaximized(),
x: passedWindow.getPosition()[0],
y: passedWindow.getPosition()[1]
});
if (getConfig("minimizeToTray") && !isQuitting) {
e.preventDefault();
passedWindow.hide();
} else if (!getConfig("minimizeToTray")) {
e.preventDefault();
app.quit();
}
}
if (getConfig("minimizeToTray") && !forceQuit) {
e.preventDefault();
passedWindow.hide();
} else if (!getConfig("minimizeToTray")) {
app.quit();
}
});
if (process.platform === "darwin") {
app.on("before-quit", function (event) {
if (!forceQuit) {
event.preventDefault();
forceQuit = true;
app.quit();
}
app.on("before-quit", () => {
const [width, height] = passedWindow.getSize();
setWindowState({
width,
height,
isMaximized: passedWindow.isMaximized(),
x: passedWindow.getPosition()[0],
y: passedWindow.getPosition()[1]
});
}
setForceQuit(true);
});
// REVIEW - Awaiting javascript execution is silly
passedWindow.on("focus", () => {
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 {getConfig, getConfigLocation, setConfig} from "./common/config.js";
import {getDisplayVersion} from "./common/version.js";
import {setForceQuit} from "./common/forceQuit.js";
export let tray: Tray;
export let isQuitting = false;
let trayIcon = "ac_plug_colored";
void app.whenReady().then(async () => {
// REVIEW - app will hang at startup if line above is awaited.
@ -86,8 +87,7 @@ void app.whenReady().then(async () => {
{
label: `Quit ${clientName}`,
click() {
//NOTE - It would be better to unify isQuitting with forceQuit in window.ts that's used on macOS
isQuitting = true;
setForceQuit(true);
app.quit();
}
}