Skip to content

Commit

Permalink
Merge pull request #85 from w-ahmad/feature/ContextFlyouts
Browse files Browse the repository at this point in the history
added cell and row context flyouts
  • Loading branch information
w-ahmad authored Dec 22, 2024
2 parents 0d69511 + 3d93144 commit 16df2ae
Show file tree
Hide file tree
Showing 7 changed files with 127 additions and 1 deletion.
15 changes: 15 additions & 0 deletions src/WinUI.TableView/TableView.Properties.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using CommunityToolkit.WinUI.Collections;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Media;
using System;
using System.Collections;
Expand Down Expand Up @@ -32,6 +33,8 @@ public partial class TableView
public static readonly DependencyProperty VerticalGridLinesStrokeProperty = DependencyProperty.Register(nameof(VerticalGridLinesStroke), typeof(Brush), typeof(TableView), new PropertyMetadata(default, OnGridLinesPropertyChanged));
public static readonly DependencyProperty AlternateRowForegroundProperty = DependencyProperty.Register(nameof(AlternateRowForeground), typeof(Brush), typeof(TableView), new PropertyMetadata(null, OnAlternateRowColorChanged));
public static readonly DependencyProperty AlternateRowBackgroundProperty = DependencyProperty.Register(nameof(AlternateRowBackground), typeof(Brush), typeof(TableView), new PropertyMetadata(null, OnAlternateRowColorChanged));
public static readonly DependencyProperty RowContextFlyoutProperty = DependencyProperty.Register(nameof(RowContextFlyout), typeof(FlyoutBase), typeof(TableView), new PropertyMetadata(null));
public static readonly DependencyProperty CellContextFlyoutProperty = DependencyProperty.Register(nameof(CellContextFlyout), typeof(FlyoutBase), typeof(TableView), new PropertyMetadata(null));

public IAdvancedCollectionView CollectionView { get; private set; } = new AdvancedCollectionView();
internal IDictionary<string, Predicate<object>> ActiveFilters { get; } = new Dictionary<string, Predicate<object>>();
Expand Down Expand Up @@ -184,6 +187,18 @@ public Brush AlternateRowForeground
set => SetValue(AlternateRowForegroundProperty, value);
}

public FlyoutBase? RowContextFlyout
{
get => (FlyoutBase?)GetValue(RowContextFlyoutProperty);
set => SetValue(RowContextFlyoutProperty, value);
}

public FlyoutBase? CellContextFlyout
{
get => (FlyoutBase?)GetValue(CellContextFlyoutProperty);
set => SetValue(CellContextFlyoutProperty, value);
}

private static void OnItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is TableView tableView)
Expand Down
38 changes: 38 additions & 0 deletions src/WinUI.TableView/TableView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1074,11 +1074,49 @@ private void EnsureAlternateRowColors()
}
}

internal void ShowRowContext(TableViewRow row, Point position)
{
if (RowContextFlyout is null) return;

var eventArgs = new TableViewRowContextFlyoutEventArgs(row.Index, row, row.Content, RowContextFlyout);
RowContextFlyoutOpening?.Invoke(this, eventArgs);

if (!eventArgs.Handled)
{
RowContextFlyout.ShowAt(row, new FlyoutShowOptions
{
ShowMode = FlyoutShowMode.Standard,
Placement = RowContextFlyout.Placement,
Position = position
});
}
}

internal void ShowCellContext(TableViewCell cell, Point position)
{
if (CellContextFlyout is null) return;

var eventArgs = new TableViewCellContextFlyoutEventArgs(cell.Slot, cell, cell.Row?.Content!, CellContextFlyout);
CellContextFlyoutOpening?.Invoke(this, eventArgs);

if (!eventArgs.Handled)
{
CellContextFlyout.ShowAt(cell, new FlyoutShowOptions
{
ShowMode = FlyoutShowMode.Standard,
Placement = CellContextFlyout.Placement,
Position = position
});
}
}

