Skip to content

Commit d313a59

Browse files
committed
feat: introduce global AppColorSchemes for managing SQL and grid colors, and sync both with the app's dark mode (still only on Windows)
1 parent 3510728 commit d313a59

File tree

5 files changed

+264
-241
lines changed

5 files changed

+264
-241
lines changed

heidisql.lpr

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
RequireDerivedFormResource:=True;
5555
Application.Scaled:=True;
5656

57+
AppColorSchemes := TAppColorSchemes.Create(True);
5758
{$IFDEF WINDOWS}
5859
PreferredAppMode := pamAllowDark;
5960
case AppSettings.ReadInt(asThemeMode) of
@@ -65,15 +66,12 @@
6566
if PreferredAppMode = pamForceDark then
6667
uMetaDarkStyle.ApplyMetaDarkStyle(DefaultDark);
6768

69+
// Switch synedit and grid colors to dark mode and vice versa
6870
WasDarkMode := AppSettings.ReadBool(asCurrentThemeIsDark);
69-
if (not WasDarkMode) and IsDarkModeEnabled then begin
70-
// switch synedit colors and grid colors after current dark mode has changed
71-
SQLSynSchemes.ApplyScheme(SQLSynSchemes.SDarkScheme);
72-
end
73-
else if WasDarkMode and (not IsDarkModeEnabled) then begin
74-
// dito
75-
SQLSynSchemes.ApplyScheme(SQLSynSchemes.SLightScheme);
76-
end;
71+
if (not WasDarkMode) and IsDarkModeEnabled then
72+
AppColorSchemes.ApplyDark
73+
else if WasDarkMode and (not IsDarkModeEnabled) then
74+
AppColorSchemes.ApplyLight;
7775
AppSettings.WriteBool(asCurrentThemeIsDark, IsDarkModeEnabled);
7876
{$ENDIF}
7977

source/generic_types.pas

Lines changed: 195 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
unit generic_types;
22

3-
{$mode ObjFPC}
3+
{$mode delphi}{$H+}
44

55
interface
66

77
uses fpjson, jsonparser, SysUtils, RegExpr, SynEditHighlighter, SynHighlighterSQL,
8-
Generics.Collections, Graphics;
8+
Classes, Generics.Collections, Graphics, dbstructures;
99

1010
type
1111
TThreeStateBoolean = (nbUnset, nbFalse, nbTrue);
@@ -21,17 +21,33 @@ TRegExprUmlauts = class(TRegExpr)
2121
constructor Create;
2222
end;
2323

24-
// Light and dark Color schemes for SynEdit SQL highlighter
25-
TSynSQLSynList = specialize TObjectList<TSynSQLSyn>;
26-
TSynSQLSynSchemes = class(TSynSQLSynList)
24+
// Light and dark Color schemes for SynEdit SQL highlighter and grid colors
25+
TGridTextColors = Array[TDBDatatypeCategoryIndex] of TColor;
26+
TAppColorScheme = class(TObject)
27+
public
28+
Name: String;
29+
SynSqlSyn: TSynSqlsyn;
30+
GridTextColors: TGridTextColors;
31+
ActiveLineBackground: TColor;
32+
MatchingBraceForeground: TColor;
33+
MatchingBraceBackground: TColor;
34+
constructor Create;
35+
destructor Destroy; override;
36+
// Load colors from settings
37+
procedure LoadFromSettings;
38+
// Write colors to settings
39+
procedure Apply;
40+
end;
41+
//TAppColorSchemeList = specialize TObjectList<TAppColorScheme>;
42+
TAppColorSchemes = class(TObjectList<TAppColorScheme>)
2743
public
2844
const SDarkScheme = 'Dark';
2945
const SLightScheme = 'Light';
3046
const SBlackScheme = 'Black';
3147
const SWhiteScheme = 'White';
3248
constructor Create(AOwnsObjects: Boolean = True); reintroduce; overload;
33-
procedure ApplyScheme(Scheme: TSynSQLSyn); overload;
34-
procedure ApplyScheme(SchemeName: String); overload;
49+
procedure ApplyLight;
50+
procedure ApplyDark;
3551
end;
3652

3753
TFileExtImageIndex = record
@@ -62,7 +78,7 @@ TFileExtImageIndex = record
6278
);
6379

6480
var
65-
SQLSynSchemes: TSynSQLSynSchemes;
81+
AppColorSchemes: TAppColorSchemes;
6682

6783
implementation
6884

