using FlaUI.UIA3; using System.Runtime.InteropServices; using XFEWorkflow.Infrastructure.Storage; namespace XFEWorkflow.Services; internal static partial class UiSmokeTest { internal static async Task PrepareNativeInputAsync(string output) { var handle = WinRT.Interop.WindowNative.GetWindowHandle(App.MainWindow); var before = GetForegroundWindow(); App.MainWindow.AppWindow.Show(); App.MainWindow.Activate(); var activated = SetForegroundWindow(handle); await Task.Delay(150); var attached = false; if (GetForegroundWindow() != handle && GetAncestor(GetForegroundWindow(), 3) != handle) { var foregroundThread = GetWindowThreadProcessId(GetForegroundWindow(), out _); var inputThread = GetCurrentThreadId(); // The shell may retain foreground ownership when launching a hidden // test process. Attach only for this initial, explicit activation; // never steal focus back while a mouse gesture is in progress. attached = foregroundThread != inputThread && AttachThreadInput(inputThread, foregroundThread, true); try { BringWindowToTop(handle); activated = SetForegroundWindow(handle); } finally { if (attached) AttachThreadInput(inputThread, foregroundThread, false); } await Task.Delay(150); } string? automationError = null; var usedAutomation = false; if (GetForegroundWindow() != handle && GetAncestor(GetForegroundWindow(), 3) != handle) { usedAutomation = true; try { // UI Automation calls into our WinUI provider. Run off the UI // thread so the provider can respond while we await activation. await Task.Run(() => { using var automation = new UIA3Automation(); automation.FromHandle(handle).Focus(); }).WaitAsync(TimeSpan.FromSeconds(5)); } catch (Exception ex) { automationError = ex.Message; } await Task.Delay(150); } await AtomicJson.WriteAsync(Path.Combine(output, "native-input-focus.json"), new { window = handle.ToInt64(), before = before.ToInt64(), after = GetForegroundWindow().ToInt64(), activated, attached, usedAutomation, automationError }); // Activation is attempted only at the start of a check. Every input // packet still verifies focus and stops if the user switches windows. EnsureForeground(); } internal static object NativeInputState(FrameworkElement element) { var expected = ElementCenter(element); GetCursorPos(out var cursor); var underCursor = WindowFromPoint(cursor); GetWindowThreadProcessId(underCursor, out var process); var handle = WinRT.Interop.WindowNative.GetWindowHandle(App.MainWindow); var className = new System.Text.StringBuilder(256); GetClassName(underCursor, className, className.Capacity); var root = (FrameworkElement)App.MainWindow.Content; var point = element.TransformToVisual(root).TransformPoint(new(element.ActualWidth / 2, element.ActualHeight / 2)); var hits = Microsoft.UI.Xaml.Media.VisualTreeHelper.FindElementsInHostCoordinates(point, root).Take(15).Select(x => new { type = x.GetType().Name, name = (x as FrameworkElement)?.Name }).ToArray(); return new { expected = new { expected.X, expected.Y, expected.Scale }, actual = new { cursor.X, cursor.Y }, window = handle.ToInt64(), underCursor = underCursor.ToInt64(), process, capture = GetCapture().ToInt64(), ownProcess = Environment.ProcessId, className = className.ToString(), ancestor = GetAncestor(underCursor, 2).ToInt64(), hit = SendMessage(handle, 0x0084, 0, (nint)((cursor.Y << 16) | (cursor.X & 0xffff))).ToInt64(), hits, visible = IsWindowVisible(handle), enabled = IsWindowEnabled(handle), leftDown = (GetAsyncKeyState(1) & 0x8000) != 0 }; } [DllImport("user32.dll")] private static extern uint GetWindowThreadProcessId(nint window, out uint process); [DllImport("kernel32.dll")] private static extern uint GetCurrentThreadId(); [DllImport("user32.dll")] private static extern bool AttachThreadInput(uint from, uint to, bool attach); [DllImport("user32.dll")] private static extern bool BringWindowToTop(nint window); [DllImport("user32.dll")] private static extern bool GetCursorPos(out ScreenPoint point); [DllImport("user32.dll")] private static extern nint WindowFromPoint(ScreenPoint point); [DllImport("user32.dll")] private static extern nint GetCapture(); [DllImport("user32.dll")] private static extern bool IsWindowVisible(nint window); [DllImport("user32.dll")] private static extern bool IsWindowEnabled(nint window); [DllImport("user32.dll")] private static extern short GetAsyncKeyState(int key); [DllImport("user32.dll", CharSet = CharSet.Unicode)] private static extern int GetClassName(nint window, System.Text.StringBuilder name, int size); [DllImport("user32.dll")] private static extern nint SendMessage(nint window, uint message, nint wparam, nint lparam); }