Intercepting containers for threads.

This commit is contained in:
Burak Kaan Köse
2025-10-26 23:35:09 +01:00
parent 79d5b6ed40
commit d9fc365aeb
12 changed files with 528 additions and 175 deletions
@@ -1,4 +1,5 @@
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Wino.Mail.ViewModels.Data;
namespace Wino.Mail.WinUI.Controls.ListView;
@@ -7,34 +8,70 @@ public partial class WinoListView : Microsoft.UI.Xaml.Controls.ListView
{
public bool IsAllSelected => Items.Count == SelectedItems.Count;
protected override DependencyObject GetContainerForItemOverride() => new WinoListViewItem();
public WinoListView()
{
ChoosingItemContainer += WinoListView_ChoosingItemContainer;
}
private void WinoListView_ChoosingItemContainer(ListViewBase sender, ChoosingItemContainerEventArgs args)
{
if (args.Item is ThreadMailItemViewModel)
{
args.ItemContainer = new WinoThreadMailItemViewModelListViewItem();
}
else if (args.Item is MailItemViewModel)
{
args.ItemContainer = new WinoMailItemViewModelListViewItem();
}
// Handle the preparation in PrepareContainerForItemOverride
args.IsContainerPrepared = false;
}
protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
{
if (item is MailItemViewModel mailItemViewModel && element is WinoMailItemViewModelListViewItem container)
{
// Ensure the container's selection state matches the model's state
// This is crucial for virtualization scenarios where containers are recycled
container.IsSelected = mailItemViewModel.IsSelected;
}
else if (item is ThreadMailItemViewModel threadMailItemViewModel && element is WinoThreadMailItemViewModelListViewItem threadContainer)
{
threadContainer.IsThreadExpanded = threadMailItemViewModel.IsThreadExpanded;
}
base.PrepareContainerForItemOverride(element, item);
}
public bool SelectMailItemContainer(MailItemViewModel mailItemViewModel)
{
WinoListViewItem? itemContainer = null;
WinoMailItemViewModelListViewItem? itemContainer = null;
WinoThreadMailItemViewModelListViewItem? threadContainer = null;
foreach (var item in Items)
{
if (item is MailItemViewModel mailItem && mailItem.Id == mailItemViewModel.Id)
{
itemContainer = ContainerFromItem(mailItemViewModel) as WinoListViewItem;
itemContainer = ContainerFromItem(mailItemViewModel) as WinoMailItemViewModelListViewItem;
break;
}
else if (item is ThreadMailItemViewModel threadMailItemViewModel && threadMailItemViewModel.HasUniqueId(mailItemViewModel.MailCopy.UniqueId))
{
itemContainer = ContainerFromItem(threadMailItemViewModel) as WinoListViewItem;
threadContainer = ContainerFromItem(threadMailItemViewModel) as WinoThreadMailItemViewModelListViewItem;
// Try to get the inner WinoListView.
if (itemContainer != null)
if (threadContainer != null)
{
itemContainer.IsExpanded = true;
threadContainer.IsThreadExpanded = true;
var innerListViewControl = itemContainer.GetWinoListViewControl();
var innerListViewControl = threadContainer.GetWinoListViewControl();
if (innerListViewControl != null)
{
itemContainer = innerListViewControl.ContainerFromItem(mailItemViewModel) as WinoListViewItem;
itemContainer = innerListViewControl.ContainerFromItem(mailItemViewModel) as WinoMailItemViewModelListViewItem;
}
}
@@ -42,8 +79,37 @@ public partial class WinoListView : Microsoft.UI.Xaml.Controls.ListView
}
}
itemContainer?.IsSelected = true;
if (itemContainer != null)
{
itemContainer.IsSelected = true;
return true;
}
else if (threadContainer != null)
{
return true;
}
return itemContainer != null;
return false;
}
public void ChangeSelectionMode(ListViewSelectionMode mode)
{
// Not only this control, but also all inner WinoListView controls should change the selection mode.
// TODO: New threads added after this call won't have the correct selection mode.
SelectionMode = mode;
foreach (var item in Items)
{
if (item is ThreadMailItemViewModel)
{
var itemContainer = ContainerFromItem(item) as WinoThreadMailItemViewModelListViewItem;
if (itemContainer != null)
{
var innerListViewControl = itemContainer.GetWinoListViewControl();
innerListViewControl?.ChangeSelectionMode(mode);
}
}
}
}
}
@@ -2,14 +2,16 @@
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="using:Wino.Controls"
xmlns:local="using:Wino.Mail.WinUI.Controls.ListView">
<ResourceDictionary.MergedDictionaries>
<XamlControlsResources xmlns="using:Microsoft.UI.Xaml.Controls" />
<ResourceDictionary Source="/Styles/WinoExpanderStyle.xaml" />
<ResourceDictionary>
<!-- Thread Mail ListViewItem Style -->
<Style x:Key="DefaultThreadListViewItemStyle" TargetType="local:WinoListViewItem">
<Style x:Key="DefaultThreadListViewItemStyle" TargetType="local:WinoMailItemViewModelListViewItem">
<Setter Property="FontFamily" Value="{ThemeResource ContentControlThemeFontFamily}" />
<Setter Property="FontSize" Value="{ThemeResource ControlContentThemeFontSize}" />
<Setter Property="Background" Value="{ThemeResource ListViewItemBackground}" />
@@ -30,20 +32,17 @@
<Setter Property="FocusVisualSecondaryThickness" Value="1" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="local:WinoListViewItem">
<Expander Header="Thread" IsExpanded="{TemplateBinding IsExpanded}">
<Expander.Content>
<!-- Expandable Content -->
<ContentPresenter
x:Name="ThreadContent"
Margin="{TemplateBinding Padding}"
HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}"
ContentTransitions="{TemplateBinding ContentTransitions}" />
</Expander.Content>
</Expander>
<ControlTemplate TargetType="local:WinoMailItemViewModelListViewItem">
<!-- Expandable Content -->
<ContentPresenter
x:Name="ThreadContent"
Margin="{TemplateBinding Padding}"
HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}"
ContentTransitions="{TemplateBinding ContentTransitions}" />
</ControlTemplate>
</Setter.Value>
</Setter>
@@ -53,7 +52,7 @@
<Style
x:Key="DefaultMailListViewItemStyle"
BasedOn="{StaticResource DefaultListViewItemStyle}"
TargetType="local:WinoListViewItem" />
TargetType="local:WinoMailItemViewModelListViewItem" />
<local:WinoMailItemContainerStyleSelector
x:Name="WinoMailItemContainerStyleSelector"
@@ -1,47 +1,30 @@
using CommunityToolkit.Mvvm.Messaging;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media;
using Wino.Mail.ViewModels.Data;
using Wino.Messaging.Client.Mails;
namespace Wino.Mail.WinUI.Controls.ListView;
public partial class WinoListViewItem : ListViewItem
public partial class WinoMailItemViewModelListViewItem : ListViewItem
{
public bool IsExpanded
public WinoMailItemViewModelListViewItem()
{
get { return (bool)GetValue(IsExpandedProperty); }
set { SetValue(IsExpandedProperty, value); }
}
public static readonly DependencyProperty IsExpandedProperty = DependencyProperty.Register(nameof(IsExpanded), typeof(bool), typeof(WinoListViewItem), new PropertyMetadata(false, OnIsExpandedChanged));
public WinoListViewItem()
{
DefaultStyleKey = typeof(WinoListViewItem);
DefaultStyleKey = typeof(WinoMailItemViewModelListViewItem);
RegisterPropertyChangedCallback(IsSelectedProperty, OnIsSelectedChanged);
}
private static void OnIsExpandedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is WinoListViewItem item)
{
// Handle expansion state change if needed
}
}
protected override void OnContentChanged(object oldContent, object newContent)
{
base.OnContentChanged(oldContent, newContent);
if (oldContent is IMailListItem oldMailItem)
if (oldContent is MailItemViewModel oldMailItem)
{
UnregisterSelectionCallback(oldMailItem);
}
if (newContent is IMailListItem newMailItem)
if (newContent is MailItemViewModel newMailItem)
{
IsSelected = newMailItem.IsSelected;
RegisterSelectionCallback(newMailItem);
@@ -61,9 +44,9 @@ public partial class WinoListViewItem : ListViewItem
// From model
private void MailPropChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
{
if (sender is not IMailListItem mailItem) return;
if (sender is not MailItemViewModel mailItem) return;
if (e.PropertyName == nameof(IMailListItem.IsSelected)) ApplySelectionForContainer(mailItem);
if (e.PropertyName == nameof(MailItemViewModel.IsSelected)) ApplySelectionForContainer(mailItem);
}
// From container.
@@ -91,13 +74,4 @@ public partial class WinoListViewItem : ListViewItem
IsSelected = mailItem.IsSelected;
}
}
public WinoListView? GetWinoListViewControl()
{
var expander = GetTemplateChild("ExpanderPart") as Expander;
if (expander?.Content is ContentPresenter presenter) return VisualTreeHelper.GetChild(presenter, 0) as WinoListView;
return null;
}
}
@@ -0,0 +1,116 @@
using System.ComponentModel;
using System.Linq;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Wino.Controls;
using Wino.Helpers;
using Wino.Mail.ViewModels.Data;
namespace Wino.Mail.WinUI.Controls.ListView;
public partial class WinoThreadMailItemViewModelListViewItem : ListViewItem
{
public bool IsThreadExpanded
{
get { return (bool)GetValue(IsThreadExpandedProperty); }
set { SetValue(IsThreadExpandedProperty, value); }
}
public static readonly DependencyProperty IsThreadExpandedProperty = DependencyProperty.Register(nameof(IsThreadExpanded), typeof(bool), typeof(WinoThreadMailItemViewModelListViewItem), new PropertyMetadata(false, new PropertyChangedCallback(OnIsThreadExpandedChanged)));
public WinoThreadMailItemViewModelListViewItem()
{
RegisterPropertyChangedCallback(IsSelectedProperty, OnIsSelectedChanged);
}
private static void OnIsThreadExpandedChanged(DependencyObject sender, DependencyPropertyChangedEventArgs dp)
{
// 1. Reflect expansion changes to WinoExpander.
// 2. Automatically select first item on expansion, if none selected.
// 3. Unselect all items on collapse.
var control = sender as WinoThreadMailItemViewModelListViewItem;
var innerControl = control?.GetWinoListViewControl();
var expander = control?.GetExpander();
if (innerControl == null || control == null || expander == null) return;
// 1
expander.IsExpanded = control.IsThreadExpanded;
// 2
if (control.IsThreadExpanded && innerControl.SelectedItems.Count == 0 && innerControl.Items.Count > 0)
{
innerControl.SelectedItem = innerControl.Items[0];
}
// 3
if (!control.IsSelected) innerControl?.SelectedItems.Clear();
}
protected override void OnContentChanged(object oldContent, object newContent)
{
base.OnContentChanged(oldContent, newContent);
if (oldContent is ThreadMailItemViewModel oldMailItem)
{
UnregisterSelectionCallback(oldMailItem);
}
if (newContent is ThreadMailItemViewModel newMailItem)
{
IsSelected = newMailItem.IsSelected;
RegisterSelectionCallback(newMailItem);
}
}
private void OnIsSelectedChanged(DependencyObject sender, DependencyProperty dp)
{
IsThreadExpanded = IsSelected;
}
private void UnregisterSelectionCallback(ThreadMailItemViewModel mailItem)
{
mailItem.PropertyChanged -= MailPropChanged;
}
private void MailPropChanged(object? sender, PropertyChangedEventArgs e)
{
if (sender is not ThreadMailItemViewModel mailItem) return;
if (e.PropertyName == nameof(ThreadMailItemViewModel.IsThreadExpanded))
{
ApplySelectionForContainer(mailItem);
}
}
private void RegisterSelectionCallback(ThreadMailItemViewModel mailItem)
{
mailItem.PropertyChanged += MailPropChanged;
}
private void ApplySelectionForModel(ThreadMailItemViewModel mailItem)
{
if (mailItem.IsThreadExpanded != IsThreadExpanded)
{
mailItem.IsThreadExpanded = IsThreadExpanded;
}
}
private void ApplySelectionForContainer(ThreadMailItemViewModel mailItem)
{
if (IsThreadExpanded != mailItem.IsThreadExpanded) IsThreadExpanded = mailItem.IsThreadExpanded;
}
public WinoListView? GetWinoListViewControl()
{
var expander = GetExpander();
if (expander?.Content is WinoListView control) return control;
return null;
}
public WinoExpander? GetExpander() => WinoVisualTreeHelper.FindDescendants<WinoExpander>(this).FirstOrDefault();
}
+1 -1
View File
@@ -83,7 +83,7 @@ public partial class WinoExpander : Control
return;
}
IsExpanded = !IsExpanded;
// IsExpanded = !IsExpanded;
}
private static void OnIsExpandedChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
+34 -1
View File
@@ -69,7 +69,39 @@
</DataTemplate>
<DataTemplate x:Key="ThreadMailItemTemplate" x:DataType="viewModelData:ThreadMailItemViewModel">
<TextBlock Text="thread :)" />
<controls:WinoExpander x:Name="ExpanderPart" IsExpanded="{x:Bind IsThreadExpanded, Mode=TwoWay}">
<controls:WinoExpander.Header>
<controls:MailItemDisplayInformationControl
x:DefaultBindMode="OneWay"
CenterHoverAction="{Binding ElementName=root, Path=ViewModel.PreferencesService.CenterHoverAction, Mode=OneWay}"
ContextRequested="MailItemContextRequested"
DisplayMode="{Binding ElementName=root, Path=ViewModel.PreferencesService.MailItemDisplayMode, Mode=OneWay}"
HoverActionExecutedCommand="{Binding ElementName=root, Path=ViewModel.ExecuteHoverActionCommand}"
IsAvatarVisible="{Binding ElementName=root, Path=ViewModel.PreferencesService.IsShowSenderPicturesEnabled, Mode=OneWay}"
IsHoverActionsEnabled="{Binding ElementName=root, Path=ViewModel.PreferencesService.IsHoverActionsEnabled, Mode=OneWay}"
IsThreadExpanded="{x:Bind IsThreadExpanded, Mode=OneWay}"
IsThreadExpanderVisible="True"
IsThumbnailUpdated="{x:Bind LatestMailViewModel.ThumbnailUpdatedEvent, Mode=OneWay}"
LeftHoverAction="{Binding ElementName=root, Path=ViewModel.PreferencesService.LeftHoverAction, Mode=OneWay}"
MailItem="{x:Bind LatestMailViewModel.MailCopy, Mode=OneWay}"
Prefer24HourTimeFormat="{Binding ElementName=root, Path=ViewModel.PreferencesService.Prefer24HourTimeFormat, Mode=OneWay}"
RightHoverAction="{Binding ElementName=root, Path=ViewModel.PreferencesService.RightHoverAction, Mode=OneWay}"
ShowPreviewText="{Binding ElementName=root, Path=ViewModel.PreferencesService.IsShowPreviewEnabled, Mode=OneWay}" />
</controls:WinoExpander.Header>
<controls:WinoExpander.Content>
<listview:WinoListView
HorizontalAlignment="Stretch"
HorizontalContentAlignment="Stretch"
toolkitExt:ListViewExtensions.ItemContainerStretchDirection="Horizontal"
toolkitExt:ScrollViewerExtensions.VerticalScrollBarMargin="0"
ItemContainerStyle="{StaticResource DefaultMailListViewItemStyle}"
ItemTemplate="{StaticResource SingleMailItemTemplate}"
ItemsSource="{x:Bind ThreadEmails, Mode=OneTime}"
ProcessKeyboardAccelerators="WinoListViewProcessKeyboardAccelerators"
SelectionMode="Extended" />
</controls:WinoExpander.Content>
</controls:WinoExpander>
</DataTemplate>
<listview:WinoMailItemTemplateSelector
@@ -355,6 +387,7 @@
ItemTemplateSelector="{StaticResource MailItemTemplateSelector}"
ItemsSource="{x:Bind MailCollectionViewSource.View, Mode=OneWay}"
ProcessKeyboardAccelerators="WinoListViewProcessKeyboardAccelerators"
SelectionChanged="WinoListViewSelectionChanged"
SelectionMode="Extended">
<listview:WinoListView.ItemContainerTransitions>
<TransitionCollection>
+7 -64
View File
@@ -8,7 +8,6 @@ using Microsoft.Extensions.DependencyInjection;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Hosting;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media.Animation;
using Microsoft.UI.Xaml.Navigation;
@@ -21,7 +20,6 @@ using Wino.Core.Domain.Interfaces;
using Wino.Core.Domain.Models.MailItem;
using Wino.Core.Domain.Models.Menus;
using Wino.Core.Domain.Models.Navigation;
using Wino.Helpers;
using Wino.Mail.ViewModels.Data;
using Wino.Mail.ViewModels.Messages;
using Wino.MenuFlyouts.Context;
@@ -123,7 +121,7 @@ public sealed partial class MailListPage : MailListPageAbstract,
private void ChangeSelectionMode(ListViewSelectionMode mode)
{
MailListView.SelectionMode = mode;
MailListView.ChangeSelectionMode(mode);
if (ViewModel?.PivotFolders != null)
{
@@ -509,72 +507,12 @@ public sealed partial class MailListPage : MailListPageAbstract,
private void DeleteAllInvoked(KeyboardAccelerator sender, KeyboardAcceleratorInvokedEventArgs args)
=> ViewModel.ExecuteMailOperationCommand.Execute(MailOperation.SoftDelete);
/// <summary>
/// Animates the rotation using high-performance Composition APIs
/// </summary>
private void AnimateRotationWithComposition(FrameworkElement element, float targetAngleInDegrees)
{
// Get the element's visual from the composition layer
var visual = ElementCompositionPreview.GetElementVisual(element);
var compositor = visual.Compositor;
// Set the center point for rotation (center of the element)
visual.CenterPoint = new System.Numerics.Vector3(
(float)element.ActualWidth / 2f,
(float)element.ActualHeight / 2f,
0f);
// Create a rotation animation
var rotationAnimation = compositor.CreateScalarKeyFrameAnimation();
rotationAnimation.Target = "RotationAngleInDegrees";
rotationAnimation.Duration = TimeSpan.FromMilliseconds(200);
// Add easing function for smooth animation
var easingFunction = compositor.CreateCubicBezierEasingFunction(
new System.Numerics.Vector2(0.25f, 0.1f), // Control point 1
new System.Numerics.Vector2(0.25f, 1f)); // Control point 2 (similar to CircleEase)
// Insert keyframe with the target angle and easing
rotationAnimation.InsertKeyFrame(1.0f, targetAngleInDegrees, easingFunction);
// Start the animation
visual.StartAnimation("RotationAngleInDegrees", rotationAnimation);
}
private void WinoMailCollectionSelectionChanged(object sender, EventArgs args)
private void WinoMailCollectionSelectionChanged(object? sender, EventArgs args)
{
UpdateSelectAllButtonStatus();
UpdateAdaptiveness();
}
private void ThreadContainerRightTapped(object sender, RightTappedRoutedEventArgs e)
{
if (sender is ItemContainer container && container.Tag is ThreadMailItemViewModel expander)
{
expander.IsThreadExpanded = !expander.IsThreadExpanded;
// Select all.
// ViewModel.MailCollection.AllItems.Where(a => expander.ThreadEmails.Contains(a)).ForEach(a => a.IsSelected = true);
}
}
private void ThreadContainerTapped(object sender, TappedRoutedEventArgs e)
{
if (sender is ItemContainer container && container.Tag is ThreadMailItemViewModel expander)
{
// Toggle expansion state
expander.IsThreadExpanded = !expander.IsThreadExpanded;
// Find the expander icon and animate its rotation using Composition APIs
var expanderIcon = WinoVisualTreeHelper.GetChildObject<FontIcon>(container, "ExpanderIcon");
if (expanderIcon != null)
{
var targetAngle = expander.IsThreadExpanded ? 90f : 0f;
AnimateRotationWithComposition(expanderIcon, targetAngle);
}
}
}
private async void WinoListViewProcessKeyboardAccelerators(UIElement sender, ProcessKeyboardAcceleratorEventArgs args)
{
if (args.Key == VirtualKey.Delete)
@@ -588,4 +526,9 @@ public sealed partial class MailListPage : MailListPageAbstract,
await ViewModel.MailCollection.ToggleSelectAllAsync();
}
}
private void WinoListViewSelectionChanged(object sender, SelectionChangedEventArgs e)
{
}
}