dsh-watch-together 0.2.12 → 0.2.13
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 +64 -0
- package/README.md +41 -5
- package/docs/VOICE.zh.md +44 -0
- package/dsh.plugin.json +12 -12
- package/examples/floating-chat/glass_capture.py +362 -341
- package/examples/floating-chat/glass_gpu.py +1 -1
- package/examples/floating-chat/window.py +1387 -1215
- package/examples/floating-chat/window_widgets.py +6 -3
- package/examples/voice-edge-tts/index.mjs +77 -7
- package/examples/voice-edge-tts/stream/client.mjs +8 -0
- package/examples/voice-edge-tts/stream/daemon.py +30 -0
- package/package.json +118 -118
- package/scripts/check-multimonitor.py +102 -0
- package/src/companion-api.js +88 -0
- package/src/playhead.js +75 -0
- package/src/public-api.js +7 -0
- package/src/voice-adapter.js +8 -1
- package/src/voice.js +9 -0
- package/src/watch-loop.js +26 -0
|
@@ -1,1215 +1,1387 @@
|
|
|
1
|
-
#!/usr/bin/env python3
|
|
2
|
-
"""陪看悬浮窗:不铺全屏背景图,实时抓窗口背后的桌面并模糊成液态玻璃。
|
|
3
|
-
|
|
4
|
-
三层,各自不知道对方的实现:
|
|
5
|
-
|
|
6
|
-
glass_geometry.py 一套尺寸的全部贴图(不可变,尺寸/形状变了换一个新对象)
|
|
7
|
-
glass_cpu.py CPU 合成:抓屏 → 模糊 → 边缘折射 → 内框透镜 → 染色 → 色散
|
|
8
|
-
glass_gpu.py 同一件事搬到 GPU(moderngl / OpenGL 3.3+)
|
|
9
|
-
window.py 这个文件:Tk 窗口、聊天流、文字层、拖动/缩放/折叠/改名
|
|
10
|
-
|
|
11
|
-
`glass_capture.py` 是两条渲染路径共用的抓屏与运动判定 —— 历史上同一条判断
|
|
12
|
-
在 CPU / GPU 各写一份,于是同一个 bug 也要修两遍(#39、#48)。
|
|
13
|
-
|
|
14
|
-
关键点:
|
|
15
|
-
- Windows 10 2004+ 的 SetWindowDisplayAffinity(WDA_EXCLUDEFROMCAPTURE):让本窗口
|
|
16
|
-
从屏幕捕获里消失,于是抓自己所在区域时抓到的是「背后的桌面」,不会递归。
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
import
|
|
25
|
-
import
|
|
26
|
-
import
|
|
27
|
-
import
|
|
28
|
-
import
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
_u.SetProcessDpiAwarenessContext
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
)
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
#
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
""
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
def
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
try:
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
self.
|
|
417
|
-
|
|
418
|
-
if
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
self.
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
self
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
def
|
|
539
|
-
if self.
|
|
540
|
-
return
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
self.
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
self.
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
return
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
self.
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
self.
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
self.
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
self.
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
self.
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
x0
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
self.
|
|
863
|
-
|
|
864
|
-
self.
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
""
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
if
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
self.
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
self.
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
self.
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
self.
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
self.
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
if
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
self.
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
self.
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
if
|
|
1105
|
-
return
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
self.
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
if
|
|
1215
|
-
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""陪看悬浮窗:不铺全屏背景图,实时抓窗口背后的桌面并模糊成液态玻璃。
|
|
3
|
+
|
|
4
|
+
三层,各自不知道对方的实现:
|
|
5
|
+
|
|
6
|
+
glass_geometry.py 一套尺寸的全部贴图(不可变,尺寸/形状变了换一个新对象)
|
|
7
|
+
glass_cpu.py CPU 合成:抓屏 → 模糊 → 边缘折射 → 内框透镜 → 染色 → 色散
|
|
8
|
+
glass_gpu.py 同一件事搬到 GPU(moderngl / OpenGL 3.3+)
|
|
9
|
+
window.py 这个文件:Tk 窗口、聊天流、文字层、拖动/缩放/折叠/改名
|
|
10
|
+
|
|
11
|
+
`glass_capture.py` 是两条渲染路径共用的抓屏与运动判定 —— 历史上同一条判断
|
|
12
|
+
在 CPU / GPU 各写一份,于是同一个 bug 也要修两遍(#39、#48)。
|
|
13
|
+
|
|
14
|
+
关键点:
|
|
15
|
+
- Windows 10 2004+ 的 SetWindowDisplayAffinity(WDA_EXCLUDEFROMCAPTURE):让本窗口
|
|
16
|
+
从屏幕捕获里消失,于是抓自己所在区域时抓到的是「背后的桌面」,不会递归。
|
|
17
|
+
代价是远程桌面/录屏/截图里整窗也看不见;用 `glass: solid` 切纯色底板,
|
|
18
|
+
不抓屏、不设 WDA,任何捕获方式都能看到。
|
|
19
|
+
- 抓到的背景按需重合成(静止时跳帧),所以是实时模糊,不是静态截图。
|
|
20
|
+
- 这不是 DWM Acrylic;Acrylic 需要系统「透明效果」开启,这里是软件实时模糊。
|
|
21
|
+
|
|
22
|
+
渲染后端:默认 CPU。`DSH_GLASS_NATIVE=1` 或 `DSH_GLASS_GPU=1` 走 GPU。
|
|
23
|
+
"""
|
|
24
|
+
import ctypes
|
|
25
|
+
import json
|
|
26
|
+
import os
|
|
27
|
+
import queue
|
|
28
|
+
import subprocess
|
|
29
|
+
import threading
|
|
30
|
+
import time
|
|
31
|
+
from ctypes import wintypes
|
|
32
|
+
from pathlib import Path
|
|
33
|
+
|
|
34
|
+
# 必须在 import tkinter / 创建任何窗口之前声明 per-monitor v2 DPI 感知。
|
|
35
|
+
_u = ctypes.windll.user32
|
|
36
|
+
try:
|
|
37
|
+
_u.SetProcessDpiAwarenessContext.argtypes = [ctypes.c_void_p]
|
|
38
|
+
_u.SetProcessDpiAwarenessContext.restype = ctypes.c_bool
|
|
39
|
+
_u.SetProcessDpiAwarenessContext(ctypes.c_void_p(-4))
|
|
40
|
+
except Exception:
|
|
41
|
+
pass
|
|
42
|
+
|
|
43
|
+
try:
|
|
44
|
+
ctypes.windll.winmm.timeBeginPeriod(1)
|
|
45
|
+
except Exception:
|
|
46
|
+
pass
|
|
47
|
+
|
|
48
|
+
# ---------------------------------------------------------------- 依赖预检
|
|
49
|
+
#
|
|
50
|
+
# 必须在 import mss / numpy / PIL / glass_* **之前**:pythonw 没有控制台,
|
|
51
|
+
# 导入失败时 Python 直接退出,用户只看到"窗口没出来",连一行提示都没有(#76)。
|
|
52
|
+
# 这里用 ctypes 弹 MessageBox(不依赖 tkinter —— 它也可能缺),并把原因落盘。
|
|
53
|
+
|
|
54
|
+
def _missing_deps() -> list:
|
|
55
|
+
missing = []
|
|
56
|
+
for module, package in (("tkinter", "tkinter(Python 安装时勾选 tcl/tk)"),
|
|
57
|
+
("mss", "mss"), ("numpy", "numpy"), ("PIL", "pillow")):
|
|
58
|
+
try:
|
|
59
|
+
__import__(module)
|
|
60
|
+
except Exception:
|
|
61
|
+
missing.append(package)
|
|
62
|
+
return missing
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def _die_missing(missing: list) -> None:
|
|
66
|
+
log_path = Path.home() / ".dsh" / "watch-window-error.log"
|
|
67
|
+
text = ("悬浮窗启动失败:缺少 " + "、".join(missing) + "\n\n"
|
|
68
|
+
+ "装一次就好:\n pip install pillow numpy mss\n\n"
|
|
69
|
+
+ f"详情:{log_path}")
|
|
70
|
+
try:
|
|
71
|
+
log_path.parent.mkdir(parents=True, exist_ok=True)
|
|
72
|
+
log_path.write_text(text + "\n", encoding="utf-8")
|
|
73
|
+
except Exception:
|
|
74
|
+
pass
|
|
75
|
+
try:
|
|
76
|
+
ctypes.windll.user32.MessageBoxW(None, text, "dsh-watch-together", 0x10)
|
|
77
|
+
except Exception:
|
|
78
|
+
print(text, flush=True)
|
|
79
|
+
raise SystemExit(2)
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
if __name__ == "__main__":
|
|
83
|
+
_missing = _missing_deps()
|
|
84
|
+
if _missing:
|
|
85
|
+
_die_missing(_missing)
|
|
86
|
+
|
|
87
|
+
import tkinter as tk
|
|
88
|
+
|
|
89
|
+
import mss
|
|
90
|
+
import numpy as np
|
|
91
|
+
from PIL import Image, ImageTk
|
|
92
|
+
|
|
93
|
+
from glass_capture import CaptureSource, MotionTracker
|
|
94
|
+
from glass_cpu import CpuRenderer
|
|
95
|
+
from glass_geometry import (
|
|
96
|
+
BAR_FRAC, BAR_THICK, BAR_Y, DEFAULT_H, DEFAULT_W, KEY, MESSAGE_SLOTS,
|
|
97
|
+
MIC_SIZE, MIN_H, MIN_W, Geometry, collapsed_height, shapes_for,
|
|
98
|
+
)
|
|
99
|
+
from window_widgets import ChatFeed, Fonts, TextLayer
|
|
100
|
+
|
|
101
|
+
try: # 悬浮窗接口:插件写 window-events.jsonl,窗口回 window-outbox.jsonl
|
|
102
|
+
from window_protocol import WindowProtocol
|
|
103
|
+
except Exception:
|
|
104
|
+
WindowProtocol = None
|
|
105
|
+
|
|
106
|
+
INTERVAL_MS = 4
|
|
107
|
+
MAX_FPS = 120
|
|
108
|
+
# 静止画面下「有没有变」只能靠抓屏看,单次 grab ~7ms;空转按 60fps 就是 40% 单核。
|
|
109
|
+
# 降到 30fps 探针,有变化时 idle 立刻归零、下一拍恢复全速。
|
|
110
|
+
PROBE_FPS = 30
|
|
111
|
+
|
|
112
|
+
# 仅用于文档截图/CI 视觉检查:暂停抓屏并把 WDA 关掉,窗口才会出现在截图里
|
|
113
|
+
PREVIEW = os.environ.get("DSH_GLASS_PREVIEW") == "1"
|
|
114
|
+
# 文档截图:正常抓屏 2.5 秒后暂停并取消 WDA,保留最后一帧玻璃
|
|
115
|
+
SNAPSHOT = os.environ.get("DSH_GLASS_SNAPSHOT") == "1"
|
|
116
|
+
# GPU:native 表示全分辨率采样(边缘透镜没有 1/4 放大的像素感)
|
|
117
|
+
NATIVE = os.environ.get("DSH_GLASS_NATIVE") == "1"
|
|
118
|
+
USE_GPU = NATIVE or os.environ.get("DSH_GLASS_GPU") == "1"
|
|
119
|
+
# 气泡底板(和 glass_geometry.BUBBLE_PLATE 同一个开关):关掉 = 回到逐字翻色,
|
|
120
|
+
# 只用来拍新旧对比图,正常别动。
|
|
121
|
+
PLATE = os.environ.get("DSH_GLASS_PLATE", "1") != "0"
|
|
122
|
+
|
|
123
|
+
HERE = Path(__file__).resolve().parent
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
class _RECT(ctypes.Structure):
|
|
127
|
+
_fields_ = [("left", ctypes.c_long), ("top", ctypes.c_long),
|
|
128
|
+
("right", ctypes.c_long), ("bottom", ctypes.c_long)]
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
# ---------------------------------------------------------------- 路径与配置
|
|
132
|
+
|
|
133
|
+
def cache_dir() -> Path:
|
|
134
|
+
d = os.environ.get("DSH_VIDEO_CACHE_DIR")
|
|
135
|
+
if d:
|
|
136
|
+
return Path(d)
|
|
137
|
+
local = os.environ.get("LOCALAPPDATA")
|
|
138
|
+
if os.name == "nt" and local:
|
|
139
|
+
return Path(local) / "dsh-video-storyboard" / "cache"
|
|
140
|
+
return Path(os.environ.get("XDG_CACHE_HOME") or "~/.cache").expanduser() / "dsh-video-storyboard"
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
CHAT_FILE = cache_dir() / "chat.jsonl"
|
|
144
|
+
CONFIG = Path(os.environ.get("DSH_HOME") or os.path.expanduser("~/.dsh")) / "watch-window.json"
|
|
145
|
+
MIC_SCRIPT = HERE / "mic.py"
|
|
146
|
+
|
|
147
|
+
# **心跳**:插件侧靠它区分「进程还在」和「窗口真的在画」。只看 pid 不够 ——
|
|
148
|
+
# pid 会被复用,进程也可能活着但界面已经卡死,那样宿主会永远认为窗还开着,
|
|
149
|
+
# 于是再也不重开(see-now issue #2 第 3 条)。每秒最多写一次。
|
|
150
|
+
HEARTBEAT = cache_dir() / "window-heartbeat"
|
|
151
|
+
HEARTBEAT_EVERY = 1.0
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def read_config() -> dict:
|
|
155
|
+
try:
|
|
156
|
+
return json.loads(CONFIG.read_text(encoding="utf-8")) or {}
|
|
157
|
+
except Exception:
|
|
158
|
+
return {}
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
def write_config(patch: dict) -> None:
|
|
162
|
+
cfg = read_config()
|
|
163
|
+
cfg.update(patch or {})
|
|
164
|
+
try:
|
|
165
|
+
CONFIG.parent.mkdir(parents=True, exist_ok=True)
|
|
166
|
+
tmp = CONFIG.with_suffix(".json.tmp")
|
|
167
|
+
tmp.write_text(json.dumps(cfg, ensure_ascii=False, indent=2), encoding="utf-8")
|
|
168
|
+
os.replace(tmp, CONFIG)
|
|
169
|
+
except Exception:
|
|
170
|
+
pass
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
def capturable() -> bool:
|
|
174
|
+
"""录屏 / 截图能不能拍到窗口。
|
|
175
|
+
|
|
176
|
+
默认**不能**:走 `WDA_EXCLUDEFROMCAPTURE`,窗口不会出现在截图、直播、共享屏幕里。
|
|
177
|
+
要给别人看效果就在 `~/.dsh/watch-window.json` 写 `{ "capturable": true }`。
|
|
178
|
+
环境变量 `DSH_GLASS_WDA=0` 只对这一次启动有效(插件拉起窗口时不传环境变量)。
|
|
179
|
+
"""
|
|
180
|
+
if os.environ.get("DSH_GLASS_WDA") == "0":
|
|
181
|
+
return True
|
|
182
|
+
try:
|
|
183
|
+
return read_config().get("capturable") is True
|
|
184
|
+
except Exception:
|
|
185
|
+
return False
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
# 远程控制软件不一定置 SM_REMOTESESSION;它们的窗口/类名是最稳的旁证。
|
|
189
|
+
# 命中任意一个就按 remote 处理,用 solid 底板,保证对面能看到整窗。
|
|
190
|
+
REMOTE_MARKS = (
|
|
191
|
+
"gameviewer", "todesk", "sunlogin", "anydesk", "teamviewer",
|
|
192
|
+
"rustdesk", "parsec", "uu远程", "向日葵",
|
|
193
|
+
)
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
def remote_capture_likely() -> bool:
|
|
197
|
+
"""当前是不是远程/捕获环境:RDP 标志位 + 常见远程软件窗口。"""
|
|
198
|
+
try:
|
|
199
|
+
if ctypes.windll.user32.GetSystemMetrics(0x1000): # SM_REMOTESESSION
|
|
200
|
+
return True
|
|
201
|
+
except Exception:
|
|
202
|
+
pass
|
|
203
|
+
try:
|
|
204
|
+
u = ctypes.windll.user32
|
|
205
|
+
u.GetWindowTextLengthW.argtypes = [wintypes.HWND]
|
|
206
|
+
u.GetWindowTextLengthW.restype = ctypes.c_int
|
|
207
|
+
u.GetWindowTextW.argtypes = [wintypes.HWND, wintypes.LPWSTR, ctypes.c_int]
|
|
208
|
+
u.GetWindowTextW.restype = ctypes.c_int
|
|
209
|
+
u.GetClassNameW.argtypes = [wintypes.HWND, wintypes.LPWSTR, ctypes.c_int]
|
|
210
|
+
u.GetClassNameW.restype = ctypes.c_int
|
|
211
|
+
u.IsWindowVisible.argtypes = [wintypes.HWND]
|
|
212
|
+
u.IsWindowVisible.restype = wintypes.BOOL
|
|
213
|
+
hit = [False]
|
|
214
|
+
|
|
215
|
+
@ctypes.WINFUNCTYPE(wintypes.BOOL, wintypes.HWND, wintypes.LPARAM)
|
|
216
|
+
def visit(hwnd, _lparam):
|
|
217
|
+
if not u.IsWindowVisible(hwnd):
|
|
218
|
+
return True
|
|
219
|
+
n = u.GetWindowTextLengthW(hwnd)
|
|
220
|
+
title = ""
|
|
221
|
+
if n > 0:
|
|
222
|
+
buf = ctypes.create_unicode_buffer(n + 1)
|
|
223
|
+
u.GetWindowTextW(hwnd, buf, n + 1)
|
|
224
|
+
title = buf.value
|
|
225
|
+
cls = ctypes.create_unicode_buffer(256)
|
|
226
|
+
u.GetClassNameW(hwnd, cls, 256)
|
|
227
|
+
hay = (title + " " + cls.value).lower()
|
|
228
|
+
if any(mark in hay for mark in REMOTE_MARKS):
|
|
229
|
+
hit[0] = True
|
|
230
|
+
return False
|
|
231
|
+
return True
|
|
232
|
+
|
|
233
|
+
u.EnumWindows(visit, 0)
|
|
234
|
+
return hit[0]
|
|
235
|
+
except Exception:
|
|
236
|
+
return False
|
|
237
|
+
|
|
238
|
+
|
|
239
|
+
def glass_mode() -> str:
|
|
240
|
+
"""玻璃背景模式:live(实时抓屏,默认)/ solid(纯色底板)。
|
|
241
|
+
|
|
242
|
+
`WDA_EXCLUDEFROMCAPTURE` 会让窗口在远程桌面、OBS、截图工具里**整窗消失**
|
|
243
|
+
(微软文档:“Everywhere else, the window does not appear at all”)。
|
|
244
|
+
本机看起来正常,因为 mss 抓到的是窗口背后的像素;远程端却连文字都看不到。
|
|
245
|
+
|
|
246
|
+
- `{ "glass": "solid" }`:不抓屏、不设 WDA,用纯色底板;任何捕获方式都能看到;
|
|
247
|
+
- `{ "glass": "live" }`:原来的实时玻璃;
|
|
248
|
+
- 不写 / `auto`:检测 `SM_REMOTESESSION` 和常见远程软件窗口,命中就用 solid。
|
|
249
|
+
"""
|
|
250
|
+
if os.environ.get("DSH_GLASS_SOLID") == "1":
|
|
251
|
+
return "solid"
|
|
252
|
+
if os.environ.get("DSH_GLASS_LIVE") == "1":
|
|
253
|
+
return "live"
|
|
254
|
+
try:
|
|
255
|
+
mode = str(read_config().get("glass") or "").strip().lower()
|
|
256
|
+
except Exception:
|
|
257
|
+
mode = ""
|
|
258
|
+
if mode in ("solid", "live"):
|
|
259
|
+
return mode
|
|
260
|
+
# 空值 / auto:RDP 标志位或常见远程软件窗口任一命中,就用 solid。
|
|
261
|
+
if remote_capture_likely():
|
|
262
|
+
return "solid"
|
|
263
|
+
return "live"
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
def append_user(text: str) -> None:
|
|
267
|
+
CHAT_FILE.parent.mkdir(parents=True, exist_ok=True)
|
|
268
|
+
with open(CHAT_FILE, "a", encoding="utf-8") as f:
|
|
269
|
+
f.write(json.dumps({"role": "user", "text": text, "at": int(time.time() * 1000)},
|
|
270
|
+
ensure_ascii=False) + "\n")
|
|
271
|
+
|
|
272
|
+
|
|
273
|
+
def asr_python() -> str:
|
|
274
|
+
"""麦克风用 ASR 的 Python,不是带 tkinter 的这个。"""
|
|
275
|
+
for key in ("DSH_ASR_PYTHON", "DSH_FLOAT_PYTHON"):
|
|
276
|
+
v = os.environ.get(key)
|
|
277
|
+
if v:
|
|
278
|
+
return v
|
|
279
|
+
return "python.exe" if os.name == "nt" else "python3"
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
def display_hz() -> int:
|
|
283
|
+
try:
|
|
284
|
+
u = ctypes.windll.user32
|
|
285
|
+
g = ctypes.windll.gdi32
|
|
286
|
+
dc = u.GetDC(0)
|
|
287
|
+
hz = int(g.GetDeviceCaps(dc, 116))
|
|
288
|
+
u.ReleaseDC(0, dc)
|
|
289
|
+
return hz if hz > 0 else 60
|
|
290
|
+
except Exception:
|
|
291
|
+
return 60
|
|
292
|
+
|
|
293
|
+
|
|
294
|
+
# ---------------------------------------------------------------- 窗口
|
|
295
|
+
|
|
296
|
+
# 虚拟桌面 = 所有显示器拼起来的物理像素范围。主屏固定从 (0,0) 起算,
|
|
297
|
+
# 第二块屏可能在它左边 / 上面,那时坐标本来就是负的 —— 只拿
|
|
298
|
+
# winfo_screenwidth()(= 主屏宽度)当边界,窗口就永远离不开主屏(#8)。
|
|
299
|
+
SM_XVIRTUALSCREEN, SM_YVIRTUALSCREEN = 76, 77
|
|
300
|
+
SM_CXVIRTUALSCREEN, SM_CYVIRTUALSCREEN = 78, 79
|
|
301
|
+
|
|
302
|
+
|
|
303
|
+
def virtual_bounds(fallback_w=0, fallback_h=0):
|
|
304
|
+
"""(x, y, w, h):虚拟桌面左上角与尺寸,物理像素。
|
|
305
|
+
|
|
306
|
+
取不到(非 Windows / 远程会话偶尔返回 0)就退回主屏 (0, 0, w, h)。
|
|
307
|
+
"""
|
|
308
|
+
try:
|
|
309
|
+
u = ctypes.windll.user32
|
|
310
|
+
vx = int(u.GetSystemMetrics(SM_XVIRTUALSCREEN))
|
|
311
|
+
vy = int(u.GetSystemMetrics(SM_YVIRTUALSCREEN))
|
|
312
|
+
vw = int(u.GetSystemMetrics(SM_CXVIRTUALSCREEN))
|
|
313
|
+
vh = int(u.GetSystemMetrics(SM_CYVIRTUALSCREEN))
|
|
314
|
+
if vw > 0 and vh > 0:
|
|
315
|
+
return vx, vy, vw, vh
|
|
316
|
+
except Exception:
|
|
317
|
+
pass
|
|
318
|
+
return 0, 0, int(fallback_w or 0), int(fallback_h or 0)
|
|
319
|
+
|
|
320
|
+
|
|
321
|
+
class LiveGlass:
|
|
322
|
+
def __init__(self):
|
|
323
|
+
cfg = read_config()
|
|
324
|
+
raw_name = cfg.get("name")
|
|
325
|
+
self.name = (raw_name.strip()[:24] if isinstance(raw_name, str) and raw_name.strip()
|
|
326
|
+
else "陪看")
|
|
327
|
+
|
|
328
|
+
self.feed = ChatFeed(CHAT_FILE)
|
|
329
|
+
self._mic_proc = None
|
|
330
|
+
self._protocol = WindowProtocol() if WindowProtocol is not None else None
|
|
331
|
+
self._collapsed = False
|
|
332
|
+
self._saved_h = DEFAULT_H
|
|
333
|
+
self._snapshot_done = False
|
|
334
|
+
self._title_entry = None
|
|
335
|
+
self._title_item = None
|
|
336
|
+
self._editing = False
|
|
337
|
+
self._hidden = False
|
|
338
|
+
self._cfg_key = None
|
|
339
|
+
|
|
340
|
+
self.root = tk.Tk()
|
|
341
|
+
self.root.withdraw()
|
|
342
|
+
self.root.overrideredirect(True)
|
|
343
|
+
self.root.title(self.name)
|
|
344
|
+
self.root.attributes("-topmost", True)
|
|
345
|
+
self.root.configure(bg=KEY)
|
|
346
|
+
self.root.attributes("-transparentcolor", KEY)
|
|
347
|
+
self.fonts = Fonts(self.root)
|
|
348
|
+
|
|
349
|
+
self._sw, self._sh = self.root.winfo_screenwidth(), self.root.winfo_screenheight()
|
|
350
|
+
# 主屏尺寸只用来算默认位置;能拖到哪、抓屏能抓到哪,看虚拟桌面(多显示器)
|
|
351
|
+
self._vx, self._vy, self._vw, self._vh = virtual_bounds(self._sw, self._sh)
|
|
352
|
+
w, h, x, y = self._initial_geometry(cfg.get("geometry"))
|
|
353
|
+
self._x, self._y = x, y
|
|
354
|
+
self._geo = Geometry.for_size(w, h)
|
|
355
|
+
# 入场:从下方 22px 滑上来(不碰 -alpha,避免和抠色冲突)
|
|
356
|
+
self._intro_target_y = y
|
|
357
|
+
self._intro_y = y + 22
|
|
358
|
+
self.root.geometry(f"{w}x{h}+{x}+{self._intro_y}")
|
|
359
|
+
|
|
360
|
+
self.canvas = tk.Canvas(self.root, width=w, height=h, bg=KEY,
|
|
361
|
+
highlightthickness=0, bd=0)
|
|
362
|
+
self.canvas.pack(fill="both", expand=True)
|
|
363
|
+
self.text = TextLayer(self.root, self.canvas, self.fonts)
|
|
364
|
+
|
|
365
|
+
self._img = None
|
|
366
|
+
self._last_frame = None
|
|
367
|
+
self._collapsed_frame = None
|
|
368
|
+
self._bg = None
|
|
369
|
+
self._entry = None
|
|
370
|
+
self._entry_item = None
|
|
371
|
+
self._bar = None
|
|
372
|
+
self._bar_len = 0
|
|
373
|
+
self._bar_color = "#b8cbe0"
|
|
374
|
+
self._aux_at = 0.0
|
|
375
|
+
|
|
376
|
+
self._hwnd = None
|
|
377
|
+
self._excluded = False
|
|
378
|
+
# 录屏模式(capturable)下 mss 会抓到窗口自己,必须每帧挖掉,见 fill_own_rect。
|
|
379
|
+
# 缓存起来:读配置是磁盘 IO,不能每帧做。
|
|
380
|
+
self._cappable = capturable()
|
|
381
|
+
# solid:远程/录屏下 WDA 会让整窗在对面消失;不抓屏、不设 WDA。
|
|
382
|
+
self._solid = glass_mode() == "solid"
|
|
383
|
+
self._drag = None
|
|
384
|
+
self._resize = None
|
|
385
|
+
self._preview_at = 0.0
|
|
386
|
+
|
|
387
|
+
self._q: queue.Queue = queue.Queue(maxsize=1)
|
|
388
|
+
self._stop = threading.Event()
|
|
389
|
+
self._pause = threading.Event()
|
|
390
|
+
self._geo_lock = threading.Lock()
|
|
391
|
+
self._source = CaptureSource((self._sw, self._sh), cappable=self._cappable,
|
|
392
|
+
solid=self._solid,
|
|
393
|
+
# 窗口落在副屏时,抓屏范围要按虚拟桌面钳
|
|
394
|
+
bounds=(self._vx, self._vy, self._vw, self._vh))
|
|
395
|
+
self._renderer = self._make_renderer(self._geo)
|
|
396
|
+
self._worker = threading.Thread(target=self._work, daemon=True)
|
|
397
|
+
|
|
398
|
+
self._hz = display_hz()
|
|
399
|
+
self._target_fps = min(MAX_FPS, self._hz)
|
|
400
|
+
self._ema = 1.0 / max(1, self._target_fps)
|
|
401
|
+
self._last_motion = 0.0
|
|
402
|
+
|
|
403
|
+
self.feed.reload()
|
|
404
|
+
self._layout()
|
|
405
|
+
|
|
406
|
+
self.root.deiconify()
|
|
407
|
+
self.root.update_idletasks()
|
|
408
|
+
self.text.refresh()
|
|
409
|
+
self._set_exclude(not capturable() and not self._solid)
|
|
410
|
+
self._worker.start()
|
|
411
|
+
self.root.lift()
|
|
412
|
+
self.root.after(80, self.root.lift)
|
|
413
|
+
self.root.after(120, self.root.focus_set)
|
|
414
|
+
self.root.after(60, self._tick)
|
|
415
|
+
self.root.after(30, lambda: self._intro_step(0))
|
|
416
|
+
self.root.after(250, self._poll_chat)
|
|
417
|
+
self.root.after(1200, self._keep_on_top)
|
|
418
|
+
if SNAPSHOT:
|
|
419
|
+
self.root.after(2600, self._snapshot_mode)
|
|
420
|
+
|
|
421
|
+
# ---- 渲染后端 ----
|
|
422
|
+
|
|
423
|
+
def _make_renderer(self, geo: Geometry):
|
|
424
|
+
if USE_GPU:
|
|
425
|
+
try:
|
|
426
|
+
from glass_gpu import GpuRenderer
|
|
427
|
+
return GpuRenderer(geo, MotionTracker(), native=NATIVE)
|
|
428
|
+
except Exception as exc:
|
|
429
|
+
print("GPU 初始化失败,回落到 CPU:", repr(exc), flush=True)
|
|
430
|
+
return CpuRenderer(geo)
|
|
431
|
+
|
|
432
|
+
# ---- 尺寸 ----
|
|
433
|
+
|
|
434
|
+
def _initial_geometry(self, geo):
|
|
435
|
+
sw, sh = self._sw, self._sh
|
|
436
|
+
w, h = DEFAULT_W, DEFAULT_H
|
|
437
|
+
# 默认位置仍是主屏居中偏右下(和以前一致);恢复位置按虚拟桌面钳位,
|
|
438
|
+
# 否则上次存在第二块屏上的坐标(负数)会被拉回主屏左上角。
|
|
439
|
+
x, y = max(40, sw - w - 80), max(40, (sh - h) // 2 - 40)
|
|
440
|
+
try:
|
|
441
|
+
size, rest = geo.split("+", 1)
|
|
442
|
+
pw, ph = (int(v) for v in size.split("x"))
|
|
443
|
+
px, py = (int(v) for v in rest.split("+")[:2])
|
|
444
|
+
w = max(MIN_W, min(sw - 40, pw))
|
|
445
|
+
h = max(MIN_H, min(sh - 40, ph))
|
|
446
|
+
x = max(self._vx, min(self._vx + self._vw - w, px))
|
|
447
|
+
y = max(self._vy, min(self._vy + self._vh - h, py))
|
|
448
|
+
except Exception:
|
|
449
|
+
pass
|
|
450
|
+
return w, h, x, y
|
|
451
|
+
|
|
452
|
+
def _sync_screens(self):
|
|
453
|
+
"""刷新虚拟桌面范围:接/拔显示器、改分辨率后不用重开窗口。
|
|
454
|
+
|
|
455
|
+
拖动/缩放开始前调一次就够(GetSystemMetrics 很便宜,但没必要每帧问)。
|
|
456
|
+
"""
|
|
457
|
+
vx, vy, vw, vh = virtual_bounds(self._sw, self._sh)
|
|
458
|
+
if vw > 0 and vh > 0:
|
|
459
|
+
self._vx, self._vy, self._vw, self._vh = vx, vy, vw, vh
|
|
460
|
+
if getattr(self, "_source", None) is not None:
|
|
461
|
+
self._source.bounds = (vx, vy, vw, vh)
|
|
462
|
+
return self._vx, self._vy, self._vw, self._vh
|
|
463
|
+
|
|
464
|
+
def _set_geometry(self, geo: Geometry) -> None:
|
|
465
|
+
"""换一套几何:渲染器跟着换,静态跳帧的签名作废。"""
|
|
466
|
+
self._geo = geo
|
|
467
|
+
self._renderer.set_geometry(geo)
|
|
468
|
+
self.canvas.config(width=geo.width, height=geo.height)
|
|
469
|
+
|
|
470
|
+
def _apply_shapes(self, shapes) -> None:
|
|
471
|
+
"""气泡宽度/高度变了:尺寸相关的遮罩/折射图直接复用,只重算框。"""
|
|
472
|
+
with self._geo_lock:
|
|
473
|
+
self._set_geometry(self._geo.with_shapes(shapes))
|
|
474
|
+
|
|
475
|
+
# ---- 窗口外观 ----
|
|
476
|
+
|
|
477
|
+
def _intro_step(self, n: int) -> None:
|
|
478
|
+
try:
|
|
479
|
+
if n >= 6:
|
|
480
|
+
self._y = self._intro_target_y
|
|
481
|
+
else:
|
|
482
|
+
self._y = self._intro_target_y + int(22 * (1 - (n + 1) / 6.0))
|
|
483
|
+
self.root.geometry(f"{self._geo.width}x{self._geo.height}+{self._x}+{self._y}")
|
|
484
|
+
except Exception:
|
|
485
|
+
pass
|
|
486
|
+
if n < 6:
|
|
487
|
+
self.root.after(26, lambda: self._intro_step(n + 1))
|
|
488
|
+
|
|
489
|
+
def _keep_on_top(self) -> None:
|
|
490
|
+
"""DSH 等置顶窗口被激活后会把悬浮窗压下去;定时重新抬到最上层。"""
|
|
491
|
+
try:
|
|
492
|
+
if not self._hidden:
|
|
493
|
+
self.root.attributes("-topmost", True)
|
|
494
|
+
self.root.lift()
|
|
495
|
+
except Exception:
|
|
496
|
+
pass
|
|
497
|
+
try:
|
|
498
|
+
self.root.after(1800, self._keep_on_top)
|
|
499
|
+
except Exception:
|
|
500
|
+
pass
|
|
501
|
+
|
|
502
|
+
# ---- 协议 ----
|
|
503
|
+
|
|
504
|
+
def _render_event(self, event) -> None:
|
|
505
|
+
if not isinstance(event, dict):
|
|
506
|
+
return
|
|
507
|
+
if event.get("type") in ("message", "meta"):
|
|
508
|
+
self.feed.add(ChatFeed.parse(json.dumps(event, ensure_ascii=False)))
|
|
509
|
+
self._refresh_feed()
|
|
510
|
+
elif event.get("type") == "control":
|
|
511
|
+
self._apply_control(event)
|
|
512
|
+
|
|
513
|
+
def _apply_control(self, event) -> None:
|
|
514
|
+
action = str(event.get("action") or "")
|
|
515
|
+
value = event.get("value")
|
|
516
|
+
try:
|
|
517
|
+
if action == "rename" and str(value or "").strip():
|
|
518
|
+
self.name = str(value).strip()[:24]
|
|
519
|
+
self.root.title(self.name)
|
|
520
|
+
write_config({"name": self.name})
|
|
521
|
+
self._refresh_feed()
|
|
522
|
+
elif action == "collapse" and not self._collapsed:
|
|
523
|
+
self.toggle_collapse()
|
|
524
|
+
elif action == "expand" and self._collapsed:
|
|
525
|
+
self.toggle_collapse()
|
|
526
|
+
elif action == "hide":
|
|
527
|
+
self._hidden = True
|
|
528
|
+
write_config({"hidden": True})
|
|
529
|
+
self.root.withdraw()
|
|
530
|
+
elif action == "show":
|
|
531
|
+
self._hidden = False
|
|
532
|
+
write_config({"hidden": False})
|
|
533
|
+
self.root.deiconify()
|
|
534
|
+
self.root.attributes("-topmost", True)
|
|
535
|
+
except Exception:
|
|
536
|
+
pass
|
|
537
|
+
|
|
538
|
+
def _drain_protocol(self) -> list:
|
|
539
|
+
if self._protocol is None:
|
|
540
|
+
return []
|
|
541
|
+
try:
|
|
542
|
+
return self._protocol.read_events()
|
|
543
|
+
except Exception:
|
|
544
|
+
return []
|
|
545
|
+
|
|
546
|
+
def _replay_protocol(self) -> None:
|
|
547
|
+
if self._protocol is None:
|
|
548
|
+
return
|
|
549
|
+
try:
|
|
550
|
+
for event in self._protocol.reset_events(60):
|
|
551
|
+
self._render_event(event)
|
|
552
|
+
except Exception:
|
|
553
|
+
pass
|
|
554
|
+
|
|
555
|
+
# ---- 聊天轮询 ----
|
|
556
|
+
|
|
557
|
+
def _poll_chat(self) -> None:
|
|
558
|
+
try:
|
|
559
|
+
self._sync_visible()
|
|
560
|
+
for event in self._drain_protocol():
|
|
561
|
+
self._render_event(event)
|
|
562
|
+
if self._mic_proc is not None and self._mic_proc.poll() is not None:
|
|
563
|
+
self._mic_proc = None
|
|
564
|
+
self.feed.status = ""
|
|
565
|
+
self._refresh_feed()
|
|
566
|
+
if self.feed.poll():
|
|
567
|
+
self._refresh_feed()
|
|
568
|
+
except Exception as exc:
|
|
569
|
+
print("chat poll error", exc, flush=True)
|
|
570
|
+
if not self._stop.is_set():
|
|
571
|
+
self.root.after(250, self._poll_chat)
|
|
572
|
+
|
|
573
|
+
def _sync_visible(self) -> None:
|
|
574
|
+
try:
|
|
575
|
+
st = CONFIG.stat()
|
|
576
|
+
key = (st.st_mtime_ns, st.st_size)
|
|
577
|
+
except OSError:
|
|
578
|
+
key = None
|
|
579
|
+
if key == self._cfg_key:
|
|
580
|
+
return
|
|
581
|
+
self._cfg_key = key
|
|
582
|
+
want = bool(read_config().get("hidden"))
|
|
583
|
+
if want == self._hidden:
|
|
584
|
+
return
|
|
585
|
+
self._hidden = want
|
|
586
|
+
try:
|
|
587
|
+
if want:
|
|
588
|
+
self.root.withdraw()
|
|
589
|
+
else:
|
|
590
|
+
self.root.deiconify()
|
|
591
|
+
self.root.attributes("-topmost", True)
|
|
592
|
+
self.root.lift()
|
|
593
|
+
except Exception:
|
|
594
|
+
self._hidden = not want
|
|
595
|
+
|
|
596
|
+
# ---- 输入 ----
|
|
597
|
+
|
|
598
|
+
def _send(self, _event=None):
|
|
599
|
+
if self._entry is None:
|
|
600
|
+
return "break"
|
|
601
|
+
text = self._entry.get().strip()
|
|
602
|
+
if not text:
|
|
603
|
+
return "break"
|
|
604
|
+
try:
|
|
605
|
+
append_user(text)
|
|
606
|
+
except Exception as exc:
|
|
607
|
+
self.feed.status = f"写不进去:{exc}"
|
|
608
|
+
self._refresh_feed()
|
|
609
|
+
return "break"
|
|
610
|
+
try:
|
|
611
|
+
self._entry.delete(0, "end")
|
|
612
|
+
except Exception:
|
|
613
|
+
pass
|
|
614
|
+
try:
|
|
615
|
+
if self._protocol is not None:
|
|
616
|
+
self._protocol.action("say", text=text)
|
|
617
|
+
except Exception:
|
|
618
|
+
pass
|
|
619
|
+
self._render_input()
|
|
620
|
+
self.root.after(40, self._poll_chat)
|
|
621
|
+
return "break"
|
|
622
|
+
|
|
623
|
+
def _wheel(self, e):
|
|
624
|
+
if self.feed.scroll(1 if e.delta > 0 else -1):
|
|
625
|
+
self._refresh_feed()
|
|
626
|
+
|
|
627
|
+
def _toggle_mic(self):
|
|
628
|
+
if self._mic_proc is not None and self._mic_proc.poll() is None:
|
|
629
|
+
self._stop_mic()
|
|
630
|
+
return
|
|
631
|
+
if not MIC_SCRIPT.exists():
|
|
632
|
+
self.feed.status = "找不到 mic.py"
|
|
633
|
+
self._refresh_feed()
|
|
634
|
+
return
|
|
635
|
+
try:
|
|
636
|
+
self._mic_proc = subprocess.Popen(
|
|
637
|
+
[asr_python(), str(MIC_SCRIPT), "--engine", "local"],
|
|
638
|
+
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
|
|
639
|
+
creationflags=0x08000000 if os.name == "nt" else 0,
|
|
640
|
+
)
|
|
641
|
+
self.feed.status = "录音中"
|
|
642
|
+
except Exception as exc:
|
|
643
|
+
self.feed.status = f"麦起不来:{exc}"
|
|
644
|
+
self._refresh_feed()
|
|
645
|
+
|
|
646
|
+
def _stop_mic(self):
|
|
647
|
+
p = self._mic_proc
|
|
648
|
+
self._mic_proc = None
|
|
649
|
+
self.feed.status = ""
|
|
650
|
+
if p is not None and p.poll() is None:
|
|
651
|
+
try:
|
|
652
|
+
p.terminate()
|
|
653
|
+
except Exception:
|
|
654
|
+
pass
|
|
655
|
+
self._refresh_feed()
|
|
656
|
+
|
|
657
|
+
def _render_input(self) -> None:
|
|
658
|
+
"""输入文字画在 Canvas 上;Entry 只负责 IME/键盘,永远不可见。"""
|
|
659
|
+
if self._entry is None:
|
|
660
|
+
return
|
|
661
|
+
c = self.canvas
|
|
662
|
+
c.delete("inputtext", "inputcursor")
|
|
663
|
+
try:
|
|
664
|
+
text = self._entry.get()
|
|
665
|
+
focused = self.root.focus_get() is self._entry
|
|
666
|
+
except Exception:
|
|
667
|
+
text, focused = "", False
|
|
668
|
+
x0, y0, x1, y1, _r = self._geo.input_shape
|
|
669
|
+
x, y = x0 + 84, y0 + 27
|
|
670
|
+
maxw = max(60, x1 - x0 - 100)
|
|
671
|
+
if not text and not focused:
|
|
672
|
+
self.text.draw(x, y, "在这里跟它说话…", anchor="w", fill="#8e9db2",
|
|
673
|
+
size=11, tags=("inputtext", "inputzone"))
|
|
674
|
+
elif text:
|
|
675
|
+
font = self.fonts.get(11)
|
|
676
|
+
while text and font.measure(text) > maxw:
|
|
677
|
+
text = text[1:]
|
|
678
|
+
self.text.draw(x, y, text, anchor="w", fill="#eef6ff",
|
|
679
|
+
size=11, tags=("inputtext", "inputzone"))
|
|
680
|
+
if focused:
|
|
681
|
+
cx = x + font.measure(text)
|
|
682
|
+
c.create_line(cx + 1, y - 9, cx + 1, y + 9,
|
|
683
|
+
fill="#b9d7ff", width=1, tags=("inputcursor",))
|
|
684
|
+
elif focused:
|
|
685
|
+
c.create_line(x + 1, y - 9, x + 1, y + 9,
|
|
686
|
+
fill="#b9d7ff", width=1, tags=("inputcursor",))
|
|
687
|
+
self.text.refresh()
|
|
688
|
+
|
|
689
|
+
def _on_input_key(self, _e=None):
|
|
690
|
+
self._render_input()
|
|
691
|
+
return None
|
|
692
|
+
|
|
693
|
+
def _focus_input(self, _e=None):
|
|
694
|
+
try:
|
|
695
|
+
self._entry.focus_force()
|
|
696
|
+
self._entry.icursor("end")
|
|
697
|
+
except Exception:
|
|
698
|
+
pass
|
|
699
|
+
self._render_input()
|
|
700
|
+
|
|
701
|
+
# ---- 改名 ----
|
|
702
|
+
|
|
703
|
+
def _edit_title(self, _event=None):
|
|
704
|
+
if self._editing:
|
|
705
|
+
return "break"
|
|
706
|
+
self._editing = True
|
|
707
|
+
if self._title_entry is None:
|
|
708
|
+
self._title_entry = tk.Entry(self.root, font=self.fonts.spec(15), relief="flat",
|
|
709
|
+
bd=0, bg="#1b2330", fg="#f7fbff",
|
|
710
|
+
insertbackground="#f7fbff")
|
|
711
|
+
self._title_entry.bind("<Return>", self._commit_title)
|
|
712
|
+
self._title_entry.bind("<Escape>", self._cancel_title)
|
|
713
|
+
self._title_entry.bind("<FocusOut>", self._commit_title)
|
|
714
|
+
self._title_entry.delete(0, "end")
|
|
715
|
+
self._title_entry.insert(0, self.name)
|
|
716
|
+
if self._title_item is not None:
|
|
717
|
+
self.canvas.delete(self._title_item)
|
|
718
|
+
self._title_item = self.canvas.create_window(68, 38, anchor="w",
|
|
719
|
+
window=self._title_entry, width=180)
|
|
720
|
+
self._title_entry.focus_set()
|
|
721
|
+
self._title_entry.select_range(0, "end")
|
|
722
|
+
return "break"
|
|
723
|
+
|
|
724
|
+
def _commit_title(self, _event=None):
|
|
725
|
+
if not self._editing:
|
|
726
|
+
return
|
|
727
|
+
self._editing = False
|
|
728
|
+
text = self._title_entry.get().strip()
|
|
729
|
+
if self._title_item is not None:
|
|
730
|
+
self.canvas.delete(self._title_item)
|
|
731
|
+
self._title_item = None
|
|
732
|
+
if text and text != self.name:
|
|
733
|
+
self.name = text[:24]
|
|
734
|
+
self.root.title(self.name)
|
|
735
|
+
write_config({"name": self.name})
|
|
736
|
+
try:
|
|
737
|
+
if self._protocol is not None:
|
|
738
|
+
self._protocol.action("rename", value=self.name)
|
|
739
|
+
except Exception:
|
|
740
|
+
pass
|
|
741
|
+
self._refresh_feed()
|
|
742
|
+
return "break"
|
|
743
|
+
|
|
744
|
+
def _cancel_title(self):
|
|
745
|
+
self._editing = False
|
|
746
|
+
if self._title_item is not None:
|
|
747
|
+
self.canvas.delete(self._title_item)
|
|
748
|
+
self._title_item = None
|
|
749
|
+
self._refresh_feed()
|
|
750
|
+
return "break"
|
|
751
|
+
|
|
752
|
+
# ---- 折叠 ----
|
|
753
|
+
|
|
754
|
+
def toggle_collapse(self, _event=None):
|
|
755
|
+
# 先读真实坐标再切高度:_x/_y 只是启动时的缓存,拖动/缩放后已过期。
|
|
756
|
+
self._sync_pos()
|
|
757
|
+
self._collapsed = not self._collapsed
|
|
758
|
+
if self._collapsed:
|
|
759
|
+
self._saved_h = self._geo.height
|
|
760
|
+
# 先换成折叠尺寸的几何,再按它裁上一帧,否则圆角遮罩还是整窗高度
|
|
761
|
+
self._set_geometry(Geometry.for_size(self._geo.width, collapsed_height()))
|
|
762
|
+
self._collapsed_frame = self._geo.collapsed_frame(self._last_frame)
|
|
763
|
+
self.root.geometry(f"{self._geo.width}x{self._geo.height}+{self._x}+{self._y}")
|
|
764
|
+
self._layout()
|
|
765
|
+
else:
|
|
766
|
+
self._collapsed_frame = None
|
|
767
|
+
self._set_geometry(Geometry.for_size(self._geo.width, self._saved_h))
|
|
768
|
+
self.root.geometry(f"{self._geo.width}x{self._geo.height}+{self._x}+{self._y}")
|
|
769
|
+
self._layout(seed=True)
|
|
770
|
+
try:
|
|
771
|
+
if self._protocol is not None:
|
|
772
|
+
self._protocol.action("collapse" if self._collapsed else "expand")
|
|
773
|
+
except Exception:
|
|
774
|
+
pass
|
|
775
|
+
|
|
776
|
+
# ---- 气泡布局 ----
|
|
777
|
+
|
|
778
|
+
def _fit_lines(self, text, size, max_width, max_height) -> list[str]:
|
|
779
|
+
"""按框宽/框高折行并截断,避免文字溢出气泡。"""
|
|
780
|
+
font = self.fonts.get(size)
|
|
781
|
+
measure = font.measure
|
|
782
|
+
ls = max(1, font.metrics("linespace"))
|
|
783
|
+
max_lines = max(1, int(max_height // ls))
|
|
784
|
+
no_start = ",。、;:!?)》」』…,.;:!?)]}"
|
|
785
|
+
lines = []
|
|
786
|
+
for para in str(text).splitlines() or [""]:
|
|
787
|
+
cur = ""
|
|
788
|
+
for ch in para:
|
|
789
|
+
if not cur or measure(cur + ch) <= max_width:
|
|
790
|
+
cur += ch
|
|
791
|
+
elif ch in no_start:
|
|
792
|
+
cur += ch # 标点不能单独掉到下一行
|
|
793
|
+
else:
|
|
794
|
+
lines.append(cur)
|
|
795
|
+
cur = ch
|
|
796
|
+
lines.append(cur)
|
|
797
|
+
if len(lines) > max_lines:
|
|
798
|
+
lines = lines[:max_lines]
|
|
799
|
+
last = lines[-1]
|
|
800
|
+
while last and measure(last + "…") > max_width:
|
|
801
|
+
last = last[:-1]
|
|
802
|
+
lines[-1] = (last + "…") if last else "…"
|
|
803
|
+
return lines or [""]
|
|
804
|
+
|
|
805
|
+
def _message_shapes(self, shown):
|
|
806
|
+
"""四个固定大框换成按正文文字测量的聊天气泡。
|
|
807
|
+
|
|
808
|
+
时间戳不算进气泡:它贴在气泡上方,气泡只包正文。先按最大可用宽度折行,
|
|
809
|
+
再取实际最长行的宽度;短消息不会撑满整行。
|
|
810
|
+
"""
|
|
811
|
+
w, h = self._geo.width, self._geo.height
|
|
812
|
+
base = shapes_for(w, h)
|
|
813
|
+
left, right = base[0][0], base[0][2]
|
|
814
|
+
max_bubble = min(right - left, max(240, int((right - left) * 0.72)))
|
|
815
|
+
min_bubble = min(110, max_bubble)
|
|
816
|
+
pad = 18
|
|
817
|
+
fbody = self.fonts.get(11)
|
|
818
|
+
fstamp = self.fonts.get(9)
|
|
819
|
+
ls = max(1, fbody.metrics("linespace"))
|
|
820
|
+
shapes, infos = [], []
|
|
821
|
+
for i in range(MESSAGE_SLOTS):
|
|
822
|
+
if i >= len(shown):
|
|
823
|
+
shapes.append((0, 0, 0, 0, 0)) # 没有这条消息:不画框
|
|
824
|
+
continue
|
|
825
|
+
msg = shown[i]
|
|
826
|
+
x0, y0, x1, y1, _r = base[i]
|
|
827
|
+
slot_h = y1 - y0
|
|
828
|
+
is_user = str(msg.get("role") or "").lower() in ("user", "me", "human")
|
|
829
|
+
stamp = str(msg.get("stamp") or "")
|
|
830
|
+
stamp_h = fstamp.metrics("linespace") if stamp else 0
|
|
831
|
+
body_avail = max(12, slot_h - stamp_h)
|
|
832
|
+
content_max = max(80, max_bubble - 2 * pad)
|
|
833
|
+
lines = self._fit_lines(msg.get("text") or "", 11, content_max, body_avail)
|
|
834
|
+
inner = max([0] + [fbody.measure(line) for line in lines])
|
|
835
|
+
bw = min(max_bubble, max(min_bubble, inner + 2 * pad))
|
|
836
|
+
bh = min(body_avail, max(ls + 16, len(lines) * ls + 16))
|
|
837
|
+
by0 = y0 + stamp_h
|
|
838
|
+
by1 = min(y1, by0 + bh)
|
|
839
|
+
if is_user:
|
|
840
|
+
bx1 = right
|
|
841
|
+
bx0 = bx1 - bw
|
|
842
|
+
tx, ta = bx1 - pad, "ne"
|
|
843
|
+
else:
|
|
844
|
+
bx0 = left
|
|
845
|
+
bx1 = bx0 + bw
|
|
846
|
+
tx, ta = bx0 + pad, "nw"
|
|
847
|
+
shape = (bx0, by0, bx1, by1, min(26, max(16, bh // 3)))
|
|
848
|
+
shapes.append(shape)
|
|
849
|
+
infos.append({
|
|
850
|
+
"shape": shape, "is_user": is_user, "stamp": stamp,
|
|
851
|
+
"tx": tx, "ta": ta, "lines": lines,
|
|
852
|
+
"ty": by0 + max(4, (bh - len(lines) * ls) // 2),
|
|
853
|
+
"stamp_y": y0, "ls": ls,
|
|
854
|
+
})
|
|
855
|
+
shapes.extend(base[MESSAGE_SLOTS:])
|
|
856
|
+
return shapes, infos
|
|
857
|
+
|
|
858
|
+
# ---- 布局 ----
|
|
859
|
+
|
|
860
|
+
def _ensure_entry(self) -> None:
|
|
861
|
+
"""Entry 只建一次:每次消息都销毁重建会丢输入法状态和草稿光标。"""
|
|
862
|
+
if self._entry is not None:
|
|
863
|
+
return
|
|
864
|
+
self._entry = tk.Entry(self.root, bg=KEY, fg=KEY, insertbackground=KEY,
|
|
865
|
+
relief="flat", bd=0, font=self.fonts.spec(11),
|
|
866
|
+
insertwidth=0, highlightthickness=0)
|
|
867
|
+
self._entry.bind("<KeyRelease>", self._on_input_key)
|
|
868
|
+
self._entry.bind("<Return>", self._send)
|
|
869
|
+
self._entry.bind("<FocusIn>", lambda _e: self._render_input())
|
|
870
|
+
self._entry.bind("<FocusOut>", lambda _e: self._render_input())
|
|
871
|
+
|
|
872
|
+
def _layout(self, seed: bool = False) -> None:
|
|
873
|
+
"""整窗重排:标题栏 + 气泡 + 输入区 + 滚动条,然后把上一帧贴回去。
|
|
874
|
+
|
|
875
|
+
`seed=True` 时按新尺寸缩放上一帧先顶着(缩放/展开的过渡帧)。
|
|
876
|
+
"""
|
|
877
|
+
c = self.canvas
|
|
878
|
+
geo = self._geo
|
|
879
|
+
w, h = geo.width, geo.height
|
|
880
|
+
c.delete("all")
|
|
881
|
+
self.text.reset()
|
|
882
|
+
self._bg = c.create_image(0, 0, anchor="nw")
|
|
883
|
+
|
|
884
|
+
frame = self._collapsed_frame if self._collapsed else self._last_frame
|
|
885
|
+
if frame is not None and frame.size == (w, h):
|
|
886
|
+
try:
|
|
887
|
+
self._img = ImageTk.PhotoImage(frame, master=c)
|
|
888
|
+
c.itemconfigure(self._bg, image=self._img)
|
|
889
|
+
except Exception:
|
|
890
|
+
pass
|
|
891
|
+
elif seed and self._last_frame is not None:
|
|
892
|
+
preview = self._last_frame.resize((w, h), Image.Resampling.BILINEAR)
|
|
893
|
+
try:
|
|
894
|
+
self._img = ImageTk.PhotoImage(preview, master=c)
|
|
895
|
+
c.itemconfigure(self._bg, image=self._img)
|
|
896
|
+
except Exception:
|
|
897
|
+
pass
|
|
898
|
+
if PREVIEW:
|
|
899
|
+
c.create_rectangle(0, 0, w, h, fill="#0d131d", outline="", tags=("preview-bg",))
|
|
900
|
+
c.tag_lower("preview-bg")
|
|
901
|
+
|
|
902
|
+
self._draw_header()
|
|
903
|
+
if self._collapsed:
|
|
904
|
+
self._bind_header()
|
|
905
|
+
c.itemconfigure(self._bg, state="normal")
|
|
906
|
+
self._bar_len = max(72, int(w * BAR_FRAC))
|
|
907
|
+
self._bar = c.create_line(w // 2 - self._bar_len // 2, BAR_Y,
|
|
908
|
+
w // 2 + self._bar_len // 2, BAR_Y,
|
|
909
|
+
width=BAR_THICK, fill=self._bar_color,
|
|
910
|
+
capstyle="round")
|
|
911
|
+
self.text.refresh()
|
|
912
|
+
return
|
|
913
|
+
|
|
914
|
+
self._draw_feed()
|
|
915
|
+
self._draw_input()
|
|
916
|
+
self._draw_scrollbar()
|
|
917
|
+
self._bind_header()
|
|
918
|
+
c.tag_bind("send", "<Button-1>", self._send)
|
|
919
|
+
c.tag_bind("inputzone", "<Button-1>", self._focus_input)
|
|
920
|
+
c.tag_raise("close")
|
|
921
|
+
c.tag_raise("send")
|
|
922
|
+
|
|
923
|
+
self._bar_len = max(72, int(w * BAR_FRAC))
|
|
924
|
+
self._bar = c.create_line(w // 2 - self._bar_len // 2, BAR_Y,
|
|
925
|
+
w // 2 + self._bar_len // 2, BAR_Y,
|
|
926
|
+
width=BAR_THICK, fill="#b8cbe0", capstyle="round")
|
|
927
|
+
self._bar_color = "#b8cbe0"
|
|
928
|
+
self.text.refresh()
|
|
929
|
+
|
|
930
|
+
def _draw_header(self) -> None:
|
|
931
|
+
c = self.canvas
|
|
932
|
+
w = self._geo.width
|
|
933
|
+
mic_on = self._mic_proc is not None and self._mic_proc.poll() is None
|
|
934
|
+
dot = "#e05a5a" if mic_on else "#0a84ff"
|
|
935
|
+
# 标题栏玻璃是 y=12..66,中心 39;右边 ▾/✕ 也在 38。点、名字、状态必须
|
|
936
|
+
# 同一条水平中线,且字号要能完整落在条内,不能压到气泡区。
|
|
937
|
+
cy, dot_x, title_x = 38, 54, 68
|
|
938
|
+
c.create_oval(dot_x - 9, cy - 9, dot_x + 9, cy + 9,
|
|
939
|
+
outline="#2b4d73" if dot == "#0a84ff" else "#6b3030", width=1)
|
|
940
|
+
c.create_oval(dot_x - 4, cy - 4, dot_x + 4, cy + 4, fill=dot, outline="")
|
|
941
|
+
name = self.name[:10]
|
|
942
|
+
self.text.draw(title_x, cy, name, anchor="w", fill="#f7fbff", size=16,
|
|
943
|
+
bold=True, tags=("title",))
|
|
944
|
+
count = self.feed.status or f"{len(self.feed.messages)} 条"
|
|
945
|
+
nx = title_x + self.fonts.get(16, True).measure(name) + 10
|
|
946
|
+
self.text.draw(nx, cy, count, anchor="w",
|
|
947
|
+
fill="#ffb4b4" if self.feed.status else "#dbe6f5", size=11)
|
|
948
|
+
self.text.draw(w - 94, 38, "▾", anchor="w", fill="#7f8da3", size=12, tags=("collapse",))
|
|
949
|
+
self.text.draw(w - 58, 38, "✕", anchor="w", fill="#9fb0c8", size=13, tags=("close",))
|
|
950
|
+
|
|
951
|
+
def _bind_header(self) -> None:
|
|
952
|
+
c = self.canvas
|
|
953
|
+
c.tag_bind("close", "<Button-1>", self._close)
|
|
954
|
+
c.tag_bind("collapse", "<Button-1>", self.toggle_collapse)
|
|
955
|
+
# 双击改名:单击要留给「按住标题拖动窗口」,两者不能抢同一个事件
|
|
956
|
+
c.tag_bind("title", "<Double-Button-1>", self._edit_title)
|
|
957
|
+
|
|
958
|
+
def _draw_feed(self) -> None:
|
|
959
|
+
w = self._geo.width
|
|
960
|
+
shown = self.feed.visible()
|
|
961
|
+
if shown:
|
|
962
|
+
shapes, infos = self._message_shapes(shown)
|
|
963
|
+
else:
|
|
964
|
+
shapes, infos = shapes_for(w, self._geo.height), []
|
|
965
|
+
if shapes != self._geo.shapes:
|
|
966
|
+
with self._geo_lock:
|
|
967
|
+
self._set_geometry(self._geo.with_shapes(shapes, plate=bool(shown)))
|
|
968
|
+
# 每条消息只按正文占一块;时间戳贴在气泡上方,不进框
|
|
969
|
+
for info in infos:
|
|
970
|
+
if info["stamp"]:
|
|
971
|
+
self.text.draw(info["tx"], info["stamp_y"], info["stamp"],
|
|
972
|
+
anchor=info["ta"], fill="#a8b7c9", size=9, tags=("feed",))
|
|
973
|
+
for li, line in enumerate(info["lines"]):
|
|
974
|
+
# 气泡里有底板,文字颜色固定、再补一层 1px 深色偏移当描边
|
|
975
|
+
self.text.draw(info["tx"], info["ty"] + li * info["ls"], line,
|
|
976
|
+
anchor=info["ta"], fill="#edf6ff", size=11, tags=("feed",),
|
|
977
|
+
shadow=PLATE, adapt=not PLATE)
|
|
978
|
+
if not shown:
|
|
979
|
+
slot = self._geo.shapes[0]
|
|
980
|
+
self.text.draw(w // 2, slot[1] + (slot[3] - slot[1]) // 2,
|
|
981
|
+
"在这里跟它说话 · 滚轮翻历史", anchor="center",
|
|
982
|
+
fill="#8e9db2", size=12, tags=("feed",))
|
|
983
|
+
|
|
984
|
+
def _draw_input(self) -> None:
|
|
985
|
+
geo = self._geo
|
|
986
|
+
x0, y0, x1, y1, _r = geo.input_shape
|
|
987
|
+
self._ensure_entry()
|
|
988
|
+
# Entry 放到 2x2 且底色是键色:真正显示的文字画在 Canvas 上,
|
|
989
|
+
# 否则玻璃里会出现一个突兀的黑条(IME 候选框仍然正常)。
|
|
990
|
+
self._entry_item = self.canvas.create_window(
|
|
991
|
+
x0 + 84, y0 + 27, window=self._entry, anchor="w",
|
|
992
|
+
width=2, height=2, tags=("feed",))
|
|
993
|
+
self._render_input()
|
|
994
|
+
sx0, sy0, sx1, sy1, _r = geo.send_shape
|
|
995
|
+
self.text.draw((sx0 + sx1) // 2, sy0 + 27, "发送", tags=("send", "feed"),
|
|
996
|
+
fill="#ffffff", size=12, bold=True)
|
|
997
|
+
|
|
998
|
+
def _draw_scrollbar(self) -> None:
|
|
999
|
+
"""滚动条放在主体玻璃右侧外面,不占消息区。"""
|
|
1000
|
+
c = self.canvas
|
|
1001
|
+
c.delete("sbar")
|
|
1002
|
+
total = len(self.feed.messages)
|
|
1003
|
+
visible = MESSAGE_SLOTS
|
|
1004
|
+
if total <= visible:
|
|
1005
|
+
return
|
|
1006
|
+
body = self._geo.slabs[1]
|
|
1007
|
+
x = self._geo.width - 6
|
|
1008
|
+
top = body[1] + 8
|
|
1009
|
+
view_h = max(1, body[3] - body[1] - 16)
|
|
1010
|
+
thumb = max(24, int(view_h * (visible / float(total))))
|
|
1011
|
+
max_off = max(1, total - visible)
|
|
1012
|
+
t = 1.0 - max(0, min(max_off, self.feed.offset)) / float(max_off)
|
|
1013
|
+
y0 = top + t * max(0, view_h - thumb)
|
|
1014
|
+
c.create_line(x, y0, x, y0 + thumb, width=3, fill="#8d95a4",
|
|
1015
|
+
capstyle="round", tags=("sbar",))
|
|
1016
|
+
c.tag_raise("sbar")
|
|
1017
|
+
|
|
1018
|
+
def _refresh_feed(self) -> None:
|
|
1019
|
+
"""聊天流变了就重排;Entry 的草稿和焦点要保住(它在 _layout 里复用)。"""
|
|
1020
|
+
if not hasattr(self, "canvas"):
|
|
1021
|
+
return
|
|
1022
|
+
self._layout()
|
|
1023
|
+
if self._last_frame is not None:
|
|
1024
|
+
self._adapt_now(self._last_frame)
|
|
1025
|
+
|
|
1026
|
+
def _adapt_now(self, frame) -> None:
|
|
1027
|
+
self._aux_at = 0.0
|
|
1028
|
+
self.text.adapt(frame)
|
|
1029
|
+
self._adapt_bar(frame)
|
|
1030
|
+
|
|
1031
|
+
def _adapt_bar(self, frame) -> None:
|
|
1032
|
+
"""最上面细条随背景切换深浅(同样要滞回,理由见 TextLayer.adapt)。"""
|
|
1033
|
+
if frame is None or self._bar is None:
|
|
1034
|
+
return
|
|
1035
|
+
now = time.time()
|
|
1036
|
+
if now - self._aux_at < 0.25:
|
|
1037
|
+
return
|
|
1038
|
+
self._aux_at = now
|
|
1039
|
+
mid = self._geo.width // 2
|
|
1040
|
+
half = max(50, self._bar_len // 2)
|
|
1041
|
+
try:
|
|
1042
|
+
arr = np.asarray(frame.convert("L"))
|
|
1043
|
+
except Exception:
|
|
1044
|
+
return
|
|
1045
|
+
patch = arr[max(0, BAR_Y - 6):BAR_Y + 12, max(0, mid - half):mid + half]
|
|
1046
|
+
if patch.size == 0:
|
|
1047
|
+
return
|
|
1048
|
+
p50 = float(np.median(patch)) / 255.0
|
|
1049
|
+
cur = self._bar_color
|
|
1050
|
+
if cur == "#4e6278":
|
|
1051
|
+
fill = "#b8cbe0" if p50 < 0.36 else cur # 已很深:要够暗才翻浅
|
|
1052
|
+
else:
|
|
1053
|
+
fill = "#4e6278" if p50 > 0.60 else cur
|
|
1054
|
+
if fill != cur:
|
|
1055
|
+
self._bar_color = fill
|
|
1056
|
+
try:
|
|
1057
|
+
self.canvas.itemconfigure(self._bar, fill=fill)
|
|
1058
|
+
except Exception:
|
|
1059
|
+
pass
|
|
1060
|
+
|
|
1061
|
+
# ---- 预览 / 重建 ----
|
|
1062
|
+
|
|
1063
|
+
def _preview_resize(self, w: int, h: int) -> None:
|
|
1064
|
+
"""拖拽时先缩放上一帧并重排文字,松手后再重建玻璃贴图。
|
|
1065
|
+
|
|
1066
|
+
这一路故意**不**重建遮罩/折射图:拖拽期间 worker 是停的,只要文字和
|
|
1067
|
+
上一帧跟得上手就行;松手的 `_rebuild()` 才做完整重建。
|
|
1068
|
+
"""
|
|
1069
|
+
if w < MIN_W or h < MIN_H:
|
|
1070
|
+
return
|
|
1071
|
+
with self._geo_lock:
|
|
1072
|
+
self._geo = Geometry.for_size(w, h, version=self._geo.version)
|
|
1073
|
+
self._renderer.geo = self._geo
|
|
1074
|
+
self.canvas.config(width=w, height=h)
|
|
1075
|
+
if self._last_frame is not None and self._last_frame.size != (w, h):
|
|
1076
|
+
self._last_frame = self._last_frame.resize((w, h), Image.Resampling.BILINEAR)
|
|
1077
|
+
self._layout()
|
|
1078
|
+
|
|
1079
|
+
def _rebuild(self, w: int, h: int) -> None:
|
|
1080
|
+
"""松手后按最终尺寸重建(含遮罩/折射图)。"""
|
|
1081
|
+
self._pause.set()
|
|
1082
|
+
with self._geo_lock:
|
|
1083
|
+
self._set_geometry(Geometry.for_size(w, h))
|
|
1084
|
+
while True:
|
|
1085
|
+
try:
|
|
1086
|
+
self._q.get_nowait()
|
|
1087
|
+
except queue.Empty:
|
|
1088
|
+
break
|
|
1089
|
+
self._layout(seed=True)
|
|
1090
|
+
self._pause.clear()
|
|
1091
|
+
|
|
1092
|
+
# ---- 截屏 / 心跳 ----
|
|
1093
|
+
|
|
1094
|
+
def _snapshot_mode(self) -> None:
|
|
1095
|
+
"""只给文档截图:停抓取、关 WDA,保留最后一帧(不改变正常路径)。"""
|
|
1096
|
+
self._snapshot_done = True
|
|
1097
|
+
self._pause.set()
|
|
1098
|
+
if self._excluded:
|
|
1099
|
+
self._set_exclude(False)
|
|
1100
|
+
|
|
1101
|
+
def _beat(self) -> None:
|
|
1102
|
+
"""写心跳(每秒最多一次)。插件侧 `windowAlive()` 读它的 mtime。"""
|
|
1103
|
+
now = time.time()
|
|
1104
|
+
if now - getattr(self, "_beat_at", 0.0) < HEARTBEAT_EVERY:
|
|
1105
|
+
return
|
|
1106
|
+
self._beat_at = now
|
|
1107
|
+
try:
|
|
1108
|
+
HEARTBEAT.parent.mkdir(parents=True, exist_ok=True)
|
|
1109
|
+
HEARTBEAT.write_bytes(b"")
|
|
1110
|
+
except Exception:
|
|
1111
|
+
pass
|
|
1112
|
+
|
|
1113
|
+
# ---- 主循环 ----
|
|
1114
|
+
|
|
1115
|
+
def _tick(self) -> None:
|
|
1116
|
+
self._beat()
|
|
1117
|
+
if SNAPSHOT and self._snapshot_done:
|
|
1118
|
+
self._pause.set()
|
|
1119
|
+
if self._excluded:
|
|
1120
|
+
self._set_exclude(False)
|
|
1121
|
+
self.root.after(100, self._tick)
|
|
1122
|
+
return
|
|
1123
|
+
if PREVIEW:
|
|
1124
|
+
self._pause.set()
|
|
1125
|
+
if self._excluded:
|
|
1126
|
+
self._set_exclude(False)
|
|
1127
|
+
self.root.after(100, self._tick)
|
|
1128
|
+
return
|
|
1129
|
+
# 缩放拖拽期间由 _preview_resize 负责画面,避免旧帧覆盖预览
|
|
1130
|
+
if self._resize or self._collapsed:
|
|
1131
|
+
self.root.after(INTERVAL_MS, self._tick)
|
|
1132
|
+
return
|
|
1133
|
+
if not self._excluded and not self._cappable and not self._solid:
|
|
1134
|
+
self._set_exclude(True)
|
|
1135
|
+
self._pause.clear()
|
|
1136
|
+
|
|
1137
|
+
try:
|
|
1138
|
+
out = self._q.get_nowait()
|
|
1139
|
+
except queue.Empty:
|
|
1140
|
+
out = None
|
|
1141
|
+
if out is not None:
|
|
1142
|
+
try:
|
|
1143
|
+
self._last_frame = out
|
|
1144
|
+
self._img = ImageTk.PhotoImage(out, master=self.canvas)
|
|
1145
|
+
self.canvas.itemconfigure(self._bg, image=self._img)
|
|
1146
|
+
self.canvas.tag_lower(self._bg)
|
|
1147
|
+
self.text.adapt(out)
|
|
1148
|
+
self._adapt_bar(out)
|
|
1149
|
+
except Exception as exc:
|
|
1150
|
+
print("draw error", exc, flush=True)
|
|
1151
|
+
|
|
1152
|
+
self.root.after(INTERVAL_MS, self._tick)
|
|
1153
|
+
|
|
1154
|
+
def _work(self) -> None:
|
|
1155
|
+
"""抓取 + 合成全在后台,主线程只负责显示最新一帧。"""
|
|
1156
|
+
import mss
|
|
1157
|
+
|
|
1158
|
+
idle = 0
|
|
1159
|
+
with mss.mss() as sct:
|
|
1160
|
+
while not self._stop.is_set():
|
|
1161
|
+
if self._pause.is_set() or self._collapsed:
|
|
1162
|
+
time.sleep(0.03)
|
|
1163
|
+
continue
|
|
1164
|
+
t0 = time.perf_counter()
|
|
1165
|
+
try:
|
|
1166
|
+
out = self._renderer.render(self._source, sct, self._bbox())
|
|
1167
|
+
if out is not None:
|
|
1168
|
+
idle = 0
|
|
1169
|
+
try:
|
|
1170
|
+
self._q.get_nowait()
|
|
1171
|
+
except queue.Empty:
|
|
1172
|
+
pass
|
|
1173
|
+
self._q.put(out)
|
|
1174
|
+
else:
|
|
1175
|
+
idle += 1
|
|
1176
|
+
except Exception as exc:
|
|
1177
|
+
print("capture error", exc, flush=True)
|
|
1178
|
+
# GPU 起不来(没有 moderngl / 显卡不支持 GL3.3)就换回 CPU,
|
|
1179
|
+
# 不然后台线程每帧刷一条,用户只看到一块永远不刷新的玻璃。
|
|
1180
|
+
if getattr(self._renderer, "broken", False):
|
|
1181
|
+
print("GPU 不可用,切换到 CPU 渲染", flush=True)
|
|
1182
|
+
self._renderer = CpuRenderer(self._geo)
|
|
1183
|
+
dt = time.perf_counter() - t0
|
|
1184
|
+
self._ema = self._ema * 0.9 + dt * 0.1
|
|
1185
|
+
if self._ema > 0.026:
|
|
1186
|
+
self._target_fps = 45
|
|
1187
|
+
elif self._ema > 0.016:
|
|
1188
|
+
self._target_fps = 60
|
|
1189
|
+
elif self._ema > 0.011:
|
|
1190
|
+
self._target_fps = 90
|
|
1191
|
+
else:
|
|
1192
|
+
self._target_fps = min(MAX_FPS, self._hz)
|
|
1193
|
+
# 静态画面:合成已经跳过,但「有没有变」只能靠抓屏看,单次 grab
|
|
1194
|
+
# 就要 ~7ms —— 按 60fps 空转等于白烧 40% 单核。探针降到 30fps,
|
|
1195
|
+
# 有变化时 idle 立刻归零、下一拍就恢复全速(最坏多等 33ms,
|
|
1196
|
+
# 拖动/滚动/播视频这类真的在动的情况根本进不到这个分支)。
|
|
1197
|
+
fps = self._target_fps if idle <= 20 else min(self._target_fps, PROBE_FPS)
|
|
1198
|
+
time.sleep(max(0.004, 1 / max(1, fps) - dt))
|
|
1199
|
+
|
|
1200
|
+
# ---- 窗口句柄 ----
|
|
1201
|
+
|
|
1202
|
+
def _hwnd_root(self):
|
|
1203
|
+
if self._hwnd is None:
|
|
1204
|
+
self._hwnd = ctypes.windll.user32.GetAncestor(self.root.winfo_id(), 2)
|
|
1205
|
+
return self._hwnd
|
|
1206
|
+
|
|
1207
|
+
def _set_exclude(self, on: bool) -> None:
|
|
1208
|
+
hwnd = self._hwnd_root()
|
|
1209
|
+
fn = ctypes.windll.user32.SetWindowDisplayAffinity
|
|
1210
|
+
fn.argtypes = [wintypes.HWND, wintypes.DWORD]
|
|
1211
|
+
fn.restype = wintypes.BOOL
|
|
1212
|
+
ok = bool(fn(hwnd, 0x11 if on else 0))
|
|
1213
|
+
self._excluded = ok and on
|
|
1214
|
+
if not ok:
|
|
1215
|
+
print("WDA failed", ctypes.GetLastError(), flush=True)
|
|
1216
|
+
|
|
1217
|
+
def _bbox(self):
|
|
1218
|
+
rc = _RECT()
|
|
1219
|
+
ctypes.windll.user32.GetWindowRect(self._hwnd_root(), ctypes.byref(rc))
|
|
1220
|
+
return (rc.left, rc.top, rc.right, rc.bottom)
|
|
1221
|
+
|
|
1222
|
+
def _sync_pos(self) -> None:
|
|
1223
|
+
"""把 Tk 当前左上角同步回 _x/_y。
|
|
1224
|
+
|
|
1225
|
+
拖动/缩放只改了 root.geometry,没改这两个缓存;折叠时如果继续用旧坐标,
|
|
1226
|
+
窗口会被拉回拖动前的位置(用户看到的就是「点收起位置跳回」)。
|
|
1227
|
+
"""
|
|
1228
|
+
try:
|
|
1229
|
+
self._x = int(self.root.winfo_x())
|
|
1230
|
+
self._y = int(self.root.winfo_y())
|
|
1231
|
+
except Exception:
|
|
1232
|
+
pass
|
|
1233
|
+
|
|
1234
|
+
def _save_geometry(self) -> None:
|
|
1235
|
+
h = self._saved_h if self._collapsed else self.root.winfo_height()
|
|
1236
|
+
write_config({"geometry": f"{self.root.winfo_width()}x{h}"
|
|
1237
|
+
f"+{self.root.winfo_x()}+{self.root.winfo_y()}"})
|
|
1238
|
+
|
|
1239
|
+
# ---- 关闭 / 隐藏 ----
|
|
1240
|
+
|
|
1241
|
+
def _close(self, _e=None):
|
|
1242
|
+
try:
|
|
1243
|
+
self._save_geometry()
|
|
1244
|
+
self._stop_mic()
|
|
1245
|
+
self._stop.set()
|
|
1246
|
+
self._pause.set()
|
|
1247
|
+
self._set_exclude(False)
|
|
1248
|
+
self._worker.join(timeout=0.3)
|
|
1249
|
+
try:
|
|
1250
|
+
self._renderer.close()
|
|
1251
|
+
except Exception:
|
|
1252
|
+
pass
|
|
1253
|
+
ctypes.windll.winmm.timeEndPeriod(1)
|
|
1254
|
+
finally:
|
|
1255
|
+
self.root.destroy()
|
|
1256
|
+
return "break"
|
|
1257
|
+
|
|
1258
|
+
# ---- 边缘缩放 / 拖动 ----
|
|
1259
|
+
|
|
1260
|
+
def _edge_at(self, x, y):
|
|
1261
|
+
w, h = self.root.winfo_width(), self.root.winfo_height()
|
|
1262
|
+
# 圆角是透明的,鼠标落在最外侧时命中的是 DWM 非客户区;
|
|
1263
|
+
# 命中带放宽到 20px,用户不必精确到 1px 也能拖边缘/四角。
|
|
1264
|
+
edge, corner = 20, 32
|
|
1265
|
+
if x <= corner and y <= corner:
|
|
1266
|
+
return "nw"
|
|
1267
|
+
if x >= w - corner and y <= corner:
|
|
1268
|
+
return "ne"
|
|
1269
|
+
if x <= corner and y >= h - corner:
|
|
1270
|
+
return "sw"
|
|
1271
|
+
if x >= w - corner and y >= h - corner:
|
|
1272
|
+
return "se"
|
|
1273
|
+
if y <= edge:
|
|
1274
|
+
return None # 顶部中间只拖动,不缩放
|
|
1275
|
+
if x <= edge:
|
|
1276
|
+
return "w"
|
|
1277
|
+
if x >= w - edge:
|
|
1278
|
+
return "e"
|
|
1279
|
+
if y >= h - edge:
|
|
1280
|
+
return "s"
|
|
1281
|
+
return None
|
|
1282
|
+
|
|
1283
|
+
@staticmethod
|
|
1284
|
+
def _cursor_for(mode):
|
|
1285
|
+
return {"nw": "size_nw_se", "se": "size_nw_se",
|
|
1286
|
+
"ne": "size_ne_sw", "sw": "size_ne_sw",
|
|
1287
|
+
"w": "size_we", "e": "size_we",
|
|
1288
|
+
"n": "size_ns", "s": "size_ns"}.get(mode, "")
|
|
1289
|
+
|
|
1290
|
+
def _hover(self, e):
|
|
1291
|
+
if self._resize:
|
|
1292
|
+
return
|
|
1293
|
+
try:
|
|
1294
|
+
self.canvas.config(cursor=self._cursor_for(self._edge_at(e.x, e.y)))
|
|
1295
|
+
except Exception:
|
|
1296
|
+
pass
|
|
1297
|
+
|
|
1298
|
+
def _leave(self, _e):
|
|
1299
|
+
if not self._resize:
|
|
1300
|
+
self.canvas.config(cursor="")
|
|
1301
|
+
|
|
1302
|
+
def _press(self, e):
|
|
1303
|
+
# 每次按下先把屏幕布局重新读一遍:拖动前插/拔显示器也能立刻生效
|
|
1304
|
+
self._sync_screens()
|
|
1305
|
+
# 麦克风图标画在玻璃里,不在 Canvas 上单独建控件;点它不能当拖窗
|
|
1306
|
+
mx, my = self._geo.mic_pos
|
|
1307
|
+
if abs(e.x - (mx + MIC_SIZE // 2)) <= 22 and abs(e.y - (my + MIC_SIZE // 2)) <= 22:
|
|
1308
|
+
self._toggle_mic()
|
|
1309
|
+
return
|
|
1310
|
+
x0, y0, x1, y1, _r = self._geo.input_shape
|
|
1311
|
+
if x0 <= e.x <= x1 and y0 <= e.y <= y1:
|
|
1312
|
+
self._focus_input()
|
|
1313
|
+
return
|
|
1314
|
+
mode = self._edge_at(e.x, e.y)
|
|
1315
|
+
if mode:
|
|
1316
|
+
self._resize = (mode, e.x_root, e.y_root,
|
|
1317
|
+
self.root.winfo_x(), self.root.winfo_y(),
|
|
1318
|
+
self.root.winfo_width(), self.root.winfo_height())
|
|
1319
|
+
self._drag = None
|
|
1320
|
+
self._pause.set()
|
|
1321
|
+
return
|
|
1322
|
+
self._drag = (e.x_root - self.root.winfo_x(),
|
|
1323
|
+
e.y_root - self.root.winfo_y())
|
|
1324
|
+
|
|
1325
|
+
def _motion(self, e):
|
|
1326
|
+
if self._resize:
|
|
1327
|
+
mode, sx, sy, x0, y0, w0, h0 = self._resize
|
|
1328
|
+
dx, dy = e.x_root - sx, e.y_root - sy
|
|
1329
|
+
nw, nh, nx, ny = w0, h0, x0, y0
|
|
1330
|
+
# 缩放边界 = 虚拟桌面(多显示器),不是主屏
|
|
1331
|
+
vx, vy, vw, vh = self._vx, self._vy, self._vw, self._vh
|
|
1332
|
+
if "e" in mode:
|
|
1333
|
+
nw = max(MIN_W, min(max(MIN_W, vx + vw - x0), w0 + dx))
|
|
1334
|
+
if "w" in mode:
|
|
1335
|
+
nw = max(MIN_W, min(max(MIN_W, x0 + w0 - vx), w0 - dx))
|
|
1336
|
+
nx = x0 + w0 - nw
|
|
1337
|
+
if "s" in mode:
|
|
1338
|
+
nh = max(MIN_H, min(max(MIN_H, vy + vh - y0), h0 + dy))
|
|
1339
|
+
if "n" in mode:
|
|
1340
|
+
nh = max(MIN_H, min(max(MIN_H, y0 + h0 - vy), h0 - dy))
|
|
1341
|
+
ny = y0 + h0 - nh
|
|
1342
|
+
nx = max(vx, min(nx, max(vx, vx + vw - nw)))
|
|
1343
|
+
ny = max(vy, min(ny, max(vy, vy + vh - nh)))
|
|
1344
|
+
self.root.geometry(f"{nw}x{nh}+{nx}+{ny}")
|
|
1345
|
+
now = time.time()
|
|
1346
|
+
if now - self._preview_at >= 0.04:
|
|
1347
|
+
self._preview_at = now
|
|
1348
|
+
self._preview_resize(nw, nh)
|
|
1349
|
+
return
|
|
1350
|
+
if self._drag:
|
|
1351
|
+
# 拖到屏幕外时窗口坐标会变负,抓屏区域随之越界(高度算出负数),
|
|
1352
|
+
# 背景永远画不出来 —— 表现为花屏。必须钳位。
|
|
1353
|
+
# 边界是虚拟桌面而不是主屏:第二块屏在主屏左边时坐标本来就是负的,
|
|
1354
|
+
# 老代码 max(0, …) 等于把窗口钉死在一块屏里。
|
|
1355
|
+
vx, vy, vw, vh = self._vx, self._vy, self._vw, self._vh
|
|
1356
|
+
nx = e.x_root - self._drag[0]
|
|
1357
|
+
ny = e.y_root - self._drag[1]
|
|
1358
|
+
nx = max(vx, min(nx, max(vx, vx + vw - self.root.winfo_width())))
|
|
1359
|
+
ny = max(vy, min(ny, max(vy, vy + vh - self.root.winfo_height())))
|
|
1360
|
+
self.root.geometry(f"+{nx}+{ny}")
|
|
1361
|
+
|
|
1362
|
+
def _release(self, e):
|
|
1363
|
+
if self._resize:
|
|
1364
|
+
self._resize = None
|
|
1365
|
+
self._preview_at = 0.0
|
|
1366
|
+
self.root.update_idletasks()
|
|
1367
|
+
self._rebuild(max(MIN_W, self.root.winfo_width()),
|
|
1368
|
+
max(MIN_H, self.root.winfo_height()))
|
|
1369
|
+
self._sync_pos()
|
|
1370
|
+
self._hover(e)
|
|
1371
|
+
return
|
|
1372
|
+
self._drag = None
|
|
1373
|
+
self._sync_pos()
|
|
1374
|
+
|
|
1375
|
+
def run(self):
|
|
1376
|
+
self.root.bind("<Escape>", self._close)
|
|
1377
|
+
self.root.bind("<MouseWheel>", self._wheel)
|
|
1378
|
+
self.root.bind("<Motion>", self._hover, add="+")
|
|
1379
|
+
self.root.bind("<Leave>", self._leave, add="+")
|
|
1380
|
+
self.root.bind("<ButtonPress-1>", self._press, add="+")
|
|
1381
|
+
self.root.bind("<B1-Motion>", self._motion, add="+")
|
|
1382
|
+
self.root.bind("<ButtonRelease-1>", self._release, add="+")
|
|
1383
|
+
self.root.mainloop()
|
|
1384
|
+
|
|
1385
|
+
|
|
1386
|
+
if __name__ == "__main__":
|
|
1387
|
+
LiveGlass().run()
|