Minimize insted of closing Obsidian when clicking on the close button

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.

To my knowledge, Sequoia didn’t remove the yellow button which is for collapsing/ minimizing windows.

Yellow has always been the button to minimize.
Red closes windows or, if the app allows, quits the process. I guess wanted to say yellow, not red.

1 Like

When I click the red button, the window closes and Obsidian remains open in the menu bar, which is what I expect. When I click the yellow button, the window minimizes to the Dock, in the section by the trash can. I don’t normally use minimize, but Finder (another app I have pinned to my Dock) behaves the same way, so this seems to be correct.

No, I actually meant the indeed red button, not the yellow button. At the moment, its behavior is not implemented consistently with the other Apple applications.

Normally, for single-window applications, the red button typically quits the application (e.g. System Preferences). For multi-window applications (such as Safari or Finder or Microsoft Word), however, it only closes the active window and the currently open document, while the application continues to run.

In an application like Obsidian, I would expect two possible behaviours that are not currently realised:

  • Either the red button closes the vault and keeps Obsidian (the Electron application) running in the background. When I click on Obsidian, it shows me the panel asking me which vault I want to open.
  • or the red button will keep the vault open but simply minimise the app. When I click on the app, it opens quickly and responsively.

Unfortunately, the current behaviour is to keep the vault open, the Electron app running, but when I click on the icon, the window reappears, but it takes a while to reload the vault that was closed in the meantime.

If the old vault is still to be opened after the application is restored (as it is now), then it would be much better not to close the vault so that the application behaves in a responsive manner. Or actually close the vault, but then ask the user which vault to open.