Merge pull request #187 from irihitech/combo

Multi ComboBox
This commit is contained in:
Dong Bin
2024-03-27 16:27:08 +08:00
committed by GitHub
11 changed files with 686 additions and 2 deletions

View File

@@ -0,0 +1,190 @@
using System.Collections;
using System.Collections.Specialized;
using Avalonia;
using Avalonia.Collections;
using Avalonia.Controls;
using Avalonia.Controls.Metadata;
using Avalonia.Controls.Primitives;
using Avalonia.Controls.Templates;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.LogicalTree;
using Irihi.Avalonia.Shared.Helpers;
using Irihi.Avalonia.Shared.Contracts;
namespace Ursa.Controls;
[TemplatePart(PART_BackgroundBorder, typeof(Border))]
[PseudoClasses(PC_DropDownOpen, PC_Empty)]
public class MultiComboBox: SelectingItemsControl, IInnerContentControl
{
public const string PART_BackgroundBorder = "PART_BackgroundBorder";
public const string PC_DropDownOpen = ":dropdownopen";
public const string PC_Empty = ":selection-empty";
private Border? _rootBorder;
private static ITemplate<Panel?> _defaultPanel = new FuncTemplate<Panel?>(() => new VirtualizingStackPanel());
public static readonly StyledProperty<bool> IsDropDownOpenProperty =
ComboBox.IsDropDownOpenProperty.AddOwner<MultiComboBox>();
public bool IsDropDownOpen
{
get => GetValue(IsDropDownOpenProperty);
set => SetValue(IsDropDownOpenProperty, value);
}
public static readonly StyledProperty<double> MaxDropdownHeightProperty = AvaloniaProperty.Register<MultiComboBox, double>(
nameof(MaxDropdownHeight));
public double MaxDropdownHeight
{
get => GetValue(MaxDropdownHeightProperty);
set => SetValue(MaxDropdownHeightProperty, value);
}
public static readonly StyledProperty<double> MaxSelectionBoxHeightProperty = AvaloniaProperty.Register<MultiComboBox, double>(
nameof(MaxSelectionBoxHeight));
public double MaxSelectionBoxHeight
{
get => GetValue(MaxSelectionBoxHeightProperty);
set => SetValue(MaxSelectionBoxHeightProperty, value);
}
public new static readonly StyledProperty<IList?> SelectedItemsProperty = AvaloniaProperty.Register<MultiComboBox, IList?>(
nameof(SelectedItems), new AvaloniaList<object>());
public new IList? SelectedItems
{
get => GetValue(SelectedItemsProperty);
set => SetValue(SelectedItemsProperty, value);
}
public static readonly StyledProperty<object?> InnerLeftContentProperty = AvaloniaProperty.Register<MultiComboBox, object?>(
nameof(InnerLeftContent));
public object? InnerLeftContent
{
get => GetValue(InnerLeftContentProperty);
set => SetValue(InnerLeftContentProperty, value);
}
public static readonly StyledProperty<object?> InnerRightContentProperty = AvaloniaProperty.Register<MultiComboBox, object?>(
nameof(InnerRightContent));
public object? InnerRightContent
{
get => GetValue(InnerRightContentProperty);
set => SetValue(InnerRightContentProperty, value);
}
static MultiComboBox()
{
FocusableProperty.OverrideDefaultValue<MultiComboBox>(true);
ItemsPanelProperty.OverrideDefaultValue<MultiComboBox>(_defaultPanel);
IsDropDownOpenProperty.AffectsPseudoClass<MultiComboBox>(PC_DropDownOpen);
SelectedItemsProperty.Changed.AddClassHandler<MultiComboBox, IList?>((box, args) => box.OnSelectedItemsChanged(args));
}
public MultiComboBox()
{
if (SelectedItems is INotifyCollectionChanged c)
{
c.CollectionChanged+=OnSelectedItemsCollectionChanged;
}
}
private void OnSelectedItemsChanged(AvaloniaPropertyChangedEventArgs<IList?> args)
{
if (args.OldValue.Value is INotifyCollectionChanged old)
{
old.CollectionChanged-=OnSelectedItemsCollectionChanged;
}
if (args.NewValue.Value is INotifyCollectionChanged @new)
{
@new.CollectionChanged += OnSelectedItemsCollectionChanged;
}
}
private void OnSelectedItemsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
PseudoClasses.Set(PC_Empty, SelectedItems?.Count == 0);
}
protected override bool NeedsContainerOverride(object? item, int index, out object? recycleKey)
{
recycleKey = item;
return item is not MultiComboBoxItem;
}
protected override Control CreateContainerForItemOverride(object? item, int index, object? recycleKey)
{
return new MultiComboBoxItem();
}
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{
base.OnApplyTemplate(e);
PointerPressedEvent.RemoveHandler(OnBackgroundPointerPressed, _rootBorder);
_rootBorder = e.NameScope.Find<Border>(PART_BackgroundBorder);
PointerPressedEvent.AddHandler(OnBackgroundPointerPressed, _rootBorder);
PseudoClasses.Set(PC_Empty, SelectedItems?.Count == 0);
}
private void OnBackgroundPointerPressed(object sender, PointerPressedEventArgs e)
{
SetCurrentValue(IsDropDownOpenProperty, !IsDropDownOpen);
}
internal void ItemFocused(MultiComboBoxItem dropDownItem)
{
if (IsDropDownOpen && dropDownItem.IsFocused && dropDownItem.IsArrangeValid)
{
dropDownItem.BringIntoView();
}
}
public void Remove(object? o)
{
if (o is StyledElement s)
{
var data = s.DataContext;
this.SelectedItems?.Remove(data);
var item = this.Items.FirstOrDefault(a => ReferenceEquals(a, data));
if (item is not null)
{
var container = ContainerFromItem(item);
if (container is MultiComboBoxItem t)
{
t.IsSelected = false;
}
}
}
}
public void Clear()
{
// this.SelectedItems?.Clear();
var containers = Presenter?.Panel?.Children;
if(containers is null) return;
foreach (var container in containers)
{
if (container is MultiComboBoxItem t)
{
t.IsSelected = false;
}
}
}
protected override void OnUnloaded(RoutedEventArgs e)
{
base.OnUnloaded(e);
if (SelectedItems is INotifyCollectionChanged c)
{
c.CollectionChanged-=OnSelectedItemsCollectionChanged;
}
}
}

