Is it possible to simulate dark mode just for my app?

I guess you would have to set the colors for everything like

if simulateDarkMode then windowBackgroundColor = colorvalue

Anyone implemented something like this?

Yes.

NSObjectSetAppearance( NSApplicationSharedApplication ) = NSAppearanceNamed( NSAppearanceClass, NSAppearanceNameDarkAqua )

I also have a button in my windows which I can use to toggle visual style.

Thanks Sam, but I think you overestimate my abilities a wee bit (a lot actually).

I guess we are not talking Xojo here but Swift … or ObjC?

My bad, that code does indeed rely on my App Kit. Sorry.

If you have the MBS plugins, I’m sure Christian can chime in and tell you which functions in the MBS will do the same thing.

You would do something like this using MBS:

Dim nsWindowObj As NSWindowMBS
Dim appearanceObj As NSAppearanceMBS

nsWindowObj = Self.NSWindowMBS

appearanceObj = NSAppearanceMBS.appearanceNamed(NSAppearanceMBS.NSAppearanceNameAqua)

If appearanceObj <> Nil Then
  NSAppearanceMBS.setAppearance(Self, appearanceObj)
End If

Unfortunately, unless Christian has added it very recently it looks like he hasn’t defined NSAppearanceNameDarkAqua. However, looking at some of the other constants it might just be the value "NSAppearanceNameDarkAqua".
eg: appearanceObj = NSAppearanceMBS.appearanceNamed("NSAppearanceNameDarkAqua")

This in the app’s Open event works for me:

  #If targetMacOS Then
    Declare Function NSClassFromString Lib "Cocoa" ( inClassName As CFStringRef ) As Integer
    Declare Sub setAppearance Lib "Cocoa" selector "setAppearance:" ( NSApplicationInstance As Integer, NSAppearanceInstance As Integer )
    Declare Function sharedApplication Lib "Cocoa" selector "sharedApplication" ( classRef As Integer ) As Integer
    Declare Function NSAppearanceNamed Lib "Cocoa" selector "appearanceNamed:" ( NSAppearanceClass As Integer, name As CFStringRef ) As Integer
    
    setAppearance( sharedApplication( NSClassFromString( "NSApplication" ) ), _
    NSAppearanceNamed( NSClassFromString( "NSAppearance" ), "NSAppearanceNameDarkAqua" ) )
  #EndIf

Actually, it even works if I put it in a Window’s Open event …

BevelButton is a bit problematic but Norman’s replacement seems to work …

Note: Xojo 2018R3 & MacOS 10.14 Mojave

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 also HAD tried this build script for MacOS X with “applies to: both” but it hung Xojo - needed a Force Quit:

Dim App As String = CurrentBuildLocation + "/""" + CurrentBuildAppName + ".app"""
Call DoShellCommand("/usr/bin/defaults write " + App + "/Contents/Info 'NSRequiresAquaSystemAppearance' 'NO'")

Any idea where it went wrong?

Hi Sam,

thanks, your code works (autocomplete did not give me a NSAppearanceNameDarkAqua so I went with the following, using it in the Action event of the CheckBox):

Sub Action() Handles Action
    
  If Me.value Then
    
    Dim nsWindowObj As NSWindowMBS
    Dim appearanceObj As NSAppearanceMBS
    
    nsWindowObj = Self.NSWindowMBS
    
    appearanceObj = NSAppearanceMBS.appearanceNamed(NSAppearanceMBS.NSAppearanceNameVibrantDark)
    
    If appearanceObj <> Nil Then
      NSAppearanceMBS.setAppearance(Self, appearanceObj)
    End If
    
    
  Else
        
    Dim nsWindowObj As NSWindowMBS
    Dim appearanceObj As NSAppearanceMBS
    
    nsWindowObj = Self.NSWindowMBS
    
    appearanceObj = NSAppearanceMBS.appearanceNamed(NSAppearanceMBS.NSAppearanceNameVibrantLight)
    
    If appearanceObj <> Nil Then
      NSAppearanceMBS.setAppearance(Self, appearanceObj)
    End If
    
  End If
  
End Sub

Except for my ListBox as it uses custom drawing, so need a bit more effort there:

Sorry. I’ll add NSAppearanceNameDarkAqua soon.

/usr/bin/defaults write " + App + "/Contents/Info 'NSRequiresAquaSystemAppearance' 'NO'"

Should be “Info.plist”.

You’re welcome.

DarkAqua was added in 10.14 Mojave and NSAppearanceNameVibrantDark was deprecated. So for older (all the way back to Yosemite) use DarkVibrant, 10.14 + use DarkAqua.

the docs for defaults says you omit the .plist extension though ?