Skip to content

Commit

Permalink
fixing cell navigation issues
Browse files Browse the repository at this point in the history
  • Loading branch information
w-ahmad committed Sep 9, 2024
1 parent 14d2d04 commit c9a4998
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 14 deletions.
63 changes: 49 additions & 14 deletions src/WinUI.TableView/TableViewCell.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using Microsoft.UI.Xaml;
using CommunityToolkit.WinUI;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media;
using System;
using System.Linq;
using System.Threading.Tasks;
using Windows.Foundation;

Expand All @@ -15,11 +18,13 @@ namespace WinUI.TableView;
[TemplateVisualState(Name = VisualStates.StateUnselected, GroupName = VisualStates.GroupSelection)]
public class TableViewCell : ContentControl
{
private ScrollViewer? _scrollViewer;
private ContentPresenter? _contentPresenter;

public TableViewCell()
{
DefaultStyleKey = typeof(TableViewCell);
ManipulationMode = ManipulationModes.TranslateX | ManipulationModes.TranslateY;
Loaded += OnLoaded;
}

Expand Down Expand Up @@ -107,7 +112,7 @@ protected override void OnTapped(TappedRoutedEventArgs e)
base.OnTapped(e);

MakeSelection();
}
}

protected override void OnPointerPressed(PointerRoutedEventArgs e)
{
Expand All @@ -116,6 +121,7 @@ protected override void OnPointerPressed(PointerRoutedEventArgs e)
if (!KeyBoardHelper.IsShiftKeyDown())
{
TableView.SelectionStartCellSlot = Slot;
CapturePointer(e.Pointer);
}
}

Expand All @@ -128,40 +134,60 @@ protected override void OnPointerReleased(PointerRoutedEventArgs e)
TableView.SelectionStartCellSlot = null;
}

ReleasePointerCaptures();

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

protected override void OnPointerMoved(PointerRoutedEventArgs e)
protected override void OnManipulationDelta(ManipulationDeltaRoutedEventArgs e)
{
base.OnPointerMoved(e);
base.OnManipulationDelta(e);

var point = e.GetCurrentPoint(this);
_scrollViewer ??= TableView.FindDescendant<ScrollViewer>();

if (point.Properties.IsLeftButtonPressed && !TableView.IsEditing)
if (PointerCaptures?.Any() is true && _scrollViewer is { })
{
var ctrlKey = KeyBoardHelper.IsCtrlKeyDown();

TableView.SelectCells(Slot, true, ctrlKey);
var transform = _scrollViewer.TransformToVisual(this).Inverse;
var point = transform.TransformPoint(e.Position);
var transformedPoint = _scrollViewer.TransformToVisual(null).TransformPoint(point);
var cell = VisualTreeHelper.FindElementsInHostCoordinates(transformedPoint, _scrollViewer)
.OfType<TableViewCell>()
.FirstOrDefault();

if (cell is not null && cell != this)
{
var ctrlKey = KeyBoardHelper.IsCtrlKeyDown();
TableView.SelectCells(cell.Slot, true, ctrlKey);
}
}
}

protected override void OnDoubleTapped(DoubleTappedRoutedEventArgs e)
{
if (!IsReadOnly)
if (!IsReadOnly && !TableView.IsEditing && !Column.UseSingleElement)
{
PrepareForEdit();

TableView.IsEditing = true;
}
}

protected override void OnGotFocus(RoutedEventArgs e)
{
base.OnGotFocus(e);

MakeSelection();
}

private void MakeSelection()
{
var shiftKey = KeyBoardHelper.IsShiftKeyDown();
var ctrlKey = KeyBoardHelper.IsCtrlKeyDown();

if ((TableView.IsEditing || Column.UseSingleElement) && TableView.CurrentCellSlot == Slot)
if ((TableView.IsEditing || Column.UseSingleElement) && IsCurrent)
{
return;
}
}

if (IsSelected && (ctrlKey || TableView.SelectionMode is ListViewSelectionMode.Multiple) && !shiftKey)
{
Expand Down Expand Up @@ -198,7 +224,11 @@ internal void SetElement()

private void SetEditingElement()
{
Content = Column.GenerateEditingElement();
if (!Column.UseSingleElement)
{
Content = Column.GenerateEditingElement();
}

if (TableView is not null)
{
TableView.IsEditing = true;
Expand All @@ -215,14 +245,19 @@ internal void ApplyCurrentCellState()
{
var stateName = IsCurrent ? VisualStates.StateCurrent : VisualStates.StateRegular;
VisualStates.GoToState(this, false, stateName);

if (IsCurrent && (Content ?? ContentTemplateRoot) is UIElement element)
{
element.Focus(FocusState.Programmatic);
}
}

internal void UpdateElementState()
{
if (Column is { })
{
Column.UpdateElementState(this);
}
}
}

private static void OnColumnChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
Expand Down
1 change: 1 addition & 0 deletions src/WinUI.TableView/TableViewCheckBoxColumn.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using System;

namespace WinUI.TableView;

Expand Down
1 change: 1 addition & 0 deletions src/WinUI.TableView/TableViewToggleSwitchColumn.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using System;

namespace WinUI.TableView;

Expand Down

0 comments on commit c9a4998

Please sign in to comment.