-
Bug Description, Steps to Reproduce, Crash Logs, Screenshots, etc.Hello. 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. class AppState: ObservableObject {
@Published var isMenuPresented: Bool = false
} struct MyApp: App {
@NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
@StateObject var appState = AppState()
var body: some Scene {
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 |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Not sure what would explain the delay. Perhaps it's because you're using a non-English system language. The library does a lot of traversal under the hood of English property names and maybe it's having to use a redundancy to perform the action and taking longer somehow. Or perhaps something is happening between I just made a new macOS SwiftUI app with the following, and clicking the button in the menubar window makes the window dismiss immediately. @main struct MBETestApp: App {
@StateObject var appState: AppState = .init()
var body: some Scene {
MenuBarExtra("ListeningDogApp") {
ContentView()
.environmentObject(appState)
}
.menuBarExtraStyle(.window)
.menuBarExtraAccess(isPresented: $appState.isMenuPresented) { statusItem in
// access status item or store it in a @State var
}
WindowGroup {
ContentView()
}
}
}
class AppState: ObservableObject {
@Published var isMenuPresented: Bool = false
}
struct ContentView: View {
@EnvironmentObject var appState: AppState
var body: some View {
VStack {
Button("Toggle") {
appState.isMenuPresented.toggle()
}
}
.padding()
}
} |
Beta Was this translation helpful? Give feedback.
-
I'll have to check the other part of my app again. Thank you for your answer. Have a nice day. |
Beta Was this translation helpful? Give feedback.
Not sure what would explain the delay. Perhaps it's because you're using a non-English system language. The library does a lot of traversal under the hood of English property names and maybe it's having to use a redundancy to perform the action and taking longer somehow. Or perhaps something is happening between
MenuBarExtraView()
andPreferencesView()
.I just made a new macOS SwiftUI app with the following, and clicking the button in the menubar window makes the window dismiss immediately.