Skip to content

Commit b4afc3a

Browse files
committed
fix: reload color scheme after auto-apply, reintroduce ThemeIsDark with a detection for Windows and macOS, use MetaDarkStyle units only on Windows
1 parent 2bc36b4 commit b4afc3a

5 files changed

Lines changed: 49 additions & 13 deletions

File tree

heidisql.lpr

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
{$IFDEF UNIX} cthreads, {$ENDIF}
77
{$IFDEF DARWIN} iosxlocale, {$ENDIF}
88
Interfaces, // this includes the LCL widgetset
9-
uMetaDarkStyle, uDarkStyleParams, uDarkStyleSchemes,
9+
{$IFDEF WINDOWS} uMetaDarkStyle, uDarkStyleParams, uDarkStyleSchemes, {$ENDIF}
1010
SysUtils, Dialogs,
1111
Forms, printer4lazarus, datetimectrls, LCLTranslator, Translations,
1212
{ you can add units after this }
@@ -65,15 +65,14 @@
6565
end;
6666
if PreferredAppMode = pamForceDark then
6767
uMetaDarkStyle.ApplyMetaDarkStyle(DefaultDark);
68-
68+
{$ENDIF}
6969
// Switch synedit and grid colors to dark mode and vice versa
7070
WasDarkMode := AppSettings.ReadBool(asCurrentThemeIsDark);
71-
if (not WasDarkMode) and IsDarkModeEnabled then
71+
if (not WasDarkMode) and ThemeIsDark then
7272
AppColorSchemes.ApplyDark
73-
else if WasDarkMode and (not IsDarkModeEnabled) then
73+
else if WasDarkMode and (not ThemeIsDark) then
7474
AppColorSchemes.ApplyLight;
75-
AppSettings.WriteBool(asCurrentThemeIsDark, IsDarkModeEnabled);
76-
{$ENDIF}
75+
AppSettings.WriteBool(asCurrentThemeIsDark, ThemeIsDark);
7776

7877
Application.Initialize;
7978

source/apphelpers.pas

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ interface
88
Classes, SysUtils, Generics.Collections, Controls, RegExpr, Math, FileUtil,
99
StrUtils, Graphics, GraphUtil, LCLIntf, Forms, Clipbrd, Process, ActnList, Menus, Dialogs,
1010
Character, DateUtils, laz.VirtualTrees, SynEdit, SynCompletion, fphttpclient,
11-
{$IFDEF WINDOWS} Windows, Registry, {$ENDIF} DelphiCompat,
11+
{$IFDEF WINDOWS} Windows, Registry, uDarkStyleParams, {$ENDIF} DelphiCompat,
1212
dbconnection, dbstructures, jsonregistry, lazaruscompat, fpjson, SynEditKeyCmds, LazFileUtils, gettext, LazUTF8,
13-
IniFiles, GraphType, Sockets, uDarkStyleParams, Contnrs;
13+
IniFiles, GraphType, Sockets, Contnrs
14+
{$IFDEF DARWIN} , MacOSAll {$ENDIF};
1415

1516
type
1617

