From 11485c4490bfb1084e7ad0ac563f3e9e8d73bc0f Mon Sep 17 00:00:00 2001 From: polarboy Date: Sat, 24 Aug 2024 11:43:24 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=E6=8C=89=E9=92=AE=EF=BC=8C?= =?UTF-8?q?=E7=BB=99=E5=8F=B3=E4=BE=A7=E9=A2=9D=E5=A4=96=E5=8C=BA=E5=9F=9F?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=A4=96=E8=BE=B9=E8=B7=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 重构按钮,给右侧额外区域增加外边距 --- .../ShowCase/DropdownButtonShowCase.axaml | 50 +++++++++++++++++++ .../Buttons/BaseButtonTheme.cs | 26 ++++++++-- src/AtomUI.Controls/Buttons/Button.cs | 11 ++-- src/AtomUI.Controls/Buttons/ButtonToken.cs | 19 +++++++ .../DropdownButton/DropdownButton.cs | 34 +++++++++++-- .../TokenResourceConst.g.cs | 3 ++ 6 files changed, 130 insertions(+), 13 deletions(-) diff --git a/samples/AtomUI.Demo.Desktop/ShowCase/DropdownButtonShowCase.axaml b/samples/AtomUI.Demo.Desktop/ShowCase/DropdownButtonShowCase.axaml index 39a9a881c..78f19f5ce 100644 --- a/samples/AtomUI.Demo.Desktop/ShowCase/DropdownButtonShowCase.axaml +++ b/samples/AtomUI.Demo.Desktop/ShowCase/DropdownButtonShowCase.axaml @@ -27,5 +27,55 @@ + + + + + Edit File + + + + + + + + + + + Edit File + + + + + + + + + + + Edit File + + + + + + + + + + + Edit File + + + + + + + + + + \ No newline at end of file diff --git a/src/AtomUI.Controls/Buttons/BaseButtonTheme.cs b/src/AtomUI.Controls/Buttons/BaseButtonTheme.cs index 03755ecb7..dbd812aea 100644 --- a/src/AtomUI.Controls/Buttons/BaseButtonTheme.cs +++ b/src/AtomUI.Controls/Buttons/BaseButtonTheme.cs @@ -5,6 +5,8 @@ using Avalonia; using Avalonia.Controls; using Avalonia.Controls.Presenters; using Avalonia.Controls.Templates; +using Avalonia.Data; +using Avalonia.Data.Converters; using Avalonia.Input; using Avalonia.Layout; using Avalonia.Styling; @@ -76,14 +78,17 @@ internal abstract class BaseButtonTheme : BaseControlTheme HorizontalContentAlignment = HorizontalAlignment.Center, VerticalContentAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Center, - VerticalAlignment = VerticalAlignment.Center + VerticalAlignment = VerticalAlignment.Center, }; - + + CreateTemplateParentBinding(extraContentPresenter, ContentPresenter.IsVisibleProperty, Button.ExtraContentProperty, + BindingMode.Default, + ObjectConverters.IsNotNull); CreateTemplateParentBinding(extraContentPresenter, ContentPresenter.ContentProperty, Button.ExtraContentProperty); DockPanel.SetDock(extraContentPresenter, Dock.Right); - var rootLayout = new DockPanel() + var rootLayout = new DockPanel { Name = RootLayoutPart, LastChildFill = true @@ -121,6 +126,11 @@ internal abstract class BaseButtonTheme : BaseControlTheme iconOnlyStyle.Add(Button.PaddingProperty, ButtonTokenResourceKey.IconOnyPaddingLG); largeSizeStyle.Add(iconOnlyStyle); } + { + var extraContentStyle = new Style(selector => selector.Nesting().Template().Name(RightExtraContentPart)); + largeSizeStyle.Add(ContentPresenter.MarginProperty, ButtonTokenResourceKey.ExtraContentMarginLG); + largeSizeStyle.Add(extraContentStyle); + } Add(largeSizeStyle); var middleSizeStyle = new Style(selector => selector.Nesting().PropertyEquals(Button.SizeTypeProperty, SizeType.Middle)); @@ -133,6 +143,11 @@ internal abstract class BaseButtonTheme : BaseControlTheme iconOnlyStyle.Add(Button.PaddingProperty, ButtonTokenResourceKey.IconOnyPadding); middleSizeStyle.Add(iconOnlyStyle); } + { + var extraContentStyle = new Style(selector => selector.Nesting().Template().Name(RightExtraContentPart)); + extraContentStyle.Add(ContentPresenter.MarginProperty, ButtonTokenResourceKey.ExtraContentMargin); + middleSizeStyle.Add(extraContentStyle); + } Add(middleSizeStyle); var smallSizeStyle = new Style(selector => selector.Nesting().PropertyEquals(Button.SizeTypeProperty, SizeType.Small)); @@ -145,6 +160,11 @@ internal abstract class BaseButtonTheme : BaseControlTheme iconOnlyStyle.Add(Button.PaddingProperty, ButtonTokenResourceKey.IconOnyPaddingSM); smallSizeStyle.Add(iconOnlyStyle); } + { + var extraContentStyle = new Style(selector => selector.Nesting().Template().Name(RightExtraContentPart)); + extraContentStyle.Add(ContentPresenter.MarginProperty, ButtonTokenResourceKey.ExtraContentMarginSM); + smallSizeStyle.Add(extraContentStyle); + } Add(smallSizeStyle); } diff --git a/src/AtomUI.Controls/Buttons/Button.cs b/src/AtomUI.Controls/Buttons/Button.cs index 162d1feda..5307efed8 100644 --- a/src/AtomUI.Controls/Buttons/Button.cs +++ b/src/AtomUI.Controls/Buttons/Button.cs @@ -503,12 +503,13 @@ public class Button : AvaloniaButton, normalFilledBrushKey = GlobalTokenResourceKey.ColorPrimary; selectedFilledBrushKey = GlobalTokenResourceKey.ColorPrimaryActive; activeFilledBrushKey = GlobalTokenResourceKey.ColorPrimaryHover; + if (IsDanger) { + normalFilledBrushKey = GlobalTokenResourceKey.ColorError; + selectedFilledBrushKey = GlobalTokenResourceKey.ColorErrorActive; + activeFilledBrushKey = GlobalTokenResourceKey.ColorErrorBorderHover; + } } - if (IsDanger) { - normalFilledBrushKey = GlobalTokenResourceKey.ColorError; - selectedFilledBrushKey = GlobalTokenResourceKey.ColorErrorActive; - activeFilledBrushKey = GlobalTokenResourceKey.ColorErrorBorderHover; - } + } else if (ButtonType == ButtonType.Text) { normalFilledBrushKey = ButtonTokenResourceKey.DefaultColor; selectedFilledBrushKey = ButtonTokenResourceKey.DefaultColor; diff --git a/src/AtomUI.Controls/Buttons/ButtonToken.cs b/src/AtomUI.Controls/Buttons/ButtonToken.cs index 53cab383d..a0b27d934 100644 --- a/src/AtomUI.Controls/Buttons/ButtonToken.cs +++ b/src/AtomUI.Controls/Buttons/ButtonToken.cs @@ -209,6 +209,21 @@ internal class ButtonToken : AbstractControlDesignToken /// 小号按钮内容字体行高 /// public double ContentLineHeightSM { get; set; } = double.NaN; + + /// + /// 按钮右边一个额外的区域对右侧的小号外边距 + /// + public Thickness ExtraContentMarginSM { get; set; } + + /// + /// 按钮右边一个额外的区域对右侧的外边距 + /// + public Thickness ExtraContentMargin { get; set; } + + /// + /// 按钮右边一个额外的区域对右侧的大号外边距 + /// + public Thickness ExtraContentMarginLG { get; set; } #region 内部 Token 定义 @@ -318,6 +333,10 @@ internal class ButtonToken : AbstractControlDesignToken Math.Max((controlHeight - ContentLineHeight) / 2 - lineWidth, 0)); PaddingLG = new Thickness(_globalToken.PaddingContentHorizontal - lineWidth, Math.Max((controlHeightLG - ContentLineHeightLG) / 2 - lineWidth, 0)); + + ExtraContentMarginSM = new Thickness(0, 0, 8 - _globalToken.SeedToken.LineWidth, 0); + ExtraContentMargin = new Thickness(0, 0, _globalToken.PaddingContentHorizontal - lineWidth, 0); + ExtraContentMarginLG = new Thickness(0, 0, _globalToken.PaddingContentHorizontal - lineWidth, 0); CirclePadding = new Thickness(PaddingSM.Left / 2); OnlyIconSizeSM = _globalToken.IconSize; diff --git a/src/AtomUI.Controls/DropdownButton/DropdownButton.cs b/src/AtomUI.Controls/DropdownButton/DropdownButton.cs index b048402dc..8faaa1641 100644 --- a/src/AtomUI.Controls/DropdownButton/DropdownButton.cs +++ b/src/AtomUI.Controls/DropdownButton/DropdownButton.cs @@ -48,6 +48,9 @@ public class DropdownButton : Button public static readonly StyledProperty MouseLeaveDelayProperty = AvaloniaProperty.Register(nameof(MouseLeaveDelay), 100); + + public static readonly StyledProperty IsShowIndicatorProperty = + AvaloniaProperty.Register(nameof(IsShowIndicator), true); public MenuFlyout? DropdownFlyout { @@ -108,6 +111,12 @@ public class DropdownButton : Button get => GetValue(MouseLeaveDelayProperty); set => SetValue(MouseLeaveDelayProperty, value); } + + public bool IsShowIndicator + { + get => GetValue(IsShowIndicatorProperty); + set => SetValue(IsShowIndicatorProperty, value); + } #endregion @@ -131,11 +140,26 @@ public class DropdownButton : Button }; BindUtils.RelayBind(this, IconSizeProperty, _openIndicatorIcon, PathIcon.WidthProperty); BindUtils.RelayBind(this, IconSizeProperty, _openIndicatorIcon, PathIcon.HeightProperty); - ExtraContent = _openIndicatorIcon; - base.OnApplyTemplate(e); TokenResourceBinder.CreateGlobalTokenBinding(this, MarginToAnchorProperty, GlobalTokenResourceKey.MarginXXS); SetupFlyoutProperties(); + if (IsShowIndicator) { + ExtraContent = _openIndicatorIcon; + } + } + + protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs e) + { + base.OnPropertyChanged(e); + if (VisualRoot is not null) { + if (e.Property == IsShowIndicatorProperty) { + if (IsShowIndicator) { + ExtraContent = _openIndicatorIcon; + } else { + ExtraContent = null; + } + } + } } protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e) @@ -346,9 +370,9 @@ public class DropdownButton : Button } protected override void NotifyIconBrushCalculated(in TokenResourceKey normalFilledBrushKey, - in TokenResourceKey selectedFilledBrushKey, - in TokenResourceKey activeFilledBrushKey, - in TokenResourceKey disabledFilledBrushKey) + in TokenResourceKey selectedFilledBrushKey, + in TokenResourceKey activeFilledBrushKey, + in TokenResourceKey disabledFilledBrushKey) { if (_openIndicatorIcon is not null) { TokenResourceBinder.CreateGlobalTokenBinding(_openIndicatorIcon, PathIcon.NormalFilledBrushProperty, normalFilledBrushKey); diff --git a/src/AtomUI.Controls/GeneratedFiles/AtomUI.Generator/AtomUI.Generator.TokenResourceKeyGenerator/TokenResourceConst.g.cs b/src/AtomUI.Controls/GeneratedFiles/AtomUI.Generator/AtomUI.Generator.TokenResourceKeyGenerator/TokenResourceConst.g.cs index 72aa7b6f7..580b0abb4 100644 --- a/src/AtomUI.Controls/GeneratedFiles/AtomUI.Generator/AtomUI.Generator.TokenResourceKeyGenerator/TokenResourceConst.g.cs +++ b/src/AtomUI.Controls/GeneratedFiles/AtomUI.Generator/AtomUI.Generator.TokenResourceKeyGenerator/TokenResourceConst.g.cs @@ -120,6 +120,9 @@ namespace AtomUI.Theme.Styling public static readonly TokenResourceKey ContentLineHeight = new TokenResourceKey("Button.ContentLineHeight", "AtomUI.Token"); public static readonly TokenResourceKey ContentLineHeightLG = new TokenResourceKey("Button.ContentLineHeightLG", "AtomUI.Token"); public static readonly TokenResourceKey ContentLineHeightSM = new TokenResourceKey("Button.ContentLineHeightSM", "AtomUI.Token"); + public static readonly TokenResourceKey ExtraContentMarginSM = new TokenResourceKey("Button.ExtraContentMarginSM", "AtomUI.Token"); + public static readonly TokenResourceKey ExtraContentMargin = new TokenResourceKey("Button.ExtraContentMargin", "AtomUI.Token"); + public static readonly TokenResourceKey ExtraContentMarginLG = new TokenResourceKey("Button.ExtraContentMarginLG", "AtomUI.Token"); public static readonly TokenResourceKey IconOnyPadding = new TokenResourceKey("Button.IconOnyPadding", "AtomUI.Token"); public static readonly TokenResourceKey IconOnyPaddingLG = new TokenResourceKey("Button.IconOnyPaddingLG", "AtomUI.Token"); public static readonly TokenResourceKey IconOnyPaddingSM = new TokenResourceKey("Button.IconOnyPaddingSM", "AtomUI.Token");