Skip to content

Commit

Permalink
Merge pull request #11 from w-ahmad/dev
Browse files Browse the repository at this point in the history
merge dev branch into main
  • Loading branch information
w-ahmad authored May 19, 2024
2 parents 3cb14af + 9d0a72c commit 6394b50
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 19 deletions.
22 changes: 21 additions & 1 deletion src/WinUI.TableView/TableView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Microsoft.UI;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Data;
using Microsoft.UI.Xaml.Hosting;
using Microsoft.UI.Xaml.Input;
Expand Down Expand Up @@ -55,6 +56,8 @@ private void OnLoaded(object sender, RoutedEventArgs e)
itemsPanelVisual.Clip = contentClip;
contentClip.TopInset = (float)Math.Max(-scrollViewer.VerticalOffset, 0);
contentClip.StartAnimation("TopInset", expressionClipAnimation);

UpdateVerticalScrollBarMargin();
}

private bool Filter(object obj)
Expand Down Expand Up @@ -344,6 +347,11 @@ private static void OnItemsSourceChanged(DependencyObject d, DependencyPropertyC
}
}

private static void OnHeaderRowHeightChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
(d as TableView)?.UpdateVerticalScrollBarMargin();
}

private static void OnAutoGenerateColumnsChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is TableView tableView)
Expand Down Expand Up @@ -375,6 +383,18 @@ private static void OnCanFilterColumnsChanged(DependencyObject d, DependencyProp
}
}

private void UpdateVerticalScrollBarMargin()
{
if (GetTemplateChild("ScrollViewer") is ScrollViewer scrollViewer)
{
var verticalScrollBar = scrollViewer.FindDescendant<ScrollBar>(x => x.Name == "VerticalScrollBar");
if (verticalScrollBar is not null)
{
verticalScrollBar.Margin = new Thickness(0, HeaderRowHeight, 0, 0);
}
}
}

internal void ClearSorting()
{
CollectionView.SortDescriptions.Clear();
Expand Down Expand Up @@ -480,7 +500,7 @@ public bool CanFilterColumns

public static readonly new DependencyProperty ItemsSourceProperty = DependencyProperty.Register(nameof(ItemsSource), typeof(IList), typeof(TableView), new PropertyMetadata(null, OnItemsSourceChanged));
public static readonly DependencyProperty ColumnsProperty = DependencyProperty.Register(nameof(Columns), typeof(TableViewColumnsCollection), typeof(TableView), new PropertyMetadata(null));
public static readonly DependencyProperty HeaderRowHeightProperty = DependencyProperty.Register(nameof(HeaderRowHeight), typeof(double), typeof(TableView), new PropertyMetadata(48d));
public static readonly DependencyProperty HeaderRowHeightProperty = DependencyProperty.Register(nameof(HeaderRowHeight), typeof(double), typeof(TableView), new PropertyMetadata(32d, OnHeaderRowHeightChanged));
public static readonly DependencyProperty RowHeightProperty = DependencyProperty.Register(nameof(RowHeight), typeof(double), typeof(TableView), new PropertyMetadata(40d));
public static readonly DependencyProperty RowMaxHeightProperty = DependencyProperty.Register(nameof(RowMaxHeight), typeof(double), typeof(TableView), new PropertyMetadata(double.PositiveInfinity));
public static readonly DependencyProperty ShowExportOptionsProperty = DependencyProperty.Register(nameof(ShowExportOptions), typeof(bool), typeof(TableView), new PropertyMetadata(false));
Expand Down
11 changes: 9 additions & 2 deletions src/WinUI.TableView/TableViewCell.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using CommunityToolkit.WinUI;
using Microsoft.UI.Input;
using Microsoft.UI.Input;
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;
using Windows.System;
using Windows.UI.Core;

Expand All @@ -22,6 +22,13 @@ public TableViewCell()
DefaultStyleKey = typeof(TableViewCell);
}

protected override Size MeasureOverride(Size availableSize)
{
var size = base.MeasureOverride(availableSize);
Column.DesiredWidth = Math.Max(Column.DesiredWidth, size.Width);
return size;
}

protected override void OnDoubleTapped(DoubleTappedRoutedEventArgs e)
{
if (!IsReadOnly)
Expand Down
2 changes: 2 additions & 0 deletions src/WinUI.TableView/TableViewColumn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ private void EnsureHeaderStyle()
}
}

internal double DesiredWidth { get; set; }

public static readonly DependencyProperty HeaderStyleProperty = DependencyProperty.Register(nameof(HeaderStyle), typeof(Style), typeof(TableViewColumn), new PropertyMetadata(null, (d, _) => ((TableViewColumn)d).EnsureHeaderStyle()));
public static readonly DependencyProperty HeaderProperty = DependencyProperty.Register(nameof(Header), typeof(object), typeof(TableViewColumn), new PropertyMetadata(null));
public static readonly DependencyProperty WidthProperty = DependencyProperty.Register(nameof(Width), typeof(double), typeof(TableViewColumn), new PropertyMetadata(200d));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,20 @@ private void InitializeCommands()

