fix #22: preserve maximised windows after minimisation

This commit is contained in:
dragonwocky 2023-08-23 20:40:47 +10:00
parent 78e819cc45
commit 5cef2b0ed7
Signed by: dragonwocky
GPG Key ID: 7998D08F7D7BD7A8
2 changed files with 24 additions and 33 deletions

55
main.js
View File

@ -41,49 +41,43 @@ const obsidian = require("obsidian"),
{ app, Tray, Menu, nativeImage } = require("electron").remote, { app, Tray, Menu, nativeImage } = require("electron").remote,
{ getCurrentWindow, globalShortcut } = require("electron").remote; { getCurrentWindow, globalShortcut } = require("electron").remote;
const childWindows = new Set(), const vaultWindows = new Set(),
observeChildWindows = () => { maximizedWindows = new Set(),
getCurrentWindow().webContents.on("did-create-window", (win) => { getWindows = () => [...vaultWindows],
childWindows.add(win); observeWindows = () => {
win.on("close", () => childWindows.delete(win)); const onWindowCreation = (win) => {
vaultWindows.add(win);
win.setSkipTaskbar(plugin.settings.hideTaskbarIcon); win.setSkipTaskbar(plugin.settings.hideTaskbarIcon);
}); win.on("close", () => vaultWindows.delete(win));
// preserve maximised windows after minimisation
if (win.isMaximized()) maximizedWindows.add(win);
win.on("maximize", () => maximizedWindows.add(win));
win.on("unmaximize", () => maximizedWindows.delete(win));
};
onWindowCreation(getCurrentWindow());
getCurrentWindow().webContents.on("did-create-window", onWindowCreation);
}, },
getAllWindows = () => [...childWindows, getCurrentWindow()],
showWindows = () => { showWindows = () => {
log(LOG_SHOWING_WINDOWS); log(LOG_SHOWING_WINDOWS);
getAllWindows().forEach((win) => win.show()); getWindows().forEach((win) => {
maximizedWindows.has(win) ? win.maximize() : win.show();
});
}, },
hideWindows = () => { hideWindows = () => {
log(LOG_HIDING_WINDOWS); log(LOG_HIDING_WINDOWS);
getAllWindows().forEach((win) => [ getWindows().forEach((win) => [
win.isFocused() && win.blur(), win.isFocused() && win.blur(),
plugin.settings.runInBackground ? win.hide() : win.minimize(), plugin.settings.runInBackground ? win.hide() : win.minimize(),
]); ]);
}, },
toggleWindows = (checkForFocus = true) => { toggleWindows = (checkForFocus = true) => {
const openWindows = getAllWindows().some((win) => { const openWindows = getWindows().some((win) => {
return (!checkForFocus || win.isFocused()) && win.isVisible(); return (!checkForFocus || win.isFocused()) && win.isVisible();
}); });
if (openWindows) hideWindows(); if (openWindows) hideWindows();
else showWindows(); else showWindows();
}; };
let isMaximized = false;
const onMinimize = () => {
if (isMaximized) getCurrentWindow().maximize();
hideWindows();
}, interceptWindowMinimize = () => {
isMaximized = getCurrentWindow().isMaximized();
getCurrentWindow().on("maximize", () => {
isMaximized = true;
});
getCurrentWindow().on("unmaximize", () => {
isMaximized = false;
});
getCurrentWindow().on("minimize", onMinimize);
};
const onWindowClose = (event) => event.preventDefault(), const onWindowClose = (event) => event.preventDefault(),
onWindowUnload = (event) => { onWindowUnload = (event) => {
log(LOG_WINDOW_CLOSE); log(LOG_WINDOW_CLOSE);
@ -107,7 +101,7 @@ const onWindowClose = (event) => event.preventDefault(),
}; };
const setHideTaskbarIcon = () => { const setHideTaskbarIcon = () => {
getAllWindows().forEach((win) => { getWindows().forEach((win) => {
win.setSkipTaskbar(plugin.settings.hideTaskbarIcon); win.setSkipTaskbar(plugin.settings.hideTaskbarIcon);
}); });
}, },
@ -127,7 +121,7 @@ const setHideTaskbarIcon = () => {
unregisterHotkeys(); unregisterHotkeys();
allowWindowClose(); allowWindowClose();
destroyTray(); destroyTray();
getAllWindows().forEach((win) => win.destroy()); getWindows().forEach((win) => win.destroy());
}; };
const addQuickNote = () => { const addQuickNote = () => {
@ -408,11 +402,8 @@ class TrayPlugin extends obsidian.Plugin {
registerHotkeys(); registerHotkeys();
setHideTaskbarIcon(); setHideTaskbarIcon();
setLaunchOnStartup(); setLaunchOnStartup();
observeChildWindows(); observeWindows();
if (settings.runInBackground) { if (settings.runInBackground) interceptWindowClose();
interceptWindowMinimize();
interceptWindowClose();
}
if (settings.hideOnLaunch) { if (settings.hideOnLaunch) {
this.registerEvent(this.app.workspace.onLayoutReady(hideWindows)); this.registerEvent(this.app.workspace.onLayoutReady(hideWindows));
} }

View File

@ -4,7 +4,7 @@
"author": "dragonwocky", "author": "dragonwocky",
"authorUrl": "https://dragonwocky.me/", "authorUrl": "https://dragonwocky.me/",
"description": "Run Obsidian from the system tray for customisable window management & global quick notes", "description": "Run Obsidian from the system tray for customisable window management & global quick notes",
"version": "0.3.3", "version": "0.3.4",
"isDesktopOnly": true, "isDesktopOnly": true,
"minAppVersion": "1.0.0" "minAppVersion": "1.0.0"
} }