@copylee/dsh-computer-use 0.11.0 → 0.12.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.
@@ -128,11 +128,15 @@ namespace DshComputerUse
128
128
  [DllImport("user32.dll")] public static extern bool GetGUIThreadInfo(uint idThread, ref GUITHREADINFO lpgui);
129
129
 
130
130
  public delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
131
+ public delegate IntPtr LowLevelMouseProc(int nCode, IntPtr wParam, IntPtr lParam);
131
132
  [DllImport("user32.dll", SetLastError = true)] public static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc fn, IntPtr hMod, uint threadId);
133
+ [DllImport("user32.dll", SetLastError = true)] public static extern IntPtr SetWindowsHookEx(int idHook, LowLevelMouseProc fn, IntPtr hMod, uint threadId);
132
134
  [DllImport("user32.dll")] public static extern bool UnhookWindowsHookEx(IntPtr hook);
133
135
  [DllImport("user32.dll")] public static extern IntPtr CallNextHookEx(IntPtr hook, int nCode, IntPtr wParam, IntPtr lParam);
134
136
  [StructLayout(LayoutKind.Sequential)]
135
137
  public struct KBDLLHOOKSTRUCT { public uint vkCode; public uint scanCode; public uint flags; public uint time; public IntPtr dwExtraInfo; }
138
+ [StructLayout(LayoutKind.Sequential)]
139
+ public struct MSLLHOOKSTRUCT { public POINT pt; public uint mouseData; public uint flags; public uint time; public IntPtr dwExtraInfo; }
136
140
 
137
141
  public const uint INPUT_MOUSE = 0, INPUT_KEYBOARD = 1;
138
142
  public const uint KEYEVENTF_EXTENDEDKEY = 0x1, KEYEVENTF_KEYUP = 0x2, KEYEVENTF_UNICODE = 0x4;
@@ -269,6 +273,17 @@ namespace DshComputerUse
269
273
  try { Emit(e); } catch (Exception) { }
270
274
  });
271
275
  }
276
+
277
+ public static void EmitDataEvent(string name, Dictionary<string, object> data)
278
+ {
279
+ ThreadPool.QueueUserWorkItem(delegate
280
+ {
281
+ var e = new Dictionary<string, object>();
282
+ e["event"] = name;
283
+ e["data"] = data;
284
+ try { Emit(e); } catch (Exception) { }
285
+ });
286
+ }
272
287
  }
273
288
 
274
289
  internal static class Commands
@@ -2965,6 +2980,58 @@ namespace DshComputerUse
2965
2980
  return d;
2966
2981
  }
2967
2982
 
