| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375 | 
							- // 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.Rendering;
 
- 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="Click"/> event.
 
-         /// </summary>
 
-         public static readonly RoutedEvent<RoutedEventArgs> ClickEvent =
 
-             RoutedEvent.Register<Button, RoutedEventArgs>("Click", RoutingStrategies.Bubble);
 
-         private ICommand _command;
 
-         /// <summary>
 
-         /// Initializes static members of the <see cref="Button"/> class.
 
-         /// </summary>
 
-         static Button()
 
-         {
 
-             FocusableProperty.OverrideDefaultValue(typeof(Button), true);
 
-             ClickEvent.AddClassHandler<Button>(x => x.OnClick);
 
-             CommandProperty.Changed.Subscribe(CommandChanged);
 
-             IsDefaultProperty.Changed.Subscribe(IsDefaultChanged);
 
-         }
 
-         /// <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); }
 
-         }
 
-         /// <inheritdoc/>
 
-         protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
 
-         {
 
-             base.OnAttachedToVisualTree(e);
 
-             if (IsDefault)
 
-             {
 
-                 var inputElement = e.Root as IInputElement;
 
-                 if (inputElement != null)
 
-                 {
 
-                     ListenForDefault(inputElement);
 
-                 }
 
-             }
 
-         }
 
-         /// <inheritdoc/>
 
-         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);
 
-         }
 
-         /// <inheritdoc/>
 
-         protected override void OnKeyUp(KeyEventArgs e)
 
-         {
 
-             if (e.Key == Key.Space)
 
-             {
 
-                 if (ClickMode == ClickMode.Release)
 
-                 {
 
-                     RaiseClickEvent();
 
-                 }
 
-                 e.Handled = true;
 
-             }
 
-         }
 
-         /// <inheritdoc/>
 
-         protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e)
 
-         {
 
-             base.OnDetachedFromVisualTree(e);
 
-             if (IsDefault)
 
-             {
 
-                 var inputElement = e.Root as IInputElement;
 
-                 if (inputElement != null)
 
-                 {
 
-                     StopListeningForDefault(inputElement);
 
-                 }
 
-             }
 
-         }
 
-         /// <summary>
 
-         /// Invokes the <see cref="Click"/> event.
 
-         /// </summary>
 
-         /// <param name="e">The event args.</param>
 
-         protected virtual void OnClick(RoutedEventArgs e)
 
-         {
 
-             if (Command != null)
 
-             {
 
-                 Command.Execute(CommandParameter);
 
-                 e.Handled = true;
 
-             }
 
-         }
 
-         /// <inheritdoc/>
 
-         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();
 
-                 }
 
-             }
 
-         }
 
-         /// <inheritdoc/>
 
-         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();
 
-                 }
 
-             }
 
-         }
 
-         protected override void UpdateDataValidation(AvaloniaProperty property, BindingNotification status)
 
-         {
 
-             base.UpdateDataValidation(property, status);
 
-             if(property == CommandProperty)
 
-             {
 
-                 if(status?.ErrorType == BindingErrorType.Error)
 
-                 {
 
-                     IsEnabled = false;
 
-                 }
 
-             }
 
-         }
 
-         /// <summary>
 
-         /// Called when the <see cref="Command"/> property changes.
 
-         /// </summary>
 
-         /// <param name="e">The event args.</param>
 
-         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);
 
-             }
 
-         }
 
-         /// <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;
 
-             var inputRoot = button?.VisualRoot as IInputElement;
 
-             if (inputRoot != null)
 
-             {
 
-                 if (isDefault)
 
-                 {
 
-                     button.ListenForDefault(inputRoot);
 
-                 }
 
-                 else
 
-                 {
 
-                     button.StopListeningForDefault(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)
 
-         {
 
-             // 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);
 
-         }
 
-         /// <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, RootKeyDown);
 
-         }
 
-         /// <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, RootKeyDown);
 
-         }
 
-         /// <summary>
 
-         /// Raises the <see cref="Click"/> event.
 
-         /// </summary>
 
-         private void RaiseClickEvent()
 
-         {
 
-             RoutedEventArgs click = new RoutedEventArgs
 
-             {
 
-                 RoutedEvent = ClickEvent,
 
-             };
 
-             RaiseEvent(click);
 
-         }
 
-         /// <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 RootKeyDown(object sender, KeyEventArgs e)
 
-         {
 
-             if (e.Key == Key.Enter && IsVisible && IsEnabled)
 
-             {
 
-                 RaiseClickEvent();
 
-             }
 
-         }
 
-     }
 
- }
 
 
  |