using System;
using System.Linq;
using System.Windows.Input;
using Avalonia.Automation.Peers;
using Avalonia.Controls.Metadata;
using Avalonia.Controls.Platform;
using Avalonia.Controls.Primitives;
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
{
private const string pcPressed = ":pressed";
private const string pcFlyoutOpen = ":flyout-open";
private EventHandler? _canExecuteChangeHandler = default;
private EventHandler CanExecuteChangedHandler => _canExecuteChangeHandler ??= new(CanExecuteChanged);
///
/// Defines the property.
///
public static readonly StyledProperty ClickModeProperty =
AvaloniaProperty.Register(nameof(ClickMode));
///
/// Defines the property.
///
public static readonly StyledProperty CommandProperty =
AvaloniaProperty.Register(nameof(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 DirectProperty IsPressedProperty =
AvaloniaProperty.RegisterDirect(nameof(IsPressed), b => b.IsPressed);
///
/// Defines the property
///
public static readonly StyledProperty FlyoutProperty =
AvaloniaProperty.Register(nameof(Flyout));
private bool _commandCanExecute = true;
private KeyGesture? _hotkey;
private bool _isFlyoutOpen = false;
private bool _isPressed = false;
///
/// Initializes static members of the class.
///
static Button()
{
FocusableProperty.OverrideDefaultValue(typeof(Button), true);
PlatformFeedback.FeedbackTypeProperty.OverrideDefaultValue(typeof(Button), FeedbackType.Auto);
AccessKeyHandler.AccessKeyPressedEvent.AddClassHandler(OnAccessKeyPressed);
}
///
/// 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 => GetValue(CommandProperty);
set => SetValue(CommandProperty, 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 => _isPressed;
private set => SetAndRaise(IsPressedProperty, ref _isPressed, 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.RootVisual is IInputElement inputElement)
{
ListenForDefault(inputElement);
}
}
if (IsCancel)
{
if (e.RootVisual is IInputElement inputElement)
{
ListenForCancel(inputElement);
}
}
}
///
protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e)
{
base.OnDetachedFromVisualTree(e);
if (IsDefault)
{
if (e.RootVisual is IInputElement inputElement)
{
StopListeningForDefault(inputElement);
}
}
if (IsCancel)
{
if (e.RootVisual 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
{
SetCurrentValue(HotKeyProperty, _hotkey);
}
base.OnAttachedToLogicalTree(e);
(var command, var parameter) = (Command, CommandParameter);
if (command is not null)
{
command.CanExecuteChanged += CanExecuteChangedHandler;
CanExecuteChanged(command, parameter);
}
}
///
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;
SetCurrentValue(HotKeyProperty, null);
}
base.OnDetachedFromLogicalTree(e);
if (Command is { } command)
{
command.CanExecuteChanged -= CanExecuteChangedHandler;
}
}
///
protected override void OnAccessKey(RoutedEventArgs e)
{
if (e is AccessKeyEventArgs { IsMultiple: true })
{
base.OnAccessKey(e);
}
else
{
OnClick();
}
}
///
protected override void OnKeyDown(KeyEventArgs e)
{
switch (e.Key)
{
case Key.Enter:
OnClick();
e.Handled = true;
break;
case Key.Space:
// Avoid handling Space if the button isn't focused: a child TextBox might need it for text input
if (IsFocused)
{
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)
{
// Avoid handling Space if the button isn't focused: a child TextBox might need it for text input
if (e.Key == Key.Space && IsFocused)
{
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();
}
this.PerformFeedback(FeedbackAction.Click);
var e = new RoutedEventArgs(ClickEvent);
RaiseEvent(e);
(var command, var parameter) = (Command, CommandParameter);
if (!e.Handled && command is not null && command.CanExecute(parameter))
{
command.Execute(parameter);
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)
{
if (_isFlyoutOpen && IsEffectivelyEnabled)
{
// When a flyout is open with OverlayDismissEventPassThrough enabled and the button is pressed,
// close the flyout, but do not transition to a pressed state
e.Handled = true;
OnClick();
}
else
{
IsPressed = true;
e.Handled = true;
if (ClickMode == ClickMode.Press)
{
OnClick();
}
}
}
}
///
protected override void OnPointerReleased(PointerReleasedEventArgs e)
{
base.OnPointerReleased(e);
if (IsPressed && e.InitialPressMouseButton == MouseButton.Left)
{
IsPressed = false;
e.Handled = true;
if (ClickMode == ClickMode.Release &&
this.GetVisualsAt(e.GetPosition(this)).Any(c => this == c || this.IsVisualAncestorOf(c)))
{
OnClick();
}
}
}
///
protected override void OnPointerCaptureLost(PointerCaptureLostEventArgs e)
{
base.OnPointerCaptureLost(e);
IsPressed = false;
}
///
protected override void OnLostFocus(FocusChangedEventArgs 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)
{
var (oldValue, newValue) = change.GetOldAndNewValue();
if (((ILogical)this).IsAttachedToLogicalTree)
{
if (oldValue is ICommand oldCommand)
{
oldCommand.CanExecuteChanged -= CanExecuteChangedHandler;
}
if (newValue is ICommand newCommand)
{
newCommand.CanExecuteChanged += CanExecuteChangedHandler;
}
}
CanExecuteChanged(newValue, CommandParameter);
}
else if (change.Property == CommandParameterProperty)
{
CanExecuteChanged(Command, change.NewValue);
}
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();
}
(oldFlyout as PopupFlyoutBase)?.SetDefaultPlacementTarget(null);
// Must unregister events here while a reference to the old flyout still exists
UnregisterFlyoutEvents(oldFlyout);
RegisterFlyoutEvents(newFlyout);
(newFlyout as PopupFlyoutBase)?.SetDefaultPlacementTarget(this);
UpdatePseudoClasses();
}
else if (change.Property == IsEffectivelyEnabledProperty)
{
if (!change.GetNewValue())
{
IsPressed = false;
}
}
}
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();
private static void OnAccessKeyPressed(Button sender, AccessKeyPressedEventArgs e)
{
if (e.Handled || e.Target is not null)
return;
e.Target = sender;
e.Handled = true;
}
///
/// Called when the event fires.
///
/// The event sender.
/// The event args.
private void CanExecuteChanged(object? sender, EventArgs e)
{
CanExecuteChanged(Command, CommandParameter);
}
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
private void CanExecuteChanged(ICommand? command, object? parameter)
{
if (!((ILogical)this).IsAttachedToLogicalTree)
{
return;
}
var canExecute = command == null || command.CanExecute(parameter);
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 && IsEffectivelyVisible && IsEffectivelyEnabled)
{
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 && IsEffectivelyVisible && IsEffectivelyEnabled)
{
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();
}
}
}
}