@@ -109,119 +125,210 @@ constructor TRegExprUmlauts.Create;
109125
WordChars := WordChars + 'äÄöÖüÜÿŸáÁéÉíÍóÓúÚýÝćĆńŃŕŔśŚźŹĺĹàÀèÈìÌòÒùÙâÂêÊîÎôÔûÛãÃõÕñÑçÇşŞţŢåÅæÆœŒøØß';
110126
end;
111127

112-
{ TSynSQLSynSchemes }
128+
{ TAppColorScheme }
129+
130+
constructor TAppColorScheme.Create;
131+
begin
132+
inherited Create;
133+
SynSqlSyn := TSynSQLSyn.Create(nil);
134+
SynSqlSyn.SQLDialect := sqlMySQL;
135+
end;
136+
137+
procedure TAppColorScheme.LoadFromSettings;
138+
var
139+
i: Integer;
140+
Attri: TSynHighlighterAttributes;
141+
begin
142+
Name := _('Current custom settings');
143+
for i:=0 to SynSqlSyn.AttrCount - 1 do begin
144+
Attri := SynSqlSyn.Attribute[i];
145+
Attri.Foreground := AppSettings.ReadInt(asHighlighterForeground, Attri.Name, Attri.Foreground);
146+
Attri.Background := AppSettings.ReadInt(asHighlighterBackground, Attri.Name, Attri.Background);
147+
// IntegerStyle gathers all font styles (bold, italic, ...) in one number
148+
Attri.IntegerStyle := AppSettings.ReadInt(asHighlighterStyle, Attri.Name, Attri.IntegerStyle);
149+
end;
150+
ActiveLineBackground := StringToColor(AppSettings.ReadString(asSQLColActiveLine));
151+
MatchingBraceBackground := StringToColor(AppSettings.ReadString(asSQLColMatchingBraceBackground));
152+
MatchingBraceForeground := StringToColor(AppSettings.ReadString(asSQLColMatchingBraceForeground));
153+
154+
GridTextColors[dtcInteger] := AppSettings.ReadInt(asFieldColorNumeric);
155+
GridTextColors[dtcReal] := AppSettings.ReadInt(asFieldColorReal);
156+
GridTextColors[dtcText] := AppSettings.ReadInt(asFieldColorText);
157+
GridTextColors[dtcBinary] := AppSettings.ReadInt(asFieldColorBinary);
158+
GridTextColors[dtcTemporal] := AppSettings.ReadInt(asFieldColorDatetime);
159+
GridTextColors[dtcSpatial] := AppSettings.ReadInt(asFieldColorSpatial);
160+
GridTextColors[dtcOther] := AppSettings.ReadInt(asFieldColorOther);
161+
end;
162+
163+
destructor TAppColorScheme.Destroy;
164+
begin
165+
SynSqlSyn.Free;
166+
inherited;
167+
end;
168+
169+
procedure TAppColorScheme.Apply;
170+
var
171+
i: Integer;
172+
Attri: TSynHighlighterAttributes;
173+
begin
174+
// Highlighter colors
175+
for i:=0 to SynSqlSyn.AttrCount - 1 do begin
176+
Attri := SynSqlSyn.Attribute[i];
177+
AppSettings.WriteInt(asHighlighterForeground, Attri.Foreground, Attri.Name);
178+
AppSettings.WriteInt(asHighlighterBackground, Attri.Background, Attri.Name);
179+
AppSettings.WriteInt(asHighlighterStyle, Attri.IntegerStyle, Attri.Name);
180+
end;
181+
// Colors sitting on a TSynEdit, not part of a highlighter
182+
AppSettings.WriteString(asSQLColActiveLine, ColorToString(ActiveLineBackground));
183+
AppSettings.WriteString(asSQLColMatchingBraceForeground, ColorToString(MatchingBraceForeground));
184+
AppSettings.WriteString(asSQLColMatchingBraceBackground, ColorToString(MatchingBraceBackground));
185+
// Grid data type colors
186+
AppSettings.WriteInt(asFieldColorNumeric, GridTextColors[dtcInteger]);
187+
AppSettings.WriteInt(asFieldColorReal, GridTextColors[dtcReal]);
188+
AppSettings.WriteInt(asFieldColorText, GridTextColors[dtcText]);
189+
AppSettings.WriteInt(asFieldColorBinary, GridTextColors[dtcBinary]);
190+
AppSettings.WriteInt(asFieldColorDatetime, GridTextColors[dtcTemporal]);
191+
AppSettings.WriteInt(asFieldColorSpatial, GridTextColors[dtcSpatial]);
192+
AppSettings.WriteInt(asFieldColorOther, GridTextColors[dtcOther]);
193+
end;
194+
195+
196+
{ TAppColorSchemes }
113197

