Skip to content

Commit

Permalink
fix(chipgroup): initial selection
Browse files Browse the repository at this point in the history
  • Loading branch information
Xiaoy312 committed Nov 28, 2024
1 parent d0f903c commit abc3692
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 22 deletions.
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
24 changes: 13 additions & 11 deletions src/Uno.Toolkit.UI/Controls/TabBar/TabBarListPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,20 @@ namespace Uno.Toolkit.UI
{
public partial class TabBarListPanel : Panel
{
#region Orientation
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()
Expand All @@ -44,11 +46,11 @@ private void OnLoaded(object sender, RoutedEventArgs e)
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 @@ -147,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

0 comments on commit abc3692

Please sign in to comment.