Switch app between Dark and Light mode with NSAppearanceMBS

Got it working with 2 gotchas:

  • During AppearanceChanged the appearance hasn’t finished changing.
  • After resetting the appearance Color.isDarkMode doesn’t work anymore.

I used to use a Pure Xojo solution, whereby I’d clear any caches on the appearanceChanged event.
control.paint is where I’d rebuild the cache (if needed) as that’s where the colors are going to be correct. This is fundamentally bad as you’ve adding more weight to the paint event which should never be done.

Using my own Ohanaware App Kit library, there’s several things I do dofferently.

  • I rebuild any caches (colors, images) from the viewDidChangeEffectiveAppearance event.
  • I use as much NSDrawing code as possible as that allows me to use system color constants which should always be correct.
  • For labels that are using system colors, I had a function which allows me to set a label using a NSColor, so the system keeps the color correct.
  • To detect Dark mode, I get the name of the current appearance and use instrb to see if “Dark” is in the name.