writefreely/writefreely-swiftui-multiplatform

Clicking dock icon when minimized does not restore window

AngeloStavrow opened this issue · 5 comments

That's typical Mac UX so I'm a little surprised it doesn't "just work" in a SwiftUI multiplatform app.

There are two workarounds; either click Window in the menubar, or right-click dock icon, and choose the window with the diamond next to it:

Screen Shot 2020-11-11 at 15 13 09

Screen Shot 2020-11-11 at 15 11 18

I've asked about this in a couple of Slack groups, here are my findings so far.

(This work can be found in the restore-window-when-clicking-dock-icon branch of the repo.)

Adding a @NSApplicationDelegateAdaptor and pointing it to a class that conforms to NSApplicationDelegate and implements applicationShouldHandleReopen(_:hasVisibleWindows:) gets the deminiaturize-minimized-window behaviour working if I ensure that this file is the delegate for NSApplication (apparently, that's not necessarily true just from setting the delegate adaptor property wrapper) — but then the reopen-closed-windows behaviour stops working, as does the Settings Scene that opens the app's preferences window (the menu item is disabled).

So right now, we can either have the reopen-closed-window behaviour and the preferences window working, or we can have the deminiaturize-minimized-window behaviour working. I'll dig a bit further, but I'm removing the 1.0 milestone from this issue because while it's what a good macOS app-citizen does, it's not a launch blocker and we have more important work to do here.

I used NSApp.hide instead of starting the finder. Maybe this also works in your case.

Hey @KHKnobl, sorry for not replying sooner — I didn't see this until now! Would you have a sample implementation you can share?

I took over your solution and just changed:

func applicationDidChangeOcclusionState(_ notification: Notification) {
    print("💬 Fired:", #function)
    if let window = NSApp.windows.first, window.isMiniaturized {
        NSApp.hide(self)
    }
}

That works great, thanks!