From 7c8982ae3f7db992f8482010e7c87b0974f75d44 Mon Sep 17 00:00:00 2001 From: Emmanuel Hansen Date: Mon, 10 Oct 2022 11:55:56 +0000 Subject: [PATCH] prevent selection from being updated on focus when toggle is enabled --- src/Avalonia.Controls/ListBox.cs | 3 +- .../Primitives/SelectingItemsControl.cs | 45 +++++++++++++------ 2 files changed, 33 insertions(+), 15 deletions(-) diff --git a/src/Avalonia.Controls/ListBox.cs b/src/Avalonia.Controls/ListBox.cs index 80b5259a53..86118d7b00 100644 --- a/src/Avalonia.Controls/ListBox.cs +++ b/src/Avalonia.Controls/ListBox.cs @@ -139,7 +139,8 @@ namespace Avalonia.Controls e.Source, true, e.KeyModifiers.HasAllFlags(KeyModifiers.Shift), - e.KeyModifiers.HasAllFlags(KeyModifiers.Control)); + e.KeyModifiers.HasAllFlags(KeyModifiers.Control), + fromFocus: true); } } diff --git a/src/Avalonia.Controls/Primitives/SelectingItemsControl.cs b/src/Avalonia.Controls/Primitives/SelectingItemsControl.cs index ea20247b4b..e03b02a479 100644 --- a/src/Avalonia.Controls/Primitives/SelectingItemsControl.cs +++ b/src/Avalonia.Controls/Primitives/SelectingItemsControl.cs @@ -586,6 +586,14 @@ namespace Avalonia.Controls.Primitives Selection.SelectAll(); e.Handled = true; } + else if (e.Key == Key.Space || e.Key == Key.Enter) + { + e.Handled = UpdateSelectionFromEventSource( + e.Source, + true, + e.KeyModifiers.HasFlag(KeyModifiers.Shift), + e.KeyModifiers.HasFlag(KeyModifiers.Control)); + } } } @@ -662,12 +670,14 @@ namespace Avalonia.Controls.Primitives /// Whether the range modifier is enabled (i.e. shift key). /// Whether the toggle modifier is enabled (i.e. ctrl key). /// Whether the event is a right-click. + /// Wheter the event is a focus event protected void UpdateSelection( int index, bool select = true, bool rangeModifier = false, bool toggleModifier = false, - bool rightButton = false) + bool rightButton = false, + bool fromFocus = false) { if (index < 0 || index >= ItemCount) { @@ -696,22 +706,25 @@ namespace Avalonia.Controls.Primitives Selection.Clear(); Selection.SelectRange(Selection.AnchorIndex, index); } - else if (multi && toggle) + else if (!fromFocus && toggle) { - if (Selection.IsSelected(index) == true) + if (multi) { - Selection.Deselect(index); + if (Selection.IsSelected(index) == true) + { + Selection.Deselect(index); + } + else + { + Selection.Select(index); + } } else { - Selection.Select(index); + SelectedIndex = (SelectedIndex == index) ? -1 : index; } } - else if (toggle) - { - SelectedIndex = (SelectedIndex == index) ? -1 : index; - } - else + else if (!toggle) { using var operation = Selection.BatchUpdate(); Selection.Clear(); @@ -735,18 +748,20 @@ namespace Avalonia.Controls.Primitives /// Whether the range modifier is enabled (i.e. shift key). /// Whether the toggle modifier is enabled (i.e. ctrl key). /// Whether the event is a right-click. + /// Wheter the event is a focus event protected void UpdateSelection( IControl container, bool select = true, bool rangeModifier = false, bool toggleModifier = false, - bool rightButton = false) + bool rightButton = false, + bool fromFocus = false) { var index = ItemContainerGenerator?.IndexFromContainer(container) ?? -1; if (index != -1) { - UpdateSelection(index, select, rangeModifier, toggleModifier, rightButton); + UpdateSelection(index, select, rangeModifier, toggleModifier, rightButton, fromFocus); } } @@ -759,6 +774,7 @@ namespace Avalonia.Controls.Primitives /// Whether the range modifier is enabled (i.e. shift key). /// Whether the toggle modifier is enabled (i.e. ctrl key). /// Whether the event is a right-click. + /// Wheter the event is a focus event /// /// True if the event originated from a container that belongs to the control; otherwise /// false. @@ -768,13 +784,14 @@ namespace Avalonia.Controls.Primitives bool select = true, bool rangeModifier = false, bool toggleModifier = false, - bool rightButton = false) + bool rightButton = false, + bool fromFocus = false) { var container = GetContainerFromEventSource(eventSource); if (container != null) { - UpdateSelection(container, select, rangeModifier, toggleModifier, rightButton); + UpdateSelection(container, select, rangeModifier, toggleModifier, rightButton, fromFocus); return true; }