@copylee/dsh-computer-use 0.14.2 → 0.16.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/helper/Input.cs CHANGED
@@ -230,19 +230,17 @@ namespace DshComputerUse
230
230
  int x1 = Args.Int(r, "x1", 0), y1 = Args.Int(r, "y1", 0), x2 = Args.Int(r, "x2", 0), y2 = Args.Int(r, "y2", 0);
231
231
  uint down, up; Flags(Args.Str(r, "button", "left"), out down, out up);
232
232
  Overlay.Dodge(x1, y1); Overlay.Dodge(x2, y2);
233
- Move(x1, y1);
234
233
  if (Overlay.ShowClickTarget) Overlay.MarkPointer(x1, y1, "拖动", 1200);
234
+ Move(x1, y1);
235
235
  Thread.Sleep(40);
236
236
  Send(new List<Native.INPUT> { Mouse(down, 0) });
237
237
  try
238
238
  {
239
239
  Thread.Sleep(60);
240
- int steps = Math.Max(8, Math.Min(40, (int)(Math.Sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1)) / 20)));
241
- for (int i = 1; i <= steps; i++)
242
- {
243
- Move(x1 + (x2 - x1) * i / steps, y1 + (y2 - y1) * i / steps);
244
- Thread.Sleep(12);
245
- }
240
+ double dist = Math.Sqrt((double)(x2 - x1) * (x2 - x1) + (double)(y2 - y1) * (y2 - y1));
241
+ // Frame-paced, eased path; every step is a real move so the app sees the drag.
242
+ Overlay.AnimatePointer(x1, y1, x2, y2, Math.Max(240.0, Overlay.GlideDurationMs(dist) * 1.4), delegate(int ix, int iy) { Move(ix, iy); });
243
+ Move(x2, y2);
246
244
  Thread.Sleep(60);
247
245
  }
248
246
  finally { Send(new List<Native.INPUT> { Mouse(up, 0) }); }
