lfroms / fluid-menu-bar-extra

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

Windows Support #3

Closed mromanbanks closed 1 week ago

mromanbanks commented 1 year ago

Hey guys! I'm trying to open new window from the app and getting Use of OpenWindowAction requires the SwiftUI App Lifecycle. error. Could you please give a hint how to correctly open windows with SwiftUI and this library?

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 {
        Window("New Window", id: "add") {
            Text("Content")
        }
    }
}

struct MainView: View {
    @Environment(\.openWindow) var openWindow

    var body: some View {
        Button("Open Window") {
            openWindow(id: "add")
        }
    }
}
lfroms commented 1 year ago

Hi! What a coincidence, I actually came across the same issue the other day. It looks like SwiftUI is unable to tell if an application is rooted in SwiftUI from NSHostingViews we create ourselves. I suppose it technically does make sense since NSHostingView creates a completely isolated environment, but it is a bit annoying in this case

Unfortunately, my workaround for now has been to just create my own NSWindow and make my own ViewModifier to interact with it (or just keep a reference in AppDelegate).

mromanbanks commented 1 year ago

Makes sense. Thank you for the explanation!

tuckermacdonald commented 1 year ago

Unfortunately, my workaround for now has been to just create my own NSWindow and make my own ViewModifier to interact with it (or just keep a reference in AppDelegate).

Hi @lfroms! Would you mind sharing an example of your workaround? I'm stuck on the same issue. Very appreciative of this little tool 👏🏻!

lfroms commented 1 year ago

Hi folks, sorry for the delay. A quick Google search turned up this guide which is similar to how I've been doing things. Can use an NSWindow subclass instead of NSPanel if you want a typical title bar setup, etc.