lfroms / fluid-menu-bar-extra

🖥️ A lightweight tool for building great menu bar extras with SwiftUI.
MIT License
63 stars 9 forks source link

Sheets Support #4

Closed mromanbanks closed 1 year ago

mromanbanks commented 1 year ago

Another observation is when you trying to open a sheet, the app is hidding automatically. Is there any workarounds?

class AppDelegate: NSObject, NSApplicationDelegate {
    private var menuBarExtra: FluidMenuBarExtra?

    func applicationDidFinishLaunching(_ notification: Notification) {
        self.menuBarExtra = FluidMenuBarExtra(title: "App", systemImage: "globe") {
            MainView()
        }
    }
}

@main
struct macOSApp: App {
    @NSApplicationDelegateAdaptor private var appDelegate: AppDelegate

    var body: some Scene {
        Settings {
            EmptyView()
        }
    }
}

struct MainView: View {
    @State var isSheetPresented = false

    var body: some View {
        Text("Hello")
            .sheet(isPresented: $isSheetPresented) {
                Text("Sheet Content")
                    .frame(width: 400, height: 300)
            }

        Button("Open Sheet") {
            isSheetPresented.toggle()
        }
    }
}

https://user-images.githubusercontent.com/48674860/215326372-5bf79492-67e2-4ad1-ba30-5a06175aeddb.mov

lfroms commented 1 year ago

Interesting... I have a feeling the underlying NSPanel of FluidMenuBarExtra is losing key or main status when the sheet opens, but not entirely sure.

Definitely a bug, I'll have a look. If you have an idea of how to fix it, contributions are welcome!

(I do think sheets from a menu make for a questionable UX, though. 😉)

rurza commented 1 year ago

One possible workaround is to change the implementation. In my fork I deleted the:

    func windowDidResignKey(_ notification: Notification) {
        globalEventMonitor?.stop()
        dismissWindow()
    }

in the FluidMenuBarExtraStatusItem.swift. Then, call dismissWindow only when global event was received and stop observing global events in the dismissWindow method. This will alter behaviour of the window a little bit as it won't be dismissed whenever local event was received.

lfroms commented 1 year ago

Going to close this as I don't think these kinds of sheets are quite part of what is familiar to the menu bar in terms of UX. I'd recommend presenting windows for these kinds of flows (file pickers could use a file picker window, for example).

If anyone would like to fix the specific bug that is occurring without affecting any other behaviour (such as dismissal) I'd still be more than happy to accept contributions for that.