@@ -254,9 +252,18 @@ namespace DshComputerUse
254
252
  {
255
253
  int sx = 0, sy = 0;
256
254
  bool hasPos = Args.Has(r, "x");
257
- if (hasPos) { sx = Args.Int(r, "x", 0); sy = Args.Int(r, "y", 0); Overlay.Dodge(sx, sy); Move(sx, sy); Thread.Sleep(30); }
258
- else { Native.POINT p; if (Native.GetCursorPos(out p)) { sx = p.X; sy = p.Y; } }
259
- if (Overlay.ShowClickTarget) Overlay.MarkPointer(sx, sy, "滚动", 900);
255
+ if (hasPos)
256
+ {
257
+ sx = Args.Int(r, "x", 0); sy = Args.Int(r, "y", 0); Overlay.Dodge(sx, sy);
258
+ // Glide first (the badge travels with the pointer), then the real move apps react to.
259
+ if (Overlay.ShowClickTarget) Overlay.MarkPointer(sx, sy, "滚动", 900);
260
+ Move(sx, sy); Thread.Sleep(30);
261
+ }
262
+ else
263
+ {
264
+ Native.POINT p; if (Native.GetCursorPos(out p)) { sx = p.X; sy = p.Y; }
265
+ if (Overlay.ShowClickTarget) Overlay.MarkPointer(sx, sy, "滚动", 900);
266
+ }
260
267
  int dx = Args.Int(r, "dx", 0), dy = Args.Int(r, "dy", 0);
261
268
  var mods = Args.Ints(r, "modifiers");
262
269
  Modifiers(mods, false);
package/helper/Native.cs CHANGED
@@ -50,7 +50,15 @@ namespace DshComputerUse
50
50
  [DllImport("user32.dll", SetLastError = true)] public static extern bool DestroyIcon(IntPtr hIcon);
51
51
  [DllImport("user32.dll", SetLastError = true)] public static extern bool SetSystemCursor(IntPtr hcur, uint id);
52
52
  [DllImport("user32.dll", SetLastError = true)] public static extern bool SystemParametersInfo(uint uiAction, uint uiParam, IntPtr pvParam, uint fWinIni);
53
- public const uint SPI_SETCURSORS = 0x0057;
53
+ [DllImport("user32.dll", SetLastError = true)] public static extern bool SystemParametersInfo(uint uiAction, uint uiParam, out bool pvParam, uint fWinIni);
54
+ [DllImport("winmm.dll")] public static extern uint timeBeginPeriod(uint uPeriod);
55
+ [DllImport("winmm.dll")] public static extern uint timeEndPeriod(uint uPeriod);
56
+ /** Blocks until the next DWM composition (vblank-locked frame pacing). */
57
+ [DllImport("dwmapi.dll")] public static extern int DwmFlush();
58
+ [StructLayout(LayoutKind.Sequential)]
59
+ public struct BITMAPINFOHEADER { public uint biSize; public int biWidth; public int biHeight; public ushort biPlanes; public ushort biBitCount; public uint biCompression; public uint biSizeImage; public int biXPelsPerMeter; public int biYPelsPerMeter; public uint biClrUsed; public uint biClrImportant; }
60
+ [DllImport("gdi32.dll")] public static extern IntPtr CreateDIBSection(IntPtr hdc, ref BITMAPINFOHEADER pbmi, uint iUsage, out IntPtr ppvBits, IntPtr hSection, uint dwOffset);
61
+ public const uint SPI_SETCURSORS = 0x0057, SPI_GETMOUSEVANISH = 0x1020, SPI_SETMOUSEVANISH = 0x1021;
54
62
  public const uint OCR_NORMAL = 32512, OCR_IBEAM = 32513, OCR_WAIT = 32514, OCR_HAND = 32649, OCR_APPSTARTING = 32650;
55
63
 
56
64
  public delegate bool MonitorEnumProc(IntPtr hMonitor, IntPtr hdc, ref RECT rect, IntPtr data);
package/helper/Overlay.cs CHANGED
@@ -389,11 +389,14 @@ namespace DshComputerUse
389
389
  public static volatile bool SkipWaitRequested;
390
390
  static volatile bool clickTargetEnabled = true;
391
391
  static volatile bool customCursorActive;
392
+ static volatile bool disabledMouseVanish;
392
393
  static string cursorBadge = "DSH";
393
394
  static LayeredForm markForm;
394
395
  static DateTime markExpiresAt = DateTime.MinValue;
395
396
  static bool markActive;
396
397
  static bool markIsResting;
398
+ static LayeredForm clickRippleForm;
399
+ static DateTime clickRippleExpiresAt = DateTime.MinValue;
397
400
  static float cachedMarkScale;
398
401
  static bool cachedMarkFlipX, cachedMarkFlipY, cachedMarkRipple, cachedMarkArrow;
399
402
  static string cachedMarkText = "";
@@ -402,7 +405,6 @@ namespace DshComputerUse
402
405
  static bool hasCachedMarkBitmap;
403
406
  static volatile int latestMouseX, latestMouseY;
404
407
  static int mouseMovePending;
405
- static int lastMouseMoveTick = int.MinValue / 2;
406
408
  static int lastTyping = int.MinValue / 2;
407
409
  static int lastEmit;
408
410
  static readonly object RectLock = new object();
@@ -656,14 +658,39 @@ namespace DshComputerUse
656
658
  g.DrawPolygon(outline, arrow);
657
659
  }
658
660
  }
659
- // Build a clean 32-bit ARGB cursor with an all-zero monochrome AND mask
661
+ // Build a clean 32-bit premultiplied ARGB DIB cursor + all-zero monochrome AND mask
660
662
  // so Windows DWM composites it purely on the hardware cursor plane without flicker.
661
- IntPtr hbmColor = bmp.GetHbitmap(Color.FromArgb(0));
662
- IntPtr hbmMask = Native.CreateBitmap(size, size, 1, 1, IntPtr.Zero);
663
+ IntPtr hbmColor = IntPtr.Zero;
664
+ IntPtr hbmMask = IntPtr.Zero;
663
665
  try
