Skip to content

Commit

Permalink
Fix NSToolbar preserving the visual state of previous selected pane o…
Browse files Browse the repository at this point in the history
…n macOS Sonoma (#113)

Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
francisfeng and sindresorhus authored Oct 19, 2023
1 parent 4f794b4 commit a2f163d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Sources/Settings/SettingsTabViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,13 @@ final class SettingsTabViewController: NSViewController, SettingsStyleController
to: toViewController,
options: options
) { [self] in
if
isAnimated,
let toolbarItemStyleViewController = settingsStyleController as? ToolbarItemStyleViewController
{
toolbarItemStyleViewController.refreshPreviousSelectedItem()
}

activeChildViewConstraints = toViewController.view.constrainToSuperviewBounds()
}
}
Expand Down
19 changes: 19 additions & 0 deletions Sources/Settings/ToolbarItemStyleViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ final class ToolbarItemStyleViewController: NSObject, SettingsStyleController {
let panes: [SettingsPane]
var isKeepingWindowCentered: Bool { centerToolbarItems }
weak var delegate: SettingsStyleControllerDelegate?
private var previousSelectedItemIdentifier: NSToolbarItem.Identifier?

init(panes: [SettingsPane], toolbar: NSToolbar, centerToolbarItems: Bool) {
self.panes = panes
Expand Down Expand Up @@ -54,4 +55,22 @@ final class ToolbarItemStyleViewController: NSObject, SettingsStyleController {
func selectTab(index: Int) {
toolbar.selectedItemIdentifier = panes[index].toolbarItemIdentifier
}

public func refreshPreviousSelectedItem() {
// On macOS Sonoma, sometimes NSToolbar would preserve the
// visual selected state of previous selected toolbar item during
// view animation.
// AppKit doesn’t seem to offer a way to refresh toolbar items.
// So we manually “refresh” it.
if
#available(macOS 14, *),
let previousSelectedItemIdentifier,
let index = toolbar.items.firstIndex(where: { $0.itemIdentifier == previousSelectedItemIdentifier })
{
toolbar.removeItem(at: index)
toolbar.insertItem(withItemIdentifier: previousSelected, at: index)
}

previousSelectedItemIdentifier = toolbar.selectedItemIdentifier
}
}
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ You might also like Sindre's [apps](https://sindresorhus.com/apps).
- [The Archive](https://zettelkasten.de/the-archive/) - Note-taking app by [Christian Tietze](https://github.com/DivineDominion)
- [Word Counter](https://wordcounterapp.com) - Measuring writer's productivity by [Christian Tietze](https://github.com/DivineDominion)
- [Medis](https://getmedis.com) - A Redis GUI by [Zihua Li](https://github.com/luin)
- [OK JSON](https://okjson.app) - A scriptable JSON formatter by [Francis Feng](https://github.com/francisfeng)

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

Expand Down

0 comments on commit a2f163d

Please sign in to comment.