Use case or problem
In MacOS, when I click on the red button in the upper left corner, I expect the application to minimize to the docker instead of closing.
Proposed solution
Obsidian should behave as all other MacOS apps. A proper way to implement it is demonstrated by the Electron app Element.
global.mainWindow.webContents.on("before-input-event", warnBeforeExit);
global.mainWindow.on("closed", () => {
global.mainWindow = null;
});
global.mainWindow.on("close", async (e) => {
// If we are not quitting and have a tray icon then minimize to tray
if (!global.appQuitting && (tray.hasTray() || process.platform === "darwin")) {
// On Mac, closing the window just hides it
// (this is generally how single-window Mac apps
// behave, eg. Mail.app)
e.preventDefault();
if (global.mainWindow?.isFullScreen()) {
global.mainWindow.once("leave-full-screen", () => global.mainWindow?.hide());
global.mainWindow.setFullScreen(false);
} else {
global.mainWindow?.hide();
}
return false;
}
});
Current workaround (optional)
I tried to develop a plugin where I handle onbeforeunload
:
let handlingUnload = false; // Flag to prevent recursion
window.onbeforeunload = async function (this: WindowEventHandlers, ev: BeforeUnloadEvent) {
if (handlingUnload) {
// Prevent recursion
return;
}
handlingUnload = true;
// Prevent the default unload behavior
ev.preventDefault();
ev.returnValue = false; // Required for older browsers/Electron
});
The function is triggered but it does not prevent closing. Of course, this was just an attempt and could not represent the final version, because this code alone would prevent closing Obsidian altogether. It was just for testing what one can do.