-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Recycle menu card hosting views and reconcile menu content in place #1394
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
steipete
merged 2 commits into
steipete:main
from
bcssewl:perf/recycle-menu-card-hosting-views
Jun 10, 2026
Merged
Changes from all commits
Commits
Show all changes
2 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
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
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
69 changes: 69 additions & 0 deletions
69
Sources/CodexBar/StatusItemController+MenuCardRecycling.swift
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,69 @@ | ||
| import AppKit | ||
|
|
||
| extension StatusItemController { | ||
| /// Collects the card hosting views of items the current populate pass is about to discard | ||
| /// so `makeMenuCardItem` can reuse them for cards with the same identifier (or, failing | ||
| /// that, the same content type) instead of building fresh hosting views. | ||
| /// | ||
| /// Safety: live menu items can alias one merged-switcher cache entry — the one for the | ||
| /// selection currently displayed, re-cached at the end of every populate. Consuming that | ||
| /// entry up front (`displacedSelection`) guarantees no cache entry can still reference a | ||
| /// harvested view; entries for other selections only hold items already detached from the | ||
| /// menu. Harvested views are detached from their outgoing items; whatever the pass does | ||
| /// not consume is released by `clearMenuCardViewRecyclePool`. | ||
| func harvestRecyclableMenuCardViews( | ||
| in menu: NSMenu, | ||
| fromIndex: Int, | ||
| displacedSelection: ProviderSwitcherSelection?, | ||
| preserveHighlightedItem: Bool = false) | ||
| { | ||
| self.menuCardViewRecyclePool.removeAll(keepingCapacity: true) | ||
| let menuKey = ObjectIdentifier(menu) | ||
| if let displacedSelection { | ||
| self.mergedSwitcherContentCaches[menuKey]?.removeValue(forKey: displacedSelection) | ||
| } | ||
| guard Self.menuCardRenderingEnabled else { return } | ||
| guard fromIndex >= 0, fromIndex < menu.items.count else { return } | ||
| for item in menu.items[fromIndex...] { | ||
| guard let id = item.representedObject as? String else { continue } | ||
| guard let view = item.view, view is any MenuCardMeasuring else { continue } | ||
| guard self.menuCardViewRecyclePool[id] == nil else { continue } | ||
| // Unhighlight before detaching: the highlight tracker unwinds through the | ||
| // outgoing item's `view`, which is about to become nil, so a recycled view | ||
| // would otherwise re-attach visibly highlighted with no path to clear it. | ||
| if self.highlightedMenuItems[menuKey] === item { | ||
| if !preserveHighlightedItem { | ||
| self.highlightedMenuItems.removeValue(forKey: menuKey) | ||
| } | ||
| } | ||
| (view as? MenuCardHighlighting)?.setHighlighted(false) | ||
| item.view = nil | ||
| self.menuCardViewRecyclePool[id] = view | ||
| } | ||
| } | ||
|
|
||
| /// Pops a pool entry adoptable as `ViewType`: the same card identifier when its view | ||
| /// matches, otherwise the first type-compatible leftover. The fallback is what makes | ||
| /// provider switches cheap — a different provider's card with a different identifier but | ||
| /// the same SwiftUI content type (for example two providers' usage cards) is repainted | ||
| /// in place instead of being rebuilt. | ||
| func takeRecyclableMenuCardView<ViewType: NSView>(for id: String, as type: ViewType.Type) -> ViewType? { | ||
| if let candidate = self.menuCardViewRecyclePool.removeValue(forKey: id) { | ||
| if let adopted = candidate as? ViewType { | ||
| return adopted | ||
| } | ||
| // A same-id view of an incompatible shape can never be adopted later in this | ||
| // pass; dropping it restores the build-fresh behavior. | ||
| return nil | ||
| } | ||
| guard let match = self.menuCardViewRecyclePool.first(where: { $0.value is ViewType }) else { | ||
| return nil | ||
| } | ||
| self.menuCardViewRecyclePool.removeValue(forKey: match.key) | ||
| return match.value as? ViewType | ||
| } | ||
|
|
||
| func clearMenuCardViewRecyclePool() { | ||
| self.menuCardViewRecyclePool.removeAll(keepingCapacity: true) | ||
| } | ||
| } | ||
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When an open menu is rebuilt while a card is highlighted, this detaches the view from the
NSMenuItemthathighlightedMenuItemsstill stores. If AppKit subsequently sendsmenu(_:willHighlight:)withnilor a different rebuilt item, the old item has noview, so the existing code cannot callsetHighlighted(false)and the recycled hosting view can remain visibly highlighted on the wrong/no row. Clear the highlight or updatehighlightedMenuItemsbefore moving the view into the recycle pool.Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch — confirmed real: both unhighlight paths (
menu(_:willHighlight:)and themenuDidClosecleanup) unwind through the tracked item'sview, which harvesting nils out, so a card highlighted at rebuild time would re-attach with stale highlight rendering and no path to clear it. On current main this can't surface because the discarded view dies with the item; with recycling the state survives into the visible menu.Fixed in 2f5cf69:
harvestRecyclableMenuCardViewsnow callssetHighlighted(false)on the view and drops the menu'shighlightedMenuItemsentry when it strips the tracked item, before pooling. Behavior matches main's rebuild semantics (fresh content starts unhighlighted; the next mouse-move re-highlights viawillHighlight). Added a regression test:harvesting a highlighted card clears its highlight and tracking entry. Full suite +make checkgreen.