Skip to content

Commit a2f163d

Browse files
Fix NSToolbar preserving the visual state of previous selected pane on macOS Sonoma (#113)
Co-authored-by: Sindre Sorhus <[email protected]>
1 parent 4f794b4 commit a2f163d

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

Sources/Settings/SettingsTabViewController.swift

+7
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,13 @@ final class SettingsTabViewController: NSViewController, SettingsStyleController
144144
to: toViewController,
145145
options: options
146146
) { [self] in
147+
if
148+
isAnimated,
149+
let toolbarItemStyleViewController = settingsStyleController as? ToolbarItemStyleViewController
150+
{
151+
toolbarItemStyleViewController.refreshPreviousSelectedItem()
152+
}
153+
147154
activeChildViewConstraints = toViewController.view.constrainToSuperviewBounds()
148155
}
149156
}

Sources/Settings/ToolbarItemStyleViewController.swift

+19
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ final class ToolbarItemStyleViewController: NSObject, SettingsStyleController {
66
let panes: [SettingsPane]
77
var isKeepingWindowCentered: Bool { centerToolbarItems }
88
weak var delegate: SettingsStyleControllerDelegate?
9+
private var previousSelectedItemIdentifier: NSToolbarItem.Identifier?
910

1011
init(panes: [SettingsPane], toolbar: NSToolbar, centerToolbarItems: Bool) {
1112
self.panes = panes
@@ -54,4 +55,22 @@ final class ToolbarItemStyleViewController: NSObject, SettingsStyleController {
5455
func selectTab(index: Int) {
5556
toolbar.selectedItemIdentifier = panes[index].toolbarItemIdentifier
5657
}
58+
59+
public func refreshPreviousSelectedItem() {
60+
// On macOS Sonoma, sometimes NSToolbar would preserve the
61+
// visual selected state of previous selected toolbar item during
62+
// view animation.
63+
// AppKit doesn’t seem to offer a way to refresh toolbar items.
64+
// So we manually “refresh” it.
65+
if
66+
#available(macOS 14, *),
67+
let previousSelectedItemIdentifier,
68+
let index = toolbar.items.firstIndex(where: { $0.itemIdentifier == previousSelectedItemIdentifier })
69+
{
70+
toolbar.removeItem(at: index)
71+
toolbar.insertItem(withItemIdentifier: previousSelected, at: index)
72+
}
73+
74+
previousSelectedItemIdentifier = toolbar.selectedItemIdentifier
75+
}
5776
}

readme.md

+1
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ You might also like Sindre's [apps](https://sindresorhus.com/apps).
339339
- [The Archive](https://zettelkasten.de/the-archive/) - Note-taking app by [Christian Tietze](https://github.com/DivineDominion)
340340
- [Word Counter](https://wordcounterapp.com) - Measuring writer's productivity by [Christian Tietze](https://github.com/DivineDominion)
341341
- [Medis](https://getmedis.com) - A Redis GUI by [Zihua Li](https://github.com/luin)
342+
- [OK JSON](https://okjson.app) - A scriptable JSON formatter by [Francis Feng](https://github.com/francisfeng)
342343

343344
Want to tell the world about your app that is using this package? Open a PR!
344345

0 commit comments

Comments
 (0)