View File

@@ -0,0 +1,97 @@
using System.Windows.Input;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Mixins;
using Avalonia.Input;
using Avalonia.LogicalTree;
using Irihi.Avalonia.Shared.Helpers;
namespace Ursa.Controls;
public class MultiComboBoxItem: ContentControl
{
private MultiComboBox? _parent;
private static readonly Point s_invalidPoint = new (double.NaN, double.NaN);
private Point _pointerDownPoint = s_invalidPoint;
public static readonly StyledProperty<bool> IsSelectedProperty = AvaloniaProperty.Register<MultiComboBoxItem, bool>(
nameof(IsSelected));
public bool IsSelected
{
get => GetValue(IsSelectedProperty);
set => SetValue(IsSelectedProperty, value);
}
static MultiComboBoxItem()
{
IsSelectedProperty.AffectsPseudoClass<MultiComboBoxItem>(":selected");
PressedMixin.Attach<MultiComboBoxItem>();
FocusableProperty.OverrideDefaultValue<MultiComboBoxItem>(true);
IsSelectedProperty.Changed.AddClassHandler<MultiComboBoxItem, bool>((item, args) =>
item.OnSelectionChanged(args));
}
private void OnSelectionChanged(AvaloniaPropertyChangedEventArgs<bool> args)
{
var parent = this.FindLogicalAncestorOfType<MultiComboBox>();
if (args.NewValue.Value)
{
parent?.SelectedItems?.Add(this.DataContext);
}
else
{
parent?.SelectedItems?.Remove(this.DataContext);
}
}
protected override void OnAttachedToLogicalTree(LogicalTreeAttachmentEventArgs e)
{
base.OnAttachedToLogicalTree(e);
_parent = this.FindLogicalAncestorOfType<MultiComboBox>();
if(this.IsSelected)
_parent?.SelectedItems?.Add(this.DataContext);
}
protected override void OnPointerPressed(PointerPressedEventArgs e)
{
base.OnPointerPressed(e);
_pointerDownPoint = e.GetPosition(this);
if (e.Handled)
{
return;
}
if (e.GetCurrentPoint(this).Properties.IsLeftButtonPressed)
{
var p = e.GetCurrentPoint(this);
if (p.Properties.PointerUpdateKind is PointerUpdateKind.LeftButtonPressed
or PointerUpdateKind.RightButtonPressed)
{
if (p.Pointer.Type == PointerType.Mouse)
{
this.IsSelected = !this.IsSelected;
e.Handled = true;
}
else
{
_pointerDownPoint = p.Position;
}
}
}
}
protected override void OnPointerReleased(PointerReleasedEventArgs e)
{
base.OnPointerReleased(e);
if (!e.Handled && !double.IsNaN(_pointerDownPoint.X) &&
e.InitialPressMouseButton is MouseButton.Left or MouseButton.Right)
{
var point = e.GetCurrentPoint(this);
if (new Rect(Bounds.Size).ContainsExclusive(point.Position))
{
this.IsSelected = !this.IsSelected;
e.Handled = true;
}
}
}
}

View File

@@ -0,0 +1,36 @@
using System.Windows.Input;
using Avalonia;
using Avalonia.Controls;
namespace Ursa.Controls;
public class MultiComboBoxSelectedItemList: ItemsControl
{
public static readonly StyledProperty<ICommand?> RemoveCommandProperty = AvaloniaProperty.Register<MultiComboBoxSelectedItemList, ICommand?>(
nameof(RemoveCommand));
public ICommand? RemoveCommand
{
get => GetValue(RemoveCommandProperty);
set => SetValue(RemoveCommandProperty, value);
}
protected override bool NeedsContainerOverride(object? item, int index, out object? recycleKey)
{
return NeedsContainer<ClosableTag>(item, out recycleKey);
}
protected override Control CreateContainerForItemOverride(object? item, int index, object? recycleKey)
{
return new ClosableTag();
}
protected override void PrepareContainerForItemOverride(Control container, object? item, int index)
{
base.PrepareContainerForItemOverride(container, item, index);
if (container is ClosableTag tag)
{
tag.Command = RemoveCommand;
}
}
}