-
-
Notifications
You must be signed in to change notification settings - Fork 54
Dismiss menu bar popover when opening Settings #481
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
FuJacob
merged 3 commits into
FuJacob:main
from
jkrauska:fix/menu-bar-close-on-settings
May 31, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| import AppKit | ||
| import Combine | ||
| import SwiftUI | ||
|
|
||
| /// Captures the `NSWindow` that backs a `MenuBarExtra(.window)` popover so SwiftUI buttons can | ||
| /// programmatically dismiss the popover the same way a `Link` would. | ||
| /// | ||
| /// Why this exists: | ||
| /// `MenuBarExtra` with `.window` style does not surface its host window through SwiftUI's | ||
| /// `@Environment(\.dismiss)` action, so a `Button` action stays open after firing. `Link` gets | ||
| /// dismissal for free because `NSWorkspace.shared.open` resigns key from the popover. Every other | ||
| /// button that triggers an app-window transition (Settings, eventually Onboarding/Welcome) needs | ||
| /// to dismiss the popover itself or it sits on top of the just-opened window. | ||
| /// | ||
| /// The dismisser hooks into the same `viewDidMoveToWindow` lifecycle that | ||
| /// `MenuBarPresentationObserver` uses to grab the popover's real `NSWindow`, then stores a weak | ||
| /// reference so the SwiftUI side can call `dismiss()` on demand. | ||
| @MainActor | ||
| final class MenuBarPopoverDismisser: ObservableObject { | ||
| /// Weak so it dies with the popover; the SwiftUI view never owns the AppKit window. | ||
| fileprivate weak var hostWindow: NSWindow? | ||
|
|
||
| /// Closes the captured popover and clears the status bar button's pressed-state highlight. | ||
| /// Safe to call when the popover isn't visible — `orderOut` is a no-op on a hidden window. | ||
| func dismiss() { | ||
| // `resignKey` first so any responder-chain bookkeeping (e.g. a focused text field) flushes | ||
| // before the window goes off-screen; `orderOut` then matches the system's own popover | ||
| // dismissal animation path. | ||
| hostWindow?.resignKey() | ||
| hostWindow?.orderOut(nil) | ||
| // The status bar button keeps its highlighted/pressed appearance after a programmatic | ||
| // `orderOut` because AppKit only clears that state when the popover dismisses through its | ||
| // own click-toggle path. Walk our own windows for the `NSStatusBarButton` and reset the | ||
| // highlight so the menu bar icon doesn't look stuck in the "open" state once Settings is up. | ||
| Self.unhighlightStatusBarButton() | ||
| } | ||
|
|
||
| private static func unhighlightStatusBarButton() { | ||
| for window in NSApp.windows { | ||
| guard let contentView = window.contentView else { continue } | ||
| if let button = findStatusBarButton(in: contentView) { | ||
| button.highlight(false) | ||
| return | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static func findStatusBarButton(in view: NSView) -> NSStatusBarButton? { | ||
| if let button = view as? NSStatusBarButton { | ||
| return button | ||
| } | ||
| for subview in view.subviews { | ||
| if let button = findStatusBarButton(in: subview) { | ||
| return button | ||
| } | ||
| } | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| /// Invisible `NSView` whose only job is to forward its host `NSWindow` to a | ||
| /// `MenuBarPopoverDismisser`. Attached as a `.background` modifier so it inherits the popover's | ||
| /// real backing window without affecting layout. | ||
| struct MenuBarPopoverDismisserBinder: NSViewRepresentable { | ||
| let dismisser: MenuBarPopoverDismisser | ||
|
|
||
| func makeNSView(context: Context) -> WindowBindingView { | ||
| let view = WindowBindingView() | ||
| view.dismisser = dismisser | ||
| return view | ||
| } | ||
|
|
||
| func updateNSView(_ nsView: WindowBindingView, context: Context) { | ||
| nsView.dismisser = dismisser | ||
| } | ||
|
|
||
| final class WindowBindingView: NSView { | ||
| weak var dismisser: MenuBarPopoverDismisser? | ||
|
|
||
| override func viewDidMoveToWindow() { | ||
| super.viewDidMoveToWindow() | ||
| // `window` is nil while the popover is being torn down. Skipping the update in that | ||
| // case keeps a stale reference from outliving the actual popover instance. | ||
| if let window { | ||
| dismisser?.hostWindow = window | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.