OkCommand.ExecuteRequested += delegate
{
SelectedValues = FilterItems.Where(x => x.IsSelected).Select(x => x.Value).ToList();
ColumnHeader.ApplyFilter();
ColumnHeader.HideFlyout();

if (ColumnHeader!._selectAllCheckBox!.IsChecked is true)
{
ColumnHeader.ClearFilter();
}
else
{
SelectedValues = FilterItems.Where(x => x.IsSelected).Select(x => x.Value).ToList();
ColumnHeader.ApplyFilter();
}
};

CancelCommand.ExecuteRequested += delegate { ColumnHeader._optionsFlyout?.Hide(); };
CancelCommand.ExecuteRequested += delegate { ColumnHeader.HideFlyout(); };
}

public TableView TableView { get; }
Expand Down
39 changes: 33 additions & 6 deletions src/WinUI.TableView/TableViewColumnHeader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,16 @@ private void ApplyFilter()
return;
}

_optionsFlyout?.Hide();
_tableView.ActiveFilters[_propertyPath] = Filter;
_tableView.CollectionView.RefreshFilter();
IsFiltered = true;
}

private void HideFlyout()
{
_optionsFlyout?.Hide();
}

private bool Filter(object item)
{
var value = GetValue(item);
Expand Down Expand Up @@ -221,10 +225,9 @@ private void PrepareFilterItems(string? _filterText)
{
var value = GetValue(item);
value = string.IsNullOrWhiteSpace(value?.ToString()) ? "(Blank)" : value;
var isSelected = !string.IsNullOrEmpty(_filterText) ||
var isSelected = !isFiltered || !string.IsNullOrEmpty(_filterText) ||
(isFiltered && _optionsFlyoutViewModel.SelectedValues.Contains(value));


return string.IsNullOrEmpty(_filterText)
|| value?.ToString()?.Contains(_filterText, StringComparison.OrdinalIgnoreCase) == true
? new FilterItem(isSelected, value, _optionsFlyoutViewModel)
Expand Down Expand Up @@ -291,9 +294,10 @@ private void SetFilterButtonVisibility()

private bool IsCursorInRightResizeArea(PointerRoutedEventArgs args)
{
var resizeArea = args.Pointer.PointerDeviceType == PointerDeviceType.Touch ? 8 : 4;
var resizeWidth = args.Pointer.PointerDeviceType == PointerDeviceType.Touch ? 8 : 4;
var point = args.GetCurrentPoint(this);
return ActualWidth - point.Position.X <= resizeArea && point.Position.Y < ActualHeight - (_optionsButton?.ActualHeight ?? 0);
var resizeHeight = ActualHeight - (CanFilter ? _optionsButton?.ActualHeight ?? 0 : 0);
return ActualWidth - point.Position.X <= resizeWidth && point.Position.Y < resizeHeight;
}

private bool IsCursorInLeftResizeArea(PointerRoutedEventArgs args)
Expand All @@ -303,6 +307,29 @@ private bool IsCursorInLeftResizeArea(PointerRoutedEventArgs args)
return point.Position.X <= resizeArea && point.Position.Y < ActualHeight;
}

protected override void OnDoubleTapped(DoubleTappedRoutedEventArgs e)
{
base.OnDoubleTapped(e);

if (IsSizingCursor && Column is not null)
{
var position = e.GetPosition(this);

if (position.X <= 8 && _headerRow?.GetPreviousHeader(this) is { Column: { } } header)
{
var width = Math.Clamp(header.Column.DesiredWidth, header.MinWidth, header.MaxWidth);
header.Width = width;
header.Measure(new Size(Width, ActualHeight));
}
else
{
var width = Math.Clamp(Column.DesiredWidth, MinWidth, MaxWidth);
Width = width;
Measure(new Size(Width, ActualHeight));
}
}
}

protected override void OnPointerMoved(PointerRoutedEventArgs e)
{
base.OnPointerMoved(e);
Expand Down Expand Up @@ -348,7 +375,7 @@ protected override void OnManipulationDelta(ManipulationDeltaRoutedEventArgs e)
}
else if (_resizePreviousStarted && _headerRow?.GetPreviousHeader(this) is { } header)
{
header.Width = Math.Clamp(header.ActualWidth + e.Position.X, MinWidth, MaxWidth);
header.Width = Math.Clamp(header.ActualWidth + e.Position.X, header.MinWidth, header.MaxWidth);
header.Measure(new Size(header.Width, ActualHeight));
}
}
Expand Down
16 changes: 9 additions & 7 deletions src/WinUI.TableView/Themes/TableViewColumnHeader.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -195,13 +195,15 @@
<MenuFlyoutItem>
<MenuFlyoutItem.Template>
<ControlTemplate TargetType="MenuFlyoutItem">
<StackPanel Margin="8,4"
Spacing="16"
HorizontalAlignment="Right"
Orientation="Horizontal">
<Button Command="{Binding OkCommand}" />
<Button Command="{Binding CancelCommand}" />
</StackPanel>
<Grid Margin="12,4"
ColumnSpacing="16">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Button Command="{Binding OkCommand}" HorizontalAlignment="Stretch" Style="{StaticResource AccentButtonStyle}" />
<Button Command="{Binding CancelCommand}" Grid.Column="1" HorizontalAlignment="Stretch" />
</Grid>
</ControlTemplate>
</MenuFlyoutItem.Template>
</MenuFlyoutItem>
Expand Down

0 comments on commit 6394b50

Please sign in to comment.