664
666
  {
665
- if (hbmColor != IntPtr.Zero && hbmMask != IntPtr.Zero)
667
+ var bmi = new Native.BITMAPINFOHEADER();
668
+ bmi.biSize = (uint)Marshal.SizeOf(typeof(Native.BITMAPINFOHEADER));
669
+ bmi.biWidth = size;
670
+ bmi.biHeight = -size; // top-down DIB
671
+ bmi.biPlanes = 1;
672
+ bmi.biBitCount = 32;
673
+ bmi.biCompression = 0; // BI_RGB
674
+ IntPtr bits = IntPtr.Zero;
675
+ IntPtr screenDc = Native.GetDC(IntPtr.Zero);
676
+ try { hbmColor = Native.CreateDIBSection(screenDc, ref bmi, 0, out bits, IntPtr.Zero, 0); }
677
+ finally { Native.ReleaseDC(IntPtr.Zero, screenDc); }
678
+ hbmMask = Native.CreateBitmap(size, size, 1, 1, IntPtr.Zero);
679
+ if (hbmColor != IntPtr.Zero && hbmMask != IntPtr.Zero && bits != IntPtr.Zero)
666
680
  {
681
+ var rect = new Rectangle(0, 0, size, size);
682
+ BitmapData data = bmp.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format32bppPArgb);
683
+ try
684
+ {
685
+ int totalBytes = size * size * 4;
686
+ byte[] buf = new byte[totalBytes];
687
+ for (int row = 0; row < size; row++)
688
+ {
689
+ Marshal.Copy(new IntPtr(data.Scan0.ToInt64() + (long)row * data.Stride), buf, row * size * 4, size * 4);
690
+ }
691
+ Marshal.Copy(buf, 0, bits, totalBytes);
692
+ }
693
+ finally { bmp.UnlockBits(data); }
667
694
  var ii = new Native.ICONINFO();
668
695
  ii.fIcon = false;
669
696
  ii.xHotspot = tipX;
@@ -680,6 +707,17 @@ namespace DshComputerUse
680
707
  }
681
708
  }
682
709
  if (hCursor == IntPtr.Zero) return;
710
+ // Disable Windows' "Hide pointer while typing" (SPI_SETMOUSEVANISH) temporarily so
711
+ // synthetic keyboard typing never makes our hardware cursor vanish for 1-2 seconds.
712
+ if (!disabledMouseVanish)
713
+ {
714
+ bool vanishOn = false;
715
+ if (Native.SystemParametersInfo(Native.SPI_GETMOUSEVANISH, 0, out vanishOn, 0) && vanishOn)
716
+ {
717
+ if (Native.SystemParametersInfo(Native.SPI_SETMOUSEVANISH, 0, IntPtr.Zero, 0))
718
+ disabledMouseVanish = true;
719
+ }
720
+ }
683
721
  uint[] ids = new uint[] { Native.OCR_NORMAL, Native.OCR_IBEAM, Native.OCR_HAND, Native.OCR_APPSTARTING, Native.OCR_WAIT };
684
722
  bool anyApplied = false;
685
723
  for (int i = 0; i < ids.Length; i++)
@@ -698,9 +736,14 @@ namespace DshComputerUse
698
736
  }
699
737
  }
700
738
 
701
- /** Reload the user's native Windows cursor scheme (idempotent & crash-safe). */
739
+ /** Reload the user's native Windows cursor scheme and mouse-vanish setting (idempotent & crash-safe). */
702
740
  public static void RestoreSystemCursor(bool force)
703
741
  {
742
+ if (disabledMouseVanish)
743
+ {
744
+ try { Native.SystemParametersInfo(Native.SPI_SETMOUSEVANISH, 0, new IntPtr(1), 0); } catch (Exception) { }
745
+ disabledMouseVanish = false;
746
+ }
704
747
  if (!force && !customCursorActive) return;
705
748
  try { Native.SystemParametersInfo(Native.SPI_SETCURSORS, 0, IntPtr.Zero, 0); } catch (Exception) { }
706
749
  customCursorActive = false;
@@ -1068,7 +1111,12 @@ namespace DshComputerUse
1068
1111
  PlaceMarkOnUi(latestMouseX, latestMouseY, cursorBadge, Accent, 0, false, false, true);
1069
1112
  }
1070
1113
 
1071
- /** Smooth EaseOutCubic glide (~150ms @ ~60-90Hz) moving both OS cursor and cached badge in lockstep. */
1114
+ /**
1115
+ * Glide the OS cursor and the DSH badge to (x, y) together. Frames are
1116
+ * locked to the display's refresh (DwmFlush), positions follow a
1117
+ * minimum-jerk curve sampled from a stopwatch, and the duration grows
1118
+ * with the distance, so the pointer moves like a hand, not in steps.
1119
+ */
1072
1120
  public static void GlideMark(int x, int y, string text, Color color, int holdMs, bool capturable, bool ripple)
