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;
using Avalonia.LogicalTree;
using Avalonia.VisualTree;
namespace Avalonia.Controls
{
///
/// Defines how a reacts to clicks.
///
public enum ClickMode
{
///
/// The event is raised when the pointer is released.
///
Release,
///
/// The event is raised when the pointer is pressed.
///
Press,
}
///
/// A standard button control.
///
[PseudoClasses(pcFlyoutOpen, pcPressed)]
public class Button : ContentControl, ICommandSource, IClickableControl
{
protected const string pcPressed = ":pressed";
protected const string pcFlyoutOpen = ":flyout-open";
///
/// Defines the property.
///
public static readonly StyledProperty ClickModeProperty =
AvaloniaProperty.Register(nameof(ClickMode));
///
/// Defines the property.
///
public static readonly DirectProperty CommandProperty =
AvaloniaProperty.RegisterDirect(nameof(Command),
button => button.Command, (button, command) => button.Command = command, enableDataValidation: true);
///
/// Defines the property.
///
public static readonly StyledProperty HotKeyProperty =
HotKeyManager.HotKeyProperty.AddOwner();
///
/// Defines the property.
///
public static readonly StyledProperty CommandParameterProperty =
AvaloniaProperty.Register(nameof(CommandParameter));
///
/// Defines the property.
///
public static readonly StyledProperty IsDefaultProperty =
AvaloniaProperty.Register(nameof(IsDefault));
///
/// Defines the property.
///
public static readonly StyledProperty IsCancelProperty =
AvaloniaProperty.Register(nameof(IsCancel));
///
/// Defines the event.
///
public static readonly RoutedEvent ClickEvent =
RoutedEvent.Register(nameof(Click), RoutingStrategies.Bubble);
///
/// Defines the property.
///
public static readonly StyledProperty IsPressedProperty =
AvaloniaProperty.Register(nameof(IsPressed));
///
/// Defines the property
///
public static readonly StyledProperty FlyoutProperty =
AvaloniaProperty.Register(nameof(Flyout));
private ICommand? _command;
private bool _commandCanExecute = true;
private KeyGesture? _hotkey;
private bool _isFlyoutOpen = false;
///
/// Initializes static members of the class.
///
static Button()
{
FocusableProperty.OverrideDefaultValue(typeof(Button), true);
AccessKeyHandler.AccessKeyPressedEvent.AddClassHandler((lbl, args) => lbl.OnAccessKey(args));
}
///
/// Initializes a new instance of the class.
///
public Button()
{
}
///
/// Raised when the user clicks the button.
///
public event EventHandler? Click
{
add => AddHandler(ClickEvent, value);
remove => RemoveHandler(ClickEvent, value);
}
///
/// Gets or sets a value indicating how the should react to clicks.
///
public ClickMode ClickMode
{
get => GetValue(ClickModeProperty);
set => SetValue(ClickModeProperty, value);
}
///
/// Gets or sets an to be invoked when the button is clicked.
///
public ICommand? Command
{
get => _command;
set => SetAndRaise(CommandProperty, ref _command, value);
}
///
/// Gets or sets an associated with this control
///
public KeyGesture? HotKey
{
get => GetValue(HotKeyProperty);
set => SetValue(HotKeyProperty, value);
}
///
/// Gets or sets a parameter to be passed to the .
///
public object? CommandParameter
{
get => GetValue(CommandParameterProperty);
set => SetValue(CommandParameterProperty, value);
}
///
/// Gets or sets a value indicating whether the button is the default button for the
/// window.
///
public bool IsDefault
{
get => GetValue(IsDefaultProperty);
set => SetValue(IsDefaultProperty, value);
}
///
/// Gets or sets a value indicating whether the button is the Cancel button for the
/// window.
///
public bool IsCancel
{
get => GetValue(IsCancelProperty);
set => SetValue(IsCancelProperty, value);
}
///
/// Gets or sets a value indicating whether the button is currently pressed.
///
public bool IsPressed
{
get => GetValue(IsPressedProperty);
private set => SetValue(IsPressedProperty, value);
}
///
/// Gets or sets the Flyout that should be shown with this button.
///
public FlyoutBase? Flyout
{
get => GetValue(FlyoutProperty);
set => SetValue(FlyoutProperty, value);
}
///
protected override bool IsEnabledCore => base.IsEnabledCore && _commandCanExecute;
///
protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
{
base.OnAttachedToVisualTree(e);
if (IsDefault)
{
if (e.Root is IInputElement inputElement)
{
ListenForDefault(inputElement);
}
}
if (IsCancel)
{
if (e.Root is IInputElement inputElement)
{
ListenForCancel(inputElement);
}
}
}
///
protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e)
{
base.OnDetachedFromVisualTree(e);
if (IsDefault)
{
if (e.Root is IInputElement inputElement)
{
StopListeningForDefault(inputElement);
}
}
if (IsCancel)
{
if (e.Root is IInputElement inputElement)
{
StopListeningForCancel(inputElement);
}
}
}
///
protected override void OnAttachedToLogicalTree(LogicalTreeAttachmentEventArgs e)
{
if (_hotkey != null) // Control attached again, set Hotkey to create a hotkey manager for this control
{
HotKey = _hotkey;
}
base.OnAttachedToLogicalTree(e);
if (Command != null)
{
Command.CanExecuteChanged += CanExecuteChanged;
CanExecuteChanged(this, EventArgs.Empty);
}
}
///
protected override void OnDetachedFromLogicalTree(LogicalTreeAttachmentEventArgs e)
{
// This will cause the hotkey manager to dispose the observer and the reference to this control
if (HotKey != null)
{
_hotkey = HotKey;
HotKey = null;
}
base.OnDetachedFromLogicalTree(e);
if (Command != null)
{
Command.CanExecuteChanged -= CanExecuteChanged;
}
}
protected virtual void OnAccessKey(RoutedEventArgs e) => OnClick();
///
protected override void OnKeyDown(KeyEventArgs e)
{
switch (e.Key)
{
case Key.Enter:
OnClick();
e.Handled = true;
break;
case Key.Space:
{
if (ClickMode == ClickMode.Press)
{
OnClick();
}
IsPressed = true;
e.Handled = true;
break;
}
case Key.Escape when Flyout != null:
// If Flyout doesn't have focusable content, close the flyout here
CloseFlyout();
break;
}
base.OnKeyDown(e);
}
///
protected override void OnKeyUp(KeyEventArgs e)
{
if (e.Key == Key.Space)
{
if (ClickMode == ClickMode.Release)
{
OnClick();
}
IsPressed = false;
e.Handled = true;
}
base.OnKeyUp(e);
}
///
/// Invokes the event.
///
protected virtual void OnClick()
{
if (IsEffectivelyEnabled)
{
if (_isFlyoutOpen)
{
CloseFlyout();
}
else
{
OpenFlyout();
}
var e = new RoutedEventArgs(ClickEvent);
RaiseEvent(e);
if (!e.Handled && Command?.CanExecute(CommandParameter) == true)
{
Command.Execute(CommandParameter);
e.Handled = true;
}
}
}
///
/// Opens the button's flyout.
///
protected virtual void OpenFlyout()
{
Flyout?.ShowAt(this);
}
///
/// Closes the button's flyout.
///
protected virtual void CloseFlyout()
{
Flyout?.Hide();
}
///
/// Invoked when the button's flyout is opened.
///
protected virtual void OnFlyoutOpened()
{
// Available for derived types
}
///
/// Invoked when the button's flyout is closed.
///
protected virtual void OnFlyoutClosed()
{
// Available for derived types
}
///
protected override void OnPointerPressed(PointerPressedEventArgs e)
{
base.OnPointerPressed(e);
if (e.GetCurrentPoint(this).Properties.IsLeftButtonPressed)
{
IsPressed = true;
if (ClickMode == ClickMode.Press)
{
e.Handled = true;
OnClick();
}
}
}
///
protected override void OnPointerReleased(PointerReleasedEventArgs e)
{
base.OnPointerReleased(e);
if (IsPressed && e.InitialPressMouseButton == MouseButton.Left)
{
IsPressed = false;
if (ClickMode == ClickMode.Release &&
this.GetVisualsAt(e.GetPosition(this)).Any(c => this == c || this.IsVisualAncestorOf(c)))
{
e.Handled = true;
OnClick();
}
}
}
///
protected override void OnPointerCaptureLost(PointerCaptureLostEventArgs e)
{
base.OnPointerCaptureLost(e);
IsPressed = false;
}
///
protected override void OnLostFocus(RoutedEventArgs e)
{
base.OnLostFocus(e);
IsPressed = false;
}
///
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{
base.OnApplyTemplate(e);
UnregisterFlyoutEvents(Flyout);
RegisterFlyoutEvents(Flyout);
UpdatePseudoClasses();
}
///
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
{
base.OnPropertyChanged(change);
if (change.Property == CommandProperty)
{
if (((ILogical)this).IsAttachedToLogicalTree)
{
var (oldValue, newValue) = change.GetOldAndNewValue();
if (oldValue is ICommand oldCommand)
{
oldCommand.CanExecuteChanged -= CanExecuteChanged;
}
if (newValue is ICommand newCommand)
{
newCommand.CanExecuteChanged += CanExecuteChanged;
}
}
CanExecuteChanged(this, EventArgs.Empty);
}
else if (change.Property == CommandParameterProperty)
{
CanExecuteChanged(this, EventArgs.Empty);
}
else if (change.Property == IsCancelProperty)
{
var isCancel = change.GetNewValue();
if (VisualRoot is IInputElement inputRoot)
{
if (isCancel)
{
ListenForCancel(inputRoot);
}
else
{
StopListeningForCancel(inputRoot);
}
}
}
else if (change.Property == IsDefaultProperty)
{
var isDefault = change.GetNewValue();
if (VisualRoot is IInputElement inputRoot)
{
if (isDefault)
{
ListenForDefault(inputRoot);
}
else
{
StopListeningForDefault(inputRoot);
}
}
}
else if (change.Property == IsPressedProperty)
{
UpdatePseudoClasses();
}
else if (change.Property == FlyoutProperty)
{
var (oldFlyout, newFlyout) = change.GetOldAndNewValue();
// If flyout is changed while one is already open, make sure we
// close the old one first
if (oldFlyout != null && oldFlyout.IsOpen)
{
oldFlyout.Hide();
}
// Must unregister events here while a reference to the old flyout still exists
UnregisterFlyoutEvents(oldFlyout);
RegisterFlyoutEvents(newFlyout);
UpdatePseudoClasses();
}
}
protected override AutomationPeer OnCreateAutomationPeer() => new ButtonAutomationPeer(this);
///
protected override void UpdateDataValidation(
AvaloniaProperty property,
BindingValueType state,
Exception? error)
{
base.UpdateDataValidation(property, state, error);
if (property == CommandProperty)
{
if (state == BindingValueType.BindingError)
{
if (_commandCanExecute)
{
_commandCanExecute = false;
UpdateIsEffectivelyEnabled();
}
}
}
}
internal void PerformClick() => OnClick();
///
/// Called when the event fires.
///
/// The event sender.
/// The event args.
private void CanExecuteChanged(object? sender, EventArgs e)
{
var canExecute = Command == null || Command.CanExecute(CommandParameter);
if (canExecute != _commandCanExecute)
{
_commandCanExecute = canExecute;
UpdateIsEffectivelyEnabled();
}
}
///
/// Registers all flyout events.
///
/// The flyout to connect events to.
private void RegisterFlyoutEvents(FlyoutBase? flyout)
{
if (flyout != null)
{
flyout.Opened += Flyout_Opened;
flyout.Closed += Flyout_Closed;
}
}
///
/// Explicitly unregisters all flyout events.
///
/// The flyout to disconnect events from.
private void UnregisterFlyoutEvents(FlyoutBase? flyout)
{
if (flyout != null)
{
flyout.Opened -= Flyout_Opened;
flyout.Closed -= Flyout_Closed;
}
}
///
/// Starts listening for the Enter key when the button .
///
/// The input root.
private void ListenForDefault(IInputElement root)
{
root.AddHandler(KeyDownEvent, RootDefaultKeyDown);
}
///
/// Starts listening for the Escape key when the button .
///
/// The input root.
private void ListenForCancel(IInputElement root)
{
root.AddHandler(KeyDownEvent, RootCancelKeyDown);
}
///
/// Stops listening for the Enter key when the button is no longer .
///
/// The input root.
private void StopListeningForDefault(IInputElement root)
{
root.RemoveHandler(KeyDownEvent, RootDefaultKeyDown);
}
///
/// Stops listening for the Escape key when the button is no longer .
///
/// The input root.
private void StopListeningForCancel(IInputElement root)
{
root.RemoveHandler(KeyDownEvent, RootCancelKeyDown);
}
///
/// Called when a key is pressed on the input root and the button .
///
/// The event sender.
/// The event args.
private void RootDefaultKeyDown(object? sender, KeyEventArgs e)
{
if (e.Key == Key.Enter && IsVisible && IsEnabled)
{
OnClick();
e.Handled = true;
}
}
///
/// Called when a key is pressed on the input root and the button .
///
/// The event sender.
/// The event args.
private void RootCancelKeyDown(object? sender, KeyEventArgs e)
{
if (e.Key == Key.Escape && IsVisible && IsEnabled)
{
OnClick();
e.Handled = true;
}
}
///
/// Updates the visual state of the control by applying latest PseudoClasses.
///
private void UpdatePseudoClasses()
{
PseudoClasses.Set(pcFlyoutOpen, _isFlyoutOpen);
PseudoClasses.Set(pcPressed, IsPressed);
}
void ICommandSource.CanExecuteChanged(object sender, EventArgs e) => this.CanExecuteChanged(sender, e);
void IClickableControl.RaiseClick() => OnClick();
///
/// Event handler for when the button's flyout is opened.
///
private void Flyout_Opened(object? sender, EventArgs e)
{
var flyout = sender as FlyoutBase;
// It is possible to share flyouts among multiple controls including Button.
// This can cause a problem here since all controls that share a flyout receive
// the same Opened/Closed events at the same time.
// For Button that means they all would be updating their pseudoclasses accordingly.
// In other words, all Buttons with a shared Flyout would have the backgrounds changed together.
// To fix this, only continue here if the Flyout target matches this Button instance.
if (object.ReferenceEquals(flyout?.Target, this))
{
_isFlyoutOpen = true;
UpdatePseudoClasses();
OnFlyoutOpened();
}
}
///
/// Event handler for when the button's flyout is closed.
///
private void Flyout_Closed(object? sender, EventArgs e)
{
var flyout = sender as FlyoutBase;
// See comments in Flyout_Opened
if (object.ReferenceEquals(flyout?.Target, this))
{
_isFlyoutOpen = false;
UpdatePseudoClasses();
OnFlyoutClosed();
}
}
}
}