Skip to content

🚧 Testing input injector with 1.2.221109.1 WinAppSDK in CI #183

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 13 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix issue with Offset in Windows App SDK for Window Chrome in Input I…
…njection Tests
  • Loading branch information
michael-hawker committed Nov 17, 2022
commit 219b3cdc53ea40cac5f56b3efc0981600b02af60
33 changes: 33 additions & 0 deletions common/CommunityToolkit.Labs.Tests.Shared/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Navigation;
using Microsoft.VisualStudio.TestTools.UnitTesting.AppContainer;
using System.Runtime.InteropServices;
#endif

namespace CommunityToolkit.Labs.Tests;
Expand All @@ -40,6 +41,22 @@ public sealed partial class App : Application
#if WINAPPSDK
private static Microsoft.UI.Xaml.Window currentWindow = Microsoft.UI.Xaml.Window.Current;


[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int Left; // x position of upper-left corner
public int Top; // y position of upper-left corner
public int Right; // x position of lower-right corner
public int Bottom; // y position of lower-right corner
}

[DllImport("user32.dll")]
static extern bool GetClientRect(IntPtr hWnd, out RECT lpRect);

[DllImport("user32", ExactSpelling = true, SetLastError = true)]
internal static extern int MapWindowPoints(IntPtr hWndFrom, IntPtr hWndTo, [In, Out] ref RECT rect, [MarshalAs(UnmanagedType.U4)] int cPoints);

private static AppWindow GetAppWindow(Microsoft.UI.Xaml.Window window)
{
// From: https://docs.microsoft.com/en-us/windows/apps/windows-app-sdk/windowing/windowing-overview#code-example
Expand All @@ -56,6 +73,22 @@ private static AppWindow GetAppWindow(Microsoft.UI.Xaml.Window window)
public static Rect Bounds { get
{
var appWindow = GetAppWindow(currentWindow);

var hWnd = WinRT.Interop.WindowNative.GetWindowHandle(currentWindow);

// Get client area position
RECT ir = new RECT();
MapWindowPoints(hWnd, IntPtr.Zero, ref ir, 1);

RECT rct;

// And size
if (GetClientRect(hWnd, out rct))
{
return new Rect(ir.Left, ir.Top, rct.Right - rct.Left, rct.Bottom - rct.Top);
}

// AppWindow uses GetWindowRect which includes the chrome, not what we want... TODO: File bug currentWindow.Bounds should be the same between the two platforms. Should AppWindow also have Bounds?
var position = appWindow.Position;
var size = appWindow.Size;
return new Rect(position.X, position.Y, size.Width, size.Height);
Expand Down
1 change: 1 addition & 0 deletions tests/Labs.Tests.WinAppSdk.props
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
</ItemGroup>

<PropertyGroup>
<IsTestProject>true</IsTestProject>
<!-- Removed in 1.1+, see this bug: https://developercommunity.visualstudio.com/t/Windows-App-SDK-11-Unit-Tests-not-laun/10192460 -->
<WindowsAppContainer>true</WindowsAppContainer>
</PropertyGroup>
Expand Down