114-
constructor TSynSQLSynSchemes.Create(AOwnsObjects: Boolean = True);
198+
constructor TAppColorSchemes.Create(AOwnsObjects: Boolean = True);
115199
var
116-
Scheme: TSynSQLSyn;
200+
Scheme: TAppColorScheme;
117201
begin
118-
inherited Create(AOwnsObjects);
202+
inherited; // Create(AOwnsObjects);
119203

120-
Scheme := TSynSQLSyn.Create(nil);
204+
Scheme := TAppColorScheme.Create;
205+
Scheme.LoadFromSettings;
206+
Add(Scheme);
207+
208+
Scheme := TAppColorScheme.Create;
121209
Scheme.Name := SDarkScheme;
122-
Scheme.SQLDialect := sqlMySQL;
123-
Scheme.CommentAttri.Foreground := 8710076;
124-
Scheme.DataTypeAttri.Foreground := 11184895;
125-
Scheme.FunctionAttri.Foreground := 15792639;
126-
Scheme.IdentifierAttri.Foreground := 6460927;
127-
Scheme.KeyAttri.Foreground := 15792639;
128-
Scheme.NumberAttri.Foreground := 4610525;
129-
Scheme.StringAttri.Foreground := 5293907;
130-
Scheme.SymbolAttri.Foreground := 15792639;
131-
Scheme.TableNameAttri.Foreground := 16755327;
132-
Scheme.VariableAttri.Foreground := clPurple;
210+
Scheme.SynSqlSyn.CommentAttri.Foreground := 8710076;
211+
Scheme.SynSqlSyn.DataTypeAttri.Foreground := 11184895;
212+
Scheme.SynSqlSyn.FunctionAttri.Foreground := 15792639;
213+
Scheme.SynSqlSyn.IdentifierAttri.Foreground := 6460927;
214+
Scheme.SynSqlSyn.KeyAttri.Foreground := 15792639;
215+
Scheme.SynSqlSyn.NumberAttri.Foreground := 4610525;
216+
Scheme.SynSqlSyn.StringAttri.Foreground := 5293907;
217+
Scheme.SynSqlSyn.SymbolAttri.Foreground := 15792639;
218+
Scheme.SynSqlSyn.TableNameAttri.Foreground := 16755327;
219+
Scheme.SynSqlSyn.VariableAttri.Foreground := clPurple;
220+
Scheme.ActiveLineBackground := clNone;
221+
Scheme.MatchingBraceForeground := $0028EFFF;
222+
Scheme.MatchingBraceBackground := $004D513B;
223+
Scheme.GridTextColors[dtcInteger] := $00FF9785;
224+
Scheme.GridTextColors[dtcReal] := $00D07D7D;
225+
Scheme.GridTextColors[dtcText] := $0073D573;
226+
Scheme.GridTextColors[dtcBinary] := $00C9767F;
227+
Scheme.GridTextColors[dtcTemporal] := $007373C9;
228+
Scheme.GridTextColors[dtcSpatial] := $00CECE73;
229+
Scheme.GridTextColors[dtcOther] := $0073C1C1;
133230
Add(Scheme);
134231

135-
Scheme := TSynSQLSyn.Create(nil);
232+
Scheme := TAppColorScheme.Create;
136233
Scheme.Name := SLightScheme;
137-
Scheme.SQLDialect := sqlMySQL;
138-
Scheme.CommentAttri.Foreground := clGray;
139-
Scheme.DataTypeAttri.Foreground := clMaroon;
140-
Scheme.FunctionAttri.Foreground := clNavy;
141-
Scheme.IdentifierAttri.Foreground := clOlive;
142-
Scheme.KeyAttri.Foreground := clBlue;
143-
Scheme.NumberAttri.Foreground := clPurple;
144-
Scheme.StringAttri.Foreground := clGreen;
145-
Scheme.SymbolAttri.Foreground := clBlue;
146-
Scheme.TableNameAttri.Foreground := clFuchsia;
147-
Scheme.VariableAttri.Foreground := clPurple;
234+
Scheme.SynSqlSyn.CommentAttri.Foreground := clGray;
235+
Scheme.SynSqlSyn.DataTypeAttri.Foreground := clMaroon;
236+
Scheme.SynSqlSyn.FunctionAttri.Foreground := clNavy;
237+
Scheme.SynSqlSyn.IdentifierAttri.Foreground := clOlive;
238+
Scheme.SynSqlSyn.KeyAttri.Foreground := clBlue;
239+
Scheme.SynSqlSyn.NumberAttri.Foreground := clPurple;
240+
Scheme.SynSqlSyn.StringAttri.Foreground := clGreen;
241+
Scheme.SynSqlSyn.SymbolAttri.Foreground := clBlue;
242+
Scheme.SynSqlSyn.TableNameAttri.Foreground := clFuchsia;
243+
Scheme.SynSqlSyn.VariableAttri.Foreground := clPurple;
244+
Scheme.ActiveLineBackground := clNone;
245+
Scheme.MatchingBraceForeground := clBlack;
246+
Scheme.MatchingBraceBackground := clAqua;
247+
Scheme.GridTextColors[dtcInteger] := $00FF0000;
248+
Scheme.GridTextColors[dtcReal] := $00FF0048;
249+
Scheme.GridTextColors[dtcText] := $00008000;
250+
Scheme.GridTextColors[dtcBinary] := $00800080;
251+
Scheme.GridTextColors[dtcTemporal] := $00000080;
252+
Scheme.GridTextColors[dtcSpatial] := $00808000;
253+
Scheme.GridTextColors[dtcOther] := $00008080;
148254
Add(Scheme);
149255