1073
1121
  {
1074
1122
  int startX, startY;
@@ -1076,23 +1124,68 @@ namespace DshComputerUse
1076
1124
  if (Native.GetCursorPos(out cur)) { startX = cur.X; startY = cur.Y; }
1077
1125
  else if (hasLastMark) { startX = lastMarkX; startY = lastMarkY; }
1078
1126
  else { startX = x; startY = y; }
1079
- double dist = Math.Sqrt((double)(x - startX) * (x - startX) + (double)(y - startY) * (y - startY));
1080
- if (dist >= 30.0)
1127
+ AnimatePointer(startX, startY, x, y, 0, delegate(int ix, int iy)
1128
+ {
1129
+ try { Native.SetCursorPos(ix, iy); } catch (Exception) { }
1130
+ Mark(ix, iy, text, color, holdMs, false, false);
1131
+ });
1132
+ try { Native.SetCursorPos(x, y); } catch (Exception) { }
1133
+ Mark(x, y, text, color, holdMs, false, false);
1134
+ }
1135
+
1136
+ /** Glide duration for a move of `dist` pixels: short hops stay quick, long sweeps stay readable. */
1137
+ public static double GlideDurationMs(double dist)
1138
+ {
1139
+ return Math.Max(150.0, Math.Min(420.0, 110.0 + Math.Sqrt(dist) * 9.5));
1140
+ }
1141
+
1142
+ /** Minimum-jerk position profile (10t^3 - 15t^4 + 6t^5): zero velocity and acceleration at both ends. */
1143
+ public static double MinimumJerk(double t)
1144
+ {
1145
+ if (t <= 0) return 0;
1146
+ if (t >= 1) return 1;
1147
+ return t * t * t * (10.0 + t * (-15.0 + 6.0 * t));
1148
+ }
1149
+
1150
+ /**
1151
+ * Drive `step` once per displayed frame from (x0, y0) to (x1, y1), stopping
1152
+ * just before the end (callers place the final point themselves).
1153
+ * durationMs <= 0 picks GlideDurationMs. Moves under 10 px are not animated.
1154
+ */
1155
+ public static void AnimatePointer(int x0, int y0, int x1, int y1, double durationMs, Action<int, int> step)
1156
+ {
1157
+ double dist = Math.Sqrt((double)(x1 - x0) * (x1 - x0) + (double)(y1 - y0) * (y1 - y0));
1158
+ if (dist < 10.0) return;
1159
+ if (durationMs <= 0) durationMs = GlideDurationMs(dist);
1160
+ bool highRes = false;
1161
+ bool vsync = true;
1162
+ int lastX = x0, lastY = y0;
1163
+ try
1081
1164
  {
1082
- int steps = Math.Max(8, Math.Min(16, (int)(dist / 65.0) + 8));
1083
- for (int i = 1; i < steps; i++)
1165
+ highRes = Native.timeBeginPeriod(1) == 0;
1166
+ var sw = Stopwatch.StartNew();
1167
+ while (true)
1084
1168
  {
1085
- double t = (double)i / steps;
1086
- double ease = 1.0 - Math.Pow(1.0 - t, 3.0); // EaseOutCubic
1087
- int ix = startX + (int)Math.Round((x - startX) * ease);
1088
- int iy = startY + (int)Math.Round((y - startY) * ease);
1089
- try { Native.SetCursorPos(ix, iy); } catch (Exception) { }
1090
- Mark(ix, iy, text, color, holdMs, capturable, false);
1091
- Thread.Sleep(10);
1169
+ // Wait for the next composed frame first, then place the pointer for that frame's time.
1170
+ if (vsync)
1171
+ {
1172
+ try { vsync = Native.DwmFlush() == 0; } catch (Exception) { vsync = false; }
1173
+ }
1174
+ if (!vsync) Thread.Sleep(4);
1175
+ double elapsed = sw.Elapsed.TotalMilliseconds;
1176
+ if (elapsed >= durationMs) break;
1177
+ double e = MinimumJerk(elapsed / durationMs);
1178
+ int ix = x0 + (int)Math.Round((x1 - x0) * e);
1179
+ int iy = y0 + (int)Math.Round((y1 - y0) * e);
1180
+ if (ix == lastX && iy == lastY) continue;
1181
+ lastX = ix; lastY = iy;
1182
+ step(ix, iy);
1092
1183
  }
1093
1184
  }
1094
- try { Native.SetCursorPos(x, y); } catch (Exception) { }
1095
- Mark(x, y, text, color, holdMs, capturable, ripple);
1185
+ finally
1186
+ {
1187
+ if (highRes) { try { Native.timeEndPeriod(1); } catch (Exception) { } }
1188
+ }
1096
1189
  }
