| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501 |
- // 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.Data;
- using Avalonia.Input;
- using Avalonia.Interactivity;
- using Avalonia.LogicalTree;
- using Avalonia.VisualTree;
- namespace Avalonia.Controls
- {
- /// <summary>
- /// Defines how a <see cref="Button"/> reacts to clicks.
- /// </summary>
- public enum ClickMode
- {
- /// <summary>
- /// The <see cref="Button.Click"/> event is raised when the pointer is released.
- /// </summary>
- Release,
- /// <summary>
- /// The <see cref="Button.Click"/> event is raised when the pointer is pressed.
- /// </summary>
- Press,
- }
- /// <summary>
- /// A button control.
- /// </summary>
- public class Button : ContentControl
- {
- /// <summary>
- /// Defines the <see cref="ClickMode"/> property.
- /// </summary>
- public static readonly StyledProperty<ClickMode> ClickModeProperty =
- AvaloniaProperty.Register<Button, ClickMode>(nameof(ClickMode));
- /// <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);
- /// <summary>
- /// Defines the <see cref="HotKey"/> property.
- /// </summary>
- public static readonly StyledProperty<KeyGesture> HotKeyProperty =
- HotKeyManager.HotKeyProperty.AddOwner<Button>();
- /// <summary>
- /// Defines the <see cref="CommandParameter"/> property.
- /// </summary>
- public static readonly StyledProperty<object> CommandParameterProperty =
- AvaloniaProperty.Register<Button, object>(nameof(CommandParameter));
- /// <summary>
- /// Defines the <see cref="IsDefaultProperty"/> property.
- /// </summary>
- public static readonly StyledProperty<bool> IsDefaultProperty =
- AvaloniaProperty.Register<Button, bool>(nameof(IsDefault));
- /// <summary>
- /// Defines the <see cref="IsCancelProperty"/> property.
- /// </summary>
- public static readonly StyledProperty<bool> IsCancelProperty =
- AvaloniaProperty.Register<Button, bool>(nameof(IsCancel));
- /// <summary>
- /// Defines the <see cref="Click"/> event.
- /// </summary>
- public static readonly RoutedEvent<RoutedEventArgs> ClickEvent =
- RoutedEvent.Register<Button, RoutedEventArgs>(nameof(Click), RoutingStrategies.Bubble);
- public static readonly StyledProperty<bool> IsPressedProperty =
- AvaloniaProperty.Register<Button, bool>(nameof(IsPressed));
- private ICommand _command;
- private bool _commandCanExecute = true;
- /// <summary>
- /// Initializes static members of the <see cref="Button"/> class.
- /// </summary>
- static Button()
- {
- FocusableProperty.OverrideDefaultValue(typeof(Button), true);
- CommandProperty.Changed.Subscribe(CommandChanged);
- IsDefaultProperty.Changed.Subscribe(IsDefaultChanged);
- IsCancelProperty.Changed.Subscribe(IsCancelChanged);
- }
- public Button()
- {
- UpdatePseudoClasses(IsPressed);
- }
- /// <summary>
- /// Raised when the user clicks the button.
- /// </summary>
- public event EventHandler<RoutedEventArgs> Click
- {
- add { AddHandler(ClickEvent, value); }
- remove { RemoveHandler(ClickEvent, value); }
- }
- /// <summary>
- /// Gets or sets a value indicating how the <see cref="Button"/> should react to clicks.
- /// </summary>
- public ClickMode ClickMode
- {
- get { return GetValue(ClickModeProperty); }
- set { SetValue(ClickModeProperty, value); }
- }
- /// <summary>
- /// Gets or sets an <see cref="ICommand"/> to be invoked when the button is clicked.
- /// </summary>
- public ICommand Command
- {
- get { return _command; }
- set { SetAndRaise(CommandProperty, ref _command, value); }
- }
- /// <summary>
- /// Gets or sets an <see cref="KeyGesture"/> associated with this control
- /// </summary>
- public KeyGesture HotKey
- {
- get { return GetValue(HotKeyProperty); }
- set { SetValue(HotKeyProperty, value); }
- }
- /// <summary>
- /// Gets or sets a parameter to be passed to the <see cref="Command"/>.
- /// </summary>
- public object CommandParameter
- {
- get { return GetValue(CommandParameterProperty); }
- set { SetValue(CommandParameterProperty, value); }
- }
- /// <summary>
- /// Gets or sets a value indicating whether the button is the default button for the
- /// window.
- /// </summary>
- public bool IsDefault
- {
- get { return GetValue(IsDefaultProperty); }
- set { SetValue(IsDefaultProperty, value); }
- }
- /// <summary>
- /// Gets or sets a value indicating whether the button is the Cancel button for the
- /// window.
- /// </summary>
- public bool IsCancel
- {
- get { return GetValue(IsCancelProperty); }
- set { SetValue(IsCancelProperty, value); }
- }
- public bool IsPressed
- {
- get { return GetValue(IsPressedProperty); }
- private set { SetValue(IsPressedProperty, value); }
- }
- protected override bool IsEnabledCore => base.IsEnabledCore && _commandCanExecute;
- /// <inheritdoc/>
- 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);
- }
- }
- }
- /// <inheritdoc/>
- protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e)
- {
- base.OnDetachedFromVisualTree(e);
- if (IsDefault)
- {
- if (e.Root is IInputElement inputElement)
- {
- StopListeningForDefault(inputElement);
- }
- }
- }
- protected override void OnAttachedToLogicalTree(LogicalTreeAttachmentEventArgs e)
- {
- base.OnAttachedToLogicalTree(e);
- if (Command != null)
- {
- Command.CanExecuteChanged += CanExecuteChanged;
- }
- }
- protected override void OnDetachedFromLogicalTree(LogicalTreeAttachmentEventArgs e)
- {
- base.OnDetachedFromLogicalTree(e);
- if (Command != null)
- {
- Command.CanExecuteChanged -= CanExecuteChanged;
- }
- }
- /// <inheritdoc/>
- protected override void OnKeyDown(KeyEventArgs e)
- {
- if (e.Key == Key.Enter)
- {
- OnClick();
- e.Handled = true;
- }
- else if (e.Key == Key.Space)
- {
- if (ClickMode == ClickMode.Press)
- {
- OnClick();
- }
- IsPressed = true;
- e.Handled = true;
- }
- base.OnKeyDown(e);
- }
- /// <inheritdoc/>
- protected override void OnKeyUp(KeyEventArgs e)
- {
- if (e.Key == Key.Space)
- {
- if (ClickMode == ClickMode.Release)
- {
- OnClick();
- }
- IsPressed = false;
- e.Handled = true;
- }
- }
- /// <summary>
- /// Invokes the <see cref="Click"/> event.
- /// </summary>
- protected virtual void OnClick()
- {
- var e = new RoutedEventArgs(ClickEvent);
- RaiseEvent(e);
- if (!e.Handled && Command?.CanExecute(CommandParameter) == true)
- {
- Command.Execute(CommandParameter);
- e.Handled = true;
- }
- }
- /// <inheritdoc/>
- protected override void OnPointerPressed(PointerPressedEventArgs e)
- {
- base.OnPointerPressed(e);
- if (e.MouseButton == MouseButton.Left)
- {
- IsPressed = true;
- e.Handled = true;
- if (ClickMode == ClickMode.Press)
- {
- OnClick();
- }
- }
- }
- /// <inheritdoc/>
- 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)
- {
- IsPressed = false;
- }
- protected override void OnPropertyChanged<T>(
- AvaloniaProperty<T> property,
- Optional<T> oldValue,
- BindingValue<T> newValue,
- BindingPriority priority)
- {
- base.OnPropertyChanged(property, oldValue, newValue, priority);
- if (property == IsPressedProperty)
- {
- UpdatePseudoClasses(newValue.GetValueOrDefault<bool>());
- }
- }
- protected override void UpdateDataValidation<T>(AvaloniaProperty<T> property, BindingValue<T> value)
- {
- base.UpdateDataValidation(property, value);
- if (property == CommandProperty)
- {
- if (value.Type == BindingValueType.BindingError)
- {
- if (_commandCanExecute)
- {
- _commandCanExecute = false;
- UpdateIsEffectivelyEnabled();
- }
- }
- }
- }
- /// <summary>
- /// Called when the <see cref="Command"/> property changes.
- /// </summary>
- /// <param name="e">The event args.</param>
- private static void CommandChanged(AvaloniaPropertyChangedEventArgs e)
- {
- if (e.Sender is Button button)
- {
- if (((ILogical)button).IsAttachedToLogicalTree)
- {
- if (e.OldValue is ICommand oldCommand)
- {
- oldCommand.CanExecuteChanged -= button.CanExecuteChanged;
- }
- if (e.NewValue is ICommand newCommand)
- {
- newCommand.CanExecuteChanged += button.CanExecuteChanged;
- }
- }
- button.CanExecuteChanged(button, EventArgs.Empty);
- }
- }
- /// <summary>
- /// Called when the <see cref="IsDefault"/> property changes.
- /// </summary>
- /// <param name="e">The event args.</param>
- private static void IsDefaultChanged(AvaloniaPropertyChangedEventArgs e)
- {
- var button = e.Sender as Button;
- var isDefault = (bool)e.NewValue;
- if (button?.VisualRoot is IInputElement inputRoot)
- {
- if (isDefault)
- {
- button.ListenForDefault(inputRoot);
- }
- else
- {
- button.StopListeningForDefault(inputRoot);
- }
- }
- }
- /// <summary>
- /// Called when the <see cref="IsCancel"/> property changes.
- /// </summary>
- /// <param name="e">The event args.</param>
- private static void IsCancelChanged(AvaloniaPropertyChangedEventArgs e)
- {
- var button = e.Sender as Button;
- var isCancel = (bool)e.NewValue;
- if (button?.VisualRoot is IInputElement inputRoot)
- {
- if (isCancel)
- {
- button.ListenForCancel(inputRoot);
- }
- else
- {
- button.StopListeningForCancel(inputRoot);
- }
- }
- }
- /// <summary>
- /// Called when the <see cref="ICommand.CanExecuteChanged"/> event fires.
- /// </summary>
- /// <param name="sender">The event sender.</param>
- /// <param name="e">The event args.</param>
- private void CanExecuteChanged(object sender, EventArgs e)
- {
- var canExecute = Command == null || Command.CanExecute(CommandParameter);
- if (canExecute != _commandCanExecute)
- {
- _commandCanExecute = canExecute;
- UpdateIsEffectivelyEnabled();
- }
- }
- /// <summary>
- /// Starts listening for the Enter key when the button <see cref="IsDefault"/>.
- /// </summary>
- /// <param name="root">The input root.</param>
- private void ListenForDefault(IInputElement root)
- {
- root.AddHandler(KeyDownEvent, RootDefaultKeyDown);
- }
- /// <summary>
- /// Starts listening for the Escape key when the button <see cref="IsCancel"/>.
- /// </summary>
- /// <param name="root">The input root.</param>
- private void ListenForCancel(IInputElement root)
- {
- root.AddHandler(KeyDownEvent, RootCancelKeyDown);
- }
- /// <summary>
- /// Stops listening for the Enter key when the button is no longer <see cref="IsDefault"/>.
- /// </summary>
- /// <param name="root">The input root.</param>
- private void StopListeningForDefault(IInputElement root)
- {
- root.RemoveHandler(KeyDownEvent, RootDefaultKeyDown);
- }
- /// <summary>
- /// Stops listening for the Escape key when the button is no longer <see cref="IsCancel"/>.
- /// </summary>
- /// <param name="root">The input root.</param>
- private void StopListeningForCancel(IInputElement root)
- {
- root.RemoveHandler(KeyDownEvent, RootCancelKeyDown);
- }
- /// <summary>
- /// Called when a key is pressed on the input root and the button <see cref="IsDefault"/>.
- /// </summary>
- /// <param name="sender">The event sender.</param>
- /// <param name="e">The event args.</param>
- private void RootDefaultKeyDown(object sender, KeyEventArgs e)
- {
- if (e.Key == Key.Enter && IsVisible && IsEnabled)
- {
- OnClick();
- }
- }
- /// <summary>
- /// Called when a key is pressed on the input root and the button <see cref="IsCancel"/>.
- /// </summary>
- /// <param name="sender">The event sender.</param>
- /// <param name="e">The event args.</param>
- private void RootCancelKeyDown(object sender, KeyEventArgs e)
- {
- if (e.Key == Key.Escape && IsVisible && IsEnabled)
- {
- OnClick();
- }
- }
- private void UpdatePseudoClasses(bool isPressed)
- {
- PseudoClasses.Set(":pressed", isPressed);
- }
- }
- }
|