Page 1 of 1

Cross platform detection of OS dark mode

Posted: Tue Jun 11, 2024 6:27 am
by coco2
Has anyone written a function to detect whether the OS is in dark mode, Windows 10, Windows 11, macOS and Linux?

Re: Cross platform detection of OS dark mode

Posted: Tue Jun 11, 2024 9:16 am
by Mindphazer

Re: Cross platform detection of OS dark mode

Posted: Tue Jun 11, 2024 10:10 am
by mk-soft

Re: Cross platform detection of OS dark mode

Posted: Tue Jun 11, 2024 10:32 am
by RASHAD
For Windows
Read the registry

Code: Select all

HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize\SystemUsesLightTheme 

Re: Cross platform detection of OS dark mode

Posted: Tue Jun 11, 2024 11:29 am
by coco2
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

Posted: Tue Jun 11, 2024 11:38 am
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

Re: Cross platform detection of OS dark mode

Posted: Tue Jun 11, 2024 11:49 am
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

Re: Cross platform detection of OS dark mode

Posted: Tue Jun 11, 2024 12:22 pm
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

Re: Cross platform detection of OS dark mode

Posted: Tue Jun 11, 2024 12:33 pm
by infratec
But I think you want:

Code: Select all

AppsUseLightTheme

Re: Cross platform detection of OS dark mode

Posted: Tue Jun 11, 2024 1:11 pm
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

Re: Cross platform detection of OS dark mode

Posted: Tue Jun 11, 2024 4:25 pm
by ChrisR
Depends on what you want to do with it, there are 2 keys: SystemUsesLightTheme and AppsUseLightTheme

Re: Cross platform detection of OS dark mode

Posted: Wed Jun 12, 2024 4:44 am
by coco2
Does Linux even have a dark mode or does it just have different themes?

Re: Cross platform detection of OS dark mode

Posted: Fri Jun 14, 2024 9:23 am
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

Re: Cross platform detection of OS dark mode

Posted: Fri Jun 14, 2024 9:39 am
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.