dsh-watch-together 0.2.2 → 0.2.3
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 +51 -0
- package/README.md +21 -1
- package/docs/INTERFACES.zh.md +17 -0
- package/docs/images/reward-qr.png +0 -0
- package/dsh.plugin.json +12 -12
- package/examples/floating-chat/glass_window_gpu.py +2 -8
- package/examples/floating-chat/window.py +210 -47
- package/package.json +115 -111
- package/src/chat-store.js +17 -10
- package/src/handler.js +11 -1
- package/src/index.js +5 -5
- package/src/input-surface.js +13 -12
- package/src/interfaces.js +8 -2
- package/src/public-api.js +6 -0
- package/src/runtime-ports.js +45 -0
- package/src/watch-actions.js +177 -173
- package/src/watch-loop.js +13 -10
- package/src/watch-run.js +5 -5
- package/src/window-interface.js +21 -13
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,57 @@
|
|
|
3
3
|
All notable changes to **dsh-watch-together** are documented here.
|
|
4
4
|
This package starts its own version line at `0.0.7` — it is not a continuation of `dsh-video-storyboard`.
|
|
5
5
|
|
|
6
|
+
## [0.2.3] - 2026-11-02
|
|
7
|
+
|
|
8
|
+
> 主题:**悬浮窗这一轮收口** —— 社区提的 #22 / #23 / #25 / #26 / #27 全部合进来,
|
|
9
|
+
> 核心与 TTS / 悬浮窗之间的依赖收成 `runtime-ports`。
|
|
10
|
+
> 播放位置、台词锚点、语音调度这些**一处没动**;变的只有窗口和它下面那层接口。
|
|
11
|
+
|
|
12
|
+
### Added — 核心只认 runtime-ports(合 PR #22)
|
|
13
|
+
|
|
14
|
+
- 新增 `src/runtime-ports.js`:**唯一**给出 TTS / 悬浮窗适配器的地方。
|
|
15
|
+
`chat-store` / `watch-loop` / `watch-actions` / `input-surface` 不再直接
|
|
16
|
+
`import` `voice.js` / `floating-window.js` / `window-protocol.js`。
|
|
17
|
+
- `installPorts()` / `resetPorts()` / `getTtsPort()` / `getWindowPort()` 从 `public-api` 导出,
|
|
18
|
+
宿主或测试可以注入自己的实现;裸 `import` 仍拿历史默认(speech bus + 内置窗口),
|
|
19
|
+
CLI 和老测试照旧能跑。
|
|
20
|
+
- `test/decoupling.test.mjs` 守这条线:核心模块一旦直接依赖实现就红。
|
|
21
|
+
|
|
22
|
+
### Changed — 悬浮窗气泡与折叠重做(合 PR #22)
|
|
23
|
+
|
|
24
|
+
- **气泡改成「时间头 + 正文折行」**:`_fit_lines()` 按框宽/框高折行,标点不落行首,
|
|
25
|
+
超长截断加省略号;四条气泡统一 88px 并吃满内容宽度,不再为了塞下文字把字号压到 9。
|
|
26
|
+
- **折叠不再重建整窗几何**:只缩窗口 + 只画标题栏。原来 `rebuild_geometry(78)` 会在
|
|
27
|
+
`rounded_mask` 抛 `y1 must be >= y0`,**折叠直接异常**(main 上可复现)。
|
|
28
|
+
- **每 1.8 秒重新置顶一次**:DSH 抢焦点后浮窗不再被压下去。
|
|
29
|
+
- **麦克风改 Canvas 矢量图标**,不再往合成帧上贴第二层;GPU 路径同步去掉。
|
|
30
|
+
- 补三处收尾:空态分支还在用已删掉的 `bh` → 清空 `chat.jsonl` 冷启动直接 `NameError`,
|
|
31
|
+
窗口起不来;标题改回**双击**改名(单击要留给按住拖动);折叠态跳过抓屏,
|
|
32
|
+
否则遮罩尺寸不符会每帧刷一条 `images do not match`。
|
|
33
|
+
|
|
34
|
+
### Fixed
|
|
35
|
+
|
|
36
|
+
- **拖到屏幕外不再花屏**(合 PR #23)—— `_x` / `_y` 只在启动时读一次,拖动后已过期;
|
|
37
|
+
加上越界钳位后仍可能算出 0 或负的抓屏尺寸,`mss` 抛 `Region has zero or negative size`,
|
|
38
|
+
异常在 `_work()` 里被吞掉只打一行的结果是:每帧一条日志、背景永远画不出来。
|
|
39
|
+
现在拖动/缩放分支都钳位,并在抓屏前直接挡掉非正尺寸。
|
|
40
|
+
- **`window` 接口的 hide/show 兼容只返回布尔的实现**(合 PR #25)—— 以前拿返回值当对象解构,
|
|
41
|
+
实现只回 `true` 时 `hide` 变成静默失败。
|
|
42
|
+
- **snapshot 模式不再被 `_tick` 重新打开 WDA**(合 PR #26)—— worker 已暂停,
|
|
43
|
+
下一拍却把 WDA 开回来,窗口再次从截图/录屏里消失,`_snapshot_mode` 等于没做。
|
|
44
|
+
- **折叠不再跳回原位、折叠背景有圆角、标题栏排版收紧**(合 PR #27)——
|
|
45
|
+
`_sync_pos()` 在 `_release` 与 `toggle_collapse` 前同步真实坐标(`_x/_y` 过期会让窗口弹回启动位置);
|
|
46
|
+
折叠态按 78px 重新贴圆角遮罩,不再直接裁 `_last_frame` 留一条硬边;
|
|
47
|
+
`_save_geometry` 存**展开高度**,否则下次启动按 78px 恢复;折叠时 `_tick` 不再消费旧队列,
|
|
48
|
+
避免 560px 的帧贴回 78px 画布闪一下;状态点/名字/计数统一到 y=38 中线,字号 21→16。
|
|
49
|
+
实测对照 main:拖到 (300,220) 折叠后停在 (300,220)、配置写 `760x560+300+220`,
|
|
50
|
+
main 会跳回 (1720,480) 并写 `760x78`。
|
|
51
|
+
|
|
52
|
+
### Docs
|
|
53
|
+
|
|
54
|
+
- **README 挂上赞赏码**(微信,`docs/images/reward-qr.png`)。写法上明确一句:
|
|
55
|
+
打赏**不解锁任何东西**,也不影响这个项目以后的授权方式 —— 它仍然是 Apache-2.0。
|
|
56
|
+
|
|
6
57
|
## [0.2.2] - 2026-11-01
|
|
7
58
|
|
|
8
59
|
只改元数据,不动行为。
|
package/README.md
CHANGED
|
@@ -417,7 +417,7 @@ pythonw examples/floating-chat/window.py # 无控制台
|
|
|
417
417
|
```
|
|
418
418
|
|
|
419
419
|
它长这样:三块分开的液态玻璃(标题 / 主体 / 输入栏),气泡圆角、半透明,
|
|
420
|
-
|
|
420
|
+
你说的话靠右;标题栏可拖动、双击标题改名、▾ 折叠、✕ 关闭,底部有麦克风图标和发送。
|
|
421
421
|
窗口底稿就是 PR #15 的 `glass_window.py`(现在叫 `window.py`),没有另写一套
|
|
422
422
|
近似样式;GPU 伴生 `glass_window_gpu.py` 只把每帧合成搬到 moderngl,UI 和交互
|
|
423
423
|
仍是同一份,所以 CPU/GPU 形式一致。
|
|
@@ -594,6 +594,26 @@ node scripts/install-dev.mjs # 把本包同步进 DSH profile(副本
|
|
|
594
594
|
|
|
595
595
|
改完代码要重启 DSH Desktop 才生效(插件树在进程启动时组合)。
|
|
596
596
|
|
|
597
|
+
## 致谢
|
|
598
|
+
|
|
599
|
+
社区提的改动按 PR 记在这里(storyboard 那边的完整名单在 `dsh-video-storyboard/CREDITS.md`):
|
|
600
|
+
|
|
601
|
+
- [@ggfgfgf-on](https://github.com/ggfgfgf-on) —— 插件设置页([PR #11](https://github.com/SAOS-3624/dsh-watch-together/pull/11),设置直接渲染在插件详情页);
|
|
602
|
+
核心改走 runtime-ports、悬浮窗气泡与折叠重做([PR #22](https://github.com/SAOS-3624/dsh-watch-together/pull/22))
|
|
603
|
+
- [@DreamDLYY](https://github.com/DreamDLYY) —— 悬浮窗拖出屏幕后抓屏越界、日志每帧刷错、背景永久花屏的复现与修复([PR #23](https://github.com/SAOS-3624/dsh-watch-together/pull/23))
|
|
604
|
+
|
|
597
605
|
## 许可
|
|
598
606
|
|
|
599
607
|
Apache License 2.0 —— 见 [LICENSE](LICENSE)(全文)与 [NOTICE](NOTICE)(这个项目**不做什么**:不托管视频、不上传位置、不绕过 DRM、不内置 TTS 引擎)。
|
|
608
|
+
|
|
609
|
+
## 赞赏
|
|
610
|
+
|
|
611
|
+
免费、不锁功能、不需要授权 —— 就是 Apache-2.0,拿去用就行。
|
|
612
|
+
|
|
613
|
+
如果它替你省了时间,欢迎扫码:
|
|
614
|
+
|
|
615
|
+
<p align="center">
|
|
616
|
+
<img src="./docs/images/reward-qr.png" alt="赞赏码(微信 / 支付宝)" width="240">
|
|
617
|
+
</p>
|
|
618
|
+
|
|
619
|
+
打赏不解锁任何东西,也不影响这个项目以后的授权方式。
|
package/docs/INTERFACES.zh.md
CHANGED
|
@@ -30,6 +30,23 @@ api.tts.due({ at, segment, ... }); // 声音事件
|
|
|
30
30
|
每个接口都是 frozen 对象;缺少依赖时在**创建时**报错(`recognition` 缺
|
|
31
31
|
`runStoryboard` 等),而不是陪看跑到一半才炸。
|
|
32
32
|
|
|
33
|
+
### 运行时端口
|
|
34
|
+
|
|
35
|
+
`src/runtime-ports.js` 是核心模块取接口的唯一入口:
|
|
36
|
+
|
|
37
|
+
```js
|
|
38
|
+
import { installPorts, getTtsPort, getWindowPort } from 'dsh-watch-together/public-api';
|
|
39
|
+
|
|
40
|
+
installPorts({ tts: api.tts, window: api.window }); // 组合根注入
|
|
41
|
+
getTtsPort().due({ at: 12, text: '...' });
|
|
42
|
+
getWindowPort().message({ role: 'assistant', text: '...' });
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
`chat-store.js` / `watch-loop.js` / `watch-actions.js` / `input-surface.js`
|
|
46
|
+
只 import 这个端口模块,不再 import `voice.js`、`floating-window.js` 或
|
|
47
|
+
`window-protocol.js`。没有注入时端口提供历史默认实现(speech-bus + 自带
|
|
48
|
+
Python 窗口),所以 CLI 和既有测试行为不变。
|
|
49
|
+
|
|
33
50
|
## 2. 窗口线协议
|
|
34
51
|
|
|
35
52
|
窗口实现不需要 import 插件代码,只读写两个 JSONL 文件(默认在
|
|
Binary file
|
package/dsh.plugin.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "dsh-watch-together",
|
|
3
|
-
"description": "Watch a video together with a text-only agent: follow the user's real playback position and hand over one segment at a time, just before the segment ends.",
|
|
4
|
-
"version": "0.2.
|
|
5
|
-
"entry": {
|
|
6
|
-
"name": "dsh-watch-together",
|
|
7
|
-
"inject": [
|
|
8
|
-
"tools",
|
|
9
|
-
"attachments"
|
|
10
|
-
]
|
|
11
|
-
}
|
|
12
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "dsh-watch-together",
|
|
3
|
+
"description": "Watch a video together with a text-only agent: follow the user's real playback position and hand over one segment at a time, just before the segment ends.",
|
|
4
|
+
"version": "0.2.3",
|
|
5
|
+
"entry": {
|
|
6
|
+
"name": "dsh-watch-together",
|
|
7
|
+
"inject": [
|
|
8
|
+
"tools",
|
|
9
|
+
"attachments"
|
|
10
|
+
]
|
|
11
|
+
}
|
|
12
|
+
}
|
|
@@ -462,14 +462,8 @@ def capture_gpu(self, sct):
|
|
|
462
462
|
self._sig_bbox = box
|
|
463
463
|
|
|
464
464
|
panel = _gpu.render(shot.bgra, shot.size, px, py, motion=_gpu._motion)
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
G.MIC_POS[0] + 28, G.MIC_POS[1] + 28)).convert("L")
|
|
468
|
-
mic_bg = float(np.median(np.asarray(mic_box))) / 255.0
|
|
469
|
-
out = out.convert("RGBA")
|
|
470
|
-
icon = G.MIC_LIGHT if mic_bg < 0.48 else G.MIC_DARK
|
|
471
|
-
out.paste(icon, G.MIC_POS, icon)
|
|
472
|
-
return G.finish(out.convert("RGB"))
|
|
465
|
+
# 麦克风由 window.py 的 Canvas 图标绘制,GPU 路径不再叠第二层。
|
|
466
|
+
return G.finish(panel.convert("RGB"))
|
|
473
467
|
except Exception as exc:
|
|
474
468
|
print("GPU capture failed, falling back to CPU:", repr(exc), flush=True)
|
|
475
469
|
return _orig_capture(self, sct)
|
|
@@ -158,7 +158,9 @@ def chat_layout(h):
|
|
|
158
158
|
"""消息区在高度不足 560 时压缩行高;底部输入框始终贴底。"""
|
|
159
159
|
input_top = h - 80
|
|
160
160
|
if h >= 560:
|
|
161
|
-
|
|
161
|
+
# 四条消息每条都能放下"时间头 + 两行正文";f12 在 150% DPI 下
|
|
162
|
+
# 行高约 28px,82px 的框刚好装 2 行,不再为了塞下文字把字号压到 9。
|
|
163
|
+
ys, bh = [104, 198, 292, 386], 88
|
|
162
164
|
else:
|
|
163
165
|
# 顶部留 8px 空隙给最后一条和输入框,避免贴在一起
|
|
164
166
|
avail = max(0, input_top - 124 - 8)
|
|
@@ -359,6 +361,7 @@ class LiveGlass:
|
|
|
359
361
|
self._seen = set()
|
|
360
362
|
self._collapsed = False
|
|
361
363
|
self._saved_h = H
|
|
364
|
+
self._snapshot_done = False
|
|
362
365
|
# 只在双击标题时创建,平时不占画布
|
|
363
366
|
self._title_entry = None
|
|
364
367
|
self._title_item = None
|
|
@@ -388,6 +391,7 @@ class LiveGlass:
|
|
|
388
391
|
|
|
389
392
|
self._img = None
|
|
390
393
|
self._last_frame = None
|
|
394
|
+
self._collapsed_frame = None
|
|
391
395
|
self._bg = None
|
|
392
396
|
self._hwnd = None
|
|
393
397
|
self._excluded = False
|
|
@@ -428,6 +432,7 @@ class LiveGlass:
|
|
|
428
432
|
self.root.after(60, self._tick)
|
|
429
433
|
self.root.after(30, lambda: self._intro_step(0))
|
|
430
434
|
self.root.after(250, self._poll_chat)
|
|
435
|
+
self.root.after(1200, self._keep_on_top)
|
|
431
436
|
if SNAPSHOT:
|
|
432
437
|
self.root.after(2600, self._snapshot_mode)
|
|
433
438
|
|
|
@@ -444,6 +449,19 @@ class LiveGlass:
|
|
|
444
449
|
if n < 6:
|
|
445
450
|
self.root.after(26, lambda: self._intro_step(n + 1))
|
|
446
451
|
|
|
452
|
+
def _keep_on_top(self):
|
|
453
|
+
"""DSH 等置顶窗口被激活后会把悬浮窗压下去;定时重新抬到最上层。"""
|
|
454
|
+
try:
|
|
455
|
+
if not getattr(self, "_hidden", False):
|
|
456
|
+
self.root.attributes("-topmost", True)
|
|
457
|
+
self.root.lift()
|
|
458
|
+
except Exception:
|
|
459
|
+
pass
|
|
460
|
+
try:
|
|
461
|
+
self.root.after(1800, self._keep_on_top)
|
|
462
|
+
except Exception:
|
|
463
|
+
pass
|
|
464
|
+
|
|
447
465
|
# ---- 真实聊天流 ----
|
|
448
466
|
|
|
449
467
|
def _parse_geometry(self, geo):
|
|
@@ -605,7 +623,7 @@ class LiveGlass:
|
|
|
605
623
|
if changed:
|
|
606
624
|
if self._msg_offset > 0:
|
|
607
625
|
self._msg_offset += len(new)
|
|
608
|
-
self._msg_offset = max(0, min(self._msg_offset, max(0, len(self.messages) -
|
|
626
|
+
self._msg_offset = max(0, min(self._msg_offset, max(0, len(self.messages) - 4)))
|
|
609
627
|
self._refresh_feed()
|
|
610
628
|
self._file_size = size
|
|
611
629
|
except Exception as exc:
|
|
@@ -637,9 +655,10 @@ class LiveGlass:
|
|
|
637
655
|
self._hidden = not want
|
|
638
656
|
|
|
639
657
|
def _visible_messages(self):
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
658
|
+
total = len(self.messages)
|
|
659
|
+
visible = min(4, total)
|
|
660
|
+
start = max(0, total - visible - self._msg_offset)
|
|
661
|
+
return self.messages[start:start + visible]
|
|
643
662
|
|
|
644
663
|
def _refresh_feed(self):
|
|
645
664
|
"""聊天流变了就重画文字;Entry 的草稿和焦点要保住。"""
|
|
@@ -689,7 +708,8 @@ class LiveGlass:
|
|
|
689
708
|
if not self.messages:
|
|
690
709
|
return
|
|
691
710
|
step = 1 if e.delta > 0 else -1
|
|
692
|
-
|
|
711
|
+
max_off = max(0, len(self.messages) - 4)
|
|
712
|
+
self._msg_offset = max(0, min(max_off, self._msg_offset + step))
|
|
693
713
|
self._refresh_feed()
|
|
694
714
|
|
|
695
715
|
def _toggle_mic(self):
|
|
@@ -722,8 +742,42 @@ class LiveGlass:
|
|
|
722
742
|
pass
|
|
723
743
|
self._refresh_feed()
|
|
724
744
|
|
|
745
|
+
def _make_collapsed_frame(self):
|
|
746
|
+
"""折叠时只保留标题栏那一块玻璃,并补上圆角。
|
|
747
|
+
|
|
748
|
+
直接裁 _last_frame 会让底部变成一条硬切边(遮罩还是 560px 高),
|
|
749
|
+
看起来像渲染坏了;这里按 78px 重新贴一次圆角遮罩。
|
|
750
|
+
"""
|
|
751
|
+
if self._last_frame is None:
|
|
752
|
+
return None
|
|
753
|
+
try:
|
|
754
|
+
w, h = W, H
|
|
755
|
+
crop = self._last_frame.convert("RGB").crop((0, 0, w, h))
|
|
756
|
+
mask = Image.new("L", (w, h), 0)
|
|
757
|
+
d = ImageDraw.Draw(mask)
|
|
758
|
+
d.rounded_rectangle((12, 12, w - 12, 66), radius=22, fill=255)
|
|
759
|
+
out = Image.new("RGB", (w, h), KEY)
|
|
760
|
+
out.paste(crop, (0, 0), mask)
|
|
761
|
+
return out
|
|
762
|
+
except Exception:
|
|
763
|
+
return None
|
|
764
|
+
|
|
765
|
+
def _sync_pos(self):
|
|
766
|
+
"""把 Tk 当前左上角同步回 _x/_y。
|
|
767
|
+
|
|
768
|
+
拖动/缩放只改了 root.geometry,没有改这两个缓存;折叠时如果继续用
|
|
769
|
+
旧坐标,窗口会被拉回拖动前的位置(用户看到的就是"点收起位置跳回")。
|
|
770
|
+
"""
|
|
771
|
+
try:
|
|
772
|
+
self._x = int(self.root.winfo_x())
|
|
773
|
+
self._y = int(self.root.winfo_y())
|
|
774
|
+
except Exception:
|
|
775
|
+
pass
|
|
776
|
+
|
|
725
777
|
def _save_geometry(self):
|
|
726
|
-
|
|
778
|
+
# 折叠态存回展开高度,否则下次启动会按 78px 的高度恢复。
|
|
779
|
+
h = getattr(self, "_saved_h", H) if getattr(self, "_collapsed", False) else self.root.winfo_height()
|
|
780
|
+
write_config({"geometry": f"{self.root.winfo_width()}x{h}"
|
|
727
781
|
f"+{self.root.winfo_x()}+{self.root.winfo_y()}"})
|
|
728
782
|
|
|
729
783
|
def _hwnd_root(self):
|
|
@@ -756,6 +810,11 @@ class LiveGlass:
|
|
|
756
810
|
gt = max(0, t - PAD)
|
|
757
811
|
gr = min(self._sw, r + PAD)
|
|
758
812
|
gb = min(self._sh, b + PAD)
|
|
813
|
+
# 防御:多显示器 / DPI 变化 / 被其他程序挪动时,窗口仍可能越界,
|
|
814
|
+
# 钳位后宽高会变成 0 或负数,mss 会抛 "Region has zero or negative size";
|
|
815
|
+
# 那个异常在 _work() 里被吞掉只打一行日志,于是每帧刷一条、背景永远画不出来。
|
|
816
|
+
if gr - gl <= 0 or gb - gt <= 0:
|
|
817
|
+
return None
|
|
759
818
|
shot = sct.grab({"left": gl, "top": gt,
|
|
760
819
|
"width": gr - gl, "height": gb - gt})
|
|
761
820
|
im = Image.frombytes("RGB", shot.size, shot.rgb)
|
|
@@ -814,15 +873,9 @@ class LiveGlass:
|
|
|
814
873
|
panel = Image.composite(lens_2x, base_2x, BOX_UNION_2X)
|
|
815
874
|
panel = Image.alpha_composite(
|
|
816
875
|
panel.convert("RGBA"), OVERLAY_2X).convert("RGB")
|
|
817
|
-
# 麦克风图标:先回到全分辨率再贴,避免坐标落在 2x 画布外
|
|
818
876
|
out = panel.convert("RGB").resize((W, H), Image.Resampling.BILINEAR)
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
mic_bg = float(np.median(np.asarray(mic_box))) / 255.0
|
|
822
|
-
out = out.convert("RGBA")
|
|
823
|
-
icon = MIC_LIGHT if mic_bg < 0.48 else MIC_DARK
|
|
824
|
-
out.paste(icon, MIC_POS, icon)
|
|
825
|
-
return finish(out.convert("RGB"))
|
|
877
|
+
# 麦克风由 Canvas 矢量绘制(_build_ui),这里不再叠位图,避免两层图标。
|
|
878
|
+
return finish(out)
|
|
826
879
|
|
|
827
880
|
def _work(self):
|
|
828
881
|
"""抓取 + 模糊 + 合成全在后台,主线程只负责显示最新一帧。"""
|
|
@@ -832,6 +885,11 @@ class LiveGlass:
|
|
|
832
885
|
if self._pause.is_set():
|
|
833
886
|
time.sleep(0.03)
|
|
834
887
|
continue
|
|
888
|
+
if getattr(self, "_collapsed", False):
|
|
889
|
+
# 折叠态只剩一条标题栏,玻璃遮罩仍是展开时的尺寸;继续抓会在
|
|
890
|
+
# 合成时抛 images do not match(每帧一条,能把日志刷到几十 MB)。
|
|
891
|
+
time.sleep(0.05)
|
|
892
|
+
continue
|
|
835
893
|
t0 = time.perf_counter()
|
|
836
894
|
try:
|
|
837
895
|
with self._geo_lock:
|
|
@@ -865,6 +923,33 @@ class LiveGlass:
|
|
|
865
923
|
else:
|
|
866
924
|
time.sleep(max(0.0, 1 / max(1, self._target_fps) - dt))
|
|
867
925
|
|
|
926
|
+
def _fit_lines(self, text, font, max_width, max_height):
|
|
927
|
+
"""按框宽/框高折行并截断,避免文字溢出气泡。"""
|
|
928
|
+
fobj = tkfont.Font(root=self.root, font=font)
|
|
929
|
+
ls = max(1, fobj.metrics("linespace"))
|
|
930
|
+
max_lines = max(1, int(max_height // ls))
|
|
931
|
+
lines = []
|
|
932
|
+
no_start = ",。、;:!?)》」』…,.;:!?)]}"
|
|
933
|
+
for para in str(text).splitlines() or [""]:
|
|
934
|
+
cur = ""
|
|
935
|
+
for ch in para:
|
|
936
|
+
if not cur or fobj.measure(cur + ch) <= max_width:
|
|
937
|
+
cur += ch
|
|
938
|
+
elif ch in no_start:
|
|
939
|
+
# 标点不能单独掉到下一行(会变成孤零零一个句号)
|
|
940
|
+
cur += ch
|
|
941
|
+
else:
|
|
942
|
+
lines.append(cur)
|
|
943
|
+
cur = ch
|
|
944
|
+
lines.append(cur)
|
|
945
|
+
if len(lines) > max_lines:
|
|
946
|
+
lines = lines[:max_lines]
|
|
947
|
+
last = lines[-1]
|
|
948
|
+
while last and fobj.measure(last + "…") > max_width:
|
|
949
|
+
last = last[:-1]
|
|
950
|
+
lines[-1] = (last + "…") if last else "…"
|
|
951
|
+
return lines or [""]
|
|
952
|
+
|
|
868
953
|
def _text(self, x, y, text, anchor="center", fill="#f2f8ff",
|
|
869
954
|
font=None, max_width=None, tags=()):
|
|
870
955
|
"""按字符拆成 Canvas 文本;每个字单独适配背后的明暗。"""
|
|
@@ -957,7 +1042,7 @@ class LiveGlass:
|
|
|
957
1042
|
return "break"
|
|
958
1043
|
self._editing = True
|
|
959
1044
|
if self._title_entry is None:
|
|
960
|
-
self._title_entry = tk.Entry(self.root, font=f(
|
|
1045
|
+
self._title_entry = tk.Entry(self.root, font=f(15), relief="flat", bd=0,
|
|
961
1046
|
bg="#1b2330", fg="#f7fbff", insertbackground="#f7fbff")
|
|
962
1047
|
self._title_entry.bind("<Return>", self._commit_title)
|
|
963
1048
|
self._title_entry.bind("<Escape>", self._cancel_title)
|
|
@@ -966,7 +1051,7 @@ class LiveGlass:
|
|
|
966
1051
|
self._title_entry.insert(0, self.name)
|
|
967
1052
|
if self._title_item is not None:
|
|
968
1053
|
self.canvas.delete(self._title_item)
|
|
969
|
-
self._title_item = self.canvas.create_window(
|
|
1054
|
+
self._title_item = self.canvas.create_window(68, 38, anchor="w",
|
|
970
1055
|
window=self._title_entry, width=180)
|
|
971
1056
|
self._title_entry.focus_set()
|
|
972
1057
|
self._title_entry.select_range(0, "end")
|
|
@@ -1004,15 +1089,23 @@ class LiveGlass:
|
|
|
1004
1089
|
|
|
1005
1090
|
def toggle_collapse(self, _event=None):
|
|
1006
1091
|
global H
|
|
1092
|
+
# 先读真实坐标再切高度:_x/_y 只是启动时的缓存,拖动/缩放后已过期。
|
|
1093
|
+
self._sync_pos()
|
|
1007
1094
|
self._collapsed = not getattr(self, "_collapsed", False)
|
|
1008
1095
|
if self._collapsed:
|
|
1009
1096
|
self._saved_h = H
|
|
1010
1097
|
H = 2 * 12 + 54
|
|
1098
|
+
self._collapsed_frame = self._make_collapsed_frame()
|
|
1099
|
+
# 折叠态只需要标题栏:不重建整窗几何,直接缩窗口 + 只画头。
|
|
1100
|
+
# 这样不会在小高度下生成负尺寸的输入框/主体玻璃。
|
|
1101
|
+
self.root.geometry(f"{W}x{H}+{self._x}+{self._y}")
|
|
1102
|
+
self._build_ui()
|
|
1011
1103
|
else:
|
|
1012
1104
|
H = getattr(self, "_saved_h", H)
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1105
|
+
self._collapsed_frame = None
|
|
1106
|
+
rebuild_geometry(W, H)
|
|
1107
|
+
self.root.geometry(f"{W}x{H}+{self._x}+{self._y}")
|
|
1108
|
+
self._rebuild(W, H)
|
|
1016
1109
|
try:
|
|
1017
1110
|
if self._protocol is not None:
|
|
1018
1111
|
self._protocol.action("collapse" if self._collapsed else "expand")
|
|
@@ -1037,9 +1130,10 @@ class LiveGlass:
|
|
|
1037
1130
|
c.delete("all")
|
|
1038
1131
|
# 背景图 item 先建,后续文字都在它上面
|
|
1039
1132
|
self._bg = c.create_image(0, 0, anchor="nw")
|
|
1040
|
-
|
|
1133
|
+
frame = self._collapsed_frame if getattr(self, "_collapsed", False) else self._last_frame
|
|
1134
|
+
if frame is not None:
|
|
1041
1135
|
try:
|
|
1042
|
-
self._img = ImageTk.PhotoImage(
|
|
1136
|
+
self._img = ImageTk.PhotoImage(frame, master=self.canvas)
|
|
1043
1137
|
c.itemconfigure(self._bg, image=self._img)
|
|
1044
1138
|
except Exception:
|
|
1045
1139
|
pass
|
|
@@ -1049,11 +1143,29 @@ class LiveGlass:
|
|
|
1049
1143
|
c.tag_lower("preview-bg")
|
|
1050
1144
|
|
|
1051
1145
|
w, h = W, H
|
|
1146
|
+
# 状态点:仓库实现里的"亮着的蓝点 + 很淡的光环"。纯色圆点像贴纸,
|
|
1147
|
+
# 带一圈环才像状态灯;录音时变红。
|
|
1148
|
+
dot = "#e05a5a" if (self._mic_proc is not None and self._mic_proc.poll() is None) else "#0a84ff"
|
|
1149
|
+
# 标题栏玻璃是 y=12..66,中心 39;右边 ▾/✕ 也在 38。点、名字、
|
|
1150
|
+
# 状态必须同一条水平中线,且字号要能完整落在条内,不能压到气泡区。
|
|
1151
|
+
cy = 38
|
|
1152
|
+
dot_x = 54
|
|
1153
|
+
title_x = 68
|
|
1154
|
+
# 点放大但保留光环;之前半径 3/7 在 54px 的标题条里太小。
|
|
1155
|
+
c.create_oval(dot_x - 9, cy - 9, dot_x + 9, cy + 9,
|
|
1156
|
+
outline="#2b4d73" if dot == "#0a84ff" else "#6b3030", width=1)
|
|
1157
|
+
c.create_oval(dot_x - 4, cy - 4, dot_x + 4, cy + 4, fill=dot, outline="")
|
|
1052
1158
|
name = self.name[:10]
|
|
1053
|
-
|
|
1054
|
-
|
|
1159
|
+
title_font = f(16, True)
|
|
1160
|
+
self._text(title_x, cy, name, anchor="w", fill="#f7fbff",
|
|
1161
|
+
font=title_font, tags=("title",))
|
|
1055
1162
|
count = self._status or f"{len(self.messages)} 条"
|
|
1056
|
-
|
|
1163
|
+
try:
|
|
1164
|
+
nx = title_x + tkfont.Font(root=self.root, font=title_font).measure(name) + 10
|
|
1165
|
+
except Exception:
|
|
1166
|
+
nx = title_x + 72
|
|
1167
|
+
self._text(nx, cy, count, anchor="w",
|
|
1168
|
+
fill="#ffb4b4" if self._status else "#dbe6f5",
|
|
1057
1169
|
font=f(11))
|
|
1058
1170
|
# 右上角按钮跟随窗口宽度内收
|
|
1059
1171
|
self._text(w - 94, 38, "▾", anchor="w",
|
|
@@ -1061,31 +1173,45 @@ class LiveGlass:
|
|
|
1061
1173
|
self._text(w - 58, 38, "✕", anchor="w", fill="#9fb0c8",
|
|
1062
1174
|
font=f(13), tags=("close",))
|
|
1063
1175
|
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
fill="#a8b7c9", font=f(9), tags=("feed",))
|
|
1176
|
+
if getattr(self, "_collapsed", False):
|
|
1177
|
+
self.canvas.config(width=w, height=h)
|
|
1178
|
+
c.tag_bind("close", "<Button-1>", self._close)
|
|
1179
|
+
c.tag_bind("collapse", "<Button-1>", self.toggle_collapse)
|
|
1180
|
+
# 双击改名:单击要留给"按住标题拖动窗口",两者不能抢同一个事件
|
|
1181
|
+
c.tag_bind("title", "<Double-Button-1>", self._edit_title)
|
|
1182
|
+
self._bar_len = max(72, int(w * BAR_FRAC))
|
|
1183
|
+
self._bar = c.create_line(w // 2 - self._bar_len // 2, BAR_Y,
|
|
1184
|
+
w // 2 + self._bar_len // 2, BAR_Y,
|
|
1185
|
+
width=BAR_THICK, fill="#b8cbe0",
|
|
1186
|
+
capstyle="round")
|
|
1187
|
+
return
|
|
1077
1188
|
|
|
1078
|
-
|
|
1189
|
+
shown = self._visible_messages()
|
|
1190
|
+
# 每条消息的头部(时间戳)和正文都在自己的框里;正文按框高折行/截断,
|
|
1191
|
+
# 不再用气泡中心当基线,否则两行以上会溢出框外(时间也会被顶到框上)。
|
|
1079
1192
|
for i, msg in enumerate(shown):
|
|
1080
|
-
x0, y0, x1,
|
|
1193
|
+
x0, y0, x1, y1, _r = SHAPES[i]
|
|
1081
1194
|
is_user = str(msg.get("role") or "").lower() in ("user", "me", "human")
|
|
1082
1195
|
tx = (x1 - 26) if is_user else (x0 + 26)
|
|
1083
|
-
ta = "
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1196
|
+
ta = "ne" if is_user else "nw"
|
|
1197
|
+
stamp = str(msg.get("stamp") or "")
|
|
1198
|
+
head_h = 26 if stamp else 4
|
|
1199
|
+
if stamp:
|
|
1200
|
+
self._text(tx, y0 + 2, stamp, anchor=ta,
|
|
1201
|
+
fill="#a8b7c9", font=f(9), tags=("feed",))
|
|
1202
|
+
max_w = max(80, x1 - x0 - 52)
|
|
1203
|
+
avail_h = max(12, (y1 - y0) - head_h - 2)
|
|
1204
|
+
body_font = f(11)
|
|
1205
|
+
lines = self._fit_lines(msg.get("text") or "", body_font, max_w, avail_h)
|
|
1206
|
+
fobj = tkfont.Font(root=self.root, font=body_font)
|
|
1207
|
+
ls = fobj.metrics("linespace")
|
|
1208
|
+
ty = y0 + head_h + 2
|
|
1209
|
+
for li, line in enumerate(lines):
|
|
1210
|
+
self._text(tx, ty + li * ls, line, anchor=ta,
|
|
1211
|
+
fill="#edf6ff", font=body_font, tags=("feed",))
|
|
1087
1212
|
if not shown:
|
|
1088
|
-
|
|
1213
|
+
# 空态:`bh` 是旧代码的局部变量,这里只能从 SHAPES 反推第一格高度
|
|
1214
|
+
self._text(w // 2, SHAPES[0][1] + (SHAPES[0][3] - SHAPES[0][1]) // 2,
|
|
1089
1215
|
"在这里跟它说话 · 滚轮翻历史", anchor="center",
|
|
1090
1216
|
fill="#8e9db2", font=f(12), tags=("feed",))
|
|
1091
1217
|
|
|
@@ -1113,6 +1239,18 @@ class LiveGlass:
|
|
|
1113
1239
|
self._text((x0 + x1) // 2, y0 + 27, "发送", tags=("send", "feed"),
|
|
1114
1240
|
fill="#ffffff", font=f(12, True))
|
|
1115
1241
|
|
|
1242
|
+
# 麦克风:直接用 PR #15 的 mic-dsh.png 图标(白色/录音红),
|
|
1243
|
+
# 不再用几条线画一个像"i"的代替品;Canvas 图不依赖合成帧,预览也可见。
|
|
1244
|
+
if not getattr(self, "_collapsed", False):
|
|
1245
|
+
active = self._mic_proc is not None and self._mic_proc.poll() is None
|
|
1246
|
+
color = (255, 128, 128, 0) if active else (224, 238, 255, 0)
|
|
1247
|
+
icon_img = Image.new("RGBA", MIC_MASK.size, color)
|
|
1248
|
+
icon_img.putalpha(MIC_MASK)
|
|
1249
|
+
self._mic_icon = ImageTk.PhotoImage(icon_img, master=self.canvas)
|
|
1250
|
+
c.create_image(67, H - 58, image=self._mic_icon,
|
|
1251
|
+
tags=("mic", "micart"))
|
|
1252
|
+
c.tag_bind("mic", "<Button-1>", lambda _e: self._toggle_mic())
|
|
1253
|
+
|
|
1116
1254
|
# 最上面正中的细条,长度随窗口宽度走
|
|
1117
1255
|
self._bar_len = max(72, int(w * BAR_FRAC))
|
|
1118
1256
|
self._bar = c.create_line(w // 2 - self._bar_len // 2, BAR_Y,
|
|
@@ -1123,6 +1261,7 @@ class LiveGlass:
|
|
|
1123
1261
|
c.tag_bind("close", "<Button-1>", self._close)
|
|
1124
1262
|
c.tag_bind("send", "<Button-1>", self._send)
|
|
1125
1263
|
c.tag_bind("collapse", "<Button-1>", self.toggle_collapse)
|
|
1264
|
+
# 同上:单击留给拖动,改名走双击
|
|
1126
1265
|
c.tag_bind("title", "<Double-Button-1>", self._edit_title)
|
|
1127
1266
|
c.tag_bind("inputzone", "<Button-1>", self._focus_input)
|
|
1128
1267
|
c.tag_raise("close")
|
|
@@ -1192,6 +1331,7 @@ class LiveGlass:
|
|
|
1192
1331
|
self.canvas.delete("all")
|
|
1193
1332
|
self._img = None
|
|
1194
1333
|
self._bg = None
|
|
1334
|
+
self._collapsed_frame = None
|
|
1195
1335
|
self._text_items = []
|
|
1196
1336
|
self._text_colors = {}
|
|
1197
1337
|
self._text_shadows = {}
|
|
@@ -1276,11 +1416,20 @@ class LiveGlass:
|
|
|
1276
1416
|
|
|
1277
1417
|
def _snapshot_mode(self):
|
|
1278
1418
|
"""只给文档截图:停抓取、关 WDA,保留最后一帧(不改变正常路径)。"""
|
|
1419
|
+
self._snapshot_done = True
|
|
1279
1420
|
self._pause.set()
|
|
1280
1421
|
if self._excluded:
|
|
1281
1422
|
self._set_exclude(False)
|
|
1282
1423
|
|
|
1283
1424
|
def _tick(self):
|
|
1425
|
+
# 截图模式:worker 已暂停,保持 WDA 关闭;否则下一拍会重新开 WDA,
|
|
1426
|
+
# 窗口再次从截图/录屏里消失,_snapshot_mode 等于没做。
|
|
1427
|
+
if SNAPSHOT and getattr(self, "_snapshot_done", False):
|
|
1428
|
+
self._pause.set()
|
|
1429
|
+
if self._excluded:
|
|
1430
|
+
self._set_exclude(False)
|
|
1431
|
+
self.root.after(100, self._tick)
|
|
1432
|
+
return
|
|
1284
1433
|
if PREVIEW:
|
|
1285
1434
|
self._pause.set()
|
|
1286
1435
|
if self._excluded:
|
|
@@ -1291,7 +1440,13 @@ class LiveGlass:
|
|
|
1291
1440
|
if self._resize:
|
|
1292
1441
|
self.root.after(INTERVAL_MS, self._tick)
|
|
1293
1442
|
return
|
|
1294
|
-
if
|
|
1443
|
+
if getattr(self, "_collapsed", False):
|
|
1444
|
+
# 折叠态用 _make_collapsed_frame 的静态背景;_work 也停抓取。
|
|
1445
|
+
# 若还让这里消费旧队列,会把 560px 的帧贴回 78px 的 Canvas,
|
|
1446
|
+
# 底部圆角/暗边会闪一下,看起来像渲染错位。
|
|
1447
|
+
self.root.after(INTERVAL_MS, self._tick)
|
|
1448
|
+
return
|
|
1449
|
+
if not self._excluded and os.environ.get("DSH_GLASS_WDA") != "0":
|
|
1295
1450
|
self._set_exclude(True)
|
|
1296
1451
|
self._pause.clear()
|
|
1297
1452
|
|
|
@@ -1425,7 +1580,13 @@ class LiveGlass:
|
|
|
1425
1580
|
self._preview_resize(nw, nh)
|
|
1426
1581
|
return
|
|
1427
1582
|
if self._drag:
|
|
1428
|
-
|
|
1583
|
+
# 和缩放分支一样必须钳位:拖到屏幕外时窗口坐标会变负,
|
|
1584
|
+
# 抓屏区域随之越界(高度算出负数),背景永远画不出来 —— 表现为花屏。
|
|
1585
|
+
nx = e.x_root - self._drag[0]
|
|
1586
|
+
ny = e.y_root - self._drag[1]
|
|
1587
|
+
nx = max(0, min(max(0, self._sw - self.root.winfo_width()), nx))
|
|
1588
|
+
ny = max(0, min(max(0, self._sh - self.root.winfo_height()), ny))
|
|
1589
|
+
self.root.geometry(f"+{nx}+{ny}")
|
|
1429
1590
|
|
|
1430
1591
|
def _release(self, e):
|
|
1431
1592
|
if self._resize:
|
|
@@ -1435,9 +1596,11 @@ class LiveGlass:
|
|
|
1435
1596
|
w = max(MIN_W, self.root.winfo_width())
|
|
1436
1597
|
h = max(MIN_H, self.root.winfo_height())
|
|
1437
1598
|
self._rebuild(w, h)
|
|
1599
|
+
self._sync_pos()
|
|
1438
1600
|
self._hover(e)
|
|
1439
1601
|
return
|
|
1440
1602
|
self._drag = None
|
|
1603
|
+
self._sync_pos()
|
|
1441
1604
|
|
|
1442
1605
|
def run(self):
|
|
1443
1606
|
self.root.bind("<Escape>", self._close)
|