Skip to content

Commit e5635c1

Browse files
committed
fixed arrow keys cycle navigation
1 parent e1a3de2 commit e5635c1

File tree

4 files changed

+47
-13
lines changed

4 files changed

+47
-13
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
namespace WinUI.TableView.Extensions;
2+
3+
internal static class TableViewCellSlotExtensions
4+
{
5+
public static bool IsValidRow(this TableViewCellSlot slot, TableView tableView)
6+
{
7+
return slot.Row >= 0 && slot.Row < tableView?.Items.Count;
8+
}
9+
10+
public static bool IsValidColumn(this TableViewCellSlot slot, TableView tableView)
11+
{
12+
return slot.Column >= 0 && slot.Column < tableView?.Columns.VisibleColumns.Count;
13+
}
14+
15+
public static bool IsValid(this TableViewCellSlot slot, TableView tableView)
16+
{
17+
return slot.IsValidRow(tableView) && slot.IsValidColumn(tableView);
18+
}
19+
}

src/WinUI.TableView/TableView.Properties.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public partial class TableView
2828

2929
public IAdvancedCollectionView CollectionView { get; private set; } = new AdvancedCollectionView();
3030
internal IDictionary<string, Predicate<object>> ActiveFilters { get; } = new Dictionary<string, Predicate<object>>();
31+
internal TableViewSelectionUnit LastSelectionUnit { get; set; }
3132
internal TableViewCellSlot? CurrentCellSlot { get; set; }
3233
internal TableViewCellSlot? SelectionStartCellSlot { get; set; }
3334
internal int? SelectionStartRowIndex { get; set; }

src/WinUI.TableView/TableView.cs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ private void HandleNavigations(KeyRoutedEventArgs e, bool shiftKey, bool ctrlKey
158158
else if ((e.Key is VirtualKey.Left or VirtualKey.Right or VirtualKey.Up or VirtualKey.Down)
159159
&& !IsEditing)
160160
{
161-
var row = (SelectionUnit is TableViewSelectionUnit.Row ? SelectionStartRowIndex : SelectionStartCellSlot?.Row) ?? -1;
161+
var row = (LastSelectionUnit is TableViewSelectionUnit.Row ? SelectionStartRowIndex : SelectionStartCellSlot?.Row) ?? -1;
162162
var column = CurrentCellSlot?.Column ?? -1;
163163

164164
if (row == -1 && column == -1)
@@ -168,6 +168,11 @@ private void HandleNavigations(KeyRoutedEventArgs e, bool shiftKey, bool ctrlKey
168168
else if (e.Key is VirtualKey.Left or VirtualKey.Right)
169169
{
170170
column = e.Key is VirtualKey.Left ? ctrlKey ? 0 : column - 1 : ctrlKey ? Columns.VisibleColumns.Count - 1 : column + 1;
171+
if (column >= Columns.VisibleColumns.Count)
172+
{
173+
column = 0;
174+
row++;
175+
}
171176
}
172177
else
173178
{
@@ -612,11 +617,6 @@ internal void ClearFilters()
612617
}
613618
}
614619

615-
private bool IsValidSlot(TableViewCellSlot slot)
616-
{
617-
return slot.Row >= 0 && slot.Column >= 0 && slot.Row < Items.Count && slot.Column < Columns.VisibleColumns.Count;
618-
}
619-
620620
internal new void SelectAll()
621621
{
622622
if (IsEditing)
@@ -703,7 +703,7 @@ private void DeselectAllCells()
703703

704704
internal void MakeSelection(TableViewCellSlot slot, bool shiftKey, bool ctrlKey = false)
705705
{
706-
if (slot.Row < 0 || slot.Row >= Items.Count)
706+
if (!slot.IsValidRow(this))
707707
{
708708
return;
709709
}
@@ -725,13 +725,17 @@ internal void MakeSelection(TableViewCellSlot slot, bool shiftKey, bool ctrlKey
725725
}
726726
}
727727

728-
if (SelectionUnit is TableViewSelectionUnit.Row)
728+
if (SelectionUnit is TableViewSelectionUnit.Row
729+
|| (LastSelectionUnit is TableViewSelectionUnit.Row && slot.IsValidRow(this) && !slot.IsValidColumn(this))
730+
|| (SelectionUnit is TableViewSelectionUnit.CellOrRow && slot.IsValidRow(this) && !slot.IsValidColumn(this)))
729731
{
730732
SelectRows(slot, shiftKey);
733+
LastSelectionUnit = TableViewSelectionUnit.Row;
731734
}
732735
else
733736
{
734737
SelectCells(slot, shiftKey);
738+
LastSelectionUnit = TableViewSelectionUnit.Cell;
735739
}
736740
}
737741
else if (!IsReadOnly)
@@ -771,7 +775,7 @@ private void SelectRows(TableViewCellSlot slot, bool shiftKey)
771775
}
772776
}
773777

774-
if (!IsReadOnly && IsValidSlot(slot))
778+
if (!IsReadOnly && slot.IsValid(this))
775779
{
776780
DispatcherQueue.TryEnqueue(() => SetCurrentCell(slot));
777781
}
@@ -787,7 +791,7 @@ private void SelectRows(TableViewCellSlot slot, bool shiftKey)
787791

788792
private void SelectCells(TableViewCellSlot slot, bool shiftKey)
789793
{
790-
if (!IsValidSlot(slot))
794+
if (!slot.IsValid(this))
791795
{
792796
return;
793797
}
@@ -896,7 +900,7 @@ private void OnCellSelectionChanged()
896900

897901
internal async Task<TableViewCell> ScrollCellIntoView(TableViewCellSlot slot)
898902
{
899-
if (_scrollViewer is null || !IsValidSlot(slot)) return default!;
903+
if (_scrollViewer is null || !slot.IsValid(this)) return default!;
900904

901905
var row = await ScrollRowIntoView(slot.Row);
902906
var (start, end) = GetColumnsInDisplay();
@@ -1007,7 +1011,7 @@ void ViewChanged(object? _, ScrollViewerViewChangedEventArgs e)
10071011

10081012
internal TableViewCell GetCellFromSlot(TableViewCellSlot slot)
10091013
{
1010-
return IsValidSlot(slot) && ContainerFromIndex(slot.Row) is TableViewRow row ? row.Cells[slot.Column] : default!;
1014+
return slot.IsValid(this) && ContainerFromIndex(slot.Row) is TableViewRow row ? row.Cells[slot.Column] : default!;
10111015
}
10121016

10131017
private (int start, int end) GetColumnsInDisplay()

src/WinUI.TableView/TableViewRow.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,17 @@ protected override void OnPointerReleased(PointerRoutedEventArgs e)
5454
if (!KeyBoardHelper.IsShiftKeyDown() && TableView is not null)
5555
{
5656
TableView.SelectionStartCellSlot = null;
57-
TableView.SelectionStartRowIndex = null;
57+
TableView.SelectionStartRowIndex = Index;
58+
}
59+
}
60+
61+
protected override void OnTapped(TappedRoutedEventArgs e)
62+
{
63+
base.OnTapped(e);
64+
65+
if (TableView?.SelectionUnit is TableViewSelectionUnit.Row or TableViewSelectionUnit.CellOrRow)
66+
{
67+
TableView.LastSelectionUnit = TableViewSelectionUnit.Row;
5868
}
5969
}
6070

0 commit comments

Comments
 (0)