Browse Source

Changes after review

pull/6381/head
Max Katz 5 years ago
parent
commit
f276c4ed8b
  1. 2
      samples/ControlCatalog/Pages/ListBoxPage.xaml
  2. 15
      src/Avalonia.Controls/ItemsControl.cs
  3. 10
      src/Avalonia.Controls/Panel.cs
  4. 8
      src/Avalonia.Controls/Presenters/ItemsPresenterBase.cs
  5. 10
      src/Avalonia.Controls/Repeater/ItemsRepeater.cs
  6. 4
      src/Avalonia.Styling/LogicalTree/IChildIndexProvider.cs
  7. 9
      src/Avalonia.Styling/Styling/Activators/NthChildActivator.cs
  8. 9
      src/Avalonia.Styling/Styling/NthChildSelector.cs
  9. 2
      tests/Avalonia.Markup.Xaml.UnitTests/Xaml/StyleTests.cs
  10. 2
      tests/Avalonia.Styling.UnitTests/SelectorTests_NthChild.cs
  11. 1
      tests/Avalonia.Styling.UnitTests/SelectorTests_NthLastChild.cs

2
samples/ControlCatalog/Pages/ListBoxPage.xaml

@ -15,7 +15,7 @@
<StackPanel DockPanel.Dock="Top" Margin="4">
<TextBlock Classes="h1">ListBox</TextBlock>
<TextBlock Classes="h2">Hosts a collection of ListBoxItem.</TextBlock>
<TextBlock Classes="h2">Each 2nd item is highlighted</TextBlock>
<TextBlock Classes="h2">Each 5th item is highlighted with nth-child(5n+3) and nth-last-child(5n+4) rules.</TextBlock>
</StackPanel>
<StackPanel DockPanel.Dock="Right" Margin="4">
<CheckBox IsChecked="{Binding Multiple}">Multiple</CheckBox>

15
src/Avalonia.Controls/ItemsControl.cs

@ -13,7 +13,6 @@ using Avalonia.Controls.Utils;
using Avalonia.Input;
using Avalonia.LogicalTree;
using Avalonia.Metadata;
using Avalonia.Styling;
using Avalonia.VisualTree;
namespace Avalonia.Controls
@ -147,8 +146,6 @@ namespace Avalonia.Controls
protected set;
}
int? IChildIndexProvider.TotalCount => (Presenter as IChildIndexProvider)?.TotalCount ?? ItemCount;
event EventHandler<ChildIndexChangedEventArgs> IChildIndexProvider.ChildIndexChanged
{
add => _childIndexChanged += value;
@ -538,5 +535,17 @@ namespace Avalonia.Controls
return Presenter is IChildIndexProvider innerProvider
? innerProvider.GetChildIndex(child) : -1;
}
bool IChildIndexProvider.TryGetTotalCount(out int count)
{
if (Presenter is IChildIndexProvider presenter
&& presenter.TryGetTotalCount(out count))
{
return true;
}
count = ItemCount;
return true;
}
}
}

10
src/Avalonia.Controls/Panel.cs

@ -2,8 +2,6 @@ using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using Avalonia.Controls.Presenters;
using Avalonia.LogicalTree;
using Avalonia.Media;
using Avalonia.Metadata;
@ -59,8 +57,6 @@ namespace Avalonia.Controls
set { SetValue(BackgroundProperty, value); }
}
int? IChildIndexProvider.TotalCount => Children.Count;
event EventHandler<ChildIndexChangedEventArgs> IChildIndexProvider.ChildIndexChanged
{
add => _childIndexChanged += value;
@ -180,5 +176,11 @@ namespace Avalonia.Controls
{
return child is IControl control ? Children.IndexOf(control) : -1;
}
public bool TryGetTotalCount(out int count)
{
count = Children.Count;
return true;
}
}
}

8
src/Avalonia.Controls/Presenters/ItemsPresenterBase.cs

@ -1,7 +1,6 @@
using System;
using System.Collections;
using System.Collections.Specialized;
using Avalonia.Collections;
using Avalonia.Controls.Generators;
using Avalonia.Controls.Templates;
@ -132,8 +131,6 @@ namespace Avalonia.Controls.Presenters
protected bool IsHosted => TemplatedParent is IItemsPresenterHost;
int? IChildIndexProvider.TotalCount => Items.TryGetCountFast(out var count) ? count : (int?)null;
event EventHandler<ChildIndexChangedEventArgs> IChildIndexProvider.ChildIndexChanged
{
add => _childIndexChanged += value;
@ -285,5 +282,10 @@ namespace Avalonia.Controls.Presenters
return -1;
}
bool IChildIndexProvider.TryGetTotalCount(out int count)
{
return Items.TryGetCountFast(out count);
}
}
}

