enabling stylecop on ColorPicker (#5336)

* Getting all warnings fixed

* fixed feedback, missed resize for some reason as well.

Co-authored-by: Clint Rutkas <crutkas@microsoft.com>
This commit is contained in:
Clint Rutkas
2020-07-31 09:13:05 -07:00
committed by GitHub
parent 8fb095e2f4
commit ab86fc9a5e
36 changed files with 309 additions and 124 deletions

View File

@@ -1,4 +1,8 @@
using System;
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Threading;
using System.Windows;
using ColorPicker.Helpers;
@@ -62,7 +66,9 @@ namespace ColorPickerUI
protected override void OnExit(ExitEventArgs e)
{
if (_instanceMutex != null)
{
_instanceMutex.ReleaseMutex();
}
CursorManager.RestoreOriginalCursors();
base.OnExit(e);

View File

@@ -1,4 +1,8 @@
using System;
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Windows;
using System.Windows.Interactivity;
using System.Windows.Media.Animation;

View File

@@ -1,4 +1,8 @@
using System.Windows;
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Windows;
using System.Windows.Interactivity;
using ColorPicker.Helpers;
using ColorPicker.Mouse;
@@ -59,9 +63,9 @@ namespace ColorPicker.Behaviors
windowLeft -= MonitorRightSideDeadZone - ((int)monitorBounds.Right - windowLeft);
}
if ((windowTop + MonitorBottomSideDeadZone / dpi.DpiScaleX) > monitorBounds.Bottom / dpi.DpiScaleX)
if ((windowTop + (MonitorBottomSideDeadZone / dpi.DpiScaleX)) > monitorBounds.Bottom / dpi.DpiScaleX)
{
windowTop -= MonitorBottomSideDeadZone / dpi.DpiScaleX - (((int)monitorBounds.Bottom / dpi.DpiScaleX - windowTop));
windowTop -= (MonitorBottomSideDeadZone / dpi.DpiScaleX) - (((int)monitorBounds.Bottom / dpi.DpiScaleX) - windowTop);
}
AssociatedObject.Left = windowLeft;

View File

@@ -1,6 +1,10 @@
using ColorPicker.Helpers;
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Windows;
using System.Windows.Interactivity;
using ColorPicker.Helpers;
namespace ColorPicker.Behaviors
{

View File

@@ -1,4 +1,8 @@
using System;
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Windows;
using System.Windows.Interactivity;
using System.Windows.Media.Animation;
@@ -7,10 +11,10 @@ namespace ColorPicker.Behaviors
{
public class MoveWindowBehavior : Behavior<Window>
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1401:Fields should be private", Justification = "https://docs.microsoft.com/en-us/dotnet/framework/wpf/advanced/dependency-property-security#:~:text=Dependency%20properties%20should%20generally%20be%20considered%20to%20be,make%20security%20guarantees%20about%20a%20dependency%20property%20value.")]
public static DependencyProperty LeftProperty = DependencyProperty.Register("Left", typeof(double), typeof(MoveWindowBehavior), new PropertyMetadata(new PropertyChangedCallback(LeftPropertyChanged)));
private static void LeftPropertyChanged(DependencyObject d,
DependencyPropertyChangedEventArgs e)
private static void LeftPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var sender = ((MoveWindowBehavior)d).AssociatedObject;
var move = new DoubleAnimation(sender.Left, (double)e.NewValue, new Duration(TimeSpan.FromMilliseconds(150)), FillBehavior.Stop);
@@ -18,10 +22,10 @@ namespace ColorPicker.Behaviors
sender.BeginAnimation(Window.LeftProperty, move, HandoffBehavior.SnapshotAndReplace);
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1401:Fields should be private", Justification = "https://docs.microsoft.com/en-us/dotnet/framework/wpf/advanced/dependency-property-security#:~:text=Dependency%20properties%20should%20generally%20be%20considered%20to%20be,make%20security%20guarantees%20about%20a%20dependency%20property%20value.")]
public static DependencyProperty TopProperty = DependencyProperty.Register("Top", typeof(double), typeof(MoveWindowBehavior), new PropertyMetadata(new PropertyChangedCallback(TopPropertyChanged)));
private static void TopPropertyChanged(DependencyObject d,
DependencyPropertyChangedEventArgs e)
private static void TopPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var sender = ((MoveWindowBehavior)d).AssociatedObject;
var move = new DoubleAnimation(sender.Top, (double)e.NewValue, new Duration(TimeSpan.FromMilliseconds(150)), FillBehavior.Stop);
@@ -35,6 +39,7 @@ namespace ColorPicker.Behaviors
{
return (double)GetValue(LeftProperty);
}
set
{
SetValue(LeftProperty, value);
@@ -47,6 +52,7 @@ namespace ColorPicker.Behaviors
{
return (double)GetValue(TopProperty);
}
set
{
SetValue(TopProperty, value);

View File

@@ -1,4 +1,8 @@
using System;
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Windows;
using System.Windows.Interactivity;
using System.Windows.Media.Animation;
@@ -7,29 +11,36 @@ namespace ColorPicker.Behaviors
{
public class ResizeBehavior : Behavior<FrameworkElement>
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1401:Fields should be private", Justification = "https://docs.microsoft.com/en-us/dotnet/framework/wpf/advanced/dependency-property-security#:~:text=Dependency%20properties%20should%20generally%20be%20considered%20to%20be,make%20security%20guarantees%20about%20a%20dependency%20property%20value.")]
public static DependencyProperty WidthProperty = DependencyProperty.Register("Width", typeof(double), typeof(ResizeBehavior), new PropertyMetadata(new PropertyChangedCallback(WidthPropertyChanged)));
private static void WidthPropertyChanged(DependencyObject d,
DependencyPropertyChangedEventArgs e)
private static void WidthPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var sender = ((ResizeBehavior)d).AssociatedObject;
var move = new DoubleAnimation(sender.Width, (double)e.NewValue, new Duration(TimeSpan.FromMilliseconds(150)), FillBehavior.Stop);
move.Completed += (s, e1) =>
{
sender.BeginAnimation(FrameworkElement.WidthProperty, null); sender.Width = (double)e.NewValue;
sender.BeginAnimation(FrameworkElement.WidthProperty, null);
sender.Width = (double)e.NewValue;
};
move.EasingFunction = new QuadraticEase() { EasingMode = EasingMode.EaseOut };
sender.BeginAnimation(FrameworkElement.WidthProperty, move, HandoffBehavior.SnapshotAndReplace);
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1401:Fields should be private", Justification = "https://docs.microsoft.com/en-us/dotnet/framework/wpf/advanced/dependency-property-security#:~:text=Dependency%20properties%20should%20generally%20be%20considered%20to%20be,make%20security%20guarantees%20about%20a%20dependency%20property%20value.")]
public static DependencyProperty HeightProperty = DependencyProperty.Register("Height", typeof(double), typeof(ResizeBehavior), new PropertyMetadata(new PropertyChangedCallback(HeightPropertyChanged)));
private static void HeightPropertyChanged(DependencyObject d,
DependencyPropertyChangedEventArgs e)
private static void HeightPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var sender = ((ResizeBehavior)d).AssociatedObject;
var move = new DoubleAnimation(sender.Height, (double)e.NewValue, new Duration(TimeSpan.FromMilliseconds(150)), FillBehavior.Stop);
move.Completed += (s, e1) => { sender.BeginAnimation(FrameworkElement.HeightProperty, null); sender.Height = (double)e.NewValue; };
move.Completed += (s, e1) =>
{
sender.BeginAnimation(FrameworkElement.HeightProperty, null);
sender.Height = (double)e.NewValue;
};
move.EasingFunction = new QuadraticEase() { EasingMode = EasingMode.EaseOut };
sender.BeginAnimation(FrameworkElement.HeightProperty, move, HandoffBehavior.SnapshotAndReplace);
}
@@ -40,6 +51,7 @@ namespace ColorPicker.Behaviors
{
return (double)GetValue(WidthProperty);
}
set
{
SetValue(WidthProperty, value);
@@ -52,6 +64,7 @@ namespace ColorPicker.Behaviors
{
return (double)GetValue(HeightProperty);
}
set
{
SetValue(HeightProperty, value);

View File

@@ -1,4 +1,8 @@
using System.ComponentModel.Composition;
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
namespace ColorPicker

View File

@@ -230,5 +230,20 @@
<Name>Microsoft.PowerToys.Settings.UI.Lib</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Compile Include="..\..\..\codeAnalysis\GlobalSuppressions.cs">
<Link>GlobalSuppressions.cs</Link>
</Compile>
<AdditionalFiles Include="..\..\..\codeAnalysis\StyleCop.json">
<Link>StyleCop.json</Link>
</AdditionalFiles>
</ItemGroup>
<ItemGroup>
<PackageReference Include="StyleCop.Analyzers">
<Version>1.1.118</Version>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

View File

@@ -1,5 +1,10 @@
using System;
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Windows.Input;
namespace ColorPicker.Common
{
public class RelayCommand : ICommand
@@ -13,7 +18,8 @@ namespace ColorPicker.Common
_execute = x => { execute.Invoke(); };
}
public RelayCommand(Action<object> execute) : this(x => true, execute)
public RelayCommand(Action<object> execute)
: this(x => true, execute)
{
}

View File

@@ -1,4 +1,8 @@
using System.ComponentModel;
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace ColorPicker.Common

View File

@@ -1,4 +1,8 @@
using System;
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.ComponentModel.Composition;
using System.Windows;

View File

@@ -1,4 +1,8 @@
using System;
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Diagnostics;
using System.Globalization;
using System.IO;
@@ -7,7 +11,7 @@ namespace ColorPicker.Helpers
{
public static class Logger
{
private static string ApplicationLogPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "ColorPicker");
private static readonly string ApplicationLogPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "ColorPicker");
static Logger()
{
@@ -30,7 +34,8 @@ namespace ColorPicker.Helpers
public static void LogError(string message, Exception ex)
{
Log(message + Environment.NewLine +
Log(
message + Environment.NewLine +
ex?.Message + Environment.NewLine +
"Inner exception: " + Environment.NewLine +
ex?.InnerException?.Message + Environment.NewLine +

View File

@@ -1,4 +1,8 @@
using System;
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

View File

@@ -1,6 +1,7 @@
using ColorPicker.Telemetry;
using ColorPicker.ViewModelContracts;
using Microsoft.PowerToys.Telemetry;
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.ComponentModel.Composition;
using System.Drawing;
@@ -8,6 +9,9 @@ using System.Drawing.Imaging;
using System.IO;
using System.Windows;
using System.Windows.Media.Imaging;
using ColorPicker.Telemetry;
using ColorPicker.ViewModelContracts;
using Microsoft.PowerToys.Telemetry;
namespace ColorPicker.Helpers
{
@@ -19,11 +23,12 @@ namespace ColorPicker.Helpers
private const int MaxZoomLevel = 3;
private const int MinZoomLevel = 0;
private readonly IZoomViewModel _zoomViewModel;
private readonly AppStateHandler _appStateHandler;
private int _currentZoomLevel = 0;
private int _previousZoomLevel = 0;
private readonly IZoomViewModel _zoomViewModel;
private readonly AppStateHandler _appStateHandler;
private ZoomWindow _zoomWindow;
private double _lastLeft;
@@ -79,8 +84,8 @@ namespace ColorPicker.Helpers
// we just started zooming, copy screen area
if (_previousZoomLevel == 0)
{
var x = (int)point.X - BaseZoomImageSize / 2;
var y = (int)point.Y - BaseZoomImageSize / 2;
var x = (int)point.X - (BaseZoomImageSize / 2);
var y = (int)point.Y - (BaseZoomImageSize / 2);
var rect = new Rectangle(x, y, BaseZoomImageSize, BaseZoomImageSize);
var bmp = new Bitmap(rect.Width, rect.Height, PixelFormat.Format32bppArgb);
var g = Graphics.FromImage(bmp);
@@ -143,8 +148,8 @@ namespace ColorPicker.Helpers
_previousScaledY = y / dpi.DpiScaleY;
}
_lastLeft = Math.Floor(_previousScaledX - ((BaseZoomImageSize * Math.Pow(ZoomFactor, _currentZoomLevel - 1)) / 2));
_lastTop = Math.Floor(_previousScaledY - ((BaseZoomImageSize * Math.Pow(ZoomFactor, _currentZoomLevel - 1)) / 2));
_lastLeft = Math.Floor(_previousScaledX - (BaseZoomImageSize * Math.Pow(ZoomFactor, _currentZoomLevel - 1) / 2));
_lastTop = Math.Floor(_previousScaledY - (BaseZoomImageSize * Math.Pow(ZoomFactor, _currentZoomLevel - 1) / 2));
var justShown = false;
if (!_zoomWindow.IsVisible)
@@ -155,11 +160,12 @@ namespace ColorPicker.Helpers
_zoomViewModel.Width = BaseZoomImageSize;
_zoomWindow.Show();
justShown = true;
// make sure color picker window is on top of just opened zoom window
AppStateHandler.SetTopMost();
}
// dirty hack - sometimes when we just show a window on a second monitor with different DPI,
// dirty hack - sometimes when we just show a window on a second monitor with different DPI,
// window position is not set correctly on a first time, we need to "ping" it again to make it appear on the proper location
if (justShown)
{

View File

@@ -1,4 +1,8 @@
using System;
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.InteropServices;
@@ -47,6 +51,7 @@ namespace ColorPicker.Keyboard
int errorCode = Marshal.GetLastWin32Error();
throw new Win32Exception(errorCode, $"Failed to remove keyboard hooks for '{Process.GetCurrentProcess().ProcessName}'. Error {errorCode}: {new Win32Exception(Marshal.GetLastWin32Error()).Message}.");
}
_windowsHookHandle = IntPtr.Zero;
// ReSharper disable once DelegateSubtraction
@@ -56,11 +61,13 @@ namespace ColorPicker.Keyboard
if (_user32LibraryHandle != IntPtr.Zero)
{
if (!FreeLibrary(_user32LibraryHandle)) // reduces reference to library by 1.
// reduces reference to library by 1.
if (!FreeLibrary(_user32LibraryHandle))
{
int errorCode = Marshal.GetLastWin32Error();
throw new Win32Exception(errorCode, $"Failed to unload library 'User32.dll'. Error {errorCode}: {new Win32Exception(Marshal.GetLastWin32Error()).Message}.");
}
_user32LibraryHandle = IntPtr.Zero;
}
}
@@ -81,7 +88,7 @@ namespace ColorPicker.Keyboard
KeyDown = 0x0100,
KeyUp = 0x0101,
SysKeyDown = 0x0104,
SysKeyUp = 0x0105
SysKeyUp = 0x0105,
}
private IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam)

View File

@@ -1,4 +1,8 @@
using System.ComponentModel;
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.ComponentModel;
using static ColorPicker.Win32Apis;
namespace ColorPicker.Keyboard
@@ -6,6 +10,7 @@ namespace ColorPicker.Keyboard
internal class GlobalKeyboardHookEventArgs : HandledEventArgs
{
internal GlobalKeyboardHook.KeyboardState KeyboardState { get; private set; }
internal LowLevelKeyboardInputEvent KeyboardData { get; private set; }
internal GlobalKeyboardHookEventArgs(

View File

@@ -1,4 +1,8 @@
using System;
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Windows.Input;
@@ -59,8 +63,9 @@ namespace ColorPicker.Keyboard
private void Hook_KeyboardPressed(object sender, GlobalKeyboardHookEventArgs e)
{
var virtualCode = e.KeyboardData.VirtualCode;
// ESC pressed
if(virtualCode == KeyInterop.VirtualKeyFromKey(Key.Escape))
if (virtualCode == KeyInterop.VirtualKeyFromKey(Key.Escape))
{
_currentlyPressedKeys.Clear();
_appStateHandler.HideColorPicker();
@@ -113,6 +118,7 @@ namespace ColorPicker.Keyboard
return false;
}
}
return true;
}
}

View File

@@ -1,6 +1,10 @@
using ColorPicker.ViewModelContracts;
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.ComponentModel.Composition;
using System.Windows;
using ColorPicker.ViewModelContracts;
namespace ColorPicker
{
@@ -18,7 +22,6 @@ namespace ColorPicker
Hide();
}
[Import]
public IMainViewModel MainViewModel { get; set; }

View File

@@ -1,7 +1,11 @@
using ColorPicker.Helpers;
using Microsoft.Win32;
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.IO;
using ColorPicker.Helpers;
using Microsoft.Win32;
namespace ColorPicker.Mouse
{
@@ -19,6 +23,11 @@ namespace ColorPicker.Mouse
private static string _originalCrosshairCursorPath;
private static string _originalHandCursorPath;
[System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.NamingRules", "SA1310:Field names should not contain underscore", Justification = "Interop object")]
private const int SPI_SETCURSORS = 0x0057;
[System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.NamingRules", "SA1310:Field names should not contain underscore", Justification = "Interop object")]
private const int SPIF_SENDCHANGE = 0x02;
public static void SetColorPickerCursor()
{
BackupOriginalCursors();
@@ -60,21 +69,21 @@ namespace ColorPicker.Mouse
{
_originalArrowCursorPath = (string)Registry.GetValue(CursorsRegistryPath, ArrowRegistryName, string.Empty);
}
if (string.IsNullOrEmpty(_originalIBeamCursorPath))
{
_originalIBeamCursorPath = (string)Registry.GetValue(CursorsRegistryPath, IBeamRegistryName, string.Empty);
}
if (string.IsNullOrEmpty(_originalCrosshairCursorPath))
{
_originalCrosshairCursorPath = (string)Registry.GetValue(CursorsRegistryPath, CrosshairRegistryName, string.Empty);
}
if (string.IsNullOrEmpty(_originalHandCursorPath))
{
_originalHandCursorPath = (string)Registry.GetValue(CursorsRegistryPath, HandRegistryName, string.Empty);
}
}
const int SPI_SETCURSORS = 0x0057;
const int SPIF_SENDCHANGE = 0x02;
}
}

View File

@@ -1,4 +1,8 @@
using System;
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Drawing;
namespace ColorPicker.Mouse

View File

@@ -1,4 +1,8 @@
using System;
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Runtime.InteropServices;
@@ -11,15 +15,20 @@ namespace ColorPicker.Mouse
internal class MouseHook
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.NamingRules", "SA1310:Field names should not contain underscore", Justification = "Interop object")]
private const int WH_MOUSE_LL = 14;
[System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.NamingRules", "SA1310:Field names should not contain underscore", Justification = "Interop object")]
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 WHEEL_DELTA = 120;
private IntPtr _mouseHookHandle;
private HookProc _mouseDelegate;
private event MouseUpEventHandler MouseDown;
public event MouseUpEventHandler OnMouseDown
{
add
@@ -27,6 +36,7 @@ namespace ColorPicker.Mouse
Subscribe();
MouseDown += value;
}
remove
{
MouseDown -= value;
@@ -35,6 +45,7 @@ namespace ColorPicker.Mouse
}
private event MouseWheelEventHandler MouseWheel;
public event MouseWheelEventHandler OnMouseWheel
{
add
@@ -42,6 +53,7 @@ namespace ColorPicker.Mouse
Subscribe();
MouseWheel += value;
}
remove
{
MouseWheel -= value;
@@ -69,10 +81,12 @@ namespace ColorPicker.Mouse
if (_mouseHookHandle == IntPtr.Zero)
{
_mouseDelegate = MouseHookProc;
_mouseHookHandle = SetWindowsHookEx(WH_MOUSE_LL,
_mouseHookHandle = SetWindowsHookEx(
WH_MOUSE_LL,
_mouseDelegate,
GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName),
0);
if (_mouseHookHandle == IntPtr.Zero)
{
int errorCode = Marshal.GetLastWin32Error();
@@ -92,8 +106,10 @@ namespace ColorPicker.Mouse
{
MouseDown.Invoke(null, new System.Drawing.Point(mouseHookStruct.pt.x, mouseHookStruct.pt.y));
}
return new IntPtr(-1);
}
if (wParam.ToInt32() == WM_MOUSEWHEEL)
{
if (MouseWheel != null)
@@ -103,6 +119,7 @@ namespace ColorPicker.Mouse
}
}
}
return CallNextHookEx(_mouseHookHandle, nCode, wParam, lParam);
}
}

View File

@@ -1,4 +1,8 @@
using System;
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.ComponentModel.Composition;
using System.Drawing;
using System.Drawing.Imaging;

View File

@@ -1,4 +1,8 @@
using Microsoft.PowerToys.Settings.UI.Lib;
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using Microsoft.PowerToys.Settings.UI.Lib;
namespace ColorPicker.Settings
{

View File

@@ -1,8 +1,12 @@
using System;
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.ComponentModel;
namespace ColorPicker.Settings
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1649:File name should match first type name", Justification = "It does, it is a generic. False positive")]
public sealed class SettingItem<T> : INotifyPropertyChanged
{
private T _value;
@@ -18,6 +22,7 @@ namespace ColorPicker.Settings
{
return _value;
}
set
{
_value = value;

View File

@@ -1,11 +1,14 @@
using System.IO;
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.ComponentModel.Composition;
using System.IO;
using System.Threading;
using ColorPicker.Helpers;
using Microsoft.PowerToys.Settings.UI.Lib;
using Microsoft.PowerToys.Settings.UI.Lib.Utilities;
using ColorPicker.Helpers;
using System.Threading.Tasks;
using System.Threading;
using System;
namespace ColorPicker.Settings
{
@@ -57,6 +60,7 @@ namespace ColorPicker.Settings
var defaultColorPickerSettings = new ColorPickerSettings();
defaultColorPickerSettings.Save();
}
var settings = SettingsUtils.GetSettings<ColorPickerSettings>(ColorPickerModuleName);
if (settings != null)
{
@@ -73,6 +77,7 @@ namespace ColorPicker.Settings
{
retry = false;
}
Logger.LogError("Failed to read changed settings", ex);
Thread.Sleep(500);
}
@@ -81,7 +86,7 @@ namespace ColorPicker.Settings
Logger.LogError("Failed to read changed settings", ex);
}
}
};
}
}
}
}

View File

@@ -1,6 +1,10 @@
using Microsoft.PowerToys.Telemetry;
using Microsoft.PowerToys.Telemetry.Events;
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Diagnostics.Tracing;
using Microsoft.PowerToys.Telemetry;
using Microsoft.PowerToys.Telemetry.Events;
namespace ColorPicker.Telemetry
{

View File

@@ -1,6 +1,10 @@
using Microsoft.PowerToys.Telemetry;
using Microsoft.PowerToys.Telemetry.Events;
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Diagnostics.Tracing;
using Microsoft.PowerToys.Telemetry;
using Microsoft.PowerToys.Telemetry.Events;
namespace ColorPicker.Telemetry
{

View File

@@ -1,6 +1,10 @@
using Microsoft.PowerToys.Telemetry;
using Microsoft.PowerToys.Telemetry.Events;
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Diagnostics.Tracing;
using Microsoft.PowerToys.Telemetry;
using Microsoft.PowerToys.Telemetry.Events;
namespace ColorPicker.Telemetry
{

View File

@@ -1,4 +1,8 @@
using System.Windows.Media;
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Windows.Media;
namespace ColorPicker.ViewModelContracts
{

View File

@@ -1,4 +1,8 @@
using System.Windows.Media.Imaging;
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Windows.Media.Imaging;
namespace ColorPicker.ViewModelContracts
{

View File

@@ -1,4 +1,8 @@
using System;
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.ComponentModel.Composition;
using System.Globalization;
using System.Runtime.InteropServices;
@@ -9,8 +13,8 @@ using ColorPicker.Helpers;
using ColorPicker.Keyboard;
using ColorPicker.Mouse;
using ColorPicker.Settings;
using ColorPicker.ViewModelContracts;
using ColorPicker.Telemetry;
using ColorPicker.ViewModelContracts;
using Microsoft.PowerToys.Settings.UI.Lib;
using Microsoft.PowerToys.Telemetry;
@@ -19,12 +23,12 @@ namespace ColorPicker.ViewModels
[Export(typeof(IMainViewModel))]
public class MainViewModel : ViewModelBase, IMainViewModel
{
private string _hexColor;
private string _rgbColor;
private Brush _colorBrush;
private readonly ZoomWindowHelper _zoomWindowHelper;
private readonly AppStateHandler _appStateHandler;
private readonly IUserSettings _userSettings;
private string _hexColor;
private string _rgbColor;
private Brush _colorBrush;
[ImportingConstructor]
public MainViewModel(
@@ -51,6 +55,7 @@ namespace ColorPicker.ViewModels
{
return _hexColor;
}
private set
{
_hexColor = value;
@@ -64,6 +69,7 @@ namespace ColorPicker.ViewModels
{
return _rgbColor;
}
private set
{
_rgbColor = value;
@@ -77,6 +83,7 @@ namespace ColorPicker.ViewModels
{
return _colorBrush;
}
private set
{
_colorBrush = value;
@@ -133,6 +140,7 @@ namespace ColorPicker.ViewModels
Logger.LogError("Failed to set text into clipboard", ex);
}
}
System.Threading.Thread.Sleep(10);
}
}

View File

@@ -1,7 +1,11 @@
using ColorPicker.Common;
using ColorPicker.ViewModelContracts;
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.ComponentModel.Composition;
using System.Windows.Media.Imaging;
using ColorPicker.Common;
using ColorPicker.ViewModelContracts;
namespace ColorPicker.ViewModels
{
@@ -26,6 +30,7 @@ namespace ColorPicker.ViewModels
{
return _zoomArea;
}
set
{
_zoomArea = value;
@@ -39,6 +44,7 @@ namespace ColorPicker.ViewModels
{
return _zoomFactor;
}
set
{
_zoomFactor = value;
@@ -52,6 +58,7 @@ namespace ColorPicker.ViewModels
{
return _desiredWidth;
}
set
{
_desiredWidth = value;
@@ -65,6 +72,7 @@ namespace ColorPicker.ViewModels
{
return _desiredHeight;
}
set
{
_desiredHeight = value;
@@ -78,6 +86,7 @@ namespace ColorPicker.ViewModels
{
return _width;
}
set
{
_width = value;
@@ -91,6 +100,7 @@ namespace ColorPicker.ViewModels
{
return _height;
}
set
{
_height = value;

View File

@@ -1,17 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace ColorPicker.Views
{

View File

@@ -1,17 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace ColorPicker.Views
{

View File

@@ -1,4 +1,8 @@
using System;
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
@@ -6,10 +10,11 @@ namespace ColorPicker
{
public static class Win32Apis
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.NamingRules", "SA1310:Field names should not contain underscore", Justification = "Interop")]
public const int WH_KEYBOARD_LL = 13;
public const int VkSnapshot = 0x2c;
public const int KfAltdown = 0x2000;
public const int LlkhfAltdown = (KfAltdown >> 8);
public const int LlkhfAltdown = KfAltdown >> 8;
public const int MonitorinfofPrimary = 0x00000001;
public delegate bool MonitorEnumProc(
@@ -23,9 +28,7 @@ namespace ColorPicker
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
internal static extern bool FreeLibrary(IntPtr hModule);
[DllImport("kernel32.dll", CharSet = CharSet.Unicode,
CallingConvention = CallingConvention.StdCall)]
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall)]
internal static extern IntPtr GetModuleHandle(string name);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
@@ -41,21 +44,19 @@ namespace ColorPicker
[DllImport("user32.dll")]
internal static extern bool GetCursorPos(out PointInter lpPoint);
[DllImport("user32.dll", CharSet = CharSet.Auto,
CallingConvention = CallingConvention.StdCall, SetLastError = true)]
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, SetLastError = true)]
internal static extern IntPtr SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hMod, int dwThreadId);
[DllImport("user32.dll", CharSet = CharSet.Auto,
CallingConvention = CallingConvention.StdCall, SetLastError = true)]
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, SetLastError = true)]
internal static extern bool UnhookWindowsHookEx(IntPtr idHook);
[DllImport("user32.dll", CharSet = CharSet.Auto,
CallingConvention = CallingConvention.StdCall, SetLastError = true)]
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, SetLastError = true)]
internal static extern IntPtr CallNextHookEx(IntPtr idHook, int nCode, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", EntryPoint = "SystemParametersInfo")]
internal static extern bool SystemParametersInfo(int uiAction, int uiParam, IntPtr pvParam, int fWinIni);
[System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.NamingRules", "SA1307:Accessible fields should begin with upper-case letter", Justification = "Interop object")]
[StructLayout(LayoutKind.Sequential)]
internal struct POINT
{
@@ -63,6 +64,7 @@ namespace ColorPicker
public int y;
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.NamingRules", "SA1307:Accessible fields should begin with upper-case letter", Justification = "Interop object")]
[StructLayout(LayoutKind.Sequential)]
internal struct MSLLHOOKSTRUCT
{
@@ -78,10 +80,11 @@ namespace ColorPicker
{
public int X;
public int Y;
public static explicit operator System.Windows.Point(PointInter point) => new System.Windows.Point(point.X, point.Y);
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.NamingRules", "SA1307:Accessible fields should begin with upper-case letter", Justification = "Interop object")]
[StructLayout(LayoutKind.Sequential)]
internal struct Rect
{
@@ -91,15 +94,17 @@ namespace ColorPicker
public int bottom;
}
[System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.NamingRules", "SA1307:Accessible fields should begin with upper-case letter", Justification = "Interop object")]
[System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.MaintainabilityRules", "SA1401:Fields should be private", Justification = "false positive, used in MonitorResolutionHelper")]
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto, Pack = 4)]
internal class MonitorInfoEx
{
internal int cbSize = Marshal.SizeOf(typeof(MonitorInfoEx));
internal Rect rcMonitor = default;
internal Rect rcWork = default;
internal int dwFlags = 0;
public int cbSize = Marshal.SizeOf(typeof(MonitorInfoEx));
public Rect rcMonitor = default;
public Rect rcWork = default;
public int dwFlags = 0;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
internal char[] szDevice = new char[32];
public char[] szDevice = new char[32];
}
[StructLayout(LayoutKind.Sequential)]
@@ -111,7 +116,7 @@ namespace ColorPicker
public int VirtualCode;
/// <summary>
/// A hardware scan code for the key.
/// A hardware scan code for the key.
/// </summary>
public int HardwareScanCode;
@@ -126,10 +131,9 @@ namespace ColorPicker
public int TimeStamp;
/// <summary>
/// Additional information associated with the message.
/// Additional information associated with the message.
/// </summary>
public IntPtr AdditionalInformation;
}
}
}

View File

@@ -1,4 +1,8 @@
using System.ComponentModel;
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.ComponentModel;
using System.Windows;
namespace ColorPicker
@@ -16,12 +20,14 @@ namespace ColorPicker
InitializeComponent();
DataContext = this;
}
public double DesiredLeft
{
get
{
return _left;
}
set
{
_left = value;
@@ -35,6 +41,7 @@ namespace ColorPicker
{
return _top;
}
set
{
_top = value;