Browse Source

prevent selection from being updated on focus when toggle is enabled

pull/9144/head
Emmanuel Hansen 4 years ago
parent
commit
7c8982ae3f
  1. 3
      src/Avalonia.Controls/ListBox.cs
  2. 45
      src/Avalonia.Controls/Primitives/SelectingItemsControl.cs

3
src/Avalonia.Controls/ListBox.cs

@ -139,7 +139,8 @@ namespace Avalonia.Controls
e.Source, e.Source,
true, true,
e.KeyModifiers.HasAllFlags(KeyModifiers.Shift), e.KeyModifiers.HasAllFlags(KeyModifiers.Shift),
e.KeyModifiers.HasAllFlags(KeyModifiers.Control)); e.KeyModifiers.HasAllFlags(KeyModifiers.Control),
fromFocus: true);
} }
} }

45
src/Avalonia.Controls/Primitives/SelectingItemsControl.cs

@ -586,6 +586,14 @@ namespace Avalonia.Controls.Primitives
Selection.SelectAll(); Selection.SelectAll();
e.Handled = true; 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
/// <param name="rangeModifier">Whether the range modifier is enabled (i.e. shift key).</param> /// <param name="rangeModifier">Whether the range modifier is enabled (i.e. shift key).</param>
/// <param name="toggleModifier">Whether the toggle modifier is enabled (i.e. ctrl key).</param> /// <param name="toggleModifier">Whether the toggle modifier is enabled (i.e. ctrl key).</param>
/// <param name="rightButton">Whether the event is a right-click.</param> /// <param name="rightButton">Whether the event is a right-click.</param>
/// <param name="fromFocus">Wheter the event is a focus event</param>
protected void UpdateSelection( protected void UpdateSelection(
int index, int index,
bool select = true, bool select = true,
bool rangeModifier = false, bool rangeModifier = false,
bool toggleModifier = false, bool toggleModifier = false,
bool rightButton = false) bool rightButton = false,
bool fromFocus = false)
{ {
if (index < 0 || index >= ItemCount) if (index < 0 || index >= ItemCount)
{ {
@ -696,22 +706,25 @@ namespace Avalonia.Controls.Primitives
Selection.Clear(); Selection.Clear();
Selection.SelectRange(Selection.AnchorIndex, index); 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 else
{ {
Selection.Select(index); SelectedIndex = (SelectedIndex == index) ? -1 : index;
} }
} }
else if (toggle) else if (!toggle)
{
SelectedIndex = (SelectedIndex == index) ? -1 : index;
}
else
{ {
using var operation = Selection.BatchUpdate(); using var operation = Selection.BatchUpdate();
Selection.Clear(); Selection.Clear();
@ -735,18 +748,20 @@ namespace Avalonia.Controls.Primitives
/// <param name="rangeModifier">Whether the range modifier is enabled (i.e. shift key).</param> /// <param name="rangeModifier">Whether the range modifier is enabled (i.e. shift key).</param>
/// <param name="toggleModifier">Whether the toggle modifier is enabled (i.e. ctrl key).</param> /// <param name="toggleModifier">Whether the toggle modifier is enabled (i.e. ctrl key).</param>
/// <param name="rightButton">Whether the event is a right-click.</param> /// <param name="rightButton">Whether the event is a right-click.</param>
/// <param name="fromFocus">Wheter the event is a focus event</param>
protected void UpdateSelection( protected void UpdateSelection(
IControl container, IControl container,
bool select = true, bool select = true,
bool rangeModifier = false, bool rangeModifier = false,
bool toggleModifier = false, bool toggleModifier = false,
bool rightButton = false) bool rightButton = false,
bool fromFocus = false)
{ {
var index = ItemContainerGenerator?.IndexFromContainer(container) ?? -1; var index = ItemContainerGenerator?.IndexFromContainer(container) ?? -1;
if (index != -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
/// <param name="rangeModifier">Whether the range modifier is enabled (i.e. shift key).</param> /// <param name="rangeModifier">Whether the range modifier is enabled (i.e. shift key).</param>
/// <param name="toggleModifier">Whether the toggle modifier is enabled (i.e. ctrl key).</param> /// <param name="toggleModifier">Whether the toggle modifier is enabled (i.e. ctrl key).</param>
/// <param name="rightButton">Whether the event is a right-click.</param> /// <param name="rightButton">Whether the event is a right-click.</param>
/// <param name="fromFocus">Wheter the event is a focus event</param>
/// <returns> /// <returns>
/// True if the event originated from a container that belongs to the control; otherwise /// True if the event originated from a container that belongs to the control; otherwise
/// false. /// false.
@ -768,13 +784,14 @@ namespace Avalonia.Controls.Primitives
bool select = true, bool select = true,
bool rangeModifier = false, bool rangeModifier = false,
bool toggleModifier = false, bool toggleModifier = false,
bool rightButton = false) bool rightButton = false,
bool fromFocus = false)
{ {
var container = GetContainerFromEventSource(eventSource); var container = GetContainerFromEventSource(eventSource);
if (container != null) if (container != null)
{ {
UpdateSelection(container, select, rangeModifier, toggleModifier, rightButton); UpdateSelection(container, select, rangeModifier, toggleModifier, rightButton, fromFocus);
return true; return true;
} }

Loading…
Cancel
Save