Bookstore

Recent

Author Topic: Windows 10 1809 Dark Theme  (Read 3157 times)

Pascal

  • Hero Member
  • *****
  • Posts: 876
Windows 10 1809 Dark Theme
« on: October 08, 2018, 10:25:52 am »
Is there a way to make Dark Theme work for apps build with Lazarus?

As i understood Microsoft added support for standard UI controls and they only react on Dark Theme if they are used in File Explorer.
laz trunk - fpc trunk 32bit - Windows 10 Pro x64 (1909)

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8052
Re: Windows 10 1809 Dark Theme
« Reply #1 on: October 08, 2018, 10:33:15 am »
Usually you need to toggle something in the manifest file to allow newer features to be used. Best to do some research on Microsoft sites.

rvk

  • Hero Member
  • *****
  • Posts: 3943
Re: Windows 10 1809 Dark Theme
« Reply #2 on: October 08, 2018, 10:58:10 am »
I don't think it is possible for desktop apps (other then setting High Contrast options for the desktop).

Quote
The real problem with the new Dark Mode setting is that it doesn’t affect the Windows desktop theme at all. Desktop applications like File Explorer continue using the normal, light theme.

Windows has a built-in dark theme for desktop applications, but it probably isn’t ideal. To enable it, head to Settings > Ease of Access > High Contrast. On the right, enable the “Turn on High Contrast” option and set the “Choose a Theme” dropdown to the “High Contrast Black” setting. Click “Apply” to save the setting.
https://www.howtogeek.com/222614/how-to-enable-windows-10s-hidden-dark-theme/

PascalDragon

  • Hero Member
  • *****
  • Posts: 1088
  • Compiler Developer
Re: Windows 10 1809 Dark Theme
« Reply #3 on: October 08, 2018, 04:12:00 pm »
I don't think it is possible for desktop apps (other then setting High Contrast options for the desktop).

Quote
The real problem with the new Dark Mode setting is that it doesn’t affect the Windows desktop theme at all. Desktop applications like File Explorer continue using the normal, light theme.

Windows has a built-in dark theme for desktop applications, but it probably isn’t ideal. To enable it, head to Settings > Ease of Access > High Contrast. On the right, enable the “Turn on High Contrast” option and set the “Choose a Theme” dropdown to the “High Contrast Black” setting. Click “Apply” to save the setting.
https://www.howtogeek.com/222614/how-to-enable-windows-10s-hidden-dark-theme/

With the newest update it might be possible as Microsoft added Dark Theme support to File Explorer.

dbannon

  • Hero Member
  • *****
  • Posts: 973
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Windows 10 1809 Dark Theme
« Reply #4 on: June 16, 2019, 01:49:33 pm »
OK, posting to an old topic because its an old issue  :-)

My end users are getting excited about new Windows10 dark themes. Apparently 'some' applications now automatically switch themselves to a dark theme if the OS is so set.

I am wondering if anyone has yet found a way to determine if the (Windows) OS does in fact have that dark theme set ?

Its still most certainly not a case of the app inheriting its colours from the OS like we have on Linux and Mac but I guess I could, if I knew it was necessary, have each form reset its colours if the user appeared to have set a dark OS theme.

Otherwise, why bother to have an OS setting of Dark Theme ?

(And I still think Dark Themes are very, very ugly)

Davo
Lazarus 2, Linux (and reluctantly Win10, OSX)
My Project - https://github.com/tomboy-notes/tomboy-ng

PascalDragon

  • Hero Member
  • *****
  • Posts: 1088
  • Compiler Developer
Re: Windows 10 1809 Dark Theme
« Reply #5 on: June 17, 2019, 09:20:50 am »
There appears to be a registry key for this. See also this Stack Overflow question.

dbannon

  • Hero Member
  • *****
  • Posts: 973
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Windows 10 1809 Dark Theme
« Reply #6 on: June 18, 2019, 09:21:10 am »
Thanks PascalDragon, looks like you are right !

Code: Pascal  [Select]
  1.  procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   RegValue : string='';
  4.   Registry : TRegistry;
  5. begin
  6.   Registry := TRegistry.Create;
  7.   try
  8.     Registry.RootKey := HKEY_CURRENT_USER;
  9.     if Registry.OpenKeyReadOnly('\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize') then // this exists in HKCU
  10.         RegValue := inttostr(Registry.ReadInteger('AppsUseLightTheme'))
  11.     else showmessage('Key not found');
  12.     if RegValue <> '' then
  13.         showmessage('Key Value = ' + RegValue);
  14.   finally
  15.     Registry.Free;
  16.   end;
  17. end;
           

