Skip to content

Commit

Permalink
Converted a variety of writeable DirectProperty definitions to Styled…
Browse files Browse the repository at this point in the history
…Property
  • Loading branch information
TomEdwardsEnscape committed Feb 21, 2023
1 parent b8b6324 commit 775d33f
Show file tree
Hide file tree
Showing 49 changed files with 691 additions and 830 deletions.
7 changes: 6 additions & 1 deletion src/Avalonia.Base/AttachedProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,14 @@ public AttachedProperty(
/// </summary>
/// <typeparam name="TOwner">The owner type.</typeparam>
/// <returns>The property.</returns>
public new AttachedProperty<TValue> AddOwner<TOwner>() where TOwner : AvaloniaObject
public new AttachedProperty<TValue> AddOwner<TOwner>(StyledPropertyMetadata<TValue>? metadata = null) where TOwner : AvaloniaObject
{
AvaloniaPropertyRegistry.Instance.Register(typeof(TOwner), this);
if (metadata != null)
{
OverrideMetadata<TOwner>(metadata);
}

return this;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using System.Collections.Generic;
using Avalonia.Controls;
using Avalonia.LogicalTree;
using Avalonia.Styling;
using Avalonia.Reactive;

namespace Avalonia.Input.GestureRecognizers
{
Expand All @@ -11,13 +11,13 @@ public class GestureRecognizerCollection : IReadOnlyCollection<IGestureRecognize
private readonly IInputElement _inputElement;
private List<IGestureRecognizer>? _recognizers;
private Dictionary<IPointer, IGestureRecognizer>? _pointerGrabs;


public GestureRecognizerCollection(IInputElement inputElement)
{
_inputElement = inputElement;
}

public void Add(IGestureRecognizer recognizer)
{
if (_recognizers == null)
Expand All @@ -31,14 +31,13 @@ public void Add(IGestureRecognizer recognizer)
recognizer.Initialize(_inputElement, this);

// Hacks to make bindings work

if (_inputElement is ILogical logicalParent && recognizer is ISetLogicalParent logical)
{
logical.SetParent(logicalParent);
if (recognizer is StyledElement styleableRecognizer
&& _inputElement is StyledElement styleableParent)
styleableRecognizer.Bind(StyledElement.TemplatedParentProperty,
styleableParent.GetObservable(StyledElement.TemplatedParentProperty));
styleableParent.GetObservable(StyledElement.TemplatedParentProperty).Subscribe(parent => styleableRecognizer.TemplatedParent = parent);
}
}

Expand All @@ -58,7 +57,7 @@ internal bool HandlePointerPressed(PointerPressedEventArgs e)
return false;
foreach (var r in _recognizers)
{
if(e.Handled)
if (e.Handled)
break;
r.PointerPressed(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,18 @@ public class PullGestureRecognizer : StyledElement, IGestureRecognizer
private Point _initialPosition;
private int _gestureId;
private IPointer? _tracking;
private PullDirection _pullDirection;
private bool _pullInProgress;

/// <summary>
/// Defines the <see cref="PullDirection"/> property.
/// </summary>
public static readonly DirectProperty<PullGestureRecognizer, PullDirection> PullDirectionProperty =
AvaloniaProperty.RegisterDirect<PullGestureRecognizer, PullDirection>(
nameof(PullDirection),
o => o.PullDirection,
(o, v) => o.PullDirection = v);
public static readonly StyledProperty<PullDirection> PullDirectionProperty =
AvaloniaProperty.Register<PullGestureRecognizer, PullDirection>(nameof(PullDirection));

public PullDirection PullDirection
{
get => _pullDirection;
set => SetAndRaise(PullDirectionProperty, ref _pullDirection, value);
get => GetValue(PullDirectionProperty);
set => SetValue(PullDirectionProperty, value);
}

public PullGestureRecognizer(PullDirection pullDirection)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,88 +17,72 @@ public class ScrollGestureRecognizer
private IPointer? _tracking;
private IInputElement? _target;
private IGestureRecognizerActionsDispatcher? _actions;
private bool _canHorizontallyScroll;
private bool _canVerticallyScroll;
private int _gestureId;
private int _scrollStartDistance = 30;
private Point _pointerPressedPoint;
private VelocityTracker? _velocityTracker;

// Movement per second
private Vector _inertia;
private ulong? _lastMoveTimestamp;
private bool _isScrollInertiaEnabled;

/// <summary>
/// Defines the <see cref="CanHorizontallyScroll"/> property.
/// </summary>
public static readonly DirectProperty<ScrollGestureRecognizer, bool> CanHorizontallyScrollProperty =
AvaloniaProperty.RegisterDirect<ScrollGestureRecognizer, bool>(
nameof(CanHorizontallyScroll),
o => o.CanHorizontallyScroll,
(o, v) => o.CanHorizontallyScroll = v);
public static readonly StyledProperty<bool> CanHorizontallyScrollProperty =
AvaloniaProperty.Register<ScrollGestureRecognizer, bool>(nameof(CanHorizontallyScroll));

/// <summary>
/// Defines the <see cref="CanVerticallyScroll"/> property.
/// </summary>
public static readonly DirectProperty<ScrollGestureRecognizer, bool> CanVerticallyScrollProperty =
AvaloniaProperty.RegisterDirect<ScrollGestureRecognizer, bool>(
nameof(CanVerticallyScroll),
o => o.CanVerticallyScroll,
(o, v) => o.CanVerticallyScroll = v);
public static readonly StyledProperty<bool> CanVerticallyScrollProperty =
AvaloniaProperty.Register<ScrollGestureRecognizer, bool>(nameof(CanVerticallyScroll));

/// <summary>
/// Defines the <see cref="IsScrollInertiaEnabled"/> property.
/// </summary>
public static readonly DirectProperty<ScrollGestureRecognizer, bool> IsScrollInertiaEnabledProperty =
AvaloniaProperty.RegisterDirect<ScrollGestureRecognizer, bool>(
nameof(IsScrollInertiaEnabled),
o => o.IsScrollInertiaEnabled,
(o, v) => o.IsScrollInertiaEnabled = v);
public static readonly StyledProperty<bool> IsScrollInertiaEnabledProperty =
AvaloniaProperty.Register<ScrollGestureRecognizer, bool>(nameof(IsScrollInertiaEnabled));

/// <summary>
/// Defines the <see cref="ScrollStartDistance"/> property.
/// </summary>
public static readonly DirectProperty<ScrollGestureRecognizer, int> ScrollStartDistanceProperty =
AvaloniaProperty.RegisterDirect<ScrollGestureRecognizer, int>(
nameof(ScrollStartDistance),
o => o.ScrollStartDistance,
(o, v) => o.ScrollStartDistance = v);
public static readonly StyledProperty<int> ScrollStartDistanceProperty =
AvaloniaProperty.Register<ScrollGestureRecognizer, int>(nameof(ScrollStartDistance), 30);

/// <summary>
/// Gets or sets a value indicating whether the content can be scrolled horizontally.
/// </summary>
public bool CanHorizontallyScroll
{
get => _canHorizontallyScroll;
set => SetAndRaise(CanHorizontallyScrollProperty, ref _canHorizontallyScroll, value);
get => GetValue(CanHorizontallyScrollProperty);
set => SetValue(CanHorizontallyScrollProperty, value);
}

/// <summary>
/// Gets or sets a value indicating whether the content can be scrolled horizontally.
/// </summary>
public bool CanVerticallyScroll
{
get => _canVerticallyScroll;
set => SetAndRaise(CanVerticallyScrollProperty, ref _canVerticallyScroll, value);
get => GetValue(CanVerticallyScrollProperty);
set => SetValue(CanVerticallyScrollProperty, value);
}

/// <summary>
/// Gets or sets whether the gesture should include inertia in it's behavior.
/// </summary>
public bool IsScrollInertiaEnabled
{
get => _isScrollInertiaEnabled;
set => SetAndRaise(IsScrollInertiaEnabledProperty, ref _isScrollInertiaEnabled, value);
get => GetValue(IsScrollInertiaEnabledProperty);
set => SetValue(IsScrollInertiaEnabledProperty, value);
}

/// <summary>
/// Gets or sets a value indicating the distance the pointer moves before scrolling is started
/// </summary>
public int ScrollStartDistance
{
get => _scrollStartDistance;
set => SetAndRaise(ScrollStartDistanceProperty, ref _scrollStartDistance, value);
get => GetValue(ScrollStartDistanceProperty);
set => SetValue(ScrollStartDistanceProperty, value);
}


Expand Down Expand Up @@ -137,8 +121,8 @@ public void PointerMoved(PointerEventArgs e)

// Correct _trackedRootPoint with ScrollStartDistance, so scrolling does not start with a skip of ScrollStartDistance
_trackedRootPoint = new Point(
_trackedRootPoint.X - (_trackedRootPoint.X >= rootPoint.X ? _scrollStartDistance : -_scrollStartDistance),
_trackedRootPoint.Y - (_trackedRootPoint.Y >= rootPoint.Y ? _scrollStartDistance : -_scrollStartDistance));
_trackedRootPoint.X - (_trackedRootPoint.X >= rootPoint.X ? ScrollStartDistance : -ScrollStartDistance),
_trackedRootPoint.Y - (_trackedRootPoint.Y >= rootPoint.Y ? ScrollStartDistance : -ScrollStartDistance));

_actions!.Capture(e.Pointer, this);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Avalonia.Base/Media/DashStyle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public DashStyle()
/// <param name="offset">The dash sequence offset.</param>
public DashStyle(IEnumerable<double>? dashes, double offset)
{
Dashes = (dashes as AvaloniaList<double>) ?? new AvaloniaList<double>(dashes ?? Array.Empty<double>());
SetCurrentValue(DashesProperty, (dashes as AvaloniaList<double>) ?? new AvaloniaList<double>(dashes ?? Array.Empty<double>()));
Offset = offset;
}

Expand Down
21 changes: 9 additions & 12 deletions src/Avalonia.Base/Media/DrawingGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,8 @@ public class DrawingGroup : Drawing
public static readonly StyledProperty<IBrush?> OpacityMaskProperty =
AvaloniaProperty.Register<DrawingGroup, IBrush?>(nameof(OpacityMask));

public static readonly DirectProperty<DrawingGroup, DrawingCollection> ChildrenProperty =
AvaloniaProperty.RegisterDirect<DrawingGroup, DrawingCollection>(
nameof(Children),
o => o.Children,
(o, v) => o.Children = v);

private DrawingCollection _children = new DrawingCollection();
public static readonly StyledProperty<DrawingCollection> ChildrenProperty =
AvaloniaProperty.Register<DrawingGroup, DrawingCollection>(nameof(Children));

public double Opacity
{
Expand Down Expand Up @@ -60,11 +55,13 @@ public IBrush? OpacityMask
[Content]
public DrawingCollection Children
{
get => _children;
set
{
SetAndRaise(ChildrenProperty, ref _children, value);
}
get => GetValue(ChildrenProperty);
set => SetValue(ChildrenProperty, value);
}

public DrawingGroup()
{
SetCurrentValue(ChildrenProperty, new DrawingCollection());
}

public DrawingContext Open()
Expand Down
49 changes: 25 additions & 24 deletions src/Avalonia.Base/Media/GeometryGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,20 @@ namespace Avalonia.Media
/// </summary>
public class GeometryGroup : Geometry
{
public static readonly DirectProperty<GeometryGroup, GeometryCollection> ChildrenProperty =
AvaloniaProperty.RegisterDirect<GeometryGroup, GeometryCollection> (
nameof(Children),
o => o.Children,
(o, v)=> o.Children = v);
public static readonly StyledProperty<GeometryCollection> ChildrenProperty =
AvaloniaProperty.Register<GeometryGroup, GeometryCollection>(nameof(Children));

public static readonly StyledProperty<FillRule> FillRuleProperty =
AvaloniaProperty.Register<GeometryGroup, FillRule>(nameof(FillRule));

private GeometryCollection _children;

public GeometryGroup()
{
_children = new GeometryCollection
{
Parent = this
};
SetCurrentValue(ChildrenProperty, new() { Parent = this });
}

static GeometryGroup()
{
ChildrenProperty.Changed.AddClassHandler<GeometryGroup>(OnChildrenChanged);
}

/// <summary>
Expand All @@ -35,12 +32,8 @@ public GeometryGroup()
[Content]
public GeometryCollection Children
{
get => _children;
set
{
OnChildrenChanged(_children, value);
SetAndRaise(ChildrenProperty, ref _children, value);
}
get => GetValue(ChildrenProperty);
set => SetValue(ChildrenProperty, value);
}

/// <summary>
Expand All @@ -57,28 +50,36 @@ public override Geometry Clone()
{
var result = new GeometryGroup { FillRule = FillRule, Transform = Transform };

if (_children.Count > 0)
if (Children.Count > 0)
{
result.Children = new GeometryCollection(_children);
result.Children = new GeometryCollection(Children);
}

return result;
}

protected void OnChildrenChanged(GeometryCollection oldChildren, GeometryCollection newChildren)
private static void OnChildrenChanged(GeometryGroup sender, AvaloniaPropertyChangedEventArgs e)
{
oldChildren.Parent = null;
var (oldChildren, newChildren) = e.GetOldAndNewValue<GeometryCollection>();

if (oldChildren is not null)
{
oldChildren.Parent = null;
}

newChildren.Parent = this;
if (newChildren is not null)
{
newChildren.Parent = sender;
}
}

protected override IGeometryImpl? CreateDefiningGeometry()
{
if (_children.Count > 0)
if (Children.Count > 0)
{
var factory = AvaloniaLocator.Current.GetRequiredService<IPlatformRenderInterface>();

return factory.CreateGeometryGroup(FillRule, _children);
return factory.CreateGeometryGroup(FillRule, Children);
}

return null;
Expand Down
2 changes: 1 addition & 1 deletion src/Avalonia.Base/Media/GradientBrush.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ static GradientBrush()
/// </summary>
public GradientBrush()
{
this.GradientStops = new GradientStops();
SetCurrentValue(GradientStopsProperty, new());
}

/// <inheritdoc/>
Expand Down
2 changes: 0 additions & 2 deletions src/Avalonia.Base/Media/Imaging/CroppedBitmap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ public PixelRect SourceRect

public CroppedBitmap()
{
Source = null;
SourceRect = default;
}

public CroppedBitmap(IImage source, PixelRect sourceRect)
Expand Down
Loading

0 comments on commit 775d33f

Please sign in to comment.