1097
1190
 
1098
1191
  static void ShowRestingMarkOnUi(int x, int y)
@@ -1106,6 +1199,42 @@ namespace DshComputerUse
1106
1199
  Ui(delegate { PlaceMarkOnUi(x, y, text, color, holdMs, capturable, ripple, false); });
1107
1200
  }
1108
1201
 
1202
+ /** Render post-click ripple on a separate capturable form so the live DSH badge never toggles affinity or blinks. */
1203
+ static void ShowClickRippleOnUi(int x, int y, string text, Color color, int holdMs)
1204
+ {
1205
+ if (clickRippleForm == null)
1206
+ {
1207
+ clickRippleForm = new LayeredForm(true);
1208
+ clickRippleForm.SetCapturable(true);
1209
+ clickRippleForm.Show();
1210
+ }
1211
+ float scale; Rectangle mon;
1212
+ MonitorAt(x, y, out scale, out mon);
1213
+ float s = Math.Max(0.75f, Math.Min(3f, scale));
1214
+ string labelText = text ?? "";
1215
+ int estW = (int)((Math.Max(6, labelText.Length) * 11 + 36) * s);
1216
+ bool flipX = !mon.IsEmpty && x + estW > mon.Right;
1217
+ bool flipY = !mon.IsEmpty && y + (int)(52 * s) > mon.Bottom;
1218
+ int cx, cy;
1219
+ using (var bmp = DrawMark(labelText, color, scale, flipX, flipY, true, false, out cx, out cy))
1220
+ {
1221
+ clickRippleForm.SetBitmap(bmp, new Rectangle(x - cx, y - cy, bmp.Width, bmp.Height), 255);
1222
+ }
1223
+ clickRippleForm.KeepOnTop();
1224
+ clickRippleExpiresAt = DateTime.UtcNow.AddMilliseconds(holdMs);
1225
+ if (!timer.Enabled) timer.Start();
1226
+ }
1227
+
1228
+ static void HideClickRipple()
1229
+ {
1230
+ clickRippleExpiresAt = DateTime.MinValue;
1231
+ if (clickRippleForm != null)
1232
+ {
1233
+ clickRippleForm.Close();
1234
+ clickRippleForm = null;
1235
+ }
1236
+ }
1237
+
1109
1238
  public static void MarkResult(int x, int y, string button, int count, string coordLabel)
1110
1239
  {
1111
1240
  Color color;
@@ -1115,11 +1244,23 @@ namespace DshComputerUse
1115
1244
  else if (button == "right") { color = Color.FromArgb(16, 185, 129); verb = "已右键"; }
1116
1245
  else if (button == "middle") { color = Color.FromArgb(6, 182, 212); verb = "已中键"; }
1117
1246
  else { color = Color.FromArgb(59, 130, 246); verb = "已单击"; }
1118
- Mark(x, y, verb + " " + coordLabel, color, 900, true, true);
1247
+ Ensure();
1248
+ Ui(delegate
1249
+ {
1250
+ ShowClickRippleOnUi(x, y, verb + " " + coordLabel, color, 750);
1251
+ // Keep the live DSH badge immediately tracking the pointer without freezing at the click spot.
1252
+ if (visible && clickTargetEnabled && !petFinished && !paused)
1253
+ {
1254
+ Native.POINT cp;
1255
+ if (Native.GetCursorPos(out cp)) ShowRestingMarkOnUi(cp.X, cp.Y);
1256
+ else ShowRestingMarkOnUi(x, y);
1257
+ }
1258
+ });
1119
1259
  }
