瀏覽代碼

Added InitialPressMouseButton to PointerReleasedEventArgs that returns the button that caused the PointerPressed event

MouseButton is now deprecated with error, since people need to decide which behavior they want
Nikita Tsukanov 6 年之前
父節點
當前提交
a16e3bff7f

+ 1 - 1
src/Avalonia.Controls.DataGrid/DataGridColumnHeader.cs

@@ -468,7 +468,7 @@ namespace Avalonia.Controls
 
 
         private void DataGridColumnHeader_PointerReleased(object sender, PointerReleasedEventArgs e)
         private void DataGridColumnHeader_PointerReleased(object sender, PointerReleasedEventArgs e)
         {
         {
-            if (OwningColumn == null || e.Handled || !IsEnabled || e.MouseButton != MouseButton.Left)
+            if (OwningColumn == null || e.Handled || !IsEnabled || e.InitialPressMouseButton != MouseButton.Left)
             {
             {
                 return;
                 return;
             }
             }

+ 1 - 1
src/Avalonia.Controls/Button.cs

@@ -294,7 +294,7 @@ namespace Avalonia.Controls
         {
         {
             base.OnPointerReleased(e);
             base.OnPointerReleased(e);
 
 
-            if (IsPressed && e.MouseButton == MouseButton.Left)
+            if (IsPressed && e.InitialPressMouseButton == MouseButton.Left)
             {
             {
                 IsPressed = false;
                 IsPressed = false;
                 e.Handled = true;
                 e.Handled = true;

+ 1 - 1
src/Avalonia.Controls/Calendar/Calendar.cs

@@ -1565,7 +1565,7 @@ namespace Avalonia.Controls
         protected override void OnPointerReleased(PointerReleasedEventArgs e)
         protected override void OnPointerReleased(PointerReleasedEventArgs e)
         {
         {
             base.OnPointerReleased(e);
             base.OnPointerReleased(e);
-            if (!HasFocusInternal && e.MouseButton == MouseButton.Left)
+            if (!HasFocusInternal && e.InitialPressMouseButton == MouseButton.Left)
             {
             {
                 FocusManager.Instance.Focus(this);
                 FocusManager.Instance.Focus(this);
             }
             }

+ 1 - 1
src/Avalonia.Controls/Calendar/CalendarButton.cs

@@ -173,7 +173,7 @@ namespace Avalonia.Controls.Primitives
         protected override void OnPointerReleased(PointerReleasedEventArgs e)
         protected override void OnPointerReleased(PointerReleasedEventArgs e)
         {
         {
             base.OnPointerReleased(e);
             base.OnPointerReleased(e);
-            if (e.MouseButton == MouseButton.Left)
+            if (e.InitialPressMouseButton == MouseButton.Left)
                 CalendarLeftMouseButtonUp?.Invoke(this, e);
                 CalendarLeftMouseButtonUp?.Invoke(this, e);
         }
         }
     }
     }

+ 1 - 1
src/Avalonia.Controls/Calendar/CalendarDayButton.cs

@@ -231,7 +231,7 @@ namespace Avalonia.Controls.Primitives
         {
         {
             base.OnPointerReleased(e);
             base.OnPointerReleased(e);
 
 
-            if (e.MouseButton == MouseButton.Left)
+            if (e.InitialPressMouseButton == MouseButton.Left)
                 CalendarDayButtonMouseUp?.Invoke(this, e);
                 CalendarDayButtonMouseUp?.Invoke(this, e);
         }
         }
     }
     }

+ 1 - 1
src/Avalonia.Controls/ContextMenu.cs

@@ -192,7 +192,7 @@ namespace Avalonia.Controls
                 e.Handled = true;
                 e.Handled = true;
             }
             }
 
 
-            if (e.MouseButton == MouseButton.Right)
+            if (e.InitialPressMouseButton == MouseButton.Right)
             {
             {
                 if (contextMenu.CancelOpening())
                 if (contextMenu.CancelOpening())
                     return;
                     return;

+ 1 - 1
src/Avalonia.Controls/Platform/DefaultMenuInteractionHandler.cs

@@ -356,7 +356,7 @@ namespace Avalonia.Controls.Platform
         {
         {
             var item = GetMenuItem(e.Source as IControl);
             var item = GetMenuItem(e.Source as IControl);
 
 
-            if (e.MouseButton == MouseButton.Left && item?.HasSubMenu == false)
+            if (e.InitialPressMouseButton == MouseButton.Left && item?.HasSubMenu == false)
             {
             {
                 Click(item);
                 Click(item);
                 e.Handled = true;
                 e.Handled = true;

+ 2 - 2
src/Avalonia.Controls/RepeatButton.cs

@@ -98,10 +98,10 @@ namespace Avalonia.Controls
         {
         {
             base.OnPointerReleased(e);
             base.OnPointerReleased(e);
 
 
-            if (e.MouseButton == MouseButton.Left)
+            if (e.InitialPressMouseButton == MouseButton.Left)
             {
             {
                 StopTimer();
                 StopTimer();
             }
             }
         }
         }
     }
     }
-}
+}

+ 1 - 1
src/Avalonia.Controls/TabControl.cs

@@ -196,7 +196,7 @@ namespace Avalonia.Controls
 
 
         protected override void OnPointerReleased(PointerReleasedEventArgs e)
         protected override void OnPointerReleased(PointerReleasedEventArgs e)
         {
         {
-            if (e.MouseButton == MouseButton.Left && e.Pointer.Type != PointerType.Mouse)
+            if (e.InitialPressMouseButton == MouseButton.Left && e.Pointer.Type != PointerType.Mouse)
             {
             {
                 var container = GetContainerFromEventSource(e.Source);
                 var container = GetContainerFromEventSource(e.Source);
                 if (container != null
                 if (container != null

+ 1 - 1
src/Avalonia.Controls/Utils/SelectingItemsControlSelectionAdapter.cs

@@ -178,7 +178,7 @@ namespace Avalonia.Controls.Utils
         /// <param name="e">The event data.</param>
         /// <param name="e">The event data.</param>
         private void OnSelectorPointerReleased(object sender, PointerReleasedEventArgs e)
         private void OnSelectorPointerReleased(object sender, PointerReleasedEventArgs e)
         {
         {
-            if (e.MouseButton == MouseButton.Left)
+            if (e.InitialPressMouseButton == MouseButton.Left)
             {
             {
                 OnCommit();
                 OnCommit();
             }
             }

+ 1 - 1
src/Avalonia.Input/Gestures.cs

@@ -97,7 +97,7 @@ namespace Avalonia.Input
 
 
                 if (s_lastPress.TryGetTarget(out var target) && target == e.Source)
                 if (s_lastPress.TryGetTarget(out var target) && target == e.Source)
                 {
                 {
-                    var et = e.MouseButton != MouseButton.Right ? TappedEvent : RightTappedEvent;
+                    var et = e.InitialPressMouseButton != MouseButton.Right ? TappedEvent : RightTappedEvent;
                     e.Source.RaiseEvent(new RoutedEventArgs(et));
                     e.Source.RaiseEvent(new RoutedEventArgs(et));
                 }
                 }
             }
             }

+ 3 - 2
src/Avalonia.Input/MouseDevice.cs

@@ -221,7 +221,7 @@ namespace Avalonia.Input
                     _lastClickTime = timestamp;
                     _lastClickTime = timestamp;
                     _lastClickRect = new Rect(p, new Size())
                     _lastClickRect = new Rect(p, new Size())
                         .Inflate(new Thickness(settings.DoubleClickSize.Width / 2, settings.DoubleClickSize.Height / 2));
                         .Inflate(new Thickness(settings.DoubleClickSize.Width / 2, settings.DoubleClickSize.Height / 2));
-                    _lastMouseDownButton = properties.GetObsoleteMouseButton();
+                    _lastMouseDownButton = properties.PointerUpdateKind.GetMouseButton();
                     var e = new PointerPressedEventArgs(source, _pointer, root, p, timestamp, properties, inputModifiers, _clickCount);
                     var e = new PointerPressedEventArgs(source, _pointer, root, p, timestamp, properties, inputModifiers, _clickCount);
                     source.RaiseEvent(e);
                     source.RaiseEvent(e);
                     return e.Handled;
                     return e.Handled;
@@ -267,7 +267,8 @@ namespace Avalonia.Input
             if (hit != null)
             if (hit != null)
             {
             {
                 var source = GetSource(hit);
                 var source = GetSource(hit);
-                var e = new PointerReleasedEventArgs(source, _pointer, root, p, timestamp, props, inputModifiers);
+                var e = new PointerReleasedEventArgs(source, _pointer, root, p, timestamp, props, inputModifiers,
+                    _lastMouseDownButton);
 
 
                 source?.RaiseEvent(e);
                 source?.RaiseEvent(e);
                 _pointer.Capture(null);
                 _pointer.Capture(null);

+ 11 - 5
src/Avalonia.Input/PointerEventArgs.cs

@@ -124,7 +124,7 @@ namespace Avalonia.Input
         public int ClickCount => _obsoleteClickCount;
         public int ClickCount => _obsoleteClickCount;
 
 
         [Obsolete("Use PointerUpdateKind")]
         [Obsolete("Use PointerUpdateKind")]
-        public MouseButton MouseButton => Properties.GetObsoleteMouseButton();
+        public MouseButton MouseButton => Properties.PointerUpdateKind.GetMouseButton();
     }
     }
 
 
     public class PointerReleasedEventArgs : PointerEventArgs
     public class PointerReleasedEventArgs : PointerEventArgs
@@ -132,15 +132,21 @@ namespace Avalonia.Input
         public PointerReleasedEventArgs(
         public PointerReleasedEventArgs(
             IInteractive source, IPointer pointer,
             IInteractive source, IPointer pointer,
             IVisual rootVisual, Point rootVisualPosition, ulong timestamp,
             IVisual rootVisual, Point rootVisualPosition, ulong timestamp,
-            PointerPointProperties properties, KeyModifiers modifiers)
+            PointerPointProperties properties, KeyModifiers modifiers,
+            MouseButton initialPressMouseButton)
             : base(InputElement.PointerReleasedEvent, source, pointer, rootVisual, rootVisualPosition,
             : base(InputElement.PointerReleasedEvent, source, pointer, rootVisual, rootVisualPosition,
                 timestamp, properties, modifiers)
                 timestamp, properties, modifiers)
         {
         {
-            
+            InitialPressMouseButton = initialPressMouseButton;
         }
         }
 
 
-        [Obsolete("Use PointerUpdateKind")] 
-        public MouseButton MouseButton => Properties.GetObsoleteMouseButton();
+        /// <summary>
+        /// Gets the mouse button that triggered the corresponding PointerPressed event
+        /// </summary>
+        public MouseButton InitialPressMouseButton { get; }
+
+        [Obsolete("Either use GetCurrentPoint(this).Properties.PointerUpdateKind or InitialPressMouseButton, see ", true)]
+        public MouseButton MouseButton => InitialPressMouseButton;
     }
     }
 
 
     public class PointerCaptureLostEventArgs : RoutedEventArgs
     public class PointerCaptureLostEventArgs : RoutedEventArgs

+ 14 - 11
src/Avalonia.Input/PointerPoint.cs

@@ -49,17 +49,6 @@ namespace Avalonia.Input
         }
         }
 
 
         public static PointerPointProperties None { get; } = new PointerPointProperties();
         public static PointerPointProperties None { get; } = new PointerPointProperties();
-        
-        public MouseButton GetObsoleteMouseButton()
-        {
-            if (PointerUpdateKind == PointerUpdateKind.LeftButtonPressed || PointerUpdateKind == PointerUpdateKind.LeftButtonReleased)
-                return MouseButton.Left;
-            if (PointerUpdateKind == PointerUpdateKind.MiddleButtonPressed || PointerUpdateKind == PointerUpdateKind.MiddleButtonReleased)
-                return MouseButton.Middle;
-            if (PointerUpdateKind == PointerUpdateKind.RightButtonPressed || PointerUpdateKind == PointerUpdateKind.RightButtonReleased)
-                return MouseButton.Right;
-            return MouseButton.None;
-        }
     }
     }
 
 
     public enum PointerUpdateKind
     public enum PointerUpdateKind
@@ -72,4 +61,18 @@ namespace Avalonia.Input
         RightButtonReleased,
         RightButtonReleased,
         Other
         Other
     }
     }
+
+    public static class PointerUpdateKindExtensions
+    {
+        public static MouseButton GetMouseButton(this PointerUpdateKind kind)
+        {
+            if (kind == PointerUpdateKind.LeftButtonPressed || kind == PointerUpdateKind.LeftButtonReleased)
+                return MouseButton.Left;
+            if (kind == PointerUpdateKind.MiddleButtonPressed || kind == PointerUpdateKind.MiddleButtonReleased)
+                return MouseButton.Middle;
+            if (kind == PointerUpdateKind.RightButtonPressed || kind == PointerUpdateKind.RightButtonReleased)
+                return MouseButton.Right;
+            return MouseButton.None;
+        }
+    }
 }
 }

