Browse Source

Remove TotalCountChanged.

Merge it into existing `IChildIndexProvider.ChildIndexChanged` event.
pull/10055/head
Steven Kirk 4 years ago
parent
commit
d98467c3d8
  1. 63
      src/Avalonia.Base/LogicalTree/ChildIndexChangedEventArgs.cs
  2. 5
      src/Avalonia.Base/LogicalTree/IChildIndexProvider.cs
  3. 9
      src/Avalonia.Base/Styling/Activators/NthChildActivator.cs
  4. 2
      src/Avalonia.Controls.DataGrid/Primitives/DataGridCellsPresenter.cs
  5. 2
      src/Avalonia.Controls.DataGrid/Primitives/DataGridColumnHeadersPresenter.cs
  6. 11
      src/Avalonia.Controls/ItemsControl.cs
  7. 11
      src/Avalonia.Controls/Panel.cs

63
src/Avalonia.Base/LogicalTree/ChildIndexChangedEventArgs.cs

@ -1,26 +1,59 @@
#nullable enable
using System;
using System;
#nullable enable
namespace Avalonia.LogicalTree
{
/// <summary>
/// Event args for <see cref="IChildIndexProvider.ChildIndexChanged"/> event.
/// Describes the action that caused a <see cref="IChildIndexProvider.ChildIndexChanged"/> event.
/// </summary>
public class ChildIndexChangedEventArgs : EventArgs
public enum ChildIndexChangedAction
{
public static new ChildIndexChangedEventArgs Empty { get; } = new ChildIndexChangedEventArgs();
/// <summary>
/// The index of a single child changed.
/// </summary>
ChildIndexChanged,
private ChildIndexChangedEventArgs()
{
Index = -1;
}
/// <summary>
/// The index of multiple children changed and all children should be re-evaluated.
/// </summary>
ChildIndexesReset,
/// <summary>
/// The total number of children changed.
/// </summary>
TotalCountChanged,
}
/// <summary>
/// Event args for <see cref="IChildIndexProvider.ChildIndexChanged"/> event.
/// </summary>
public class ChildIndexChangedEventArgs : EventArgs
{
/// <summary>
/// Initializes a new instance of the <see cref="ChildIndexChangedEventArgs"/> class with
/// an action of <see cref="ChildIndexChangedAction.ChildIndexChanged"/>.
/// </summary>
/// <param name="child">The child whose index was changed.</param>
/// <param name="index">The new index of the child.</param>
public ChildIndexChangedEventArgs(ILogical child, int index)
{
Action = ChildIndexChangedAction.ChildIndexChanged;
Child = child;
Index = index;
}
private ChildIndexChangedEventArgs(ChildIndexChangedAction action)
{
Action = action;
Index = -1;
}
/// <summary>
/// Gets the type of change action that ocurred on the list control.
/// </summary>
public ChildIndexChangedAction Action { get; }
/// <summary>
/// Gets the logical child whose index was changed or null if all children should be re-evaluated.
/// </summary>
@ -30,5 +63,17 @@ namespace Avalonia.LogicalTree
/// Gets the new index of <see cref="Child"/> or -1 if all children should be re-evaluated.
/// </summary>
public int Index { get; }
/// <summary>
/// Gets an instance of the <see cref="ChildIndexChangedEventArgs"/> with an action of
/// <see cref="ChildIndexChangedAction.ChildIndexesReset"/>.
/// </summary>
public static ChildIndexChangedEventArgs ChildIndexesReset { get; } = new(ChildIndexChangedAction.ChildIndexesReset);
/// <summary>
/// Gets an instance of the <see cref="ChildIndexChangedEventArgs"/> with an action of
/// <see cref="ChildIndexChangedAction.TotalCountChanged"/>.
/// </summary>
public static ChildIndexChangedEventArgs TotalCountChanged { get; } = new(ChildIndexChangedAction.TotalCountChanged);
}
}

5
src/Avalonia.Base/LogicalTree/IChildIndexProvider.cs

@ -28,10 +28,5 @@ namespace Avalonia.LogicalTree
/// Notifies subscriber when a child's index was changed.
/// </summary>
event EventHandler<ChildIndexChangedEventArgs>? ChildIndexChanged;
/// <summary>
/// Notifies subscriber when the total child count changes.
/// </summary>
event EventHandler<EventArgs>? TotalCountChanged;
}
}

9
src/Avalonia.Base/Styling/Activators/NthChildActivator.cs

