Cross platform detection of OS dark mode
Cross platform detection of OS dark mode
Has anyone written a function to detect whether the OS is in dark mode, Windows 10, Windows 11, macOS and Linux?
- Mindphazer
- Enthusiast

- Posts: 486
- Joined: Mon Sep 10, 2012 10:41 am
- Location: Savoie
Re: Cross platform detection of OS dark mode
Here for MacOS :
https://www.purebasic.fr/english/viewto ... 83#p494483
https://www.purebasic.fr/english/viewto ... 83#p494483
MacBook Pro 16" M4 Pro - 24 Gb - MacOS 15.4.1 - Iphone 15 Pro Max - iPad at home
...and unfortunately... Windows at work...
...and unfortunately... Windows at work...
Re: Cross platform detection of OS dark mode
For Linux ...
https://www.purebasic.fr/english/viewtopic.php?t=80298
https://www.purebasic.fr/english/viewtopic.php?t=80298
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Re: Cross platform detection of OS dark mode
For Windows
Read the registry
Read the registry
Code: Select all
HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize\SystemUsesLightTheme
Egypt my love
Re: Cross platform detection of OS dark mode
Any idea how to read the registry? Searching the forum returns a lot of information that doesn't seem to explain it.
Re: Cross platform detection of OS dark mode
https://www.purebasic.fr/english/viewto ... 12&t=56204coco2 wrote: Tue Jun 11, 2024 11:29 am Any idea how to read the registry? Searching the forum returns a lot of information that doesn't seem to explain it.
Code: Select all
EnableExplicit
Procedure.q RegRead(Root, KeyPath$, ValueName$)
Protected Size, ValueData.q = -1, hKey, Type
If #ERROR_SUCCESS = RegOpenKeyEx_(Root, KeyPath$, 0, #KEY_READ, @hKey)
If hKey And #ERROR_SUCCESS = RegQueryValueEx_(hKey, ValueName$, 0, @Type, 0, @Size)
If Type = #REG_DWORD And #ERROR_SUCCESS = RegQueryValueEx_(hKey, ValueName$, 0, 0, @ValueData, @Size)
Debug "successful, parameter value " + ValueName$ + ": " + ValueData
EndIf
EndIf
RegCloseKey_(hKey)
EndIf
ProcedureReturn ValueData
EndProcedure
Define res
res = RegRead(#HKEY_CURRENT_USER, "Software\Microsoft\Windows\CurrentVersion\Themes\Personalize", "SystemUsesLightTheme")
If res = -1
Debug "Error"
Else
Debug res
EndIf
Last edited by AZJIO on Tue Jun 11, 2024 12:22 pm, edited 4 times in total.
Re: Cross platform detection of OS dark mode
https://www.purebasic.fr/english/viewtopic.php?t=56204coco2 wrote: Tue Jun 11, 2024 11:29 am Any idea how to read the registry? Searching the forum returns a lot of information that doesn't seem to explain it.
Good morning, that's a nice tnetennba!
PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
Re: Cross platform detection of OS dark mode
Code: Select all
sRemMachName.s = ""
lTopLevelKey.l = #HKEY_CURRENT_USER
sKeyName.s = "Software\Microsoft\Windows\CurrentVersion\Themes\Personalize"
sValueName.s = "SystemUsesLightTheme"
Procedure.l QueryValueEx(lhkey.l, szValueName.s)
Shared vValue.s
cch = 255
sValue.s = Space(255)
lrc = RegQueryValueEx_(lhkey, szValueName, 0, @lType, 0, @cch)
Select lType
;For strings
Case #REG_SZ
lrc = RegQueryValueEx_(lhkey, szValueName, 0, @lType, @sValue.s, @cch)
If lrc = 0
vValue.s = Left(sValue.s, cch-1)
Else
vValue.s = "Empty"
EndIf
Case #REG_MULTI_SZ
lrc = RegQueryValueEx_(lhkey, szValueName, 0, @lType, @sValue.s, @cch)
If lrc = 0
vValue.s = Left(sValue.s, cch-1)
Else
vValue.s = "Empty"
EndIf
;For DWORDS
Case #REG_DWORD
lrc = RegQueryValueEx_(lhkey, szValueName, 0, @lType, @lValue, @cch)
If lrc = 0
vValue = Str(lValue)
EndIf
Default
;all other Data types not supported
lrc = -1
EndSelect
ProcedureReturn lrc
EndProcedure
lRetVal = RegConnectRegistry_(sRemMachName, lTopLevelKey.l, @lHKeyhandle)
lRetVal = RegOpenKeyEx_(lHKeyhandle, sKeyName, 0, #KEY_ALL_ACCESS, @lhkey)
lRetVal = QueryValueEx(lhkey, sValueName)
RegCloseKey_(lhkey)
If lRetVal = 0
If Val(vValue) = 1
Debug "System uses Light theme"
Else
Debug "System uses Dark theme"
EndIf
EndIf
Egypt my love
Re: Cross platform detection of OS dark mode
But I think you want:
Code: Select all
AppsUseLightThemeRe: Cross platform detection of OS dark mode
RASHAD I rejigged your code
Code: Select all
Procedure.l RegistryQueryValueEx(lhkey.l, szValueName.s, *vValue.Integer)
Protected lValue.l
Protected vValue.s
Protected cch = 255
Protected sValue.s = Space(255)
Protected lType.l
Protected lrc = RegQueryValueEx_(lhkey, szValueName, 0, @lType, 0, @cch)
Select lType
;For strings
Case #REG_SZ
lrc = RegQueryValueEx_(lhkey, szValueName, 0, @lType, @sValue.s, @cch)
If lrc = 0
vValue = Left(sValue.s, cch-1)
Else
vValue = "Empty"
EndIf
Case #REG_MULTI_SZ
lrc = RegQueryValueEx_(lhkey, szValueName, 0, @lType, @sValue.s, @cch)
If lrc = 0
vValue = Left(sValue.s, cch-1)
Else
vValue = "Empty"
EndIf
;For DWORDS
Case #REG_DWORD
lrc = RegQueryValueEx_(lhkey, szValueName, 0, @lType, @lValue, @cch)
Debug "lrc: " + lrc
If lrc = 0
Debug "lValue: " + lValue
vValue = Str(lValue)
EndIf
Debug "vValue: " + vValue
Default
;all other Data types not supported
lrc = -1
EndSelect
*vValue\i = Val(vValue)
ProcedureReturn lrc
EndProcedure
Procedure.i isDarkModeWindows()
Protected sRemMachName.s = ""
Protected lTopLevelKey.l = #HKEY_CURRENT_USER
Protected sKeyName.s = "Software\Microsoft\Windows\CurrentVersion\Themes\Personalize"
Protected sValueName.s = "SystemUsesLightTheme"
Protected Result.i = 0 ; light mode
Protected lRetVal.l
Protected lHKeyhandle.l
Protected lhkey.l
Protected vValue.i
lRetVal = RegConnectRegistry_(sRemMachName, lTopLevelKey.l, @lHKeyhandle)
lRetVal = RegOpenKeyEx_(lHKeyhandle, sKeyName, 0, #KEY_ALL_ACCESS, @lhkey)
lRetVal = RegistryQueryValueEx(lhkey, sValueName, @vValue)
Debug "vValue: " + vValue
RegCloseKey_(lhkey)
If lRetVal = 0 And vValue <> 1
Result = 1 ; dark theme
EndIf
ProcedureReturn Result
EndProcedure
Re: Cross platform detection of OS dark mode
Depends on what you want to do with it, there are 2 keys: SystemUsesLightTheme and AppsUseLightTheme
Re: Cross platform detection of OS dark mode
Does Linux even have a dark mode or does it just have different themes?
Re: Cross platform detection of OS dark mode
Depends on the Linux distribution.coco2 wrote: Wed Jun 12, 2024 4:44 am Does Linux even have a dark mode or does it just have different themes?
With my Linux example it works with Ubuntu and Mint-Linux. But not original Raspberry OS
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Re: Cross platform detection of OS dark mode
What does gtk-application-prefer-dark-theme do? I flipped the switch on Mint and it did nothing. The only thing that changes it is when I set a theme.