2983
+ /** Lightweight bounded lookup of the UIA control at physical point (x, y) for user-takeover feedback. */
2984
+ public static void DescribeAtPoint(int x, int y, out string role, out string name, out bool isPassword)
2985
+ {
2986
+ string r = "", n = "";
2987
+ bool pwd = false;
2988
+ var worker = new Thread(delegate ()
2989
+ {
2990
+ try
2991
+ {
2992
+ var el = AutomationElement.FromPoint(new System.Windows.Point(x, y));
2993
+ if (el == null) return;
2994
+ pwd = IsPasswordElement(el);
2995
+ var ct = el.Current.ControlType;
2996
+ r = pwd ? "PasswordField" : (ct != null ? ct.ProgrammaticName.Replace("ControlType.", "") : "");
2997
+ string raw = pwd ? "" : (el.Current.Name ?? "").Trim();
2998
+ n = raw.Length > 60 ? raw.Substring(0, 60) + "…" : raw;
2999
+ }
3000
+ catch (Exception) { }
3001
+ });
3002
+ worker.IsBackground = true;
3003
+ worker.SetApartmentState(ApartmentState.MTA);
3004
+ worker.Start();
3005
+ worker.Join(260);
3006
+ role = r; name = n; isPassword = pwd;
3007
+ }
3008
+
3009
+ /** Lightweight bounded lookup of the focused UIA control for privacy-safe typing feedback. */
3010
+ public static void DescribeFocused(out string role, out string name, out bool isPassword)
3011
+ {
3012
+ string r = "", n = "";
3013
+ bool pwd = false;
3014
+ var worker = new Thread(delegate ()
3015
+ {
3016
+ try
3017
+ {
3018
+ var el = AutomationElement.FocusedElement;
3019
+ if (el == null) return;
3020
+ pwd = IsPasswordElement(el);
3021
+ var ct = el.Current.ControlType;
3022
+ r = pwd ? "PasswordField" : (ct != null ? ct.ProgrammaticName.Replace("ControlType.", "") : "");
3023
+ string raw = pwd ? "" : (el.Current.Name ?? "").Trim();
3024
+ n = raw.Length > 60 ? raw.Substring(0, 60) + "…" : raw;
3025
+ }
3026
+ catch (Exception) { }
3027
+ });
3028
+ worker.IsBackground = true;
3029
+ worker.SetApartmentState(ApartmentState.MTA);
3030
+ worker.Start();
3031
+ worker.Join(240);
3032
+ role = r; name = n; isPassword = pwd;
3033
+ }
3034
+
2968
3035
  /**
2969
3036
  * Try to invoke/toggle/select an enabled native UIA control directly under
2970
3037
  * physical point (x, y) without moving the physical cursor or stealing focus.
@@ -3362,6 +3429,14 @@ namespace DshComputerUse
3362
3429
  static DateTime shownAt;
3363
3430
  static IntPtr hook = IntPtr.Zero;
3364
3431
  static Native.LowLevelKeyboardProc hookProc;
3432
+ static IntPtr mouseHook = IntPtr.Zero;
3433
+ static Native.LowLevelMouseProc mouseHookProc;
3434
+ static readonly object UserActionLock = new object();
3435
+ static int lastClickTick = int.MinValue / 2;
3436
+ static int lastClickX, lastClickY;
3437
+ static int lastScrollTick = int.MinValue / 2;
3438
+ static int lastKeySummaryTick = int.MinValue / 2;
3439
+ static int keyBurstCount;
3365
3440
  static volatile bool visible;
3366
3441
  static volatile bool paused;
3367
3442
  static volatile bool clickTargetEnabled = true;
@@ -3374,6 +3449,9 @@ namespace DshComputerUse
3374
3449
 
3375
3450
  // The progress card ("pet"): replaces the pill while DeepSeek Harness is minimized.
3376
3451
  static bool petMode;
3452
+ static string petSize = "standard";
3453
+ static string hoveredBtn = "";
3454
+ static string pressedBtn = "";
3377
3455
  static LayeredForm pet;
3378
3456
  public static volatile IntPtr PetHwnd = IntPtr.Zero;
3379
3457
  static string petThinking = "";
@@ -3451,10 +3529,14 @@ namespace DshComputerUse
3451
3529
  catch (Exception) { icon = null; }
3452
3530
  }
3453
3531
  petAlpha = (byte)Math.Max(90, Math.Min(255, Args.Int(r, "petOpacity", 90) * 255 / 100));
3532
+ string reqSize = Args.Str(r, "petSize", "");
3533
+ if (reqSize == "compact" || reqSize == "standard" || reqSize == "detailed") petSize = reqSize;
3454
3534
  clickTargetEnabled = Args.Bool(r, "glow", true);
3455
3535
  if (visible && !petFinished) { RenderPill(); return; }
3456
3536
  if (visible) HideCore(); // the previous run's "done" card is still up
3457
3537
  petMode = Args.Bool(r, "pet", false);
3538
+ hoveredBtn = "";
3539
+ pressedBtn = "";
3458
3540
  petThinking = Args.Str(r, "thinking", "");
3459
3541
  petSteps = new List<string>();
3460
3542
  if (status.Length > 0) petSteps.Add(status);
@@ -3478,10 +3560,38 @@ namespace DshComputerUse
3478
3560
  if (pet == null)
3479
3561
  {
3480
3562
  pet = new LayeredForm(false);
3563
+ pet.MouseMove += delegate (object s, MouseEventArgs e)
3564
+ {
3565
+ string hit = "";
3566
+ foreach (var b in buttons) if (b.Key.Contains(e.Location)) { hit = b.Value; break; }
3567
+ pet.Cursor = hit.Length > 0 ? Cursors.Hand : Cursors.SizeAll;
3568
+ if (hit != hoveredBtn)
3569
+ {
3570
+ hoveredBtn = hit;
3571
+ RenderPill();
3572
+ }
3573
+ };
3574
+ pet.MouseLeave += delegate
3575
+ {
3576
+ if (hoveredBtn.Length > 0 || pressedBtn.Length > 0)
3577
+ {
3578
+ hoveredBtn = "";
3579
+ pressedBtn = "";
3580
+ RenderPill();
3581
+ }
3582
+ };
3481
3583
  pet.MouseDown += delegate (object s, MouseEventArgs e)
3482
3584
  {
3483
3585
  if (e.Button != MouseButtons.Left) return;
3484
- foreach (var b in buttons) if (b.Key.Contains(e.Location)) return;
3586
+ foreach (var b in buttons)
3587
+ {
3588
+ if (b.Key.Contains(e.Location))
3589
+ {
3590
+ pressedBtn = b.Value;
3591
+ RenderPill();
3592
+ return;
3593
+ }
3594
+ }
3485
3595
  // Drag the card anywhere; it keeps that place until the next run.
3486
3596
  Native.ReleaseCapture();
3487
3597
  Native.SendMessage(pet.Handle, 0xA1, new IntPtr(2), IntPtr.Zero); // WM_NCLBUTTONDOWN, HTCAPTION
@@ -3492,9 +3602,12 @@ namespace DshComputerUse
3492
3602
  };
3493
3603
  pet.MouseUp += delegate (object s, MouseEventArgs e)
3494
3604
  {
3605
+ bool hadPressed = pressedBtn.Length > 0;
3606
+ pressedBtn = "";
3495
3607
  if (petDragged) { petDragged = false; return; }
3496
3608
  foreach (var b in buttons)
3497
- if (b.Key.Contains(e.Location)) { Press(b.Value); break; }
3609
+ if (b.Key.Contains(e.Location)) { Press(b.Value); return; }
3610
+ if (hadPressed) RenderPill();
3498
3611
  };
3499
3612
  pet.Show();
3500
3613
  PetHwnd = pet.Handle;
@@ -4067,6 +4180,18 @@ namespace DshComputerUse
4067
4180
  return "…" + s.Substring(Math.Min(lo, s.Length));
4068
4181
  }
4069
4182
 
4183
+ static string ButtonTip(string act)
4184
+ {
4185
+ if (act == "stop") return "停止本轮";
4186
+ if (act == "pause") return "暂停操控";
4187
+ if (act == "resume") return "继续操控";
4188
+ if (act == "expand") return petExpanded ? "收起详情" : "展开详情";
4189
+ if (act == "input") return input != null ? "关闭输入框" : "输入新指令";
4190
+ if (act == "close") return "关闭卡片";
4191
+ if (act == "send") return "发送指令";
4192
+ return "";
4193
+ }
4194
+
4070
4195
  static void RenderPet()
4071
4196
  {
4072
4197
  if (pet == null) return;
@@ -4081,15 +4206,22 @@ namespace DshComputerUse
4081
4206
  }
4082
4207
  rendered = Key();
4083
4208
  bool dark = DarkTheme();
4084
- Color bg = dark ? Color.FromArgb(petAlpha, 32, 31, 30) : Color.FromArgb(petAlpha, 252, 252, 251);
4209
+ Color bg = dark ? Color.FromArgb(petAlpha, 30, 30, 32) : Color.FromArgb(petAlpha, 252, 252, 251);
4085
4210
  Color ink = dark ? Color.FromArgb(244, 243, 241) : Color.FromArgb(31, 30, 29);
4086
4211
  Color muted = dark ? Color.FromArgb(168, 165, 160) : Color.FromArgb(112, 110, 106);
4087
- Color edge = dark ? Color.FromArgb(60, 255, 255, 255) : Color.FromArgb(34, 0, 0, 0);
4088
- Color chip = dark ? Color.FromArgb(70, 68, 66) : Color.FromArgb(234, 232, 228);
4089
- Color field = dark ? Color.FromArgb(52, 51, 49) : Color.White;
4090
-
4091
- string title = petFinished ? "已完成" : label;
4092
- string state = petFinished ? "" : paused ? (input != null ? "输入指令中…" : "已暂停 · Esc 继续") : Typing() ? "你正在操作,已暂停" : "Esc 暂停";
4212
+ Color edge = dark ? Color.FromArgb(64, 255, 255, 255) : Color.FromArgb(38, 0, 0, 0);
4213
+ Color chip = dark ? Color.FromArgb(64, 63, 66) : Color.FromArgb(236, 234, 230);
4214
+ Color chipHover = dark ? Color.FromArgb(92, 86, 82) : Color.FromArgb(224, 214, 206);
4215
+ Color chipPress = dark ? Color.FromArgb(110, 96, 88) : Color.FromArgb(212, 198, 188);
4216
+ Color field = dark ? Color.FromArgb(50, 50, 52) : Color.White;
4217
+
4218
+ bool isCompact = petSize == "compact";
4219
+ bool isDetailed = petSize == "detailed";
4220
+ string title = petFinished ? "已完成" : (isCompact && label.Length > 12 ? label.Substring(0, 11) + "…" : label);
4221
+ string hoverTip = ButtonTip(hoveredBtn);
4222
+ string state = hoverTip.Length > 0 ? hoverTip
4223
+ : petFinished ? "" : paused ? (input != null ? "输入指令中…" : "已暂停 · Esc 继续")
4224
+ : Typing() ? "你正在操作,已暂停" : (isCompact ? "" : "Esc 暂停");
4093
4225
  // Header buttons, right to left.
4094
4226
  var actions = new List<string>(); var glyphs = new List<string>();
4095
4227
  if (petFinished) { actions.Add("close"); glyphs.Add("\uE711"); }
@@ -4104,24 +4236,29 @@ namespace DshComputerUse
4104
4236
  float s = scale, px = 1.333f * scale;
4105
4237
  int shadow = (int)(14 * s);
4106
4238
  petShadow = shadow;
4107
- float bodyW = 420 * s, pad = 16 * s;
4108
- float headH = 44 * s, replyH = 26 * s;
4239
+ float bodyW = (isCompact ? 320f : isDetailed ? 484f : 420f) * s;
4240
+ float pad = (isCompact ? 13f : 16f) * s;
4241
+ float headH = (isCompact ? 40f : 44f) * s;
4242
+ bool showThinking = !isCompact || petExpanded;
4243
+ bool showTimeline = petExpanded || isDetailed;
4244
+ float replyH = showThinking ? (isDetailed && !petExpanded ? 42f * s : 26f * s) : 0f;
4109
4245
  string progress = petFinished ? "已交还电脑控制" : petThinking.Length > 0 ? "思考 · " + petThinking : "正在操作,等待下一步…";
4110
- if (petExpanded)
4246
+ if (showThinking && (petExpanded || isDetailed))
4111
4247
  {
4112
- // Grow with the reply, up to about eight lines.
4113
4248
  using (var probe = new Bitmap(1, 1))
4114
4249
  using (var pg = Graphics.FromImage(probe))
4115
- using (var pf = new Font("Microsoft YaHei UI", 10.5f * 1.333f * scale, FontStyle.Regular, GraphicsUnit.Pixel))
4250
+ using (var pf = new Font("Microsoft YaHei UI", (isCompact ? 9.5f : 10.5f) * 1.333f * scale, FontStyle.Regular, GraphicsUnit.Pixel))
4116
4251
  {
4117
- float need = pg.MeasureString(progress, pf, new SizeF(420 * scale - 32 * scale, 100000), new StringFormat(StringFormat.GenericTypographic)).Height + 6 * scale;
4118
- replyH = Math.Max(26 * scale, Math.Min(172 * scale, need));
4252
+ float need = pg.MeasureString(progress, pf, new SizeF(bodyW - 2 * pad, 100000), new StringFormat(StringFormat.GenericTypographic)).Height + 6 * scale;
4253
+ float cap = petExpanded ? (isDetailed ? 210f : 172f) * scale : 44f * scale;
4254
+ replyH = Math.Max(26f * scale, Math.Min(cap, need));
4119
4255
  }
4120
4256
  }
4121
- int stepRows = petExpanded ? Math.Min(5, Math.Max(1, petSteps.Count)) : 0;
4122
- float listH = petExpanded ? 8 * s + stepRows * 22 * s : 24 * s;
4257
+ int maxSteps = petExpanded ? (isDetailed ? 8 : 5) : (isDetailed ? 3 : 0);
4258
+ int stepRows = showTimeline ? Math.Min(maxSteps, Math.Max(1, petSteps.Count)) : 0;
4259
+ float listH = showTimeline ? 10 * s + stepRows * 22 * s : 24 * s;
4123
4260
  float inputH = input != null ? 46 * s : 0;
4124
- float bodyH = headH + replyH + listH + inputH + 12 * s;
4261
+ float bodyH = headH + replyH + listH + inputH + (isCompact ? 10f : 12f) * s;
4125
4262
  int W = (int)Math.Ceiling(bodyW) + 2 * shadow, H = (int)Math.Ceiling(bodyH) + 2 * shadow;
4126
4263
  if (petAnchor.IsEmpty) petAnchor = new Point(work.Right - (int)(16 * s), work.Bottom - (int)(16 * s));
4127
4264
  int x = petAnchor.X - (int)bodyW - shadow, y = petAnchor.Y - (int)bodyH - shadow;
@@ -4130,8 +4267,8 @@ namespace DshComputerUse
4130
4267
 
4131
4268
  string glyphFont = IconFont();
4132
4269
  using (var fHead = new Font("Microsoft YaHei UI", 8.5f * px, FontStyle.Bold, GraphicsUnit.Pixel))
4133
- using (var fState = new Font("Microsoft YaHei UI", 8f * px, FontStyle.Regular, GraphicsUnit.Pixel))
4134
- using (var fText = new Font("Microsoft YaHei UI", 10.5f * px, FontStyle.Regular, GraphicsUnit.Pixel))
4270
+ using (var fState = new Font("Microsoft YaHei UI", 8.2f * px, FontStyle.Regular, GraphicsUnit.Pixel))
4271
+ using (var fText = new Font("Microsoft YaHei UI", (isCompact ? 9.5f : 10.2f) * px, FontStyle.Regular, GraphicsUnit.Pixel))
4135
4272
  using (var fAct = new Font("Microsoft YaHei UI", 8.5f * px, FontStyle.Regular, GraphicsUnit.Pixel))
4136
4273
  using (var fGlyph = new Font(glyphFont.Length > 0 ? glyphFont : "Segoe UI Symbol", 8.5f * px, FontStyle.Regular, GraphicsUnit.Pixel))
4137
4274
  using (var bmp = new Bitmap(W, H, PixelFormat.Format32bppArgb))
@@ -4148,7 +4285,7 @@ namespace DshComputerUse
4148
4285
  var center = new StringFormat(); center.Alignment = StringAlignment.Center; center.LineAlignment = StringAlignment.Center;
4149
4286
 
4150
4287
  var body = new RectangleF(shadow, shadow, bodyW, bodyH);
4151
- float radius = 18 * s;
4288
+ float radius = (isCompact ? 15f : 18f) * s;
4152
4289
  for (int i = shadow; i > 0; i--)
4153
4290
  {
4154
4291
  double f = 1.0 - (double)i / shadow;
@@ -4158,11 +4295,11 @@ namespace DshComputerUse
4158
4295
  using (var path = Rounded(body, radius))
4159
4296
  {
4160
4297
  using (var brush = new SolidBrush(bg)) g.FillPath(brush, path);
4161
- using (var pen = new Pen(edge, Math.Max(1f, s))) g.DrawPath(pen, path);
4298
+ using (var pen = new Pen(petFinished ? Color.FromArgb(145, 46, 160, 67) : edge, Math.Max(1f, s))) g.DrawPath(pen, path);
4162
4299
  }
4163
4300
 
4164
- // Header: logo, title, state, round icon buttons.
4165
- float hy = body.Y + 10 * s, hh = 28 * s;
4301
+ // Header: logo, title, state / button hover tooltip, tactile round icon buttons.
4302
+ float hy = body.Y + (isCompact ? 8f : 10f) * s, hh = 26 * s;
4166
4303
  float iconSize = 18 * s;
4167
4304
  var iconRect = new RectangleF(body.X + pad, hy + (hh - iconSize) / 2f, iconSize, iconSize);
4168
4305
  Image logo = dark ? icon : (iconLight ?? icon);
@@ -4179,56 +4316,85 @@ namespace DshComputerUse
4179
4316
  tx += headSize.Width + 8 * s;
4180
4317
 
4181
4318
  buttons.Clear();
4182
- float d = 26 * s, bx = body.Right - pad;
4319
+ float d = (isCompact ? 24f : 26f) * s, bx = body.Right - pad;
4183
4320
  for (int i = 0; i < actions.Count; i++)
4184
4321
  {
4322
+ string act = actions[i];
4185
4323
  bx -= d;
4186
- var circle = new RectangleF(bx, hy + (hh - d) / 2f, d, d);
4187
- bool stop = actions[i] == "stop";
4188
- bool on = (actions[i] == "input" && input != null) || (actions[i] == "resume");
4189
- using (var brush = new SolidBrush(stop ? Accent : on ? Color.FromArgb(dark ? 110 : 60, Accent) : chip)) g.FillEllipse(brush, circle);
4190
- string glyph = glyphFont.Length > 0 ? glyphs[i] : (stop ? "■" : actions[i] == "pause" ? "❚❚" : actions[i] == "resume" ? "▶" : actions[i] == "expand" ? (petExpanded ? "˅" : "˄") : actions[i] == "close" ? "✕" : "✎");
4191
- using (var b = new SolidBrush(stop ? Color.White : ink)) g.DrawString(glyph, fGlyph, b, circle, center);
4192
- buttons.Add(new KeyValuePair<Rectangle, string>(Rectangle.Round(circle), actions[i]));
4324
+ var hitRect = new RectangleF(bx, hy + (hh - d) / 2f, d, d);
4325
+ bool stop = act == "stop";
4326
+ bool on = (act == "input" && input != null) || (act == "resume");
4327
+ bool hover = hoveredBtn == act;
4328
+ bool press = pressedBtn == act;
4329
+ float inflate = press ? -0.8f * s : hover ? 1.1f * s : 0f;
4330
+ var circle = RectangleF.Inflate(hitRect, inflate, inflate);
4331
+ if (hover && stop)
4332
+ {
4333
+ using (var halo = new SolidBrush(Color.FromArgb(65, Accent)))
4334
+ g.FillEllipse(halo, RectangleF.Inflate(circle, 2.6f * s, 2.6f * s));
4335
+ }
4336
+ Color btnFill = stop
4337
+ ? (press ? Color.FromArgb(190, 92, 62) : hover ? AccentHi : Accent)
4338
+ : on
4339
+ ? Color.FromArgb(dark ? (hover ? 145 : 115) : (hover ? 95 : 65), Accent)
4340
+ : (press ? chipPress : hover ? chipHover : chip);
4341
+ using (var brush = new SolidBrush(btnFill)) g.FillEllipse(brush, circle);
4342
+ if (hover && !stop)
4343
+ {
4344
+ using (var ring = new Pen(Color.FromArgb(dark ? 150 : 120, Accent), Math.Max(1f, 1.2f * s)))
4345
+ g.DrawEllipse(ring, circle);
4346
+ }
4347
+ string glyph = glyphFont.Length > 0 ? glyphs[i] : (stop ? "■" : act == "pause" ? "❚❚" : act == "resume" ? "▶" : act == "expand" ? (petExpanded ? "˅" : "˄") : act == "close" ? "✕" : "✎");
4348
+ Color glyphColor = stop ? Color.White : (hover || on) ? (dark ? Color.White : Color.FromArgb(168, 72, 42)) : ink;
4349
+ using (var b = new SolidBrush(glyphColor)) g.DrawString(glyph, fGlyph, b, circle, center);
4350
+ buttons.Add(new KeyValuePair<Rectangle, string>(Rectangle.Round(hitRect), act));
4193
4351
  bx -= 6 * s;
4194
4352
  }
4195
4353
  if (state.Length > 0)
4196
4354
  {
4197
4355
  string st = Tail(g, state, fState, Math.Max(0, bx - tx - 4 * s), fmt);
4198
4356
  SizeF stSize = g.MeasureString(st, fState, 100000, fmt);
4199
- using (var b = new SolidBrush(paused ? Accent : muted)) g.DrawString(st, fState, b, tx, hy + (hh - stSize.Height) / 2f, fmt);
4357
+ Color stColor = (hoverTip.Length > 0 || paused) ? Accent : muted;
4358
+ using (var b = new SolidBrush(stColor)) g.DrawString(st, fState, b, tx, hy + (hh - stSize.Height) / 2f, fmt);
4200
4359
  }
4201
4360
 
4202
4361
  // Reasoning and operations use separate rows throughout.
4203
4362
  float lineW = bodyW - 2 * pad;
4204
4363
  float ry = body.Y + headH;
4205
- using (var b = new SolidBrush(petThinking.Length > 0 && !petFinished ? ink : muted))
4364
+ if (showThinking)
4206
4365
  {
4207
- if (petExpanded)
4208
- g.DrawString(TailWrapped(g, progress, fText, lineW, replyH, wrap), fText, b, new RectangleF(body.X + pad, ry, lineW, replyH), wrap);
4209
- else
4366
+ using (var b = new SolidBrush(petThinking.Length > 0 && !petFinished ? ink : muted))
4210
4367
  {
4211
- string shown = petThinking.Length > 0 && !petFinished
4212
- ? "思考 · " + Tail(g, petThinking, fText, lineW - g.MeasureString("思考 · ", fText, 100000, fmt).Width, fmt)
4213
- : Tail(g, progress, fText, lineW, fmt);
4214
- SizeF size = g.MeasureString(shown.Length > 0 ? shown : " ", fText, 100000, fmt);
4215
- g.DrawString(shown, fText, b, body.X + pad, ry + (replyH - size.Height) / 2f, fmt);
4368
+ if (petExpanded || isDetailed)
4369
+ g.DrawString(TailWrapped(g, progress, fText, lineW, replyH, wrap), fText, b, new RectangleF(body.X + pad, ry, lineW, replyH), wrap);
4370
+ else
4371
+ {
4372
+ string shown = petThinking.Length > 0 && !petFinished
4373
+ ? "思考 · " + Tail(g, petThinking, fText, lineW - g.MeasureString("思考 · ", fText, 100000, fmt).Width, fmt)
4374
+ : Tail(g, progress, fText, lineW, fmt);
4375
+ SizeF size = g.MeasureString(shown.Length > 0 ? shown : " ", fText, 100000, fmt);
4376
+ g.DrawString(shown, fText, b, body.X + pad, ry + (replyH - size.Height) / 2f, fmt);
4377
+ }
4216
4378
  }
4217
4379
  }
4218
4380
 
4219
4381
  // Thinking and the step timeline.
4220
4382
  float ly = ry + replyH;
4221
- if (petExpanded)
4383
+ if (showTimeline)
4222
4384
  {
4223
4385
  ly += 4 * s;
4224
4386
  using (var pen = new Pen(edge, Math.Max(1f, s))) g.DrawLine(pen, body.X + pad, ly, body.Right - pad, ly);
4225
- ly += 4 * s;
4387
+ ly += 5 * s;
4226
4388
  int first = Math.Max(0, petSteps.Count - stepRows);
4227
4389
  for (int i = first; i < petSteps.Count; i++)
4228
4390
  {
4229
4391
  bool current = i == petSteps.Count - 1 && !petFinished;
4230
4392
  float cy = ly + 11 * s;
4231
- if (current) using (var brush = new SolidBrush(Accent)) g.FillEllipse(brush, body.X + pad + 2 * s, cy - 3 * s, 6 * s, 6 * s);
4393
+ if (current)
4394
+ {
4395
+ using (var halo = new SolidBrush(Color.FromArgb(60, Accent))) g.FillEllipse(halo, body.X + pad, cy - 5 * s, 10 * s, 10 * s);
4396
+ using (var brush = new SolidBrush(Accent)) g.FillEllipse(brush, body.X + pad + 2 * s, cy - 3 * s, 6 * s, 6 * s);
4397
+ }
4232
4398
  else using (var b = new SolidBrush(Color.FromArgb(46, 160, 67))) g.DrawString(glyphFont.Length > 0 ? "\uE73E" : "✓", fGlyph, b, new RectangleF(body.X + pad - 2 * s, ly, 14 * s, 22 * s), center);
4233
4399
  string line = Tail(g, petSteps[i], fAct, lineW - 16 * s, fmt);
4234
4400
  using (var b = new SolidBrush(current ? ink : muted)) g.DrawString(line, fAct, b, body.X + pad + 16 * s, ly + 3 * s, fmt);
@@ -4239,15 +4405,17 @@ namespace DshComputerUse
4239
4405
  {
4240
4406
  string act;
4241
4407
  if (petFinished) act = petSteps.Count > 0 ? "最后操作 · " + petSteps[petSteps.Count - 1] : "";
4408
+ else if (isCompact) act = petSteps.Count > 0 ? petSteps[petSteps.Count - 1] : "准备操作…";
4242
4409
  else
4243
4410
  {
4244
4411
  var parts = new List<string>();
4245
4412
  for (int i = Math.Max(0, petSteps.Count - 4); i < petSteps.Count; i++) parts.Add(i < petSteps.Count - 1 ? "✓ " + petSteps[i] : petSteps[i]);
4246
4413
  act = string.Join(" › ", parts);
4247
4414
  }
4415
+ if (!petFinished) using (var halo = new SolidBrush(Color.FromArgb(55, Accent))) g.FillEllipse(halo, body.X + pad - 2 * s, ly + 6 * s, 10 * s, 10 * s);
4248
4416
  using (var brush = new SolidBrush(petFinished ? Color.FromArgb(46, 160, 67) : Accent)) g.FillEllipse(brush, body.X + pad, ly + 8 * s, 6 * s, 6 * s);
4249
4417
  string shown = Tail(g, act, fAct, lineW - 14 * s, fmt);
4250
- using (var b = new SolidBrush(muted)) g.DrawString(shown, fAct, b, body.X + pad + 14 * s, ly + 3 * s, fmt);
4418
+ using (var b = new SolidBrush(isCompact ? ink : muted)) g.DrawString(shown, fAct, b, body.X + pad + 14 * s, ly + 3 * s, fmt);
4251
4419
  ly += 24 * s;
4252
4420
  }
4253
4421
 
@@ -4262,10 +4430,14 @@ namespace DshComputerUse
4262
4430
  using (var brush = new SolidBrush(field)) g.FillPath(brush, path);
4263
4431
  using (var pen = new Pen(Color.FromArgb(150, Accent), Math.Max(1f, 1.2f * s))) g.DrawPath(pen, path);
4264
4432
  }
4265
- var send = new RectangleF(body.Right - pad - sendD, ly + (34 * s - sendD) / 2f, sendD, sendD);
4266
- using (var brush = new SolidBrush(Accent)) g.FillEllipse(brush, send);
4433
+ bool sendHover = hoveredBtn == "send";
4434
+ bool sendPress = pressedBtn == "send";
4435
+ var sendHit = new RectangleF(body.Right - pad - sendD, ly + (34 * s - sendD) / 2f, sendD, sendD);
4436
+ var send = RectangleF.Inflate(sendHit, sendPress ? -0.8f * s : sendHover ? 1.1f * s : 0f, sendPress ? -0.8f * s : sendHover ? 1.1f * s : 0f);
4437
+ if (sendHover) using (var halo = new SolidBrush(Color.FromArgb(65, Accent))) g.FillEllipse(halo, RectangleF.Inflate(send, 2.5f * s, 2.5f * s));
4438
+ using (var brush = new SolidBrush(sendPress ? Color.FromArgb(190, 92, 62) : sendHover ? AccentHi : Accent)) g.FillEllipse(brush, send);
4267
4439
  using (var b = new SolidBrush(Color.White)) g.DrawString(glyphFont.Length > 0 ? "\uE724" : "➤", fGlyph, b, send, center);
4268
- buttons.Add(new KeyValuePair<Rectangle, string>(Rectangle.Round(send), "send"));
4440
+ buttons.Add(new KeyValuePair<Rectangle, string>(Rectangle.Round(sendHit), "send"));
4269
4441
  var boxRect = new Rectangle(x + (int)(fieldRect.X + 14 * s), y + (int)(fieldRect.Y + 3 * s), (int)(fieldRect.Width - 28 * s), (int)(fieldRect.Height - 6 * s));
4270
4442
  if (boxRect != inputBounds) { inputBounds = boxRect; input.Place(boxRect); }
4271
4443
  }
@@ -4365,16 +4537,167 @@ namespace DshComputerUse
4365
4537
 
4366
4538
  static void InstallHook()
4367
4539
  {
4368
- if (hook != IntPtr.Zero) return;
4369
- hookProc = HookCallback;
4370
- hook = Native.SetWindowsHookEx(13, hookProc, Native.GetModuleHandle(null), 0);
4540
+ if (hook == IntPtr.Zero)
4541
+ {
4542
+ hookProc = HookCallback;
4543
+ hook = Native.SetWindowsHookEx(13, hookProc, Native.GetModuleHandle(null), 0);
4544
+ }
4545
+ if (mouseHook == IntPtr.Zero)
4546
+ {
4547
+ mouseHookProc = MouseHookCallback;
4548
+ mouseHook = Native.SetWindowsHookEx(14, mouseHookProc, Native.GetModuleHandle(null), 0);
4549
+ }
4371
4550
  }
4372
4551
 
4373
4552
  static void RemoveHook()
4374
4553
  {
4375
- if (hook == IntPtr.Zero) return;
4376
- Native.UnhookWindowsHookEx(hook);
4377
- hook = IntPtr.Zero;
4554
+ if (hook != IntPtr.Zero)
4555
+ {
4556
+ Native.UnhookWindowsHookEx(hook);
4557
+ hook = IntPtr.Zero;
4558
+ }
4559
+ if (mouseHook != IntPtr.Zero)
4560
+ {
4561
+ Native.UnhookWindowsHookEx(mouseHook);
4562
+ mouseHook = IntPtr.Zero;
4563
+ }
4564
+ }
4565
+
4566
+ static bool IsOnOwnUi(int x, int y)
4567
+ {
4568
+ lock (RectLock)
4569
+ {
4570
+ if (!pillRect.IsEmpty && pillRect.Contains(x, y)) return true;
4571
+ if (pet != null && !pet.Physical.IsEmpty && pet.Physical.Contains(x, y)) return true;
4572
+ if (!inputBounds.IsEmpty && inputBounds.Contains(x, y)) return true;
4573
+ }
4574
+ IntPtr card = CardLayer.Hwnd;
4575
+ if (card != IntPtr.Zero && Native.IsWindow(card))
4576
+ {
4577
+ Native.RECT rc;
4578
+ if (Native.GetWindowRect(card, out rc) && x >= rc.Left && x < rc.Right && y >= rc.Top && y < rc.Bottom)
4579
+ return true;
4580
+ }
4581
+ return false;
4582
+ }
4583
+
4584
+ static void RecordUserMouseAsync(string kind, string button, string direction, int x, int y)
4585
+ {
4586
+ ThreadPool.QueueUserWorkItem(delegate
4587
+ {
4588
+ try
4589
+ {
4590
+ var win = Windows.At(x, y);
4591
+ string exe = win.ContainsKey("exe") ? Convert.ToString(win["exe"]) ?? "" : "";
4592
+ string title = win.ContainsKey("title") ? Convert.ToString(win["title"]) ?? "" : "";
4593
+ string exeLower = exe.ToLowerInvariant();
4594
+ // Clicking the DSH chat window itself is the user reading/chatting, not taking over target apps.
4595
+ if (exeLower == "cu-helper.exe" || exeLower == "deepseek harness.exe") return;
4596
+ string role = "", name = "";
4597
+ bool isPassword = false;
4598
+ if (kind == "click" || kind == "double_click")
4599
+ Uia.DescribeAtPoint(x, y, out role, out name, out isPassword);
4600
+ var data = new Dictionary<string, object>();
4601
+ data["kind"] = kind;
4602
+ if (!string.IsNullOrEmpty(button)) data["button"] = button;
4603
+ if (!string.IsNullOrEmpty(direction)) data["direction"] = direction;
4604
+ data["x"] = x;
4605
+ data["y"] = y;
4606
+ if (exe.Length > 0) data["exe"] = exe;
4607
+ if (title.Length > 0) data["title"] = title.Length > 80 ? title.Substring(0, 80) + "…" : title;
4608
+ if (role.Length > 0) data["role"] = role;
4609
+ if (name.Length > 0) data["name"] = name;
4610
+ if (isPassword) data["password"] = true;
4611
+ Program.EmitDataEvent("user_action", data);
4612
+ }
4613
+ catch (Exception) { }
4614
+ });
4615
+ }
4616
+
4617
+ static void RecordUserKeyAsync(int count)
4618
+ {
4619
+ ThreadPool.QueueUserWorkItem(delegate
4620
+ {
4621
+ try
4622
+ {
4623
+ IntPtr fg = Native.GetForegroundWindow();
4624
+ var win = Windows.Describe(fg);
4625
+ string exe = win.ContainsKey("exe") ? Convert.ToString(win["exe"]) ?? "" : "";
4626
+ string title = win.ContainsKey("title") ? Convert.ToString(win["title"]) ?? "" : "";
4627
+ string exeLower = exe.ToLowerInvariant();
4628
+ if (exeLower == "cu-helper.exe" || exeLower == "deepseek harness.exe") return;
4629
+ string role, name;
4630
+ bool isPassword;
4631
+ Uia.DescribeFocused(out role, out name, out isPassword);
4632
+ var data = new Dictionary<string, object>();
4633
+ data["kind"] = "type";
4634
+ data["keys"] = count;
4635
+ if (exe.Length > 0) data["exe"] = exe;
4636
+ if (title.Length > 0) data["title"] = title.Length > 80 ? title.Substring(0, 80) + "…" : title;
4637
+ if (role.Length > 0) data["role"] = role;
4638
+ if (name.Length > 0) data["name"] = name;
4639
+ if (isPassword) data["password"] = true;
4640
+ Program.EmitDataEvent("user_action", data);
4641
+ }
4642
+ catch (Exception) { }
4643
+ });
4644
+ }
4645
+
4646
+ static IntPtr MouseHookCallback(int nCode, IntPtr wParam, IntPtr lParam)
4647
+ {
4648
+ if (nCode >= 0 && visible)
4649
+ {
4650
+ int msg = wParam.ToInt32();
4651
+ // Deliberately ignore WM_MOUSEMOVE (0x0200): nudging the mouse never triggers takeover.
4652
+ bool isClick = msg == 0x0201 || msg == 0x0204 || msg == 0x0207;
4653
+ bool isWheel = msg == 0x020A || msg == 0x020E;
4654
+ if (isClick || isWheel)
4655
+ {
4656
+ var ms = (Native.MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(Native.MSLLHOOKSTRUCT));
4657
+ // Ignore synthetic clicks injected by our own SendInput (LLMHF_INJECTED = 0x01, lower IL = 0x02).
4658
+ if ((ms.flags & 0x03) == 0 && ms.dwExtraInfo != Native.Marker && !IsOnOwnUi(ms.pt.X, ms.pt.Y))
4659
+ {
4660
+ int now = Environment.TickCount;
4661
+ lastTyping = now;
4662
+ if (isClick)
4663
+ {
4664
+ string button = msg == 0x0204 ? "right" : msg == 0x0207 ? "middle" : "left";
4665
+ string kind = "click";
4666
+ lock (UserActionLock)
4667
+ {
4668
+ if (button == "left" && unchecked(now - lastClickTick) <= 500
4669
+ && Math.Abs(ms.pt.X - lastClickX) <= 6 && Math.Abs(ms.pt.Y - lastClickY) <= 6)
4670
+ {
4671
+ kind = "double_click";
4672
+ lastClickTick = int.MinValue / 2;
4673
+ }
4674
+ else
4675
+ {
4676
+ lastClickTick = now;
4677
+ lastClickX = ms.pt.X;
4678
+ lastClickY = ms.pt.Y;
4679
+ }
4680
+ }
4681
+ RecordUserMouseAsync(kind, button, "", ms.pt.X, ms.pt.Y);
4682
+ }
4683
+ else
4684
+ {
4685
+ bool shouldEmit = false;
4686
+ lock (UserActionLock)
4687
+ {
4688
+ if (unchecked(now - lastScrollTick) > 600) { lastScrollTick = now; shouldEmit = true; }
4689
+ }
4690
+ if (shouldEmit)
4691
+ {
4692
+ short delta = (short)((ms.mouseData >> 16) & 0xFFFF);
4693
+ string dir = msg == 0x020E ? (delta >= 0 ? "right" : "left") : (delta >= 0 ? "up" : "down");
4694
+ RecordUserMouseAsync("scroll", "", dir, ms.pt.X, ms.pt.Y);
4695
+ }
4696
+ }
4697
+ }
4698
+ }
4699
+ }
4700
+ return Native.CallNextHookEx(mouseHook, nCode, wParam, lParam);
4378
4701
  }
4379
4702
 
4380
4703
  static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
@@ -4405,6 +4728,18 @@ namespace DshComputerUse
4405
4728
  int now = Environment.TickCount;
4406
4729
  lastTyping = now;
4407
4730
  if (unchecked(now - lastEmit) > 250) { lastEmit = now; Program.EmitEvent("user_input", "vk=0x" + vk.ToString("X2")); }
4731
+ int emitCount = 0;
4732
+ lock (UserActionLock)
4733
+ {
4734
+ keyBurstCount++;
4735
+ if (unchecked(now - lastKeySummaryTick) > 900)
4736
+ {
4737
+ lastKeySummaryTick = now;
4738
+ emitCount = keyBurstCount;
4739
+ keyBurstCount = 0;
4740
+ }
4741
+ }
4742
+ if (emitCount > 0) RecordUserKeyAsync(emitCount);
4408
4743
  }
4409
4744
  }
4410
4745
  }