[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
This commit is contained in:
Fredrik Salomonsson
2023-10-19 16:01:58 +02:00
committed by GitHub
parent e63dbe00b6
commit 3c10542c4c
4 changed files with 58 additions and 0 deletions

View File

@@ -18,6 +18,8 @@ namespace ColorPicker.Mouse
event MouseUpEventHandler OnMouseDown; event MouseUpEventHandler OnMouseDown;
event SecondaryMouseUpEventHandler OnSecondaryMouseUp;
System.Windows.Point CurrentPosition { get; } System.Windows.Point CurrentPosition { get; }
Color CurrentColor { get; } Color CurrentColor { get; }

View File

@@ -14,6 +14,8 @@ namespace ColorPicker.Mouse
{ {
public delegate void MouseUpEventHandler(object sender, System.Drawing.Point p); public delegate void MouseUpEventHandler(object sender, System.Drawing.Point p);
public delegate void SecondaryMouseUpEventHandler(object sender, IntPtr wParam);
internal class MouseHook internal class MouseHook
{ {
[System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.NamingRules", "SA1310:Field names should not contain underscore", Justification = "Interop object")] [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; private const int WM_LBUTTONDOWN = 0x0201;
[System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.NamingRules", "SA1310:Field names should not contain underscore", Justification = "Interop object")] [System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.NamingRules", "SA1310:Field names should not contain underscore", Justification = "Interop object")]
private const int WM_MOUSEWHEEL = 0x020A; 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 IntPtr _mouseHookHandle;
private HookProc _mouseDelegate; 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; private event MouseWheelEventHandler MouseWheel;
public event MouseWheelEventHandler OnMouseWheel public event MouseWheelEventHandler OnMouseWheel
@@ -109,6 +132,22 @@ namespace ColorPicker.Mouse
return new IntPtr(-1); 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 (wParam.ToInt32() == WM_MOUSEWHEEL)
{ {
if (MouseWheel != null) if (MouseWheel != null)

View File

@@ -4,6 +4,7 @@
using System; using System;
using System.ComponentModel.Composition; using System.ComponentModel.Composition;
using System.Configuration;
using System.Drawing; using System.Drawing;
using System.Drawing.Imaging; using System.Drawing.Imaging;
using System.Windows.Input; using System.Windows.Input;
@@ -55,6 +56,8 @@ namespace ColorPicker.Mouse
public event MouseUpEventHandler OnMouseDown; public event MouseUpEventHandler OnMouseDown;
public event SecondaryMouseUpEventHandler OnSecondaryMouseUp;
public System.Windows.Point CurrentPosition public System.Windows.Point CurrentPosition
{ {
get get
@@ -143,6 +146,7 @@ namespace ColorPicker.Mouse
_mouseHook.OnMouseDown += MouseHook_OnMouseDown; _mouseHook.OnMouseDown += MouseHook_OnMouseDown;
_mouseHook.OnMouseWheel += MouseHook_OnMouseWheel; _mouseHook.OnMouseWheel += MouseHook_OnMouseWheel;
_mouseHook.OnSecondaryMouseUp += MouseHook_OnSecondaryMouseUp;
if (_userSettings.ChangeCursor.Value) if (_userSettings.ChangeCursor.Value)
{ {
@@ -167,6 +171,12 @@ namespace ColorPicker.Mouse
OnMouseDown?.Invoke(this, p); 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) private void CopiedColorRepresentation_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
{ {
_colorFormatChanged = true; _colorFormatChanged = true;
@@ -182,6 +192,7 @@ namespace ColorPicker.Mouse
_previousMousePosition = new System.Windows.Point(-1, 1); _previousMousePosition = new System.Windows.Point(-1, 1);
_mouseHook.OnMouseDown -= MouseHook_OnMouseDown; _mouseHook.OnMouseDown -= MouseHook_OnMouseDown;
_mouseHook.OnMouseWheel -= MouseHook_OnMouseWheel; _mouseHook.OnMouseWheel -= MouseHook_OnMouseWheel;
_mouseHook.OnSecondaryMouseUp -= MouseHook_OnSecondaryMouseUp;
if (_userSettings.ChangeCursor.Value) if (_userSettings.ChangeCursor.Value)
{ {

View File

@@ -72,6 +72,7 @@ namespace ColorPicker.ViewModels
mouseInfoProvider.MouseColorChanged += Mouse_ColorChanged; mouseInfoProvider.MouseColorChanged += Mouse_ColorChanged;
mouseInfoProvider.OnMouseDown += MouseInfoProvider_OnMouseDown; mouseInfoProvider.OnMouseDown += MouseInfoProvider_OnMouseDown;
mouseInfoProvider.OnMouseWheel += MouseInfoProvider_OnMouseWheel; mouseInfoProvider.OnMouseWheel += MouseInfoProvider_OnMouseWheel;
mouseInfoProvider.OnSecondaryMouseUp += MouseInfoProvider_OnSecondaryMouseUp;
} }
_userSettings.ShowColorName.PropertyChanged += (s, e) => { OnPropertyChanged(nameof(ShowColorName)); }; _userSettings.ShowColorName.PropertyChanged += (s, e) => { OnPropertyChanged(nameof(ShowColorName)); };
@@ -166,6 +167,11 @@ namespace ColorPicker.ViewModels
_appStateHandler.OnColorPickerMouseDown(); _appStateHandler.OnColorPickerMouseDown();
} }
private void MouseInfoProvider_OnSecondaryMouseUp(object sender, IntPtr wParam)
{
_appStateHandler.EndUserSession();
}
private string GetColorString() private string GetColorString()
{ {
var color = ((SolidColorBrush)ColorBrush).Color; var color = ((SolidColorBrush)ColorBrush).Color;