10
src/Avalonia.Controls/Repeater/ItemsRepeater.cs

@ -6,13 +6,11 @@
using System;
using System.Collections;
using System.Collections.Specialized;
using Avalonia.Controls.Templates;
using Avalonia.Input;
using Avalonia.Layout;
using Avalonia.Logging;
using Avalonia.LogicalTree;
using Avalonia.Styling;
using Avalonia.Utilities;
using Avalonia.VisualTree;
@ -167,8 +165,6 @@ namespace Avalonia.Controls
}
}
int? IChildIndexProvider.TotalCount => ItemsSourceView.Count;
event EventHandler<ChildIndexChangedEventArgs> IChildIndexProvider.ChildIndexChanged
{
add => _childIndexChanged += value;
@ -182,6 +178,12 @@ namespace Avalonia.Controls
: -1;
}
bool IChildIndexProvider.TryGetTotalCount(out int count)
{
count = ItemsSourceView.Count;
return true;
}
/// <summary>
/// Occurs each time an element is cleared and made available to be re-used.
/// </summary>

4
src/Avalonia.Styling/LogicalTree/IChildIndexProvider.cs

@ -20,9 +20,9 @@ namespace Avalonia.LogicalTree
/// <summary>
/// Total children count or null if source is infinite.
/// Some Avalonia features might not work if <see cref="TotalCount"/> is null, for instance: nth-last-child selector.
/// Some Avalonia features might not work if <see cref="TryGetTotalCount"/> returns false, for instance: nth-last-child selector.
/// </summary>
int? TotalCount { get; }
bool TryGetTotalCount(out int count);
/// <summary>
/// Notifies subscriber when child's index or total count was changed.

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

@ -1,6 +1,4 @@
#nullable enable
using System;
using Avalonia.LogicalTree;
namespace Avalonia.Styling.Activators
@ -15,7 +13,6 @@ namespace Avalonia.Styling.Activators
private readonly int _step;
private readonly int _offset;
private readonly bool _reversed;
private EventHandler<ChildIndexChangedEventArgs>? _childIndexChangedHandler;
public NthChildActivator(
ILogical control,
@ -29,17 +26,15 @@ namespace Avalonia.Styling.Activators
_reversed = reversed;
}
private EventHandler<ChildIndexChangedEventArgs> ChildIndexChangedHandler => _childIndexChangedHandler ??= ChildIndexChanged;
protected override void Initialize()
{
PublishNext(IsMatching());
_provider.ChildIndexChanged += ChildIndexChangedHandler;
_provider.ChildIndexChanged += ChildIndexChanged;
}
protected override void Deinitialize()
{
_provider.ChildIndexChanged -= ChildIndexChangedHandler;
_provider.ChildIndexChanged -= ChildIndexChanged;
}
private void ChildIndexChanged(object sender, ChildIndexChangedEventArgs e)

9
src/Avalonia.Styling/Styling/NthChildSelector.cs

@ -1,7 +1,6 @@
#nullable enable
using System;
using System.Text;
using Avalonia.LogicalTree;
using Avalonia.Styling.Activators;
@ -51,7 +50,11 @@ namespace Avalonia.Styling
protected override SelectorMatch Evaluate(IStyleable control, bool subscribe)
{
var logical = (ILogical)control;
if (!(control is ILogical logical))
{
return SelectorMatch.NeverThisType;
}
var controlParent = logical.LogicalParent;
if (controlParent is IChildIndexProvider childIndexProvider)
@ -78,7 +81,7 @@ namespace Avalonia.Styling
if (reversed)
{
if (childIndexProvider.TotalCount is int totalCountValue)
if (childIndexProvider.TryGetTotalCount(out var totalCountValue))
{
index = totalCountValue - index;
}

2
tests/Avalonia.Markup.Xaml.UnitTests/Xaml/StyleTests.cs

@ -1,10 +1,8 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
using System.Xml;
using Avalonia.Controls;
using Avalonia.Markup.Data;
using Avalonia.Markup.Xaml.Styling;
using Avalonia.Markup.Xaml.Templates;
using Avalonia.Media;

2
tests/Avalonia.Styling.UnitTests/SelectorTests_NthChild.cs

@ -1,7 +1,5 @@
using System.Threading.Tasks;
using Avalonia.Controls;
using Xunit;
namespace Avalonia.Styling.UnitTests

1
tests/Avalonia.Styling.UnitTests/SelectorTests_NthLastChild.cs

@ -1,5 +1,4 @@
using System.Threading.Tasks;
using Avalonia.Controls;
using Xunit;

Loading…
Cancel
Save