From ee6249bb179486c2f0d96ef9eececd1181a3e4aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Burak=20Kaan=20K=C3=B6se?= Date: Sat, 31 Aug 2024 15:08:43 +0200 Subject: [PATCH] Selecting first mail when thread is expanded. --- Wino.Mail/Controls/Advanced/WinoListView.cs | 44 +++++++++++++++++++-- Wino.Mail/Extensions/UtilExtensions.cs | 3 +- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/Wino.Mail/Controls/Advanced/WinoListView.cs b/Wino.Mail/Controls/Advanced/WinoListView.cs index c3289800..85e135a9 100644 --- a/Wino.Mail/Controls/Advanced/WinoListView.cs +++ b/Wino.Mail/Controls/Advanced/WinoListView.cs @@ -1,5 +1,6 @@ using System; using System.Linq; +using System.Threading.Tasks; using System.Windows.Input; using CommunityToolkit.Mvvm.Messaging; using MoreLinq; @@ -47,6 +48,13 @@ namespace Wino.Controls.Advanced set { SetValue(LoadMoreCommandProperty, value); } } + public bool IsThreadScrollingEnabled + { + get { return (bool)GetValue(IsThreadScrollingEnabledProperty); } + set { SetValue(IsThreadScrollingEnabledProperty, value); } + } + + public static readonly DependencyProperty IsThreadScrollingEnabledProperty = DependencyProperty.Register(nameof(IsThreadScrollingEnabled), typeof(bool), typeof(WinoListView), new PropertyMetadata(false)); public static readonly DependencyProperty LoadMoreCommandProperty = DependencyProperty.Register(nameof(LoadMoreCommand), typeof(ICommand), typeof(WinoListView), new PropertyMetadata(null)); public static readonly DependencyProperty IsThreadListViewProperty = DependencyProperty.Register(nameof(IsThreadListView), typeof(bool), typeof(WinoListView), new PropertyMetadata(false, new PropertyChangedCallback(OnIsThreadViewChanged))); public static readonly DependencyProperty ItemDeletedCommandProperty = DependencyProperty.Register(nameof(ItemDeletedCommand), typeof(ICommand), typeof(WinoListView), new PropertyMetadata(null)); @@ -316,10 +324,19 @@ namespace Wino.Controls.Advanced } else if (addedItem is ThreadMailItemViewModel threadMailItemViewModel) { - // threadMailItemViewModel.IsThreadExpanded = true; + if (IsThreadScrollingEnabled) + { + if (internalScrollviewer != null && ContainerFromItem(threadMailItemViewModel) is FrameworkElement threadFrameworkElement) + { + internalScrollviewer.ScrollToElement(threadFrameworkElement, true, true, bringToTopOrLeft: true); + } + } - // Don't select thread containers. - // SelectedItems.Remove(addedItem); + // Try to select first item. + if (GetThreadInternalListView(threadMailItemViewModel) is WinoListView internalListView) + { + internalListView.SelectFirstItem(); + } } } } @@ -355,6 +372,27 @@ namespace Wino.Controls.Advanced } } + public async void SelectFirstItem() + { + if (Items.Count > 0) + { + if (Items[0] is MailItemViewModel firstMailItemViewModel) + { + // Make sure the invisible container is realized. + await Task.Delay(250); + + if (ContainerFromItem(firstMailItemViewModel) is ListViewItem firstItemContainer) + { + firstItemContainer.IsSelected = true; + } + + firstMailItemViewModel.IsSelected = true; + + // WeakReferenceMessenger.Default.Send(new MailItemSelectedEvent(firstMailItemViewModel)); + } + } + } + private WinoListView GetThreadInternalListView(ThreadMailItemViewModel threadMailItemViewModel) { var itemContainer = ContainerFromItem(threadMailItemViewModel); diff --git a/Wino.Mail/Extensions/UtilExtensions.cs b/Wino.Mail/Extensions/UtilExtensions.cs index bfbea266..50367afc 100644 --- a/Wino.Mail/Extensions/UtilExtensions.cs +++ b/Wino.Mail/Extensions/UtilExtensions.cs @@ -93,7 +93,8 @@ namespace Wino.Extensions if (isVerticalScrolling) { - scrollViewer.ChangeView(null, position.Y, zoomFactor, !smoothScrolling); + // Accomodate for additional header. + scrollViewer.ChangeView(null, Math.Max(0, position.Y - 48), zoomFactor, !smoothScrolling); } else {