From fd65e37aab90475bec4c5ca79ceeef87a9644740 Mon Sep 17 00:00:00 2001 From: chinboy Date: Thu, 30 Oct 2025 11:38:47 +0800 Subject: [PATCH] Add IsItemSelectableProperty for List --- src/AtomUI.Controls/List/List.cs | 9 ++ src/AtomUI.Controls/List/ListDefaultView.cs | 95 ++++++++++++------- .../List/Themes/ListItemTheme.axaml | 6 +- .../List/Themes/ListTheme.axaml | 1 + 4 files changed, 75 insertions(+), 36 deletions(-) diff --git a/src/AtomUI.Controls/List/List.cs b/src/AtomUI.Controls/List/List.cs index 1c0e6325c..0a709d1cf 100644 --- a/src/AtomUI.Controls/List/List.cs +++ b/src/AtomUI.Controls/List/List.cs @@ -70,6 +70,9 @@ public class List : TemplatedControl, public static readonly StyledProperty SelectionModeProperty = AvaloniaProperty.Register(nameof(SelectionMode)); + public static readonly StyledProperty IsItemSelectableProperty = + AvaloniaProperty.Register(nameof(IsItemSelectable), true); + public static readonly StyledProperty SizeTypeProperty = SizeTypeAwareControlProperty.SizeTypeProperty.AddOwner(); @@ -158,6 +161,12 @@ public class List : TemplatedControl, set => SetValue(SelectionModeProperty, value); } + public bool IsItemSelectable + { + get => GetValue(IsItemSelectableProperty); + set => SetValue(IsItemSelectableProperty, value); + } + public SizeType SizeType { get => GetValue(SizeTypeProperty); diff --git a/src/AtomUI.Controls/List/ListDefaultView.cs b/src/AtomUI.Controls/List/ListDefaultView.cs index 8fe782f1e..7d1809c62 100644 --- a/src/AtomUI.Controls/List/ListDefaultView.cs +++ b/src/AtomUI.Controls/List/ListDefaultView.cs @@ -41,6 +41,9 @@ internal class ListDefaultView : SelectingItemsControl public new static readonly StyledProperty SelectionModeProperty = SelectingItemsControl.SelectionModeProperty; + public static readonly StyledProperty IsItemSelectableProperty = + List.IsItemSelectableProperty.AddOwner(); + public static readonly StyledProperty SizeTypeProperty = SizeTypeAwareControlProperty.SizeTypeProperty.AddOwner(); @@ -86,6 +89,12 @@ internal class ListDefaultView : SelectingItemsControl set => base.SelectionMode = value; } + public bool IsItemSelectable + { + get => GetValue(IsItemSelectableProperty); + set => SetValue(IsItemSelectableProperty, value); + } + public SizeType SizeType { get => GetValue(SizeTypeProperty); @@ -235,33 +244,37 @@ internal class ListDefaultView : SelectingItemsControl protected override void OnKeyDown(KeyEventArgs e) { - var hotkeys = Application.Current!.PlatformSettings?.HotkeyConfiguration; - var ctrl = hotkeys is not null && e.KeyModifiers.HasAllFlags(hotkeys.CommandModifiers); + if (IsItemSelectable) + { + var hotkeys = Application.Current!.PlatformSettings?.HotkeyConfiguration; + var ctrl = hotkeys is not null && e.KeyModifiers.HasAllFlags(hotkeys.CommandModifiers); - if (!ctrl && - e.Key.ToNavigationDirection() is { } direction && - direction.IsDirectional()) - { - e.Handled |= MoveSelection( - direction, - WrapSelection, - e.KeyModifiers.HasAllFlags(KeyModifiers.Shift)); - } - else if (SelectionMode.HasAllFlags(SelectionMode.Multiple) && - hotkeys is not null && hotkeys.SelectAll.Any(x => x.Matches(e))) - { - Selection.SelectAll(); - e.Handled = true; - } - else if (e.Key == Key.Space || e.Key == Key.Enter) - { - UpdateSelectionFromEventSource( - e.Source, - true, - e.KeyModifiers.HasFlag(KeyModifiers.Shift), - ctrl); - } + if (!ctrl && + e.Key.ToNavigationDirection() is { } direction && + direction.IsDirectional()) + { + e.Handled |= MoveSelection( + direction, + WrapSelection, + e.KeyModifiers.HasAllFlags(KeyModifiers.Shift)); + } + else if (SelectionMode.HasAllFlags(SelectionMode.Multiple) && + hotkeys is not null && hotkeys.SelectAll.Any(x => x.Matches(e))) + { + Selection.SelectAll(); + e.Handled = true; + } + else if (e.Key == Key.Space || e.Key == Key.Enter) + { + UpdateSelectionFromEventSource( + e.Source, + true, + e.KeyModifiers.HasFlag(KeyModifiers.Shift), + ctrl); + } + } + base.OnKeyDown(e); } @@ -273,13 +286,29 @@ internal class ListDefaultView : SelectingItemsControl protected internal virtual bool UpdateSelectionFromPointerEvent(Control source, PointerEventArgs e) { - var hotkeys = Application.Current!.PlatformSettings?.HotkeyConfiguration; - var toggle = hotkeys is not null && e.KeyModifiers.HasAllFlags(hotkeys.CommandModifiers); - return UpdateSelectionFromEventSource( - source, - true, - e.KeyModifiers.HasAllFlags(KeyModifiers.Shift), - toggle, - e.GetCurrentPoint(source).Properties.IsRightButtonPressed); + if (IsItemSelectable) + { + var hotkeys = Application.Current!.PlatformSettings?.HotkeyConfiguration; + var toggle = hotkeys is not null && e.KeyModifiers.HasAllFlags(hotkeys.CommandModifiers); + return UpdateSelectionFromEventSource( + source, + true, + e.KeyModifiers.HasAllFlags(KeyModifiers.Shift), + toggle, + e.GetCurrentPoint(source).Properties.IsRightButtonPressed); + } + return false; + } + + protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change) + { + base.OnPropertyChanged(change); + if (change.Property == IsItemSelectableProperty) + { + if (!IsItemSelectable) + { + Selection.Clear(); + } + } } } \ No newline at end of file diff --git a/src/AtomUI.Controls/List/Themes/ListItemTheme.axaml b/src/AtomUI.Controls/List/Themes/ListItemTheme.axaml index 5d17fe402..f37584c33 100644 --- a/src/AtomUI.Controls/List/Themes/ListItemTheme.axaml +++ b/src/AtomUI.Controls/List/Themes/ListItemTheme.axaml @@ -52,19 +52,19 @@ diff --git a/src/AtomUI.Controls/List/Themes/ListTheme.axaml b/src/AtomUI.Controls/List/Themes/ListTheme.axaml index 6273a5b12..0087f4aa7 100644 --- a/src/AtomUI.Controls/List/Themes/ListTheme.axaml +++ b/src/AtomUI.Controls/List/Themes/ListTheme.axaml @@ -46,6 +46,7 @@ ItemTemplate="{TemplateBinding ItemTemplate}" SelectedItems="{TemplateBinding SelectedItems, Mode=TwoWay}" IsShowSelectedIndicator="{TemplateBinding IsShowSelectedIndicator}" + IsItemSelectable="{TemplateBinding IsItemSelectable}" ItemsSource="{TemplateBinding CollectionView}"/>