Skip to content
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
22 changes: 16 additions & 6 deletions src/Controls/TableViewFilterItemsControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,7 @@ internal async void Initialize()
/// </summary>
internal void ClearSearchBox()
{
if (searchBox is not null)
{
searchBox.Text = string.Empty;
}
searchBox?.Text = string.Empty;
}

private void OnSearchBoxTextChanged(object sender, TextChangedEventArgs e)
Expand All @@ -67,7 +64,8 @@ private void OnSearchBoxKeyDown(object sender, KeyRoutedEventArgs e)
{
if (e.Key == VirtualKey.Enter && searchBox?.Text.Length > 0)
{
ColumnHeader?.ExecuteOkCommand();
ColumnHeader?.HideFlyout();
ColumnHeader?.ApplyFilter();

e.Handled = true;
}
Expand Down Expand Up @@ -175,7 +173,19 @@ internal ICollection<TableViewFilterItem>? FilterItems
/// <summary>
/// Gets or sets the column header associated with the filter items control.
/// </summary>
public TableViewColumnHeader? ColumnHeader { get; internal set; }
public TableViewColumnHeader? ColumnHeader
{
get;
set
{
if (value is { FilterItemsControl: null })
{
value.FilterItemsControl = this;
}

field = value;
}
}

/// <summary>
/// Gets or sets the TableView associated with the filter items control.
Expand Down
25 changes: 2 additions & 23 deletions src/TableViewColumnHeader.OptionComamnds.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Input;
using WinUI.TableView.Extensions;
using SD = WinUI.TableView.SortDirection;

namespace WinUI.TableView;
Expand All @@ -11,9 +10,7 @@ partial class TableViewColumnHeader
private readonly StandardUICommand _sortAscendingCommand = new() { Label = TableViewLocalizedStrings.SortAscending };
private readonly StandardUICommand _sortDescendingCommand = new() { Label = TableViewLocalizedStrings.SortDescending };
private readonly StandardUICommand _clearSortingCommand = new() { Label = TableViewLocalizedStrings.ClearSorting };
private readonly StandardUICommand _clearFilterCommand = new() { Label = TableViewLocalizedStrings.ClearFilter };
private readonly StandardUICommand _okCommand = new() { Label = TableViewLocalizedStrings.Ok };
private readonly StandardUICommand _cancelCommand = new() { Label = TableViewLocalizedStrings.Cancel };
private readonly StandardUICommand _clearFilterCommand = new() { Label = TableViewLocalizedStrings.ClearFilter };

/// <summary>
/// Sets commands to option menu items.
Expand All @@ -30,15 +27,6 @@ private void SetOptionCommands()
clearSortingMenuItem.Command = _clearSortingCommand;
if (GetTemplateChild("ClearFilterMenuItem") is MenuFlyoutItem clearFilterMenuItem)
clearFilterMenuItem.Command = _clearFilterCommand;
if (GetTemplateChild("ActionButtonsMenuItem") is MenuFlyoutItem actionButtonsMenuItem)
{
actionButtonsMenuItem.ApplyTemplate();

if (actionButtonsMenuItem.FindDescendant<Button>(b => b.Name is "OkButton") is { } okButton)
okButton.Command = _okCommand;
if (actionButtonsMenuItem.FindDescendant<Button>(b => b.Name is "CancelButton") is { } cancelButton)
cancelButton.Command = _cancelCommand;
}
}

/// <summary>
Expand All @@ -62,16 +50,7 @@ private void InitializeCommands()

_clearFilterCommand.ExecuteRequested += delegate { ClearFilter(); };
_clearFilterCommand.CanExecuteRequested += (_, e) => e.CanExecute = Column?.IsFiltered is true;

_okCommand.ExecuteRequested += delegate { ExecuteOkCommand(); };

_cancelCommand.ExecuteRequested += delegate { HideFlyout(); };

_commandsInitialized = true;
}

internal void ExecuteOkCommand()
{
HideFlyout();
ApplyFilter();
}
}
70 changes: 19 additions & 51 deletions src/TableViewColumnHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,15 @@ public partial class TableViewColumnHeader : ContentControl
private TableView? _tableView;
private TableViewHeaderRow? _headerRow;
private Button? _optionsButton;
private MenuFlyout? _optionsFlyout;
private MenuFlyoutItem? _filterItemsMenuItem;
private TableViewFilterMenuFlyout? _optionsFlyout;
private ContentPresenter? _contentPresenter;
private Rectangle? _v_gridLine;
private bool _resizeStarted;
private double _resizeStartingWidth;
private bool _resizePreviousStarted;
private double _reorderStartingPosition;
private bool _reorderStarted;
private RenderTargetBitmap? _dragVisuals;
private TableViewFilterItemsControl? _filterItemsControl;
private RenderTargetBitmap? _dragVisuals;

