Skip to content

Commit

Permalink
feat: themeable HighlightingManager (#46)
Browse files Browse the repository at this point in the history
* The default environment is set as static, so it can be
updated by GitExtensions with theme colors.
Any overrides in the actual themes would still be overriding the default
environment colors (the theme overrides were recently removed though).

* Make it possible to set colors as adaptable, to control if they should
be adapted for the themes or not.
Highlight colors hardcoded in the interface or in .xshd files
(unless they are system colors)
should generally be adapted, but colors in themes are likely to be
set to the intended color.
  • Loading branch information
gerhardol authored Jan 31, 2025
1 parent a23e8d6 commit 1cb6d9c
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,32 @@ public class DefaultHighlightingStrategy : IHighlightingStrategyUsingRuleSets
// Span state variables
protected bool inSpan;

public DefaultHighlightingStrategy() : this("Default")
{
}
// Default environment, can be overridden
public static readonly DefaultHighlightingStrategy Default = new();

public DefaultHighlightingStrategy(string name)
{
Name = name;

DigitColor = new HighlightColor(SystemColors.WindowText, bold: false, italic: false);
DefaultTextColor = new HighlightColor(SystemColors.WindowText, bold: false, italic: false);
environmentColors = Default.environmentColors;
DigitColor = Default.DigitColor;
DefaultTextColor = Default.DefaultTextColor;
}

private DefaultHighlightingStrategy() {}

static DefaultHighlightingStrategy()
{
Default.Name = "Default";
Default.DigitColor = new HighlightColor(nameof(SystemColors.WindowText), bold: false, italic: false);
Default.DefaultTextColor = new HighlightColor(nameof(SystemColors.WindowText), bold: false, italic: false);

// set small 'default color environment'
environmentColors = new Dictionary<string, HighlightColor>
// colors that are not system colors will be adapted to the theme
Default.environmentColors = new Dictionary<string, HighlightColor>
{
["Default"] = new HighlightBackground(nameof(SystemColors.WindowText), nameof(SystemColors.Window), bold: false, italic: false),
["Selection"] = new HighlightColor(SystemColors.WindowText, Color.FromArgb(0xc3, 0xc3, 0xff), bold: false, italic: false),
["Selection"] = new HighlightColor(SystemColors.WindowText, Color.FromArgb(0xc3, 0xc3, 0xff), bold: false, italic: false, adaptable: true),
["VRuler"] = new HighlightColor(nameof(SystemColors.ControlLight), nameof(SystemColors.Window), bold: false, italic: false),
["InvalidLines"] = new HighlightColor(Color.FromArgb(0xB6, 0xB6, 0xC0), bold: false, italic: false),
["CaretMarker"] = new HighlightColor(nameof(SystemColors.MenuBar), bold: false, italic: false),
Expand Down Expand Up @@ -284,7 +294,7 @@ private void ResolveExternalReferences()
public void SetColorFor(string name, HighlightColor color)
{
if (name == "Default")
DefaultTextColor = new HighlightColor(color.Color, color.Bold, color.Italic);
DefaultTextColor = new HighlightColor(color.Color, color.Bold, color.Italic, adaptable: color.Adaptable);
environmentColors[name] = color;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ public HighlightBackground(XmlElement el) : base(el)
/// <summary>
/// Creates a new instance of <see cref="HighlightBackground" />
/// </summary>
public HighlightBackground(Color color, Color backgroundcolor, bool bold, bool italic) : base(color, backgroundcolor, bold, italic)
{
}

public HighlightBackground(string systemColor, string systemBackgroundColor, bool bold, bool italic) : base(systemColor, systemBackgroundColor, bold, italic)
{
}
Expand Down
14 changes: 12 additions & 2 deletions Project/Src/Document/HighlightingStrategy/HighlightColor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,32 +117,35 @@ public HighlightColor(HighlightColor original, Color color, Color backColor)
Italic = original.Italic;
HasForeground = original.HasForeground;
HasBackground = original.HasBackground;
Adaptable = original.Adaptable;
Color = color;
BackgroundColor = backColor;
}

/// <summary>
/// Creates a new instance of <see cref="HighlightColor" />
/// </summary>
public HighlightColor(Color color, bool bold, bool italic)
public HighlightColor(Color color, bool bold, bool italic, bool adaptable = true)
{
HasForeground = true;
Color = color;
Bold = bold;
Italic = italic;
Adaptable = adaptable;
}

/// <summary>
/// Creates a new instance of <see cref="HighlightColor" />
/// </summary>
public HighlightColor(Color color, Color backgroundcolor, bool bold, bool italic)
public HighlightColor(Color color, Color backgroundcolor, bool bold, bool italic, bool adaptable = true)
{
HasForeground = true;
HasBackground = true;
Color = color;
BackgroundColor = backgroundcolor;
Bold = bold;
Italic = italic;
Adaptable = adaptable;
}

/// <summary>
Expand All @@ -158,6 +161,7 @@ public HighlightColor(string systemColor, string systemBackgroundColor, bool bol

Bold = bold;
Italic = italic;
Adaptable = false;
}

/// <summary>
Expand All @@ -171,6 +175,7 @@ public HighlightColor(string systemColor, bool bold, bool italic)

Bold = bold;
Italic = italic;
Adaptable = false;
}

public bool HasForeground { get; }
Expand All @@ -197,6 +202,11 @@ public HighlightColor(string systemColor, bool bold, bool italic)
/// </value>
public Color Color { get; }

/// <value>
/// If the color should be adpted to the theme or if it is absolute
/// </value>
public bool Adaptable { get; } = true;

/// <value>
/// The font used
/// </value>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public void ReloadSyntaxModes()

private void CreateDefaultHighlightingStrategy()
{
var defaultHighlightingStrategy = new DefaultHighlightingStrategy();
var defaultHighlightingStrategy = DefaultHighlightingStrategy.Default;
defaultHighlightingStrategy.Extensions = new string[] { };
defaultHighlightingStrategy.Rules.Add(new HighlightRuleSet());
HighlightingDefinitions["Default"] = defaultHighlightingStrategy;
Expand Down Expand Up @@ -144,4 +144,4 @@ protected virtual void OnReloadSyntaxHighlighting(EventArgs e)

public event EventHandler ReloadSyntaxHighlighting;
}
}
}
2 changes: 1 addition & 1 deletion Project/Src/Document/LineManager/LineSegment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public HighlightColor GetColorForPosition(int x)
}
}

return new HighlightColor(SystemColors.WindowText, bold: false, italic: false);
return new HighlightColor(nameof(SystemColors.WindowText), bold: false, italic: false);
}

/// <summary>
Expand Down

0 comments on commit 1cb6d9c

Please sign in to comment.