@@ -416,6 +417,7 @@ TAppSettings = class(TObject)
416417
procedure Help(Sender: TObject; Anchor: String);
417418
function IsPortFree(APort: Word; const AIP: string = '127.0.0.1'): Boolean;
418419
function GetThemeColor(Color: TColor): TColor;
420+
function ThemeIsDark: Boolean;
419421
function ProcessExists(pid: Cardinal; ExeNamePattern: String): Boolean;
420422
function SynCompletionProposalPrettyText(ImageIndex: Integer; LeftText, CenterText, RightText: String; LeftColor: TColor=-1; CenterColor: TColor=-1; RightColor: TColor=-1): String;
421423
function PopupComponent(Sender: TObject): TComponent;
@@ -2361,7 +2363,7 @@ function MessageDialog(const Title, Msg: string; DlgType: TMsgDlgType; Buttons:
23612363
Dialog.Caption := MainForm.ActiveConnection.Parameters.SessionName + ': ' + Dialog.Caption;
23622364
rx := TRegExpr.Create;
23632365
rx.Expression := 'https?://[^\s"]+';
2364-
if IsDarkModeEnabled then
2366+
if ThemeIsDark then
23652367
Dialog.Text := Msg
23662368
else // See issue #2036
23672369
Dialog.Text := rx.Replace(Msg, '<a href="$0">$0</a>', True);
@@ -2830,6 +2832,40 @@ function GetThemeColor(Color: TColor): TColor;
28302832
end;
28312833

28322834

2835+
function ThemeIsDark: Boolean;
2836+
{$IFDEF DARWIN}
2837+
var
2838+
cfValue: CFPropertyListRef;
2839+
s: CFStringRef;
2840+
buf: array[0..15] of UniChar;
2841+
{$ENDIF}
2842+
begin
2843+
{$IFDEF WINDOWS}
2844+
// This is not a system detection, but a more precise switch in MetaDarkStyle.
2845+
// On Windows, the user may have forced MetaDarkStyle's light mode when the system is in dark mode.
2846+
Result := uDarkStyleParams.IsDarkModeEnabled;
2847+
{$ENDIF}
2848+
{$IFDEF LINUX}
2849+
// Not yet possible to detect the system's dark mode. Ideas welcome.
2850+
Result := False;
2851+
{$ENDIF}
2852+
{$IFDEF DARWIN}
2853+
// Detect system's dark mode on macOS
2854+
cfValue := CFPreferencesCopyAppValue(
2855+
CFSTR('AppleInterfaceStyle'),
2856+
CFSTR('.GlobalPreferences')
2857+
);
2858+
if (cfValue <> nil) and (CFGetTypeID(cfValue) = CFStringGetTypeID) then begin
2859+
s := CFStringRef(cfValue);
2860+
if CFStringGetLength(s) <= Length(buf) then begin
2861+
CFStringGetCharacters(s, CFRangeMake(0, CFStringGetLength(s)), @buf[0]);
2862+
Result := CFStringCompare(s, CFSTR('Dark'), 0) = kCFCompareEqualTo;
2863+
end;
2864+
CFRelease(cfValue);
2865+
end;
2866+
{$ENDIF}
2867+
end;
2868+
28332869
function ProcessExists(pid: Cardinal; ExeNamePattern: String): Boolean;
28342870
{var
28352871
Proc: TProcessEntry32;

source/const.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const
88
CRLF = #13#10;
99
LB_UNIX = #10;
1010
LB_MAC = #13;
11-
LB_WIDE = WideChar($2027);
11+
LB_WIDE = system.WideChar($2027);
1212

1313
// Placeholder text for NULL values
1414
TEXT_NULL = '(NULL)';

source/generic_types.pas

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ procedure TAppColorSchemes.ApplyLight;
317317
for Scheme in Self do begin
318318
if Scheme.Name = SLightScheme then begin
319319
Scheme.Apply;
320+
First.LoadFromSettings;
320321
Break;
321322
end;
322323
end;
@@ -329,6 +330,7 @@ procedure TAppColorSchemes.ApplyDark;
329330
for Scheme in Self do begin
330331
if Scheme.Name = SDarkScheme then begin
331332
Scheme.Apply;
333+
First.LoadFromSettings;
332334
Break;
333335
end;
334336
end;

source/usermanager.pas

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ interface
77
uses
88
SysUtils, Classes, Graphics, Controls, Forms, Dialogs, ComCtrls, StdCtrls, EditBtn, Buttons,
99
ExtCtrls, ClipBrd, Generics.Collections, Generics.Defaults, RegExpr, extra_controls,
10-
dbconnection, dbstructures, dbstructures.mysql, apphelpers, laz.VirtualTrees, Menus,
11-
uDarkStyleParams;
10+
dbconnection, dbstructures, dbstructures.mysql, apphelpers, laz.VirtualTrees, Menus;
1211

1312
{$I const.inc}
1413

@@ -288,7 +287,7 @@ procedure TUserManagerForm.FormShow(Sender: TObject);
288287
FColorReadPriv := clGreen;
289288
FColorWritePriv := clMaroon;
290289
FColorAdminPriv := clNavy;
291-
if IsDarkModeEnabled then begin
290+
if ThemeIsDark then begin
292291
FColorReadPriv := ColorAdjustBrightness(FColorReadPriv, 128);
293292
FColorWritePriv := ColorAdjustBrightness(FColorWritePriv, 128);
294293
FColorAdminPriv := ColorAdjustBrightness(FColorAdminPriv, 128);

0 commit comments

Comments
 (0)