I have NSButtons in a NSTableView, exactly as in my other question here.
I have implemented a switch between "Dark mode" and "Normal mode" using NSAppearance on the window in the NSWindowController:
override public func windowDidLoad() {
if darkModeOn {
setDarkMode()
} else {
setLightMode()
}
}
func setDarkMode() {
window?.appearance = NSAppearance(named: NSAppearanceNameVibrantDark)
window?.backgroundColor = nil
}
func setLightMode() {
window?.appearance = nil
window?.backgroundColor = .white
}
The buttons have their isTemplate
attribute to true
, so that their color is the inverse of the window's one.
Light:
Dark:
This works ok when the app launches: the buttons are black when the window is white, and they're white when the window is dark.
But this doesn't work anymore when the app is already launched.
Once the app is launched, if I call setDarkMode()
when the window is white, I suddenly get black or grey buttons instead of the desired white ones.
Same if I call setLightMode()
when the window is dark.
In both cases, the buttons return to normal once I scroll the tableView back and forth enough to force the cells to be redrawn.
Note another side-effect of switching the window's state: some of the the tableView's row separators are suddenly thicker, and also become normal again after the scrolling.
As you can imagine, I don't want the user to be forced to quit the app and relaunch, so I would like to have all these things correctly drawn in the window when the NSAppearance changes.
Unfortunately I've tried many solutions without success: combinations of needsDisplay
and other ways to force "refreshing" the buttons (and the tableView row separators) have had no effect whatsoever. The documentation is sparse. I've watched all WWDC videos. I'm lost. :p