mirror of
https://github.com/AtomUI/AtomUI.git
synced 2026-10-23 09:48:48 +08:00
Refactor GlobalTokens -> SharedTokens
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Theme name="Daybreak Blue Dark">
|
||||
<Algorithms>DarkAlgorithm</Algorithms>
|
||||
<GlobalTokens>
|
||||
</GlobalTokens>
|
||||
<SharedTokens>
|
||||
</SharedTokens>
|
||||
<ControlTokens>
|
||||
</ControlTokens>
|
||||
</Theme>
|
||||
@@ -1,8 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Theme Name="Daybreak Blue Light">
|
||||
<Algorithms>DefaultAlgorithm</Algorithms>
|
||||
<GlobalTokens>
|
||||
</GlobalTokens>
|
||||
<SharedTokens>
|
||||
</SharedTokens>
|
||||
<ControlTokens>
|
||||
</ControlTokens>
|
||||
</Theme>
|
||||
+18
-21
@@ -1,4 +1,5 @@
|
||||
using AtomUI.Theme.Styling;
|
||||
using System.Diagnostics;
|
||||
using AtomUI.Theme.Styling;
|
||||
using AtomUI.Theme.TokenSystem;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Styling;
|
||||
@@ -10,6 +11,8 @@ namespace AtomUI.Theme;
|
||||
/// </summary>
|
||||
public class Theme : ITheme
|
||||
{
|
||||
public static readonly IList<string> SUPPORTED_ALGORITHMS;
|
||||
|
||||
private string _id;
|
||||
private string? _loadErrorMsg;
|
||||
private ThemeVariant _themeVariant;
|
||||
@@ -23,7 +26,6 @@ public class Theme : ITheme
|
||||
protected ThemeDefinition? ThemeDefinition;
|
||||
protected ResourceDictionary ResourceDictionary;
|
||||
protected Dictionary<string, IControlDesignToken> ControlTokens;
|
||||
public static readonly IList<string> SUPPORTED_ALGORITHMS;
|
||||
|
||||
public string DefinitionFilePath { get; }
|
||||
|
||||
@@ -70,8 +72,8 @@ public class Theme : ITheme
|
||||
{
|
||||
ThemeDefinition = new ThemeDefinition(_id);
|
||||
NotifyLoadThemeDef();
|
||||
var themeDef = ThemeDefinition!;
|
||||
var globalTokenConfig = themeDef.GlobalTokens;
|
||||
var themeDef = ThemeDefinition;
|
||||
var sharedTokenConfig = themeDef.SharedTokens;
|
||||
var controlTokenConfig = themeDef.ControlTokens;
|
||||
CheckAlgorithmNames(themeDef.Algorithms);
|
||||
|
||||
@@ -98,26 +100,26 @@ public class Theme : ITheme
|
||||
}
|
||||
|
||||
IThemeVariantCalculator? baseCalculator = null;
|
||||
IThemeVariantCalculator calculator = default!;
|
||||
IThemeVariantCalculator? calculator = null;
|
||||
foreach (var algorithmId in themeDef.Algorithms)
|
||||
{
|
||||
calculator = CreateThemeVariantCalculator(algorithmId, baseCalculator);
|
||||
baseCalculator = calculator;
|
||||
}
|
||||
|
||||
Debug.Assert(calculator != null);
|
||||
ThemeVariantCalculator = calculator;
|
||||
_sharedToken.LoadConfig(globalTokenConfig);
|
||||
_sharedToken.LoadConfig(sharedTokenConfig);
|
||||
|
||||
ThemeVariantCalculator.Calculate(_sharedToken);
|
||||
ThemeVariantCalculator?.Calculate(_sharedToken);
|
||||
|
||||
// 交付最终的基础色
|
||||
_sharedToken.ColorBgBase = ThemeVariantCalculator.ColorBgBase;
|
||||
_sharedToken.ColorTextBase = ThemeVariantCalculator.ColorTextBase;
|
||||
_sharedToken.ColorBgBase = ThemeVariantCalculator?.ColorBgBase;
|
||||
_sharedToken.ColorTextBase = ThemeVariantCalculator?.ColorTextBase;
|
||||
|
||||
_sharedToken.CalculateAliasTokenValues();
|
||||
|
||||
// TODO 先用算法,然后再设置配置文件中的值,不知道合理不
|
||||
_sharedToken.LoadConfig(globalTokenConfig);
|
||||
_sharedToken.LoadConfig(sharedTokenConfig);
|
||||
_sharedToken.BuildResourceDictionary(ResourceDictionary);
|
||||
|
||||
CollectControlTokens();
|
||||
@@ -138,11 +140,11 @@ public class Theme : ITheme
|
||||
|
||||
// 需要 Review
|
||||
var controlAliasToken = (DesignToken)_sharedToken.Clone();
|
||||
controlAliasToken.LoadConfig(controlTokenInfo.ControlTokens);
|
||||
controlAliasToken.LoadConfig(controlTokenInfo.Tokens);
|
||||
|
||||
if (controlTokenInfo.UseAlgorithm)
|
||||
{
|
||||
ThemeVariantCalculator.Calculate(controlAliasToken);
|
||||
ThemeVariantCalculator?.Calculate(controlAliasToken);
|
||||
controlAliasToken.CalculateAliasTokenValues();
|
||||
}
|
||||
|
||||
@@ -150,7 +152,7 @@ public class Theme : ITheme
|
||||
controlToken.AssignSharedToken(controlAliasToken);
|
||||
(controlToken as AbstractControlDesignToken)!.IsCustomTokenConfig = true;
|
||||
(controlToken as AbstractControlDesignToken)!.CustomTokens =
|
||||
controlTokenInfo.ControlTokens.Keys.ToList();
|
||||
controlTokenInfo.Tokens.Keys.ToList();
|
||||
}
|
||||
|
||||
foreach (var controlToken in ControlTokens.Values)
|
||||
@@ -159,7 +161,7 @@ public class Theme : ITheme
|
||||
if (controlTokenConfig.ContainsKey(controlToken.Id))
|
||||
{
|
||||
(controlToken as AbstractControlDesignToken)!.LoadConfig(controlTokenConfig[controlToken.Id]
|
||||
.ControlTokens);
|
||||
.Tokens);
|
||||
}
|
||||
|
||||
controlToken.BuildResourceDictionary(ResourceDictionary);
|
||||
@@ -228,12 +230,7 @@ public class Theme : ITheme
|
||||
|
||||
public IControlDesignToken? GetControlToken(string tokenId)
|
||||
{
|
||||
if (ControlTokens.TryGetValue(tokenId, out var token))
|
||||
{
|
||||
return token;
|
||||
}
|
||||
|
||||
return null;
|
||||
return ControlTokens.GetValueOrDefault(tokenId);
|
||||
}
|
||||
|
||||
internal virtual void NotifyAboutToActive()
|
||||
|
||||
@@ -8,14 +8,14 @@ public class ThemeDefinition
|
||||
public string DisplayName { get; set; }
|
||||
public IList<string> Algorithms { get; set; }
|
||||
public IDictionary<string, ControlTokenConfigInfo> ControlTokens { get; set; }
|
||||
public IDictionary<string, string> GlobalTokens { get; set; }
|
||||
public IDictionary<string, string> SharedTokens { get; set; }
|
||||
|
||||
public ThemeDefinition(string id, string? displayName = null)
|
||||
{
|
||||
Id = id;
|
||||
Algorithms = new List<string>();
|
||||
ControlTokens = new Dictionary<string, ControlTokenConfigInfo>();
|
||||
GlobalTokens = new Dictionary<string, string>();
|
||||
SharedTokens = new Dictionary<string, string>();
|
||||
DisplayName = displayName ?? id;
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ public class ThemeDefinition
|
||||
{
|
||||
Algorithms.Clear();
|
||||
ControlTokens.Clear();
|
||||
GlobalTokens.Clear();
|
||||
SharedTokens.Clear();
|
||||
DisplayName = string.Empty;
|
||||
}
|
||||
}
|
||||
@@ -13,12 +13,12 @@ internal class ThemeDefinitionReader
|
||||
// 上下文信息
|
||||
private readonly Stack<string> _currentElementNames;
|
||||
private ControlTokenConfigInfo? _currentControlToken;
|
||||
private bool _inGlobalTokenCtx;
|
||||
private bool _inSharedTokenCtx;
|
||||
private bool _inControlTokenCtx;
|
||||
|
||||
private const string ThemeElementName = "Theme";
|
||||
private const string AlgorithmsElementName = "Algorithms";
|
||||
private const string GlobalTokensElementName = "GlobalTokens";
|
||||
private const string SharedTokensElementName = "SharedTokens";
|
||||
private const string ControlTokensElementName = "ControlTokens";
|
||||
private const string ControlTokenElementName = "ControlToken";
|
||||
private const string TokenElementName = "Token";
|
||||
@@ -75,7 +75,7 @@ internal class ThemeDefinitionReader
|
||||
_currentControlToken = null;
|
||||
_currentElementNames.Clear();
|
||||
_inControlTokenCtx = false;
|
||||
_inGlobalTokenCtx = false;
|
||||
_inSharedTokenCtx = false;
|
||||
_currentDef = null;
|
||||
}
|
||||
}
|
||||
@@ -92,10 +92,10 @@ internal class ThemeDefinitionReader
|
||||
{
|
||||
HandleStartAlgorithmsElement(reader);
|
||||
}
|
||||
else if (name == GlobalTokensElementName)
|
||||
else if (name == SharedTokensElementName)
|
||||
{
|
||||
_currentDef?.GlobalTokens.Clear();
|
||||
_inGlobalTokenCtx = true;
|
||||
_currentDef?.SharedTokens.Clear();
|
||||
_inSharedTokenCtx = true;
|
||||
_inControlTokenCtx = false;
|
||||
}
|
||||
else if (name == ControlTokensElementName)
|
||||
@@ -104,7 +104,7 @@ internal class ThemeDefinitionReader
|
||||
}
|
||||
else if (name == ControlTokenElementName)
|
||||
{
|
||||
_inGlobalTokenCtx = false;
|
||||
_inSharedTokenCtx = false;
|
||||
_inControlTokenCtx = true;
|
||||
HandleStartControlTokenElement(reader);
|
||||
}
|
||||
@@ -131,9 +131,9 @@ internal class ThemeDefinitionReader
|
||||
_currentControlToken = null;
|
||||
_inControlTokenCtx = false;
|
||||
}
|
||||
else if (name == GlobalTokensElementName)
|
||||
else if (name == SharedTokensElementName)
|
||||
{
|
||||
_inGlobalTokenCtx = false;
|
||||
_inSharedTokenCtx = false;
|
||||
}
|
||||
|
||||
_parseFinished = name == ThemeElementName;
|
||||
@@ -203,17 +203,17 @@ internal class ThemeDefinitionReader
|
||||
EmitRequiredAttrError(reader, NameAttrName);
|
||||
}
|
||||
|
||||
if (_inGlobalTokenCtx)
|
||||
if (_inSharedTokenCtx)
|
||||
{
|
||||
_currentDef!.GlobalTokens.Add(tokenName!, tokenValue);
|
||||
_currentDef!.SharedTokens.Add(tokenName!, tokenValue);
|
||||
}
|
||||
else if (_inControlTokenCtx)
|
||||
{
|
||||
_currentControlToken!.ControlTokens.Add(tokenName!, tokenValue);
|
||||
_currentControlToken!.Tokens.Add(tokenName!, tokenValue);
|
||||
}
|
||||
else
|
||||
{
|
||||
EmitErrorMsg(reader, "The Token element must appear under WidgetToken or GlobalTokens.");
|
||||
EmitErrorMsg(reader, "The Token element must appear under WidgetToken or SharedTokens.");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -4,10 +4,10 @@ public class ControlTokenConfigInfo
|
||||
{
|
||||
public bool UseAlgorithm { get; set; } = false;
|
||||
public string TokenId { get; set; } = string.Empty;
|
||||
public IDictionary<string, string> ControlTokens { get; set; }
|
||||
public IDictionary<string, string> Tokens { get; set; }
|
||||
|
||||
public ControlTokenConfigInfo()
|
||||
{
|
||||
ControlTokens = new Dictionary<string, string>();
|
||||
Tokens = new Dictionary<string, string>();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user