Skip to content

Commit a29ac0e

Browse files
committed
chore(spike): win the foreground on the Arm64 image and wait for the IME to settle [ime]
1 parent 1e0a050 commit a29ac0e

1 file changed

Lines changed: 60 additions & 9 deletions

File tree

‎spikes/windows-editor/app/Gate.cs‎

Lines changed: 60 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ namespace EditorGate;
1616
internal static class Gate
1717
{
1818
private static Report? s_report;
19+
private static TextBlock? s_status;
1920
private static string s_out = @"C:\gate\out";
2021

2122
public static void Fatal(Exception e) => s_report?.Line("fatal", e.ToString());
@@ -46,7 +47,8 @@ private static async Task RunCoreAsync(Window window, Report report, bool softwa
4647
report.Line("processors", Environment.ProcessorCount);
4748
var editor = new SqlEditor { PreferSoftwareRendering = software, Lexer = new CoreSqlLexer(SqlGrammar.MySql) };
4849
Microsoft.UI.Xaml.Automation.AutomationProperties.SetName(editor, "SQL editor");
49-
var status = new TextBlock { Margin = new Thickness(8, 4, 8, 4) };
50+
var status = new TextBlock { Margin = new Thickness(8, 4, 8, 4), Text = "starting" };
51+
s_status = status;
5052
var root = new Grid();
5153
root.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto });
5254
root.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) });
@@ -351,8 +353,7 @@ private static async Task ImeAsync(Window window, SqlEditor editor, Report repor
351353
var before = editor.Document.GetLineText(line);
352354
var undoBefore = editor.Document.Undo.UndoCount;
353355
await Task.Run(() => Keys.Type(" tieengs vieetj "));
354-
await Task.Delay(1500);
355-
var afterTelex = editor.Document.GetLineText(line);
356+
var afterTelex = await SettledLineAsync(editor, line);
356357
await Shots(editor, "ime-telex");
357358
var telexSteps = editor.Document.Undo.UndoCount - undoBefore;
358359
Keys.Chord(Keys.Control, (ushort)'Z');
@@ -386,7 +387,8 @@ private static async Task ImeAsync(Window window, SqlEditor editor, Report repor
386387
await Task.Delay(500);
387388
var windowsBefore = Windows32.Visible().Select(w => w.Hwnd).ToHashSet();
388389
await Task.Run(() => Keys.Type("nihao", 80));
389-
await Task.Delay(2000);
390+
await SettledLineAsync(editor, line);
391+
await Task.Delay(800);
390392
var composing = editor.CompositionText;
391393
var caret = CaretScreen(editor);
392394
var visible = Windows32.Visible();
@@ -404,8 +406,7 @@ private static async Task ImeAsync(Window window, SqlEditor editor, Report repor
404406
report.Line("uia_while_composing_answered", await UiaDriver.RequestAsync(s_out, "uia-composing", 120));
405407
// "1" picks the first candidate: on the Arm64 image a first-run Bing panel in the candidate window takes Space.
406408
Keys.Press((ushort)'1');
407-
await Task.Delay(1500);
408-
var afterPinyin = editor.Document.GetLineText(line);
409+
var afterPinyin = await SettledLineAsync(editor, line);
409410
await Shots(editor, "ime-pinyin-committed");
410411
report.Line("pinyin", new JsonObject
411412
{
@@ -523,18 +524,68 @@ private static int MiddleOffset(TablePro.Editor.Model.TextDocument document)
523524
return document.GetLineStart(line) + Math.Min(20, document.GetLineLength(line));
524525
}
525526

527+
// From the IME lane's harness: the Arm64 image leaves SearchHost in front, and Windows refuses SetForegroundWindow
528+
// unless the caller owns the last input. Try the call, then sharing input with the foreground thread, then a click
529+
// on the window's own status line.
526530
private static async Task EnsureForegroundAsync(Window window, Report report)
527531
{
528532
var hwnd = WinRT.Interop.WindowNative.GetWindowHandle(window);
529-
for (var i = 0; i < 40 && Windows32.GetForegroundWindow() != hwnd; i++)
533+
var how = "already";
534+
for (var i = 0; i < 12 && Windows32.GetForegroundWindow() != hwnd; i++)
530535
{
531-
// SetForegroundWindow is refused unless this process sent the last input; a held Alt counts.
536+
how = "set_foreground";
532537
Keys.Down(Keys.Alt);
533538
Windows32.SetForegroundWindow(hwnd);
534539
Keys.Up(Keys.Alt);
535-
await Task.Delay(250);
540+
await Task.Delay(150);
541+
if (Windows32.GetForegroundWindow() == hwnd)
542+
{
543+
break;
544+
}
545+
how = "attach_thread_input";
546+
var foregroundThread = Windows32.GetWindowThreadProcessId(Windows32.GetForegroundWindow(), out _);
547+
var self = Windows32.GetCurrentThreadId();
548+
Windows32.AttachThreadInput(self, foregroundThread, true);
549+
Windows32.BringWindowToTop(hwnd);
550+
Windows32.SetForegroundWindow(hwnd);
551+
Windows32.AttachThreadInput(self, foregroundThread, false);
552+
await Task.Delay(150);
553+
if (Windows32.GetForegroundWindow() == hwnd)
554+
{
555+
continue;
556+
}
557+
how = "click";
558+
if (s_status is { } status)
559+
{
560+
var p = status.TransformToVisual(null).TransformPoint(new Windows.Foundation.Point(4, 4));
561+
var scale = status.XamlRoot.RasterizationScale;
562+
var origin = new Windows32.Point32();
563+
Windows32.ClientToScreen(hwnd, ref origin);
564+
Mouse.Click(origin.X + (int)(p.X * scale), origin.Y + (int)(p.Y * scale));
565+
}
566+
await Task.Delay(300);
536567
}
537568
report.Line("window_is_foreground", Windows32.GetForegroundWindow() == hwnd);
569+
report.Line("foreground_by", how);
570+
}
571+
572+
// The IME applies keys after SendInput returns, slower on Arm64: wait until the line stops changing.
573+
private static async Task<string> SettledLineAsync(SqlEditor editor, int line)
574+
{
575+
var last = editor.Document.GetLineText(line) + editor.CompositionText;
576+
var total = Stopwatch.StartNew();
577+
var stable = Stopwatch.StartNew();
578+
while (total.ElapsedMilliseconds < 8000 && stable.ElapsedMilliseconds < 800)
579+
{
580+
await Task.Delay(100);
581+
var now = editor.Document.GetLineText(line) + editor.CompositionText;
582+
if (now != last)
583+
{
584+
last = now;
585+
stable.Restart();
586+
}
587+
}
588+
return editor.Document.GetLineText(line);
538589
}
539590

540591
private static async Task WaitAsync(Func<bool> condition, int timeoutMs)

0 commit comments

Comments
 (0)