Super odd DarkMode problem

If I start my app in LightMode the app can switch to DarkMode and back fine. If I start the app in DarkMode then the app doesn’t switch to LightMode.

App started in DarkMode:

Switched to LightMode:

I’m at a loss what I can do to fix the problem. So far I have:

  • removed code for handling display changes
  • removed all pre and post build scripts
  • copied all external classes, icons and what not to a new project

Problem shows up for 10.14 and 10.15. I had a user test and he sees the same problem. Only the main app is affected and not the helper apps.

Does anyone have an idea what I could change/check/do?

That is super weird.

Do you get the same behavior with a blank project?

Nope. A blank project works fine. My helper apps also work fine.

I tested with 2019r3 and 2020r1. Both show the same problem.

The events AppearnceChanged do fire :fire:?

The AppearanceChanged event fires. I have a notification that does to open windows which then gets colours. Like this:

if Color.IsDarkMode then
  Return kWindowBackGroundDM
  Return kWindowBackGround
end if

The app still thinks it’s DarkMode.

It used to be the case that when Appearance Changed fired it was too soon to do the correct assessment of what colors to grab
You can / should only do that in paint events AFTER that event has fired

1 Like

@Norman Palardy: if that were the case then I should have problems when starting in LightMode, too. Also the whole app doesn’t switch as you can see in the black toolbar.

But testing is always good. I added an App.SleepCurrentThread(100) to the AppearanceChanged event. And there is no change in behaviour.

All I can say is that in my largest app I have waited until the next paint event to grab the correct colors and never have this issue going either way regardless of what mode I started in

The colours aren’t the problem. If they were then I would end up in a sort of Frankenstein mode. If you look at the toolbar then you can see that the complete app is in DarkMode. If I open new windows they are also in DarkMode.

New windows open AFTER the appearance changed event has completed and so asking “if I’m in dark mode” works :slight_smile:

New windows open in DarkMode even after switching to LightMode for my application!

what version of Xojo ?
does this behaviour change if you move back to an older version ?
reboot does it persist ?

What happens if you hide the window(s) when you change, then show the window(s) again ?

Xojo versions: 2019r3 and 2020r1
Older version: going to test tomorrow. Also going to test older versions of my app.
Rebooting: yes, the problem persists.
Window hiding: doesn’t make a difference.

Did you get a resolution?

Nope, problem still shows up for latest BS and Xojo 2020r2.1:

Can you manually set it to DarkMode / LightMode? E.g.

  UseDarkMode = DarkMode
  // can we simulate dark mode 
  Dim nsWindowObj As NSWindowMBS
  Dim appearanceObj As NSAppearanceMBS
  nsWindowObj = w.NSWindowMBS
  If IsDarkMode Or 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

You might be able to test for the color shown and try to set the mode manually accordingly.

Oh, and I presume you checked with Arbed for project corruption …?

Yes, I already imported everything into a new project. The effect was the same.

Big thanks for the code. This works fine.

Marcus, I’m not sure I understand the implications here…

Does this mean I can switch a darkmode-capable app into dark mode even when the macOS is not in dark mode? (or vice versa)