// Copyright (c) The Avalonia Project. All rights reserved.
// Licensed under the MIT license. See licence.md file in the project root for full license information.
using System;
using System.Linq;
using System.Windows.Input;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Rendering;
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 button control.
///
public class Button : ContentControl
{
///
/// Defines the property.
///
public static readonly StyledProperty ClickModeProperty =
AvaloniaProperty.Register(nameof(ClickMode));
///
/// Defines the property.
///
public static readonly StyledProperty CommandProperty =
AvaloniaProperty.Register(nameof(Command));
///
/// 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 event.
///
public static readonly RoutedEvent ClickEvent =
RoutedEvent.Register("Click", RoutingStrategies.Bubble);
///
/// Initializes static members of the class.
///
static Button()
{
FocusableProperty.OverrideDefaultValue(typeof(Button), true);
ClickEvent.AddClassHandler(x => x.OnClick);
CommandProperty.Changed.Subscribe(CommandChanged);
IsDefaultProperty.Changed.Subscribe(IsDefaultChanged);
}
///
/// 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 { return GetValue(ClickModeProperty); }
set { SetValue(ClickModeProperty, value); }
}
///
/// Gets or sets an to be invoked when the button is clicked.
///
public ICommand Command
{
get { return GetValue(CommandProperty); }
set { SetValue(CommandProperty, value); }
}
///
/// Gets or sets an associated with this control
///
public KeyGesture HotKey
{
get { return GetValue(HotKeyProperty); }
set { SetValue(HotKeyProperty, value); }
}
///
/// Gets or sets a parameter to be passed to the .
///
public object CommandParameter
{
get { return 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 { return GetValue(IsDefaultProperty); }
set { SetValue(IsDefaultProperty, value); }
}
///
protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
{
base.OnAttachedToVisualTree(e);
if (IsDefault)
{
var inputElement = e.Root as IInputElement;
if (inputElement != null)
{
ListenForDefault(inputElement);
}
}
}
///
protected override void OnKeyDown(KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
RaiseClickEvent();
e.Handled = true;
}
else if (e.Key == Key.Space)
{
if (ClickMode == ClickMode.Press)
{
RaiseClickEvent();
}
e.Handled = true;
}
base.OnKeyDown(e);
}
///
protected override void OnKeyUp(KeyEventArgs e)
{
if (e.Key == Key.Space)
{
if (ClickMode == ClickMode.Release)
{
RaiseClickEvent();
}
e.Handled = true;
}
}
///
protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e)
{
base.OnDetachedFromVisualTree(e);
if (IsDefault)
{
var inputElement = e.Root as IInputElement;
if (inputElement != null)
{
StopListeningForDefault(inputElement);
}
}
}
///
/// Invokes the event.
///
/// The event args.
protected virtual void OnClick(RoutedEventArgs e)
{
if (Command != null)
{
Command.Execute(CommandParameter);
e.Handled = true;
}
}
///
protected override void OnPointerPressed(PointerPressedEventArgs e)
{
base.OnPointerPressed(e);
if (e.MouseButton == MouseButton.Left)
{
PseudoClasses.Add(":pressed");
e.Device.Capture(this);
e.Handled = true;
if (ClickMode == ClickMode.Press)
{
RaiseClickEvent();
}
}
}
///
protected override void OnPointerReleased(PointerReleasedEventArgs e)
{
base.OnPointerReleased(e);
if (e.MouseButton == MouseButton.Left)
{
e.Device.Capture(null);
PseudoClasses.Remove(":pressed");
e.Handled = true;
if (ClickMode == ClickMode.Release && new Rect(Bounds.Size).Contains(e.GetPosition(this)))
{
RaiseClickEvent();
}
}
}
///
/// Called when the property changes.
///
/// The event args.
private static void CommandChanged(AvaloniaPropertyChangedEventArgs e)
{
var button = e.Sender as Button;
if (button != null)
{
var oldCommand = e.OldValue as ICommand;
var newCommand = e.NewValue as ICommand;
if (oldCommand != null)
{
oldCommand.CanExecuteChanged -= button.CanExecuteChanged;
}
if (newCommand != null)
{
newCommand.CanExecuteChanged += button.CanExecuteChanged;
}
button.CanExecuteChanged(button, EventArgs.Empty);
}
}
///
/// Called when the property changes.
///
/// The event args.
private static void IsDefaultChanged(AvaloniaPropertyChangedEventArgs e)
{
var button = e.Sender as Button;
var isDefault = (bool)e.NewValue;
var inputRoot = button?.VisualRoot as IInputElement;
if (inputRoot != null)
{
if (isDefault)
{
button.ListenForDefault(inputRoot);
}
else
{
button.StopListeningForDefault(inputRoot);
}
}
}
///
/// Called when the event fires.
///
/// The event sender.
/// The event args.
private void CanExecuteChanged(object sender, EventArgs e)
{
// HACK: Just set the IsEnabled property for the moment. This needs to be changed to
// use IsEnabledCore etc. but it will do for now.
IsEnabled = Command == null || Command.CanExecute(CommandParameter);
}
///
/// Starts listening for the Enter key when the button .
///
/// The input root.
private void ListenForDefault(IInputElement root)
{
root.AddHandler(KeyDownEvent, RootKeyDown);
}
///
/// Stops listening for the Enter key when the button is no longer .
///
/// The input root.
private void StopListeningForDefault(IInputElement root)
{
root.RemoveHandler(KeyDownEvent, RootKeyDown);
}
///
/// Raises the event.
///
private void RaiseClickEvent()
{
RoutedEventArgs click = new RoutedEventArgs
{
RoutedEvent = ClickEvent,
};
RaiseEvent(click);
}
///
/// Called when a key is pressed on the input root and the button .
///
/// The event sender.
/// The event args.
private void RootKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter && IsVisible && IsEnabled)
{
RaiseClickEvent();
}
}
}
}