Lazarus IDE and your program in dark mode on Microsoft Windows

Following up on my previous post about making the Pascal IDE Lazarus look more modern by docking its floating windows into one (and arguably more comfortable to some) single program window, this time I’d like to write about a dark-mode package which makes not only the IDE itself run in dark-mode on MS Windows, but even allows you to support dark-mode in your own applications.

Lazarus IDE in dark mode on Windows Lazarus IDE in dark mode on Windows

The dark mode package

The package you want is called “MetaDarkStyle” which you can download and install by using the Online Package Manager right from the IDE. In the Lazarus IDE navigate to “Package -> Online Package Manager”. In the “Filter by:” section enter MetaDarkStyle, mark the checkbox next to the package and click “Install” bellow. You will be asked to recompile the IDE which won’t take long. A new option should appear in “Tools -> Options” called “Dark Style” where you can switch between dark mode or light mode for the IDE.

Dark mode code editor

If you were using Lazarus with the default options, despite setting the IDE to be in dark mode, you probably have noticed that the code editor itself didn’t adapt to the rest of the IDE. That is because the code editor has its own colour schemes that can be changed by going to “Tools -> Options -> Editor -> Display -> Colors” and clicking on the second dropdown menu right at the top, to the right of the filter field.

Although by default the available choices aren’t too pleasing on the eyes, you can download user made color schemes from the Free Pascal wiki that would complement the rest of your now tinted IDE (and possibly cause less strain for your eyes). Notable choices are Mellow Evening, Breeze Dark, GitHub like, VS Code Dark, and Kurwish Dark.

Code editor extras - outlines

Outlines - with and without Outlines - with and without

I’ve also found it very useful to have outlines drawn for every code block in the code editor. You can enable them by going to “Tools -> Options -> Editor -> Display -> Markup and matches” and in the Matching Keywords section down in the scrollable checkbox list select all of them (or only the ones you fancy).

Having your own apps run in dark mode on Windows

Your program - dark mode vs the default light mode Your program - dark mode vs the default light mode

You can also tint your native Windows apps. For that, you’ll have to:

  1. include the MetaDarkStyle (not to be confused with metadarkstyledsgn) package into your project;
  2. add these units to your uses clause in your main project file:
uses
  uDarkStyleParams,
  uDarkStyleSchemes,
  uMetaDarkStyle,
  1. integrate parts of the code bellow into your project’s main begin...end statement.
begin
  // Your app options
  Application.Scaled := True;

  { - DARK MODE START - }
  // By default this is set to pamForceLight
  PreferredAppMode := pamForceDark;
  // This doesn't work if the above is set to pamForceLight
  uMetaDarkStyle.ApplyMetaDarkStyle(DefaultDark);
  { -  DARK MODE END  - }

  // Form creation logic
  Application.Initialize;
  Application.CreateForm(TFormMain, FormMain);
  Application.Run;
end.       

Checking if dark mode is enabled on Windows

This code is taken from the Free Pascal wiki. It allows us to check if dark mode is set in Windows so we could apply the dark mode theme to our program while keeping in mind our user’s system wide preferences.

// by "jwdietrich" from Lazarus forum
uses
  Windows, Win32Proc, Registry;
     
// IsDarkTheme: Detects if the Dark Theme (true) has been enabled or not (false)
function IsDarkTheme: boolean;
const
  KEYPATH = '\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize';
  KEYNAME = 'AppsUseLightTheme';
var
  LightKey: boolean;
  Registry: TRegistry;
begin
  Result := false;
  Registry := TRegistry.Create;
  try
    Registry.RootKey := HKEY_CURRENT_USER;
    if Registry.OpenKeyReadOnly(KEYPATH) then
      begin
        if Registry.ValueExists(KEYNAME) then
          LightKey := Registry.ReadBool(KEYNAME)
        else
          LightKey := true;
      end
    else
      LightKey := true;
    Result := not LightKey
  finally
    Registry.Free;
  end;
end;

Conclusion

Lazarus uses native widgetsets when possible. That’s why on Linux Mint I never had a problem with the IDE or my own written program not following the system-wide dark mode setting. The Windows widgetset (if I understand right) doesn’t support dark mode natively, as you have probably noticed that already when running certain applications and being greeted by a bright white panel and button mishmash. Hence a different approach is needed to support dark mode on Windows.

You can find more information about the Lazarus IDE at lazarus-ide.org and the Free Pascal compiler at freepascal.org.

Tags: lazarus pascal