Skip to content

Commit

Permalink
Converted several "dataValidation" properties to StyledProperty
Browse files Browse the repository at this point in the history
Changed Button.IsPressed to a read-only DirectProperty
  • Loading branch information
TomEdwardsEnscape committed Feb 27, 2023
1 parent cc13b0b commit dcaaad7
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 114 deletions.
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
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,9 @@ public partial class CalendarDatePicker
/// <summary>
/// Defines the <see cref="SelectedDate"/> property.
/// </summary>
public static readonly DirectProperty<CalendarDatePicker, DateTime?> SelectedDateProperty =
AvaloniaProperty.RegisterDirect<CalendarDatePicker, DateTime?>(
public static readonly StyledProperty<DateTime?> SelectedDateProperty =
AvaloniaProperty.Register<CalendarDatePicker, DateTime?>(
nameof(SelectedDate),
o => o.SelectedDate,
(o, v) => o.SelectedDate = v,
enableDataValidation: true,
defaultBindingMode:BindingMode.TwoWay);

Expand Down Expand Up @@ -211,8 +209,8 @@ public bool IsTodayHighlighted
/// </exception>
public DateTime? SelectedDate
{
get => _selectedDate;
set => SetAndRaise(SelectedDateProperty, ref _selectedDate, value);
get => GetValue(SelectedDateProperty);
set => SetValue(SelectedDateProperty, value);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ public partial class CalendarDatePicker : TemplatedControl
private DateTime? _onOpenSelectedDate;
private bool _settingSelectedDate;

private DateTime? _selectedDate;
private bool _suspendTextChangeHandler;
private bool _isPopupClosing;
private bool _ignoreButtonClick;
Expand Down
22 changes: 9 additions & 13 deletions src/Avalonia.Controls/MenuItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,8 @@ public class MenuItem : HeaderedSelectingItemsControl, IMenuItem, ISelectable, I
/// <summary>
/// Defines the <see cref="Command"/> property.
/// </summary>
public static readonly DirectProperty<MenuItem, ICommand?> CommandProperty =
Button.CommandProperty.AddOwner<MenuItem>(
menuItem => menuItem.Command,
(menuItem, command) => menuItem.Command = command,
enableDataValidation: true);
public static readonly StyledProperty<ICommand?> CommandProperty =
Button.CommandProperty.AddOwner<MenuItem>(new(enableDataValidation: true));

/// <summary>
/// Defines the <see cref="HotKey"/> property.
Expand Down Expand Up @@ -113,7 +110,6 @@ public class MenuItem : HeaderedSelectingItemsControl, IMenuItem, ISelectable, I
private static readonly ITemplate<Panel> DefaultPanel =
new FuncTemplate<Panel>(() => new StackPanel());

private ICommand? _command;
private bool _commandCanExecute = true;
private bool _commandBindingError;
private Popup? _popup;
Expand Down Expand Up @@ -217,8 +213,8 @@ public event EventHandler<RoutedEventArgs>? SubmenuOpened
/// </summary>
public ICommand? Command
{
get { return _command; }
set { SetAndRaise(CommandProperty, ref _command, value); }
get => GetValue(CommandProperty);
set => SetValue(CommandProperty, value);
}

/// <summary>
Expand Down Expand Up @@ -337,15 +333,15 @@ public bool StaysOpenOnClick
/// <remarks>
/// This has the same effect as setting <see cref="IsSubMenuOpen"/> to true.
/// </remarks>
public void Open() => IsSubMenuOpen = true;
public void Open() => SetCurrentValue(IsSubMenuOpenProperty, true);

/// <summary>
/// Closes the submenu.
/// </summary>
/// <remarks>
/// This has the same effect as setting <see cref="IsSubMenuOpen"/> to false.
/// </remarks>
public void Close() => IsSubMenuOpen = false;
public void Close() => SetCurrentValue(IsSubMenuOpenProperty, false);

/// <inheritdoc/>
void IMenuItem.RaiseClick() => RaiseEvent(new RoutedEventArgs(ClickEvent));
Expand All @@ -369,7 +365,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 Down Expand Up @@ -397,7 +393,7 @@ protected override void OnDetachedFromLogicalTree(LogicalTreeAttachmentEventArgs
if (HotKey != null)
{
_hotkey = HotKey;
HotKey = null;
SetCurrentValue(HotKeyProperty, null);
}

base.OnDetachedFromLogicalTree(e);
Expand Down Expand Up @@ -663,7 +659,7 @@ private void SubMenuOpenChanged(AvaloniaPropertyChangedEventArgs e)
}

RaiseEvent(new RoutedEventArgs(SubmenuOpenedEvent));
IsSelected = true;
SetCurrentValue(IsSelectedProperty, true);
PseudoClasses.Add(":open");
}
else
Expand Down
31 changes: 12 additions & 19 deletions src/Avalonia.Controls/NativeMenuItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ namespace Avalonia.Controls
{
public class NativeMenuItem : NativeMenuItemBase, INativeMenuItemExporterEventsImplBridge
{
private ICommand? _command;
private readonly CanExecuteChangedSubscriber _canExecuteChangedSubscriber;

class CanExecuteChangedSubscriber : IWeakEventSubscriber<EventArgs>
Expand Down Expand Up @@ -100,11 +99,8 @@ public NativeMenuItemToggleType ToggleType
set => SetValue(ToggleTypeProperty, value);
}

public static readonly DirectProperty<NativeMenuItem, ICommand?> CommandProperty =
Button.CommandProperty.AddOwner<NativeMenuItem>(
menuItem => menuItem.Command,
(menuItem, command) => menuItem.Command = command,
enableDataValidation: true);
public static readonly StyledProperty<ICommand?> CommandProperty =
Button.CommandProperty.AddOwner<NativeMenuItem>(new(enableDataValidation: true));

/// <summary>
/// Defines the <see cref="CommandParameter"/> property.
Expand All @@ -130,19 +126,8 @@ void CanExecuteChanged()

public ICommand? Command
{
get => _command;
set
{
if (_command != null)
WeakEvents.CommandCanExecuteChanged.Unsubscribe(_command, _canExecuteChangedSubscriber);

SetAndRaise(CommandProperty, ref _command, value);

if (_command != null)
WeakEvents.CommandCanExecuteChanged.Subscribe(_command, _canExecuteChangedSubscriber);

CanExecuteChanged();
}
get => GetValue(CommandProperty);
set => SetValue(CommandProperty, value);
}

/// <summary>
Expand Down Expand Up @@ -180,6 +165,14 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang
throw new InvalidOperationException("NativeMenu already has a parent");
newMenu.Parent = this;
}
else if (change.Property == CommandProperty)
{
if (change.OldValue is ICommand oldCommand)
WeakEvents.CommandCanExecuteChanged.Unsubscribe(oldCommand, _canExecuteChangedSubscriber);
if (change.NewValue is ICommand newCommand)
WeakEvents.CommandCanExecuteChanged.Subscribe(newCommand, _canExecuteChangedSubscriber);
CanExecuteChanged();
}
}
}

Expand Down
Loading

0 comments on commit dcaaad7

Please sign in to comment.