Skip to content

Commit

Permalink
fixing up selection behavior based on selection modes
Browse files Browse the repository at this point in the history
  • Loading branch information
w-ahmad committed Jun 18, 2024
1 parent e388865 commit 9070ba6
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 15 deletions.
10 changes: 6 additions & 4 deletions src/WinUI.TableView/TableView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ protected override void OnPreviewKeyDown(KeyRoutedEventArgs e)

if (isEditing && currentCell is not null)
{
currentCell.SetElement();
currentCell = GetCellFromSlot(newSlot);
currentCell.PrepareForEdit();
}
Expand Down Expand Up @@ -655,9 +654,10 @@ internal async void SelectCells(TableViewCellSlot slot, bool shiftKey, bool ctrl
SelectedCellRanges.Remove(selectionRange);
selectionRange.Clear();
SelectionStartCellSlot ??= CurrentCellSlot;
SelectionStartCellSlot ??= slot;
if (shiftKey && SelectionMode is ListViewSelectionMode.Multiple or ListViewSelectionMode.Extended)
{
var currentSlot = SelectionStartCellSlot ?? slot;
var currentSlot = SelectionStartCellSlot.Value;
var startRow = Math.Min(slot.Row, currentSlot.Row);
var endRow = Math.Max(slot.Row, currentSlot.Row);
var startCol = Math.Min(slot.Column, currentSlot.Column);
Expand All @@ -677,7 +677,7 @@ internal async void SelectCells(TableViewCellSlot slot, bool shiftKey, bool ctrl
}
else
{
SelectionStartCellSlot = null;
SelectionStartCellSlot = slot;
selectionRange.Add(slot);

if (SelectedCellRanges.LastOrDefault(x => x.Contains(slot)) is { } range)
Expand Down Expand Up @@ -713,6 +713,8 @@ internal void DeselectCell(TableViewCellSlot slot)
internal void SetCurrentCell(TableViewCellSlot? slot)
{
var oldSlot = CurrentCellSlot;
var currentCell = oldSlot.HasValue ? GetCellFromSlot(oldSlot.Value) : default;
currentCell?.SetElement();
CurrentCellSlot = slot;
CurrentCellChanged?.Invoke(this, new TableViewCurrentCellChangedEventArgs(oldSlot, slot));
}
Expand Down Expand Up @@ -832,7 +834,7 @@ void ViewChanged(object? _, ScrollViewerViewChangedEventArgs e)
return default;
}

private TableViewCell GetCellFromSlot(TableViewCellSlot slot)
internal TableViewCell GetCellFromSlot(TableViewCellSlot slot)
{
return ContainerFromIndex(slot.Row) is TableViewRow row ? row.Cells[slot.Column] : default!;
}
Expand Down
4 changes: 2 additions & 2 deletions src/WinUI.TableView/TableViewCell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,6 @@ protected override void OnPointerPressed(PointerRoutedEventArgs e)
{
TableView.SelectionStartCellSlot = Slot;
}

e.Handled = TableView.SelectionUnit != TableViewSelectionUnit.Row;
}

protected override void OnPointerReleased(PointerRoutedEventArgs e)
Expand All @@ -94,6 +92,8 @@ protected override void OnPointerReleased(PointerRoutedEventArgs e)
{
TableView.SelectionStartCellSlot = null;
}

e.Handled = TableView.SelectionUnit != TableViewSelectionUnit.Row;
}

protected override void OnPointerMoved(PointerRoutedEventArgs e)
Expand Down
9 changes: 0 additions & 9 deletions src/WinUI.TableView/TableViewRow.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
Expand All @@ -10,21 +9,13 @@ namespace WinUI.TableView;
public class TableViewRow : ListViewItem
{
private TableViewCellPresenter? _cellPresenter;
private ListViewItemPresenter _itemPresenter = null!;

public TableViewRow()
{
DefaultStyleKey = typeof(TableViewRow);
SizeChanged += OnSizeChanged;
}

protected override void OnApplyTemplate()
{
base.OnApplyTemplate();

_itemPresenter = (ListViewItemPresenter)GetTemplateChild("Root");
}

protected override void OnContentChanged(object oldContent, object newContent)
{
if (TableView is null)
Expand Down
2 changes: 2 additions & 0 deletions src/WinUI.TableView/Themes/TableView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
Value="{ThemeResource TableViewGridLinesBrush}" />
<Setter Property="BorderThickness"
Value="1" />
<Setter Property="SingleSelectionFollowsFocus"
Value="False" />
<Setter Property="ItemContainerTransitions">
<Setter.Value>
<TransitionCollection>
Expand Down

0 comments on commit 9070ba6

Please sign in to comment.