Skip to content

Commit e8ceac2

Browse files
committed
Open Settings window in foreground via floating NSWindow
SettingsLink from MenuBarExtra does not activate the app under cooperative activation (LSUIElement apps). Use the same custom NSWindow pattern as AboutView with window.level = .floating.
1 parent 92ce2e9 commit e8ceac2

2 files changed

Lines changed: 32 additions & 3 deletions

File tree

Sources/MicGuard/PopoverView.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ struct PopoverView: View {
1717

1818
Divider()
1919

20-
SettingsLink {
21-
Text("Settings...")
22-
}
20+
Button("Settings...") { SettingsView.showWindow() }
2321

2422
Button("About MicGuard") { AboutView.showWindow() }
2523
Button("Quit MicGuard") { NSApplication.shared.terminate(nil) }

Sources/MicGuard/SettingsView.swift

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,37 @@ struct SettingsView: View {
8282
}
8383
}
8484

85+
static func showWindow() {
86+
let windowID = "settings-micguard"
87+
88+
// Reuse existing window if open
89+
if let existing = NSApp.windows.first(where: { $0.identifier?.rawValue == windowID }) {
90+
existing.level = .floating
91+
existing.makeKeyAndOrderFront(nil)
92+
NSApp.activate()
93+
return
94+
}
95+
96+
let hostingView = NSHostingView(rootView: SettingsView())
97+
hostingView.translatesAutoresizingMaskIntoConstraints = false
98+
let fittingSize = hostingView.fittingSize
99+
100+
let window = NSWindow(
101+
contentRect: NSRect(origin: .zero, size: fittingSize),
102+
styleMask: [.titled, .closable],
103+
backing: .buffered,
104+
defer: false
105+
)
106+
window.identifier = NSUserInterfaceItemIdentifier(windowID)
107+
window.title = "MicGuard Settings"
108+
window.contentView = hostingView
109+
window.center()
110+
window.isReleasedWhenClosed = false
111+
window.level = .floating
112+
window.makeKeyAndOrderFront(nil)
113+
NSApp.activate()
114+
}
115+
85116
private func toggleLoginItem(_ enable: Bool) {
86117
do {
87118
if enable {

0 commit comments

Comments
 (0)