Skip to content

isMenuPresented using ObservableObject... #4

@dortus47

Description

@dortus47

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

Hello.
First of all, thank you for making a good library.
I found something while using the functions of the library, so I left a post.

I didn't want to keep passing the variable isMenuPresented to the subview with Binding for MenuBarExtraAccess, so I tried to use it wrapped in an ObservableObject.
However, using it in this way caused menubar to close and open abnormally slow (state -> binding is normal)
Below is the code of the way I implemented it.
Is Presented for controlling MenuBarExtraAccess, but is ObservableObject hard to use?

class AppState: ObservableObject {
    @Published var isMenuPresented: Bool = false
}
struct MyApp: App {

@NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
@StateObject var appState = AppState()
        
        MenuBarExtra("ListeningDogApp") {
            MenuBarExtraView()
                .environmentObject(appDelegate)
                .environmentObject(appState)
        }
        .menuBarExtraStyle(.window)
        .menuBarExtraAccess(isPresented: $listengDogAppState.isMenuPresented) { statusItem in // <-- the magic ✨
            // access status item or store it in a @State var
        }
}
    struct PreferencesView: View {
        
        @EnvironmentObject var appDelegate: AppDelegate
        @EnvironmentObject var AppState: AppState
        @State private var isHovered = false
        
        var body: some View {
            
            Button {
                appState.isMenuPresented = false
                appDelegate.showMainWindow()
            } label: {
                HStack {
                    Text("Preferences...")
                    Spacer()
                }
                .frame(height: 30)
            }
            .buttonStyle(.plain)
        }
2023-07-08.10.12.34.mov

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