/// <summary>
/// Initializes a new instance of the TableViewColumnHeader class.
Expand Down Expand Up @@ -156,9 +154,9 @@ private void ClearFilter()
/// <summary>
/// Applies the filter for the column.
/// </summary>
private void ApplyFilter()
internal void ApplyFilter()
{
var shouldApplyFilter = _filterItemsControl?.ShouldApplyFilter ?? false;
var shouldApplyFilter = FilterItemsControl?.ShouldApplyFilter ?? false;

if (!shouldApplyFilter && (Column?.IsFiltered ?? false))
{
Expand All @@ -173,7 +171,7 @@ private void ApplyFilter()

private ICollection<object?> GetSelectedValues()
{
var filterItems = _filterItemsControl?.FilterItems ?? [];
var filterItems = FilterItemsControl?.FilterItems ?? [];
var selectedValues = filterItems.Where(x => x.IsSelected).Select(x => x.Value);
var firstItem = selectedValues.FirstOrDefault(x => x is not null);
var firstItemType = firstItem?.GetType();
Expand All @@ -197,7 +195,7 @@ private void ApplyFilter()
/// <summary>
/// Hides the options flyout.
/// </summary>
private void HideFlyout()
internal void HideFlyout()
{
_optionsFlyout?.Hide();
}
Expand Down Expand Up @@ -231,20 +229,16 @@ protected override void OnApplyTemplate()
{
base.OnApplyTemplate();

_optionsFlyout?.Opening -= OnOptionsFlyoutOpening;
_optionsFlyout?.Closed -= OnOptionsFlyoutClosed;
_optionsButton?.Tapped -= OnOptionsButtonTaped;
_filterItemsMenuItem?.PreviewKeyUp -= OnFilterItemsMenuItemPreviewKeyUp;

_filterItemsControl?.FilterItems = null;
_filterItemsControl?.TableView = null;
_filterItemsControl?.ColumnHeader = null;
_filterItemsControl = null;
FilterItemsControl?.FilterItems = null;
FilterItemsControl?.TableView = null;
FilterItemsControl?.ColumnHeader = null;
FilterItemsControl = null;
_tableView = this.FindAscendant<TableView>();
_headerRow = this.FindAscendant<TableViewHeaderRow>();
_optionsButton = GetTemplateChild("OptionsButton") as Button;
_optionsFlyout = GetTemplateChild("OptionsFlyout") as MenuFlyout;
_filterItemsMenuItem = GetTemplateChild("FilterItemsMenuItem") as MenuFlyoutItem;
_optionsFlyout = GetTemplateChild("OptionsFlyout") as TableViewFilterMenuFlyout;
_contentPresenter = GetTemplateChild("ContentPresenter") as ContentPresenter;
_v_gridLine = GetTemplateChild("VerticalGridLine") as Rectangle;

Expand All @@ -253,39 +247,16 @@ protected override void OnApplyTemplate()
return;
}

_optionsFlyout.Opening += OnOptionsFlyoutOpening;
_optionsFlyout.Closed += OnOptionsFlyoutClosed;
_optionsButton.Tapped += OnOptionsButtonTaped;

_filterItemsMenuItem?.ApplyTemplate();
_filterItemsControl = _filterItemsMenuItem?.FindDescendant<TableViewFilterItemsControl>();

_filterItemsControl?.TableView = _tableView;
_filterItemsControl?.ColumnHeader = this;
_optionsFlyout.TableView = _tableView;
_optionsFlyout.ColumnHeader = this;

_filterItemsMenuItem?.PreviewKeyUp += OnFilterItemsMenuItemPreviewKeyUp;
_optionsButton.Tapped += OnOptionsButtonTaped;

SetOptionCommands();
SetFilterButtonVisibility();
EnsureGridLines();
}

/// <summary>
/// Handles the Opening event for the options flyout.
/// </summary>
private async void OnOptionsFlyoutOpening(object? sender, object e)
{
_filterItemsControl?.Initialize();
}

/// <summary>
/// Handles the Closed event for the options flyout.
/// </summary>
private void OnOptionsFlyoutClosed(object? sender, object e)
{
_filterItemsControl?.ClearSearchBox();
}

/// <summary>
/// Handles the Tapped event for the options button.
/// </summary>
Expand All @@ -294,14 +265,6 @@ private void OnOptionsButtonTaped(object sender, TappedRoutedEventArgs e)
e.Handled = true;
}

/// <summary>
/// Handles the PreviewKeyUp event for the filter items menu item.
/// </summary>
private void OnFilterItemsMenuItemPreviewKeyUp(object sender, KeyRoutedEventArgs e)
{
e.Handled = e.Key is VirtualKey.Space;
}

/// <summary>
/// Handles changes to the SortDirection property.
/// </summary>
Expand Down Expand Up @@ -565,4 +528,9 @@ internal void EnsureGridLines()
/// Gets a value indicating whether the cursor is in the sizing area.
/// </summary>
private bool IsSizingCursor => ProtectedCursor is InputSystemCursor { CursorShape: InputSystemCursorShape.SizeWestEast };

/// <summary>
/// Gets or sets the filter items control associated with the column header.
/// </summary>
internal TableViewFilterItemsControl? FilterItemsControl { get; set; }
}
117 changes: 117 additions & 0 deletions src/TableViewFilterMenuFlyout.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Input;
using System.Collections.Generic;
using System.Linq;
using WinUI.TableView.Controls;
using WinUI.TableView.Extensions;

