mirror of
https://github.com/AtomUI/AtomUI.git
synced 2026-10-29 07:16:33 +08:00
Refactor Combobox
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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<object?> 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<Popup>(ComboBoxTheme.PopupPart);
|
||||
_openIndicatorButton = e.NameScope.Find<IconButton>(ComboBoxTheme.OpenIndicatorButtonPart);
|
||||
_spinnerInnerBox = e.NameScope.Find<ComboBoxSpinnerInnerBox>(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);
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
});
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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>((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()
|
||||
|
||||
@@ -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";
|
||||
}
|
||||
Reference in New Issue
Block a user