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

Convert various DirectProperty definitions to StyledProperty #10370

Merged
Show file tree
Hide file tree
Changes from all commits
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
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: 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
3 changes: 1 addition & 2 deletions src/Avalonia.Base/StyledElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ public class StyledElement : Animatable,
public static readonly DirectProperty<StyledElement, AvaloniaObject?> TemplatedParentProperty =
AvaloniaProperty.RegisterDirect<StyledElement, AvaloniaObject?>(
nameof(TemplatedParent),
o => o.TemplatedParent,
(o ,v) => o.TemplatedParent = v);
o => o.TemplatedParent);

/// <summary>
/// Defines the <see cref="Theme"/> property.
Expand Down
7 changes: 6 additions & 1 deletion src/Avalonia.Base/StyledProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,14 @@ public StyledProperty(
/// </summary>
/// <typeparam name="TOwner">The type of the additional owner.</typeparam>
/// <returns>The property.</returns>
public StyledProperty<TValue> AddOwner<TOwner>() where TOwner : AvaloniaObject
public StyledProperty<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
45 changes: 21 additions & 24 deletions src/Avalonia.Controls/Button.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
using System;
using System.Diagnostics;
using System.Linq;
using System.Windows.Input;
using Avalonia.Automation.Peers;
using Avalonia.Controls.Metadata;
using Avalonia.Controls.Primitives;
using Avalonia.Controls.Templates;
using Avalonia.Data;
using Avalonia.Input;
using Avalonia.Interactivity;
Expand Down Expand Up @@ -48,9 +46,8 @@ public class Button : ContentControl, ICommandSource, IClickableControl
/// <summary>
/// Defines the <see cref="Command"/> property.
/// </summary>
public static readonly DirectProperty<Button, ICommand?> CommandProperty =
AvaloniaProperty.RegisterDirect<Button, ICommand?>(nameof(Command),
button => button.Command, (button, command) => button.Command = command, enableDataValidation: true);
public static readonly StyledProperty<ICommand?> CommandProperty =
AvaloniaProperty.Register<Button, ICommand?>(nameof(Command), enableDataValidation: true);

/// <summary>
/// Defines the <see cref="HotKey"/> property.
Expand Down Expand Up @@ -85,19 +82,19 @@ public class Button : ContentControl, ICommandSource, IClickableControl
/// <summary>
/// Defines the <see cref="IsPressed"/> property.
/// </summary>
public static readonly StyledProperty<bool> IsPressedProperty =
AvaloniaProperty.Register<Button, bool>(nameof(IsPressed));
public static readonly DirectProperty<Button, bool> IsPressedProperty =
AvaloniaProperty.RegisterDirect<Button, bool>(nameof(IsPressed), b => b.IsPressed);

/// <summary>
/// Defines the <see cref="Flyout"/> property
/// </summary>
public static readonly StyledProperty<FlyoutBase?> FlyoutProperty =
AvaloniaProperty.Register<Button, FlyoutBase?>(nameof(Flyout));

private ICommand? _command;
private bool _commandCanExecute = true;
private KeyGesture? _hotkey;
private bool _isFlyoutOpen = false;
private bool _isPressed = false;

/// <summary>
/// Initializes static members of the <see cref="Button"/> class.
Expand Down Expand Up @@ -138,8 +135,8 @@ public ClickMode ClickMode
/// </summary>
public ICommand? Command
{
get => _command;
set => SetAndRaise(CommandProperty, ref _command, value);
get => GetValue(CommandProperty);
set => SetValue(CommandProperty, value);
}

/// <summary>
Expand Down Expand Up @@ -185,8 +182,8 @@ public bool IsCancel
/// </summary>
public bool IsPressed
{
get => GetValue(IsPressedProperty);
private set => SetValue(IsPressedProperty, value);
get => _isPressed;
private set => SetAndRaise(IsPressedProperty, ref _isPressed, value);
}

/// <summary>
Expand Down Expand Up @@ -248,7 +245,7 @@ protected override void OnAttachedToLogicalTree(LogicalTreeAttachmentEventArgs e
{
if (_hotkey != null) // Control attached again, set Hotkey to create a hotkey manager for this control
{
HotKey = _hotkey;
SetCurrentValue(HotKeyProperty, _hotkey);
}

base.OnAttachedToLogicalTree(e);
Expand All @@ -267,7 +264,7 @@ protected override void OnDetachedFromLogicalTree(LogicalTreeAttachmentEventArgs
if (HotKey != null)
{
_hotkey = HotKey;
HotKey = null;
SetCurrentValue(HotKeyProperty, null);
}

base.OnDetachedFromLogicalTree(e);
Expand All @@ -291,17 +288,17 @@ protected override void OnKeyDown(KeyEventArgs e)
break;

case Key.Space:
{
if (ClickMode == ClickMode.Press)
{
OnClick();
if (ClickMode == ClickMode.Press)
{
OnClick();
}

IsPressed = true;
e.Handled = true;
break;
}

IsPressed = true;
e.Handled = true;
break;
}

case Key.Escape when Flyout != null:
// If Flyout doesn't have focusable content, close the flyout here
CloseFlyout();
Expand Down Expand Up @@ -592,7 +589,7 @@ private void UnregisterFlyoutEvents(FlyoutBase? flyout)
{
flyout.Opened -= Flyout_Opened;
flyout.Closed -= Flyout_Closed;
}
}
}

/// <summary>
Expand Down Expand Up @@ -671,7 +668,7 @@ private void UpdatePseudoClasses()
void ICommandSource.CanExecuteChanged(object sender, EventArgs e) => this.CanExecuteChanged(sender, e);

void IClickableControl.RaiseClick() => OnClick();

/// <summary>
/// Event handler for when the button's flyout is opened.
/// </summary>
Expand Down
Loading