Dependencies cleanup (#34011)

<!-- Enter a brief description/summary of your PR here. What does it
fix/what does it change/how was it tested (even manually, if necessary)?
-->
## Summary of the Pull Request

- Removed `Vanara.PInvoke.*` packages used in Quick Accent in favor of
`Microsoft.Windows.CsWin32` that is already in use for other utilities.
- Removed `Microsoft.Windows.SDK.Contracts` package instanned only on
MWB. This is not needed:
https://learn.microsoft.com/windows/apps/desktop/modernize/desktop-to-uwp-enhance#modify-a-net-project-to-use-windows-runtime-apis
- Cleaned up obsolete packages from `Directory.Packages.props`: I
realized that uninstalling a NuGet package from all projects doesn't
remove it from `Directory.Packages.props`.

<!-- Please review the items on the PR checklist before submitting-->
## PR Checklist

- [ ] **Closes:** #xxx
- [ ] **Communication:** I've discussed this with core contributors
already. If work hasn't been agreed, this work might be rejected
- [ ] **Tests:** Added/updated and all pass
- [ ] **Localization:** All end user facing strings can be localized
- [ ] **Dev docs:** Added/updated
- [ ] **New binaries:** Added on the required places
- [ ] [JSON for
signing](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ESRPSigning_core.json)
for new binaries
- [ ] [WXS for
installer](https://github.com/microsoft/PowerToys/blob/main/installer/PowerToysSetup/Product.wxs)
for new binaries and localization folder
- [ ] [YML for CI
pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/ci/templates/build-powertoys-steps.yml)
for new test projects
- [ ] [YML for signed
pipeline](https://github.com/microsoft/PowerToys/blob/main/.pipelines/release.yml)
- [ ] **Documentation updated:** If checked, please file a pull request
on [our docs
repo](https://github.com/MicrosoftDocs/windows-uwp/tree/docs/hub/powertoys)
and link it here: #xxx

<!-- Provide a more detailed description of the PR, other things fixed
or any additional comments/features here -->
## Detailed Description of the Pull Request / Additional comments

<!-- Describe how you validated the behavior. Add automated tests
wherever possible, but list manual validation steps taken as well -->
## Validation Steps Performed

- Manually tested Quick Accent
This commit is contained in:
Davide Giacometti
2024-08-07 14:16:38 +02:00
committed by GitHub
parent 3798a101a6
commit 3e133ae8bd
7 changed files with 80 additions and 54 deletions

View File

@@ -215,7 +215,6 @@
<ItemGroup>
<PackageReference Include="Microsoft.Windows.CsWinRT" />
<PackageReference Include="Microsoft.Windows.Compatibility" />
<PackageReference Include="Microsoft.Windows.SDK.Contracts" />
<PackageReference Include="StreamJsonRpc" />
<PackageReference Include="System.Data.SqlClient" /> <!-- It's a dependency of Microsoft.Windows.Compatibility. We're adding it here to force it to the version specified in Directory.Packages.props -->
</ItemGroup>

View File

@@ -24,7 +24,7 @@ public struct Point
Y = y;
}
public Point(Vanara.PInvoke.POINT point)
public Point(System.Drawing.Point point)
{
X = point.X;
Y = point.Y;
@@ -34,7 +34,7 @@ public struct Point
public double Y { get; init; }
public static implicit operator Point(Vanara.PInvoke.POINT point) => new Point(point.X, point.Y);
public static implicit operator Point(System.Drawing.Point point) => new Point(point.X, point.Y);
public static Point operator /(Point point, double divider)
{

View File

@@ -0,0 +1,6 @@
GetDpiForWindow
GetGUIThreadInfo
GetKeyState
GetMonitorInfo
MonitorFromWindow
SendInput

View File

@@ -23,11 +23,13 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Windows.CsWin32">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Windows.CsWinRT" />
<PackageReference Include="System.Configuration.ConfigurationManager" />
<PackageReference Include="UnicodeInformation" />
<PackageReference Include="Vanara.PInvoke.User32" />
<PackageReference Include="Vanara.PInvoke.Shell32" />
</ItemGroup>
<ItemGroup>

View File

@@ -3,7 +3,10 @@
// See the LICENSE file in the project root for more information.
using System.Runtime.InteropServices;
using Vanara.PInvoke;
using Windows.Win32;
using Windows.Win32.Graphics.Gdi;
using Windows.Win32.UI.Input.KeyboardAndMouse;
using Windows.Win32.UI.WindowsAndMessaging;
namespace PowerAccent.Core.Tools;
@@ -16,72 +19,98 @@ internal static class WindowsFunctions
if (back)
{
// Split in 2 different SendInput (Powershell doesn't take back issue)
var inputsBack = new User32.INPUT[]
var inputsBack = new INPUT[]
{
new User32.INPUT { type = User32.INPUTTYPE.INPUT_KEYBOARD, ki = new User32.KEYBDINPUT { wVk = (ushort)User32.VK.VK_BACK } },
new User32.INPUT { type = User32.INPUTTYPE.INPUT_KEYBOARD, ki = new User32.KEYBDINPUT { wVk = (ushort)User32.VK.VK_BACK, dwFlags = User32.KEYEVENTF.KEYEVENTF_KEYUP } },
new INPUT
{
type = INPUT_TYPE.INPUT_KEYBOARD,
Anonymous = new INPUT._Anonymous_e__Union
{
ki = new KEYBDINPUT
{
wVk = VIRTUAL_KEY.VK_BACK,
},
},
},
new INPUT
{
type = INPUT_TYPE.INPUT_KEYBOARD,
Anonymous = new INPUT._Anonymous_e__Union
{
ki = new KEYBDINPUT
{
wVk = VIRTUAL_KEY.VK_BACK,
dwFlags = KEYBD_EVENT_FLAGS.KEYEVENTF_KEYUP,
},
},
},
};
var temp1 = User32.SendInput((uint)inputsBack.Length, inputsBack, sizeof(User32.INPUT));
System.Threading.Thread.Sleep(1); // Some apps, like Terminal, need a little wait to process the sent backspace or they'll ignore it.
_ = PInvoke.SendInput(inputsBack, Marshal.SizeOf<INPUT>());
Thread.Sleep(1); // Some apps, like Terminal, need a little wait to process the sent backspace or they'll ignore it.
}
foreach (char c in s)
{
// Letter
var inputsInsert = new User32.INPUT[]
var inputsInsert = new INPUT[]
{
new User32.INPUT { type = User32.INPUTTYPE.INPUT_KEYBOARD, ki = new User32.KEYBDINPUT { wVk = 0, dwFlags = User32.KEYEVENTF.KEYEVENTF_UNICODE, wScan = c } },
new User32.INPUT { type = User32.INPUTTYPE.INPUT_KEYBOARD, ki = new User32.KEYBDINPUT { wVk = 0, dwFlags = User32.KEYEVENTF.KEYEVENTF_UNICODE | User32.KEYEVENTF.KEYEVENTF_KEYUP, wScan = c } },
new INPUT
{
type = INPUT_TYPE.INPUT_KEYBOARD,
Anonymous = new INPUT._Anonymous_e__Union
{
ki = new KEYBDINPUT
{
wScan = c,
dwFlags = KEYBD_EVENT_FLAGS.KEYEVENTF_UNICODE,
},
},
},
new INPUT
{
type = INPUT_TYPE.INPUT_KEYBOARD,
Anonymous = new INPUT._Anonymous_e__Union
{
ki = new KEYBDINPUT
{
wScan = c,
dwFlags = KEYBD_EVENT_FLAGS.KEYEVENTF_UNICODE | KEYBD_EVENT_FLAGS.KEYEVENTF_KEYUP,
},
},
},
};
var temp2 = User32.SendInput((uint)inputsInsert.Length, inputsInsert, sizeof(User32.INPUT));
_ = PInvoke.SendInput(inputsInsert, Marshal.SizeOf<INPUT>());
}
}
}
public static Point GetCaretPosition()
{
User32.GUITHREADINFO guiInfo = default;
guiInfo.cbSize = (uint)Marshal.SizeOf(guiInfo);
User32.GetGUIThreadInfo(0, ref guiInfo);
POINT caretPosition = new POINT(guiInfo.rcCaret.left, guiInfo.rcCaret.top);
User32.ClientToScreen(guiInfo.hwndCaret, ref caretPosition);
if (caretPosition.X == 0)
{
POINT testPoint;
User32.GetCaretPos(out testPoint);
return testPoint;
}
return caretPosition;
}
public static (Point Location, Size Size, double Dpi) GetActiveDisplay()
{
User32.GUITHREADINFO guiInfo = default;
GUITHREADINFO guiInfo = default;
guiInfo.cbSize = (uint)Marshal.SizeOf(guiInfo);
User32.GetGUIThreadInfo(0, ref guiInfo);
var res = User32.MonitorFromWindow(guiInfo.hwndActive, User32.MonitorFlags.MONITOR_DEFAULTTONEAREST);
PInvoke.GetGUIThreadInfo(0, ref guiInfo);
var res = PInvoke.MonitorFromWindow(guiInfo.hwndActive, MONITOR_FROM_FLAGS.MONITOR_DEFAULTTONEAREST);
User32.MONITORINFO monitorInfo = default;
MONITORINFO monitorInfo = default;
monitorInfo.cbSize = (uint)Marshal.SizeOf(monitorInfo);
User32.GetMonitorInfo(res, ref monitorInfo);
PInvoke.GetMonitorInfo(res, ref monitorInfo);
double dpi = User32.GetDpiForWindow(guiInfo.hwndActive) / 96d;
return (monitorInfo.rcWork.Location, monitorInfo.rcWork.Size, dpi);
double dpi = PInvoke.GetDpiForWindow(guiInfo.hwndActive) / 96d;
var location = new Point(monitorInfo.rcWork.left, monitorInfo.rcWork.top);
return (location, monitorInfo.rcWork.Size, dpi);
}
public static bool IsCapsLockState()
{
var capital = User32.GetKeyState((int)User32.VK.VK_CAPITAL);
var capital = PInvoke.GetKeyState((int)VIRTUAL_KEY.VK_CAPITAL);
return capital != 0;
}
public static bool IsShiftState()
{
var shift = User32.GetKeyState((int)User32.VK.VK_SHIFT);
var shift = PInvoke.GetKeyState((int)VIRTUAL_KEY.VK_SHIFT);
return shift < 0;
}
}