feat: update click behavior.

This commit is contained in:
rabbitism
2024-04-11 23:47:36 +08:00
parent 40c1d96f2e
commit edf0e387cc
4 changed files with 39 additions and 12 deletions

View File

@@ -9,7 +9,11 @@
<StackPanel>
<u:TreeComboBox>
<u:TreeComboBoxItem Header="Hello">
<u:TreeComboBoxItem Header="Hello World"/>
<u:TreeComboBoxItem Header="Hello World">
<u:TreeComboBoxItem Header="Hello World 1"/>
<u:TreeComboBoxItem Header="Hello World 2"/>
<u:TreeComboBoxItem Header="Hello World 3"/>
</u:TreeComboBoxItem>
<u:TreeComboBoxItem Header="Hello Avalonia"/>
<u:TreeComboBoxItem Header="Hello Another"/>
</u:TreeComboBoxItem>

View File

@@ -1,17 +1,19 @@
<ResourceDictionary xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:u="https://irihi.tech/ursa"
xmlns:iri="https://irihi.tech/shared">
xmlns:iri="https://irihi.tech/shared"
xmlns:converters="clr-namespace:Avalonia.Controls.Converters;assembly=Avalonia.Controls">
<!-- Add Resources Here -->
<converters:MarginMultiplierConverter x:Key="LeftMarginConverter" Indent="20" Left="True"/>
<ControlTheme x:Key="{x:Type u:TreeComboBox}" TargetType="u:TreeComboBox">
<Setter Property="Template">
<ControlTemplate TargetType="u:TreeComboBox">
<Grid ColumnDefinitions="*, Auto, 32">
<Border Grid.Column="0" Grid.ColumnSpan="3" Background="Red"></Border>
<Grid ColumnDefinitions="*, Auto, 32" Background="Transparent">
<Border Grid.Column="0" Grid.ColumnSpan="3" Background="Transparent" BorderBrush="Blue" BorderThickness="1" CornerRadius="3"/>
<ContentPresenter Grid.Column="0" MinHeight="32" Content="{TemplateBinding SelectionBoxItem}"/>
<Popup Grid.Column="0" IsOpen="{Binding IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}}">
<Popup Name="{x:Static iri:PartNames.PART_Popup}" Grid.Column="0" IsLightDismissEnabled="True" IsOpen="{Binding IsDropDownOpen, RelativeSource={RelativeSource TemplatedParent}}">
<Border Background="{DynamicResource ComboBoxPopupBackground}">
<ScrollViewer>
<ItemsPresenter ItemsPanel="{TemplateBinding ItemsPanel}"/>
@@ -28,12 +30,15 @@
<ControlTemplate TargetType="u:TreeComboBoxItem">
<StackPanel>
<Border Name="PART_LayoutRoot" MinHeight="{TemplateBinding MinHeight}" TemplatedControl.IsTemplateFocusTarget="True">
<Grid Name="{x:Static iri:PartNames.PART_Header}" Margin="10" ColumnDefinitions="Auto, *">
<ToggleButton Name="PART_ExpandCollapseChevron" Grid.Column="0" Focusable="False"></ToggleButton>
<Grid
Name="{x:Static iri:PartNames.PART_Header}"
Margin="{TemplateBinding Level, Mode=OneWay, Converter={StaticResource LeftMarginConverter}}"
ColumnDefinitions="Auto, *">
<ToggleButton IsChecked="{TemplateBinding IsExpanded, Mode=TwoWay}" Theme="{DynamicResource ToggleButtonTreeViewItemIconButton}" Name="PART_ExpandCollapseChevron" Grid.Column="0" Focusable="False"></ToggleButton>
<ContentPresenter Grid.Column="1" Name="{x:Static iri:PartNames.PART_HeaderPresenter}" Content="{TemplateBinding Header}" ContentTemplate="{TemplateBinding HeaderTemplate}" Focusable="False"/>
</Grid>
</Border>
<ItemsPresenter Name="{x:Static iri:PartNames.PART_ItemsPresenter}" ItemsPanel="{TemplateBinding ItemsPanel}"/>
<ItemsPresenter IsVisible="{TemplateBinding IsExpanded}" Name="{x:Static iri:PartNames.PART_ItemsPresenter}" ItemsPanel="{TemplateBinding ItemsPanel}"/>
</StackPanel>
</ControlTemplate>
</Setter>

View File

@@ -15,6 +15,8 @@ namespace Ursa.Controls;
[TemplatePart(PartNames.PART_Popup, typeof(Popup))]
public class TreeComboBox: SelectingItemsControl
{
private Popup? _popup;
private static readonly FuncTemplate<Panel?> DefaultPanel =
new FuncTemplate<Panel?>(() => new VirtualizingStackPanel());
@@ -87,6 +89,12 @@ public class TreeComboBox: SelectingItemsControl
FocusableProperty.OverrideDefaultValue<TreeComboBox>(true);
}
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{
base.OnApplyTemplate(e);
_popup = e.NameScope.Find<Popup>(PartNames.PART_Popup);
}
protected override bool NeedsContainerOverride(object? item, int index, out object? recycleKey)
{
return NeedsContainer<TreeComboBoxItem>(item, out recycleKey);
@@ -122,7 +130,16 @@ public class TreeComboBox: SelectingItemsControl
base.OnPointerReleased(e);
if (e.InitialPressMouseButton == MouseButton.Left)
{
IsDropDownOpen = !IsDropDownOpen;
if (_popup is not null && _popup.IsOpen && e.Source is TextBlock v && _popup.IsInsidePopup(v))
{
SelectionBoxItem = v.Text;
SetCurrentValue(IsDropDownOpenProperty, false);
}
else
{
IsDropDownOpen = !IsDropDownOpen;
}
}
}
}

View File

@@ -3,6 +3,7 @@ using Avalonia.Controls;
using Avalonia.Controls.Metadata;
using Avalonia.Controls.Primitives;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.LogicalTree;
using Avalonia.Media;
using Avalonia.Media.TextFormatting;
@@ -47,9 +48,9 @@ public class TreeComboBoxItem: HeaderedItemsControl, ISelectable
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{
base.OnApplyTemplate(e);
DoubleTappedEvent.RemoveHandler(OnDoubleTapped, _header);
DoubleTappedEvent.RemoveHandler(OnDoubleTapped, this);
_header = e.NameScope.Find<Control>(PartNames.PART_Header);
DoubleTappedEvent.AddHandler(OnDoubleTapped, _header);
DoubleTappedEvent.AddHandler(OnDoubleTapped, RoutingStrategies.Tunnel, true, this);
}
protected override void OnAttachedToLogicalTree(LogicalTreeAttachmentEventArgs e)