Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed CheckBox & ToggleSwitch column IsReadOnly state issues #44

Merged
merged 5 commits into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/WinUI.TableView/TableView.Properties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,14 @@ private static void OnAutoGenerateColumnsChanged(DependencyObject d, DependencyP
}
}

private static void OnIsReadOnlyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is TableView tableView)
{
tableView.IsReadOnlyChanged?.Invoke(d, e);
}
}

private static void OnCanSortColumnsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is TableView tableView && e.NewValue is false)
Expand Down
8 changes: 4 additions & 4 deletions src/WinUI.TableView/TableView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public TableView()
{
DefaultStyleKey = typeof(TableView);

Columns.TableView = this;
CollectionView.Filter = Filter;
base.ItemsSource = CollectionView;
base.SelectionMode = SelectionMode;
Expand Down Expand Up @@ -84,10 +85,8 @@ protected override DependencyObject GetContainerForItemOverride()
return new TableViewRow { TableView = this };
}

protected override void OnPreviewKeyDown(KeyRoutedEventArgs e)
protected override void OnKeyDown(KeyRoutedEventArgs e)
{
base.OnPreviewKeyDown(e);

var shiftKey = KeyBoardHelper.IsShiftKeyDown();
var ctrlKey = KeyBoardHelper.IsCtrlKeyDown();
var currentCell = CurrentCellSlot.HasValue ? GetCellFromSlot(CurrentCellSlot.Value) : default;
Expand Down Expand Up @@ -881,7 +880,6 @@ void ViewChanged(object? _, ScrollViewerViewChangedEventArgs e)
}
}

row.Focus(FocusState.Programmatic);
return row;
}

Expand Down Expand Up @@ -935,6 +933,8 @@ private void UpdateBaseSelectionMode()
public event EventHandler<TableViewExportContentEventArgs>? ExportAllContent;
public event EventHandler<TableViewExportContentEventArgs>? ExportSelectedContent;
public event EventHandler<TableViewCopyToClipboardEventArgs>? CopyToClipboard;
public event DependencyPropertyChangedEventHandler? IsReadOnlyChanged;

internal event EventHandler<TableViewCellSelectionChangedEvenArgs>? SelectedCellsChanged;
internal event EventHandler<TableViewCurrentCellChangedEventArgs>? CurrentCellChanged;
}
107 changes: 78 additions & 29 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 @@ -106,25 +111,7 @@ protected override void OnTapped(TappedRoutedEventArgs e)
{
base.OnTapped(e);

if (TableView.IsEditing && TableView.CurrentCellSlot == Slot)
{
return;
}

var shiftKey = KeyBoardHelper.IsShiftKeyDown();
var ctrlKey = KeyBoardHelper.IsCtrlKeyDown();

if (IsSelected && (ctrlKey || TableView.SelectionMode is ListViewSelectionMode.Multiple) && !shiftKey)
{
TableView.DeselectCell(Slot);
}
else
{
TableView.IsEditing = false;
TableView.SelectCells(Slot, shiftKey, ctrlKey);
}

Focus(FocusState.Programmatic);
MakeSelection();
}

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

Expand All @@ -146,33 +134,77 @@ 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) && IsCurrent)
{
return;
}

if (IsSelected && (ctrlKey || TableView.SelectionMode is ListViewSelectionMode.Multiple) && !shiftKey)
{
TableView.DeselectCell(Slot);
}
else
{
if (Column.UseSingleElement)
{
TableView.DeselectCell(Slot);
}

TableView.IsEditing = false;
TableView.SelectCells(Slot, shiftKey, ctrlKey);
}
}

