Skip to content

Commit

Permalink
Expose "colors" dictionary
Browse files Browse the repository at this point in the history
My ultimate plan with this in Blitz Search is to provide "easy" theme selection.  But I do think that AvaloniaEdit.TextMate should use the editor background to fix what I see as a bug in the AvaloniaEdit demonstration, where some themes don't look like they were meant for a dark background.

Having things like selection highlights and all that come from a place that's already authored will ease up a lot of UI work for me ( color pickers and what-not ).
  • Loading branch information
Natestah committed Jun 22, 2024
1 parent 3a00ae0 commit 69b92f8
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 7 deletions.
10 changes: 10 additions & 0 deletions src/TextMateSharp.Demo/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@ static void Main(string[] args)
}
}

var colorDictionary = theme.GetGuiColorDictionary();
if (colorDictionary is { Count: > 0 })
{
Console.WriteLine("Gui Control Colors");
foreach (var kvp in colorDictionary)
{
Console.WriteLine( $" {kvp.Key}, {kvp.Value}");
}
}

Console.WriteLine("File {0} tokenized in {1}ms.",
Path.GetFileName(fileToParse),
Environment.TickCount - tokenizeIni);
Expand Down
11 changes: 11 additions & 0 deletions src/TextMateSharp/Internal/Themes/ThemeRaw.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class ThemeRaw : Dictionary<string, object>, IRawTheme, IRawThemeSetting,
private static string NAME = "name";
private static string INCLUDE = "include";
private static string SETTINGS = "settings";
private static string COLORS = "colors";
private static string TOKEN_COLORS = "tokenColors";
private static string SCOPE = "scope";
private static string FONT_STYLE = "fontStyle";
Expand Down Expand Up @@ -47,6 +48,16 @@ public ICollection<IRawThemeSetting> GetTokenColors()
return result.Cast<IRawThemeSetting>().ToList();
}

public ICollection<KeyValuePair<string,object>> GetGuiColors()
{
ICollection result = TryGetObject<ICollection>(COLORS);

if (result == null)
return null;

return result.Cast<KeyValuePair<string,object>>().ToList();
}

public object GetScope()
{
return TryGetObject<object>(SCOPE);
Expand Down
1 change: 1 addition & 0 deletions src/TextMateSharp/Themes/IRawTheme.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ public interface IRawTheme
string GetInclude();
ICollection<IRawThemeSetting> GetSettings();
ICollection<IRawThemeSetting> GetTokenColors();
ICollection<KeyValuePair<string,object>> GetGuiColors();
}
}
48 changes: 41 additions & 7 deletions src/TextMateSharp/Themes/Theme.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using TextMateSharp.Internal.Utils;
using TextMateSharp.Registry;
Expand All @@ -11,29 +12,40 @@ public class Theme
private ParsedTheme _theme;
private ParsedTheme _include;
private ColorMap _colorMap;
private Dictionary<string, string> _guiColorDictionary;

public static Theme CreateFromRawTheme(
IRawTheme source,
IRegistryOptions registryOptions)
{
ColorMap colorMap = new ColorMap();
var guiColorsDictionary = new Dictionary<string, string>();

var themeRuleList = ParsedTheme.ParseTheme(source,0);

ParsedTheme theme = ParsedTheme.CreateFromParsedTheme(
ParsedTheme.ParseTheme(source, 0),
themeRuleList,
colorMap);

IRawTheme themeInclude;
ParsedTheme include = ParsedTheme.CreateFromParsedTheme(
ParsedTheme.ParseInclude(source, registryOptions, 0),
ParsedTheme.ParseInclude(source, registryOptions, 0, out themeInclude),
colorMap);

return new Theme(colorMap, theme, include);
// First get colors from include, then try and overwrite with local colors..
// I don't see this happening currently, but here just in case that ever happens.
ParsedTheme.ParsedGuiColors(themeInclude, guiColorsDictionary);
ParsedTheme.ParsedGuiColors(source, guiColorsDictionary);

return new Theme(colorMap, theme, include, guiColorsDictionary);
}

Theme(ColorMap colorMap, ParsedTheme theme, ParsedTheme include)
Theme(ColorMap colorMap, ParsedTheme theme, ParsedTheme include, Dictionary<string,string> guiColorDictionary)
{
_colorMap = colorMap;
_theme = theme;
_include = include;
_guiColorDictionary = guiColorDictionary;
}

public List<ThemeTrieElementRule> Match(IList<string> scopeNames)
Expand All @@ -49,6 +61,11 @@ public List<ThemeTrieElementRule> Match(IList<string> scopeNames)
return result;
}

public ReadOnlyDictionary<string, string> GetGuiColorDictionary()
{
return new ReadOnlyDictionary<string, string>(this._guiColorDictionary);
}

public ICollection<string> GetColorMap()
{
return this._colorMap.GetColorMap();
Expand Down Expand Up @@ -92,19 +109,37 @@ internal static List<ParsedThemeRule> ParseTheme(IRawTheme source, int priority)
return result;
}

internal static void ParsedGuiColors(IRawTheme source, Dictionary<string,string> colorDictionary)
{
var colors = source.GetGuiColors();
if (colors == null)
{
return;
}
foreach (var kvp in colors)
{
colorDictionary[kvp.Key] = (string)kvp.Value;
}
}


internal static List<ParsedThemeRule> ParseInclude(
IRawTheme source,
IRegistryOptions registryOptions,
int priority)
int priority,
out IRawTheme themeInclude)
{
List<ParsedThemeRule> result = new List<ParsedThemeRule>();

string include = source.GetInclude();

if (string.IsNullOrEmpty(include))
{
themeInclude = null;
return result;
}

IRawTheme themeInclude = registryOptions.GetTheme(include);
themeInclude = registryOptions.GetTheme(include);

if (themeInclude == null)
return result;
Expand Down Expand Up @@ -270,7 +305,6 @@ static ParsedTheme ResolveParsedThemeRules(
root.Insert(rule.name, 0, rule.scope, rule.parentScopes, rule.fontStyle, colorMap.GetId(rule.foreground),
colorMap.GetId(rule.background));
}

return new ParsedTheme(defaults, root);
}

Expand Down

0 comments on commit 69b92f8

Please sign in to comment.