# Switch app between Dark and Light mode with NSAppearanceMBS

**URL:** <https://forum.xojo.com/t/switch-app-between-dark-and-light-mode-with-nsappearancembs/67557>\
**Category:** Extras\
**Created:** [December 22, 2021, 5:41am UTC](https://forum.xojo.com/t/switch-app-between-dark-and-light-mode-with-nsappearancembs/67557 "2021-12-22T05:41:03Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Beatrix\_Willius](https://forum.xojo.com/user_avatar/forum.xojo.com/beatrix_willius/32/282_2.png) [@Beatrix\_Willius](https://forum.xojo.com/u/Beatrix_Willius)\
**Post date:** [December 22, 2021, 5:41am UTC](https://forum.xojo.com/t/switch-app-between-dark-and-light-mode-with-nsappearancembs/67557/1 "2021-12-22T05:41:03Z")

</div>

How do I switch my whole app from Dark to Light Mode and vice versa with NSAppearanceMBS? The code of the NSAppearanceMBS:

```auto
dim t as string = NSAppearanceMBS.NSAppearanceNameVibrantDark

dim a as NSAppearanceMBS = NSAppearanceMBS.appearanceNamed(t)

if a = nil then
  MsgBox "No Appearance with that name?"
else
  NSAppearanceMBS.setCurrentAppearance a
  NSAppearanceMBS.setAppearance(self, a) // apply to window
end if

```

produces very odd results in complex windows. And only one window is switched:

 ![Screenshot 2021-12-22 at 06.35.26](https://forum.xojo.com/uploads/default/original/2X/5/5bb5996d7d0b8b49e8660a1e933cb3098fed21fd.jpeg)

The main window in the background of the screenshot doesn’t switch modes at all. The window in the front of the screenshot uses custom colors so that the grey background is okay. But why do the listbox and the checkbox look okay while all the labels are not okay?

---

<div class="post-metadata">

**Author:** ![Christian\_Schmitz](https://forum.xojo.com/user_avatar/forum.xojo.com/christian_schmitz/32/158_2.png) [@Christian\_Schmitz](https://forum.xojo.com/u/Christian_Schmitz)\
**Post date:** [December 22, 2021, 8:16am UTC](https://forum.xojo.com/t/switch-app-between-dark-and-light-mode-with-nsappearancembs/67557/2 "2021-12-22T08:16:46Z")

</div>

Switching via OS methods may not trigger Xojo framework to switch with it.

---

<div class="post-metadata">

**Author:** ![Beatrix\_Willius](https://forum.xojo.com/user_avatar/forum.xojo.com/beatrix_willius/32/282_2.png) [@Beatrix\_Willius](https://forum.xojo.com/u/Beatrix_Willius)\
**Post date:** [December 22, 2021, 9:13am UTC](https://forum.xojo.com/t/switch-app-between-dark-and-light-mode-with-nsappearancembs/67557/3 "2021-12-22T09:13:36Z")

</div>

So what you say is “tough luck”?

---

<div class="post-metadata">

**Author:** ![Martin\_T](https://forum.xojo.com/letter_avatar_proxy/v4/letter/m/a88e57/32.png) [@Martin\_T](https://forum.xojo.com/u/Martin_T)\
**Post date:** [December 22, 2021, 9:16am UTC](https://forum.xojo.com/t/switch-app-between-dark-and-light-mode-with-nsappearancembs/67557/4 "2021-12-22T09:16:00Z")

</div>

```auto
Declare Function NSClassFromString Lib "AppKit" (inClassName As CFStringRef) As Integer
Declare Sub setAppearance Lib "AppKit" Selector "setAppearance:" (NSApplicationInstance As Integer, NSAppearanceInstance As Integer)
Declare Function sharedApplication Lib "AppKit" Selector "sharedApplication" (classRef As Integer) As Integer
Declare Function NSAppearanceNamed Lib "AppKit" Selector "appearanceNamed:" (NSAppearanceClass As Integer, name As CFStringRef) As Integer

setAppearance(sharedApplication(NSClassFromString("NSApplication")), _
  NSAppearanceNamed(NSClassFromString("NSAppearance"), _
  "NSAppearanceNameVibrantLight"))

```

---

<div class="post-metadata">

**Author:** ![Beatrix\_Willius](https://forum.xojo.com/user_avatar/forum.xojo.com/beatrix_willius/32/282_2.png) [@Beatrix\_Willius](https://forum.xojo.com/u/Beatrix_Willius)\
**Post date:** [December 22, 2021, 9:37am UTC](https://forum.xojo.com/t/switch-app-between-dark-and-light-mode-with-nsappearancembs/67557/5 "2021-12-22T09:37:27Z")

</div>

Thank, that works at least on all windows.

---

<div class="post-metadata">

**Author:** ![Christian\_Schmitz](https://forum.xojo.com/user_avatar/forum.xojo.com/christian_schmitz/32/158_2.png) [@Christian\_Schmitz](https://forum.xojo.com/u/Christian_Schmitz)\
**Post date:** [December 22, 2021, 10:20am UTC](https://forum.xojo.com/t/switch-app-between-dark-and-light-mode-with-nsappearancembs/67557/6 "2021-12-22T10:20:01Z")

</div>

But that sets appearance for whole app.  
While your code above tried to set it for a window!?

---

<div class="post-metadata">

**Author:** ![Beatrix\_Willius](https://forum.xojo.com/user_avatar/forum.xojo.com/beatrix_willius/32/282_2.png) [@Beatrix\_Willius](https://forum.xojo.com/u/Beatrix_Willius)\
**Post date:** [December 22, 2021, 11:11am UTC](https://forum.xojo.com/t/switch-app-between-dark-and-light-mode-with-nsappearancembs/67557/7 "2021-12-22T11:11:01Z")

</div>

What do I need to change to affect all windows?

---

<div class="post-metadata">

**Author:** ![Christian\_Schmitz](https://forum.xojo.com/user_avatar/forum.xojo.com/christian_schmitz/32/158_2.png) [@Christian\_Schmitz](https://forum.xojo.com/u/Christian_Schmitz)\
**Post date:** [December 22, 2021, 11:23am UTC](https://forum.xojo.com/t/switch-app-between-dark-and-light-mode-with-nsappearancembs/67557/8 "2021-12-22T11:23:03Z")

</div>

Something like

`NSAppearanceMBS.setAppearance(app, a)`

---

<div class="post-metadata">

**Author:** ![Beatrix\_Willius](https://forum.xojo.com/user_avatar/forum.xojo.com/beatrix_willius/32/282_2.png) [@Beatrix\_Willius](https://forum.xojo.com/u/Beatrix_Willius)\
**Post date:** [December 22, 2021, 12:08pm UTC](https://forum.xojo.com/t/switch-app-between-dark-and-light-mode-with-nsappearancembs/67557/9 "2021-12-22T12:08:25Z")

</div>

Great.

“Das funzt.”.

---

<div class="post-metadata">

**Author:** ![Sam\_Rowlands](https://forum.xojo.com/user_avatar/forum.xojo.com/sam_rowlands/32/265_2.png) [@Sam\_Rowlands](https://forum.xojo.com/u/Sam_Rowlands)\
**Post date:** [December 22, 2021, 2:02pm UTC](https://forum.xojo.com/t/switch-app-between-dark-and-light-mode-with-nsappearancembs/67557/10 "2021-12-22T14:02:40Z")

</div>

> [@Beatrix\_Willius](#):
>
> `NSAppearanceNameVibrantDark`
> 
> produces very odd results in complex windows. And only one window is switched:

> [@Martin\_T](#):
>
> `NSAppearanceNameVibrantLight`

Both of these will produce weird results, as technically we’re not supposed to directly use these values, instead these are meant to read during the paint or updateLayer event (same for the High Contrast versions).

The ones we should use are

```auto
NSAppearanceNameAqua

```

and

```auto
NSAppearanceNameDarkAqua

```

However DarkAqua doesn’t exist on 10.13, so then you have to use “NSAppearanceNameVibrantDark” if you want any resemblance of a system dark theme.

---

<div class="post-metadata">

**Author:** ![Beatrix\_Willius](https://forum.xojo.com/user_avatar/forum.xojo.com/beatrix_willius/32/282_2.png) [@Beatrix\_Willius](https://forum.xojo.com/u/Beatrix_Willius)\
**Post date:** [December 22, 2021, 2:23pm UTC](https://forum.xojo.com/t/switch-app-between-dark-and-light-mode-with-nsappearancembs/67557/11 "2021-12-22T14:23:14Z")

</div>

> [@Sam\_Rowlands](#):
>
> However DarkAqua doesn’t exist on 10.13, so then you have to use “NSAppearanceNameVibrantDark” if you want any resemblance of a system dark theme.

Good to know. The declare made my app crash anyways on High Sierra.

---

<div class="post-metadata">

**Author:** ![Martin\_T](https://forum.xojo.com/letter_avatar_proxy/v4/letter/m/a88e57/32.png) [@Martin\_T](https://forum.xojo.com/u/Martin_T)\
**Post date:** [December 22, 2021, 3:02pm UTC](https://forum.xojo.com/t/switch-app-between-dark-and-light-mode-with-nsappearancembs/67557/12 "2021-12-22T15:02:45Z")

</div>

> [@Beatrix\_Willius](#):
>
> Good to know. The declare made my app crash anyways on High Sierra.

It was a hard coded sample. You can find all possible names at [NSAppearanceName | Apple Developer Documentation](https://developer.apple.com/documentation/appkit/nsappearancename) including macOS version hints.

---

<div class="post-metadata">

**Author:** ![Beatrix\_Willius](https://forum.xojo.com/user_avatar/forum.xojo.com/beatrix_willius/32/282_2.png) [@Beatrix\_Willius](https://forum.xojo.com/u/Beatrix_Willius)\
**Post date:** [December 22, 2021, 3:31pm UTC](https://forum.xojo.com/t/switch-app-between-dark-and-light-mode-with-nsappearancembs/67557/13 "2021-12-22T15:31:03Z")

</div>

Next question @Christian_Schmitz : how do I get the system mode for Dark or Light? Color.IsDarkMode gives me the current appearance.

---

<div class="post-metadata">

**Author:** ![Christian\_Schmitz](https://forum.xojo.com/user_avatar/forum.xojo.com/christian_schmitz/32/158_2.png) [@Christian\_Schmitz](https://forum.xojo.com/u/Christian_Schmitz)\
**Post date:** [December 22, 2021, 4:45pm UTC](https://forum.xojo.com/t/switch-app-between-dark-and-light-mode-with-nsappearancembs/67557/14 "2021-12-22T16:45:03Z")

</div>

Well, [NSAppearanceMBS](https://www.monkeybreadsoftware.net/class-nsappearancembs.shtml) class has a name property and when the name contains “dark”, then it’s a dark mode.

So if [NSAppearanceMBS](https://www.monkeybreadsoftware.net/class-nsappearancembs.shtml).[currentAppearance](https://www.monkeybreadsoftware.net/cocoa-nsappearancembs-shared-method.shtml#5) contains dark or is same text as in [NSAppearanceNameDarkAqua](https://www.monkeybreadsoftware.net/cocoa-nsappearancembs-shared-method.shtml#12), then you have dark.  
Or compare to [NSAppearanceMBS](https://www.monkeybreadsoftware.net/class-nsappearancembs.shtml).NSAppearanceNameVibrantDark for older systems.

---

<div class="post-metadata">

**Author:** ![Beatrix\_Willius](https://forum.xojo.com/user_avatar/forum.xojo.com/beatrix_willius/32/282_2.png) [@Beatrix\_Willius](https://forum.xojo.com/u/Beatrix_Willius)\
**Post date:** [December 22, 2021, 5:31pm UTC](https://forum.xojo.com/t/switch-app-between-dark-and-light-mode-with-nsappearancembs/67557/15 "2021-12-22T17:31:23Z")

</div>

Well, as I thought the NSAppearanceMBS.currentAppearance is for the current appearance of my app. I need the global appearance setting of macOS.

---

<div class="post-metadata">

**Author:** ![anon93744516](https://forum.xojo.com/letter_avatar_proxy/v4/letter/a/5e9695/32.png) [@anon93744516](https://forum.xojo.com/u/anon93744516)\
**Post date:** [December 22, 2021, 5:47pm UTC](https://forum.xojo.com/t/switch-app-between-dark-and-light-mode-with-nsappearancembs/67557/16 "2021-12-22T17:47:16Z")

</div>

> [@Beatrix\_Willius](#):
>
> I need the global appearance setting of macOS.

Hi Beatrix,

If you need the light or dark mode of macOS in general, call `NSAppearanceMBS.currentAppearance` in your `App.Opening` Event and store the result in a property of the `App` Class, for later use.

Do the above assignment, before having your app logic switch it’s light or dark mode appearance.

Works for me. This allows me to provide three appearance options in an app, that can be changed at any time:

- System Assigned
- Light
- Dark

Note: I am also using the MBS plugins to manage appearance changes.

---

<div class="post-metadata">

**Author:** ![Sam\_Rowlands](https://forum.xojo.com/user_avatar/forum.xojo.com/sam_rowlands/32/265_2.png) [@Sam\_Rowlands](https://forum.xojo.com/u/Sam_Rowlands)\
**Post date:** [December 22, 2021, 10:06pm UTC](https://forum.xojo.com/t/switch-app-between-dark-and-light-mode-with-nsappearancembs/67557/17 "2021-12-22T22:06:41Z")

</div>

Yeah it will do. I’ll let @Martin_T explain why.

---

<div class="post-metadata">

**Author:** ![Sam\_Rowlands](https://forum.xojo.com/user_avatar/forum.xojo.com/sam_rowlands/32/265_2.png) [@Sam\_Rowlands](https://forum.xojo.com/u/Sam_Rowlands)\
**Post date:** [December 23, 2021, 12:08am UTC](https://forum.xojo.com/t/switch-app-between-dark-and-light-mode-with-nsappearancembs/67557/18 "2021-12-23T00:08:09Z")

</div>

> [@Beatrix\_Willius](#):
>
> Well, as I thought the NSAppearanceMBS.currentAppearance is for the current appearance of my app.

currentAppearance was deprecated for macOS 11 / 10.16, like Xojo, Apple renamed the function name to currentDrawingAppearance from macOS 11 onwards.

Please note that this function (and currentAppearance) are only guaranteed to return the correct appearance from certain events.

- Paint
- updateLayer (which Xojo doesn’t expose)
- viewDidChangeEffectiveAppearance (which Xojo doesn’t expose)

But also in those events you can use effectiveAppearance on the control to determine appearance. Appearances can alter based upon their parent controls.

> [@Beatrix\_Willius](#):
>
> I need the global appearance setting of macOS.

You can revert to the system default by setting the appearance of a control or the application object to nil and then it _should_ adopt the current system appearance. Once you’ve set it to nil, you can then retrieve it and match it’s name against the constants.

---

<div class="post-metadata">

**Author:** ![Beatrix\_Willius](https://forum.xojo.com/user_avatar/forum.xojo.com/beatrix_willius/32/282_2.png) [@Beatrix\_Willius](https://forum.xojo.com/u/Beatrix_Willius)\
**Post date:** [December 23, 2021, 6:12am UTC](https://forum.xojo.com/t/switch-app-between-dark-and-light-mode-with-nsappearancembs/67557/19 "2021-12-23T06:12:55Z")

</div>

> [@Sam\_Rowlands](#):
>
> You can revert to the system default by setting the appearance of a control or the application object to nil and then it _should_ adopt the current system appearance. Once you’ve set it to nil, you can then retrieve it and match it’s name against the constants.

How do I reset the appearance of the app?

---

<div class="post-metadata">

**Author:** ![Sam\_Rowlands](https://forum.xojo.com/user_avatar/forum.xojo.com/sam_rowlands/32/265_2.png) [@Sam\_Rowlands](https://forum.xojo.com/u/Sam_Rowlands)\
**Post date:** [December 23, 2021, 6:21am UTC](https://forum.xojo.com/t/switch-app-between-dark-and-light-mode-with-nsappearancembs/67557/20 "2021-12-23T06:21:56Z")

</div>

I assume with the MBS, you can simply pass NIL as the appearance object.

With the declares you pass in zero 0 for the appearance object.

[Next page](https://forum.xojo.com/t/switch-app-between-dark-and-light-mode-with-nsappearancembs/67557.md?page=2)