internal async void PrepareForEdit()
{
SetEditingElement();
Expand All @@ -192,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 @@ -209,6 +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
26 changes: 23 additions & 3 deletions src/WinUI.TableView/TableViewCheckBoxColumn.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,49 @@
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using System;

namespace WinUI.TableView;

public class TableViewCheckBoxColumn : TableViewBoundColumn
{
public TableViewCheckBoxColumn()
{
UseSingleElement = true;
}

public override FrameworkElement GenerateElement()
{
var checkBox = new CheckBox
{
MinWidth = 20,
MaxWidth = 20,
IsEnabled = !IsReadOnly,
Margin = new Thickness(12, 0, 12, 0),
HorizontalAlignment = HorizontalAlignment.Center
HorizontalAlignment = HorizontalAlignment.Center,
UseSystemFocusVisuals = false,
};

checkBox.SetBinding(ToggleButton.IsCheckedProperty, Binding);
UpdateCheckBoxState(checkBox);

return checkBox;
}

public override FrameworkElement GenerateEditingElement()
{
return GenerateElement();
throw new NotImplementedException();
}

public override void UpdateElementState(TableViewCell cell)
{
if (cell?.Content is CheckBox checkBox)
{
UpdateCheckBoxState(checkBox);
}
}

private void UpdateCheckBoxState(CheckBox checkBox)
{
checkBox.IsHitTestVisible = TableView?.IsReadOnly is false && !IsReadOnly;
}
}
22 changes: 19 additions & 3 deletions src/WinUI.TableView/TableViewColumn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,25 @@ namespace WinUI.TableView;
[StyleTypedProperty(Property = nameof(HeaderStyle), StyleTargetType = typeof(TableViewColumnHeader))]
public abstract class TableViewColumn : DependencyObject
{
protected TableView? TableView { get; private set; }
private TableViewColumnsCollection? _owningCollection;
private TableViewColumnHeader? _headerControl;
private double _desiredWidth;

public abstract FrameworkElement GenerateElement();
public abstract FrameworkElement GenerateEditingElement();
public virtual void UpdateElementState(TableViewCell cell) { }

internal void SetOwingCollection(TableViewColumnsCollection collection)
internal void SetOwningCollection(TableViewColumnsCollection collection)
{
_owningCollection = collection;
}

internal void SetOwningTableView(TableView tableView)
{
TableView = tableView;
}

public object Header
{
get => GetValue(HeaderProperty);
Expand Down Expand Up @@ -97,6 +104,8 @@ internal double DesiredWidth

public bool IsAutoGenerated { get; internal set; }

public bool UseSingleElement { get; set; }

private void EnsureHeaderStyle()
{
if (_headerControl is not null && HeaderStyle is not null)
Expand All @@ -105,7 +114,6 @@ private void EnsureHeaderStyle()
}
}


private static void OnWidthChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is TableViewColumn column && column._owningCollection is { })
Expand Down Expand Up @@ -138,6 +146,14 @@ private static void OnActualWidthChanged(DependencyObject d, DependencyPropertyC
}
}

private static void OnIsReadOnlyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is TableViewColumn column && column._owningCollection is { })
{
column._owningCollection.HandleColumnPropertyChanged(column, nameof(IsReadOnly));
}
}

private static void OnVisibilityChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is TableViewColumn column && column._owningCollection is { })
Expand All @@ -153,6 +169,6 @@ private static void OnVisibilityChanged(DependencyObject d, DependencyPropertyCh
public static readonly DependencyProperty MaxWidthProperty = DependencyProperty.Register(nameof(MaxWidth), typeof(double?), typeof(TableViewColumn), new PropertyMetadata(default, OnMaxWidthChanged));
public static readonly DependencyProperty ActualWidthProperty = DependencyProperty.Register(nameof(ActualWidth), typeof(double), typeof(TableViewColumn), new PropertyMetadata(0d, OnActualWidthChanged));
public static readonly DependencyProperty CanResizeProperty = DependencyProperty.Register(nameof(CanResize), typeof(bool), typeof(TableViewColumn), new PropertyMetadata(true));
public static readonly DependencyProperty IsReadOnlyProperty = DependencyProperty.Register(nameof(IsReadOnly), typeof(bool), typeof(TableViewColumn), new PropertyMetadata(false));
public static readonly DependencyProperty IsReadOnlyProperty = DependencyProperty.Register(nameof(IsReadOnly), typeof(bool), typeof(TableViewColumn), new PropertyMetadata(false, OnIsReadOnlyChanged));
public static readonly DependencyProperty VisibilityProperty = DependencyProperty.Register(nameof(Visibility), typeof(Visibility), typeof(TableViewColumn), new PropertyMetadata(Visibility.Visible, OnVisibilityChanged));
}
8 changes: 6 additions & 2 deletions src/WinUI.TableView/TableViewColumnsColection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,17 @@ protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
{
foreach (var column in e.NewItems.OfType<TableViewColumn>())
{
column.SetOwingCollection(this);
column.SetOwningCollection(this);
column.SetOwningTableView(TableView!);
}
}

if (e.OldItems != null)
{
foreach (var column in e.OldItems.OfType<TableViewColumn>())
{
column.SetOwingCollection(null!);
column.SetOwningCollection(null!);
column.SetOwningTableView(null!);
}
}
}
Expand All @@ -41,6 +43,8 @@ internal void HandleColumnPropertyChanged(TableViewColumn column, string propert
ColumnPropertyChanged?.Invoke(this, new TableViewColumnPropertyChanged(column, propertyName, index));
}
}

public TableView? TableView { get; internal set; }
}

internal class TableViewColumnPropertyChanged : EventArgs
Expand Down
Loading