+ 1 - 1
src/Avalonia.Input/TouchDevice.cs

@@ -60,7 +60,7 @@ namespace Avalonia.Input
                         args.Root, args.Position, ev.Timestamp,
                         args.Root, args.Position, ev.Timestamp,
                         new PointerPointProperties(GetModifiers(args.InputModifiers, false),
                         new PointerPointProperties(GetModifiers(args.InputModifiers, false),
                             PointerUpdateKind.LeftButtonReleased),
                             PointerUpdateKind.LeftButtonReleased),
-                        GetKeyModifiers(args.InputModifiers)));
+                        GetKeyModifiers(args.InputModifiers), MouseButton.Left));
                 }
                 }
             }
             }
 
 

+ 3 - 1
tests/Avalonia.Controls.UnitTests/Platform/DefaultMenuInteractionHandlerTests.cs

@@ -18,7 +18,9 @@ namespace Avalonia.Controls.UnitTests.Platform
             default);
             default);
         
         
         static PointerReleasedEventArgs CreateReleased(IInteractive source) => new PointerReleasedEventArgs(source,
         static PointerReleasedEventArgs CreateReleased(IInteractive source) => new PointerReleasedEventArgs(source,
-            new FakePointer(), (IVisual)source, default,0, new PointerPointProperties(RawInputModifiers.None, PointerUpdateKind.LeftButtonReleased), default);
+            new FakePointer(), (IVisual)source, default,0,
+            new PointerPointProperties(RawInputModifiers.None, PointerUpdateKind.LeftButtonReleased),
+            default, MouseButton.Left);
         
         
         public class TopLevel
         public class TopLevel
         {
         {

+ 1 - 2
tests/Avalonia.UnitTests/MouseTestHelper.cs

@@ -86,8 +86,7 @@ namespace Avalonia.UnitTests
             {
             {
                 _pointer.Capture(null);
                 _pointer.Capture(null);
                 target.RaiseEvent(new PointerReleasedEventArgs(source, _pointer, (IVisual)target, position,
                 target.RaiseEvent(new PointerReleasedEventArgs(source, _pointer, (IVisual)target, position,
-                    Timestamp(), props,
-                    GetModifiers(modifiers)));
+                    Timestamp(), props, GetModifiers(modifiers), _pressedButton));
             }
             }
             else
             else
                 Move(target, source, position);
                 Move(target, source, position);