Your link mentions it in HKLM but I could only find it in _CURRENT_USER , its a REG_DWORD, 1 if normal 'light' theme, '0' if set to DarkTheme.

My guess is if its not been set by the user, .OpenKeyReadOnly() will return false, assume default light theme.

Davo
Lazarus 2, Linux (and reluctantly Win10, OSX)
My Project - https://github.com/tomboy-notes/tomboy-ng

jwdietrich

  • Hero Member
  • *****
  • Posts: 1071
    • formatio reticularis
Re: Windows 10 1809 Dark Theme
« Reply #7 on: March 25, 2020, 01:48:20 pm »
Thanks, @dbannon. Based on your code, I have written a platform-sensitive function that delivers information if the system is in dark mode. In order to remain compatible with older Windows version, I have slightly changed the registry code. This function provides information about the theme on macOS, too:

Code: Pascal  [Select]
  1. uses
  2.   // ...
  3.   {$IFDEF Darwin}
  4.   , MacOSAll
  5.   {$ENDIF}
  6.   {$IFDEF WINDOWS}
  7.   , Windows, Win32Proc, registry
  8.   {$ENDIF}
  9.   // ...
  10.  
  11. function IsMinMacOS(Maj, Min: integer): boolean;
  12.   { returns true, if this app runs on a macOS version as specified or newer }
  13.   {$IFDEF DARWIN}
  14. var
  15.   Major, Minor: SInt32;
  16.   theError: SInt16;
  17.   {$ENDIF}
  18. begin
  19.   result := false;
  20.   {$IFDEF DARWIN}
  21.   theError := Gestalt(gestaltSystemVersionMajor, Major);
  22.   if theError = 0 then
  23.     theError := Gestalt(gestaltSystemVersionMinor, Minor);
  24.   if theError = 0 then
  25.     if (Major = Maj) and (Minor >= Min) or (Major > Maj) then
  26.       Result := True;
  27.   {$ENDIF}
  28. end;
  29.  
  30. function MojaveOrNewer: boolean;
  31.   { returns true, if this app runs on macOS X 10.14 Mojave or newer }
  32. begin
  33.   result := false;
  34.   {$IFDEF DARWIN}
  35.   result := IsMinMacOS(10, 14);
  36.   {$ENDIF}
  37. end;
  38.  
  39. {$IFDEF LCLCocoa}
  40. {The following two functions were suggested by Hansaplast at https://forum.lazarus.freepascal.org/index.php/topic,43111.msg304366.html}
  41.  
  42. // Retrieve key's string value from user preferences. Result is encoded using NSStrToStr's default encoding.
  43. function GetPrefString(const KeyName : string) : string;
  44. begin
  45.   Result := NSStringToString(NSUserDefaults.standardUserDefaults.stringForKey(NSStr(@KeyName[1])));
  46. end;
  47. {$ENDIF}
  48.  
  49. // IsDarkTheme: Detects if the Dark Theme (true) has been enabled or not (false)
  50. function DarkTheme: boolean;
  51. {$IFDEF Windows}
  52. const
  53.   KEYPATH = '\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize';
  54.   KEYNAME = 'AppsUseLightTheme';
  55. var
  56.   LightKey: boolean;
  57.   Registry: TRegistry;
  58. {$ENDIF}
  59. begin
  60.   Result := false;
  61.   {$IFDEF Windows}
  62.   Registry := TRegistry.Create;
  63.   try
  64.     Registry.RootKey := HKEY_CURRENT_USER;
  65.     if Registry.OpenKeyReadOnly(KEYPATH) then
  66.       begin
  67.         if Registry.ValueExists(KEYNAME) then
  68.           LightKey := Registry.ReadBool(KEYNAME)
  69.         else
  70.           LightKey := true;
  71.       end
  72.     else
  73.       LightKey := true;
  74.       Result := not LightKey
  75.   finally
  76.     Registry.Free;
  77.   end;
  78.   {$ELSE}
  79.   {$IFDEF LCLCocoa}
  80.   if MojaveOrNewer then
  81.     Result := pos('DARK',UpperCase(GetPrefString('AppleInterfaceStyle'))) > 0;
  82.   {$ENDIF}
  83.   {$ENDIF}
  84. end;
  85.  

It should be easily extensible to Linux, too.
« Last Edit: March 25, 2020, 01:55:38 pm by jwdietrich »
function GetRandomNumber: integer; // xkcd.com
begin
  GetRandomNumber := 4; // chosen by fair dice roll. Guaranteed to be random.
end;

http://www.formatio-reticularis.de

Lazarus 2.0.6 | FPC 3.0.4 | PPC, Intel, ARM | macOS, Windows, Linux

lucamar

  • Hero Member
  • *****
  • Posts: 2572
Re: Windows 10 1809 Dark Theme
« Reply #8 on: March 25, 2020, 03:19:45 pm »
It should be easily extensible to Linux, too.

AFAICT, most Linuxen don't need this "extravaganza" ( ;) ): you just change the theme and (almost) all applications change to the new theme automagically. One would only need something like that for custom-drawn controls.

Though, admitedly, my experience isn't that wide-ranging; just Ubuntu/Debian and a few other Debian derived distros. And a guy out there that is fond of RHEL. That's all :-[
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.6/FPC 3.0.4 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

jwdietrich

  • Hero Member
  • *****
  • Posts: 1071
    • formatio reticularis
Re: Windows 10 1809 Dark Theme
« Reply #9 on: March 25, 2020, 04:42:50 pm »
AFAICT, most Linuxen don't need this "extravaganza" ( ;) ): you just change the theme and (almost) all applications change to the new theme automagically. One would only need something like that for custom-drawn controls.

It depends on both the app and the platform. On the Mac most standard apps are fine in dark mode, but if they use a custom design it is better to know about the theme. And in Windows 10 there is little support by the OS, so it is definitively advisable to have information on the mode.
function GetRandomNumber: integer; // xkcd.com
begin
  GetRandomNumber := 4; // chosen by fair dice roll. Guaranteed to be random.
end;

http://www.formatio-reticularis.de

Lazarus 2.0.6 | FPC 3.0.4 | PPC, Intel, ARM | macOS, Windows, Linux

Hansaplast

  • Hero Member
  • *****
  • Posts: 585
  • Tweaking4All.com
    • Tweaking4All
Re: Windows 10 1809 Dark Theme
« Reply #10 on: March 25, 2020, 04:50:00 pm »
Linux adapts easier for sure, but detecting if it is darktheme seems hard (which can also be my lack of Linux/GTK knowledge) - for example when you'd like to adapt your custom drawn control.


For now I use a sloppy/elaborate method where I convert clWindowText and clWindow to grayscale, and then compare the two. If the "grayscale of clWindow" < "grayscale clWindowText" then we seem to have DarkTheme.
I'm sure there is a better method ...

dbannon

  • Hero Member
  • *****
  • Posts: 973
    • tomboy-ng, a rewrite of the classic Tomboy
Re: Windows 10 1809 Dark Theme
« Reply #11 on: March 26, 2020, 01:17:39 pm »
Yes, on Linux it usually just works.  But that 'usually' implies exceptions, my app uses KMemo and it does need some help adapting to a dark theme, highlighted text colors etc.  I am not sure its any better than Hansaplast's method but this is what I do -
Code: Pascal  [Select]
  1. var
  2.     Col : string
  3. begin
  4. ......
  5.         // if char 3, 5 and 7 are all 'A' or above, we are not in a DarkTheme
  6.         Col := hexstr(qword(GetRGBColorResolvingParent()), 8);
  7.         Sett.DarkTheme := (Col[3] < 'A') and (Col[5] < 'A') and (Col[7] < 'A');

At least its easier than poking in the Windows Registery looking for something that may, or may not be there .....

Davo
 
Lazarus 2, Linux (and reluctantly Win10, OSX)
My Project - https://github.com/tomboy-notes/tomboy-ng

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8052
Re: Windows 10 1809 Dark Theme
« Reply #12 on: March 26, 2020, 03:36:00 pm »
It should be easily extensible to Linux, too.

AFAICT, most Linuxen don't need this "extravaganza" ( ;) ): you just change the theme and (almost) all applications change to the new theme automagically. One would only need something like that for custom-drawn controls.

And for syntax highlighting editors where the various colours are generally not specified by the theme, but the background is.

Hansaplast

  • Hero Member
  • *****
  • Posts: 585
  • Tweaking4All.com
    • Tweaking4All
Re: Windows 10 1809 Dark Theme
« Reply #13 on: March 26, 2020, 04:17:58 pm »

@dbannon: I like your method, it seems to take less effort than mine - thanks for the suggestion, I'll play with that for sure!  :)