Cross platform detection of OS dark mode
Posted: Tue Jun 11, 2024 6:27 am
Has anyone written a function to detect whether the OS is in dark mode, Windows 10, Windows 11, macOS and Linux?
http://www.purebasic.com
https://www.purebasic.fr/english/
Code: Select all
HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Themes\Personalize\SystemUsesLightTheme
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
EndIfhttps://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.
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
Code: Select all
AppsUseLightThemeCode: 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
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?