feat: deal with virtualization.

This commit is contained in:
rabbitism
2024-03-25 11:10:11 +08:00
parent b15d237056
commit e600937fd7
3 changed files with 228 additions and 41 deletions

View File

@@ -1,32 +1,166 @@
<ResourceDictionary xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:u="https://irihi.tech/ursa">
<!-- Add Resources Here -->
<ControlTheme TargetType="u:MultiComboBox" x:Key="{x:Type u:MultiComboBox}">
<Setter Property="Focusable" Value="True"></Setter>
<ResourceDictionary
xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:u="https://irihi.tech/ursa">
<!-- Add Resources Here -->
<ControlTheme x:Key="{x:Type u:MultiComboBox}" TargetType="u:MultiComboBox">
<Setter Property="Focusable" Value="True" />
<Setter Property="MaxDropdownHeight" Value="300" />
<Setter Property="Template">
<ControlTemplate TargetType="u:MultiComboBox">
<Panel>
<ToggleButton Name="button">
<ToggleButton Name="button" IsChecked="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsDropDownOpen, Mode=TwoWay}">
<ItemsControl ItemsSource="{TemplateBinding SelectedItems}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel Orientation="Horizontal"/>
<WrapPanel Orientation="Horizontal" />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
</ToggleButton>
<Popup PlacementTarget="button" IsOpen="{Binding #button.IsChecked}">
<ListBox Name="{x:Static u:MultiComboBox.PART_ListBox}" SelectionMode="Multiple" ItemsSource="{TemplateBinding ItemsSource}" SelectedItems="{TemplateBinding SelectedItems}">
</ListBox>
<Popup
MaxHeight="{TemplateBinding MaxDropdownHeight}"
IsOpen="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=IsDropDownOpen, Mode=TwoWay}"
IsLightDismissEnabled="True"
PlacementTarget="button">
<Border Theme="{DynamicResource CardBorder}">
<ScrollViewer>
<ItemsPresenter ItemsPanel="{TemplateBinding ItemsPanel}" />
</ScrollViewer>
</Border>
</Popup>
</Panel>
</ControlTemplate>
</Setter>
</ControlTheme>
<ControlTheme TargetType="u:MultiComboBoxItem" x:Key="{x:Type u:MultiComboBoxItem}">
<Setter Property="Focusable" Value="True"></Setter>
<ControlTheme x:Key="{x:Type u:MultiComboBoxItem}" TargetType="u:MultiComboBoxItem">
<Setter Property="Padding" Value="8,0,0,0" />
<Setter Property="HorizontalAlignment" Value="Left" />
<Setter Property="VerticalAlignment" Value="Top" />
<Setter Property="Cursor" Value="Hand" />
<Setter Property="HorizontalContentAlignment" Value="Left" />
<Setter Property="VerticalContentAlignment" Value="Center" />
<Setter Property="FontSize" Value="{DynamicResource ListBoxItemCheckFontSize}" />
<Setter Property="CornerRadius" Value="{DynamicResource ListBoxItemCheckBoxCornerRadius}" />
<Setter Property="MinHeight" Value="32" />
<Setter Property="Foreground" Value="{DynamicResource ListBoxItemCheckForeground}" />
<Setter Property="Background" Value="{DynamicResource ListBoxItemCheckDefaultBackground}" />
<Setter Property="BorderBrush" Value="{DynamicResource ListBoxItemCheckDefaultBorderBrush}" />
<Setter Property="Template">
<ControlTemplate TargetType="u:MultiComboBoxItem">
<Border
x:Name="RootBorder"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
CornerRadius="{TemplateBinding CornerRadius}">
<Grid x:Name="RootGrid" ColumnDefinitions="Auto,*">
<Grid
Grid.Column="0"
Margin="0,0,0,0"
VerticalAlignment="Center">
<Border
x:Name="NormalRectangle"
Width="{DynamicResource ListBoxItemCheckBoxWidth}"
Height="{DynamicResource ListBoxItemCheckBoxHeight}"
Background="{DynamicResource ListBoxItemCheckDefaultBackground}"
BorderBrush="{DynamicResource ListBoxItemCheckDefaultBorderBrush}"
BorderThickness="{DynamicResource ListBoxItemCheckBoxBorderThickness}"
CornerRadius="{TemplateBinding CornerRadius}"
UseLayoutRounding="False" />
<PathIcon
Name="CheckGlyph"
Width="{DynamicResource ListBoxItemCheckBoxGlyphWidth}"
Height="{DynamicResource ListBoxItemCheckBoxGlyphHeight}"
Foreground="{DynamicResource ListBoxItemCheckGlyphFill}" />
</Grid>
<ContentPresenter
x:Name="ContentPresenter"
Grid.Column="1"
Margin="{TemplateBinding Padding}"
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="Center"
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}"
IsVisible="{TemplateBinding Content,Converter={x:Static ObjectConverters.IsNotNull}}"
RecognizesAccessKey="True"
TextWrapping="Wrap" />
</Grid>
</Border>
</ControlTemplate>
</Setter>
<!-- Unchecked Pointerover State -->
<Style Selector="^:pointerover">
<Style Selector="^ /template/ Border#NormalRectangle">
<Setter Property="BorderBrush" Value="{DynamicResource ListBoxItemCheckPointeroverBorderBrush}" />
<Setter Property="Background" Value="{DynamicResource ListBoxItemCheckPointeroverBackground}" />
</Style>
</Style>
<!-- Unchecked Pressed State -->
<Style Selector="^:pressed">
<Style Selector="^ /template/ Border#NormalRectangle">
<Setter Property="BorderBrush" Value="{DynamicResource ListBoxItemCheckPressedBorderBrush}" />
<Setter Property="Background" Value="{DynamicResource ListBoxItemCheckPressedBackground}" />
</Style>
</Style>
<!-- Unchecked Disabled state -->
<Style Selector="^:disabled">
<Style Selector="^ /template/ ContentPresenter#ContentPresenter">
<Setter Property="Foreground" Value="{DynamicResource ListBoxItemCheckDisabledForeground}" />
</Style>
<Style Selector="^ /template/ Border#NormalRectangle">
<Setter Property="BorderBrush" Value="{DynamicResource ListBoxItemCheckDefaultDisabledBorderBrush}" />
<Setter Property="Background" Value="{DynamicResource ListBoxItemCheckDefaultDisabledBackground}" />
</Style>
</Style>
<Style Selector="^:selected">
<Style Selector="^ /template/ Border#NormalRectangle">
<Setter Property="BorderBrush" Value="{DynamicResource ListBoxItemCheckCheckedDefaultBorderBrush}" />
<Setter Property="Background" Value="{DynamicResource ListBoxItemCheckCheckedDefaultBackground}" />
</Style>
<Style Selector="^ /template/ PathIcon#CheckGlyph">
<Setter Property="Data" Value="{DynamicResource ListBoxItemCheckCheckGlyph}" />
<Setter Property="Opacity" Value="1" />
</Style>
<!-- Checked Pointerover State -->
<Style Selector="^:pointerover">
<Style Selector="^ /template/ Border#NormalRectangle">
<Setter Property="BorderBrush"
Value="{DynamicResource ListBoxItemCheckCheckedPointeroverBorderBrush}" />
<Setter Property="Background"
Value="{DynamicResource ListBoxItemCheckCheckedPointeroverBackground}" />
</Style>
</Style>
<!-- Checked Pressed State -->
<Style Selector="^:pressed">
<Style Selector="^ /template/ Border#NormalRectangle">
<Setter Property="BorderBrush" Value="{DynamicResource ListBoxItemCheckCheckedPressedBorderBrush}" />
<Setter Property="Background" Value="{DynamicResource ListBoxItemCheckCheckedPressedBackground}" />
</Style>
</Style>
<!-- Checked Disabled State -->
<Style Selector="^:disabled">
<Style Selector="^ /template/ ContentPresenter#ContentPresenter">
<Setter Property="Foreground" Value="{DynamicResource ListBoxItemCheckDisabledForeground}" />
</Style>
<Style Selector="^ /template/ Border#NormalRectangle">
<Setter Property="BorderBrush" Value="{DynamicResource ListBoxItemCheckCheckedDisabledBorderBrush}" />
<Setter Property="Background" Value="{DynamicResource ListBoxItemCheckCheckedDisabledBackground}" />
</Style>
<Style Selector="^ /template/ Path#CheckGlyph">
<Setter Property="Fill" Value="{DynamicResource ListBoxItemCheckGlyphDisabledFill}" />
</Style>
</Style>
</Style>
</ControlTheme>
</ResourceDictionary>

