Skip to content

Commit 2a7e8ac

Browse files
authored
Merge pull request #327 from w-ahmad/fix/row_selection
fix: Improve row selection and prevent unnecessary event triggers
2 parents 03c7237 + 5a0be1e commit 2a7e8ac

File tree

2 files changed

+42
-35
lines changed

2 files changed

+42
-35
lines changed

src/TableView.cs

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ protected override DependencyObject GetContainerForItemOverride()
130130
_rows.Add(row);
131131
return row;
132132
}
133-
133+
134134
/// <inheritdoc/>
135135
protected override async void OnKeyDown(KeyRoutedEventArgs e)
136136
{
@@ -660,7 +660,7 @@ private static TableViewBoundColumn GetTableViewColumnFromType(string? propertyN
660660
{
661661
column = new TableViewCheckBoxColumn();
662662
}
663-
else if(type.IsUri())
663+
else if (type.IsUri())
664664
{
665665
column = new TableViewHyperlinkColumn();
666666
}
@@ -968,29 +968,20 @@ internal void MakeSelection(TableViewCellSlot slot, bool shiftKey, bool ctrlKey
968968
{
969969
ctrlKey = ctrlKey || SelectionMode is ListViewSelectionMode.Multiple;
970970

971-
if (!ctrlKey || !(SelectionMode is ListViewSelectionMode.Multiple or ListViewSelectionMode.Extended))
972-
{
973-
if (SelectedItems.Count > 0)
974-
{
975-
DeselectAllItems();
976-
}
977-
978-
if (SelectedCells.Count > 0)
979-
{
980-
SelectedCellRanges.Clear();
981-
}
982-
}
983-
984971
if (SelectionUnit is TableViewSelectionUnit.Row
985972
|| (LastSelectionUnit is TableViewSelectionUnit.Row && slot.IsValidRow(this) && !slot.IsValidColumn(this))
986973
|| (SelectionUnit is TableViewSelectionUnit.CellOrRow && slot.IsValidRow(this) && !slot.IsValidColumn(this)))
987974
{
988-
SelectRows(slot, shiftKey);
975+
if (!ctrlKey)
976+
DeselectAllCells();
977+
SelectRows(slot, shiftKey, ctrlKey);
989978
LastSelectionUnit = TableViewSelectionUnit.Row;
990979
}
991980
else
992981
{
993-
SelectCells(slot, shiftKey);
982+
if (!ctrlKey)
983+
DeselectAllItems();
984+
SelectCells(slot, shiftKey, ctrlKey);
994985
LastSelectionUnit = TableViewSelectionUnit.Cell;
995986
}
996987
}
@@ -1004,34 +995,46 @@ internal void MakeSelection(TableViewCellSlot slot, bool shiftKey, bool ctrlKey
1004995
/// <summary>
1005996
/// Selects rows based on the specified cell slot.
1006997
/// </summary>
1007-
private void SelectRows(TableViewCellSlot slot, bool shiftKey)
998+
private void SelectRows(TableViewCellSlot slot, bool shiftKey, bool ctrlKey)
1008999
{
10091000
var selectionRange = SelectedRanges.FirstOrDefault(x => x.IsInRange(slot.Row));
10101001
SelectionStartRowIndex ??= slot.Row;
1011-
CurrentRowIndex = slot.Row;
10121002

1013-
if (selectionRange is not null)
1003+
if (selectionRange is not null && ctrlKey && !shiftKey && (CurrentRowIndex != slot.Row || CurrentCellSlot == slot))
10141004
{
1015-
DeselectRange(selectionRange);
1005+
DeselectRange(new ItemIndexRange(slot.Row, 1));
10161006
}
1017-
1018-
if (shiftKey && SelectionMode is ListViewSelectionMode.Multiple or ListViewSelectionMode.Extended)
1007+
else if ((!shiftKey && !ctrlKey && SelectedItems.Count <= 1) || SelectionMode is ListViewSelectionMode.Single)
1008+
{
1009+
SelectionStartRowIndex = CurrentRowIndex = SelectedIndex = slot.Row;
1010+
}
1011+
else if ((!ctrlKey && !shiftKey) || !(SelectionMode is ListViewSelectionMode.Multiple or ListViewSelectionMode.Extended))
1012+
{
1013+
SelectionStartRowIndex = CurrentRowIndex = SelectedIndex = slot.Row;
1014+
}
1015+
else if (SelectionMode is ListViewSelectionMode.Multiple or ListViewSelectionMode.Extended)
10191016
{
10201017
var min = Math.Min(SelectionStartRowIndex.Value, slot.Row);
10211018
var max = Math.Max(SelectionStartRowIndex.Value, slot.Row);
1019+
var newSelection = new ItemIndexRange(min, (uint)(max - min) + 1);
10221020

1023-
SelectRange(new ItemIndexRange(min, (uint)(max - min) + 1));
1024-
}
1025-
else
1026-
{
1027-
SelectionStartRowIndex = slot.Row;
1028-
if (SelectionMode is ListViewSelectionMode.Single)
1021+
if (!ctrlKey && newSelection.Length == 1)
10291022
{
1030-
SelectedIndex = slot.Row;
1023+
SelectionStartRowIndex = CurrentRowIndex = SelectedIndex = slot.Row;
10311024
}
1032-
else
1025+
if (selectionRange?.LastIndex > newSelection.LastIndex)
10331026
{
1034-
SelectRange(new ItemIndexRange(slot.Row, 1));
1027+
var deselectRange = new ItemIndexRange(newSelection.LastIndex + 1, (uint)(selectionRange.LastIndex - newSelection.LastIndex));
1028+
DeselectRange(deselectRange);
1029+
}
1030+
else if (selectionRange?.FirstIndex < newSelection.FirstIndex)
1031+
{
1032+
var deselectRange = new ItemIndexRange(selectionRange.FirstIndex, (uint)(newSelection.FirstIndex - selectionRange.FirstIndex));
1033+
DeselectRange(deselectRange);
1034+
}
1035+
else if (selectionRange != newSelection)
1036+
{
1037+
SelectRange(newSelection);
10351038
}
10361039
}
10371040

@@ -1052,13 +1055,18 @@ private void SelectRows(TableViewCellSlot slot, bool shiftKey)
10521055
/// <summary>
10531056
/// Selects cells based on the specified cell slot.
10541057
/// </summary>
1055-
private void SelectCells(TableViewCellSlot slot, bool shiftKey)
1058+
private void SelectCells(TableViewCellSlot slot, bool shiftKey, bool ctrlKey)
10561059
{
10571060
if (!slot.IsValid(this))
10581061
{
10591062
return;
10601063
}
10611064

1065+
if (!ctrlKey || !(SelectionMode is ListViewSelectionMode.Multiple or ListViewSelectionMode.Extended))
1066+
{
1067+
DeselectAll();
1068+
}
1069+
10621070
var selectionRange = (SelectionStartCellSlot is null ? null : SelectedCellRanges.LastOrDefault(x => SelectionStartCellSlot.HasValue && x.Contains(SelectionStartCellSlot.Value))) ?? [];
10631071
SelectedCellRanges.Remove(selectionRange);
10641072
selectionRange.Clear();

src/TableViewCell.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ protected override async void OnTapped(TappedRoutedEventArgs e)
224224
if (e.Handled) return;
225225
}
226226

227-
if (TableView?.CurrentCellSlot != Slot)
227+
if (TableView?.CurrentCellSlot != Slot || TableView?.LastSelectionUnit is TableViewSelectionUnit.Row)
228228
{
229229
MakeSelection();
230230
e.Handled = true;
@@ -362,7 +362,6 @@ private void MakeSelection()
362362
}
363363

364364
TableView.SetIsEditing(false);
365-
TableView.UpdateCornerButtonState();
366365
}
367366

368367
/// <summary>

0 commit comments

Comments
 (0)