150-
Scheme := TSynSQLSyn.Create(nil);
256+
Scheme := TAppColorScheme.Create;
151257
Scheme.Name := SBlackScheme;
152-
Scheme.SQLDialect := sqlMySQL;
153-
Scheme.CommentAttri.Foreground := clBlack;
154-
Scheme.DataTypeAttri.Foreground := clBlack;
155-
Scheme.FunctionAttri.Foreground := clBlack;
156-
Scheme.IdentifierAttri.Foreground := clBlack;
157-
Scheme.KeyAttri.Foreground := clBlack;
158-
Scheme.NumberAttri.Foreground := clBlack;
159-
Scheme.StringAttri.Foreground := clBlack;
160-
Scheme.SymbolAttri.Foreground := clBlack;
161-
Scheme.TableNameAttri.Foreground := clBlack;
162-
Scheme.VariableAttri.Foreground := clBlack;
258+
Scheme.SynSqlSyn.CommentAttri.Foreground := clBlack;
259+
Scheme.SynSqlSyn.DataTypeAttri.Foreground := clBlack;
260+
Scheme.SynSqlSyn.FunctionAttri.Foreground := clBlack;
261+
Scheme.SynSqlSyn.IdentifierAttri.Foreground := clBlack;
262+
Scheme.SynSqlSyn.KeyAttri.Foreground := clBlack;
263+
Scheme.SynSqlSyn.NumberAttri.Foreground := clBlack;
264+
Scheme.SynSqlSyn.StringAttri.Foreground := clBlack;
265+
Scheme.SynSqlSyn.SymbolAttri.Foreground := clBlack;
266+
Scheme.SynSqlSyn.TableNameAttri.Foreground := clBlack;
267+
Scheme.SynSqlSyn.VariableAttri.Foreground := clBlack;
268+
Scheme.ActiveLineBackground := clNone;
269+
Scheme.MatchingBraceForeground := clBlack;
270+
Scheme.MatchingBraceBackground := clAqua;
271+
Scheme.GridTextColors[dtcInteger] := $00000000;
272+
Scheme.GridTextColors[dtcReal] := $00000000;
273+
Scheme.GridTextColors[dtcText] := $00000000;
274+
Scheme.GridTextColors[dtcBinary] := $00000000;
275+
Scheme.GridTextColors[dtcTemporal] := $00000000;
276+
Scheme.GridTextColors[dtcSpatial] := $00000000;
277+
Scheme.GridTextColors[dtcOther] := $00000000;
163278
Add(Scheme);
164279

