Browse Source

wip

pull/9961/head
Emmanuel Hansen 4 years ago
parent
commit
b1b63cba2a
  1. 60
      src/Avalonia.Controls/ItemsControl.cs
  2. 67
      src/Avalonia.Controls/Presenters/ItemsPresenter.cs

60
src/Avalonia.Controls/ItemsControl.cs

@ -12,6 +12,8 @@ using Avalonia.Controls.Primitives;
using Avalonia.Controls.Templates;
using Avalonia.Data;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Layout;
using Avalonia.LogicalTree;
using Avalonia.Metadata;
using Avalonia.Styling;
@ -23,7 +25,7 @@ namespace Avalonia.Controls
/// Displays a collection of items.
/// </summary>
[PseudoClasses(":empty", ":singleitem")]
public class ItemsControl : TemplatedControl, IChildIndexProvider
public class ItemsControl : TemplatedControl, IChildIndexProvider, IScrollSnapPointsInfo
{
/// <summary>
/// The default value for the <see cref="ItemsPanel"/> property.
@ -91,6 +93,7 @@ namespace Avalonia.Controls
private IDataTemplate? _displayMemberItemTemplate;
private Tuple<int, Control>? _containerBeingPrepared;
private ScrollViewer? _scrollViewer;
private ItemsPresenter? _itemsPresenter;
/// <summary>
/// Initializes a new instance of the <see cref="ItemsControl"/> class.
@ -203,6 +206,45 @@ namespace Avalonia.Controls
remove => _childIndexChanged -= value;
}
public event EventHandler<RoutedEventArgs> HorizontalSnapPointsChanged
{
add
{
if (_itemsPresenter != null)
{
_itemsPresenter.HorizontalSnapPointsChanged += value;
}
}
remove
{
if (_itemsPresenter != null)
{
_itemsPresenter.HorizontalSnapPointsChanged -= value;
}
}
}
public event EventHandler<RoutedEventArgs> VerticalSnapPointsChanged
{
add
{
if (_itemsPresenter != null)
{
_itemsPresenter.VerticalSnapPointsChanged += value;
}
}
remove
{
if (_itemsPresenter != null)
{
_itemsPresenter.VerticalSnapPointsChanged -= value;
}
}
}
/// <summary>
/// Returns the container for the item at the specified index.
/// </summary>
@ -254,7 +296,8 @@ namespace Avalonia.Controls
/// Gets the currently realized containers.
/// </summary>
public IEnumerable<Control> GetRealizedContainers() => Presenter?.GetRealizedContainers() ?? Array.Empty<Control>();
public bool AreHorizontalSnapPointsRegular => _itemsPresenter?.AreHorizontalSnapPointsRegular ?? false;
public bool AreVerticalSnapPointsRegular => _itemsPresenter?.AreVerticalSnapPointsRegular ?? false;
/// <summary>
/// Creates or a container that can be used to display an item.
@ -355,6 +398,7 @@ namespace Avalonia.Controls
{
base.OnApplyTemplate(e);
_scrollViewer = e.NameScope.Find<ScrollViewer>("PART_ScrollViewer");
_itemsPresenter = e.NameScope.Find<ItemsPresenter>("PART_ItemsPresenter");
}
/// <summary>
@ -671,5 +715,17 @@ namespace Avalonia.Controls
count = ItemsView.Count;
return true;
}
public IReadOnlyList<double> GetIrregularSnapPoints(Orientation orientation, SnapPointsAlignment snapPointsAlignment)
{
return _itemsPresenter?.GetIrregularSnapPoints(orientation, snapPointsAlignment) ?? new List<double>();
}
public double GetRegularSnapPoints(Orientation orientation, SnapPointsAlignment snapPointsAlignment, out double offset)
{
offset = 0;
return _itemsPresenter?.GetRegularSnapPoints(orientation, snapPointsAlignment, out offset) ?? 0;
}
}
}

67
src/Avalonia.Controls/Presenters/ItemsPresenter.cs

@ -3,13 +3,15 @@ using System.Collections.Generic;
using System.Diagnostics;
using Avalonia.Controls.Primitives;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Layout;
namespace Avalonia.Controls.Presenters
{
/// <summary>
/// Presents items inside an <see cref="Avalonia.Controls.ItemsControl"/>.
/// </summary>
public class ItemsPresenter : Control, ILogicalScrollable
public class ItemsPresenter : Control, ILogicalScrollable, IScrollSnapPointsInfo
{
/// <summary>
/// Defines the <see cref="ItemsPanel"/> property.
@ -21,6 +23,44 @@ namespace Avalonia.Controls.Presenters
private ILogicalScrollable? _logicalScrollable;
private EventHandler? _scrollInvalidated;
public event EventHandler<RoutedEventArgs> HorizontalSnapPointsChanged
{
add
{
if (Panel is IScrollSnapPointsInfo scrollSnapPointsInfo)
{
scrollSnapPointsInfo.HorizontalSnapPointsChanged += value;
}
}
remove
{
if (Panel is IScrollSnapPointsInfo scrollSnapPointsInfo)
{
scrollSnapPointsInfo.HorizontalSnapPointsChanged -= value;
}
}
}
public event EventHandler<RoutedEventArgs> VerticalSnapPointsChanged
{
add
{
if (Panel is IScrollSnapPointsInfo scrollSnapPointsInfo)
{
scrollSnapPointsInfo.VerticalSnapPointsChanged += value;
}
}
remove
{
if (Panel is IScrollSnapPointsInfo scrollSnapPointsInfo)
{
scrollSnapPointsInfo.VerticalSnapPointsChanged -= value;
}
}
}
static ItemsPresenter()
{
KeyboardNavigation.TabNavigationProperty.OverrideDefaultValue(
@ -89,6 +129,9 @@ namespace Avalonia.Controls.Presenters
Size IScrollable.Extent => _logicalScrollable?.Extent ?? default;
Size IScrollable.Viewport => _logicalScrollable?.Viewport ?? default;
public bool AreHorizontalSnapPointsRegular => Panel is IScrollSnapPointsInfo scrollSnapPointsInfo ? scrollSnapPointsInfo.AreHorizontalSnapPointsRegular : false;
public bool AreVerticalSnapPointsRegular => Panel is IScrollSnapPointsInfo scrollSnapPointsInfo ? scrollSnapPointsInfo.AreVerticalSnapPointsRegular : false;
public override sealed void ApplyTemplate()
{
if (Panel is null && ItemsControl is not null)
@ -204,5 +247,27 @@ namespace Avalonia.Controls.Presenters
}
private void OnLogicalScrollInvalidated(object? sender, EventArgs e) => _scrollInvalidated?.Invoke(this, e);
public IReadOnlyList<double> GetIrregularSnapPoints(Orientation orientation, SnapPointsAlignment snapPointsAlignment)
{
if(Panel is IScrollSnapPointsInfo scrollSnapPointsInfo)
{
return scrollSnapPointsInfo.GetIrregularSnapPoints(orientation, snapPointsAlignment);
}
return new List<double>();
}
public double GetRegularSnapPoints(Orientation orientation, SnapPointsAlignment snapPointsAlignment, out double offset)
{
if (Panel is IScrollSnapPointsInfo scrollSnapPointsInfo)
{
return scrollSnapPointsInfo.GetRegularSnapPoints(orientation, snapPointsAlignment, out offset);
}
offset = 0;
return 0;
}
}
}

Loading…
Cancel
Save