Super odd DarkMode problem

You can do it for each window individually. Showed that in my xDev articles about writing an object-oriented ListBox. I just found it VERY annoying to switch modes globally to test how it looks in the other mode - that is ok if you just have a few windows open, but in Safari alone I have over 100 windows (and over 1200 Tabs open).

So I have a boolean property UseDarkMode and set it with a checkbox to what I want it to be.

Then in the method if either the System’s IsDarkMode or the window’s UseDarkMode is true then it switches it to DarkMode.

The forum ate the first lines but here it is again:

Window has a CheckBox CB_IsDarkMode with

Sub Action() Handles Action
  Self.ShowInDarkMode( Me.Value )
End Sub

The window has a property

Public Property UseDarkMode as Boolean = false

Add a Module DarkMode with an extension method for windows (slightly changed now):

Public Sub ShowInDarkMode(extends w as Window, DarkMode as Boolean)
  w.UseDarkMode = DarkMode
  // can we simulate dark mode 
  Dim nsWindowObj As NSWindowMBS
  Dim appearanceObj As NSAppearanceMBS
  nsWindowObj = w.NSWindowMBS
  If IsDarkMode Or w.UseDarkMode Then  // use DarkMode
    appearanceObj = NSAppearanceMBS.appearanceNamed(NSAppearanceMBS.NSAppearanceNameVibrantDark)
    appearanceObj = NSAppearanceMBS.appearanceNamed(NSAppearanceMBS.NSAppearanceNameVibrantLight)
  End If
  If appearanceObj <> Nil Then
    NSAppearanceMBS.setAppearance(w, appearanceObj)
  End If
End Sub

So now you can set any window to dark or light by calling the extension method with true or false - completely independent on what the system mode is. Great for testing dark mode and light mode without having to change everything back and forth.

P.S. The boolean property UseDarkMode is necessary if you want to check the dark mode state of the window as it can be different from the system state - so you need a persistent property to store the current window state.

1 Like

Awesome, thanks Markus :slight_smile:

The thanks should go to Sam Rowlands - he’s the one who let me in on it :wink:

1 Like

Found a warning in the original thread:

You can indeed set the appearance on just a window, or even just a control ( although make sure you DO NOT DO THIS on 10.13 or lower ).

Setting the appearance on the application works from 10.10 + Although the constant names changes with Mojave, if you’re going for a always dark, you’ll need to dig out the t’other constant.

I noticed that it doesn’t give an accurate appearance for the Gradient buttons I was using but other than that was useful, thanks and to Sam

1 Like
Forum for Xojo Programming Language and IDE. Copyright © 2021 Xojo, Inc.