165-
Scheme := TSynSQLSyn.Create(nil);
280+
Scheme := TAppColorScheme.Create;
166281
Scheme.Name := SWhiteScheme;
167-
Scheme.SQLDialect := sqlMySQL;
168-
Scheme.CommentAttri.Foreground := clWhite;
169-
Scheme.DataTypeAttri.Foreground := clWhite;
170-
Scheme.FunctionAttri.Foreground := clWhite;
171-
Scheme.IdentifierAttri.Foreground := clWhite;
172-
Scheme.KeyAttri.Foreground := clWhite;
173-
Scheme.NumberAttri.Foreground := clWhite;
174-
Scheme.StringAttri.Foreground := clWhite;
175-
Scheme.SymbolAttri.Foreground := clWhite;
176-
Scheme.TableNameAttri.Foreground := clWhite;
177-
Scheme.VariableAttri.Foreground := clWhite;
282+
Scheme.SynSqlSyn.CommentAttri.Foreground := clWhite;
283+
Scheme.SynSqlSyn.DataTypeAttri.Foreground := clWhite;
284+
Scheme.SynSqlSyn.FunctionAttri.Foreground := clWhite;
285+
Scheme.SynSqlSyn.IdentifierAttri.Foreground := clWhite;
286+
Scheme.SynSqlSyn.KeyAttri.Foreground := clWhite;
287+
Scheme.SynSqlSyn.NumberAttri.Foreground := clWhite;
288+
Scheme.SynSqlSyn.StringAttri.Foreground := clWhite;
289+
Scheme.SynSqlSyn.SymbolAttri.Foreground := clWhite;
290+
Scheme.SynSqlSyn.TableNameAttri.Foreground := clWhite;
291+
Scheme.SynSqlSyn.VariableAttri.Foreground := clWhite;
292+
Scheme.ActiveLineBackground := clNone;
293+
Scheme.MatchingBraceForeground := $0028EFFF;
294+
Scheme.MatchingBraceBackground := $004D513B;
295+
Scheme.GridTextColors[dtcInteger] := $00FFFFFF;
296+
Scheme.GridTextColors[dtcReal] := $00FFFFFF;
297+
Scheme.GridTextColors[dtcText] := $00FFFFFF;
298+
Scheme.GridTextColors[dtcBinary] := $00FFFFFF;
299+
Scheme.GridTextColors[dtcTemporal] := $00FFFFFF;
300+
Scheme.GridTextColors[dtcSpatial] := $00FFFFFF;
301+
Scheme.GridTextColors[dtcOther] := $00FFFFFF;
178302
Add(Scheme);
179303
end;
180304

181-
procedure TSynSQLSynSchemes.ApplyScheme(Scheme: TSynSQLSyn);
305+
procedure TAppColorSchemes.ApplyLight;
182306
var
183-
i: Integer;
184-
Attri: TSynHighlighterAttributes;
307+
Scheme: TAppColorScheme;
185308
begin
186-
for i:=0 to Scheme.AttrCount - 1 do begin
187-
Attri := Scheme.Attribute[i];
188-
AppSettings.WriteInt(asHighlighterForeground, Attri.Foreground, Attri.Name);
189-
AppSettings.WriteInt(asHighlighterBackground, Attri.Background, Attri.Name);
190-
AppSettings.WriteInt(asHighlighterStyle, Attri.IntegerStyle, Attri.Name);
309+
for Scheme in Self do begin
310+
if Scheme.Name = SLightScheme then begin
311+
Scheme.Apply;
312+
Break;
313+
end;
191314
end;
192-
if Scheme.Name = SDarkScheme then begin
193-
AppSettings.WriteString(asSQLColActiveLine, ColorToString(clNone));
194-
AppSettings.WriteString(asSQLColMatchingBraceForeground, ColorToString($0028EFFF));
195-
AppSettings.WriteString(asSQLColMatchingBraceBackground, ColorToString($004D513B));
196-
end
197-
else if Scheme.Name = SLightScheme then begin
198-
AppSettings.WriteString(asSQLColActiveLine, ColorToString(clNone));
199-
AppSettings.WriteString(asSQLColMatchingBraceForeground, AppSettings.GetDefaultString(asSQLColMatchingBraceForeground));
200-
AppSettings.WriteString(asSQLColMatchingBraceBackground, AppSettings.GetDefaultString(asSQLColMatchingBraceBackground));
201-
end
202315
end;
203316

204-
procedure TSynSQLSynSchemes.ApplyScheme(SchemeName: String);
317+
procedure TAppColorSchemes.ApplyDark;
205318
var
206-
Scheme: TSynSQLSyn;
207-
Found: Boolean;
319+
Scheme: TAppColorScheme;
208320
begin
209-
Found := False;
210321
for Scheme in Self do begin
211-
if Scheme.Name = SchemeName then begin
212-
ApplyScheme(Scheme);
213-
Found := True;
322+
if Scheme.Name = SDarkScheme then begin
323+
Scheme.Apply;
214324
Break;
215325
end;
216326
end;
217-
if not Found then
218-
raise Exception.Create('SQL scheme not found: '+SchemeName);
219327
end;
220328

221329
initialization
222330

223331
SetJSONInstanceType(jitNumberFloat, TJSONFloatNumberUntouched);
224332

225-
SQLSynSchemes := TSynSQLSynSchemes.Create(True);
226333

227334
end.

0 commit comments

Comments
 (0)