Skip to content

Using isPresented with Binding doesn't work #23

@gilmatok

Description

@gilmatok

Bug Description, Steps to Reproduce, Crash Logs, Screenshots, etc.

This is my app code:

struct MyApp: App {
  @NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate

  var body: some Scene {
      MenuBarExtra {
          MainView().environmentObject(appDelegate.handler)
      } label: {
          Image("tray-no-permissions-dark")
      }
      .menuBarExtraAccess(
        isPresented: Binding(
            get: { appDelegate.handler.isMenuOpen },
            set: { appDelegate.handler.isMenuOpen = $0 }
            )
      ) { statusItem in
          appDelegate.statusItem = statusItem
      }
      .menuBarExtraStyle(.window)
  }

This is my handler:

class callbackHandler: ObservableObject {

    @Published var isMenuOpen: Bool = false

}

Then in my AppDelegate I want to open the menu programmatically after 15 seconds like so:

    func applicationDidFinishLaunching(_ notification: Notification) {
        DispatchQueue.main.asyncAfter(deadline: .now() + 15) {
            self.handler.isMenuOpen = true
        }
    }

However, nothing happens. I can use the statusItem to performClick but I thought the binding would work. What am I missing?

Also, there are parts in my app that would need to open the menu programmatically, but that would make this boolean stay true even if the user doesn't dismiss the view. Wouldn't that be an issue? Is there a way to "trigger" the opening once?

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions