dsh-watch-together 0.3.3 → 0.3.4

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/CHANGELOG.md CHANGED
@@ -1,5 +1,46 @@
1
1
  # Changelog
2
2
 
3
+ ## [0.3.4] - 2026-11-16
4
+
5
+ > 三件都在 `examples/floating-chat/window.py`。前两条来自 see-now 那边的社区报告
6
+ > (`dsh-see-now#70` / `#71`),第三条是 B 站评论里的现场实测 —— 窗口脚本两个包共用,
7
+ > 所以一起在这里修。
8
+
9
+ ### Fixed — 「关」要关到底:最小化之后抓屏线程必须真的停
10
+
11
+ 社区原话:「状态位早就是 false,窗口还在全速抓屏,4 秒吃掉 5 秒的 CPU」。
12
+ 用户眼里的「关」是三件事 —— 指示灯灭、预览停、**CPU 落回 0**;只做第一件的话,
13
+ 后两件会安静地开着,那正是"被装上摄像头"的感觉来源。
14
+
15
+ - 根因:`_minimize()` 只 `withdraw()` 了窗口,**一个字都没碰抓屏线程**。`live` 样式下
16
+ `_work()` 每帧 `mss` 抓整屏 + 跑一遍模糊,屏幕上却什么都没有。而 `_stop_worker()`
17
+ 全文件只有两个调用点(换样式 / 关窗),最小化这条路一次都没走。
18
+ - `_minimize()` 收尾调 `_stop_worker()` —— 连 mss 句柄一起释放,不是让线程空转 `sleep`。
19
+ - `_restore()` 调 `_start_worker()` 重新拉起来;`_work()` 判据补 `or self._hidden` 当第三道闸。
20
+ - 对照实测(真起 live 窗口,`render` 换计数器):旧版最小化后 700ms 内**又画了 21 帧**,
21
+ 新版 **0 帧**且线程退出。判据不能只看 `_hidden` —— 旧版它也是 True。
22
+
23
+ ### Fixed — macOS:输入框打不进字 / 触控板双指不滚 / 输入法候选框被压住(see-now#70)
24
+
25
+ - **打不进字**:aqua 上 `overrideredirect(True)` 得到的不是 key window,操作系统根本不往
26
+ 这个进程送键盘事件(探针实测 `<KeyPress>` 0 条)。Tk 8.6 的老解法
27
+ `::tk::unsupported::MacWindowStyle` 在 Tk 9 里被整个删掉,没有等价入口 —— 所以 macOS 上
28
+ 不调 `overrideredirect`,保留系统标题栏。代价是顶部多一条系统栏,换"能打字"值。
29
+ - **触控板**:Tk 9 在 mac 上把触控板拆成独立的 `<TouchpadScroll>`,`%D` 是
30
+ `dx<<16 | dy`(低 16 位有符号像素增量),~60Hz、每次几个像素。按
31
+ `::tk::PreciseScrollDeltas` 解包,攒够 32px 滚一格,余量留到下一次。
32
+ Tk 8.6 没有这个事件类型,bind 失败就静默保持"只有滚轮"。
33
+ - **候选框**:`_keep_on_top()` 每 1.8s 抬一次 topmost,把普通窗口层的候选框压在底下。
34
+ 输入框有焦点期间跳过并撤掉 topmost,`<FocusOut>` 恢复。只对 mac 生效 —— Windows 上
35
+ 撤了会被宿主顶下去(#44),那边候选框是独立浮层,不受影响。
36
+
37
+ ### Fixed — 滚动时静态底板逐格重建,单格 45~60ms(see-now#71)
38
+
39
+ 滚动路径每换一次 `_geo.shapes` 就 `frame_for()` 重画一张底板,实测 11~12ms/次。
40
+ 改成停手 `STATIC_DEFER_MS`(=200ms) 后再重建,滚动期间只重画气泡;定时器只留一个
41
+ (连线滚十格不能十个 `after` 一起醒)。`_layout()` 里那两处仍立即重建 ——
42
+ 那是尺寸/折叠变化,不是滚动。
43
+
3
44
  ## [0.3.3] - 2026-11-11
4
45
 
5
46
  > #56 的第 3 条,也就是前几轮一直卡在"语义没定"的那一件:**标题栏加一个 ‖/▶**。
package/dsh.plugin.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "dsh-watch-together",
3
3
  "description": "Watch a video together, in sync with the user's real playback: each segment arrives as one storyboard image plus its subtitle lines just before it ends, and the floating window carries spoken replies and live room audio.",
4
- "version": "0.3.3",
4
+ "version": "0.3.4",
5
5
  "entry": {
6
6
  "name": "dsh-watch-together",
7
7
  "inject": [
@@ -48,6 +48,9 @@ from pathlib import Path
48
48
  # `AttributeError: module 'ctypes' has no attribute 'windll'`,而且死在依赖预检
49
49
  # 之前,用户看到的是"启动后 700ms 退出,code=1"(issue #40)。
50
50
  IS_WIN = os.name == "nt"
51
+ # 是不是 macOS。aqua 上有一整套和 Windows 不一样的行为,**不能拿"不是 Windows"来代替**:
52
+ # 无边框窗收不到键盘、触控板走独立事件、输入法候选框是普通窗口层(#70 三条)。
53
+ IS_MAC = sys.platform == "darwin"
51
54
 
52
55
  # 窗口扩展样式:`noActivate` 用它(点窗不激活,见 `_apply_no_activate`)
53
56
  GWL_EXSTYLE = -20
@@ -154,6 +157,13 @@ INTERVAL_MS = max(4, int(1000 / MAX_FPS))
154
157
  # 降到 30fps 探针,有变化时 idle 立刻归零、下一拍恢复全速。
155
158
  PROBE_FPS = 30
156
159
 
160
+ # mac 触控板:`<TouchpadScroll>` 一次只给几个像素(~60Hz),攒够这么多才滚一行。
161
+ # 32 是社区实测的结果(#70 第 2 条):小于它一次滑动要等太久,大于它一格跳得太狠。
162
+ TOUCHPAD_ROW_PX = 32
163
+ # 滚动时把静态底板的**重建**推到停手之后这么久(#71)。底板一次 11~12ms,
164
+ # 每滚一格都重建会让单格 45~60ms;推到停手后,滚动期间只重画气泡。
165
+ STATIC_DEFER_MS = 200
166
+
157
167
  # 仅用于文档截图/CI 视觉检查:暂停抓屏并把 WDA 关掉,窗口才会出现在截图里
158
168
  PREVIEW = os.environ.get("DSH_GLASS_PREVIEW") == "1"
159
169
  # 文档截图:正常抓屏 2.5 秒后暂停并取消 WDA,保留最后一帧玻璃
@@ -565,7 +575,15 @@ class LiveGlass:
565
575
 
566
576
  self.root = tk.Tk()
567
577
  self.root.withdraw()
568
- self.root.overrideredirect(True)
578
+ # **macOS 上不能 overrideredirect**:aqua 里这样得到的不是一个 key window,
579
+ # 操作系统压根不往这个进程送键盘事件 —— 点输入区光标画得出来,敲键盘一个字
580
+ # 都不进(#70 第 1 条,用户探针实测 root/entry 的 <KeyPress> 0 条)。
581
+ # Tk 8.6 时代的老解法 `::tk::unsupported::MacWindowStyle style plain none`
582
+ # 在 Tk 9 里被整个删掉了(`invalid command name`),没有等价的 Tcl 入口。
583
+ # 所以留着系统标题栏:普通窗口能成为 key window,输入立刻可用 ——
584
+ # 视觉上多一条系统栏,但"打不进字"是硬伤,这个取舍只能这么取。
585
+ if not IS_MAC:
586
+ self.root.overrideredirect(True)
569
587
  self.root.title(self.name)
570
588
  self.root.attributes("-topmost", True)
571
589
  self.root.configure(bg=KEY)
@@ -619,6 +637,13 @@ class LiveGlass:
619
637
  self._drag = None
620
638
  self._resize = None
621
639
  self._preview_at = 0.0
640
+ # 触控板滚动的余量(像素)。mac 的 <TouchpadScroll> 每次只给几个像素、60Hz 一次,
641
+ # 攒够一行才滚 —— 见 `_touchpad_scroll`(#70 第 2 条)。
642
+ self._scroll_acc = 0
643
+ # 输入框有没有焦点。mac 上「正在输入」要撤掉 topmost,否则输入法候选框被压住(#70 第 3 条)。
644
+ self._ime_focus = False
645
+ # 滚动时底板重建的 after id(#71,见 `_schedule_static_frame`)
646
+ self._static_job = None
622
647
  # Entry 的真光标被关掉(insertwidth=0),画面上那条线是自己画的,
623
648
  # 所以闪烁也得自己来。_caret_job 存 after 的 id,重排时取消,避免叠定时器。
624
649
  self._caret_on = True
@@ -825,9 +850,15 @@ class LiveGlass:
825
850
  self.root.after(26, lambda: self._intro_step(n + 1))
826
851
 
827
852
  def _keep_on_top(self) -> None:
828
- """DSH 等置顶窗口被激活后会把悬浮窗压下去;定时重新抬到最上层。"""
853
+ """DSH 等置顶窗口被激活后会把悬浮窗压下去;定时重新抬到最上层。
854
+
855
+ **正在输入的时候不抬**(#70 第 3 条):输入法候选框是普通窗口层,
856
+ 窗口一 topmost 就把它压在底下 —— mac 上用户完全看不见自己在打什么。
857
+ `_focus_input` 会撤掉 topmost 并置起 `_ime_focus`,失焦(`_on_input_blur`)
858
+ 恢复。只对 mac 生效:Windows 上撤 topmost 会被 DSH 顶下去(#44 第 1 条)。
859
+ """
829
860
  try:
830
- if not self._hidden:
861
+ if not self._hidden and not self._ime_focus:
831
862
  self.root.attributes("-topmost", True)
832
863
  self.root.lift()
833
864
  except Exception:
@@ -979,6 +1010,29 @@ class LiveGlass:
979
1010
  # 连滚只重排最后一次:滚十格就白画九遍(#44 第 2 条,社区实测每格卡 ~1 秒)
980
1011
  self._schedule_feed_refresh()
981
1012
 
1013
+ def _touchpad_scroll(self, e):
1014
+ """mac 的触控板:`<MouseWheel>` 根本不来,来的是 `<TouchpadScroll>`(#70 第 2 条)。
1015
+
1016
+ `%D` 的语义也不是 `±120`:高 16 位是 dx,低 16 位是**有符号的 dy(像素增量)**。
1017
+ 事件约 60Hz、每次只有几个像素(实测 dy = -4 ~ -8),所以不能"一次事件滚一格" ——
1018
+ 攒够 `TOUCHPAD_ROW_PX` 才走一格,余量留着给下一次。解包方式照抄 Tk 自己的
1019
+ `::tk::PreciseScrollDeltas`。方向与 `_wheel` 一致:dy > 0 = 向上 = 看更早的消息。
1020
+ """
1021
+ try:
1022
+ raw = int(e.delta)
1023
+ except (TypeError, ValueError):
1024
+ return
1025
+ low = raw & 0xFFFF
1026
+ dy = low - 0x10000 if low >= 0x8000 else low
1027
+ self._scroll_acc += dy
1028
+ step = int(self._scroll_acc / TOUCHPAD_ROW_PX)
1029
+ if not step:
1030
+ return
1031
+ self._scroll_acc -= step * TOUCHPAD_ROW_PX
1032
+ if self.feed.scroll(step):
1033
+ self._collapse_expanded()
1034
+ self._schedule_feed_refresh()
1035
+
982
1036
  def _schedule_feed_refresh(self, delay: int = 40) -> None:
983
1037
  job = getattr(self, "_feed_job", None)
984
1038
  if job is not None:
@@ -1017,7 +1071,35 @@ class LiveGlass:
1017
1071
  self._draw_scrollbar()
1018
1072
  self.text.refresh()
1019
1073
  if self._geo.shapes != shapes_before:
1020
- self._sync_static_frame()
1074
+ self._schedule_static_frame()
1075
+
1076
+ def _schedule_static_frame(self) -> None:
1077
+ """把静态底板的重建推到「手停下来之后」(#71)。
1078
+
1079
+ 社区实测(macOS / Tk 9.1,issue 里有逐步计时):底板 `frame_for()` 一次
1080
+ 11~12ms,而滚动路径**每滚一格都会换 `_geo.shapes`**,于是每格都重建一张 ——
1081
+ 单格 45~60ms,滚动必然掉帧,而且靠调参救不了。
1082
+
1083
+ 推迟之后滚动期间只重画气泡,代价是滚动中短暂看到旧底板(形状差一点点),
1084
+ 停手 200ms 内就对上了。**语义没变**:`_sync_static_frame()` 本来就按 shapes
1085
+ 缓存,形状没变时是 0 成本,所以"多画一次"不亏。
1086
+
1087
+ 定时器必须只留一个:连线滚十格,十个 after 一起醒来等于把省下的又还回去。
1088
+ """
1089
+ job = getattr(self, "_static_job", None)
1090
+ if job is not None:
1091
+ try:
1092
+ self.root.after_cancel(job)
1093
+ except Exception:
1094
+ pass
1095
+ try:
1096
+ self._static_job = self.root.after(STATIC_DEFER_MS, self._run_static_frame)
1097
+ except Exception:
1098
+ self._static_job = None
1099
+
1100
+ def _run_static_frame(self) -> None:
1101
+ self._static_job = None
1102
+ self._sync_static_frame()
1021
1103
 
1022
1104
  def _toggle_mic(self):
1023
1105
  if self._mic_proc is not None and self._mic_proc.poll() is None:
@@ -1162,6 +1244,15 @@ class LiveGlass:
1162
1244
  # 理由见 `_apply_no_activate`。
1163
1245
  if getattr(self, "_no_activate_on", False):
1164
1246
  self._apply_no_activate(False)
1247
+ # mac:**输入期间必须撤 topmost**,否则输入法候选框被压在窗口底下看不见(#70 第 3 条)。
1248
+ # 只对 mac 生效 —— Windows 上撤了会被 DSH 顶下去(#44 第 1 条),而那边候选框
1249
+ # 是独立浮层,不受 topmost 影响。
1250
+ if IS_MAC and not self._ime_focus:
1251
+ self._ime_focus = True
1252
+ try:
1253
+ self.root.attributes("-topmost", False)
1254
+ except Exception:
1255
+ pass
1165
1256
  try:
1166
1257
  self._entry.focus_force()
1167
1258
  self._entry.icursor("end")
@@ -1170,6 +1261,16 @@ class LiveGlass:
1170
1261
  self._caret_on = True
1171
1262
  self._render_input()
1172
1263
 
1264
+ def _on_input_blur(self, _e=None):
1265
+ """输入框失焦:重画光标 + 把 topmost 还回去(mac,见 `_focus_input`)。"""
1266
+ if IS_MAC and self._ime_focus:
1267
+ self._ime_focus = False
1268
+ try:
1269
+ self.root.attributes("-topmost", True)
1270
+ except Exception:
1271
+ pass
1272
+ self._render_input()
1273
+
1173
1274
  # ---- 改名 ----
1174
1275
 
1175
1276
  def _edit_title(self, _event=None):
@@ -1365,6 +1466,15 @@ class LiveGlass:
1365
1466
  except Exception:
1366
1467
  self._hidden = False
1367
1468
  return
1469
+ # **抓屏线程必须一起停**(社区实报 2026-11-16)。
1470
+ #
1471
+ # `withdraw()` 只是把窗口从屏幕上拿走 —— `mss` 照样每帧抓整屏、再跑一遍模糊,
1472
+ # 用户看到的是"窗口关掉状态、CPU 却满载"(实测 4 秒墙钟吃掉 5 秒 CPU 时间)。
1473
+ # 他说的"关"是三件事:指示灯灭、预览停、**CPU 落回 0**。只做第一件的话,
1474
+ # 后两件会安静地开着 —— 这正是"被装上摄像头"的感觉来源。
1475
+ #
1476
+ # 放在 withdraw 之后:先让窗口消失,再花那 0.4s 的 join,用户感知不到卡。
1477
+ self._stop_worker()
1368
1478
  if self._tray is not None:
1369
1479
  self._tray.show()
1370
1480
  # 每次运行只提示一次:Windows 11 会把新图标塞进溢出区,不告诉用户
@@ -1386,6 +1496,9 @@ class LiveGlass:
1386
1496
  pass
1387
1497
  if self._tray is not None:
1388
1498
  self._tray.hide()
1499
+ # 回来要把抓屏线程重新拉起来 —— `_start_worker()` 会重建 CaptureSource;
1500
+ # 静态样式(solid / frost)下它自己会 return,不需要在这里分情况。
1501
+ self._start_worker()
1389
1502
  self._mark_read()
1390
1503
 
1391
1504
  def _sync_danmaku(self, cfg=None) -> None:
@@ -1594,7 +1707,7 @@ class LiveGlass:
1594
1707
  self._entry.bind("<KeyRelease>", self._on_input_key)
1595
1708
  self._entry.bind("<Return>", self._send)
1596
1709
  self._entry.bind("<FocusIn>", lambda _e: self._render_input())
1597
- self._entry.bind("<FocusOut>", lambda _e: self._render_input())
1710
+ self._entry.bind("<FocusOut>", self._on_input_blur)
1598
1711
  # 插入点还能被鼠标和选择改动,这些不发 KeyRelease:不补这几个,点一下
1599
1712
  # 输入框中间,画面上的光标就跟不过去。after_idle 等 Entry 自己先处理完。
1600
1713
  for seq in ("<ButtonRelease-1>", "<B1-Motion>", "<<Selection>>"):
@@ -2013,7 +2126,10 @@ class LiveGlass:
2013
2126
  idle = 0
2014
2127
  with mss.mss() as sct:
2015
2128
  while not stop.is_set() and not self._stop.is_set():
2016
- if self._pause.is_set() or self._collapsed:
2129
+ # `_hidden` 是**第三道闸**:正常路径上 `_minimize()` 会直接 `_stop_worker()`
2130
+ # 把线程杀掉,这里再判一次是防别的路径设了 `_hidden` 却没停线程
2131
+ # (状态位说"关了"、CPU 还在烧,正是社区报的那种假关)。
2132
+ if self._pause.is_set() or self._collapsed or self._hidden:
2017
2133
  time.sleep(0.03)
2018
2134
  continue
2019
2135
  t0 = time.perf_counter()
@@ -2292,6 +2408,14 @@ class LiveGlass:
2292
2408
  def run(self):
2293
2409
  self.root.bind("<Escape>", self._close)
2294
2410
  self.root.bind("<MouseWheel>", self._wheel)
2411
+ # **mac 触控板不走 <MouseWheel>**:Tk 9 把它拆成了独立的 <TouchpadScroll>
2412
+ # (tk.h: `MappingNotify + 5`)。Tk 8.6 没有这个事件类型,bind 会当场抛
2413
+ # `bad event type or keysym`,所以只在 mac 上试,失败就保持"只有滚轮"。
2414
+ if IS_MAC:
2415
+ try:
2416
+ self.root.bind("<TouchpadScroll>", self._touchpad_scroll)
2417
+ except tk.TclError:
2418
+ pass
2295
2419
  self.root.bind("<Motion>", self._hover, add="+")
2296
2420
  self.root.bind("<Leave>", self._leave, add="+")
2297
2421
  self.root.bind("<ButtonPress-1>", self._press, add="+")
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-watch-together",
3
- "version": "0.3.3",
3
+ "version": "0.3.4",
4
4
  "description": "Watch a video together, in sync with the user's real playback: every segment arrives as one storyboard image plus its subtitle lines just before it ends, while the floating window carries spoken replies and live room audio — picture and voice, not a transcript.",
5
5
  "keywords": [
6
6
  "dsh",