@copylee/dsh-computer-use 0.9.0 → 0.10.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 +453 -56
- package/lib/client.js +74 -12
- package/lib/index.js +432 -30
- package/lib/types/access.d.ts +5 -0
- package/lib/types/computer.d.ts +30 -2
- package/lib/types/diagnostics.d.ts +71 -0
- package/lib/types/index.d.ts +5 -1
- package/lib/types/routes.d.ts +6 -0
- package/lib/types/sessions.d.ts +2 -0
- package/package.json +1 -1
package/helper/CuHelper.cs
CHANGED
|
@@ -21,6 +21,7 @@ using System.Drawing.Drawing2D;
|
|
|
21
21
|
using System.Drawing.Imaging;
|
|
22
22
|
using System.Drawing.Text;
|
|
23
23
|
using System.IO;
|
|
24
|
+
using System.Reflection;
|
|
24
25
|
using System.Runtime.InteropServices;
|
|
25
26
|
using System.Text;
|
|
26
27
|
using System.Threading;
|
|
@@ -278,7 +279,15 @@ namespace DshComputerUse
|
|
|
278
279
|
case "screenshot": return Screen2.Capture(r);
|
|
279
280
|
case "settle": return Screen2.Settle(r);
|
|
280
281
|
case "cursor": { Native.POINT p; Native.GetCursorPos(out p); return Pt(p.X, p.Y); }
|
|
281
|
-
case "move":
|
|
282
|
+
case "move":
|
|
283
|
+
using (new PassThrough())
|
|
284
|
+
{
|
|
285
|
+
int mx = Args.Int(r, "x", 0), my = Args.Int(r, "y", 0);
|
|
286
|
+
Overlay.Dodge(mx, my);
|
|
287
|
+
Input.Move(mx, my);
|
|
288
|
+
if (Overlay.ShowClickTarget) Overlay.MarkPointer(mx, my, "DSH", 900);
|
|
289
|
+
}
|
|
290
|
+
return null;
|
|
282
291
|
case "click":
|
|
283
292
|
Native.POINT clickPoint;
|
|
284
293
|
if (Args.Has(r, "x")) clickPoint = new Native.POINT(Args.Int(r, "x", 0), Args.Int(r, "y", 0));
|
|
@@ -319,6 +328,7 @@ namespace DshComputerUse
|
|
|
319
328
|
case "clipboard_restore": { var d = new Dictionary<string, object>(); d["restored"] = Clip.RestoreAgent(); return d; }
|
|
320
329
|
case "ui": return Uia.Elements(r);
|
|
321
330
|
case "text": return Uia.Text(r);
|
|
331
|
+
case "ocr": return Ocr.Read(r);
|
|
322
332
|
case "overlay_show": Overlay.ExcludeFromCapture = Args.Bool(r, "excludeFromCapture", true); Overlay.Show(r); return null;
|
|
323
333
|
case "overlay_pause": Overlay.SetPaused(Args.Bool(r, "paused", false)); return null;
|
|
324
334
|
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));
|
|
@@ -454,14 +464,11 @@ namespace DshComputerUse
|
|
|
454
464
|
|
|
455
465
|
static ImageCodecInfo jpegCodec;
|
|
456
466
|
|
|
457
|
-
public static
|
|
467
|
+
public static Bitmap Grab(int x, int y, int w, int h, out string via)
|
|
458
468
|
{
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
if (w <= 0 || h <= 0 || ow <= 0 || oh <= 0) throw new Exception("invalid capture size");
|
|
463
|
-
string via = "blt";
|
|
464
|
-
using (var bmp = new Bitmap(w, h, PixelFormat.Format24bppRgb))
|
|
469
|
+
via = "blt";
|
|
470
|
+
var bmp = new Bitmap(w, h, PixelFormat.Format24bppRgb);
|
|
471
|
+
try
|
|
465
472
|
{
|
|
466
473
|
using (var g = Graphics.FromImage(bmp))
|
|
467
474
|
{
|
|
@@ -472,7 +479,6 @@ namespace DshComputerUse
|
|
|
472
479
|
// the card (no flicker). Fallback: hide the card for one BitBlt.
|
|
473
480
|
bool done = false;
|
|
474
481
|
var area = new Rectangle(x, y, w, h);
|
|
475
|
-
via = "blt";
|
|
476
482
|
if (CardLayer.Covers(area))
|
|
477
483
|
{
|
|
478
484
|
g.ReleaseHdc(dst);
|
|
@@ -493,6 +499,24 @@ namespace DshComputerUse
|
|
|
493
499
|
}
|
|
494
500
|
Native.ReleaseDC(IntPtr.Zero, src);
|
|
495
501
|
}
|
|
502
|
+
return bmp;
|
|
503
|
+
}
|
|
504
|
+
catch
|
|
505
|
+
{
|
|
506
|
+
bmp.Dispose();
|
|
507
|
+
throw;
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
|
|
511
|
+
public static Dictionary<string, object> Capture(Dictionary<string, object> r)
|
|
512
|
+
{
|
|
513
|
+
int x = Args.Int(r, "x", 0), y = Args.Int(r, "y", 0), w = Args.Int(r, "width", 0), h = Args.Int(r, "height", 0);
|
|
514
|
+
int ow = Args.Int(r, "outWidth", w), oh = Args.Int(r, "outHeight", h);
|
|
515
|
+
int quality = Args.Int(r, "quality", 80);
|
|
516
|
+
if (w <= 0 || h <= 0 || ow <= 0 || oh <= 0) throw new Exception("invalid capture size");
|
|
517
|
+
string via;
|
|
518
|
+
using (var bmp = Grab(x, y, w, h, out via))
|
|
519
|
+
{
|
|
496
520
|
// compare: report "unchanged" instead of an identical image; mark: remember for next time.
|
|
497
521
|
bool compare = Args.Bool(r, "compare", false), mark = Args.Bool(r, "mark", false) || compare;
|
|
498
522
|
if (mark)
|
|
@@ -555,6 +579,298 @@ namespace DshComputerUse
|
|
|
555
579
|
}
|
|
556
580
|
}
|
|
557
581
|
|
|
582
|
+
/**
|
|
583
|
+
* Built-in Windows 10/11 OCR via WinRT reflection (Windows.Media.Ocr).
|
|
584
|
+
* Zero extra compile references or native DLLs: falls back cleanly if the
|
|
585
|
+
* host OS has no WinRT OCR language pack installed.
|
|
586
|
+
*/
|
|
587
|
+
internal static class Ocr
|
|
588
|
+
{
|
|
589
|
+
static Type tEngine, tDecoder, tStream, tWriter, tAsyncInfo;
|
|
590
|
+
static object cachedEngine;
|
|
591
|
+
static string cachedLanguage = "";
|
|
592
|
+
static int cachedMaxDim = 2600;
|
|
593
|
+
static bool initTried;
|
|
594
|
+
|
|
595
|
+
static void EnsureTypes()
|
|
596
|
+
{
|
|
597
|
+
if (initTried) return;
|
|
598
|
+
initTried = true;
|
|
599
|
+
tEngine = Type.GetType("Windows.Media.Ocr.OcrEngine, Windows.Media, ContentType=WindowsRuntime", false);
|
|
600
|
+
tDecoder = Type.GetType("Windows.Graphics.Imaging.BitmapDecoder, Windows.Graphics, ContentType=WindowsRuntime", false);
|
|
601
|
+
tStream = Type.GetType("Windows.Storage.Streams.InMemoryRandomAccessStream, Windows.Storage, ContentType=WindowsRuntime", false);
|
|
602
|
+
tWriter = Type.GetType("Windows.Storage.Streams.DataWriter, Windows.Storage, ContentType=WindowsRuntime", false);
|
|
603
|
+
tAsyncInfo = Type.GetType("Windows.Foundation.IAsyncInfo, Windows.Foundation, ContentType=WindowsRuntime", false);
|
|
604
|
+
if (tEngine == null || tDecoder == null || tStream == null || tWriter == null || tAsyncInfo == null) return;
|
|
605
|
+
try
|
|
606
|
+
{
|
|
607
|
+
var create = tEngine.GetMethod("TryCreateFromUserProfileLanguages", BindingFlags.Public | BindingFlags.Static);
|
|
608
|
+
if (create != null) cachedEngine = create.Invoke(null, null);
|
|
609
|
+
if (cachedEngine != null)
|
|
610
|
+
{
|
|
611
|
+
object lang = GetProp(cachedEngine, "RecognizerLanguage");
|
|
612
|
+
if (lang != null) cachedLanguage = Convert.ToString(GetProp(lang, "LanguageTag")) ?? "";
|
|
613
|
+
var maxProp = tEngine.GetProperty("MaxImageDimension", BindingFlags.Public | BindingFlags.Static);
|
|
614
|
+
if (maxProp != null)
|
|
615
|
+
{
|
|
616
|
+
int dim = Convert.ToInt32(maxProp.GetValue(null, null));
|
|
617
|
+
if (dim >= 512) cachedMaxDim = dim;
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
catch (Exception) { cachedEngine = null; }
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
static MethodInfo FindMethod(Type t, string name, int argCount)
|
|
625
|
+
{
|
|
626
|
+
if (t == null) return null;
|
|
627
|
+
foreach (var m in t.GetMethods(BindingFlags.Public | BindingFlags.Instance))
|
|
628
|
+
if (m.Name == name && m.GetParameters().Length == argCount) return m;
|
|
629
|
+
foreach (var iface in t.GetInterfaces())
|
|
630
|
+
foreach (var m in iface.GetMethods(BindingFlags.Public | BindingFlags.Instance))
|
|
631
|
+
if (m.Name == name && m.GetParameters().Length == argCount) return m;
|
|
632
|
+
return null;
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
static object Invoke(object target, string name, params object[] args)
|
|
636
|
+
{
|
|
637
|
+
var m = FindMethod(target.GetType(), name, args == null ? 0 : args.Length);
|
|
638
|
+
if (m == null) throw new Exception("WinRT method not found: " + name);
|
|
639
|
+
return m.Invoke(target, args);
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
static object InvokeAsync(object target, string name, int timeoutMs, params object[] args)
|
|
643
|
+
{
|
|
644
|
+
var m = FindMethod(target.GetType(), name, args == null ? 0 : args.Length);
|
|
645
|
+
if (m == null) throw new Exception("WinRT method not found: " + name);
|
|
646
|
+
object op = m.Invoke(target, args);
|
|
647
|
+
return AwaitWinRt(op, m.ReturnType, timeoutMs);
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
static object GetProp(object target, string name)
|
|
651
|
+
{
|
|
652
|
+
var t = target.GetType();
|
|
653
|
+
var p = t.GetProperty(name, BindingFlags.Public | BindingFlags.Instance);
|
|
654
|
+
if (p != null) return p.GetValue(target, null);
|
|
655
|
+
foreach (var iface in t.GetInterfaces())
|
|
656
|
+
{
|
|
657
|
+
p = iface.GetProperty(name, BindingFlags.Public | BindingFlags.Instance);
|
|
658
|
+
if (p != null) return p.GetValue(target, null);
|
|
659
|
+
}
|
|
660
|
+
return null;
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
static object AwaitWinRt(object asyncOp, Type asyncType, int timeoutMs)
|
|
664
|
+
{
|
|
665
|
+
if (asyncOp == null) return null;
|
|
666
|
+
var statusProp = tAsyncInfo.GetProperty("Status");
|
|
667
|
+
var errProp = tAsyncInfo.GetProperty("ErrorCode");
|
|
668
|
+
var watch = Stopwatch.StartNew();
|
|
669
|
+
while (true)
|
|
670
|
+
{
|
|
671
|
+
int status = Convert.ToInt32(statusProp.GetValue(asyncOp, null));
|
|
672
|
+
if (status == 1) // Completed
|
|
673
|
+
{
|
|
674
|
+
var getResults = FindMethod(asyncType, "GetResults", 0) ?? FindMethod(asyncOp.GetType(), "GetResults", 0);
|
|
675
|
+
return getResults != null ? getResults.Invoke(asyncOp, null) : null;
|
|
676
|
+
}
|
|
677
|
+
if (status == 2) throw new Exception("Windows OCR operation was cancelled");
|
|
678
|
+
if (status == 3)
|
|
679
|
+
{
|
|
680
|
+
object err = errProp != null ? errProp.GetValue(asyncOp, null) : null;
|
|
681
|
+
throw new Exception("Windows OCR failed: " + (err != null ? err.ToString() : "unknown error"));
|
|
682
|
+
}
|
|
683
|
+
if (watch.ElapsedMilliseconds >= timeoutMs)
|
|
684
|
+
{
|
|
685
|
+
try { var cancel = tAsyncInfo.GetMethod("Cancel"); if (cancel != null) cancel.Invoke(asyncOp, null); } catch (Exception) { }
|
|
686
|
+
throw new Exception("Windows OCR timed out");
|
|
687
|
+
}
|
|
688
|
+
Thread.Sleep(5);
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
static bool IsCjk(char c)
|
|
693
|
+
{
|
|
694
|
+
return (c >= '\u4E00' && c <= '\u9FFF') || (c >= '\u3400' && c <= '\u4DBF')
|
|
695
|
+
|| (c >= '\u3000' && c <= '\u303F') || (c >= '\uFF00' && c <= '\uFFEF');
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
/** Collapse spaces Windows.Media.Ocr inserts between adjacent CJK characters. */
|
|
699
|
+
static string CleanLineText(string s)
|
|
700
|
+
{
|
|
701
|
+
if (string.IsNullOrEmpty(s)) return "";
|
|
702
|
+
var sb = new StringBuilder(s.Length);
|
|
703
|
+
for (int i = 0; i < s.Length; i++)
|
|
704
|
+
{
|
|
705
|
+
char c = s[i];
|
|
706
|
+
if (c == ' ' && sb.Length > 0 && i + 1 < s.Length && IsCjk(sb[sb.Length - 1]) && IsCjk(s[i + 1]))
|
|
707
|
+
continue;
|
|
708
|
+
sb.Append(c);
|
|
709
|
+
}
|
|
710
|
+
return sb.ToString();
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
static void TryDispose(object obj)
|
|
714
|
+
{
|
|
715
|
+
var d = obj as IDisposable;
|
|
716
|
+
if (d != null) { try { d.Dispose(); } catch (Exception) { } }
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
public static Dictionary<string, object> Read(Dictionary<string, object> r)
|
|
720
|
+
{
|
|
721
|
+
EnsureTypes();
|
|
722
|
+
if (cachedEngine == null)
|
|
723
|
+
throw new Exception("Windows OCR is not available on this system (no OCR language pack found).");
|
|
724
|
+
|
|
725
|
+
int x, y, w, h;
|
|
726
|
+
var meta = new Dictionary<string, object>();
|
|
727
|
+
if (Args.Has(r, "width") && Args.Has(r, "height"))
|
|
728
|
+
{
|
|
729
|
+
x = Args.Int(r, "x", 0);
|
|
730
|
+
y = Args.Int(r, "y", 0);
|
|
731
|
+
w = Args.Int(r, "width", 0);
|
|
732
|
+
h = Args.Int(r, "height", 0);
|
|
733
|
+
}
|
|
734
|
+
else
|
|
735
|
+
{
|
|
736
|
+
IntPtr hwnd = Args.Has(r, "hwnd") ? new IntPtr(Convert.ToInt64(r["hwnd"])) : Native.GetForegroundWindow();
|
|
737
|
+
var desc = Windows.Describe(hwnd);
|
|
738
|
+
if (desc.Count == 0) throw new Exception("No target window found for OCR.");
|
|
739
|
+
foreach (var kv in desc) meta[kv.Key] = kv.Value;
|
|
740
|
+
var vs = SystemInformation.VirtualScreen;
|
|
741
|
+
int rx = Convert.ToInt32(desc["x"]), ry = Convert.ToInt32(desc["y"]);
|
|
742
|
+
int rw = Convert.ToInt32(desc["width"]), rh = Convert.ToInt32(desc["height"]);
|
|
743
|
+
int left = Math.Max(vs.Left, rx);
|
|
744
|
+
int top = Math.Max(vs.Top, ry);
|
|
745
|
+
int right = Math.Min(vs.Right, rx + rw);
|
|
746
|
+
int bottom = Math.Min(vs.Bottom, ry + rh);
|
|
747
|
+
x = left; y = top; w = right - left; h = bottom - top;
|
|
748
|
+
}
|
|
749
|
+
if (w <= 2 || h <= 2) throw new Exception("Target area is too small for OCR.");
|
|
750
|
+
|
|
751
|
+
string via;
|
|
752
|
+
using (var rawBmp = Screen2.Grab(x, y, w, h, out via))
|
|
753
|
+
{
|
|
754
|
+
double scale = 1.0;
|
|
755
|
+
int maxSide = Math.Max(w, h);
|
|
756
|
+
if (maxSide > cachedMaxDim) scale = (double)cachedMaxDim / maxSide;
|
|
757
|
+
Bitmap work = rawBmp;
|
|
758
|
+
Bitmap resized = null;
|
|
759
|
+
byte[] pngBytes;
|
|
760
|
+
try
|
|
761
|
+
{
|
|
762
|
+
if (scale < 1.0)
|
|
763
|
+
{
|
|
764
|
+
int nw = Math.Max(1, (int)Math.Round(w * scale));
|
|
765
|
+
int nh = Math.Max(1, (int)Math.Round(h * scale));
|
|
766
|
+
resized = new Bitmap(nw, nh, PixelFormat.Format24bppRgb);
|
|
767
|
+
using (var g = Graphics.FromImage(resized))
|
|
768
|
+
{
|
|
769
|
+
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
|
|
770
|
+
g.DrawImage(rawBmp, 0, 0, nw, nh);
|
|
771
|
+
}
|
|
772
|
+
work = resized;
|
|
773
|
+
}
|
|
774
|
+
using (var ms = new MemoryStream())
|
|
775
|
+
{
|
|
776
|
+
work.Save(ms, ImageFormat.Png);
|
|
777
|
+
pngBytes = ms.ToArray();
|
|
778
|
+
}
|
|
779
|
+
}
|
|
780
|
+
finally { if (resized != null) resized.Dispose(); }
|
|
781
|
+
|
|
782
|
+
object ras = null, writer = null, softwareBitmap = null;
|
|
783
|
+
try
|
|
784
|
+
{
|
|
785
|
+
ras = Activator.CreateInstance(tStream);
|
|
786
|
+
writer = Activator.CreateInstance(tWriter, new object[] { ras });
|
|
787
|
+
Invoke(writer, "WriteBytes", new object[] { pngBytes });
|
|
788
|
+
InvokeAsync(writer, "StoreAsync", 8000);
|
|
789
|
+
InvokeAsync(writer, "FlushAsync", 8000);
|
|
790
|
+
Invoke(writer, "DetachStream");
|
|
791
|
+
TryDispose(writer); writer = null;
|
|
792
|
+
Invoke(ras, "Seek", new object[] { (ulong)0 });
|
|
793
|
+
|
|
794
|
+
MethodInfo createDecoder = null;
|
|
795
|
+
foreach (var m in tDecoder.GetMethods(BindingFlags.Public | BindingFlags.Static))
|
|
796
|
+
if (m.Name == "CreateAsync" && m.GetParameters().Length == 1) { createDecoder = m; break; }
|
|
797
|
+
if (createDecoder == null) throw new Exception("BitmapDecoder.CreateAsync not found");
|
|
798
|
+
object decoder = AwaitWinRt(createDecoder.Invoke(null, new object[] { ras }), createDecoder.ReturnType, 8000);
|
|
799
|
+
if (decoder == null) throw new Exception("BitmapDecoder returned null");
|
|
800
|
+
softwareBitmap = InvokeAsync(decoder, "GetSoftwareBitmapAsync", 8000);
|
|
801
|
+
if (softwareBitmap == null) throw new Exception("GetSoftwareBitmapAsync returned null");
|
|
802
|
+
object ocrResult = InvokeAsync(cachedEngine, "RecognizeAsync", 10000, new object[] { softwareBitmap });
|
|
803
|
+
if (ocrResult == null) throw new Exception("OcrEngine.RecognizeAsync returned null");
|
|
804
|
+
|
|
805
|
+
var linesOut = new List<object>();
|
|
806
|
+
var allLines = new List<string>();
|
|
807
|
+
double invScale = 1.0 / scale;
|
|
808
|
+
var rawLines = GetProp(ocrResult, "Lines") as IEnumerable;
|
|
809
|
+
if (rawLines != null)
|
|
810
|
+
{
|
|
811
|
+
foreach (var line in rawLines)
|
|
812
|
+
{
|
|
813
|
+
string lineText = CleanLineText(Convert.ToString(GetProp(line, "Text")) ?? "");
|
|
814
|
+
if (lineText.Trim().Length == 0) continue;
|
|
815
|
+
var wordsOut = new List<object>();
|
|
816
|
+
double minX = double.MaxValue, minY = double.MaxValue, maxX = double.MinValue, maxY = double.MinValue;
|
|
817
|
+
var rawWords = GetProp(line, "Words") as IEnumerable;
|
|
818
|
+
if (rawWords != null)
|
|
819
|
+
{
|
|
820
|
+
foreach (var word in rawWords)
|
|
821
|
+
{
|
|
822
|
+
string wt = Convert.ToString(GetProp(word, "Text")) ?? "";
|
|
823
|
+
object rect = GetProp(word, "BoundingRect");
|
|
824
|
+
if (rect == null) continue;
|
|
825
|
+
double wx = Convert.ToDouble(GetProp(rect, "X")) * invScale;
|
|
826
|
+
double wy = Convert.ToDouble(GetProp(rect, "Y")) * invScale;
|
|
827
|
+
double ww = Convert.ToDouble(GetProp(rect, "Width")) * invScale;
|
|
828
|
+
double wh = Convert.ToDouble(GetProp(rect, "Height")) * invScale;
|
|
829
|
+
if (wx < minX) minX = wx;
|
|
830
|
+
if (wy < minY) minY = wy;
|
|
831
|
+
if (wx + ww > maxX) maxX = wx + ww;
|
|
832
|
+
if (wy + wh > maxY) maxY = wy + wh;
|
|
833
|
+
var wd = new Dictionary<string, object>();
|
|
834
|
+
wd["text"] = wt;
|
|
835
|
+
wd["x"] = x + (int)Math.Round(wx);
|
|
836
|
+
wd["y"] = y + (int)Math.Round(wy);
|
|
837
|
+
wd["width"] = Math.Max(1, (int)Math.Round(ww));
|
|
838
|
+
wd["height"] = Math.Max(1, (int)Math.Round(wh));
|
|
839
|
+
wordsOut.Add(wd);
|
|
840
|
+
}
|
|
841
|
+
}
|
|
842
|
+
if (minX == double.MaxValue) continue;
|
|
843
|
+
var ld = new Dictionary<string, object>();
|
|
844
|
+
ld["text"] = lineText;
|
|
845
|
+
ld["x"] = x + (int)Math.Round(minX);
|
|
846
|
+
ld["y"] = y + (int)Math.Round(minY);
|
|
847
|
+
ld["width"] = Math.Max(1, (int)Math.Round(maxX - minX));
|
|
848
|
+
ld["height"] = Math.Max(1, (int)Math.Round(maxY - minY));
|
|
849
|
+
ld["words"] = wordsOut;
|
|
850
|
+
linesOut.Add(ld);
|
|
851
|
+
allLines.Add(lineText);
|
|
852
|
+
}
|
|
853
|
+
}
|
|
854
|
+
|
|
855
|
+
meta["language"] = cachedLanguage;
|
|
856
|
+
meta["x"] = x;
|
|
857
|
+
meta["y"] = y;
|
|
858
|
+
meta["width"] = w;
|
|
859
|
+
meta["height"] = h;
|
|
860
|
+
meta["text"] = string.Join("\n", allLines.ToArray());
|
|
861
|
+
meta["lines"] = linesOut;
|
|
862
|
+
return meta;
|
|
863
|
+
}
|
|
864
|
+
finally
|
|
865
|
+
{
|
|
866
|
+
TryDispose(softwareBitmap);
|
|
867
|
+
TryDispose(writer);
|
|
868
|
+
TryDispose(ras);
|
|
869
|
+
}
|
|
870
|
+
}
|
|
871
|
+
}
|
|
872
|
+
}
|
|
873
|
+
|
|
558
874
|
internal static class Input
|
|
559
875
|
{
|
|
560
876
|
static readonly int InputSize = Marshal.SizeOf(typeof(Native.INPUT));
|
|
@@ -703,8 +1019,8 @@ namespace DshComputerUse
|
|
|
703
1019
|
if (showTarget)
|
|
704
1020
|
{
|
|
705
1021
|
string preVerb = count >= 3 ? "三击" : count == 2 ? "双击" : button == "right" ? "右键" : button == "middle" ? "中键" : "点击";
|
|
706
|
-
Overlay.Mark(x, y, preVerb + " " + coordLabel, Color.FromArgb(217, 119, 87), 2000, false);
|
|
707
|
-
Thread.Sleep(
|
|
1022
|
+
Overlay.Mark(x, y, preVerb + " " + coordLabel, Color.FromArgb(217, 119, 87), 2000, false, false);
|
|
1023
|
+
Thread.Sleep(160);
|
|
708
1024
|
}
|
|
709
1025
|
else if (moved)
|
|
710
1026
|
{
|
|
@@ -749,7 +1065,9 @@ namespace DshComputerUse
|
|
|
749
1065
|
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);
|
|
750
1066
|
uint down, up; Flags(Args.Str(r, "button", "left"), out down, out up);
|
|
751
1067
|
Overlay.Dodge(x1, y1); Overlay.Dodge(x2, y2);
|
|
752
|
-
Move(x1, y1);
|
|
1068
|
+
Move(x1, y1);
|
|
1069
|
+
if (Overlay.ShowClickTarget) Overlay.MarkPointer(x1, y1, "拖动", 1200);
|
|
1070
|
+
Thread.Sleep(40);
|
|
753
1071
|
Send(new List<Native.INPUT> { Mouse(down, 0) });
|
|
754
1072
|
try
|
|
755
1073
|
{
|
|
@@ -763,12 +1081,17 @@ namespace DshComputerUse
|
|
|
763
1081
|
Thread.Sleep(60);
|
|
764
1082
|
}
|
|
765
1083
|
finally { Send(new List<Native.INPUT> { Mouse(up, 0) }); }
|
|
1084
|
+
if (Overlay.ShowClickTarget) Overlay.MarkPointer(x2, y2, "已拖动", 1200);
|
|
766
1085
|
return null;
|
|
767
1086
|
}
|
|
768
1087
|
|
|
769
1088
|
public static object Scroll(Dictionary<string, object> r)
|
|
770
1089
|
{
|
|
771
|
-
|
|
1090
|
+
int sx = 0, sy = 0;
|
|
1091
|
+
bool hasPos = Args.Has(r, "x");
|
|
1092
|
+
if (hasPos) { sx = Args.Int(r, "x", 0); sy = Args.Int(r, "y", 0); Overlay.Dodge(sx, sy); Move(sx, sy); Thread.Sleep(30); }
|
|
1093
|
+
else { Native.POINT p; if (Native.GetCursorPos(out p)) { sx = p.X; sy = p.Y; } }
|
|
1094
|
+
if (Overlay.ShowClickTarget) Overlay.MarkPointer(sx, sy, "滚动", 900);
|
|
772
1095
|
int dx = Args.Int(r, "dx", 0), dy = Args.Int(r, "dy", 0);
|
|
773
1096
|
var mods = Args.Ints(r, "modifiers");
|
|
774
1097
|
Modifiers(mods, false);
|
|
@@ -2889,16 +3212,40 @@ namespace DshComputerUse
|
|
|
2889
3212
|
}
|
|
2890
3213
|
}
|
|
2891
3214
|
|
|
2892
|
-
|
|
3215
|
+
/**
|
|
3216
|
+
* Collaborative Codex-style cursor badge: the corner nearest the pointer
|
|
3217
|
+
* tip stays tight (2px), while the other three corners are smoothly rounded.
|
|
3218
|
+
*/
|
|
3219
|
+
static GraphicsPath TagShape(RectangleF r, bool flipX, bool flipY, float scale)
|
|
3220
|
+
{
|
|
3221
|
+
float tight = 2f * scale;
|
|
3222
|
+
float round = 7f * scale;
|
|
3223
|
+
float tl = (!flipX && !flipY) ? tight : round;
|
|
3224
|
+
float tr = (flipX && !flipY) ? tight : round;
|
|
3225
|
+
float br = (flipX && flipY) ? tight : round;
|
|
3226
|
+
float bl = (!flipX && flipY) ? tight : round;
|
|
3227
|
+
var p = new GraphicsPath();
|
|
3228
|
+
p.AddArc(r.X, r.Y, tl * 2f, tl * 2f, 180, 90);
|
|
3229
|
+
p.AddArc(r.Right - tr * 2f, r.Y, tr * 2f, tr * 2f, 270, 90);
|
|
3230
|
+
p.AddArc(r.Right - br * 2f, r.Bottom - br * 2f, br * 2f, br * 2f, 0, 90);
|
|
3231
|
+
p.AddArc(r.X, r.Bottom - bl * 2f, bl * 2f, bl * 2f, 90, 90);
|
|
3232
|
+
p.CloseFigure();
|
|
3233
|
+
return p;
|
|
3234
|
+
}
|
|
3235
|
+
|
|
3236
|
+
/**
|
|
3237
|
+
* Draw a sleek Codex-style collaborative AI pointer cursor whose tip sits
|
|
3238
|
+
* pixel-exact at (cx, cy): soft radial spotlight glow, crisp filled vector
|
|
3239
|
+
* arrow with white outline, attached rounded name-tag pill (auto-flipping
|
|
3240
|
+
* at all 4 screen edges), and a subtle click ripple ring on post-click.
|
|
3241
|
+
*/
|
|
3242
|
+
static Bitmap DrawMark(string text, Color color, float scale, bool flipX, bool flipY, bool ripple, out int cx, out int cy)
|
|
2893
3243
|
{
|
|
2894
|
-
float s = Math.Max(0.75f, scale);
|
|
3244
|
+
float s = Math.Max(0.75f, Math.Min(3f, scale));
|
|
2895
3245
|
float px = 1.333f * s;
|
|
2896
|
-
float
|
|
2897
|
-
float arm = 25f * s;
|
|
2898
|
-
float gap = 6f * s;
|
|
3246
|
+
float glowR = (ripple ? 20f : 16f) * s;
|
|
2899
3247
|
float pad = 4f * s;
|
|
2900
|
-
|
|
2901
|
-
using (var font = new Font("Microsoft YaHei UI", 8.5f * px, FontStyle.Bold, GraphicsUnit.Pixel))
|
|
3248
|
+
using (var font = new Font("Microsoft YaHei UI", 8f * px, FontStyle.Bold, GraphicsUnit.Pixel))
|
|
2902
3249
|
using (var probe = new Bitmap(1, 1))
|
|
2903
3250
|
using (var mg = Graphics.FromImage(probe))
|
|
2904
3251
|
using (var fmt = new StringFormat(StringFormat.GenericTypographic))
|
|
@@ -2906,15 +3253,23 @@ namespace DshComputerUse
|
|
|
2906
3253
|
mg.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
|
|
2907
3254
|
fmt.FormatFlags |= StringFormatFlags.MeasureTrailingSpaces | StringFormatFlags.NoWrap;
|
|
2908
3255
|
SizeF textSize = string.IsNullOrEmpty(text) ? SizeF.Empty : mg.MeasureString(text, font, 10000, fmt);
|
|
2909
|
-
float pillPadX =
|
|
2910
|
-
float pillW = textSize.Width > 0 ? textSize.Width + 2f * pillPadX : 0f;
|
|
2911
|
-
float pillH = textSize.Height > 0 ? textSize.Height +
|
|
2912
|
-
|
|
2913
|
-
|
|
2914
|
-
|
|
2915
|
-
|
|
2916
|
-
|
|
2917
|
-
|
|
3256
|
+
float pillPadX = 7f * s;
|
|
3257
|
+
float pillW = textSize.Width > 0 ? (float)Math.Ceiling(textSize.Width + 2f * pillPadX) : 0f;
|
|
3258
|
+
float pillH = textSize.Height > 0 ? Math.Max(19f * s, (float)Math.Ceiling(textSize.Height + 5f * s)) : 0f;
|
|
3259
|
+
|
|
3260
|
+
// Offsets relative to the pointer tip at (0, 0).
|
|
3261
|
+
float tagX = flipX ? (-10f * s - pillW) : (13f * s);
|
|
3262
|
+
float tagY = flipY ? (-10f * s - pillH) : (15f * s);
|
|
3263
|
+
|
|
3264
|
+
float minX = Math.Min(-glowR, pillW > 0 ? tagX : 0f) - pad;
|
|
3265
|
+
float maxX = Math.Max(glowR, Math.Max(14f * s, pillW > 0 ? tagX + pillW : 0f)) + pad;
|
|
3266
|
+
float minY = Math.Min(-glowR, pillH > 0 ? tagY : 0f) - pad;
|
|
3267
|
+
float maxY = Math.Max(glowR, Math.Max(22f * s, pillH > 0 ? tagY + pillH : 0f)) + pad;
|
|
3268
|
+
|
|
3269
|
+
int W = Math.Max(1, (int)Math.Ceiling(maxX - minX));
|
|
3270
|
+
int H = Math.Max(1, (int)Math.Ceiling(maxY - minY));
|
|
3271
|
+
cx = (int)Math.Round(-minX);
|
|
3272
|
+
cy = (int)Math.Round(-minY);
|
|
2918
3273
|
|
|
2919
3274
|
var bmp = new Bitmap(W, H, PixelFormat.Format32bppArgb);
|
|
2920
3275
|
using (var g = Graphics.FromImage(bmp))
|
|
@@ -2923,42 +3278,81 @@ namespace DshComputerUse
|
|
|
2923
3278
|
g.SmoothingMode = SmoothingMode.AntiAlias;
|
|
2924
3279
|
g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit;
|
|
2925
3280
|
|
|
2926
|
-
// Soft
|
|
2927
|
-
using (var
|
|
2928
|
-
|
|
2929
|
-
|
|
2930
|
-
|
|
2931
|
-
|
|
2932
|
-
|
|
3281
|
+
// 1. Soft radial spotlight centered right under the tip (cx, cy).
|
|
3282
|
+
using (var spot = new GraphicsPath())
|
|
3283
|
+
{
|
|
3284
|
+
spot.AddEllipse(cx - glowR, cy - glowR, glowR * 2f, glowR * 2f);
|
|
3285
|
+
using (var glow = new PathGradientBrush(spot))
|
|
3286
|
+
{
|
|
3287
|
+
glow.CenterPoint = new PointF(cx, cy);
|
|
3288
|
+
glow.CenterColor = Color.FromArgb(ripple ? 130 : 95, color);
|
|
3289
|
+
glow.SurroundColors = new Color[] { Color.FromArgb(0, color) };
|
|
3290
|
+
g.FillPath(glow, spot);
|
|
3291
|
+
}
|
|
3292
|
+
}
|
|
3293
|
+
|
|
3294
|
+
// 2. Post-click delicate ripple ring so exact placement reads cleanly in screenshots.
|
|
3295
|
+
if (ripple)
|
|
2933
3296
|
{
|
|
2934
|
-
|
|
2935
|
-
|
|
2936
|
-
|
|
2937
|
-
|
|
2938
|
-
|
|
3297
|
+
float rr = 11.5f * s;
|
|
3298
|
+
using (var ringHalo = new Pen(Color.FromArgb(140, 255, 255, 255), Math.Max(2.5f, 3.2f * s)))
|
|
3299
|
+
g.DrawEllipse(ringHalo, cx - rr, cy - rr, rr * 2f, rr * 2f);
|
|
3300
|
+
using (var ring = new Pen(Color.FromArgb(230, color), Math.Max(1.6f, 2f * s)))
|
|
3301
|
+
g.DrawEllipse(ring, cx - rr, cy - rr, rr * 2f, rr * 2f);
|
|
2939
3302
|
}
|
|
2940
|
-
float dotR = Math.Max(2f, 2.4f * s);
|
|
2941
|
-
using (var dot = new SolidBrush(Color.FromArgb(245, color)))
|
|
2942
|
-
g.FillEllipse(dot, cx - dotR, cy - dotR, dotR * 2f, dotR * 2f);
|
|
2943
3303
|
|
|
3304
|
+
// 3. Attached collaborative name-tag pill.
|
|
2944
3305
|
if (pillW > 0 && pillH > 0)
|
|
2945
3306
|
{
|
|
2946
|
-
float
|
|
2947
|
-
|
|
2948
|
-
|
|
3307
|
+
float bx = cx + tagX;
|
|
3308
|
+
float by = cy + tagY;
|
|
3309
|
+
var shadowRect = new RectangleF(bx, by + 1.5f * s, pillW, pillH);
|
|
3310
|
+
using (var sp = TagShape(shadowRect, flipX, flipY, s))
|
|
3311
|
+
using (var sb = new SolidBrush(Color.FromArgb(62, 0, 0, 0)))
|
|
3312
|
+
g.FillPath(sb, sp);
|
|
3313
|
+
|
|
3314
|
+
var pillRect = new RectangleF(bx, by, pillW, pillH);
|
|
3315
|
+
using (var pp = TagShape(pillRect, flipX, flipY, s))
|
|
2949
3316
|
{
|
|
2950
|
-
using (var
|
|
2951
|
-
using (var border = new Pen(Color.FromArgb(
|
|
3317
|
+
using (var fill = new SolidBrush(Color.FromArgb(245, color))) g.FillPath(fill, pp);
|
|
3318
|
+
using (var border = new Pen(Color.FromArgb(235, 255, 255, 255), Math.Max(1f, 1.25f * s))) g.DrawPath(border, pp);
|
|
2952
3319
|
}
|
|
2953
3320
|
using (var fg = new SolidBrush(Color.White))
|
|
2954
|
-
g.DrawString(text, font, fg,
|
|
3321
|
+
g.DrawString(text, font, fg, bx + (pillW - textSize.Width) / 2f, by + (pillH - textSize.Height) / 2f - 0.5f * s, fmt);
|
|
3322
|
+
}
|
|
3323
|
+
|
|
3324
|
+
// 4. Crisp vector cursor arrow whose tip sits pixel-exact at (cx, cy).
|
|
3325
|
+
var arrow = new PointF[]
|
|
3326
|
+
{
|
|
3327
|
+
new PointF(cx, cy),
|
|
3328
|
+
new PointF(cx, cy + 16.5f * s),
|
|
3329
|
+
new PointF(cx + 4.3f * s, cy + 12.8f * s),
|
|
3330
|
+
new PointF(cx + 7.4f * s, cy + 19.2f * s),
|
|
3331
|
+
new PointF(cx + 10.0f * s, cy + 17.9f * s),
|
|
3332
|
+
new PointF(cx + 6.9f * s, cy + 11.7f * s),
|
|
3333
|
+
new PointF(cx + 12.4f * s, cy + 11.4f * s),
|
|
3334
|
+
};
|
|
3335
|
+
var arrowShadow = new PointF[arrow.Length];
|
|
3336
|
+
for (int i = 0; i < arrow.Length; i++)
|
|
3337
|
+
arrowShadow[i] = new PointF(arrow[i].X + 0.8f * s, arrow[i].Y + 1.5f * s);
|
|
3338
|
+
using (var sb = new SolidBrush(Color.FromArgb(72, 0, 0, 0))) g.FillPolygon(sb, arrowShadow);
|
|
3339
|
+
using (var fill = new SolidBrush(Color.FromArgb(255, color))) g.FillPolygon(fill, arrow);
|
|
3340
|
+
using (var outline = new Pen(Color.FromArgb(248, 255, 255, 255), Math.Max(1.2f, 1.5f * s)))
|
|
3341
|
+
{
|
|
3342
|
+
outline.LineJoin = LineJoin.Round;
|
|
3343
|
+
g.DrawPolygon(outline, arrow);
|
|
2955
3344
|
}
|
|
2956
3345
|
}
|
|
2957
3346
|
return bmp;
|
|
2958
3347
|
}
|
|
2959
3348
|
}
|
|
2960
3349
|
|
|
2961
|
-
public static void
|
|
3350
|
+
public static void MarkPointer(int x, int y, string text, int holdMs)
|
|
3351
|
+
{
|
|
3352
|
+
Mark(x, y, text, Accent, holdMs, false, false);
|
|
3353
|
+
}
|
|
3354
|
+
|
|
3355
|
+
public static void Mark(int x, int y, string text, Color color, int holdMs, bool capturable, bool ripple)
|
|
2962
3356
|
{
|
|
2963
3357
|
Ensure();
|
|
2964
3358
|
Ui(delegate
|
|
@@ -2971,9 +3365,12 @@ namespace DshComputerUse
|
|
|
2971
3365
|
markForm.SetCapturable(capturable || !ExcludeFromCapture);
|
|
2972
3366
|
float scale; Rectangle mon;
|
|
2973
3367
|
MonitorAt(x, y, out scale, out mon);
|
|
2974
|
-
|
|
3368
|
+
float s = Math.Max(0.75f, Math.Min(3f, scale));
|
|
3369
|
+
int estW = (int)((Math.Max(6, (text ?? "").Length) * 11 + 36) * s);
|
|
3370
|
+
bool flipX = !mon.IsEmpty && x + estW > mon.Right;
|
|
3371
|
+
bool flipY = !mon.IsEmpty && y + (int)(52 * s) > mon.Bottom;
|
|
2975
3372
|
int cx, cy;
|
|
2976
|
-
using (var bmp = DrawMark(text, color, scale,
|
|
3373
|
+
using (var bmp = DrawMark(text, color, scale, flipX, flipY, ripple, out cx, out cy))
|
|
2977
3374
|
{
|
|
2978
3375
|
markForm.SetBitmap(bmp, new Rectangle(x - cx, y - cy, bmp.Width, bmp.Height), 255);
|
|
2979
3376
|
}
|
|
@@ -2989,11 +3386,11 @@ namespace DshComputerUse
|
|
|
2989
3386
|
Color color;
|
|
2990
3387
|
string verb;
|
|
2991
3388
|
if (count >= 3) { color = Color.FromArgb(249, 115, 22); verb = "已三击"; }
|
|
2992
|
-
else if (count == 2) { color = Color.FromArgb(
|
|
2993
|
-
else if (button == "right") { color = Color.FromArgb(
|
|
3389
|
+
else if (count == 2) { color = Color.FromArgb(139, 92, 246); verb = "已双击"; }
|
|
3390
|
+
else if (button == "right") { color = Color.FromArgb(16, 185, 129); verb = "已右键"; }
|
|
2994
3391
|
else if (button == "middle") { color = Color.FromArgb(6, 182, 212); verb = "已中键"; }
|
|
2995
3392
|
else { color = Color.FromArgb(59, 130, 246); verb = "已单击"; }
|
|
2996
|
-
Mark(x, y, verb + " " + coordLabel, color, 4000, true);
|
|
3393
|
+
Mark(x, y, verb + " " + coordLabel, color, 4000, true, true);
|
|
2997
3394
|
}
|
|
2998
3395
|
|
|
2999
3396
|
static void HideMark()
|