View File

@@ -2,19 +2,17 @@
using System.Collections.ObjectModel;
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 Irihi.Avalonia.Shared.Helpers;
namespace Ursa.Controls;
[TemplatePart( PART_ListBox, typeof(ListBox))]
public class MultiComboBox: SelectingItemsControl
{
public const string PART_ListBox = "PART_ListBox";
private ListBox? _listBox;
private static ITemplate<Panel?> _defaultPanel = new FuncTemplate<Panel?>(() => new VirtualizingStackPanel());
public static readonly StyledProperty<bool> IsDropDownOpenProperty =
@@ -26,8 +24,17 @@ public class MultiComboBox: SelectingItemsControl
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 new static readonly StyledProperty<IList?> SelectedItemsProperty = AvaloniaProperty.Register<MultiComboBox, IList?>(
nameof(SelectedItems));
nameof(SelectedItems), new AvaloniaList<object>());
public new IList? SelectedItems
{
@@ -43,14 +50,46 @@ public class MultiComboBox: SelectingItemsControl
protected override bool NeedsContainerOverride(object? item, int index, out object? recycleKey)
{
return NeedsContainer<MultiComboBoxItem>(item, out recycleKey);
recycleKey = item;
return item is not MultiComboBoxItem;
}
protected override Control CreateContainerForItemOverride(object? item, int index, object? recycleKey)
{
return new MultiComboBoxItem();
}
private Dictionary<int, IDisposable?> _disposables = new Dictionary<int, IDisposable?>();
protected override void PrepareContainerForItemOverride(Control container, object? item, int index)
{
base.PrepareContainerForItemOverride(container, item, index);
if(_disposables.TryGetValue(index, out var d))
{
d?.Dispose();
_disposables.Remove(index);
}
if (container is MultiComboBoxItem comboBoxItem)
{
comboBoxItem.IsSelected = SelectedItems?.Contains(item) ?? false;
var disposable = MultiComboBoxItem.IsSelectedProperty.Changed.Subscribe(a =>
{
if (a.Sender == comboBoxItem)
{
if (comboBoxItem.IsSelected)
{
SelectedItems?.Add(item);
}
else
{
SelectedItems?.Remove(item);
}
}
});
_disposables[index] = disposable;
}
}
internal void ItemFocused(MultiComboBoxItem dropDownItem)
{
if (IsDropDownOpen && dropDownItem.IsFocused && dropDownItem.IsArrangeValid)
@@ -58,22 +97,5 @@ public class MultiComboBox: SelectingItemsControl
dropDownItem.BringIntoView();
}
}
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{
base.OnApplyTemplate(e);
_listBox = e.NameScope.Find<ListBox>(PART_ListBox);
if (_listBox != null)
{
_listBox.SelectionChanged += ListBox_SelectionChanged;
}
}
private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (sender == _listBox)
{
this.SelectedItems = _listBox.SelectedItems;
}
}
}

View File

@@ -1,11 +1,28 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Mixins;
using Avalonia.Input;
using Irihi.Avalonia.Shared.Helpers;
namespace Ursa.Controls;
public class MultiComboBoxItem: ListBoxItem
public class MultiComboBoxItem: ContentControl
{
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);
}
public MultiComboBoxItem()
{
this.GetObservable(IsFocusedProperty).Subscribe(a=> {
@@ -15,4 +32,18 @@ public class MultiComboBoxItem: ListBoxItem
}
});
}
protected override void OnPointerPressed(PointerPressedEventArgs e)
{
base.OnPointerPressed(e);
if (e.Handled)
{
return;
}
if (e.GetCurrentPoint(this).Properties.IsLeftButtonPressed)
{
this.IsSelected = !this.IsSelected;
e.Handled = true;
}
}
}