@copylee/dsh-computer-use 0.10.0 → 0.11.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/CuHelper.cs +457 -20
- package/lib/client.js +202 -7
- package/lib/index.js +711 -45
- package/lib/types/access.d.ts +2 -0
- package/lib/types/cdp.d.ts +55 -0
- package/lib/types/client/index.d.ts +4 -0
- package/lib/types/client/window-refs.d.ts +37 -0
- package/lib/types/computer.d.ts +22 -0
- package/lib/types/diagnostics.d.ts +5 -0
- package/lib/types/index.d.ts +9 -2
- package/lib/types/macros.d.ts +30 -0
- package/lib/types/routes.d.ts +8 -1
- package/lib/types/target.d.ts +4 -0
- package/lib/types/tools.d.ts +3 -0
- package/lib/types/update-check.d.ts +15 -0
- package/package.json +1 -1
package/helper/CuHelper.cs
CHANGED
|
@@ -28,6 +28,7 @@ using System.Threading;
|
|
|
28
28
|
using System.Web.Script.Serialization;
|
|
29
29
|
using System.Windows.Automation;
|
|
30
30
|
using System.Windows.Forms;
|
|
31
|
+
using Microsoft.Win32;
|
|
31
32
|
|
|
32
33
|
namespace DshComputerUse
|
|
33
34
|
{
|
|
@@ -122,6 +123,9 @@ namespace DshComputerUse
|
|
|
122
123
|
[DllImport("user32.dll")] public static extern bool SetWindowDisplayAffinity(IntPtr hwnd, uint affinity);
|
|
123
124
|
[DllImport("user32.dll")] public static extern bool ReleaseCapture();
|
|
124
125
|
[DllImport("user32.dll")] public static extern IntPtr SendMessage(IntPtr hwnd, uint msg, IntPtr w, IntPtr l);
|
|
126
|
+
[StructLayout(LayoutKind.Sequential)]
|
|
127
|
+
public struct GUITHREADINFO { public int cbSize; public int flags; public IntPtr hwndActive; public IntPtr hwndFocus; public IntPtr hwndCapture; public IntPtr hwndMenuOwner; public IntPtr hwndMoveSize; public IntPtr hwndCaret; public RECT rcCaret; }
|
|
128
|
+
[DllImport("user32.dll")] public static extern bool GetGUIThreadInfo(uint idThread, ref GUITHREADINFO lpgui);
|
|
125
129
|
|
|
126
130
|
public delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
|
|
127
131
|
[DllImport("user32.dll", SetLastError = true)] public static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc fn, IntPtr hMod, uint threadId);
|
|
@@ -329,6 +333,9 @@ namespace DshComputerUse
|
|
|
329
333
|
case "ui": return Uia.Elements(r);
|
|
330
334
|
case "text": return Uia.Text(r);
|
|
331
335
|
case "ocr": return Ocr.Read(r);
|
|
336
|
+
case "marks": return Uia.Marks(r);
|
|
337
|
+
case "password_focus": return Uia.PasswordFocus();
|
|
338
|
+
case "ui_point_click": return Uia.ClickAtPoint(r);
|
|
332
339
|
case "overlay_show": Overlay.ExcludeFromCapture = Args.Bool(r, "excludeFromCapture", true); Overlay.Show(r); return null;
|
|
333
340
|
case "overlay_pause": Overlay.SetPaused(Args.Bool(r, "paused", false)); return null;
|
|
334
341
|
case "window_card": CardLayer.SetOpacity(Args.Int(r, "opacity", 100)); return Windows.Card(new IntPtr(Convert.ToInt64(r["hwnd"])), Args.Int(r, "x", 0), Args.Int(r, "y", 0), Args.Int(r, "width", 0), Args.Int(r, "height", 0));
|
|
@@ -993,19 +1000,19 @@ namespace DshComputerUse
|
|
|
993
1000
|
public static object Click(Dictionary<string, object> r)
|
|
994
1001
|
{
|
|
995
1002
|
int x, y;
|
|
1003
|
+
Native.POINT savedCursor;
|
|
1004
|
+
bool hadSavedCursor = Native.GetCursorPos(out savedCursor);
|
|
1005
|
+
bool restorePointer = Args.Bool(r, "restorePointer", false);
|
|
996
1006
|
bool moved = Args.Has(r, "x");
|
|
997
1007
|
if (moved)
|
|
998
1008
|
{
|
|
999
1009
|
x = Args.Int(r, "x", 0);
|
|
1000
1010
|
y = Args.Int(r, "y", 0);
|
|
1001
|
-
Overlay.Dodge(x, y);
|
|
1002
|
-
Move(x, y);
|
|
1003
1011
|
}
|
|
1004
1012
|
else
|
|
1005
1013
|
{
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
else { x = pos.X; y = pos.Y; }
|
|
1014
|
+
if (!hadSavedCursor) { x = 0; y = 0; }
|
|
1015
|
+
else { x = savedCursor.X; y = savedCursor.Y; }
|
|
1009
1016
|
}
|
|
1010
1017
|
string button = Args.Str(r, "button", "left");
|
|
1011
1018
|
if (button != "right" && button != "middle") button = "left";
|
|
@@ -1019,8 +1026,16 @@ namespace DshComputerUse
|
|
|
1019
1026
|
if (showTarget)
|
|
1020
1027
|
{
|
|
1021
1028
|
string preVerb = count >= 3 ? "三击" : count == 2 ? "双击" : button == "right" ? "右键" : button == "middle" ? "中键" : "点击";
|
|
1022
|
-
Overlay.
|
|
1023
|
-
|
|
1029
|
+
Overlay.GlideMark(x, y, preVerb + " " + coordLabel, Color.FromArgb(217, 119, 87), 2000, false, false);
|
|
1030
|
+
}
|
|
1031
|
+
if (moved)
|
|
1032
|
+
{
|
|
1033
|
+
Overlay.Dodge(x, y);
|
|
1034
|
+
Move(x, y);
|
|
1035
|
+
}
|
|
1036
|
+
if (showTarget)
|
|
1037
|
+
{
|
|
1038
|
+
Thread.Sleep(70);
|
|
1024
1039
|
}
|
|
1025
1040
|
else if (moved)
|
|
1026
1041
|
{
|
|
@@ -1037,7 +1052,14 @@ namespace DshComputerUse
|
|
|
1037
1052
|
if (i < count - 1) Thread.Sleep(40);
|
|
1038
1053
|
}
|
|
1039
1054
|
}
|
|
1040
|
-
finally
|
|
1055
|
+
finally
|
|
1056
|
+
{
|
|
1057
|
+
Modifiers(mods, true);
|
|
1058
|
+
if (restorePointer && moved && hadSavedCursor)
|
|
1059
|
+
{
|
|
1060
|
+
try { Native.SetCursorPos(savedCursor.X, savedCursor.Y); } catch (Exception) { }
|
|
1061
|
+
}
|
|
1062
|
+
}
|
|
1041
1063
|
if (showTarget)
|
|
1042
1064
|
{
|
|
1043
1065
|
Overlay.MarkResult(x, y, button, count, coordLabel);
|
|
@@ -1049,6 +1071,7 @@ namespace DshComputerUse
|
|
|
1049
1071
|
result["button"] = button;
|
|
1050
1072
|
result["count"] = count;
|
|
1051
1073
|
result["repeat"] = repeat;
|
|
1074
|
+
result["restoredPointer"] = restorePointer && moved && hadSavedCursor;
|
|
1052
1075
|
return result;
|
|
1053
1076
|
}
|
|
1054
1077
|
|
|
@@ -2210,10 +2233,120 @@ namespace DshComputerUse
|
|
|
2210
2233
|
lock (Gate) { return Load(); }
|
|
2211
2234
|
}
|
|
2212
2235
|
|
|
2236
|
+
static void AddIfNew(List<KeyValuePair<string, string>> list, HashSet<string> seen, string name, string id)
|
|
2237
|
+
{
|
|
2238
|
+
if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(id)) return;
|
|
2239
|
+
string key = name.Trim().ToLowerInvariant() + "\u0000" + id.Trim().ToLowerInvariant();
|
|
2240
|
+
if (seen.Add(key)) list.Add(new KeyValuePair<string, string>(name.Trim(), id.Trim()));
|
|
2241
|
+
}
|
|
2242
|
+
|
|
2243
|
+
static string ExtractExePath(string raw)
|
|
2244
|
+
{
|
|
2245
|
+
if (string.IsNullOrEmpty(raw)) return "";
|
|
2246
|
+
string s = raw.Trim();
|
|
2247
|
+
if (s.StartsWith("\""))
|
|
2248
|
+
{
|
|
2249
|
+
int end = s.IndexOf('"', 1);
|
|
2250
|
+
if (end > 1) s = s.Substring(1, end - 1);
|
|
2251
|
+
}
|
|
2252
|
+
else
|
|
2253
|
+
{
|
|
2254
|
+
int comma = s.IndexOf(',');
|
|
2255
|
+
if (comma > 0) s = s.Substring(0, comma);
|
|
2256
|
+
}
|
|
2257
|
+
s = s.Trim();
|
|
2258
|
+
if (!s.EndsWith(".exe", StringComparison.OrdinalIgnoreCase)) return "";
|
|
2259
|
+
return File.Exists(s) ? s : "";
|
|
2260
|
+
}
|
|
2261
|
+
|
|
2262
|
+
static void ScanStartFolder(string dir, int depth, List<KeyValuePair<string, string>> list, HashSet<string> seen)
|
|
2263
|
+
{
|
|
2264
|
+
if (depth > 3 || string.IsNullOrEmpty(dir) || !Directory.Exists(dir)) return;
|
|
2265
|
+
try
|
|
2266
|
+
{
|
|
2267
|
+
foreach (var f in Directory.GetFiles(dir, "*.lnk"))
|
|
2268
|
+
{
|
|
2269
|
+
string name = Path.GetFileNameWithoutExtension(f);
|
|
2270
|
+
if (name.IndexOf("uninstall", StringComparison.OrdinalIgnoreCase) >= 0 || name.IndexOf("卸载", StringComparison.OrdinalIgnoreCase) >= 0) continue;
|
|
2271
|
+
AddIfNew(list, seen, name, "lnk:" + f);
|
|
2272
|
+
}
|
|
2273
|
+
foreach (var sub in Directory.GetDirectories(dir))
|
|
2274
|
+
ScanStartFolder(sub, depth + 1, list, seen);
|
|
2275
|
+
}
|
|
2276
|
+
catch (Exception) { }
|
|
2277
|
+
}
|
|
2278
|
+
|
|
2279
|
+
static void ScanRegistryApps(List<KeyValuePair<string, string>> list, HashSet<string> seen)
|
|
2280
|
+
{
|
|
2281
|
+
var roots = new RegistryKey[] { Registry.CurrentUser, Registry.LocalMachine };
|
|
2282
|
+
foreach (var root in roots)
|
|
2283
|
+
{
|
|
2284
|
+
// 1. App Paths (e.g. WeChat.exe, chrome.exe, code.exe)
|
|
2285
|
+
try
|
|
2286
|
+
{
|
|
2287
|
+
using (var ap = root.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths"))
|
|
2288
|
+
{
|
|
2289
|
+
if (ap != null)
|
|
2290
|
+
{
|
|
2291
|
+
foreach (var subName in ap.GetSubKeyNames())
|
|
2292
|
+
{
|
|
2293
|
+
try
|
|
2294
|
+
{
|
|
2295
|
+
using (var sub = ap.OpenSubKey(subName))
|
|
2296
|
+
{
|
|
2297
|
+
if (sub == null) continue;
|
|
2298
|
+
string exe = ExtractExePath(Convert.ToString(sub.GetValue(null)) ?? "");
|
|
2299
|
+
if (exe.Length > 0) AddIfNew(list, seen, Path.GetFileNameWithoutExtension(subName), "exe:" + exe);
|
|
2300
|
+
}
|
|
2301
|
+
}
|
|
2302
|
+
catch (Exception) { }
|
|
2303
|
+
}
|
|
2304
|
+
}
|
|
2305
|
+
}
|
|
2306
|
+
}
|
|
2307
|
+
catch (Exception) { }
|
|
2308
|
+
|
|
2309
|
+
// 2. Uninstall table DisplayName + DisplayIcon (.exe)
|
|
2310
|
+
var uninstallPaths = new string[] {
|
|
2311
|
+
@"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall",
|
|
2312
|
+
@"SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall",
|
|
2313
|
+
};
|
|
2314
|
+
foreach (var upath in uninstallPaths)
|
|
2315
|
+
{
|
|
2316
|
+
try
|
|
2317
|
+
{
|
|
2318
|
+
using (var uk = root.OpenSubKey(upath))
|
|
2319
|
+
{
|
|
2320
|
+
if (uk == null) continue;
|
|
2321
|
+
foreach (var subName in uk.GetSubKeyNames())
|
|
2322
|
+
{
|
|
2323
|
+
try
|
|
2324
|
+
{
|
|
2325
|
+
using (var sub = uk.OpenSubKey(subName))
|
|
2326
|
+
{
|
|
2327
|
+
if (sub == null) continue;
|
|
2328
|
+
string displayName = Convert.ToString(sub.GetValue("DisplayName")) ?? "";
|
|
2329
|
+
if (displayName.Length == 0) continue;
|
|
2330
|
+
string iconExe = ExtractExePath(Convert.ToString(sub.GetValue("DisplayIcon")) ?? "");
|
|
2331
|
+
if (iconExe.Length == 0) continue;
|
|
2332
|
+
if (iconExe.IndexOf("unins", StringComparison.OrdinalIgnoreCase) >= 0 || iconExe.IndexOf("setup", StringComparison.OrdinalIgnoreCase) >= 0) continue;
|
|
2333
|
+
AddIfNew(list, seen, displayName, "exe:" + iconExe);
|
|
2334
|
+
}
|
|
2335
|
+
}
|
|
2336
|
+
catch (Exception) { }
|
|
2337
|
+
}
|
|
2338
|
+
}
|
|
2339
|
+
}
|
|
2340
|
+
catch (Exception) { }
|
|
2341
|
+
}
|
|
2342
|
+
}
|
|
2343
|
+
}
|
|
2344
|
+
|
|
2213
2345
|
static List<KeyValuePair<string, string>> Load()
|
|
2214
2346
|
{
|
|
2215
2347
|
if (cache != null && (DateTime.UtcNow - cachedAt).TotalSeconds < 600) return cache;
|
|
2216
2348
|
var list = new List<KeyValuePair<string, string>>();
|
|
2349
|
+
var seen = new HashSet<string>();
|
|
2217
2350
|
Type t = Type.GetTypeFromProgID("Shell.Application");
|
|
2218
2351
|
dynamic shell = Activator.CreateInstance(t);
|
|
2219
2352
|
try
|
|
@@ -2222,21 +2355,22 @@ namespace DshComputerUse
|
|
|
2222
2355
|
foreach (dynamic item in folder.Items())
|
|
2223
2356
|
{
|
|
2224
2357
|
string name = item.Name; string path = item.Path;
|
|
2225
|
-
|
|
2358
|
+
AddIfNew(list, seen, name, path);
|
|
2226
2359
|
}
|
|
2227
2360
|
}
|
|
2228
2361
|
finally { Marshal.FinalReleaseComObject(shell); }
|
|
2229
|
-
// Desktop shortcuts
|
|
2230
|
-
foreach (var folder in new[] {
|
|
2362
|
+
// Desktop & Start Menu shortcuts: portable and classic apps often only live there.
|
|
2363
|
+
foreach (var folder in new[] {
|
|
2364
|
+
Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory),
|
|
2365
|
+
Environment.GetFolderPath(Environment.SpecialFolder.CommonDesktopDirectory),
|
|
2366
|
+
Environment.GetFolderPath(Environment.SpecialFolder.Programs),
|
|
2367
|
+
Environment.GetFolderPath(Environment.SpecialFolder.CommonPrograms),
|
|
2368
|
+
})
|
|
2231
2369
|
{
|
|
2232
|
-
|
|
2233
|
-
{
|
|
2234
|
-
if (folder.Length == 0 || !Directory.Exists(folder)) continue;
|
|
2235
|
-
foreach (var f in Directory.GetFiles(folder, "*.lnk"))
|
|
2236
|
-
list.Add(new KeyValuePair<string, string>(Path.GetFileNameWithoutExtension(f), "lnk:" + f));
|
|
2237
|
-
}
|
|
2238
|
-
catch (Exception) { }
|
|
2370
|
+
ScanStartFolder(folder, 0, list, seen);
|
|
2239
2371
|
}
|
|
2372
|
+
// Registry App Paths & Uninstall executable locations.
|
|
2373
|
+
ScanRegistryApps(list, seen);
|
|
2240
2374
|
cache = list; cachedAt = DateTime.UtcNow;
|
|
2241
2375
|
return list;
|
|
2242
2376
|
}
|
|
@@ -2270,6 +2404,7 @@ namespace DshComputerUse
|
|
|
2270
2404
|
string args = Args.Str(r, "args", "");
|
|
2271
2405
|
ProcessStartInfo psi;
|
|
2272
2406
|
if (appId.StartsWith("lnk:")) psi = new ProcessStartInfo(appId.Substring(4));
|
|
2407
|
+
else if (appId.StartsWith("exe:")) psi = new ProcessStartInfo(appId.Substring(4));
|
|
2273
2408
|
else if (appId.Length > 0) psi = new ProcessStartInfo("explorer.exe", "shell:AppsFolder\\" + appId);
|
|
2274
2409
|
else { psi = new ProcessStartInfo(target); if (args.Length > 0) psi.Arguments = args; }
|
|
2275
2410
|
psi.UseShellExecute = true;
|
|
@@ -2723,6 +2858,7 @@ namespace DshComputerUse
|
|
|
2723
2858
|
if (++looked > 200) break;
|
|
2724
2859
|
try
|
|
2725
2860
|
{
|
|
2861
|
+
if (IsPasswordElement(el)) continue;
|
|
2726
2862
|
object pattern;
|
|
2727
2863
|
if (!el.TryGetCurrentPattern(ValuePattern.Pattern, out pattern)) continue;
|
|
2728
2864
|
string got = ((ValuePattern)pattern).Current.Value;
|
|
@@ -2753,6 +2889,253 @@ namespace DshComputerUse
|
|
|
2753
2889
|
return sb.ToString();
|
|
2754
2890
|
}
|
|
2755
2891
|
|
|
2892
|
+
static bool IsWin32PasswordHwnd(IntPtr h)
|
|
2893
|
+
{
|
|
2894
|
+
if (h == IntPtr.Zero || !Native.IsWindow(h)) return false;
|
|
2895
|
+
var cls = new StringBuilder(64);
|
|
2896
|
+
Native.GetClassName(h, cls, cls.Capacity);
|
|
2897
|
+
string c = cls.ToString();
|
|
2898
|
+
if (c.IndexOf("Edit", StringComparison.OrdinalIgnoreCase) < 0 && c.IndexOf("RichEdit", StringComparison.OrdinalIgnoreCase) < 0)
|
|
2899
|
+
return false;
|
|
2900
|
+
int style = Native.GetWindowLong(h, -16); // GWL_STYLE
|
|
2901
|
+
return (style & 0x0020) != 0; // ES_PASSWORD
|
|
2902
|
+
}
|
|
2903
|
+
|
|
2904
|
+
static bool IsPasswordElement(AutomationElement el)
|
|
2905
|
+
{
|
|
2906
|
+
if (el == null) return false;
|
|
2907
|
+
try
|
|
2908
|
+
{
|
|
2909
|
+
if (el.Current.IsPassword) return true;
|
|
2910
|
+
int nh = el.Current.NativeWindowHandle;
|
|
2911
|
+
if (nh != 0 && IsWin32PasswordHwnd(new IntPtr(nh))) return true;
|
|
2912
|
+
}
|
|
2913
|
+
catch (Exception) { }
|
|
2914
|
+
return false;
|
|
2915
|
+
}
|
|
2916
|
+
|
|
2917
|
+
/** Check whether keyboard focus is currently inside a password input (UIA IsPassword or Win32 ES_PASSWORD). */
|
|
2918
|
+
public static object PasswordFocus()
|
|
2919
|
+
{
|
|
2920
|
+
bool isPwd = false;
|
|
2921
|
+
string reason = "";
|
|
2922
|
+
string name = "";
|
|
2923
|
+
try
|
|
2924
|
+
{
|
|
2925
|
+
IntPtr fg = Native.GetForegroundWindow();
|
|
2926
|
+
if (fg != IntPtr.Zero)
|
|
2927
|
+
{
|
|
2928
|
+
uint pid;
|
|
2929
|
+
uint tid = Native.GetWindowThreadProcessId(fg, out pid);
|
|
2930
|
+
var gti = new Native.GUITHREADINFO();
|
|
2931
|
+
gti.cbSize = Marshal.SizeOf(typeof(Native.GUITHREADINFO));
|
|
2932
|
+
if (Native.GetGUIThreadInfo(tid, ref gti) && gti.hwndFocus != IntPtr.Zero && IsWin32PasswordHwnd(gti.hwndFocus))
|
|
2933
|
+
{
|
|
2934
|
+
isPwd = true;
|
|
2935
|
+
reason = "win32_es_password";
|
|
2936
|
+
}
|
|
2937
|
+
}
|
|
2938
|
+
}
|
|
2939
|
+
catch (Exception) { }
|
|
2940
|
+
if (!isPwd)
|
|
2941
|
+
{
|
|
2942
|
+
var worker = new Thread(delegate ()
|
|
2943
|
+
{
|
|
2944
|
+
try
|
|
2945
|
+
{
|
|
2946
|
+
var focused = AutomationElement.FocusedElement;
|
|
2947
|
+
if (focused != null && IsPasswordElement(focused))
|
|
2948
|
+
{
|
|
2949
|
+
isPwd = true;
|
|
2950
|
+
reason = "uia_is_password";
|
|
2951
|
+
name = focused.Current.Name ?? "";
|
|
2952
|
+
}
|
|
2953
|
+
}
|
|
2954
|
+
catch (Exception) { }
|
|
2955
|
+
});
|
|
2956
|
+
worker.IsBackground = true;
|
|
2957
|
+
worker.SetApartmentState(ApartmentState.MTA);
|
|
2958
|
+
worker.Start();
|
|
2959
|
+
worker.Join(500);
|
|
2960
|
+
}
|
|
2961
|
+
var d = new Dictionary<string, object>();
|
|
2962
|
+
d["password"] = isPwd;
|
|
2963
|
+
d["reason"] = reason;
|
|
2964
|
+
d["name"] = name;
|
|
2965
|
+
return d;
|
|
2966
|
+
}
|
|
2967
|
+
|
|
2968
|
+
/**
|
|
2969
|
+
* Try to invoke/toggle/select an enabled native UIA control directly under
|
|
2970
|
+
* physical point (x, y) without moving the physical cursor or stealing focus.
|
|
2971
|
+
*/
|
|
2972
|
+
public static object ClickAtPoint(Dictionary<string, object> r)
|
|
2973
|
+
{
|
|
2974
|
+
int x = Args.Int(r, "x", 0);
|
|
2975
|
+
int y = Args.Int(r, "y", 0);
|
|
2976
|
+
long expectedHwnd = Args.Has(r, "hwnd") ? Convert.ToInt64(r["hwnd"]) : 0;
|
|
2977
|
+
bool ok = false;
|
|
2978
|
+
string mechanism = "";
|
|
2979
|
+
string role = "";
|
|
2980
|
+
string name = "";
|
|
2981
|
+
var worker = new Thread(delegate ()
|
|
2982
|
+
{
|
|
2983
|
+
try
|
|
2984
|
+
{
|
|
2985
|
+
var el = AutomationElement.FromPoint(new System.Windows.Point(x, y));
|
|
2986
|
+
if (el == null) return;
|
|
2987
|
+
if (!el.Current.IsEnabled || IsPasswordElement(el)) return;
|
|
2988
|
+
var ct = el.Current.ControlType;
|
|
2989
|
+
// Only intercept clearly semantic, single-click invocable controls;
|
|
2990
|
+
// never intercept Edit/Document/Pane/Custom/Group where cursor placement matters.
|
|
2991
|
+
bool allowed = ct == ControlType.Button || ct == ControlType.CheckBox || ct == ControlType.RadioButton
|
|
2992
|
+
|| ct == ControlType.MenuItem || ct == ControlType.TabItem || ct == ControlType.Hyperlink;
|
|
2993
|
+
if (!allowed) return;
|
|
2994
|
+
if (expectedHwnd != 0)
|
|
2995
|
+
{
|
|
2996
|
+
int nh = el.Current.NativeWindowHandle;
|
|
2997
|
+
IntPtr root = nh != 0 ? Native.GetAncestor(new IntPtr(nh), 2) : IntPtr.Zero;
|
|
2998
|
+
if (root != IntPtr.Zero && root.ToInt64() != expectedHwnd) return;
|
|
2999
|
+
}
|
|
3000
|
+
role = ct.ProgrammaticName.Replace("ControlType.", "");
|
|
3001
|
+
name = el.Current.Name ?? "";
|
|
3002
|
+
object pat;
|
|
3003
|
+
if (el.TryGetCurrentPattern(InvokePattern.Pattern, out pat))
|
|
3004
|
+
{
|
|
3005
|
+
((InvokePattern)pat).Invoke();
|
|
3006
|
+
ok = true;
|
|
3007
|
+
mechanism = "invoke";
|
|
3008
|
+
return;
|
|
3009
|
+
}
|
|
3010
|
+
if (el.TryGetCurrentPattern(TogglePattern.Pattern, out pat))
|
|
3011
|
+
{
|
|
3012
|
+
((TogglePattern)pat).Toggle();
|
|
3013
|
+
ok = true;
|
|
3014
|
+
mechanism = "toggle";
|
|
3015
|
+
return;
|
|
3016
|
+
}
|
|
3017
|
+
if (el.TryGetCurrentPattern(SelectionItemPattern.Pattern, out pat))
|
|
3018
|
+
{
|
|
3019
|
+
((SelectionItemPattern)pat).Select();
|
|
3020
|
+
ok = true;
|
|
3021
|
+
mechanism = "select";
|
|
3022
|
+
return;
|
|
3023
|
+
}
|
|
3024
|
+
}
|
|
3025
|
+
catch (Exception) { }
|
|
3026
|
+
});
|
|
3027
|
+
worker.IsBackground = true;
|
|
3028
|
+
worker.SetApartmentState(ApartmentState.MTA);
|
|
3029
|
+
worker.Start();
|
|
3030
|
+
worker.Join(900);
|
|
3031
|
+
var d = new Dictionary<string, object>();
|
|
3032
|
+
d["ok"] = ok;
|
|
3033
|
+
d["mechanism"] = mechanism;
|
|
3034
|
+
d["role"] = role;
|
|
3035
|
+
d["name"] = name;
|
|
3036
|
+
return d;
|
|
3037
|
+
}
|
|
3038
|
+
|
|
3039
|
+
/**
|
|
3040
|
+
* Set-of-Marks annotated screenshot: capture the display, draw crisp numbered
|
|
3041
|
+
* badges [1..N] over interactive UIA controls of the target window, and return
|
|
3042
|
+
* both the JPEG image and the indexed element list.
|
|
3043
|
+
*/
|
|
3044
|
+
public static object Marks(Dictionary<string, object> r)
|
|
3045
|
+
{
|
|
3046
|
+
int sx = Args.Int(r, "x", 0), sy = Args.Int(r, "y", 0);
|
|
3047
|
+
int sw = Args.Int(r, "width", 0), sh = Args.Int(r, "height", 0);
|
|
3048
|
+
int outW = Args.Int(r, "outWidth", sw), outH = Args.Int(r, "outHeight", sh);
|
|
3049
|
+
int quality = Math.Max(30, Math.Min(100, Args.Int(r, "quality", 75)));
|
|
3050
|
+
if (sw <= 0 || sh <= 0) throw new Exception("marks needs display width and height");
|
|
3051
|
+
var ui = Elements(r) as Dictionary<string, object>;
|
|
3052
|
+
var rawElements = ui != null && ui.ContainsKey("elements") ? ui["elements"] as List<object> : new List<object>();
|
|
3053
|
+
if (rawElements == null) rawElements = new List<object>();
|
|
3054
|
+
|
|
3055
|
+
string via;
|
|
3056
|
+
using (var rawBmp = Screen2.Grab(sx, sy, sw, sh, out via))
|
|
3057
|
+
using (var canvas = new Bitmap(outW, outH, PixelFormat.Format24bppRgb))
|
|
3058
|
+
using (var g = Graphics.FromImage(canvas))
|
|
3059
|
+
{
|
|
3060
|
+
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
|
|
3061
|
+
g.SmoothingMode = SmoothingMode.AntiAlias;
|
|
3062
|
+
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
|
|
3063
|
+
g.DrawImage(rawBmp, 0, 0, outW, outH);
|
|
3064
|
+
|
|
3065
|
+
double scaleX = (double)outW / sw;
|
|
3066
|
+
double scaleY = (double)outH / sh;
|
|
3067
|
+
var placed = new List<RectangleF>();
|
|
3068
|
+
using (var font = new Font("Microsoft YaHei UI", 10.5f, FontStyle.Bold, GraphicsUnit.Pixel))
|
|
3069
|
+
using (var fmt = new StringFormat(StringFormat.GenericTypographic))
|
|
3070
|
+
using (var bgBrush = new SolidBrush(Color.FromArgb(228, 217, 119, 87)))
|
|
3071
|
+
using (var borderPen = new Pen(Color.FromArgb(245, 255, 255, 255), 1.3f))
|
|
3072
|
+
using (var boxPen = new Pen(Color.FromArgb(140, 217, 119, 87), 1.1f))
|
|
3073
|
+
{
|
|
3074
|
+
fmt.FormatFlags |= StringFormatFlags.NoWrap;
|
|
3075
|
+
int drawn = 0;
|
|
3076
|
+
for (int i = 0; i < rawElements.Count && drawn < 120; i++)
|
|
3077
|
+
{
|
|
3078
|
+
var el = rawElements[i] as Dictionary<string, object>;
|
|
3079
|
+
if (el == null) continue;
|
|
3080
|
+
int ex = Convert.ToInt32(el["x"]), ey = Convert.ToInt32(el["y"]);
|
|
3081
|
+
int ew = Convert.ToInt32(el["width"]), eh = Convert.ToInt32(el["height"]);
|
|
3082
|
+
float rx = (float)((ex - sx) * scaleX);
|
|
3083
|
+
float ry = (float)((ey - sy) * scaleY);
|
|
3084
|
+
float rw = (float)(ew * scaleX);
|
|
3085
|
+
float rh = (float)(eh * scaleY);
|
|
3086
|
+
if (rx + rw < 0 || ry + rh < 0 || rx >= outW || ry >= outH) continue;
|
|
3087
|
+
if (rw < 4 || rh < 4) continue;
|
|
3088
|
+
el["mark"] = i + 1;
|
|
3089
|
+
drawn++;
|
|
3090
|
+
if (rw <= outW * 0.85f && rh <= outH * 0.85f)
|
|
3091
|
+
g.DrawRectangle(boxPen, rx, ry, rw, rh);
|
|
3092
|
+
string tag = (i + 1).ToString();
|
|
3093
|
+
SizeF ts = g.MeasureString(tag, font, 1000, fmt);
|
|
3094
|
+
float bw = Math.Max(16f, ts.Width + 6f);
|
|
3095
|
+
float bh = Math.Max(15f, ts.Height + 2f);
|
|
3096
|
+
float bx = Math.Max(1f, Math.Min(outW - bw - 1f, rx));
|
|
3097
|
+
float by = Math.Max(1f, Math.Min(outH - bh - 1f, ry - bh * 0.35f));
|
|
3098
|
+
var badge = new RectangleF(bx, by, bw, bh);
|
|
3099
|
+
for (int attempt = 0; attempt < 3; attempt++)
|
|
3100
|
+
{
|
|
3101
|
+
bool overlap = false;
|
|
3102
|
+
for (int j = 0; j < placed.Count; j++)
|
|
3103
|
+
if (placed[j].IntersectsWith(badge)) { overlap = true; break; }
|
|
3104
|
+
if (!overlap) break;
|
|
3105
|
+
badge.X = Math.Min(outW - bw - 1f, badge.X + bw + 2f);
|
|
3106
|
+
}
|
|
3107
|
+
placed.Add(badge);
|
|
3108
|
+
g.FillRectangle(bgBrush, badge);
|
|
3109
|
+
g.DrawRectangle(borderPen, badge.X, badge.Y, badge.Width, badge.Height);
|
|
3110
|
+
g.DrawString(tag, font, Brushes.White, badge.X + (badge.Width - ts.Width) / 2f, badge.Y + (badge.Height - ts.Height) / 2f - 0.5f, fmt);
|
|
3111
|
+
}
|
|
3112
|
+
}
|
|
3113
|
+
|
|
3114
|
+
using (var ms = new MemoryStream())
|
|
3115
|
+
{
|
|
3116
|
+
ImageCodecInfo jpeg = null;
|
|
3117
|
+
foreach (var c in ImageCodecInfo.GetImageEncoders()) if (c.MimeType == "image/jpeg") { jpeg = c; break; }
|
|
3118
|
+
if (jpeg != null)
|
|
3119
|
+
{
|
|
3120
|
+
using (var ep = new EncoderParameters(1))
|
|
3121
|
+
{
|
|
3122
|
+
ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)quality);
|
|
3123
|
+
canvas.Save(ms, jpeg, ep);
|
|
3124
|
+
}
|
|
3125
|
+
}
|
|
3126
|
+
else canvas.Save(ms, ImageFormat.Jpeg);
|
|
3127
|
+
|
|
3128
|
+
var outDict = new Dictionary<string, object>();
|
|
3129
|
+
if (ui != null) foreach (var kv in ui) outDict[kv.Key] = kv.Value;
|
|
3130
|
+
outDict["data"] = Convert.ToBase64String(ms.ToArray());
|
|
3131
|
+
outDict["width"] = outW;
|
|
3132
|
+
outDict["height"] = outH;
|
|
3133
|
+
outDict["via"] = via;
|
|
3134
|
+
return outDict;
|
|
3135
|
+
}
|
|
3136
|
+
}
|
|
3137
|
+
}
|
|
3138
|
+
|
|
2756
3139
|
static List<object> Collect(IntPtr hwnd, int max, bool includeText)
|
|
2757
3140
|
{
|
|
2758
3141
|
var root = AutomationElement.FromHandle(hwnd);
|
|
@@ -2769,6 +3152,8 @@ namespace DshComputerUse
|
|
|
2769
3152
|
cache.Add(AutomationElement.RuntimeIdProperty);
|
|
2770
3153
|
cache.Add(AutomationElement.ClassNameProperty);
|
|
2771
3154
|
cache.Add(AutomationElement.IsKeyboardFocusableProperty);
|
|
3155
|
+
cache.Add(AutomationElement.IsPasswordProperty);
|
|
3156
|
+
cache.Add(AutomationElement.NativeWindowHandleProperty);
|
|
2772
3157
|
cache.Add(ValuePattern.ValueProperty);
|
|
2773
3158
|
cache.Add(TogglePattern.ToggleStateProperty);
|
|
2774
3159
|
cache.AutomationElementMode = AutomationElementMode.None;
|
|
@@ -2787,9 +3172,22 @@ namespace DshComputerUse
|
|
|
2787
3172
|
// Web 编辑器的 contenteditable 常是 Group(ProseMirror/tiptap 等),
|
|
2788
3173
|
// 不是 Edit;保留这类 Group,其余 Group 跳过,避免淹没按钮。
|
|
2789
3174
|
if (type == ControlType.Group && !IsEditorClass(cls)) continue;
|
|
3175
|
+
bool isPwd = false;
|
|
3176
|
+
try
|
|
3177
|
+
{
|
|
3178
|
+
object pwdProp = el.GetCachedPropertyValue(AutomationElement.IsPasswordProperty, true);
|
|
3179
|
+
if (pwdProp is bool && (bool)pwdProp) isPwd = true;
|
|
3180
|
+
if (!isPwd)
|
|
3181
|
+
{
|
|
3182
|
+
object nhProp = el.GetCachedPropertyValue(AutomationElement.NativeWindowHandleProperty, true);
|
|
3183
|
+
if (nhProp is int && (int)nhProp != 0 && IsWin32PasswordHwnd(new IntPtr((int)nhProp))) isPwd = true;
|
|
3184
|
+
}
|
|
3185
|
+
}
|
|
3186
|
+
catch (Exception) { }
|
|
2790
3187
|
var d = new Dictionary<string, object>();
|
|
2791
3188
|
d["role"] = type == ControlType.Group ? "Edit" : type.ProgrammaticName.Replace("ControlType.", "");
|
|
2792
3189
|
if (type == ControlType.Group) d["editable"] = true;
|
|
3190
|
+
if (isPwd) d["password"] = true;
|
|
2793
3191
|
d["name"] = name.Length > 120 ? name.Substring(0, 120) + "…" : name;
|
|
2794
3192
|
string aid = (el.GetCachedPropertyValue(AutomationElement.AutomationIdProperty) as string) ?? "";
|
|
2795
3193
|
if (aid.Length > 0) d["automationId"] = aid;
|
|
@@ -2797,7 +3195,8 @@ namespace DshComputerUse
|
|
|
2797
3195
|
if (runtimeId != null && runtimeId.Length > 0) d["runtimeId"] = String.Join(",", runtimeId);
|
|
2798
3196
|
d["focusable"] = (bool)el.GetCachedPropertyValue(AutomationElement.IsKeyboardFocusableProperty);
|
|
2799
3197
|
object value = el.GetCachedPropertyValue(ValuePattern.ValueProperty, true);
|
|
2800
|
-
if (
|
|
3198
|
+
if (isPwd) { d["value"] = "[password]"; }
|
|
3199
|
+
else if (value is string && ((string)value).Length > 0) { var v = (string)value; d["value"] = v.Length > 120 ? v.Substring(0, 120) + "…" : v; }
|
|
2801
3200
|
object toggle = el.GetCachedPropertyValue(TogglePattern.ToggleStateProperty, true);
|
|
2802
3201
|
if (toggle is ToggleState) d["checked"] = (ToggleState)toggle == ToggleState.On;
|
|
2803
3202
|
if (!(bool)el.GetCachedPropertyValue(AutomationElement.IsEnabledProperty)) d["disabled"] = true;
|
|
@@ -3347,13 +3746,51 @@ namespace DshComputerUse
|
|
|
3347
3746
|
}
|
|
3348
3747
|
}
|
|
3349
3748
|
|
|
3749
|
+
static int lastMarkX, lastMarkY;
|
|
3750
|
+
static bool hasLastMark;
|
|
3751
|
+
|
|
3350
3752
|
public static void MarkPointer(int x, int y, string text, int holdMs)
|
|
3351
3753
|
{
|
|
3352
|
-
|
|
3754
|
+
GlideMark(x, y, text, Accent, holdMs, false, false);
|
|
3755
|
+
}
|
|
3756
|
+
|
|
3757
|
+
/** Smooth single-segment EaseOutCubic glide (~160ms) to (x, y) before holding the Codex cursor. */
|
|
3758
|
+
public static void GlideMark(int x, int y, string text, Color color, int holdMs, bool capturable, bool ripple)
|
|
3759
|
+
{
|
|
3760
|
+
int startX, startY;
|
|
3761
|
+
if (hasLastMark)
|
|
3762
|
+
{
|
|
3763
|
+
startX = lastMarkX;
|
|
3764
|
+
startY = lastMarkY;
|
|
3765
|
+
}
|
|
3766
|
+
else
|
|
3767
|
+
{
|
|
3768
|
+
Native.POINT cur;
|
|
3769
|
+
if (Native.GetCursorPos(out cur)) { startX = cur.X; startY = cur.Y; }
|
|
3770
|
+
else { startX = x; startY = y; }
|
|
3771
|
+
}
|
|
3772
|
+
double dist = Math.Sqrt((double)(x - startX) * (x - startX) + (double)(y - startY) * (y - startY));
|
|
3773
|
+
if (dist >= 40.0)
|
|
3774
|
+
{
|
|
3775
|
+
int steps = Math.Max(5, Math.Min(10, (int)(dist / 90.0) + 5));
|
|
3776
|
+
for (int i = 1; i < steps; i++)
|
|
3777
|
+
{
|
|
3778
|
+
double t = (double)i / steps;
|
|
3779
|
+
double ease = 1.0 - Math.Pow(1.0 - t, 3.0); // EaseOutCubic
|
|
3780
|
+
int ix = startX + (int)Math.Round((x - startX) * ease);
|
|
3781
|
+
int iy = startY + (int)Math.Round((y - startY) * ease);
|
|
3782
|
+
Mark(ix, iy, text, color, holdMs, capturable, false);
|
|
3783
|
+
Thread.Sleep(16);
|
|
3784
|
+
}
|
|
3785
|
+
}
|
|
3786
|
+
Mark(x, y, text, color, holdMs, capturable, ripple);
|
|
3353
3787
|
}
|
|
3354
3788
|
|
|
3355
3789
|
public static void Mark(int x, int y, string text, Color color, int holdMs, bool capturable, bool ripple)
|
|
3356
3790
|
{
|
|
3791
|
+
lastMarkX = x;
|
|
3792
|
+
lastMarkY = y;
|
|
3793
|
+
hasLastMark = true;
|
|
3357
3794
|
Ensure();
|
|
3358
3795
|
Ui(delegate
|
|
3359
3796
|
{
|