namespace WinUI.TableView;

/// <summary>
/// Represents the filter menu flyout for a TableViewColumnHeader.
/// </summary>
public partial class TableViewFilterMenuFlyout : Flyout
{
private TableViewFilterItemsControl? _filterItemsControl;
private readonly StandardUICommand _okCommand = new() { Label = TableViewLocalizedStrings.Ok };
private readonly StandardUICommand _cancelCommand = new() { Label = TableViewLocalizedStrings.Cancel };

/// <summary>
/// Initializes a new instance of the <see cref="TableViewFilterMenuFlyout"/> class.
/// </summary>
public TableViewFilterMenuFlyout()
{
Items = [];
Opening += OnOpening;
Closed += OnClosed;

_okCommand.ExecuteRequested += delegate { ExecuteOkCommand(); };
_cancelCommand.ExecuteRequested += delegate { Hide(); };
}

/// <inheritdoc/>
protected override Control CreatePresenter()
{
var presenter = base.CreatePresenter();
presenter.Style = FlyoutPresenterStyle;
presenter.Loaded += OnPresenterLoaded;

return presenter;
}

/// <summary>
/// Handles the Opening event of the flyout, initializing the filter items control.
/// </summary>
private void OnOpening(object? sender, object e)
{
_filterItemsControl?.Initialize();
}

/// <summary>
/// Handles the Closed event of the flyout, clearing the search box in the filter items control.
/// </summary>
private void OnClosed(object? sender, object e)
{
_filterItemsControl?.ClearSearchBox();
}

/// <summary>
/// Handles the Loaded event of the MenuFlyoutPresenter.
/// </summary>
private void OnPresenterLoaded(object sender, RoutedEventArgs e)
{
var presenter = (FlyoutPresenter)sender;
var okButton = presenter.FindDescendant<Button>(b => b.Name is "OkButton");
var cancelButton = presenter.FindDescendant<Button>(b => b.Name is "CancelButton");
var menuPresenter = presenter.FindDescendant<MenuFlyoutPresenter>();
menuPresenter?.ItemsSource = Items;

_filterItemsControl = presenter.FindDescendant<TableViewFilterItemsControl>();
_filterItemsControl?.TableView = TableView;
_filterItemsControl?.ColumnHeader = ColumnHeader;

okButton?.Command = _okCommand;
cancelButton?.Command = _cancelCommand;

presenter.Loaded -= OnPresenterLoaded;
_filterItemsControl?.Initialize();

foreach (var item in Items.OfType<MenuFlyoutItem>())
{
item.Tapped += OnMenuItemTapped;
}
}

/// <summary>
/// Handles the Tapped event of menu items.
/// </summary>
private void OnMenuItemTapped(object sender, TappedRoutedEventArgs e)
{
Hide();
}

/// <summary>
/// Executes the OK command, applying the filter and hiding the flyout.
/// </summary>
private void ExecuteOkCommand()
{
Hide();
ColumnHeader?.ApplyFilter();
}

/// <summary>
/// Gets or sets the TableView associated with this filter menu flyout.
/// </summary>
public TableView? TableView { get; set; }

/// <summary>
/// Gets or sets the TableViewColumnHeader associated with this filter menu flyout.
/// </summary>
public TableViewColumnHeader? ColumnHeader { get; set; }

/// <summary>
/// Gets or sets the collection of menu items to be displayed in the filter flyout.
/// </summary>
public IList<MenuFlyoutItemBase> Items { get; set; }
}
Loading
Loading