From 82c35b280e22fc40c5cf306c4e9baee685aeb69c Mon Sep 17 00:00:00 2001 From: chinboy Date: Fri, 7 Feb 2025 09:28:49 +0800 Subject: [PATCH] Refactor GlobalTokens -> SharedTokens --- .../Assets/Themes/DaybreakBlueDark.xml | 4 +- .../Assets/Themes/DaybreakBlueLight.xml | 4 +- src/AtomUI.Theme/Theme.cs | 39 +++++++++---------- src/AtomUI.Theme/ThemeDefinition.cs | 6 +-- src/AtomUI.Theme/ThemeDefinitionReader.cs | 26 ++++++------- .../TokenSystem/ControlTokenConfigInfo.cs | 4 +- 6 files changed, 40 insertions(+), 43 deletions(-) diff --git a/src/AtomUI.Theme/Assets/Themes/DaybreakBlueDark.xml b/src/AtomUI.Theme/Assets/Themes/DaybreakBlueDark.xml index 4397141d5..b66806007 100644 --- a/src/AtomUI.Theme/Assets/Themes/DaybreakBlueDark.xml +++ b/src/AtomUI.Theme/Assets/Themes/DaybreakBlueDark.xml @@ -1,8 +1,8 @@ DarkAlgorithm - - + + \ No newline at end of file diff --git a/src/AtomUI.Theme/Assets/Themes/DaybreakBlueLight.xml b/src/AtomUI.Theme/Assets/Themes/DaybreakBlueLight.xml index e9add8c5b..3a872ebc2 100644 --- a/src/AtomUI.Theme/Assets/Themes/DaybreakBlueLight.xml +++ b/src/AtomUI.Theme/Assets/Themes/DaybreakBlueLight.xml @@ -1,8 +1,8 @@ DefaultAlgorithm - - + + \ No newline at end of file diff --git a/src/AtomUI.Theme/Theme.cs b/src/AtomUI.Theme/Theme.cs index 04ff9e259..02d2e0047 100644 --- a/src/AtomUI.Theme/Theme.cs +++ b/src/AtomUI.Theme/Theme.cs @@ -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; /// public class Theme : ITheme { + public static readonly IList 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 ControlTokens; - public static readonly IList 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() diff --git a/src/AtomUI.Theme/ThemeDefinition.cs b/src/AtomUI.Theme/ThemeDefinition.cs index 7bd56978b..70595db39 100644 --- a/src/AtomUI.Theme/ThemeDefinition.cs +++ b/src/AtomUI.Theme/ThemeDefinition.cs @@ -8,14 +8,14 @@ public class ThemeDefinition public string DisplayName { get; set; } public IList Algorithms { get; set; } public IDictionary ControlTokens { get; set; } - public IDictionary GlobalTokens { get; set; } + public IDictionary SharedTokens { get; set; } public ThemeDefinition(string id, string? displayName = null) { Id = id; Algorithms = new List(); ControlTokens = new Dictionary(); - GlobalTokens = new Dictionary(); + SharedTokens = new Dictionary(); DisplayName = displayName ?? id; } @@ -23,7 +23,7 @@ public class ThemeDefinition { Algorithms.Clear(); ControlTokens.Clear(); - GlobalTokens.Clear(); + SharedTokens.Clear(); DisplayName = string.Empty; } } \ No newline at end of file diff --git a/src/AtomUI.Theme/ThemeDefinitionReader.cs b/src/AtomUI.Theme/ThemeDefinitionReader.cs index 50f738bda..08ebac8cc 100644 --- a/src/AtomUI.Theme/ThemeDefinitionReader.cs +++ b/src/AtomUI.Theme/ThemeDefinitionReader.cs @@ -13,12 +13,12 @@ internal class ThemeDefinitionReader // 上下文信息 private readonly Stack _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."); } } diff --git a/src/AtomUI.Theme/TokenSystem/ControlTokenConfigInfo.cs b/src/AtomUI.Theme/TokenSystem/ControlTokenConfigInfo.cs index 166986773..7b0b06b75 100644 --- a/src/AtomUI.Theme/TokenSystem/ControlTokenConfigInfo.cs +++ b/src/AtomUI.Theme/TokenSystem/ControlTokenConfigInfo.cs @@ -4,10 +4,10 @@ public class ControlTokenConfigInfo { public bool UseAlgorithm { get; set; } = false; public string TokenId { get; set; } = string.Empty; - public IDictionary ControlTokens { get; set; } + public IDictionary Tokens { get; set; } public ControlTokenConfigInfo() { - ControlTokens = new Dictionary(); + Tokens = new Dictionary(); } } \ No newline at end of file