Cross platform detection of OS dark mode

Just starting out? Need help? Post your questions and find answers here.
coco2
Enthusiast
Enthusiast
Posts: 461
Joined: Mon Nov 25, 2013 5:38 am
Location: Australia

Cross platform detection of OS dark mode

Post by coco2 »

Has anyone written a function to detect whether the OS is in dark mode, Windows 10, Windows 11, macOS and Linux?
User avatar
Mindphazer
Enthusiast
Enthusiast
Posts: 486
Joined: Mon Sep 10, 2012 10:41 am
Location: Savoie

Re: Cross platform detection of OS dark mode

Post by Mindphazer »

MacBook Pro 16" M4 Pro - 24 Gb - MacOS 15.4.1 - Iphone 15 Pro Max - iPad at home
...and unfortunately... Windows at work...
User avatar
mk-soft
Always Here
Always Here
Posts: 6313
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Cross platform detection of OS dark mode

Post by mk-soft »

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
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4991
Joined: Sun Apr 12, 2009 6:27 am

Re: Cross platform detection of OS dark mode

Post by RASHAD »

For Windows
Read the registry

Code: Select all

HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize\SystemUsesLightTheme 
Egypt my love
coco2
Enthusiast
Enthusiast
Posts: 461
Joined: Mon Nov 25, 2013 5:38 am
Location: Australia

Re: Cross platform detection of OS dark mode

Post by coco2 »

Any idea how to read the registry? Searching the forum returns a lot of information that doesn't seem to explain it.
AZJIO
Addict
Addict
Posts: 2218
Joined: Sun May 14, 2017 1:48 am

Re: Cross platform detection of OS dark mode

Post by AZJIO »

coco2 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.
https://www.purebasic.fr/english/viewto ... 12&t=56204

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.
User avatar
jacdelad
Addict
Addict
Posts: 2027
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: Cross platform detection of OS dark mode

Post by jacdelad »

coco2 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.
https://www.purebasic.fr/english/viewtopic.php?t=56204
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
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4991
Joined: Sun Apr 12, 2009 6:27 am

Re: Cross platform detection of OS dark mode

Post by RASHAD »

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
infratec
Always Here
Always Here
Posts: 7662
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Cross platform detection of OS dark mode

Post by infratec »

But I think you want:

Code: Select all

AppsUseLightTheme
coco2
Enthusiast
Enthusiast
Posts: 461
Joined: Mon Nov 25, 2013 5:38 am
Location: Australia

Re: Cross platform detection of OS dark mode

Post by coco2 »

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
User avatar
ChrisR
Addict
Addict
Posts: 1484
Joined: Sun Jan 08, 2017 10:27 pm
Location: France

Re: Cross platform detection of OS dark mode

Post by ChrisR »

Depends on what you want to do with it, there are 2 keys: SystemUsesLightTheme and AppsUseLightTheme
coco2
Enthusiast
Enthusiast
Posts: 461
Joined: Mon Nov 25, 2013 5:38 am
Location: Australia

Re: Cross platform detection of OS dark mode

Post by coco2 »

Does Linux even have a dark mode or does it just have different themes?
User avatar
mk-soft
Always Here
Always Here
Posts: 6313
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Cross platform detection of OS dark mode

Post by mk-soft »

coco2 wrote: Wed Jun 12, 2024 4:44 am Does Linux even have a dark mode or does it just have different themes?
Depends on the Linux distribution.
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
coco2
Enthusiast
Enthusiast
Posts: 461
Joined: Mon Nov 25, 2013 5:38 am
Location: Australia

Re: Cross platform detection of OS dark mode

Post by coco2 »

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.
Post Reply