From 42ec7eb42c92cf712a97281df75915b47cedc5bf Mon Sep 17 00:00:00 2001 From: chinboy Date: Tue, 4 Mar 2025 18:14:05 +0800 Subject: [PATCH] Refactor Combobox --- src/AtomUI.Controls/Buttons/DropdownButton.cs | 1 + src/AtomUI.Controls/ComboBox/ComboBox.cs | 24 +------- .../ComboBox/ComboBoxItemTheme.cs | 7 +-- .../ComboBox/ComboBoxSpinnerInnerBox.cs | 1 - src/AtomUI.Controls/ComboBox/ComboBoxTheme.cs | 56 ++++++++++--------- src/AtomUI.Controls/StdPseudoClass.cs | 2 + 6 files changed, 39 insertions(+), 52 deletions(-) diff --git a/src/AtomUI.Controls/Buttons/DropdownButton.cs b/src/AtomUI.Controls/Buttons/DropdownButton.cs index 9d58b440d..fb9a2fb87 100644 --- a/src/AtomUI.Controls/Buttons/DropdownButton.cs +++ b/src/AtomUI.Controls/Buttons/DropdownButton.cs @@ -182,6 +182,7 @@ public class DropdownButton : Button BindUtils.RelayBind(this, TriggerTypeProperty, _flyoutStateHelper, FlyoutStateHelper.TriggerTypeProperty); this.AddTokenBindingDisposable(TokenResourceBinder.CreateTokenBinding(this, MarginToAnchorProperty, SharedTokenKey.MarginXXS)); + ExtraContainerVisible = true; } protected override void OnApplyTemplate(TemplateAppliedEventArgs e) diff --git a/src/AtomUI.Controls/ComboBox/ComboBox.cs b/src/AtomUI.Controls/ComboBox/ComboBox.cs index 16291c455..6aa6e5218 100644 --- a/src/AtomUI.Controls/ComboBox/ComboBox.cs +++ b/src/AtomUI.Controls/ComboBox/ComboBox.cs @@ -14,12 +14,8 @@ using AvaloniaComboBox = Avalonia.Controls.ComboBox; public class ComboBox : AvaloniaComboBox, IAnimationAwareControl, - IControlSharedTokenResourcesHost, - ITokenResourceConsumer + IControlSharedTokenResourcesHost { - public const string ErrorPC = ":error"; - public const string WarningPC = ":warning"; - #region 公共属性定义 public static readonly StyledProperty LeftAddOnProperty = @@ -119,7 +115,6 @@ public class ComboBox : AvaloniaComboBox, Control IAnimationAwareControl.PropertyBindTarget => this; Control IControlSharedTokenResourcesHost.HostControl => this; string IControlSharedTokenResourcesHost.TokenId => ComboBoxToken.ID; - CompositeDisposable? ITokenResourceConsumer.TokenBindingsDisposable => _tokenBindingsDisposable; #endregion @@ -156,7 +151,6 @@ public class ComboBox : AvaloniaComboBox, protected override void OnApplyTemplate(TemplateAppliedEventArgs e) { base.OnApplyTemplate(e); - this.RunThemeTokenBindingActions(); _popup = e.NameScope.Find(ComboBoxTheme.PopupPart); _openIndicatorButton = e.NameScope.Find(ComboBoxTheme.OpenIndicatorButtonPart); _spinnerInnerBox = e.NameScope.Find(ComboBoxTheme.SpinnerInnerBoxPart); @@ -191,19 +185,7 @@ public class ComboBox : AvaloniaComboBox, private void UpdatePseudoClasses() { - PseudoClasses.Set(ErrorPC, Status == AddOnDecoratedStatus.Error); - PseudoClasses.Set(WarningPC, Status == AddOnDecoratedStatus.Warning); - } - - protected override void OnAttachedToLogicalTree(LogicalTreeAttachmentEventArgs e) - { - base.OnAttachedToLogicalTree(e); - _tokenBindingsDisposable = new CompositeDisposable(); - } - - protected override void OnDetachedFromLogicalTree(LogicalTreeAttachmentEventArgs e) - { - base.OnDetachedFromLogicalTree(e); - this.DisposeTokenBindings(); + PseudoClasses.Set(StdPseudoClass.Error, Status == AddOnDecoratedStatus.Error); + PseudoClasses.Set(StdPseudoClass.Warning, Status == AddOnDecoratedStatus.Warning); } } \ No newline at end of file diff --git a/src/AtomUI.Controls/ComboBox/ComboBoxItemTheme.cs b/src/AtomUI.Controls/ComboBox/ComboBoxItemTheme.cs index d3ee230d2..f60b2e5f1 100644 --- a/src/AtomUI.Controls/ComboBox/ComboBoxItemTheme.cs +++ b/src/AtomUI.Controls/ComboBox/ComboBoxItemTheme.cs @@ -2,7 +2,6 @@ using AtomUI.Media; using AtomUI.Theme; using AtomUI.Theme.Styling; -using Avalonia; using Avalonia.Animation; using Avalonia.Controls; using Avalonia.Controls.Presenters; @@ -50,11 +49,11 @@ internal class ComboBoxItemTheme : BaseControlTheme return o; })); CreateTemplateParentBinding(contentPresenter, ContentPresenter.ContentTemplateProperty, - ContentControl.ContentTemplateProperty); + ComboBoxItem.ContentTemplateProperty); CreateTemplateParentBinding(contentPresenter, ContentPresenter.HorizontalContentAlignmentProperty, - ContentControl.HorizontalContentAlignmentProperty); + ComboBoxItem.HorizontalContentAlignmentProperty); CreateTemplateParentBinding(contentPresenter, ContentPresenter.VerticalContentAlignmentProperty, - ContentControl.VerticalContentAlignmentProperty); + ComboBoxItem.VerticalContentAlignmentProperty); return contentPresenter; }); } diff --git a/src/AtomUI.Controls/ComboBox/ComboBoxSpinnerInnerBox.cs b/src/AtomUI.Controls/ComboBox/ComboBoxSpinnerInnerBox.cs index 4c5e80de5..9e879e960 100644 --- a/src/AtomUI.Controls/ComboBox/ComboBoxSpinnerInnerBox.cs +++ b/src/AtomUI.Controls/ComboBox/ComboBoxSpinnerInnerBox.cs @@ -4,7 +4,6 @@ using AtomUI.Theme.Styling; using Avalonia; using Avalonia.Controls.Metadata; using Avalonia.Controls.Presenters; -using Avalonia.Data; using Avalonia.LogicalTree; using Avalonia.Media; using Avalonia.Rendering; diff --git a/src/AtomUI.Controls/ComboBox/ComboBoxTheme.cs b/src/AtomUI.Controls/ComboBox/ComboBoxTheme.cs index 2e04aa85c..06b668e65 100644 --- a/src/AtomUI.Controls/ComboBox/ComboBoxTheme.cs +++ b/src/AtomUI.Controls/ComboBox/ComboBoxTheme.cs @@ -1,4 +1,6 @@ -using AtomUI.IconPkg; +using AtomUI.Controls.Utils; +using AtomUI.Data; +using AtomUI.IconPkg; using AtomUI.IconPkg.AntDesign; using AtomUI.Theme; using AtomUI.Theme.Data; @@ -38,7 +40,6 @@ internal class ComboBoxTheme : BaseControlTheme { return new FuncControlTemplate((comboBox, scope) => { - ResetTokenResourceBindings(comboBox); var panel = new Panel(); var decoratedBox = BuildSpinnerDecoratedBox(comboBox, scope); var innerBox = BuildSpinnerContent(comboBox, scope); @@ -113,28 +114,11 @@ internal class ComboBoxTheme : BaseControlTheme }; openButton.RegisterInNameScope(scope); + openButton.SetTemplatedParent(comboBox); spinnerHandleDecorator.Child = openButton; spinnerInnerBox.SpinnerContent = spinnerHandleDecorator; - RegisterTokenResourceBindings(comboBox, () => - { - comboBox.AddTokenBindingDisposable(TokenResourceBinder.CreateTokenBinding(decreaseButtonIcon, - Icon.ActiveFilledBrushProperty, - ButtonSpinnerTokenKey.HandleHoverColor)); - comboBox.AddTokenBindingDisposable(TokenResourceBinder.CreateTokenBinding(decreaseButtonIcon, - Icon.SelectedFilledBrushProperty, - SharedTokenKey.ColorPrimaryActive)); - comboBox.AddTokenBindingDisposable(TokenResourceBinder.CreateTokenBinding(openButton, Layoutable.WidthProperty, - ComboBoxTokenKey.OpenIndicatorWidth)); - comboBox.AddTokenBindingDisposable(TokenResourceBinder.CreateTokenBinding(openButton, - IconButton.IconWidthProperty, - SharedTokenKey.IconSizeSM)); - comboBox.AddTokenBindingDisposable(TokenResourceBinder.CreateTokenBinding(openButton, - IconButton.IconHeightProperty, - SharedTokenKey.IconSizeSM)); - }); - return spinnerInnerBox; } @@ -149,6 +133,7 @@ internal class ComboBoxTheme : BaseControlTheme TextTrimming = TextTrimming.CharacterEllipsis, Opacity = 0.3 }; + placeholder.SetTemplatedParent(comboBox); CreateTemplateParentBinding(placeholder, Visual.IsVisibleProperty, SelectingItemsControl.SelectedItemProperty, BindingMode.Default, @@ -161,13 +146,14 @@ internal class ComboBoxTheme : BaseControlTheme { Name = SelectedContentPresenterPart, HorizontalAlignment = HorizontalAlignment.Left, - VerticalAlignment = VerticalAlignment.Center + VerticalAlignment = VerticalAlignment.Center, }; - + + contentPresenter.SetTemplatedParent(comboBox); CreateTemplateParentBinding(contentPresenter, ContentPresenter.ContentProperty, - Avalonia.Controls.ComboBox.SelectionBoxItemProperty); + ComboBox.SelectionBoxItemProperty); CreateTemplateParentBinding(contentPresenter, ContentPresenter.ContentTemplateProperty, - ItemsControl.ItemTemplateProperty); + ComboBox.ItemTemplateProperty); contentLayout.Children.Add(contentPresenter); @@ -211,6 +197,14 @@ internal class ComboBoxTheme : BaseControlTheme } protected override void BuildStyles() + { + BuildCommonStyle(); + BuildStatusStyle(); + BuildPopupStyle(); + BuildOpenIndicatorStyle(); + } + + private void BuildCommonStyle() { var commonStyle = new Style(selector => selector.Nesting()); var largeStyle = @@ -245,9 +239,19 @@ internal class ComboBoxTheme : BaseControlTheme smallStyle.Add(spinnerInnerBox); } commonStyle.Add(smallStyle); - BuildStatusStyle(); Add(commonStyle); - BuildPopupStyle(); + } + + private void BuildOpenIndicatorStyle() + { + var openIndicatorButton = new Style(selector => selector.Nesting().Template().Name(OpenIndicatorButtonPart)); + openIndicatorButton.Add(IconButton.ActiveIconColorProperty, ButtonSpinnerTokenKey.HandleHoverColor); + openIndicatorButton.Add(IconButton.SelectedIconColorProperty, SharedTokenKey.ColorPrimaryActive); + openIndicatorButton.Add(IconButton.WidthProperty, ComboBoxTokenKey.OpenIndicatorWidth); + openIndicatorButton.Add(IconButton.IconWidthProperty, SharedTokenKey.IconSizeSM); + openIndicatorButton.Add(IconButton.IconHeightProperty, SharedTokenKey.IconSizeSM); + + Add(openIndicatorButton); } private void BuildPopupStyle() diff --git a/src/AtomUI.Controls/StdPseudoClass.cs b/src/AtomUI.Controls/StdPseudoClass.cs index ad56960cc..2ad887a3d 100644 --- a/src/AtomUI.Controls/StdPseudoClass.cs +++ b/src/AtomUI.Controls/StdPseudoClass.cs @@ -19,4 +19,6 @@ public static class StdPseudoClass public const string Horizontal = ":horizontal"; public const string Vertical = ":vertical"; public const string InActive = ":inactive"; + public const string Error = ":error"; + public const string Warning = ":warning"; } \ No newline at end of file