From 5940b292045f148dd622f61ef09fcaeaedd5cf94 Mon Sep 17 00:00:00 2001 From: chinboy Date: Tue, 11 Feb 2025 15:08:05 +0800 Subject: [PATCH] Refactor Token --- .../ControlTokenResourceRegister.cs | 61 ----------------- .../TokenDefinitions/DesignToken.Alias.cs | 68 +++++++++---------- .../TokenDefinitions/DesignToken.cs | 60 +++++++--------- 3 files changed, 59 insertions(+), 130 deletions(-) delete mode 100644 src/AtomUI.Theme/ControlTokenResourceRegister.cs diff --git a/src/AtomUI.Theme/ControlTokenResourceRegister.cs b/src/AtomUI.Theme/ControlTokenResourceRegister.cs deleted file mode 100644 index 745913e05..000000000 --- a/src/AtomUI.Theme/ControlTokenResourceRegister.cs +++ /dev/null @@ -1,61 +0,0 @@ -using System.Diagnostics; -using AtomUI.Theme.Data; -using AtomUI.Theme.TokenSystem; -using Avalonia.Controls; -using Avalonia.Styling; - -namespace AtomUI.Theme; - -public class ControlTokenResourceRegister -{ - private Control _target; - private string _tokenId; - private string? _resourceCatalog; - private ResourceDictionary? _resourceDictionary; - - public ControlTokenResourceRegister(Control target, string tokenId, string? resourceCatalog = null) - { - _target = target; - _tokenId = tokenId; - _resourceCatalog = resourceCatalog; - } - - public void RegisterResources() - { - if (_resourceDictionary != null) - { - _target.Resources.MergedDictionaries.Remove(_resourceDictionary); - } - - var controlToken = TokenFinderUtils.FindControlToken(_target, _tokenId, _resourceCatalog); - Debug.Assert(controlToken != null); - _resourceDictionary = new ResourceDictionary(); - foreach (var entry in controlToken.GetSharedResourceDeltaDictionary()) - { - _resourceDictionary.Add(entry.Key, entry.Value); - } - - var themeVariant = TokenFinderUtils.FindThemeVariant(_target); - _target.Resources.ThemeDictionaries.Add(themeVariant, _resourceDictionary); - } - - private IControlDesignToken? FindControlToken() - { - IControlDesignToken? token = null; - var current = _target; - while (current != null) - { - if (current is IThemeConfigProvider configProvider) - { - token = configProvider.GetControlToken(_tokenId, _resourceCatalog); - if (token != null) - { - break; - } - } - current = (current as IStyleHost).StylingParent as Control; - } - - return token; - } -} \ No newline at end of file diff --git a/src/AtomUI.Theme/TokenSystem/TokenDefinitions/DesignToken.Alias.cs b/src/AtomUI.Theme/TokenSystem/TokenDefinitions/DesignToken.Alias.cs index d1aeebcfc..706762cfd 100644 --- a/src/AtomUI.Theme/TokenSystem/TokenDefinitions/DesignToken.Alias.cs +++ b/src/AtomUI.Theme/TokenSystem/TokenDefinitions/DesignToken.Alias.cs @@ -5,42 +5,42 @@ namespace AtomUI.Theme.TokenSystem; [GlobalDesignToken] public partial class DesignToken { - /// - /// 内容区域背景色(悬停) - /// 控制内容区域背景色在鼠标悬停时的样式。 - /// - public Color ColorFillContentHover { get; set; } + /// + /// 内容区域背景色(悬停) + /// 控制内容区域背景色在鼠标悬停时的样式。 + /// + public Color ColorFillContentHover { get; set; } - /// - /// 替代背景色 - /// 控制元素替代背景色。 - /// - public Color ColorFillAlter { get; set; } + /// + /// 替代背景色 + /// 控制元素替代背景色。 + /// + public Color ColorFillAlter { get; set; } - /// - /// 内容区域背景色 - /// 控制内容区域的背景色。 - /// - public Color ColorFillContent { get; set; } + /// + /// 内容区域背景色 + /// 控制内容区域的背景色。 + /// + public Color ColorFillContent { get; set; } - /// - /// 容器禁用态下的背景色 - /// 控制容器在禁用状态下的背景色。 - /// - public Color ColorBgContainerDisabled { get; set; } + /// + /// 容器禁用态下的背景色 + /// 控制容器在禁用状态下的背景色。 + /// + public Color ColorBgContainerDisabled { get; set; } - /// - /// 文本悬停态背景色 - /// 控制文本在悬停状态下的背景色。 - /// - public Color ColorBgTextHover { get; set; } + /// + /// 文本悬停态背景色 + /// 控制文本在悬停状态下的背景色。 + /// + public Color ColorBgTextHover { get; set; } + + /// + /// 文本激活态背景色 + /// 控制文本在激活状态下的背景色。 + /// + public Color ColorBgTextActive { get; set; } - /// - /// 文本激活态背景色 - /// 控制文本在激活状态下的背景色。 - /// - public Color ColorBgTextActive { get; set; } - /// /// 背景边框颜色 /// 控制元素背景边框的颜色。 @@ -52,7 +52,7 @@ public partial class DesignToken /// 用于作为分割线的颜色,此颜色和 colorBorderSecondary 的颜色一致,但是用的是透明色。 /// public Color ColorSplit { get; set; } - + /// /// 占位文本颜色 /// 控制占位文本的颜色。 @@ -189,7 +189,7 @@ public partial class DesignToken /// 控制组件项在禁用状态下的激活背景颜色 /// public Color ControlItemBgActiveDisabled { get; set; } // Note. It also is a color - + /// /// 线条宽度(聚焦态) /// @@ -290,7 +290,7 @@ public partial class DesignToken /// 控制内容元素垂直内间距,适用于小屏幕设备。 /// public double PaddingContentVerticalSM { get; set; } - + /// /// 外边距 XXS /// 控制元素外边距,最小尺寸。 diff --git a/src/AtomUI.Theme/TokenSystem/TokenDefinitions/DesignToken.cs b/src/AtomUI.Theme/TokenSystem/TokenDefinitions/DesignToken.cs index 37973c730..c27bce771 100644 --- a/src/AtomUI.Theme/TokenSystem/TokenDefinitions/DesignToken.cs +++ b/src/AtomUI.Theme/TokenSystem/TokenDefinitions/DesignToken.cs @@ -8,13 +8,12 @@ namespace AtomUI.Theme.TokenSystem; public partial class DesignToken : AbstractDesignToken { - /// + /// /// 现在这里的实现是写死的主色,后面是不是可以读取配置 /// private readonly IDictionary _defaultPresetColors; - - [NotTokenDefinition] - public IDictionary ColorPalettes { get; set; } + + [NotTokenDefinition] public IDictionary ColorPalettes { get; set; } public DesignToken() { @@ -61,12 +60,12 @@ public partial class DesignToken : AbstractDesignToken BorderRadius = new CornerRadius(6); ColorTransparent = Colors.Transparent; } - + public PresetPrimaryColor GetPresetPrimaryColor(PresetColorType colorType) { return _defaultPresetColors[colorType]; } - + public void SetColorPalette(PresetPrimaryColor primaryColor, ColorMap colorMap) { ColorPalettes[primaryColor] = colorMap; @@ -91,27 +90,18 @@ public partial class DesignToken : AbstractDesignToken var screenLG = 992; var screenXL = 1200; var screenXXL = 1600; - - // Motion - if (!EnableMotion) - { - MotionDurationFast = TimeSpan.FromMilliseconds(0); - MotionDurationMid = TimeSpan.FromMilliseconds(0); - MotionDurationSlow = TimeSpan.FromMilliseconds(0); - MotionDurationVerySlow = TimeSpan.FromMilliseconds(0); - } - + // setup alias token // ============== Background ============== // ColorFillContent = ColorFillSecondary; ColorFillContentHover = ColorFill; ColorFillAlter = ColorFillQuaternary; ColorBgContainerDisabled = ColorFillTertiary; - + // ============== Split ============== // ColorBorderBg = ColorBgContainer; ColorSplit = ColorUtils.AlphaColor(ColorBorderSecondary, ColorBgContainer); - + // ============== Text ============== // ColorTextPlaceholder = ColorTextQuaternary; ColorTextDisabled = ColorTextQuaternary; @@ -122,48 +112,48 @@ public partial class DesignToken : AbstractDesignToken ColorHighlight = ColorError; ColorBgTextHover = ColorFillSecondary; ColorBgTextActive = ColorFill; - + ColorIcon = ColorTextTertiary; ColorIconHover = ColorText; - + ColorErrorOutline = ColorUtils.AlphaColor(ColorErrorBg, ColorBgContainer); ColorWarningOutline = ColorUtils.AlphaColor(ColorWarningBg, ColorBgContainer); - + // Font FontSizeIcon = FontSizeSM; - + // icon IconSizeXS = (int)FontSizeSM - 2; IconSizeSM = (int)FontSizeSM; IconSize = (int)FontSize; IconSizeLG = (int)FontSizeLG; - + // Line LineWidthFocus = LineWidth * 2; WaveAnimationRange = LineWidth * 6; WaveStartOpacity = 0.4; - + // Control ControlOutlineWidth = LineWidth * 2; - + // Checkbox size and expand icon size ControlInteractiveSize = ControlHeight / 2; - + ControlItemBgHover = ColorFillTertiary; ControlItemBgActive = ColorPrimaryBg; ControlItemBgActiveHover = ColorPrimaryBgHover; ControlItemBgActiveDisabled = ColorFill; ColorControlOutline = ColorUtils.AlphaColor(ColorPrimaryBg, ColorBgContainer); - + FontWeightStrong = 600; OpacityLoading = 0.65; LinkDecoration = null; LinkHoverDecoration = null; LinkFocusDecoration = null; - + ControlPadding = 12; ControlPaddingSM = 8; - + PaddingXXS = SizeXXS; PaddingXS = SizeXS; PaddingSM = SizeSM; @@ -171,7 +161,7 @@ public partial class DesignToken : AbstractDesignToken PaddingMD = SizeMD; PaddingLG = SizeLG; PaddingXL = SizeXL; - + PaddingContentHorizontalLG = SizeLG; PaddingContentVerticalLG = SizeMS; PaddingContentHorizontal = SizeMS; @@ -179,7 +169,7 @@ public partial class DesignToken : AbstractDesignToken PaddingContentHorizontalSM = SizeSM; PaddingContentHorizontalXS = SizeXS; PaddingContentVerticalSM = SizeXS; - + MarginXXS = SizeXXS; MarginXS = SizeXS; MarginSM = SizeSM; @@ -188,7 +178,7 @@ public partial class DesignToken : AbstractDesignToken MarginLG = SizeLG; MarginXL = SizeXL; MarginXXL = SizeXXL; - + ScreenXS = screenXS; ScreenXSMin = screenXS; ScreenXSMax = screenSM - 1; @@ -206,7 +196,7 @@ public partial class DesignToken : AbstractDesignToken ScreenXLMax = screenXXL - 1; ScreenXXL = screenXXL; ScreenXXLMin = screenXXL; - + BoxShadows = new BoxShadows(new BoxShadow { OffsetX = 0, @@ -233,7 +223,7 @@ public partial class DesignToken : AbstractDesignToken Color = ColorUtils.FromRgbF(0.05, 0, 0, 0) } }); - + BoxShadowsSecondary = new BoxShadows(new BoxShadow { OffsetX = 0, @@ -260,7 +250,7 @@ public partial class DesignToken : AbstractDesignToken Color = ColorUtils.FromRgbF(0.07, 0, 0, 0) } }); - + BoxShadowsTertiary = new BoxShadows(new BoxShadow { OffsetX = 0,