@ -37,7 +37,6 @@ namespace Avalonia.Styling.Activators
protected override void Initialize()
{
_provider.ChildIndexChanged += ChildIndexChanged;
_provider.TotalCountChanged += TotalCountChanged;
}
protected override void Deinitialize()
@ -48,9 +47,11 @@ namespace Avalonia.Styling.Activators
private void ChildIndexChanged(object? sender, ChildIndexChangedEventArgs e)
{
// Run matching again if:
// 1. e.Child is null, when all children indices were changed.
// 2. Subscribed child index was changed.
if (e.Child is null || e.Child == _control)
// 1. Subscribed child index was changed
// 2. Child indexes were reset
// 3. We're a reversed (nth-last-child) selector and total count has changed
if ((e.Child == _control || e.Action == ChildIndexChangedAction.ChildIndexesReset) ||
(_reversed && e.Action == ChildIndexChangedAction.TotalCountChanged))
{
// We're using the _index field to pass the index of the child to EvaluateIsActive
// *only* when the active state is re-evaluated via this event handler. The docs

2
src/Avalonia.Controls.DataGrid/Primitives/DataGridCellsPresenter.cs

@ -336,7 +336,7 @@ namespace Avalonia.Controls.Primitives
internal void InvalidateChildIndex()
{
_childIndexChanged?.Invoke(this, ChildIndexChangedEventArgs.Empty);
_childIndexChanged?.Invoke(this, ChildIndexChangedEventArgs.ChildIndexesReset);
}
private bool ShouldDisplayCell(DataGridColumn column, double frozenLeftEdge, double scrollingLeftEdge)

2
src/Avalonia.Controls.DataGrid/Primitives/DataGridColumnHeadersPresenter.cs

@ -423,7 +423,7 @@ namespace Avalonia.Controls.Primitives
internal void InvalidateChildIndex()
{
_childIndexChanged?.Invoke(this, ChildIndexChangedEventArgs.Empty);
_childIndexChanged?.Invoke(this, ChildIndexChangedEventArgs.ChildIndexesReset);
}
}
}

11
src/Avalonia.Controls/ItemsControl.cs

@ -101,7 +101,6 @@ namespace Avalonia.Controls
private int _itemCount;
private ItemContainerGenerator? _itemContainerGenerator;
private EventHandler<ChildIndexChangedEventArgs>? _childIndexChanged;
private EventHandler<EventArgs>? _totalCountChanged;
private IDataTemplate? _displayMemberItemTemplate;
private ScrollViewer? _scrollViewer;
private ItemsPresenter? _itemsPresenter;
@ -218,12 +217,6 @@ namespace Avalonia.Controls
remove => _childIndexChanged -= value;
}
event EventHandler<EventArgs>? IChildIndexProvider.TotalCountChanged
{
add => _totalCountChanged += value;
remove => _totalCountChanged -= value;
}
/// <inheritdoc />
public event EventHandler<RoutedEventArgs> HorizontalSnapPointsChanged
{
@ -500,7 +493,7 @@ namespace Avalonia.Controls
else if (change.Property == ItemCountProperty)
{
UpdatePseudoClasses(change.GetNewValue<int>());
_totalCountChanged?.Invoke(this, EventArgs.Empty);
_childIndexChanged?.Invoke(this, ChildIndexChangedEventArgs.TotalCountChanged);
}
else if (change.Property == ItemContainerThemeProperty && _itemContainerGenerator is not null)
{
@ -585,7 +578,7 @@ namespace Avalonia.Controls
internal void RegisterItemsPresenter(ItemsPresenter presenter)
{
Presenter = presenter;
_childIndexChanged?.Invoke(this, ChildIndexChangedEventArgs.Empty);
_childIndexChanged?.Invoke(this, ChildIndexChangedEventArgs.ChildIndexesReset);
}
internal void PrepareItemContainer(Control container, object? item, int index)

11
src/Avalonia.Controls/Panel.cs

@ -35,7 +35,6 @@ namespace Avalonia.Controls
}
private EventHandler<ChildIndexChangedEventArgs>? _childIndexChanged;
private EventHandler<EventArgs>? _totalCountChanged;
/// <summary>
/// Initializes a new instance of the <see cref="Panel"/> class.
@ -67,12 +66,6 @@ namespace Avalonia.Controls
remove => _childIndexChanged -= value;
}
event EventHandler<EventArgs>? IChildIndexProvider.TotalCountChanged
{
add => _totalCountChanged += value;
remove => _totalCountChanged -= value;
}
/// <summary>
/// Renders the visual to a <see cref="DrawingContext"/>.
/// </summary>
@ -161,7 +154,7 @@ namespace Avalonia.Controls
throw new NotSupportedException();
}
_childIndexChanged?.Invoke(this, ChildIndexChangedEventArgs.Empty);
_childIndexChanged?.Invoke(this, ChildIndexChangedEventArgs.ChildIndexesReset);
InvalidateMeasureOnChildrenChanged();
}
@ -173,7 +166,7 @@ namespace Avalonia.Controls
private void ChildrenPropertyChanged(object? sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(Children.Count) || e.PropertyName is null)
_totalCountChanged?.Invoke(this, EventArgs.Empty);
_childIndexChanged?.Invoke(this, ChildIndexChangedEventArgs.TotalCountChanged);
}
private static void AffectsParentArrangeInvalidate<TPanel>(AvaloniaPropertyChangedEventArgs e)

Loading…
Cancel
Save