1120
1260
 
1121
1261
  static void HideMark()
1122
1262
  {
1263
+ HideClickRipple();
1123
1264
  markActive = false;
1124
1265
  markIsResting = false;
1125
1266
  hasCachedMarkBitmap = false;
@@ -1221,6 +1362,10 @@ namespace DshComputerUse
1221
1362
 
1222
1363
  static void Tick()
1223
1364
  {
1365
+ if (clickRippleForm != null && DateTime.UtcNow >= clickRippleExpiresAt)
1366
+ {
1367
+ HideClickRipple();
1368
+ }
1224
1369
  if (markActive && DateTime.UtcNow >= markExpiresAt)
1225
1370
  {
1226
1371
  if (visible && clickTargetEnabled && !petFinished && !paused)
@@ -1234,7 +1379,7 @@ namespace DshComputerUse
1234
1379
  }
1235
1380
  if (!visible)
1236
1381
  {
1237
- if (!markActive) timer.Stop();
1382
+ if (!markActive && clickRippleForm == null) timer.Stop();
1238
1383
  return;
1239
1384
  }
1240
1385
  if (visible && clickTargetEnabled && !petFinished && !paused && !markActive)
@@ -1886,8 +2031,8 @@ namespace DshComputerUse
1886
2031
  if (nCode >= 0 && visible)
1887
2032
  {
1888
2033
  int msg = wParam.ToInt32();
1889
- // Hardware-rate cursor tracking on WM_MOUSEMOVE (throttled to ~60Hz so 1000Hz mice
1890
- // never flood the UI thread): never triggers takeover, only moves the cached badge.
2034
+ // Native-rate cursor tracking on WM_MOUSEMOVE: Interlocked.Exchange coalesces bursts
2035
+ // directly to the UI message loop rate (60Hz/120Hz/144Hz+) with zero 15.6ms TickCount lag.
1891
2036
  if (msg == 0x0200)
1892
2037
  {
1893
2038
  if (clickTargetEnabled && !petFinished && !paused && markActive && markIsResting && invoker != null)
@@ -1895,10 +2040,8 @@ namespace DshComputerUse
1895
2040
  var mm = (Native.MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(Native.MSLLHOOKSTRUCT));
1896
2041
  latestMouseX = mm.pt.X;
1897
2042
  latestMouseY = mm.pt.Y;
1898
- int tick = Environment.TickCount;
1899
- if (unchecked(tick - lastMouseMoveTick) >= 16 && Interlocked.Exchange(ref mouseMovePending, 1) == 0)
2043
+ if (Interlocked.Exchange(ref mouseMovePending, 1) == 0)
1900
2044
  {
1901
- lastMouseMoveTick = tick;
1902
2045
  try { invoker.BeginInvoke((MethodInvoker)SyncRestingCursorOnUi); }
1903
2046
  catch (Exception) { Interlocked.Exchange(ref mouseMovePending, 0); }
1904
2047
  }
package/helper/Program.cs CHANGED
@@ -179,8 +179,8 @@ namespace DshComputerUse
179
179
  {
180
180
  int mx = Args.Int(r, "x", 0), my = Args.Int(r, "y", 0);
181
181
  Overlay.Dodge(mx, my);
182
- Input.Move(mx, my);
183
182
  if (Overlay.ShowClickTarget) Overlay.MarkPointer(mx, my, "DSH", 900);
183
+ Input.Move(mx, my);
184
184
  }
185
185
  return null;
186
186
  case "click":
package/helper/Uia.cs CHANGED
@@ -158,11 +158,22 @@ namespace DshComputerUse
158
158
  else if (action == "set_checked")
159
159
  {
160
160
  bool want = Args.Bool(r, "checked", true);
161
+ ToggleState desired = want ? ToggleState.On : ToggleState.Off;
161
162
  var tp = (TogglePattern)pattern;
162
- bool isOn = tp.Current.ToggleState == ToggleState.On;
163
- if (isOn != want) tp.Toggle();
164
- result["changed"] = isOn != want;
165
- result["checked"] = want;
163
+ ToggleState initial = tp.Current.ToggleState;
164
+ bool changed = false;
165
+ // Handle tri-state checkboxes (Indeterminate -> On -> Off) with up to 2 toggles.
166
+ for (int attempt = 0; attempt < 2 && tp.Current.ToggleState != desired; attempt++)
167
+ {
168
+ tp.Toggle();
169
+ changed = true;
170
+ Thread.Sleep(20);
171
+ }
172
+ ToggleState actual = tp.Current.ToggleState;
173
+ result["changed"] = changed || actual != initial;
174
+ result["checked"] = actual == ToggleState.On;
175
+ result["toggleState"] = actual.ToString();
176
+ result["verified"] = actual == desired;
166
177
  }
167
178
  else if (action == "expand" || action == "collapse")
168
179
  {
@@ -174,21 +185,41 @@ namespace DshComputerUse
174
185
  {
175
186
  if (wantExpand) ec.Expand();
176
187
  else ec.Collapse();
188
+ Thread.Sleep(25);
177
189
  }
190
+ var actual = ec.Current.ExpandCollapseState;
191
+ bool verified = wantExpand
192
+ ? (actual == ExpandCollapseState.Expanded || actual == ExpandCollapseState.PartiallyExpanded)
193
+ : (actual == ExpandCollapseState.Collapsed);
178
194
  result["changed"] = !already;
195
+ result["state"] = actual.ToString();
196
+ result["verified"] = verified;
179
197
  }
180
198
  else if (action == "select")
181
199
  {
182
200
  var sp = (SelectionItemPattern)pattern;
183
201
  bool already = sp.Current.IsSelected;
184
- if (!already) sp.Select();
202
+ if (!already)
203
+ {
204
+ sp.Select();
205
+ Thread.Sleep(20);
206
+ }
207
+ bool actual = sp.Current.IsSelected;
185
208
  result["changed"] = !already;
209
+ result["selected"] = actual;
210
+ result["verified"] = actual;
186
211
  }
187
212
  else if (action == "set_value")
188
213
  {
189
214
  string val = Args.Str(r, "value", "");
190
- ((ValuePattern)pattern).SetValue(val);
215
+ var vp = (ValuePattern)pattern;
216
+ vp.SetValue(val);
217
+ Thread.Sleep(20);
218
+ string actual = "";
219
+ try { actual = vp.Current.Value ?? ""; } catch (Exception) { actual = ""; }
191
220
  result["changed"] = true;
221
+ result["verified"] = actual == val;
222
+ result["actualLength"] = actual.Length;
192
223
  }
193
224
  result["performed"] = true;
194
225
  return result;
@@ -229,6 +260,7 @@ namespace DshComputerUse
229
260
  string query = Args.Str(r, "query", "").Trim();
230
261
  int maxMatches = Math.Max(1, Math.Min(120, Args.Int(r, "maxMatches", 24)));
231
262
  List<object> result = null;
263
+ bool truncated = false;
232
264
  Exception error = null;
233
265
 
234
266
  var worker = new Thread(delegate ()
@@ -268,7 +300,6 @@ namespace DshComputerUse
268
300
  string qLower = query.ToLowerInvariant();
269
301
  foreach (AutomationElement el in found)
270
302
  {
271
- if (list.Count >= maxMatches) break;
272
303
  var rect = (System.Windows.Rect)el.GetCachedPropertyValue(AutomationElement.BoundingRectangleProperty);
273
304
  if (rect.IsEmpty || rect.Width < 2 || rect.Height < 2) continue;
274
305
  var type = (ControlType)el.GetCachedPropertyValue(AutomationElement.ControlTypeProperty);
@@ -278,6 +309,7 @@ namespace DshComputerUse
278
309
  && elName.ToLowerInvariant().IndexOf(qLower, StringComparison.Ordinal) < 0
279
310
  && elAid.ToLowerInvariant().IndexOf(qLower, StringComparison.Ordinal) < 0)
280
311
  continue;
312
+ if (list.Count >= maxMatches) { truncated = true; break; }
281
313
 
282
314
  bool isPwd = false;
283
315
  try
@@ -321,6 +353,7 @@ namespace DshComputerUse
321
353
  if (error != null) throw error;
322
354
  var outMap = Windows.Describe(hwnd);
323
355
  outMap["elements"] = result ?? new List<object>();
356
+ outMap["truncated"] = truncated;
324
357
  return outMap;
325
358
  }
326
359