From 3c10542c4cde49baf9013f1de0b9fc53caf0345e Mon Sep 17 00:00:00 2001 From: Fredrik Salomonsson <98377875+fredso90@users.noreply.github.com> Date: Thu, 19 Oct 2023 16:01:58 +0200 Subject: [PATCH] [ColorPicker]End picker session on right click (#29075) * ColorPicker will now end user session on right click. * Update IMouseInfoProvider.cs changed RMouse to SecondaryMouse * Update IMouseInfoProvider.cs * Update MouseHook.cs Changed names from RMouse to SecondaryMouse * Update MouseInfoProvider.cs changed names from RMouse to SecondaryMouse * Update MouseInfoProvider.cs * Update MainViewModel.cs changed names from RMouse to SecondaryMouse * Added handler for SecondaryMouseDown and made it start user session to avoid issues with right click to terminate ColorPicker * Secondary Mouse Up will now end user session without opening context menus as a result of right clicking. * Add comment about consuming the right mouse event --- .../ColorPickerUI/Mouse/IMouseInfoProvider.cs | 2 + .../ColorPickerUI/Mouse/MouseHook.cs | 39 +++++++++++++++++++ .../ColorPickerUI/Mouse/MouseInfoProvider.cs | 11 ++++++ .../ColorPickerUI/ViewModels/MainViewModel.cs | 6 +++ 4 files changed, 58 insertions(+) diff --git a/src/modules/colorPicker/ColorPickerUI/Mouse/IMouseInfoProvider.cs b/src/modules/colorPicker/ColorPickerUI/Mouse/IMouseInfoProvider.cs index f2766f5af5..3361b95cb4 100644 --- a/src/modules/colorPicker/ColorPickerUI/Mouse/IMouseInfoProvider.cs +++ b/src/modules/colorPicker/ColorPickerUI/Mouse/IMouseInfoProvider.cs @@ -18,6 +18,8 @@ namespace ColorPicker.Mouse event MouseUpEventHandler OnMouseDown; + event SecondaryMouseUpEventHandler OnSecondaryMouseUp; + System.Windows.Point CurrentPosition { get; } Color CurrentColor { get; } diff --git a/src/modules/colorPicker/ColorPickerUI/Mouse/MouseHook.cs b/src/modules/colorPicker/ColorPickerUI/Mouse/MouseHook.cs index 080ec9d35e..a686810de5 100644 --- a/src/modules/colorPicker/ColorPickerUI/Mouse/MouseHook.cs +++ b/src/modules/colorPicker/ColorPickerUI/Mouse/MouseHook.cs @@ -14,6 +14,8 @@ namespace ColorPicker.Mouse { public delegate void MouseUpEventHandler(object sender, System.Drawing.Point p); + public delegate void SecondaryMouseUpEventHandler(object sender, IntPtr wParam); + internal class MouseHook { [System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.NamingRules", "SA1310:Field names should not contain underscore", Justification = "Interop object")] @@ -22,6 +24,10 @@ namespace ColorPicker.Mouse private const int WM_LBUTTONDOWN = 0x0201; [System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.NamingRules", "SA1310:Field names should not contain underscore", Justification = "Interop object")] private const int WM_MOUSEWHEEL = 0x020A; + [System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.NamingRules", "SA1310:Field names should not contain underscore", Justification = "Interop object")] + private const int WM_RBUTTONUP = 0x0205; + [System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.NamingRules", "SA1310:Field names should not contain underscore", Justification = "Interop object")] + private const int WM_RBUTTONDOWN = 0x0204; private IntPtr _mouseHookHandle; private HookProc _mouseDelegate; @@ -43,6 +49,23 @@ namespace ColorPicker.Mouse } } + private event SecondaryMouseUpEventHandler SecondaryMouseUp; + + public event SecondaryMouseUpEventHandler OnSecondaryMouseUp + { + add + { + Subscribe(); + SecondaryMouseUp += value; + } + + remove + { + SecondaryMouseUp -= value; + Unsubscribe(); + } + } + private event MouseWheelEventHandler MouseWheel; public event MouseWheelEventHandler OnMouseWheel @@ -109,6 +132,22 @@ namespace ColorPicker.Mouse return new IntPtr(-1); } + if (wParam.ToInt32() == WM_RBUTTONUP) + { + if (SecondaryMouseUp != null) + { + SecondaryMouseUp.Invoke(null, wParam); + } + + return new IntPtr(-1); + } + + if (wParam.ToInt32() == WM_RBUTTONDOWN) + { + // Consume the event to avoid triggering context menus while in a Color Picker session. + return new IntPtr(-1); + } + if (wParam.ToInt32() == WM_MOUSEWHEEL) { if (MouseWheel != null) diff --git a/src/modules/colorPicker/ColorPickerUI/Mouse/MouseInfoProvider.cs b/src/modules/colorPicker/ColorPickerUI/Mouse/MouseInfoProvider.cs index 526466dc4a..3c7006ba46 100644 --- a/src/modules/colorPicker/ColorPickerUI/Mouse/MouseInfoProvider.cs +++ b/src/modules/colorPicker/ColorPickerUI/Mouse/MouseInfoProvider.cs @@ -4,6 +4,7 @@ using System; using System.ComponentModel.Composition; +using System.Configuration; using System.Drawing; using System.Drawing.Imaging; using System.Windows.Input; @@ -55,6 +56,8 @@ namespace ColorPicker.Mouse public event MouseUpEventHandler OnMouseDown; + public event SecondaryMouseUpEventHandler OnSecondaryMouseUp; + public System.Windows.Point CurrentPosition { get @@ -143,6 +146,7 @@ namespace ColorPicker.Mouse _mouseHook.OnMouseDown += MouseHook_OnMouseDown; _mouseHook.OnMouseWheel += MouseHook_OnMouseWheel; + _mouseHook.OnSecondaryMouseUp += MouseHook_OnSecondaryMouseUp; if (_userSettings.ChangeCursor.Value) { @@ -167,6 +171,12 @@ namespace ColorPicker.Mouse OnMouseDown?.Invoke(this, p); } + private void MouseHook_OnSecondaryMouseUp(object sender, IntPtr wParam) + { + DisposeHook(); + OnSecondaryMouseUp?.Invoke(this, wParam); + } + private void CopiedColorRepresentation_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e) { _colorFormatChanged = true; @@ -182,6 +192,7 @@ namespace ColorPicker.Mouse _previousMousePosition = new System.Windows.Point(-1, 1); _mouseHook.OnMouseDown -= MouseHook_OnMouseDown; _mouseHook.OnMouseWheel -= MouseHook_OnMouseWheel; + _mouseHook.OnSecondaryMouseUp -= MouseHook_OnSecondaryMouseUp; if (_userSettings.ChangeCursor.Value) { diff --git a/src/modules/colorPicker/ColorPickerUI/ViewModels/MainViewModel.cs b/src/modules/colorPicker/ColorPickerUI/ViewModels/MainViewModel.cs index d5fc11900f..87d69741ce 100644 --- a/src/modules/colorPicker/ColorPickerUI/ViewModels/MainViewModel.cs +++ b/src/modules/colorPicker/ColorPickerUI/ViewModels/MainViewModel.cs @@ -72,6 +72,7 @@ namespace ColorPicker.ViewModels mouseInfoProvider.MouseColorChanged += Mouse_ColorChanged; mouseInfoProvider.OnMouseDown += MouseInfoProvider_OnMouseDown; mouseInfoProvider.OnMouseWheel += MouseInfoProvider_OnMouseWheel; + mouseInfoProvider.OnSecondaryMouseUp += MouseInfoProvider_OnSecondaryMouseUp; } _userSettings.ShowColorName.PropertyChanged += (s, e) => { OnPropertyChanged(nameof(ShowColorName)); }; @@ -166,6 +167,11 @@ namespace ColorPicker.ViewModels _appStateHandler.OnColorPickerMouseDown(); } + private void MouseInfoProvider_OnSecondaryMouseUp(object sender, IntPtr wParam) + { + _appStateHandler.EndUserSession(); + } + private string GetColorString() { var color = ((SolidColorBrush)ColorBrush).Color;