public event EventHandler<TableViewAutoGeneratingColumnEventArgs>? AutoGeneratingColumn;
public event EventHandler<TableViewExportContentEventArgs>? ExportAllContent;
public event EventHandler<TableViewExportContentEventArgs>? ExportSelectedContent;
public event EventHandler<TableViewCopyToClipboardEventArgs>? CopyToClipboard;
public event DependencyPropertyChangedEventHandler? IsReadOnlyChanged;
public event EventHandler<TableViewRowContextFlyoutEventArgs>? RowContextFlyoutOpening;
public event EventHandler<TableViewCellContextFlyoutEventArgs>? CellContextFlyoutOpening;

internal event EventHandler<TableViewCellSelectionChangedEvenArgs>? SelectedCellsChanged;
internal event EventHandler<TableViewCurrentCellChangedEventArgs>? CurrentCellChanged;
Expand Down
9 changes: 9 additions & 0 deletions src/WinUI.TableView/TableViewCell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ public TableViewCell()
DefaultStyleKey = typeof(TableViewCell);
ManipulationMode = ManipulationModes.TranslateX | ManipulationModes.TranslateY;
Loaded += OnLoaded;
ContextRequested += OnContextRequested;
}

private void OnContextRequested(UIElement sender, ContextRequestedEventArgs args)
{
if (TableView is not null && args.TryGetPosition(sender, out var position))
{
TableView.ShowCellContext(this, position);
}
}

private void OnLoaded(object sender, RoutedEventArgs e)
Expand Down
20 changes: 20 additions & 0 deletions src/WinUI.TableView/TableViewCellContextFlyoutEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Microsoft.UI.Xaml.Controls.Primitives;
using System.ComponentModel;

namespace WinUI.TableView;

public partial class TableViewCellContextFlyoutEventArgs : HandledEventArgs
{
public TableViewCellContextFlyoutEventArgs(TableViewCellSlot slot, TableViewCell cell, object item, FlyoutBase flyout)
{
Slot = slot;
Cell = cell;
Item = item;
Flyout = flyout;
}

public TableViewCellSlot Slot { get; }
public TableViewCell Cell { get; }
public object Item { get; }
public FlyoutBase Flyout { get; }
}
2 changes: 1 addition & 1 deletion src/WinUI.TableView/TableViewCellSlot.cs
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
namespace WinUI.TableView;
internal readonly record struct TableViewCellSlot(int Row, int Column);
public readonly record struct TableViewCellSlot(int Row, int Column);
24 changes: 24 additions & 0 deletions src/WinUI.TableView/TableViewRow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,33 @@ public TableViewRow()

SizeChanged += OnSizeChanged;
Loaded += TableViewRow_Loaded;
ContextRequested += OnContextRequested;
RegisterPropertyChangedCallback(IsSelectedProperty, delegate { OnIsSelectedChanged(); });
}

private void OnContextRequested(UIElement sender, ContextRequestedEventArgs args)
{
if (args.TryGetPosition(sender, out var position))
{
if (IsContextRequestedFromCell(position) && TableView?.CellContextFlyout is not null) return;


TableView?.ShowRowContext(this, position);
}
}

private bool IsContextRequestedFromCell(Windows.Foundation.Point position)
{
if (_cellPresenter is null) return false;

var transform = _cellPresenter.TransformToVisual(this).Inverse;
var point = transform.TransformPoint(position);
var transformedPoint = _cellPresenter.TransformToVisual(null).TransformPoint(point);
return VisualTreeHelper.FindElementsInHostCoordinates(transformedPoint, _cellPresenter)
.OfType<TableViewCell>()
.Any();
}

private void OnIsSelectedChanged()
{
DispatcherQueue.TryEnqueue(() =>
Expand Down
20 changes: 20 additions & 0 deletions src/WinUI.TableView/TableViewRowContextFlyoutEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Microsoft.UI.Xaml.Controls.Primitives;
using System.ComponentModel;

namespace WinUI.TableView;

public partial class TableViewRowContextFlyoutEventArgs : HandledEventArgs
{
public TableViewRowContextFlyoutEventArgs(int index, TableViewRow row, object item, FlyoutBase flyout)
{
Index = index;
Row = row;
Item = item;
Flyout = flyout;
}

public int Index { get; }
public TableViewRow Row { get; }
public object Item { get; }
public FlyoutBase Flyout { get; }
}

0 comments on commit 16df2ae

Please sign in to comment.