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

Use ItemCount to determine PseudoClass in ItemsControl for better consistency #5844

Merged
merged 2 commits into from
Apr 29, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 9 additions & 6 deletions src/Avalonia.Controls/ItemsControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,8 @@ protected virtual void ItemsChanged(AvaloniaPropertyChangedEventArgs e)
Presenter.Items = newValue;
}

UpdatePseudoClasses();

SubscribeToItems(newValue);
}

Expand All @@ -372,9 +374,7 @@ protected virtual void ItemsCollectionChanged(object sender, NotifyCollectionCha

Presenter?.ItemsChanged(e);

var collection = sender as ICollection;
PseudoClasses.Set(":empty", collection == null || collection.Count == 0);
PseudoClasses.Set(":singleitem", collection != null && collection.Count == 1);
UpdatePseudoClasses();
}

/// <summary>
Expand Down Expand Up @@ -431,9 +431,6 @@ private void RemoveControlItemsFromLogicalChildren(IEnumerable items)
/// <param name="items">The items collection.</param>
private void SubscribeToItems(IEnumerable items)
{
PseudoClasses.Set(":empty", items == null || items.Count() == 0);
PseudoClasses.Set(":singleitem", items != null && items.Count() == 1);

if (items is INotifyCollectionChanged incc)
{
CollectionChangedEventManager.Instance.AddListener(incc, this);
Expand Down Expand Up @@ -469,6 +466,12 @@ private void UpdateItemCount()
}
}

private void UpdatePseudoClasses()
{
PseudoClasses.Set(":empty", ItemCount == 0);
PseudoClasses.Set(":singleitem", ItemCount == 1);
}

protected static IInputElement GetNextControl(
INavigableContainer container,
NavigationDirection direction,
Expand Down
64 changes: 64 additions & 0 deletions tests/Avalonia.Controls.UnitTests/ItemsControlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,70 @@ public void Empty_Class_Should_Be_Set_When_Empty_Collection_Set()
Assert.Contains(":empty", target.Classes);
}

[Fact]
public void Item_Count_Should_Be_Set_When_Items_Added()
{
var target = new ItemsControl()
{
Template = GetTemplate(),
Items = new[] { 1, 2, 3 },
};

Assert.Equal(3, target.ItemCount);
}

[Fact]
public void Item_Count_Should_Be_Set_When_Items_Changed()
{
var items = new ObservableCollection<int>() { 1, 2, 3 };

var target = new ItemsControl()
{
Template = GetTemplate(),
Items = items,
};

items.Add(4);

Assert.Equal(4, target.ItemCount);

items.Clear();

Assert.Equal(0, target.ItemCount);
}

[Fact]
public void Empty_Class_Should_Be_Set_When_Items_Collection_Cleared()
{
var items = new ObservableCollection<int>() { 1, 2, 3 };

var target = new ItemsControl()
{
Template = GetTemplate(),
Items = items,
};

items.Clear();

Assert.Contains(":empty", target.Classes);
}

[Fact]
public void Empty_Class_Should_Not_Be_Set_When_Items_Collection_Count_Increases()
{
var items = new ObservableCollection<int>() {};

var target = new ItemsControl()
{
Template = GetTemplate(),
Items = items,
};

items.Add(1);

Assert.DoesNotContain(":empty", target.Classes);
}

[Fact]
public void Setting_Presenter_Explicitly_Should_Set_Item_Parent()
{
Expand Down