Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

workaround for regressions caused by ItemsPanelRoot resolution timing #1294

Merged
merged 2 commits into from
Nov 28, 2024
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
23 changes: 20 additions & 3 deletions src/Uno.Toolkit.UI/Controls/Chips/ChipGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Uno.UI.Extensions;


#if IS_WINUI
using Microsoft.UI.Xaml;
Expand All @@ -18,7 +20,6 @@ namespace Uno.Toolkit.UI
{
public partial class ChipGroup : ItemsControl
{
private bool _isLoaded;
private bool _isSynchronizingSelection;

public ChipGroup()
Expand All @@ -31,11 +32,26 @@ public ChipGroup()
protected override void OnApplyTemplate()
{
base.OnApplyTemplate();

// workaround for #1287 ItemsPanelRoot resolution timing related issue
var presenter =
this.GetTemplateRoot() as ItemsPresenter ??
this.GetFirstDescendant<ItemsPresenter>();
if (presenter is { })
{
presenter.Loaded += OnItemsPresenterLoaded;
}
}

private void OnLoaded(object sender, RoutedEventArgs e)
{
_isLoaded = true;
SynchronizeInitialSelection();
EnforceSelectionMode();
ApplyIconTemplate(null, IconTemplate);
}

private void OnItemsPresenterLoaded(object sender, RoutedEventArgs e)
{
SynchronizeInitialSelection();
EnforceSelectionMode();
ApplyIconTemplate(null, IconTemplate);
Expand All @@ -58,6 +74,7 @@ private void OnSelectionMemberPathChanged(DependencyPropertyChangedEventArgs e)

private void ApplyIconTemplate(DataTemplate? oldTemplate, DataTemplate? newTemplate)
{
if (!IsReady) return;
if (oldTemplate == newTemplate) return;

foreach (var container in this.GetItemContainers<Chip>())
Expand Down Expand Up @@ -403,7 +420,7 @@ bool ShouldClearSelection(Chip container)
}
}

private bool IsReady => _isLoaded && HasItems && HasContainers;
private bool IsReady => IsLoaded && HasItems && HasContainers;

private bool HasItems => this.GetItems().OfType<object>().Any();

Expand Down
10 changes: 9 additions & 1 deletion src/Uno.Toolkit.UI/Controls/TabBar/TabBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,14 @@ private void OnLoaded(object sender, RoutedEventArgs e)
UpdateOrientation();
}

internal void OnItemsPanelConnected(TabBarListPanel panel)
{
System.Diagnostics.Debug.Assert(ItemsPanelRoot != null, "ItemsPanelRoot is expected to be already set in here.");

SynchronizeInitialSelection();
UpdateOrientation();
}

private void OnTabBarItemClick(object sender, RoutedEventArgs e)
{
if (_isSynchronizingSelection)
Expand Down Expand Up @@ -441,7 +449,7 @@ private void RaiseSelectionChangedEvent(object? prevItem, object? nextItem)
return null;
}

private bool IsReady => _isLoaded && HasItems;
private bool IsReady => _isLoaded && HasItems && ItemsPanelRoot is { };

private bool HasItems => this.GetItems().Any();
}
Expand Down
37 changes: 27 additions & 10 deletions src/Uno.Toolkit.UI/Controls/TabBar/TabBarListPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,40 @@ namespace Uno.Toolkit.UI
{
public partial class TabBarListPanel : Panel
{
public Orientation Orientation
{
get { return (Orientation)GetValue(OrientationProperty); }
set { SetValue(OrientationProperty, value); }
}
#region DependencyProperty: Orientation

public static DependencyProperty OrientationProperty { get; } = DependencyProperty.Register(
nameof(Orientation),
typeof(Orientation),
typeof(TabBarListPanel),
new PropertyMetadata(Orientation.Horizontal, (s, e) => ((TabBarListPanel)s).OnPropertyChanged(e)));
new PropertyMetadata(default(Orientation), OnOrientationChanged));

public Orientation Orientation
{
get => (Orientation)GetValue(OrientationProperty);
set => SetValue(OrientationProperty, value);
}

#endregion

public TabBarListPanel()
{
this.Loaded += OnLoaded;
}

private void OnLoaded(object sender, RoutedEventArgs e)
{
var owner = this.FindFirstParent<TabBar>();

// workaround for #1287 ItemsPanelRoot resolution timing related issue
owner?.OnItemsPanelConnected(this);
}

private void OnPropertyChanged(DependencyPropertyChangedEventArgs args)
private static void OnOrientationChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
if (args.Property == OrientationProperty)
if (sender is TabBar owner)
{
InvalidateMeasure();
owner.InvalidateMeasure();
}
}

Expand Down Expand Up @@ -132,6 +149,6 @@ protected override Size ArrangeOverride(Size finalSize)
return finalSize;
}

private bool IsVisible(UIElement x) => x.Visibility == Visibility.Visible;
private static bool IsVisible(UIElement x) => x.Visibility == Visibility.Visible;
}
}
8 changes: 0 additions & 8 deletions src/Uno.Toolkit.UI/Extensions/ItemsControlExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,6 @@ item as T ??
/// <remarks>An empty enumerable will returned if the <see cref="ItemsControl.ItemsPanelRoot"/> and the containers have not been materialized.</remarks>
public static IEnumerable<T> GetItemContainers<T>(this ItemsControl itemsControl) =>
itemsControl.ItemsPanelRoot?.Children.OfType<T>() ??
// #1281 workaround: ItemsPanelRoot would not be resolved until ItemsPanel is loaded,
// which will be the case between the ItemsControl::Loaded and ItemsPanel::Loaded.
// This is normally not a problem, as the container is typically created after that with ItemsSource.
// However, for xaml-defined items, this could cause a problem with initial selection synchronization,
// which happens on ItemsControl::Loaded. For this case, we will resort to using ItemsControl::Items.
(itemsControl.ItemsSource is null && itemsControl.Items is { }
? itemsControl.Items.OfType<T>()
: null) ??
Enumerable.Empty<T>();

/// <summary>
Expand Down
Loading