Skip to content

Commit

Permalink
fixed arrow keys cycle navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
w-ahmad committed Nov 25, 2024
1 parent e1a3de2 commit e5635c1
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 13 deletions.
19 changes: 19 additions & 0 deletions src/WinUI.TableView/Extensions/TableViewCellSlotExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace WinUI.TableView.Extensions;

internal static class TableViewCellSlotExtensions
{
public static bool IsValidRow(this TableViewCellSlot slot, TableView tableView)
{
return slot.Row >= 0 && slot.Row < tableView?.Items.Count;
}

public static bool IsValidColumn(this TableViewCellSlot slot, TableView tableView)
{
return slot.Column >= 0 && slot.Column < tableView?.Columns.VisibleColumns.Count;
}

public static bool IsValid(this TableViewCellSlot slot, TableView tableView)
{
return slot.IsValidRow(tableView) && slot.IsValidColumn(tableView);
}
}
1 change: 1 addition & 0 deletions src/WinUI.TableView/TableView.Properties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public partial class TableView

public IAdvancedCollectionView CollectionView { get; private set; } = new AdvancedCollectionView();
internal IDictionary<string, Predicate<object>> ActiveFilters { get; } = new Dictionary<string, Predicate<object>>();
internal TableViewSelectionUnit LastSelectionUnit { get; set; }
internal TableViewCellSlot? CurrentCellSlot { get; set; }
internal TableViewCellSlot? SelectionStartCellSlot { get; set; }
internal int? SelectionStartRowIndex { get; set; }
Expand Down
28 changes: 16 additions & 12 deletions src/WinUI.TableView/TableView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ private void HandleNavigations(KeyRoutedEventArgs e, bool shiftKey, bool ctrlKey
else if ((e.Key is VirtualKey.Left or VirtualKey.Right or VirtualKey.Up or VirtualKey.Down)
&& !IsEditing)
{
var row = (SelectionUnit is TableViewSelectionUnit.Row ? SelectionStartRowIndex : SelectionStartCellSlot?.Row) ?? -1;
var row = (LastSelectionUnit is TableViewSelectionUnit.Row ? SelectionStartRowIndex : SelectionStartCellSlot?.Row) ?? -1;
var column = CurrentCellSlot?.Column ?? -1;

if (row == -1 && column == -1)
Expand All @@ -168,6 +168,11 @@ private void HandleNavigations(KeyRoutedEventArgs e, bool shiftKey, bool ctrlKey
else if (e.Key is VirtualKey.Left or VirtualKey.Right)
{
column = e.Key is VirtualKey.Left ? ctrlKey ? 0 : column - 1 : ctrlKey ? Columns.VisibleColumns.Count - 1 : column + 1;
if (column >= Columns.VisibleColumns.Count)
{
column = 0;
row++;
}
}
else
{
Expand Down Expand Up @@ -612,11 +617,6 @@ internal void ClearFilters()
}
}

private bool IsValidSlot(TableViewCellSlot slot)
{
return slot.Row >= 0 && slot.Column >= 0 && slot.Row < Items.Count && slot.Column < Columns.VisibleColumns.Count;
}

internal new void SelectAll()
{
if (IsEditing)
Expand Down Expand Up @@ -703,7 +703,7 @@ private void DeselectAllCells()

internal void MakeSelection(TableViewCellSlot slot, bool shiftKey, bool ctrlKey = false)
{
if (slot.Row < 0 || slot.Row >= Items.Count)
if (!slot.IsValidRow(this))
{
return;
}
Expand All @@ -725,13 +725,17 @@ internal void MakeSelection(TableViewCellSlot slot, bool shiftKey, bool ctrlKey
}
}

if (SelectionUnit is TableViewSelectionUnit.Row)
if (SelectionUnit is TableViewSelectionUnit.Row
|| (LastSelectionUnit is TableViewSelectionUnit.Row && slot.IsValidRow(this) && !slot.IsValidColumn(this))
|| (SelectionUnit is TableViewSelectionUnit.CellOrRow && slot.IsValidRow(this) && !slot.IsValidColumn(this)))
{
SelectRows(slot, shiftKey);
LastSelectionUnit = TableViewSelectionUnit.Row;
}
else
{
SelectCells(slot, shiftKey);
LastSelectionUnit = TableViewSelectionUnit.Cell;
}
}
else if (!IsReadOnly)
Expand Down Expand Up @@ -771,7 +775,7 @@ private void SelectRows(TableViewCellSlot slot, bool shiftKey)
}
}

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

private void SelectCells(TableViewCellSlot slot, bool shiftKey)
{
if (!IsValidSlot(slot))
if (!slot.IsValid(this))
{
return;
}
Expand Down Expand Up @@ -896,7 +900,7 @@ private void OnCellSelectionChanged()

internal async Task<TableViewCell> ScrollCellIntoView(TableViewCellSlot slot)
{
if (_scrollViewer is null || !IsValidSlot(slot)) return default!;
if (_scrollViewer is null || !slot.IsValid(this)) return default!;

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

internal TableViewCell GetCellFromSlot(TableViewCellSlot slot)
{
return IsValidSlot(slot) && ContainerFromIndex(slot.Row) is TableViewRow row ? row.Cells[slot.Column] : default!;
return slot.IsValid(this) && ContainerFromIndex(slot.Row) is TableViewRow row ? row.Cells[slot.Column] : default!;
}

private (int start, int end) GetColumnsInDisplay()
Expand Down
12 changes: 11 additions & 1 deletion src/WinUI.TableView/TableViewRow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,17 @@ protected override void OnPointerReleased(PointerRoutedEventArgs e)
if (!KeyBoardHelper.IsShiftKeyDown() && TableView is not null)
{
TableView.SelectionStartCellSlot = null;
TableView.SelectionStartRowIndex = null;
TableView.SelectionStartRowIndex = Index;
}
}

protected override void OnTapped(TappedRoutedEventArgs e)
{
base.OnTapped(e);

if (TableView?.SelectionUnit is TableViewSelectionUnit.Row or TableViewSelectionUnit.CellOrRow)
{
TableView.LastSelectionUnit = TableViewSelectionUnit.Row;
}
}

Expand Down

0 comments on commit e5635c1

Please sign in to comment.