fix: #12 intercept window close at electron level

This commit is contained in:
dragonwocky 2023-04-10 12:37:07 +10:00
parent 046440ea3c
commit 1fd510e3d2
Signed by: dragonwocky
GPG Key ID: 7998D08F7D7BD7A8

41
main.js
View File

@ -19,10 +19,9 @@ const obsidian = require("obsidian"),
const showWindows = () => { const showWindows = () => {
console.log("obsidian-tray: showing windows"); console.log("obsidian-tray: showing windows");
const windows = BrowserWindow.getAllWindows(), const windows = BrowserWindow.getAllWindows();
currentWindow = windows.find((win) => win.isFocused()) || windows[0];
windows.forEach((win) => win.show()); windows.forEach((win) => win.show());
currentWindow.focus(); getCurrentWindow().focus();
}, },
hideWindows = (runInBackground) => { hideWindows = (runInBackground) => {
console.log("obsidian-tray: hiding windows"); console.log("obsidian-tray: hiding windows");
@ -42,31 +41,32 @@ const showWindows = () => {
} else showWindows(); } else showWindows();
}; };
// let _onbeforeunload; const onWindowClose = (event) => event.preventDefault(),
const onWindowClose = (event) => { onWindowUnload = (event) => {
event.stopImmediatePropagation();
// event.preventDefault();
console.log("obsidian-tray: intercepting window close"); console.log("obsidian-tray: intercepting window close");
const windows = BrowserWindow.getAllWindows(), getCurrentWindow().hide();
currentWindow = windows.find((win) => win.isFocused()); event.stopImmediatePropagation();
currentWindow.hide(); // setting return value manually is more reliable than
// via `return false` according to electron
event.returnValue = false;
}, },
interceptWindowClose = () => { interceptWindowClose = () => {
// _onbeforeunload = window.onbeforeunload; // intercept in renderer
// window.onbeforeunload = onWindowClose; window.addEventListener("beforeunload", onWindowUnload, true);
const closeBtn = document.querySelector(".mod-close"); // intercept in main: is asynchronously executed when registered
closeBtn.addEventListener("click", onWindowClose, true); // from renderer, so won't prevent close by itself, but counteracts
// the 3-second delayed window force close in obsidian.asar/main.js
getCurrentWindow().on("close", onWindowClose);
}, },
cleanupWindowClose = () => { cleanupWindowClose = () => {
// window.onbeforeunload = _onbeforeunload; getCurrentWindow().removeListener("close", onWindowClose);
const closeBtn = document.querySelector(".mod-close"); window.removeEventListener("beforeunload", onWindowUnload, true);
closeBtn.removeEventListener("click", onWindowClose, true);
}; };
const addQuickNote = (plugin) => { const addQuickNote = (plugin) => {
const { quickNoteLocation, quickNoteDateFormat } = plugin.settings, const { quickNoteLocation, quickNoteDateFormat } = plugin.settings,
format = quickNoteDateFormat || DEFAULT_DATE_FORMAT, pattern = quickNoteDateFormat || DEFAULT_DATE_FORMAT,
date = obsidian.moment().format(format), date = obsidian.moment().format(pattern),
name = obsidian name = obsidian
.normalizePath(`${quickNoteLocation ?? ""}/${date}`) .normalizePath(`${quickNoteLocation ?? ""}/${date}`)
.replace(/\*|"|\\|<|>|:|\||\?/g, "-"); .replace(/\*|"|\\|<|>|:|\||\?/g, "-");
@ -74,8 +74,7 @@ const addQuickNote = (plugin) => {
showWindows(); showWindows();
}, },
setHideTaskbarIcon = (plugin) => { setHideTaskbarIcon = (plugin) => {
const win = getCurrentWindow(); getCurrentWindow().setSkipTaskbar(plugin.settings.hideTaskbarIcon);
win.setSkipTaskbar(plugin.settings.hideTaskbarIcon);
}, },
setLaunchOnStartup = (plugin) => { setLaunchOnStartup = (plugin) => {
const { launchOnStartup, runInBackground, hideOnLaunch } = plugin.settings; const { launchOnStartup, runInBackground, hideOnLaunch } = plugin.settings;