dsh-watch-together 0.2.9 → 0.2.11
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 +144 -0
- package/README.md +15 -5
- package/docs/INTERFACES.zh.md +29 -12
- package/dsh.plugin.json +12 -12
- package/examples/floating-chat/glass_capture.py +286 -0
- package/examples/floating-chat/glass_cpu.py +174 -0
- package/examples/floating-chat/glass_geometry.py +288 -0
- package/examples/floating-chat/glass_gpu.py +509 -0
- package/examples/floating-chat/glass_window.py +4 -7
- package/examples/floating-chat/glass_window_gpu.py +14 -516
- package/examples/floating-chat/requirements-glass.txt +5 -0
- package/examples/floating-chat/run-glass.cmd +5 -1
- package/examples/floating-chat/window.py +1210 -1932
- package/examples/floating-chat/window_widgets.py +288 -0
- package/package.json +115 -115
|
@@ -1,1932 +1,1210 @@
|
|
|
1
|
-
#!/usr/bin/env python3
|
|
2
|
-
"""
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
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
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
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
|
-
|
|
417
|
-
|
|
418
|
-
|
|
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
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
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
|
-
self.
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
self.
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
self.
|
|
537
|
-
|
|
538
|
-
self.
|
|
539
|
-
self.
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
self.
|
|
547
|
-
self.
|
|
548
|
-
self.
|
|
549
|
-
|
|
550
|
-
self.
|
|
551
|
-
|
|
552
|
-
self.
|
|
553
|
-
self.
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
self.
|
|
558
|
-
|
|
559
|
-
self.
|
|
560
|
-
|
|
561
|
-
self.
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
self.
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
self.
|
|
578
|
-
self.
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
self.
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
self.
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
self.
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
try:
|
|
603
|
-
if
|
|
604
|
-
self.
|
|
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
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
self.
|
|
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
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
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
|
-
self.
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
self.
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
if
|
|
835
|
-
return
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
self.
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
try:
|
|
872
|
-
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
def
|
|
894
|
-
"""
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
"""
|
|
899
|
-
if
|
|
900
|
-
return
|
|
901
|
-
|
|
902
|
-
w, h =
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
def
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
self.
|
|
956
|
-
|
|
957
|
-
self.
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
return
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
if
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
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
|
-
if
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
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
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
text = self.entry.get()
|
|
1212
|
-
focused = self.root.focus_get() is self.entry
|
|
1213
|
-
except Exception:
|
|
1214
|
-
text, focused = "", False
|
|
1215
|
-
x0, y0, x1, y1, _r = SHAPES[4]
|
|
1216
|
-
x, y = x0 + 84, y0 + 27
|
|
1217
|
-
maxw = max(60, x1 - x0 - 100)
|
|
1218
|
-
if not text and not focused:
|
|
1219
|
-
self._text(x, y, "在这里跟它说话…", anchor="w",
|
|
1220
|
-
fill="#8e9db2", font=f(11),
|
|
1221
|
-
tags=("inputtext", "inputzone"))
|
|
1222
|
-
elif text:
|
|
1223
|
-
fobj = tkfont.Font(root=self.root, font=f(11))
|
|
1224
|
-
while text and fobj.measure(text) > maxw:
|
|
1225
|
-
text = text[1:]
|
|
1226
|
-
self._text(x, y, text, anchor="w", fill="#eef6ff",
|
|
1227
|
-
font=f(11), tags=("inputtext", "inputzone"))
|
|
1228
|
-
if focused:
|
|
1229
|
-
cx = x + fobj.measure(text)
|
|
1230
|
-
c.create_line(cx + 1, y - 9, cx + 1, y + 9,
|
|
1231
|
-
fill="#b9d7ff", width=1, tags=("inputcursor",))
|
|
1232
|
-
elif focused:
|
|
1233
|
-
c.create_line(x + 1, y - 9, x + 1, y + 9,
|
|
1234
|
-
fill="#b9d7ff", width=1, tags=("inputcursor",))
|
|
1235
|
-
self._text_items = [i for i in c.find_all() if c.type(i) == "text"]
|
|
1236
|
-
|
|
1237
|
-
def _on_input_key(self, _e=None):
|
|
1238
|
-
self._render_input()
|
|
1239
|
-
return None
|
|
1240
|
-
|
|
1241
|
-
def _focus_input(self, _e=None):
|
|
1242
|
-
try:
|
|
1243
|
-
# 用户已经点了输入区,这里可以抢焦点;启动时仍然不抢系统焦点
|
|
1244
|
-
self.entry.focus_force()
|
|
1245
|
-
self.entry.icursor("end")
|
|
1246
|
-
except Exception:
|
|
1247
|
-
pass
|
|
1248
|
-
self._render_input()
|
|
1249
|
-
|
|
1250
|
-
# ---- 改名 ----
|
|
1251
|
-
|
|
1252
|
-
def _edit_title(self, _event=None):
|
|
1253
|
-
"""双击标题:就地变输入框(PR #15 底稿没有改名,补上)。"""
|
|
1254
|
-
if getattr(self, "_editing", False):
|
|
1255
|
-
return "break"
|
|
1256
|
-
self._editing = True
|
|
1257
|
-
if self._title_entry is None:
|
|
1258
|
-
self._title_entry = tk.Entry(self.root, font=f(15), relief="flat", bd=0,
|
|
1259
|
-
bg="#1b2330", fg="#f7fbff", insertbackground="#f7fbff")
|
|
1260
|
-
self._title_entry.bind("<Return>", self._commit_title)
|
|
1261
|
-
self._title_entry.bind("<Escape>", self._cancel_title)
|
|
1262
|
-
self._title_entry.bind("<FocusOut>", self._commit_title)
|
|
1263
|
-
self._title_entry.delete(0, "end")
|
|
1264
|
-
self._title_entry.insert(0, self.name)
|
|
1265
|
-
if self._title_item is not None:
|
|
1266
|
-
self.canvas.delete(self._title_item)
|
|
1267
|
-
self._title_item = self.canvas.create_window(68, 38, anchor="w",
|
|
1268
|
-
window=self._title_entry, width=180)
|
|
1269
|
-
self._title_entry.focus_set()
|
|
1270
|
-
self._title_entry.select_range(0, "end")
|
|
1271
|
-
return "break"
|
|
1272
|
-
|
|
1273
|
-
def _commit_title(self, _event=None):
|
|
1274
|
-
if not getattr(self, "_editing", False):
|
|
1275
|
-
return
|
|
1276
|
-
self._editing = False
|
|
1277
|
-
text = self._title_entry.get().strip()
|
|
1278
|
-
if self._title_item is not None:
|
|
1279
|
-
self.canvas.delete(self._title_item)
|
|
1280
|
-
self._title_item = None
|
|
1281
|
-
if text and text != self.name:
|
|
1282
|
-
self.name = text[:24]
|
|
1283
|
-
self.root.title(self.name)
|
|
1284
|
-
write_config({"name": self.name})
|
|
1285
|
-
try:
|
|
1286
|
-
if self._protocol is not None:
|
|
1287
|
-
self._protocol.action("rename", value=self.name)
|
|
1288
|
-
except Exception:
|
|
1289
|
-
pass
|
|
1290
|
-
self._refresh_feed()
|
|
1291
|
-
return "break"
|
|
1292
|
-
|
|
1293
|
-
def _cancel_title(self):
|
|
1294
|
-
self._editing = False
|
|
1295
|
-
if self._title_item is not None:
|
|
1296
|
-
self.canvas.delete(self._title_item)
|
|
1297
|
-
self._title_item = None
|
|
1298
|
-
self._refresh_feed()
|
|
1299
|
-
return "break"
|
|
1300
|
-
|
|
1301
|
-
# ---- 折叠 ----
|
|
1302
|
-
|
|
1303
|
-
def toggle_collapse(self, _event=None):
|
|
1304
|
-
global H
|
|
1305
|
-
# 先读真实坐标再切高度:_x/_y 只是启动时的缓存,拖动/缩放后已过期。
|
|
1306
|
-
self._sync_pos()
|
|
1307
|
-
self._collapsed = not getattr(self, "_collapsed", False)
|
|
1308
|
-
if self._collapsed:
|
|
1309
|
-
self._saved_h = H
|
|
1310
|
-
H = 2 * 12 + 54
|
|
1311
|
-
self._collapsed_frame = self._make_collapsed_frame()
|
|
1312
|
-
# 折叠态只需要标题栏:不重建整窗几何,直接缩窗口 + 只画头。
|
|
1313
|
-
# 这样不会在小高度下生成负尺寸的输入框/主体玻璃。
|
|
1314
|
-
self.root.geometry(f"{W}x{H}+{self._x}+{self._y}")
|
|
1315
|
-
self._build_ui()
|
|
1316
|
-
else:
|
|
1317
|
-
H = getattr(self, "_saved_h", H)
|
|
1318
|
-
self._collapsed_frame = None
|
|
1319
|
-
rebuild_geometry(W, H)
|
|
1320
|
-
self.root.geometry(f"{W}x{H}+{self._x}+{self._y}")
|
|
1321
|
-
self._rebuild(W, H)
|
|
1322
|
-
try:
|
|
1323
|
-
if self._protocol is not None:
|
|
1324
|
-
self._protocol.action("collapse" if self._collapsed else "expand")
|
|
1325
|
-
except Exception:
|
|
1326
|
-
pass
|
|
1327
|
-
|
|
1328
|
-
def _message_shapes(self, shown):
|
|
1329
|
-
"""把四个固定大框换成按正文文字测量的聊天气泡。
|
|
1330
|
-
|
|
1331
|
-
时间戳不再算进气泡:它贴在气泡上方,气泡只包正文。先按最大
|
|
1332
|
-
可用宽度折行,再取实际最长行的宽度;短消息不会撑满整行。
|
|
1333
|
-
"""
|
|
1334
|
-
w, h = W, H
|
|
1335
|
-
base = shapes_for(w, h)
|
|
1336
|
-
left, right = base[0][0], base[0][2]
|
|
1337
|
-
max_bubble = min(right - left, max(240, int((right - left) * 0.72)))
|
|
1338
|
-
min_bubble = min(110, max_bubble)
|
|
1339
|
-
pad = 18
|
|
1340
|
-
body_font = f(11)
|
|
1341
|
-
stamp_font = f(9)
|
|
1342
|
-
fbody = tkfont.Font(root=self.root, font=body_font)
|
|
1343
|
-
fstamp = tkfont.Font(root=self.root, font=stamp_font)
|
|
1344
|
-
ls = max(1, fbody.metrics("linespace"))
|
|
1345
|
-
shapes, infos = [], []
|
|
1346
|
-
for i in range(4):
|
|
1347
|
-
if i >= len(shown):
|
|
1348
|
-
# 没有第 5 条消息时留空,不要把固定大框继续画出来。
|
|
1349
|
-
shapes.append((0, 0, 0, 0, 0))
|
|
1350
|
-
continue
|
|
1351
|
-
msg = shown[i]
|
|
1352
|
-
x0, y0, x1, y1, _r = base[i]
|
|
1353
|
-
slot_h = y1 - y0
|
|
1354
|
-
is_user = str(msg.get("role") or "").lower() in ("user", "me", "human")
|
|
1355
|
-
stamp = str(msg.get("stamp") or "")
|
|
1356
|
-
stamp_h = fstamp.metrics("linespace") if stamp else 0
|
|
1357
|
-
body_avail = max(12, slot_h - stamp_h)
|
|
1358
|
-
content_max = max(80, max_bubble - 2 * pad)
|
|
1359
|
-
lines = self._fit_lines(msg.get("text") or "", body_font,
|
|
1360
|
-
content_max, body_avail)
|
|
1361
|
-
inner = max([0] + [fbody.measure(line) for line in lines])
|
|
1362
|
-
bw = min(max_bubble, max(min_bubble, inner + 2 * pad))
|
|
1363
|
-
# 上下留白比原来多一圈;正文垂直居中,气泡不会贴着字。
|
|
1364
|
-
bh = min(body_avail, max(ls + 16, len(lines) * ls + 16))
|
|
1365
|
-
by0 = y0 + stamp_h
|
|
1366
|
-
by1 = min(y1, by0 + bh)
|
|
1367
|
-
if is_user:
|
|
1368
|
-
bx1 = right
|
|
1369
|
-
bx0 = bx1 - bw
|
|
1370
|
-
tx, ta = bx1 - pad, "ne"
|
|
1371
|
-
else:
|
|
1372
|
-
bx0 = left
|
|
1373
|
-
bx1 = bx0 + bw
|
|
1374
|
-
tx, ta = bx0 + pad, "nw"
|
|
1375
|
-
shape = (bx0, by0, bx1, by1, min(26, max(16, bh // 3)))
|
|
1376
|
-
shapes.append(shape)
|
|
1377
|
-
infos.append({
|
|
1378
|
-
"shape": shape, "is_user": is_user, "stamp": stamp,
|
|
1379
|
-
"tx": tx, "ta": ta, "lines": lines,
|
|
1380
|
-
"body_font": body_font,
|
|
1381
|
-
"ty": by0 + max(4, (bh - len(lines) * ls) // 2),
|
|
1382
|
-
"stamp_y": y0, "ls": ls,
|
|
1383
|
-
})
|
|
1384
|
-
shapes.extend(base[4:])
|
|
1385
|
-
return shapes, infos
|
|
1386
|
-
|
|
1387
|
-
def _build_ui(self):
|
|
1388
|
-
c = self.canvas
|
|
1389
|
-
old_text, old_focus, old_cursor = "", False, 0
|
|
1390
|
-
try:
|
|
1391
|
-
if getattr(self, "entry", None) is not None:
|
|
1392
|
-
old_text = self.entry.get()
|
|
1393
|
-
old_cursor = self.entry.index("insert")
|
|
1394
|
-
old_focus = self.root.focus_get() is self.entry
|
|
1395
|
-
self.entry.destroy()
|
|
1396
|
-
except Exception:
|
|
1397
|
-
pass
|
|
1398
|
-
self.entry = None
|
|
1399
|
-
# 重排时必须先清掉上一轮的文字和底图:只 create 不 delete 会在每一条
|
|
1400
|
-
# 新消息到达时把旧文字留在 Canvas 上,形成重影(而且旧的左对齐用户消息
|
|
1401
|
-
# 会和新的右对齐版本叠在一起,看起来像“左右没分开”)。
|
|
1402
|
-
c.delete("all")
|
|
1403
|
-
# 背景图 item 先建,后续文字都在它上面
|
|
1404
|
-
self._bg = c.create_image(0, 0, anchor="nw")
|
|
1405
|
-
frame = self._collapsed_frame if getattr(self, "_collapsed", False) else self._last_frame
|
|
1406
|
-
if frame is not None:
|
|
1407
|
-
try:
|
|
1408
|
-
self._img = ImageTk.PhotoImage(frame, master=self.canvas)
|
|
1409
|
-
c.itemconfigure(self._bg, image=self._img)
|
|
1410
|
-
except Exception:
|
|
1411
|
-
pass
|
|
1412
|
-
if PREVIEW:
|
|
1413
|
-
c.create_rectangle(0, 0, W, H, fill="#0d131d", outline="",
|
|
1414
|
-
tags=("preview-bg",))
|
|
1415
|
-
c.tag_lower("preview-bg")
|
|
1416
|
-
|
|
1417
|
-
w, h = W, H
|
|
1418
|
-
# 状态点:仓库实现里的"亮着的蓝点 + 很淡的光环"。纯色圆点像贴纸,
|
|
1419
|
-
# 带一圈环才像状态灯;录音时变红。
|
|
1420
|
-
dot = "#e05a5a" if (self._mic_proc is not None and self._mic_proc.poll() is None) else "#0a84ff"
|
|
1421
|
-
# 标题栏玻璃是 y=12..66,中心 39;右边 ▾/✕ 也在 38。点、名字、
|
|
1422
|
-
# 状态必须同一条水平中线,且字号要能完整落在条内,不能压到气泡区。
|
|
1423
|
-
cy = 38
|
|
1424
|
-
dot_x = 54
|
|
1425
|
-
title_x = 68
|
|
1426
|
-
# 点放大但保留光环;之前半径 3/7 在 54px 的标题条里太小。
|
|
1427
|
-
c.create_oval(dot_x - 9, cy - 9, dot_x + 9, cy + 9,
|
|
1428
|
-
outline="#2b4d73" if dot == "#0a84ff" else "#6b3030", width=1)
|
|
1429
|
-
c.create_oval(dot_x - 4, cy - 4, dot_x + 4, cy + 4, fill=dot, outline="")
|
|
1430
|
-
name = self.name[:10]
|
|
1431
|
-
title_font = f(16, True)
|
|
1432
|
-
self._text(title_x, cy, name, anchor="w", fill="#f7fbff",
|
|
1433
|
-
font=title_font, tags=("title",))
|
|
1434
|
-
count = self._status or f"{len(self.messages)} 条"
|
|
1435
|
-
try:
|
|
1436
|
-
nx = title_x + tkfont.Font(root=self.root, font=title_font).measure(name) + 10
|
|
1437
|
-
except Exception:
|
|
1438
|
-
nx = title_x + 72
|
|
1439
|
-
self._text(nx, cy, count, anchor="w",
|
|
1440
|
-
fill="#ffb4b4" if self._status else "#dbe6f5",
|
|
1441
|
-
font=f(11))
|
|
1442
|
-
# 右上角按钮跟随窗口宽度内收
|
|
1443
|
-
self._text(w - 94, 38, "▾", anchor="w",
|
|
1444
|
-
fill="#7f8da3", font=f(12), tags=("collapse",))
|
|
1445
|
-
self._text(w - 58, 38, "✕", anchor="w", fill="#9fb0c8",
|
|
1446
|
-
font=f(13), tags=("close",))
|
|
1447
|
-
|
|
1448
|
-
if getattr(self, "_collapsed", False):
|
|
1449
|
-
self.canvas.config(width=w, height=h)
|
|
1450
|
-
c.tag_bind("close", "<Button-1>", self._close)
|
|
1451
|
-
c.tag_bind("collapse", "<Button-1>", self.toggle_collapse)
|
|
1452
|
-
# 双击改名:单击要留给"按住标题拖动窗口",两者不能抢同一个事件
|
|
1453
|
-
c.tag_bind("title", "<Double-Button-1>", self._edit_title)
|
|
1454
|
-
self._bar_len = max(72, int(w * BAR_FRAC))
|
|
1455
|
-
self._bar = c.create_line(w // 2 - self._bar_len // 2, BAR_Y,
|
|
1456
|
-
w // 2 + self._bar_len // 2, BAR_Y,
|
|
1457
|
-
width=BAR_THICK, fill="#b8cbe0",
|
|
1458
|
-
capstyle="round")
|
|
1459
|
-
return
|
|
1460
|
-
|
|
1461
|
-
shown = self._visible_messages()
|
|
1462
|
-
if shown:
|
|
1463
|
-
shapes, infos = self._message_shapes(shown)
|
|
1464
|
-
else:
|
|
1465
|
-
shapes, infos = shapes_for(w, h), []
|
|
1466
|
-
if shapes != SHAPES:
|
|
1467
|
-
with self._geo_lock:
|
|
1468
|
-
apply_shapes(shapes)
|
|
1469
|
-
# 气泡/内框形状变了,合成的遮罩也变了;必须让上一帧的静态跳帧
|
|
1470
|
-
# 失效,否则桌面静止时 worker 会一直 return None,新遮罩/色散
|
|
1471
|
-
# 要等用户动一下鼠标或背景动一下才出现(调试 FORCE 模式下看不出)。
|
|
1472
|
-
self._invalidate_glass()
|
|
1473
|
-
# 每条消息只按正文占一块;时间戳贴在气泡上方,不进框。
|
|
1474
|
-
for info in infos:
|
|
1475
|
-
bx0, by0, bx1, by1, _r = info["shape"]
|
|
1476
|
-
if info["stamp"]:
|
|
1477
|
-
self._text(info["tx"], info["stamp_y"], info["stamp"],
|
|
1478
|
-
anchor=info["ta"], fill="#a8b7c9",
|
|
1479
|
-
font=f(9), tags=("feed",))
|
|
1480
|
-
for li, line in enumerate(info["lines"]):
|
|
1481
|
-
self._text(info["tx"], info["ty"] + li * info["ls"], line,
|
|
1482
|
-
anchor=info["ta"], fill="#edf6ff",
|
|
1483
|
-
font=info["body_font"], tags=("feed",))
|
|
1484
|
-
if not shown:
|
|
1485
|
-
# 空态:`bh` 是旧代码的局部变量,这里只能从 SHAPES 反推第一格高度
|
|
1486
|
-
self._text(w // 2, SHAPES[0][1] + (SHAPES[0][3] - SHAPES[0][1]) // 2,
|
|
1487
|
-
"在这里跟它说话 · 滚轮翻历史", anchor="center",
|
|
1488
|
-
fill="#8e9db2", font=f(12), tags=("feed",))
|
|
1489
|
-
|
|
1490
|
-
# 输入:Entry 只做 IME/键盘的背面接收器,放到 2x2 且底色是键色,
|
|
1491
|
-
# 真正显示的文字画在 Canvas 上 —— 否则玻璃里会出现一个突兀的黑条。
|
|
1492
|
-
x0, y0, x1, y1, _r = SHAPES[4]
|
|
1493
|
-
self.entry = tk.Entry(self.root, bg=KEY, fg=KEY, insertbackground=KEY,
|
|
1494
|
-
relief="flat", bd=0, font=f(11), insertwidth=0,
|
|
1495
|
-
highlightthickness=0)
|
|
1496
|
-
self._entry_item = c.create_window(
|
|
1497
|
-
x0 + 84, y0 + 27, window=self.entry, anchor="w",
|
|
1498
|
-
width=2, height=2, tags=("feed",))
|
|
1499
|
-
self.entry.bind("<KeyRelease>", self._on_input_key)
|
|
1500
|
-
self.entry.bind("<Return>", self._send)
|
|
1501
|
-
self.entry.bind("<FocusIn>", lambda _e: self._render_input())
|
|
1502
|
-
self.entry.bind("<FocusOut>", lambda _e: self._render_input())
|
|
1503
|
-
if old_text:
|
|
1504
|
-
self.entry.insert(0, old_text)
|
|
1505
|
-
self.entry.icursor(min(old_cursor, len(old_text)))
|
|
1506
|
-
if old_focus:
|
|
1507
|
-
self.entry.focus_set()
|
|
1508
|
-
self._render_input()
|
|
1509
|
-
|
|
1510
|
-
x0, y0, x1, y1, _r = SHAPES[5]
|
|
1511
|
-
self._text((x0 + x1) // 2, y0 + 27, "发送", tags=("send", "feed"),
|
|
1512
|
-
fill="#ffffff", font=f(12, True))
|
|
1513
|
-
|
|
1514
|
-
# 麦克风图标和仓库一样贴进玻璃帧(_capture 里按 MIC_POS 合成),
|
|
1515
|
-
# 不在 Canvas 上单独建图;点击仍由 _press 在 MIC_POS 附近识别。
|
|
1516
|
-
# 最上面正中的细条,长度随窗口宽度走
|
|
1517
|
-
self._bar_len = max(72, int(w * BAR_FRAC))
|
|
1518
|
-
self._bar = c.create_line(w // 2 - self._bar_len // 2, BAR_Y,
|
|
1519
|
-
w // 2 + self._bar_len // 2, BAR_Y,
|
|
1520
|
-
width=BAR_THICK, fill="#b8cbe0",
|
|
1521
|
-
capstyle="round")
|
|
1522
|
-
# 新建的条是浅色,缓存也要跟着归位;否则折叠重建后旧色值还在,
|
|
1523
|
-
# `bar_fill == cur` 会把这次同步直接跳过去,条和缓存就对不上了。
|
|
1524
|
-
self._bar_color = "#b8cbe0"
|
|
1525
|
-
|
|
1526
|
-
c.tag_bind("close", "<Button-1>", self._close)
|
|
1527
|
-
c.tag_bind("send", "<Button-1>", self._send)
|
|
1528
|
-
c.tag_bind("collapse", "<Button-1>", self.toggle_collapse)
|
|
1529
|
-
# 同上:单击留给拖动,改名走双击
|
|
1530
|
-
c.tag_bind("title", "<Double-Button-1>", self._edit_title)
|
|
1531
|
-
c.tag_bind("inputzone", "<Button-1>", self._focus_input)
|
|
1532
|
-
c.tag_raise("close")
|
|
1533
|
-
c.tag_raise("send")
|
|
1534
|
-
self._draw_scrollbar()
|
|
1535
|
-
if getattr(self, "_collapsed", False):
|
|
1536
|
-
c.itemconfigure("feed", state="hidden")
|
|
1537
|
-
|
|
1538
|
-
def _draw_scrollbar(self):
|
|
1539
|
-
"""仓库布局:滚动条放在主体玻璃右侧外面,不占消息区。"""
|
|
1540
|
-
c = self.canvas
|
|
1541
|
-
c.delete("sbar")
|
|
1542
|
-
total = len(self.messages)
|
|
1543
|
-
visible = 4
|
|
1544
|
-
if total <= visible:
|
|
1545
|
-
return
|
|
1546
|
-
_top, body, _bot = slabs_for(W, H)
|
|
1547
|
-
x = W - 6
|
|
1548
|
-
top = body[1] + 8
|
|
1549
|
-
view_h = max(1, body[3] - body[1] - 16)
|
|
1550
|
-
frac = visible / float(total)
|
|
1551
|
-
thumb = max(24, int(view_h * frac))
|
|
1552
|
-
max_off = max(1, total - visible)
|
|
1553
|
-
t = 1.0 - max(0, min(max_off, self._msg_offset)) / float(max_off)
|
|
1554
|
-
y0 = top + t * max(0, view_h - thumb)
|
|
1555
|
-
c.create_line(x, y0, x, y0 + thumb, width=3, fill="#8d95a4",
|
|
1556
|
-
capstyle="round", tags=("sbar",))
|
|
1557
|
-
c.tag_raise("sbar")
|
|
1558
|
-
|
|
1559
|
-
def _preview_resize(self, w, h):
|
|
1560
|
-
"""拖拽时先缩放上一帧并重排文字,松手后再重建玻璃贴图。"""
|
|
1561
|
-
global W, H, SHAPES, MIC_POS
|
|
1562
|
-
W, H = w, h
|
|
1563
|
-
SHAPES = shapes_for(w, h)
|
|
1564
|
-
MIC_POS = (53, h - 67)
|
|
1565
|
-
self.canvas.config(width=w, height=h)
|
|
1566
|
-
self.canvas.delete("all")
|
|
1567
|
-
self._img = None
|
|
1568
|
-
self._bg = None
|
|
1569
|
-
self._prev_out = None
|
|
1570
|
-
self._text_items = []
|
|
1571
|
-
self._text_colors = {}
|
|
1572
|
-
self._text_pending = {}
|
|
1573
|
-
self._text_shadows = {}
|
|
1574
|
-
self._build_ui()
|
|
1575
|
-
self._add_text_shadows()
|
|
1576
|
-
shadow_ids = {sid for ids in self._text_shadows.values() for sid in ids}
|
|
1577
|
-
self._text_items = [i for i in self.canvas.find_all()
|
|
1578
|
-
if self.canvas.type(i) == "text"
|
|
1579
|
-
and i not in shadow_ids]
|
|
1580
|
-
if self._last_frame is not None:
|
|
1581
|
-
preview = self._last_frame.resize(
|
|
1582
|
-
(w, h), Image.Resampling.BILINEAR)
|
|
1583
|
-
self._img = ImageTk.PhotoImage(preview, master=self.canvas)
|
|
1584
|
-
self.canvas.itemconfigure(self._bg, image=self._img)
|
|
1585
|
-
self.canvas.tag_lower(self._bg)
|
|
1586
|
-
# 预览期间也立刻按新背景选字色,不等松手
|
|
1587
|
-
self._color_at = 0.0
|
|
1588
|
-
self._aux_at = 0.0
|
|
1589
|
-
self._adapt_text(preview)
|
|
1590
|
-
self._adapt_aux(preview)
|
|
1591
|
-
|
|
1592
|
-
def _rebuild(self, w, h):
|
|
1593
|
-
"""按新窗口尺寸重建玻璃贴图和全部 Canvas 控件。"""
|
|
1594
|
-
self._pause.set()
|
|
1595
|
-
with self._geo_lock:
|
|
1596
|
-
rebuild_geometry(w, h)
|
|
1597
|
-
self.canvas.config(width=w, height=h)
|
|
1598
|
-
self.canvas.delete("all")
|
|
1599
|
-
self._img = None
|
|
1600
|
-
self._bg = None
|
|
1601
|
-
self._prev_out = None
|
|
1602
|
-
self._motion_blur = 0.0
|
|
1603
|
-
self._collapsed_frame = None
|
|
1604
|
-
self._text_items = []
|
|
1605
|
-
self._text_colors = {}
|
|
1606
|
-
self._text_pending = {}
|
|
1607
|
-
self._text_shadows = {}
|
|
1608
|
-
self._color_at = 0.0
|
|
1609
|
-
self._aux_at = 0.0
|
|
1610
|
-
self._sig = None
|
|
1611
|
-
self._sig_bbox = None
|
|
1612
|
-
self._build_ui()
|
|
1613
|
-
self._add_text_shadows()
|
|
1614
|
-
shadow_ids = {sid for ids in self._text_shadows.values() for sid in ids}
|
|
1615
|
-
self._text_items = [i for i in self.canvas.find_all()
|
|
1616
|
-
if self.canvas.type(i) == "text"
|
|
1617
|
-
and i not in shadow_ids]
|
|
1618
|
-
while True:
|
|
1619
|
-
try:
|
|
1620
|
-
self._q.get_nowait()
|
|
1621
|
-
except queue.Empty:
|
|
1622
|
-
break
|
|
1623
|
-
if self._last_frame is not None:
|
|
1624
|
-
seed = self._last_frame.resize(
|
|
1625
|
-
(w, h), Image.Resampling.BILINEAR)
|
|
1626
|
-
self._img = ImageTk.PhotoImage(seed, master=self.canvas)
|
|
1627
|
-
self.canvas.itemconfigure(self._bg, image=self._img)
|
|
1628
|
-
self.canvas.tag_lower(self._bg)
|
|
1629
|
-
self._color_at = 0.0
|
|
1630
|
-
self._aux_at = 0.0
|
|
1631
|
-
self._adapt_text(seed)
|
|
1632
|
-
self._adapt_aux(seed)
|
|
1633
|
-
self._pause.clear()
|
|
1634
|
-
|
|
1635
|
-
def _add_text_shadows(self):
|
|
1636
|
-
"""不再叠偏移描边;之前四向黑影会看起来像重影。"""
|
|
1637
|
-
self._text_shadows = {}
|
|
1638
|
-
|
|
1639
|
-
def _adapt_text(self, out):
|
|
1640
|
-
"""按文字背后的暗部/亮部比例选色。
|
|
1641
|
-
|
|
1642
|
-
**必须带滞回**:气泡是透明的,背后就是桌面/视频/网页。画面一动,
|
|
1643
|
-
这块背景的中位亮度就在 0.5 附近来回穿越,而原来这里是 `p50 < 0.50`
|
|
1644
|
-
硬切、每 0.12 秒判一次 —— 结果字一秒能翻七八次黑白,肉眼就是"一直闪"。
|
|
1645
|
-
|
|
1646
|
-
现在两道闸:
|
|
1647
|
-
1. **滞回**:白的要暗到 0.38 才翻深,深的要亮到 0.62 才翻白;
|
|
1648
|
-
中间的 0.38~0.62 一律保持现状,不给抖动留门。
|
|
1649
|
-
2. **连续两次同向才切**:单帧跳变忽略,最坏 0.24 秒才换一次色。
|
|
1650
|
-
"""
|
|
1651
|
-
now = time.time()
|
|
1652
|
-
if now - self._color_at < 0.12:
|
|
1653
|
-
return
|
|
1654
|
-
self._color_at = now
|
|
1655
|
-
for item in self._text_items:
|
|
1656
|
-
box = self.canvas.bbox(item)
|
|
1657
|
-
if not box:
|
|
1658
|
-
continue
|
|
1659
|
-
x0, y0, x1, y1 = box
|
|
1660
|
-
x0, y0 = max(0, x0 - 2), max(0, y0 - 2)
|
|
1661
|
-
x1, y1 = min(W, x1 + 2), min(H, y1 + 2)
|
|
1662
|
-
if x1 <= x0 or y1 <= y0:
|
|
1663
|
-
continue
|
|
1664
|
-
gray = np.asarray(out.crop((x0, y0, x1, y1)).convert("L"))
|
|
1665
|
-
p50 = float(np.percentile(gray, 50)) / 255.0
|
|
1666
|
-
dark = float(np.mean(gray < 107))
|
|
1667
|
-
light = float(np.mean(gray > 158))
|
|
1668
|
-
cur = self._text_colors.get(item)
|
|
1669
|
-
# 先看这块背景是偏暗还是偏亮(这两支自带 0.08 余量,本身不抖)
|
|
1670
|
-
if dark > light + 0.08:
|
|
1671
|
-
want = "#f2f8ff"
|
|
1672
|
-
elif light > dark + 0.08:
|
|
1673
|
-
want = "#07111d"
|
|
1674
|
-
elif cur == "#07111d":
|
|
1675
|
-
want = "#f2f8ff" if p50 < 0.38 else cur
|
|
1676
|
-
else:
|
|
1677
|
-
# 还没定色时先给白字;之后要亮过 0.62 才敢翻深
|
|
1678
|
-
want = "#07111d" if (cur is not None and p50 > 0.62) else (cur or "#f2f8ff")
|
|
1679
|
-
if want == cur:
|
|
1680
|
-
self._text_pending.pop(item, None)
|
|
1681
|
-
continue
|
|
1682
|
-
if self._text_pending.get(item) != want:
|
|
1683
|
-
self._text_pending[item] = want # 第一次:只记下,不换
|
|
1684
|
-
continue
|
|
1685
|
-
self._text_pending.pop(item, None)
|
|
1686
|
-
self.canvas.itemconfigure(item, fill=want)
|
|
1687
|
-
self._text_colors[item] = want
|
|
1688
|
-
|
|
1689
|
-
def _adapt_aux(self, out):
|
|
1690
|
-
"""最上面细条随背景切换深浅(同样要滞回,理由见 `_adapt_text`)。"""
|
|
1691
|
-
now = time.time()
|
|
1692
|
-
if now - self._aux_at < 0.25:
|
|
1693
|
-
return
|
|
1694
|
-
self._aux_at = now
|
|
1695
|
-
mid = W // 2
|
|
1696
|
-
half = max(50, self._bar_len // 2)
|
|
1697
|
-
bar = np.asarray(out.crop(
|
|
1698
|
-
(mid - half, BAR_Y - 6, mid + half, BAR_Y + 12)).convert("L"))
|
|
1699
|
-
p50 = float(np.median(bar)) / 255.0
|
|
1700
|
-
cur = self._bar_color
|
|
1701
|
-
if cur == "#4e6278":
|
|
1702
|
-
# 已经很深了:要暗到 0.36 才翻浅,避免在阈值附近来回跳
|
|
1703
|
-
bar_fill = "#b8cbe0" if p50 < 0.36 else cur
|
|
1704
|
-
else:
|
|
1705
|
-
bar_fill = "#4e6278" if p50 > 0.60 else cur
|
|
1706
|
-
if bar_fill != cur:
|
|
1707
|
-
self._bar_color = bar_fill
|
|
1708
|
-
self.canvas.itemconfigure(self._bar, fill=bar_fill)
|
|
1709
|
-
|
|
1710
|
-
|
|
1711
|
-
|
|
1712
|
-
def _snapshot_mode(self):
|
|
1713
|
-
"""只给文档截图:停抓取、关 WDA,保留最后一帧(不改变正常路径)。"""
|
|
1714
|
-
self._snapshot_done = True
|
|
1715
|
-
self._pause.set()
|
|
1716
|
-
if self._excluded:
|
|
1717
|
-
self._set_exclude(False)
|
|
1718
|
-
|
|
1719
|
-
def _beat(self):
|
|
1720
|
-
"""写心跳(每秒最多一次)。插件侧 `windowAlive()` 读它的 mtime。
|
|
1721
|
-
|
|
1722
|
-
为什么不是"进程活着就算活着":pid 会被系统复用,进程也可能活着但界面卡死、
|
|
1723
|
-
或者是一张全透明的空窗。那种情况下宿主一直认为窗还开着,于是**再也不重开** ——
|
|
1724
|
-
用户体感就是"点了没反应"。
|
|
1725
|
-
"""
|
|
1726
|
-
now = time.time()
|
|
1727
|
-
if now - getattr(self, "_beat_at", 0.0) < HEARTBEAT_EVERY:
|
|
1728
|
-
return
|
|
1729
|
-
self._beat_at = now
|
|
1730
|
-
try:
|
|
1731
|
-
HEARTBEAT.parent.mkdir(parents=True, exist_ok=True)
|
|
1732
|
-
HEARTBEAT.write_bytes(b"")
|
|
1733
|
-
except Exception:
|
|
1734
|
-
pass
|
|
1735
|
-
|
|
1736
|
-
def _tick(self):
|
|
1737
|
-
self._beat()
|
|
1738
|
-
# 截图模式:worker 已暂停,保持 WDA 关闭;否则下一拍会重新开 WDA,
|
|
1739
|
-
# 窗口再次从截图/录屏里消失,_snapshot_mode 等于没做。
|
|
1740
|
-
if SNAPSHOT and getattr(self, "_snapshot_done", False):
|
|
1741
|
-
self._pause.set()
|
|
1742
|
-
if self._excluded:
|
|
1743
|
-
self._set_exclude(False)
|
|
1744
|
-
self.root.after(100, self._tick)
|
|
1745
|
-
return
|
|
1746
|
-
if PREVIEW:
|
|
1747
|
-
self._pause.set()
|
|
1748
|
-
if self._excluded:
|
|
1749
|
-
self._set_exclude(False)
|
|
1750
|
-
self.root.after(100, self._tick)
|
|
1751
|
-
return
|
|
1752
|
-
# 缩放拖拽期间由 _preview_resize 负责画面,避免旧帧覆盖预览
|
|
1753
|
-
if self._resize:
|
|
1754
|
-
self.root.after(INTERVAL_MS, self._tick)
|
|
1755
|
-
return
|
|
1756
|
-
if getattr(self, "_collapsed", False):
|
|
1757
|
-
# 折叠态用 _make_collapsed_frame 的静态背景;_work 也停抓取。
|
|
1758
|
-
# 若还让这里消费旧队列,会把 560px 的帧贴回 78px 的 Canvas,
|
|
1759
|
-
# 底部圆角/暗边会闪一下,看起来像渲染错位。
|
|
1760
|
-
self.root.after(INTERVAL_MS, self._tick)
|
|
1761
|
-
return
|
|
1762
|
-
if not self._excluded and not self._cappable:
|
|
1763
|
-
self._set_exclude(True)
|
|
1764
|
-
self._pause.clear()
|
|
1765
|
-
|
|
1766
|
-
try:
|
|
1767
|
-
out = self._q.get_nowait()
|
|
1768
|
-
except queue.Empty:
|
|
1769
|
-
out = None
|
|
1770
|
-
if out is not None:
|
|
1771
|
-
try:
|
|
1772
|
-
self._last_frame = out
|
|
1773
|
-
self._img = ImageTk.PhotoImage(out, master=self.canvas)
|
|
1774
|
-
self.canvas.itemconfigure(self._bg, image=self._img)
|
|
1775
|
-
self.canvas.tag_lower(self._bg)
|
|
1776
|
-
self._adapt_text(out)
|
|
1777
|
-
self._adapt_aux(out)
|
|
1778
|
-
except Exception as exc:
|
|
1779
|
-
print("draw error", exc, flush=True)
|
|
1780
|
-
|
|
1781
|
-
self.root.after(INTERVAL_MS, self._tick)
|
|
1782
|
-
|
|
1783
|
-
def _close(self, _e=None):
|
|
1784
|
-
try:
|
|
1785
|
-
self._save_geometry()
|
|
1786
|
-
if self._chat_after is not None:
|
|
1787
|
-
try:
|
|
1788
|
-
self.root.after_cancel(self._chat_after)
|
|
1789
|
-
except Exception:
|
|
1790
|
-
pass
|
|
1791
|
-
self._stop_mic()
|
|
1792
|
-
self._stop.set()
|
|
1793
|
-
self._pause.set()
|
|
1794
|
-
self._set_exclude(False)
|
|
1795
|
-
self._worker.join(timeout=0.3)
|
|
1796
|
-
ctypes.windll.winmm.timeEndPeriod(1)
|
|
1797
|
-
finally:
|
|
1798
|
-
self.root.destroy()
|
|
1799
|
-
return "break"
|
|
1800
|
-
|
|
1801
|
-
def _hide_window(self, _e=None):
|
|
1802
|
-
"""藏起来但继续收消息;插件写 hidden=false 后会重新出现。"""
|
|
1803
|
-
write_config({"hidden": True})
|
|
1804
|
-
self._hidden = True
|
|
1805
|
-
self.root.withdraw()
|
|
1806
|
-
return "break"
|
|
1807
|
-
|
|
1808
|
-
# 边缘缩放 / 窗口拖动
|
|
1809
|
-
def _edge_at(self, x, y):
|
|
1810
|
-
w, h = self.root.winfo_width(), self.root.winfo_height()
|
|
1811
|
-
# 圆角是透明的,鼠标落在最外侧时命中的是 DWM 非客户区;
|
|
1812
|
-
# 命中带放宽到 20px,用户不必精确到 1px 也能拖边缘/四角。
|
|
1813
|
-
edge, corner = 20, 32
|
|
1814
|
-
# 圆角区域是透明的,命中区要往内收够,才能点到可见的角
|
|
1815
|
-
if x <= corner and y <= corner:
|
|
1816
|
-
return "nw"
|
|
1817
|
-
if x >= w - corner and y <= corner:
|
|
1818
|
-
return "ne"
|
|
1819
|
-
if x <= corner and y >= h - corner:
|
|
1820
|
-
return "sw"
|
|
1821
|
-
if x >= w - corner and y >= h - corner:
|
|
1822
|
-
return "se"
|
|
1823
|
-
# 顶部中间只拖动,不缩放;四角仍然可同时调宽高
|
|
1824
|
-
if y <= edge:
|
|
1825
|
-
return None
|
|
1826
|
-
if x <= edge:
|
|
1827
|
-
return "w"
|
|
1828
|
-
if x >= w - edge:
|
|
1829
|
-
return "e"
|
|
1830
|
-
if y >= h - edge:
|
|
1831
|
-
return "s"
|
|
1832
|
-
return None
|
|
1833
|
-
|
|
1834
|
-
def _cursor_for(self, mode):
|
|
1835
|
-
return {
|
|
1836
|
-
"nw": "size_nw_se", "se": "size_nw_se",
|
|
1837
|
-
"ne": "size_ne_sw", "sw": "size_ne_sw",
|
|
1838
|
-
"w": "size_we", "e": "size_we",
|
|
1839
|
-
"n": "size_ns", "s": "size_ns",
|
|
1840
|
-
}.get(mode, "")
|
|
1841
|
-
|
|
1842
|
-
def _hover(self, e):
|
|
1843
|
-
if self._resize:
|
|
1844
|
-
return
|
|
1845
|
-
try:
|
|
1846
|
-
self.canvas.config(cursor=self._cursor_for(self._edge_at(e.x, e.y)))
|
|
1847
|
-
except Exception:
|
|
1848
|
-
pass
|
|
1849
|
-
|
|
1850
|
-
def _leave(self, _e):
|
|
1851
|
-
if not self._resize:
|
|
1852
|
-
self.canvas.config(cursor="")
|
|
1853
|
-
|
|
1854
|
-
def _press(self, e):
|
|
1855
|
-
# 麦克风图标是画在玻璃里的,不在 Canvas 上单独建控件;点它不能当拖窗
|
|
1856
|
-
if abs(e.x - (MIC_POS[0] + 14)) <= 22 and abs(e.y - (MIC_POS[1] + 14)) <= 22:
|
|
1857
|
-
self._toggle_mic()
|
|
1858
|
-
return
|
|
1859
|
-
x0, y0, x1, y1, _r = SHAPES[4]
|
|
1860
|
-
if x0 <= e.x <= x1 and y0 <= e.y <= y1:
|
|
1861
|
-
self._focus_input()
|
|
1862
|
-
return
|
|
1863
|
-
mode = self._edge_at(e.x, e.y)
|
|
1864
|
-
if mode:
|
|
1865
|
-
self._resize = (mode, e.x_root, e.y_root,
|
|
1866
|
-
self.root.winfo_x(), self.root.winfo_y(),
|
|
1867
|
-
self.root.winfo_width(), self.root.winfo_height())
|
|
1868
|
-
self._drag = None
|
|
1869
|
-
self._pause.set()
|
|
1870
|
-
return
|
|
1871
|
-
self._drag = (e.x_root - self.root.winfo_x(),
|
|
1872
|
-
e.y_root - self.root.winfo_y())
|
|
1873
|
-
|
|
1874
|
-
def _motion(self, e):
|
|
1875
|
-
if self._resize:
|
|
1876
|
-
mode, sx, sy, x0, y0, w0, h0 = self._resize
|
|
1877
|
-
dx, dy = e.x_root - sx, e.y_root - sy
|
|
1878
|
-
nw, nh, nx, ny = w0, h0, x0, y0
|
|
1879
|
-
if "e" in mode:
|
|
1880
|
-
nw = max(MIN_W, min(max(MIN_W, self._sw - x0), w0 + dx))
|
|
1881
|
-
if "w" in mode:
|
|
1882
|
-
nw = max(MIN_W, min(max(MIN_W, x0 + w0), w0 - dx))
|
|
1883
|
-
nx = x0 + w0 - nw
|
|
1884
|
-
if "s" in mode:
|
|
1885
|
-
nh = max(MIN_H, min(max(MIN_H, self._sh - y0), h0 + dy))
|
|
1886
|
-
if "n" in mode:
|
|
1887
|
-
nh = max(MIN_H, min(max(MIN_H, y0 + h0), h0 - dy))
|
|
1888
|
-
ny = y0 + h0 - nh
|
|
1889
|
-
nx = max(0, min(nx, max(0, self._sw - nw)))
|
|
1890
|
-
ny = max(0, min(ny, max(0, self._sh - nh)))
|
|
1891
|
-
self.root.geometry(f"{nw}x{nh}+{nx}+{ny}")
|
|
1892
|
-
now = time.time()
|
|
1893
|
-
if now - self._preview_at >= 0.04:
|
|
1894
|
-
self._preview_at = now
|
|
1895
|
-
self._preview_resize(nw, nh)
|
|
1896
|
-
return
|
|
1897
|
-
if self._drag:
|
|
1898
|
-
# 和缩放分支一样必须钳位:拖到屏幕外时窗口坐标会变负,
|
|
1899
|
-
# 抓屏区域随之越界(高度算出负数),背景永远画不出来 —— 表现为花屏。
|
|
1900
|
-
nx = e.x_root - self._drag[0]
|
|
1901
|
-
ny = e.y_root - self._drag[1]
|
|
1902
|
-
nx = max(0, min(max(0, self._sw - self.root.winfo_width()), nx))
|
|
1903
|
-
ny = max(0, min(max(0, self._sh - self.root.winfo_height()), ny))
|
|
1904
|
-
self.root.geometry(f"+{nx}+{ny}")
|
|
1905
|
-
|
|
1906
|
-
def _release(self, e):
|
|
1907
|
-
if self._resize:
|
|
1908
|
-
self._resize = None
|
|
1909
|
-
self._preview_at = 0.0
|
|
1910
|
-
self.root.update_idletasks()
|
|
1911
|
-
w = max(MIN_W, self.root.winfo_width())
|
|
1912
|
-
h = max(MIN_H, self.root.winfo_height())
|
|
1913
|
-
self._rebuild(w, h)
|
|
1914
|
-
self._sync_pos()
|
|
1915
|
-
self._hover(e)
|
|
1916
|
-
return
|
|
1917
|
-
self._drag = None
|
|
1918
|
-
self._sync_pos()
|
|
1919
|
-
|
|
1920
|
-
def run(self):
|
|
1921
|
-
self.root.bind("<Escape>", self._close)
|
|
1922
|
-
self.root.bind("<MouseWheel>", self._wheel)
|
|
1923
|
-
self.root.bind("<Motion>", self._hover, add="+")
|
|
1924
|
-
self.root.bind("<Leave>", self._leave, add="+")
|
|
1925
|
-
self.root.bind("<ButtonPress-1>", self._press, add="+")
|
|
1926
|
-
self.root.bind("<B1-Motion>", self._motion, add="+")
|
|
1927
|
-
self.root.bind("<ButtonRelease-1>", self._release, add="+")
|
|
1928
|
-
self.root.mainloop()
|
|
1929
|
-
|
|
1930
|
-
|
|
1931
|
-
if __name__ == "__main__":
|
|
1932
|
-
LiveGlass().run()
|
|
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
|
+
- 这不是 DWM Acrylic;Acrylic 需要系统「透明效果」开启,这里是软件实时模糊。
|
|
19
|
+
|
|
20
|
+
渲染后端:默认 CPU。`DSH_GLASS_NATIVE=1` 或 `DSH_GLASS_GPU=1` 走 GPU。
|
|
21
|
+
"""
|
|
22
|
+
import ctypes
|
|
23
|
+
import json
|
|
24
|
+
import os
|
|
25
|
+
import queue
|
|
26
|
+
import subprocess
|
|
27
|
+
import threading
|
|
28
|
+
import time
|
|
29
|
+
from ctypes import wintypes
|
|
30
|
+
from pathlib import Path
|
|
31
|
+
|
|
32
|
+
# 必须在 import tkinter / 创建任何窗口之前声明 per-monitor v2 DPI 感知。
|
|
33
|
+
_u = ctypes.windll.user32
|
|
34
|
+
try:
|
|
35
|
+
_u.SetProcessDpiAwarenessContext.argtypes = [ctypes.c_void_p]
|
|
36
|
+
_u.SetProcessDpiAwarenessContext.restype = ctypes.c_bool
|
|
37
|
+
_u.SetProcessDpiAwarenessContext(ctypes.c_void_p(-4))
|
|
38
|
+
except Exception:
|
|
39
|
+
pass
|
|
40
|
+
|
|
41
|
+
try:
|
|
42
|
+
ctypes.windll.winmm.timeBeginPeriod(1)
|
|
43
|
+
except Exception:
|
|
44
|
+
pass
|
|
45
|
+
|
|
46
|
+
import tkinter as tk
|
|
47
|
+
|
|
48
|
+
import mss
|
|
49
|
+
import numpy as np
|
|
50
|
+
from PIL import Image, ImageTk
|
|
51
|
+
|
|
52
|
+
from glass_capture import CaptureSource, MotionTracker
|
|
53
|
+
from glass_cpu import CpuRenderer
|
|
54
|
+
from glass_geometry import (
|
|
55
|
+
BAR_FRAC, BAR_THICK, BAR_Y, DEFAULT_H, DEFAULT_W, KEY, MESSAGE_SLOTS,
|
|
56
|
+
MIC_SIZE, MIN_H, MIN_W, Geometry, collapsed_height, shapes_for,
|
|
57
|
+
)
|
|
58
|
+
from window_widgets import ChatFeed, Fonts, TextLayer
|
|
59
|
+
|
|
60
|
+
try: # 悬浮窗接口:插件写 window-events.jsonl,窗口回 window-outbox.jsonl
|
|
61
|
+
from window_protocol import WindowProtocol
|
|
62
|
+
except Exception:
|
|
63
|
+
WindowProtocol = None
|
|
64
|
+
|
|
65
|
+
INTERVAL_MS = 4
|
|
66
|
+
MAX_FPS = 120
|
|
67
|
+
# 静止画面下「有没有变」只能靠抓屏看,单次 grab ~7ms;空转按 60fps 就是 40% 单核。
|
|
68
|
+
# 降到 30fps 探针,有变化时 idle 立刻归零、下一拍恢复全速。
|
|
69
|
+
PROBE_FPS = 30
|
|
70
|
+
|
|
71
|
+
# 仅用于文档截图/CI 视觉检查:暂停抓屏并把 WDA 关掉,窗口才会出现在截图里
|
|
72
|
+
PREVIEW = os.environ.get("DSH_GLASS_PREVIEW") == "1"
|
|
73
|
+
# 文档截图:正常抓屏 2.5 秒后暂停并取消 WDA,保留最后一帧玻璃
|
|
74
|
+
SNAPSHOT = os.environ.get("DSH_GLASS_SNAPSHOT") == "1"
|
|
75
|
+
# GPU:native 表示全分辨率采样(边缘透镜没有 1/4 放大的像素感)
|
|
76
|
+
NATIVE = os.environ.get("DSH_GLASS_NATIVE") == "1"
|
|
77
|
+
USE_GPU = NATIVE or os.environ.get("DSH_GLASS_GPU") == "1"
|
|
78
|
+
|
|
79
|
+
HERE = Path(__file__).resolve().parent
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
class _RECT(ctypes.Structure):
|
|
83
|
+
_fields_ = [("left", ctypes.c_long), ("top", ctypes.c_long),
|
|
84
|
+
("right", ctypes.c_long), ("bottom", ctypes.c_long)]
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
# ---------------------------------------------------------------- 路径与配置
|
|
88
|
+
|
|
89
|
+
def cache_dir() -> Path:
|
|
90
|
+
d = os.environ.get("DSH_VIDEO_CACHE_DIR")
|
|
91
|
+
if d:
|
|
92
|
+
return Path(d)
|
|
93
|
+
local = os.environ.get("LOCALAPPDATA")
|
|
94
|
+
if os.name == "nt" and local:
|
|
95
|
+
return Path(local) / "dsh-video-storyboard" / "cache"
|
|
96
|
+
return Path(os.environ.get("XDG_CACHE_HOME") or "~/.cache").expanduser() / "dsh-video-storyboard"
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
CHAT_FILE = cache_dir() / "chat.jsonl"
|
|
100
|
+
CONFIG = Path(os.environ.get("DSH_HOME") or os.path.expanduser("~/.dsh")) / "watch-window.json"
|
|
101
|
+
MIC_SCRIPT = HERE / "mic.py"
|
|
102
|
+
|
|
103
|
+
# **心跳**:插件侧靠它区分「进程还在」和「窗口真的在画」。只看 pid 不够 ——
|
|
104
|
+
# pid 会被复用,进程也可能活着但界面已经卡死,那样宿主会永远认为窗还开着,
|
|
105
|
+
# 于是再也不重开(see-now issue #2 第 3 条)。每秒最多写一次。
|
|
106
|
+
HEARTBEAT = cache_dir() / "window-heartbeat"
|
|
107
|
+
HEARTBEAT_EVERY = 1.0
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def read_config() -> dict:
|
|
111
|
+
try:
|
|
112
|
+
return json.loads(CONFIG.read_text(encoding="utf-8")) or {}
|
|
113
|
+
except Exception:
|
|
114
|
+
return {}
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def write_config(patch: dict) -> None:
|
|
118
|
+
cfg = read_config()
|
|
119
|
+
cfg.update(patch or {})
|
|
120
|
+
try:
|
|
121
|
+
CONFIG.parent.mkdir(parents=True, exist_ok=True)
|
|
122
|
+
tmp = CONFIG.with_suffix(".json.tmp")
|
|
123
|
+
tmp.write_text(json.dumps(cfg, ensure_ascii=False, indent=2), encoding="utf-8")
|
|
124
|
+
os.replace(tmp, CONFIG)
|
|
125
|
+
except Exception:
|
|
126
|
+
pass
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
def capturable() -> bool:
|
|
130
|
+
"""录屏 / 截图能不能拍到窗口。
|
|
131
|
+
|
|
132
|
+
默认**不能**:走 `WDA_EXCLUDEFROMCAPTURE`,窗口不会出现在截图、直播、共享屏幕里。
|
|
133
|
+
要给别人看效果就在 `~/.dsh/watch-window.json` 写 `{ "capturable": true }`。
|
|
134
|
+
环境变量 `DSH_GLASS_WDA=0` 只对这一次启动有效(插件拉起窗口时不传环境变量)。
|
|
135
|
+
"""
|
|
136
|
+
if os.environ.get("DSH_GLASS_WDA") == "0":
|
|
137
|
+
return True
|
|
138
|
+
try:
|
|
139
|
+
return read_config().get("capturable") is True
|
|
140
|
+
except Exception:
|
|
141
|
+
return False
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
def append_user(text: str) -> None:
|
|
145
|
+
CHAT_FILE.parent.mkdir(parents=True, exist_ok=True)
|
|
146
|
+
with open(CHAT_FILE, "a", encoding="utf-8") as f:
|
|
147
|
+
f.write(json.dumps({"role": "user", "text": text, "at": int(time.time() * 1000)},
|
|
148
|
+
ensure_ascii=False) + "\n")
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
def asr_python() -> str:
|
|
152
|
+
"""麦克风用 ASR 的 Python,不是带 tkinter 的这个。"""
|
|
153
|
+
for key in ("DSH_ASR_PYTHON", "DSH_FLOAT_PYTHON"):
|
|
154
|
+
v = os.environ.get(key)
|
|
155
|
+
if v:
|
|
156
|
+
return v
|
|
157
|
+
return "python.exe" if os.name == "nt" else "python3"
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
def display_hz() -> int:
|
|
161
|
+
try:
|
|
162
|
+
u = ctypes.windll.user32
|
|
163
|
+
g = ctypes.windll.gdi32
|
|
164
|
+
dc = u.GetDC(0)
|
|
165
|
+
hz = int(g.GetDeviceCaps(dc, 116))
|
|
166
|
+
u.ReleaseDC(0, dc)
|
|
167
|
+
return hz if hz > 0 else 60
|
|
168
|
+
except Exception:
|
|
169
|
+
return 60
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
# ---------------------------------------------------------------- 窗口
|
|
173
|
+
|
|
174
|
+
class LiveGlass:
|
|
175
|
+
def __init__(self):
|
|
176
|
+
cfg = read_config()
|
|
177
|
+
raw_name = cfg.get("name")
|
|
178
|
+
self.name = (raw_name.strip()[:24] if isinstance(raw_name, str) and raw_name.strip()
|
|
179
|
+
else "陪看")
|
|
180
|
+
|
|
181
|
+
self.feed = ChatFeed(CHAT_FILE)
|
|
182
|
+
self._mic_proc = None
|
|
183
|
+
self._protocol = WindowProtocol() if WindowProtocol is not None else None
|
|
184
|
+
self._collapsed = False
|
|
185
|
+
self._saved_h = DEFAULT_H
|
|
186
|
+
self._snapshot_done = False
|
|
187
|
+
self._title_entry = None
|
|
188
|
+
self._title_item = None
|
|
189
|
+
self._editing = False
|
|
190
|
+
self._hidden = False
|
|
191
|
+
self._cfg_key = None
|
|
192
|
+
|
|
193
|
+
self.root = tk.Tk()
|
|
194
|
+
self.root.withdraw()
|
|
195
|
+
self.root.overrideredirect(True)
|
|
196
|
+
self.root.title(self.name)
|
|
197
|
+
self.root.attributes("-topmost", True)
|
|
198
|
+
self.root.configure(bg=KEY)
|
|
199
|
+
self.root.attributes("-transparentcolor", KEY)
|
|
200
|
+
self.fonts = Fonts(self.root)
|
|
201
|
+
|
|
202
|
+
self._sw, self._sh = self.root.winfo_screenwidth(), self.root.winfo_screenheight()
|
|
203
|
+
w, h, x, y = self._initial_geometry(cfg.get("geometry"))
|
|
204
|
+
self._x, self._y = x, y
|
|
205
|
+
self._geo = Geometry.for_size(w, h)
|
|
206
|
+
# 入场:从下方 22px 滑上来(不碰 -alpha,避免和抠色冲突)
|
|
207
|
+
self._intro_target_y = y
|
|
208
|
+
self._intro_y = y + 22
|
|
209
|
+
self.root.geometry(f"{w}x{h}+{x}+{self._intro_y}")
|
|
210
|
+
|
|
211
|
+
self.canvas = tk.Canvas(self.root, width=w, height=h, bg=KEY,
|
|
212
|
+
highlightthickness=0, bd=0)
|
|
213
|
+
self.canvas.pack(fill="both", expand=True)
|
|
214
|
+
self.text = TextLayer(self.root, self.canvas, self.fonts)
|
|
215
|
+
|
|
216
|
+
self._img = None
|
|
217
|
+
self._last_frame = None
|
|
218
|
+
self._collapsed_frame = None
|
|
219
|
+
self._bg = None
|
|
220
|
+
self._entry = None
|
|
221
|
+
self._entry_item = None
|
|
222
|
+
self._bar = None
|
|
223
|
+
self._bar_len = 0
|
|
224
|
+
self._bar_color = "#b8cbe0"
|
|
225
|
+
self._aux_at = 0.0
|
|
226
|
+
|
|
227
|
+
self._hwnd = None
|
|
228
|
+
self._excluded = False
|
|
229
|
+
# 录屏模式(capturable)下 mss 会抓到窗口自己,必须每帧挖掉,见 fill_own_rect。
|
|
230
|
+
# 缓存起来:读配置是磁盘 IO,不能每帧做。
|
|
231
|
+
self._cappable = capturable()
|
|
232
|
+
self._drag = None
|
|
233
|
+
self._resize = None
|
|
234
|
+
self._preview_at = 0.0
|
|
235
|
+
|
|
236
|
+
self._q: queue.Queue = queue.Queue(maxsize=1)
|
|
237
|
+
self._stop = threading.Event()
|
|
238
|
+
self._pause = threading.Event()
|
|
239
|
+
self._geo_lock = threading.Lock()
|
|
240
|
+
self._source = CaptureSource((self._sw, self._sh), cappable=self._cappable)
|
|
241
|
+
self._renderer = self._make_renderer(self._geo)
|
|
242
|
+
self._worker = threading.Thread(target=self._work, daemon=True)
|
|
243
|
+
|
|
244
|
+
self._hz = display_hz()
|
|
245
|
+
self._target_fps = min(MAX_FPS, self._hz)
|
|
246
|
+
self._ema = 1.0 / max(1, self._target_fps)
|
|
247
|
+
self._last_motion = 0.0
|
|
248
|
+
|
|
249
|
+
self.feed.reload()
|
|
250
|
+
self._layout()
|
|
251
|
+
|
|
252
|
+
self.root.deiconify()
|
|
253
|
+
self.root.update_idletasks()
|
|
254
|
+
self.text.refresh()
|
|
255
|
+
self._set_exclude(not capturable())
|
|
256
|
+
self._worker.start()
|
|
257
|
+
self.root.lift()
|
|
258
|
+
self.root.after(80, self.root.lift)
|
|
259
|
+
self.root.after(120, self.root.focus_set)
|
|
260
|
+
self.root.after(60, self._tick)
|
|
261
|
+
self.root.after(30, lambda: self._intro_step(0))
|
|
262
|
+
self.root.after(250, self._poll_chat)
|
|
263
|
+
self.root.after(1200, self._keep_on_top)
|
|
264
|
+
if SNAPSHOT:
|
|
265
|
+
self.root.after(2600, self._snapshot_mode)
|
|
266
|
+
|
|
267
|
+
# ---- 渲染后端 ----
|
|
268
|
+
|
|
269
|
+
def _make_renderer(self, geo: Geometry):
|
|
270
|
+
if USE_GPU:
|
|
271
|
+
try:
|
|
272
|
+
from glass_gpu import GpuRenderer
|
|
273
|
+
return GpuRenderer(geo, MotionTracker(), native=NATIVE)
|
|
274
|
+
except Exception as exc:
|
|
275
|
+
print("GPU 初始化失败,回落到 CPU:", repr(exc), flush=True)
|
|
276
|
+
return CpuRenderer(geo)
|
|
277
|
+
|
|
278
|
+
# ---- 尺寸 ----
|
|
279
|
+
|
|
280
|
+
def _initial_geometry(self, geo):
|
|
281
|
+
sw, sh = self._sw, self._sh
|
|
282
|
+
w, h = DEFAULT_W, DEFAULT_H
|
|
283
|
+
x, y = max(40, sw - w - 80), max(40, (sh - h) // 2 - 40)
|
|
284
|
+
try:
|
|
285
|
+
size, rest = geo.split("+", 1)
|
|
286
|
+
pw, ph = (int(v) for v in size.split("x"))
|
|
287
|
+
px, py = (int(v) for v in rest.split("+")[:2])
|
|
288
|
+
w = max(MIN_W, min(sw - 40, pw))
|
|
289
|
+
h = max(MIN_H, min(sh - 40, ph))
|
|
290
|
+
x = max(0, min(sw - w, px))
|
|
291
|
+
y = max(0, min(sh - h, py))
|
|
292
|
+
except Exception:
|
|
293
|
+
pass
|
|
294
|
+
return w, h, x, y
|
|
295
|
+
|
|
296
|
+
def _set_geometry(self, geo: Geometry) -> None:
|
|
297
|
+
"""换一套几何:渲染器跟着换,静态跳帧的签名作废。"""
|
|
298
|
+
self._geo = geo
|
|
299
|
+
self._renderer.set_geometry(geo)
|
|
300
|
+
self.canvas.config(width=geo.width, height=geo.height)
|
|
301
|
+
|
|
302
|
+
def _apply_shapes(self, shapes) -> None:
|
|
303
|
+
"""气泡宽度/高度变了:尺寸相关的遮罩/折射图直接复用,只重算框。"""
|
|
304
|
+
with self._geo_lock:
|
|
305
|
+
self._set_geometry(self._geo.with_shapes(shapes))
|
|
306
|
+
|
|
307
|
+
# ---- 窗口外观 ----
|
|
308
|
+
|
|
309
|
+
def _intro_step(self, n: int) -> None:
|
|
310
|
+
try:
|
|
311
|
+
if n >= 6:
|
|
312
|
+
self._y = self._intro_target_y
|
|
313
|
+
else:
|
|
314
|
+
self._y = self._intro_target_y + int(22 * (1 - (n + 1) / 6.0))
|
|
315
|
+
self.root.geometry(f"{self._geo.width}x{self._geo.height}+{self._x}+{self._y}")
|
|
316
|
+
except Exception:
|
|
317
|
+
pass
|
|
318
|
+
if n < 6:
|
|
319
|
+
self.root.after(26, lambda: self._intro_step(n + 1))
|
|
320
|
+
|
|
321
|
+
def _keep_on_top(self) -> None:
|
|
322
|
+
"""DSH 等置顶窗口被激活后会把悬浮窗压下去;定时重新抬到最上层。"""
|
|
323
|
+
try:
|
|
324
|
+
if not self._hidden:
|
|
325
|
+
self.root.attributes("-topmost", True)
|
|
326
|
+
self.root.lift()
|
|
327
|
+
except Exception:
|
|
328
|
+
pass
|
|
329
|
+
try:
|
|
330
|
+
self.root.after(1800, self._keep_on_top)
|
|
331
|
+
except Exception:
|
|
332
|
+
pass
|
|
333
|
+
|
|
334
|
+
# ---- 协议 ----
|
|
335
|
+
|
|
336
|
+
def _render_event(self, event) -> None:
|
|
337
|
+
if not isinstance(event, dict):
|
|
338
|
+
return
|
|
339
|
+
if event.get("type") in ("message", "meta"):
|
|
340
|
+
self.feed.add(ChatFeed.parse(json.dumps(event, ensure_ascii=False)))
|
|
341
|
+
self._refresh_feed()
|
|
342
|
+
elif event.get("type") == "control":
|
|
343
|
+
self._apply_control(event)
|
|
344
|
+
|
|
345
|
+
def _apply_control(self, event) -> None:
|
|
346
|
+
action = str(event.get("action") or "")
|
|
347
|
+
value = event.get("value")
|
|
348
|
+
try:
|
|
349
|
+
if action == "rename" and str(value or "").strip():
|
|
350
|
+
self.name = str(value).strip()[:24]
|
|
351
|
+
self.root.title(self.name)
|
|
352
|
+
write_config({"name": self.name})
|
|
353
|
+
self._refresh_feed()
|
|
354
|
+
elif action == "collapse" and not self._collapsed:
|
|
355
|
+
self.toggle_collapse()
|
|
356
|
+
elif action == "expand" and self._collapsed:
|
|
357
|
+
self.toggle_collapse()
|
|
358
|
+
elif action == "hide":
|
|
359
|
+
self._hidden = True
|
|
360
|
+
write_config({"hidden": True})
|
|
361
|
+
self.root.withdraw()
|
|
362
|
+
elif action == "show":
|
|
363
|
+
self._hidden = False
|
|
364
|
+
write_config({"hidden": False})
|
|
365
|
+
self.root.deiconify()
|
|
366
|
+
self.root.attributes("-topmost", True)
|
|
367
|
+
except Exception:
|
|
368
|
+
pass
|
|
369
|
+
|
|
370
|
+
def _drain_protocol(self) -> list:
|
|
371
|
+
if self._protocol is None:
|
|
372
|
+
return []
|
|
373
|
+
try:
|
|
374
|
+
return self._protocol.read_events()
|
|
375
|
+
except Exception:
|
|
376
|
+
return []
|
|
377
|
+
|
|
378
|
+
def _replay_protocol(self) -> None:
|
|
379
|
+
if self._protocol is None:
|
|
380
|
+
return
|
|
381
|
+
try:
|
|
382
|
+
for event in self._protocol.reset_events(60):
|
|
383
|
+
self._render_event(event)
|
|
384
|
+
except Exception:
|
|
385
|
+
pass
|
|
386
|
+
|
|
387
|
+
# ---- 聊天轮询 ----
|
|
388
|
+
|
|
389
|
+
def _poll_chat(self) -> None:
|
|
390
|
+
try:
|
|
391
|
+
self._sync_visible()
|
|
392
|
+
for event in self._drain_protocol():
|
|
393
|
+
self._render_event(event)
|
|
394
|
+
if self._mic_proc is not None and self._mic_proc.poll() is not None:
|
|
395
|
+
self._mic_proc = None
|
|
396
|
+
self.feed.status = ""
|
|
397
|
+
self._refresh_feed()
|
|
398
|
+
if self.feed.poll():
|
|
399
|
+
self._refresh_feed()
|
|
400
|
+
except Exception as exc:
|
|
401
|
+
print("chat poll error", exc, flush=True)
|
|
402
|
+
if not self._stop.is_set():
|
|
403
|
+
self.root.after(250, self._poll_chat)
|
|
404
|
+
|
|
405
|
+
def _sync_visible(self) -> None:
|
|
406
|
+
try:
|
|
407
|
+
st = CONFIG.stat()
|
|
408
|
+
key = (st.st_mtime_ns, st.st_size)
|
|
409
|
+
except OSError:
|
|
410
|
+
key = None
|
|
411
|
+
if key == self._cfg_key:
|
|
412
|
+
return
|
|
413
|
+
self._cfg_key = key
|
|
414
|
+
want = bool(read_config().get("hidden"))
|
|
415
|
+
if want == self._hidden:
|
|
416
|
+
return
|
|
417
|
+
self._hidden = want
|
|
418
|
+
try:
|
|
419
|
+
if want:
|
|
420
|
+
self.root.withdraw()
|
|
421
|
+
else:
|
|
422
|
+
self.root.deiconify()
|
|
423
|
+
self.root.attributes("-topmost", True)
|
|
424
|
+
self.root.lift()
|
|
425
|
+
except Exception:
|
|
426
|
+
self._hidden = not want
|
|
427
|
+
|
|
428
|
+
# ---- 输入 ----
|
|
429
|
+
|
|
430
|
+
def _send(self, _event=None):
|
|
431
|
+
if self._entry is None:
|
|
432
|
+
return "break"
|
|
433
|
+
text = self._entry.get().strip()
|
|
434
|
+
if not text:
|
|
435
|
+
return "break"
|
|
436
|
+
try:
|
|
437
|
+
append_user(text)
|
|
438
|
+
except Exception as exc:
|
|
439
|
+
self.feed.status = f"写不进去:{exc}"
|
|
440
|
+
self._refresh_feed()
|
|
441
|
+
return "break"
|
|
442
|
+
try:
|
|
443
|
+
self._entry.delete(0, "end")
|
|
444
|
+
except Exception:
|
|
445
|
+
pass
|
|
446
|
+
try:
|
|
447
|
+
if self._protocol is not None:
|
|
448
|
+
self._protocol.action("say", text=text)
|
|
449
|
+
except Exception:
|
|
450
|
+
pass
|
|
451
|
+
self._render_input()
|
|
452
|
+
self.root.after(40, self._poll_chat)
|
|
453
|
+
return "break"
|
|
454
|
+
|
|
455
|
+
def _wheel(self, e):
|
|
456
|
+
if self.feed.scroll(1 if e.delta > 0 else -1):
|
|
457
|
+
self._refresh_feed()
|
|
458
|
+
|
|
459
|
+
def _toggle_mic(self):
|
|
460
|
+
if self._mic_proc is not None and self._mic_proc.poll() is None:
|
|
461
|
+
self._stop_mic()
|
|
462
|
+
return
|
|
463
|
+
if not MIC_SCRIPT.exists():
|
|
464
|
+
self.feed.status = "找不到 mic.py"
|
|
465
|
+
self._refresh_feed()
|
|
466
|
+
return
|
|
467
|
+
try:
|
|
468
|
+
self._mic_proc = subprocess.Popen(
|
|
469
|
+
[asr_python(), str(MIC_SCRIPT), "--engine", "local"],
|
|
470
|
+
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
|
|
471
|
+
creationflags=0x08000000 if os.name == "nt" else 0,
|
|
472
|
+
)
|
|
473
|
+
self.feed.status = "录音中"
|
|
474
|
+
except Exception as exc:
|
|
475
|
+
self.feed.status = f"麦起不来:{exc}"
|
|
476
|
+
self._refresh_feed()
|
|
477
|
+
|
|
478
|
+
def _stop_mic(self):
|
|
479
|
+
p = self._mic_proc
|
|
480
|
+
self._mic_proc = None
|
|
481
|
+
self.feed.status = ""
|
|
482
|
+
if p is not None and p.poll() is None:
|
|
483
|
+
try:
|
|
484
|
+
p.terminate()
|
|
485
|
+
except Exception:
|
|
486
|
+
pass
|
|
487
|
+
self._refresh_feed()
|
|
488
|
+
|
|
489
|
+
def _render_input(self) -> None:
|
|
490
|
+
"""输入文字画在 Canvas 上;Entry 只负责 IME/键盘,永远不可见。"""
|
|
491
|
+
if self._entry is None:
|
|
492
|
+
return
|
|
493
|
+
c = self.canvas
|
|
494
|
+
c.delete("inputtext", "inputcursor")
|
|
495
|
+
try:
|
|
496
|
+
text = self._entry.get()
|
|
497
|
+
focused = self.root.focus_get() is self._entry
|
|
498
|
+
except Exception:
|
|
499
|
+
text, focused = "", False
|
|
500
|
+
x0, y0, x1, y1, _r = self._geo.input_shape
|
|
501
|
+
x, y = x0 + 84, y0 + 27
|
|
502
|
+
maxw = max(60, x1 - x0 - 100)
|
|
503
|
+
if not text and not focused:
|
|
504
|
+
self.text.draw(x, y, "在这里跟它说话…", anchor="w", fill="#8e9db2",
|
|
505
|
+
size=11, tags=("inputtext", "inputzone"))
|
|
506
|
+
elif text:
|
|
507
|
+
font = self.fonts.get(11)
|
|
508
|
+
while text and font.measure(text) > maxw:
|
|
509
|
+
text = text[1:]
|
|
510
|
+
self.text.draw(x, y, text, anchor="w", fill="#eef6ff",
|
|
511
|
+
size=11, tags=("inputtext", "inputzone"))
|
|
512
|
+
if focused:
|
|
513
|
+
cx = x + font.measure(text)
|
|
514
|
+
c.create_line(cx + 1, y - 9, cx + 1, y + 9,
|
|
515
|
+
fill="#b9d7ff", width=1, tags=("inputcursor",))
|
|
516
|
+
elif focused:
|
|
517
|
+
c.create_line(x + 1, y - 9, x + 1, y + 9,
|
|
518
|
+
fill="#b9d7ff", width=1, tags=("inputcursor",))
|
|
519
|
+
self.text.refresh()
|
|
520
|
+
|
|
521
|
+
def _on_input_key(self, _e=None):
|
|
522
|
+
self._render_input()
|
|
523
|
+
return None
|
|
524
|
+
|
|
525
|
+
def _focus_input(self, _e=None):
|
|
526
|
+
try:
|
|
527
|
+
self._entry.focus_force()
|
|
528
|
+
self._entry.icursor("end")
|
|
529
|
+
except Exception:
|
|
530
|
+
pass
|
|
531
|
+
self._render_input()
|
|
532
|
+
|
|
533
|
+
# ---- 改名 ----
|
|
534
|
+
|
|
535
|
+
def _edit_title(self, _event=None):
|
|
536
|
+
if self._editing:
|
|
537
|
+
return "break"
|
|
538
|
+
self._editing = True
|
|
539
|
+
if self._title_entry is None:
|
|
540
|
+
self._title_entry = tk.Entry(self.root, font=self.fonts.spec(15), relief="flat",
|
|
541
|
+
bd=0, bg="#1b2330", fg="#f7fbff",
|
|
542
|
+
insertbackground="#f7fbff")
|
|
543
|
+
self._title_entry.bind("<Return>", self._commit_title)
|
|
544
|
+
self._title_entry.bind("<Escape>", self._cancel_title)
|
|
545
|
+
self._title_entry.bind("<FocusOut>", self._commit_title)
|
|
546
|
+
self._title_entry.delete(0, "end")
|
|
547
|
+
self._title_entry.insert(0, self.name)
|
|
548
|
+
if self._title_item is not None:
|
|
549
|
+
self.canvas.delete(self._title_item)
|
|
550
|
+
self._title_item = self.canvas.create_window(68, 38, anchor="w",
|
|
551
|
+
window=self._title_entry, width=180)
|
|
552
|
+
self._title_entry.focus_set()
|
|
553
|
+
self._title_entry.select_range(0, "end")
|
|
554
|
+
return "break"
|
|
555
|
+
|
|
556
|
+
def _commit_title(self, _event=None):
|
|
557
|
+
if not self._editing:
|
|
558
|
+
return
|
|
559
|
+
self._editing = False
|
|
560
|
+
text = self._title_entry.get().strip()
|
|
561
|
+
if self._title_item is not None:
|
|
562
|
+
self.canvas.delete(self._title_item)
|
|
563
|
+
self._title_item = None
|
|
564
|
+
if text and text != self.name:
|
|
565
|
+
self.name = text[:24]
|
|
566
|
+
self.root.title(self.name)
|
|
567
|
+
write_config({"name": self.name})
|
|
568
|
+
try:
|
|
569
|
+
if self._protocol is not None:
|
|
570
|
+
self._protocol.action("rename", value=self.name)
|
|
571
|
+
except Exception:
|
|
572
|
+
pass
|
|
573
|
+
self._refresh_feed()
|
|
574
|
+
return "break"
|
|
575
|
+
|
|
576
|
+
def _cancel_title(self):
|
|
577
|
+
self._editing = False
|
|
578
|
+
if self._title_item is not None:
|
|
579
|
+
self.canvas.delete(self._title_item)
|
|
580
|
+
self._title_item = None
|
|
581
|
+
self._refresh_feed()
|
|
582
|
+
return "break"
|
|
583
|
+
|
|
584
|
+
# ---- 折叠 ----
|
|
585
|
+
|
|
586
|
+
def toggle_collapse(self, _event=None):
|
|
587
|
+
# 先读真实坐标再切高度:_x/_y 只是启动时的缓存,拖动/缩放后已过期。
|
|
588
|
+
self._sync_pos()
|
|
589
|
+
self._collapsed = not self._collapsed
|
|
590
|
+
if self._collapsed:
|
|
591
|
+
self._saved_h = self._geo.height
|
|
592
|
+
# 先换成折叠尺寸的几何,再按它裁上一帧,否则圆角遮罩还是整窗高度
|
|
593
|
+
self._set_geometry(Geometry.for_size(self._geo.width, collapsed_height()))
|
|
594
|
+
self._collapsed_frame = self._geo.collapsed_frame(self._last_frame)
|
|
595
|
+
self.root.geometry(f"{self._geo.width}x{self._geo.height}+{self._x}+{self._y}")
|
|
596
|
+
self._layout()
|
|
597
|
+
else:
|
|
598
|
+
self._collapsed_frame = None
|
|
599
|
+
self._set_geometry(Geometry.for_size(self._geo.width, self._saved_h))
|
|
600
|
+
self.root.geometry(f"{self._geo.width}x{self._geo.height}+{self._x}+{self._y}")
|
|
601
|
+
self._layout(seed=True)
|
|
602
|
+
try:
|
|
603
|
+
if self._protocol is not None:
|
|
604
|
+
self._protocol.action("collapse" if self._collapsed else "expand")
|
|
605
|
+
except Exception:
|
|
606
|
+
pass
|
|
607
|
+
|
|
608
|
+
# ---- 气泡布局 ----
|
|
609
|
+
|
|
610
|
+
def _fit_lines(self, text, size, max_width, max_height) -> list[str]:
|
|
611
|
+
"""按框宽/框高折行并截断,避免文字溢出气泡。"""
|
|
612
|
+
font = self.fonts.get(size)
|
|
613
|
+
measure = font.measure
|
|
614
|
+
ls = max(1, font.metrics("linespace"))
|
|
615
|
+
max_lines = max(1, int(max_height // ls))
|
|
616
|
+
no_start = ",。、;:!?)》」』…,.;:!?)]}"
|
|
617
|
+
lines = []
|
|
618
|
+
for para in str(text).splitlines() or [""]:
|
|
619
|
+
cur = ""
|
|
620
|
+
for ch in para:
|
|
621
|
+
if not cur or measure(cur + ch) <= max_width:
|
|
622
|
+
cur += ch
|
|
623
|
+
elif ch in no_start:
|
|
624
|
+
cur += ch # 标点不能单独掉到下一行
|
|
625
|
+
else:
|
|
626
|
+
lines.append(cur)
|
|
627
|
+
cur = ch
|
|
628
|
+
lines.append(cur)
|
|
629
|
+
if len(lines) > max_lines:
|
|
630
|
+
lines = lines[:max_lines]
|
|
631
|
+
last = lines[-1]
|
|
632
|
+
while last and measure(last + "…") > max_width:
|
|
633
|
+
last = last[:-1]
|
|
634
|
+
lines[-1] = (last + "…") if last else "…"
|
|
635
|
+
return lines or [""]
|
|
636
|
+
|
|
637
|
+
def _message_shapes(self, shown):
|
|
638
|
+
"""四个固定大框换成按正文文字测量的聊天气泡。
|
|
639
|
+
|
|
640
|
+
时间戳不算进气泡:它贴在气泡上方,气泡只包正文。先按最大可用宽度折行,
|
|
641
|
+
再取实际最长行的宽度;短消息不会撑满整行。
|
|
642
|
+
"""
|
|
643
|
+
w, h = self._geo.width, self._geo.height
|
|
644
|
+
base = shapes_for(w, h)
|
|
645
|
+
left, right = base[0][0], base[0][2]
|
|
646
|
+
max_bubble = min(right - left, max(240, int((right - left) * 0.72)))
|
|
647
|
+
min_bubble = min(110, max_bubble)
|
|
648
|
+
pad = 18
|
|
649
|
+
fbody = self.fonts.get(11)
|
|
650
|
+
fstamp = self.fonts.get(9)
|
|
651
|
+
ls = max(1, fbody.metrics("linespace"))
|
|
652
|
+
shapes, infos = [], []
|
|
653
|
+
for i in range(MESSAGE_SLOTS):
|
|
654
|
+
if i >= len(shown):
|
|
655
|
+
shapes.append((0, 0, 0, 0, 0)) # 没有这条消息:不画框
|
|
656
|
+
continue
|
|
657
|
+
msg = shown[i]
|
|
658
|
+
x0, y0, x1, y1, _r = base[i]
|
|
659
|
+
slot_h = y1 - y0
|
|
660
|
+
is_user = str(msg.get("role") or "").lower() in ("user", "me", "human")
|
|
661
|
+
stamp = str(msg.get("stamp") or "")
|
|
662
|
+
stamp_h = fstamp.metrics("linespace") if stamp else 0
|
|
663
|
+
body_avail = max(12, slot_h - stamp_h)
|
|
664
|
+
content_max = max(80, max_bubble - 2 * pad)
|
|
665
|
+
lines = self._fit_lines(msg.get("text") or "", 11, content_max, body_avail)
|
|
666
|
+
inner = max([0] + [fbody.measure(line) for line in lines])
|
|
667
|
+
bw = min(max_bubble, max(min_bubble, inner + 2 * pad))
|
|
668
|
+
bh = min(body_avail, max(ls + 16, len(lines) * ls + 16))
|
|
669
|
+
by0 = y0 + stamp_h
|
|
670
|
+
by1 = min(y1, by0 + bh)
|
|
671
|
+
if is_user:
|
|
672
|
+
bx1 = right
|
|
673
|
+
bx0 = bx1 - bw
|
|
674
|
+
tx, ta = bx1 - pad, "ne"
|
|
675
|
+
else:
|
|
676
|
+
bx0 = left
|
|
677
|
+
bx1 = bx0 + bw
|
|
678
|
+
tx, ta = bx0 + pad, "nw"
|
|
679
|
+
shape = (bx0, by0, bx1, by1, min(26, max(16, bh // 3)))
|
|
680
|
+
shapes.append(shape)
|
|
681
|
+
infos.append({
|
|
682
|
+
"shape": shape, "is_user": is_user, "stamp": stamp,
|
|
683
|
+
"tx": tx, "ta": ta, "lines": lines,
|
|
684
|
+
"ty": by0 + max(4, (bh - len(lines) * ls) // 2),
|
|
685
|
+
"stamp_y": y0, "ls": ls,
|
|
686
|
+
})
|
|
687
|
+
shapes.extend(base[MESSAGE_SLOTS:])
|
|
688
|
+
return shapes, infos
|
|
689
|
+
|
|
690
|
+
# ---- 布局 ----
|
|
691
|
+
|
|
692
|
+
def _ensure_entry(self) -> None:
|
|
693
|
+
"""Entry 只建一次:每次消息都销毁重建会丢输入法状态和草稿光标。"""
|
|
694
|
+
if self._entry is not None:
|
|
695
|
+
return
|
|
696
|
+
self._entry = tk.Entry(self.root, bg=KEY, fg=KEY, insertbackground=KEY,
|
|
697
|
+
relief="flat", bd=0, font=self.fonts.spec(11),
|
|
698
|
+
insertwidth=0, highlightthickness=0)
|
|
699
|
+
self._entry.bind("<KeyRelease>", self._on_input_key)
|
|
700
|
+
self._entry.bind("<Return>", self._send)
|
|
701
|
+
self._entry.bind("<FocusIn>", lambda _e: self._render_input())
|
|
702
|
+
self._entry.bind("<FocusOut>", lambda _e: self._render_input())
|
|
703
|
+
|
|
704
|
+
def _layout(self, seed: bool = False) -> None:
|
|
705
|
+
"""整窗重排:标题栏 + 气泡 + 输入区 + 滚动条,然后把上一帧贴回去。
|
|
706
|
+
|
|
707
|
+
`seed=True` 时按新尺寸缩放上一帧先顶着(缩放/展开的过渡帧)。
|
|
708
|
+
"""
|
|
709
|
+
c = self.canvas
|
|
710
|
+
geo = self._geo
|
|
711
|
+
w, h = geo.width, geo.height
|
|
712
|
+
c.delete("all")
|
|
713
|
+
self.text.reset()
|
|
714
|
+
self._bg = c.create_image(0, 0, anchor="nw")
|
|
715
|
+
|
|
716
|
+
frame = self._collapsed_frame if self._collapsed else self._last_frame
|
|
717
|
+
if frame is not None and frame.size == (w, h):
|
|
718
|
+
try:
|
|
719
|
+
self._img = ImageTk.PhotoImage(frame, master=c)
|
|
720
|
+
c.itemconfigure(self._bg, image=self._img)
|
|
721
|
+
except Exception:
|
|
722
|
+
pass
|
|
723
|
+
elif seed and self._last_frame is not None:
|
|
724
|
+
preview = self._last_frame.resize((w, h), Image.Resampling.BILINEAR)
|
|
725
|
+
try:
|
|
726
|
+
self._img = ImageTk.PhotoImage(preview, master=c)
|
|
727
|
+
c.itemconfigure(self._bg, image=self._img)
|
|
728
|
+
except Exception:
|
|
729
|
+
pass
|
|
730
|
+
if PREVIEW:
|
|
731
|
+
c.create_rectangle(0, 0, w, h, fill="#0d131d", outline="", tags=("preview-bg",))
|
|
732
|
+
c.tag_lower("preview-bg")
|
|
733
|
+
|
|
734
|
+
self._draw_header()
|
|
735
|
+
if self._collapsed:
|
|
736
|
+
self._bind_header()
|
|
737
|
+
c.itemconfigure(self._bg, state="normal")
|
|
738
|
+
self._bar_len = max(72, int(w * BAR_FRAC))
|
|
739
|
+
self._bar = c.create_line(w // 2 - self._bar_len // 2, BAR_Y,
|
|
740
|
+
w // 2 + self._bar_len // 2, BAR_Y,
|
|
741
|
+
width=BAR_THICK, fill=self._bar_color,
|
|
742
|
+
capstyle="round")
|
|
743
|
+
self.text.refresh()
|
|
744
|
+
return
|
|
745
|
+
|
|
746
|
+
self._draw_feed()
|
|
747
|
+
self._draw_input()
|
|
748
|
+
self._draw_scrollbar()
|
|
749
|
+
self._bind_header()
|
|
750
|
+
c.tag_bind("send", "<Button-1>", self._send)
|
|
751
|
+
c.tag_bind("inputzone", "<Button-1>", self._focus_input)
|
|
752
|
+
c.tag_raise("close")
|
|
753
|
+
c.tag_raise("send")
|
|
754
|
+
|
|
755
|
+
self._bar_len = max(72, int(w * BAR_FRAC))
|
|
756
|
+
self._bar = c.create_line(w // 2 - self._bar_len // 2, BAR_Y,
|
|
757
|
+
w // 2 + self._bar_len // 2, BAR_Y,
|
|
758
|
+
width=BAR_THICK, fill="#b8cbe0", capstyle="round")
|
|
759
|
+
self._bar_color = "#b8cbe0"
|
|
760
|
+
self.text.refresh()
|
|
761
|
+
|
|
762
|
+
def _draw_header(self) -> None:
|
|
763
|
+
c = self.canvas
|
|
764
|
+
w = self._geo.width
|
|
765
|
+
mic_on = self._mic_proc is not None and self._mic_proc.poll() is None
|
|
766
|
+
dot = "#e05a5a" if mic_on else "#0a84ff"
|
|
767
|
+
# 标题栏玻璃是 y=12..66,中心 39;右边 ▾/✕ 也在 38。点、名字、状态必须
|
|
768
|
+
# 同一条水平中线,且字号要能完整落在条内,不能压到气泡区。
|
|
769
|
+
cy, dot_x, title_x = 38, 54, 68
|
|
770
|
+
c.create_oval(dot_x - 9, cy - 9, dot_x + 9, cy + 9,
|
|
771
|
+
outline="#2b4d73" if dot == "#0a84ff" else "#6b3030", width=1)
|
|
772
|
+
c.create_oval(dot_x - 4, cy - 4, dot_x + 4, cy + 4, fill=dot, outline="")
|
|
773
|
+
name = self.name[:10]
|
|
774
|
+
self.text.draw(title_x, cy, name, anchor="w", fill="#f7fbff", size=16,
|
|
775
|
+
bold=True, tags=("title",))
|
|
776
|
+
count = self.feed.status or f"{len(self.feed.messages)} 条"
|
|
777
|
+
nx = title_x + self.fonts.get(16, True).measure(name) + 10
|
|
778
|
+
self.text.draw(nx, cy, count, anchor="w",
|
|
779
|
+
fill="#ffb4b4" if self.feed.status else "#dbe6f5", size=11)
|
|
780
|
+
self.text.draw(w - 94, 38, "▾", anchor="w", fill="#7f8da3", size=12, tags=("collapse",))
|
|
781
|
+
self.text.draw(w - 58, 38, "✕", anchor="w", fill="#9fb0c8", size=13, tags=("close",))
|
|
782
|
+
|
|
783
|
+
def _bind_header(self) -> None:
|
|
784
|
+
c = self.canvas
|
|
785
|
+
c.tag_bind("close", "<Button-1>", self._close)
|
|
786
|
+
c.tag_bind("collapse", "<Button-1>", self.toggle_collapse)
|
|
787
|
+
# 双击改名:单击要留给「按住标题拖动窗口」,两者不能抢同一个事件
|
|
788
|
+
c.tag_bind("title", "<Double-Button-1>", self._edit_title)
|
|
789
|
+
|
|
790
|
+
def _draw_feed(self) -> None:
|
|
791
|
+
w = self._geo.width
|
|
792
|
+
shown = self.feed.visible()
|
|
793
|
+
if shown:
|
|
794
|
+
shapes, infos = self._message_shapes(shown)
|
|
795
|
+
else:
|
|
796
|
+
shapes, infos = shapes_for(w, self._geo.height), []
|
|
797
|
+
if shapes != self._geo.shapes:
|
|
798
|
+
with self._geo_lock:
|
|
799
|
+
self._set_geometry(self._geo.with_shapes(shapes))
|
|
800
|
+
# 每条消息只按正文占一块;时间戳贴在气泡上方,不进框
|
|
801
|
+
for info in infos:
|
|
802
|
+
if info["stamp"]:
|
|
803
|
+
self.text.draw(info["tx"], info["stamp_y"], info["stamp"],
|
|
804
|
+
anchor=info["ta"], fill="#a8b7c9", size=9, tags=("feed",))
|
|
805
|
+
for li, line in enumerate(info["lines"]):
|
|
806
|
+
self.text.draw(info["tx"], info["ty"] + li * info["ls"], line,
|
|
807
|
+
anchor=info["ta"], fill="#edf6ff", size=11, tags=("feed",))
|
|
808
|
+
if not shown:
|
|
809
|
+
slot = self._geo.shapes[0]
|
|
810
|
+
self.text.draw(w // 2, slot[1] + (slot[3] - slot[1]) // 2,
|
|
811
|
+
"在这里跟它说话 · 滚轮翻历史", anchor="center",
|
|
812
|
+
fill="#8e9db2", size=12, tags=("feed",))
|
|
813
|
+
|
|
814
|
+
def _draw_input(self) -> None:
|
|
815
|
+
geo = self._geo
|
|
816
|
+
x0, y0, x1, y1, _r = geo.input_shape
|
|
817
|
+
self._ensure_entry()
|
|
818
|
+
# Entry 放到 2x2 且底色是键色:真正显示的文字画在 Canvas 上,
|
|
819
|
+
# 否则玻璃里会出现一个突兀的黑条(IME 候选框仍然正常)。
|
|
820
|
+
self._entry_item = self.canvas.create_window(
|
|
821
|
+
x0 + 84, y0 + 27, window=self._entry, anchor="w",
|
|
822
|
+
width=2, height=2, tags=("feed",))
|
|
823
|
+
self._render_input()
|
|
824
|
+
sx0, sy0, sx1, sy1, _r = geo.send_shape
|
|
825
|
+
self.text.draw((sx0 + sx1) // 2, sy0 + 27, "发送", tags=("send", "feed"),
|
|
826
|
+
fill="#ffffff", size=12, bold=True)
|
|
827
|
+
|
|
828
|
+
def _draw_scrollbar(self) -> None:
|
|
829
|
+
"""滚动条放在主体玻璃右侧外面,不占消息区。"""
|
|
830
|
+
c = self.canvas
|
|
831
|
+
c.delete("sbar")
|
|
832
|
+
total = len(self.feed.messages)
|
|
833
|
+
visible = MESSAGE_SLOTS
|
|
834
|
+
if total <= visible:
|
|
835
|
+
return
|
|
836
|
+
body = self._geo.slabs[1]
|
|
837
|
+
x = self._geo.width - 6
|
|
838
|
+
top = body[1] + 8
|
|
839
|
+
view_h = max(1, body[3] - body[1] - 16)
|
|
840
|
+
thumb = max(24, int(view_h * (visible / float(total))))
|
|
841
|
+
max_off = max(1, total - visible)
|
|
842
|
+
t = 1.0 - max(0, min(max_off, self.feed.offset)) / float(max_off)
|
|
843
|
+
y0 = top + t * max(0, view_h - thumb)
|
|
844
|
+
c.create_line(x, y0, x, y0 + thumb, width=3, fill="#8d95a4",
|
|
845
|
+
capstyle="round", tags=("sbar",))
|
|
846
|
+
c.tag_raise("sbar")
|
|
847
|
+
|
|
848
|
+
def _refresh_feed(self) -> None:
|
|
849
|
+
"""聊天流变了就重排;Entry 的草稿和焦点要保住(它在 _layout 里复用)。"""
|
|
850
|
+
if not hasattr(self, "canvas"):
|
|
851
|
+
return
|
|
852
|
+
self._layout()
|
|
853
|
+
if self._last_frame is not None:
|
|
854
|
+
self._adapt_now(self._last_frame)
|
|
855
|
+
|
|
856
|
+
def _adapt_now(self, frame) -> None:
|
|
857
|
+
self._aux_at = 0.0
|
|
858
|
+
self.text.adapt(frame)
|
|
859
|
+
self._adapt_bar(frame)
|
|
860
|
+
|
|
861
|
+
def _adapt_bar(self, frame) -> None:
|
|
862
|
+
"""最上面细条随背景切换深浅(同样要滞回,理由见 TextLayer.adapt)。"""
|
|
863
|
+
if frame is None or self._bar is None:
|
|
864
|
+
return
|
|
865
|
+
now = time.time()
|
|
866
|
+
if now - self._aux_at < 0.25:
|
|
867
|
+
return
|
|
868
|
+
self._aux_at = now
|
|
869
|
+
mid = self._geo.width // 2
|
|
870
|
+
half = max(50, self._bar_len // 2)
|
|
871
|
+
try:
|
|
872
|
+
arr = np.asarray(frame.convert("L"))
|
|
873
|
+
except Exception:
|
|
874
|
+
return
|
|
875
|
+
patch = arr[max(0, BAR_Y - 6):BAR_Y + 12, max(0, mid - half):mid + half]
|
|
876
|
+
if patch.size == 0:
|
|
877
|
+
return
|
|
878
|
+
p50 = float(np.median(patch)) / 255.0
|
|
879
|
+
cur = self._bar_color
|
|
880
|
+
if cur == "#4e6278":
|
|
881
|
+
fill = "#b8cbe0" if p50 < 0.36 else cur # 已很深:要够暗才翻浅
|
|
882
|
+
else:
|
|
883
|
+
fill = "#4e6278" if p50 > 0.60 else cur
|
|
884
|
+
if fill != cur:
|
|
885
|
+
self._bar_color = fill
|
|
886
|
+
try:
|
|
887
|
+
self.canvas.itemconfigure(self._bar, fill=fill)
|
|
888
|
+
except Exception:
|
|
889
|
+
pass
|
|
890
|
+
|
|
891
|
+
# ---- 预览 / 重建 ----
|
|
892
|
+
|
|
893
|
+
def _preview_resize(self, w: int, h: int) -> None:
|
|
894
|
+
"""拖拽时先缩放上一帧并重排文字,松手后再重建玻璃贴图。
|
|
895
|
+
|
|
896
|
+
这一路故意**不**重建遮罩/折射图:拖拽期间 worker 是停的,只要文字和
|
|
897
|
+
上一帧跟得上手就行;松手的 `_rebuild()` 才做完整重建。
|
|
898
|
+
"""
|
|
899
|
+
if w < MIN_W or h < MIN_H:
|
|
900
|
+
return
|
|
901
|
+
with self._geo_lock:
|
|
902
|
+
self._geo = Geometry.for_size(w, h, version=self._geo.version)
|
|
903
|
+
self._renderer.geo = self._geo
|
|
904
|
+
self.canvas.config(width=w, height=h)
|
|
905
|
+
if self._last_frame is not None and self._last_frame.size != (w, h):
|
|
906
|
+
self._last_frame = self._last_frame.resize((w, h), Image.Resampling.BILINEAR)
|
|
907
|
+
self._layout()
|
|
908
|
+
|
|
909
|
+
def _rebuild(self, w: int, h: int) -> None:
|
|
910
|
+
"""松手后按最终尺寸重建(含遮罩/折射图)。"""
|
|
911
|
+
self._pause.set()
|
|
912
|
+
with self._geo_lock:
|
|
913
|
+
self._set_geometry(Geometry.for_size(w, h))
|
|
914
|
+
while True:
|
|
915
|
+
try:
|
|
916
|
+
self._q.get_nowait()
|
|
917
|
+
except queue.Empty:
|
|
918
|
+
break
|
|
919
|
+
self._layout(seed=True)
|
|
920
|
+
self._pause.clear()
|
|
921
|
+
|
|
922
|
+
# ---- 截屏 / 心跳 ----
|
|
923
|
+
|
|
924
|
+
def _snapshot_mode(self) -> None:
|
|
925
|
+
"""只给文档截图:停抓取、关 WDA,保留最后一帧(不改变正常路径)。"""
|
|
926
|
+
self._snapshot_done = True
|
|
927
|
+
self._pause.set()
|
|
928
|
+
if self._excluded:
|
|
929
|
+
self._set_exclude(False)
|
|
930
|
+
|
|
931
|
+
def _beat(self) -> None:
|
|
932
|
+
"""写心跳(每秒最多一次)。插件侧 `windowAlive()` 读它的 mtime。"""
|
|
933
|
+
now = time.time()
|
|
934
|
+
if now - getattr(self, "_beat_at", 0.0) < HEARTBEAT_EVERY:
|
|
935
|
+
return
|
|
936
|
+
self._beat_at = now
|
|
937
|
+
try:
|
|
938
|
+
HEARTBEAT.parent.mkdir(parents=True, exist_ok=True)
|
|
939
|
+
HEARTBEAT.write_bytes(b"")
|
|
940
|
+
except Exception:
|
|
941
|
+
pass
|
|
942
|
+
|
|
943
|
+
# ---- 主循环 ----
|
|
944
|
+
|
|
945
|
+
def _tick(self) -> None:
|
|
946
|
+
self._beat()
|
|
947
|
+
if SNAPSHOT and self._snapshot_done:
|
|
948
|
+
self._pause.set()
|
|
949
|
+
if self._excluded:
|
|
950
|
+
self._set_exclude(False)
|
|
951
|
+
self.root.after(100, self._tick)
|
|
952
|
+
return
|
|
953
|
+
if PREVIEW:
|
|
954
|
+
self._pause.set()
|
|
955
|
+
if self._excluded:
|
|
956
|
+
self._set_exclude(False)
|
|
957
|
+
self.root.after(100, self._tick)
|
|
958
|
+
return
|
|
959
|
+
# 缩放拖拽期间由 _preview_resize 负责画面,避免旧帧覆盖预览
|
|
960
|
+
if self._resize or self._collapsed:
|
|
961
|
+
self.root.after(INTERVAL_MS, self._tick)
|
|
962
|
+
return
|
|
963
|
+
if not self._excluded and not self._cappable:
|
|
964
|
+
self._set_exclude(True)
|
|
965
|
+
self._pause.clear()
|
|
966
|
+
|
|
967
|
+
try:
|
|
968
|
+
out = self._q.get_nowait()
|
|
969
|
+
except queue.Empty:
|
|
970
|
+
out = None
|
|
971
|
+
if out is not None:
|
|
972
|
+
try:
|
|
973
|
+
self._last_frame = out
|
|
974
|
+
self._img = ImageTk.PhotoImage(out, master=self.canvas)
|
|
975
|
+
self.canvas.itemconfigure(self._bg, image=self._img)
|
|
976
|
+
self.canvas.tag_lower(self._bg)
|
|
977
|
+
self.text.adapt(out)
|
|
978
|
+
self._adapt_bar(out)
|
|
979
|
+
except Exception as exc:
|
|
980
|
+
print("draw error", exc, flush=True)
|
|
981
|
+
|
|
982
|
+
self.root.after(INTERVAL_MS, self._tick)
|
|
983
|
+
|
|
984
|
+
def _work(self) -> None:
|
|
985
|
+
"""抓取 + 合成全在后台,主线程只负责显示最新一帧。"""
|
|
986
|
+
import mss
|
|
987
|
+
|
|
988
|
+
idle = 0
|
|
989
|
+
with mss.mss() as sct:
|
|
990
|
+
while not self._stop.is_set():
|
|
991
|
+
if self._pause.is_set() or self._collapsed:
|
|
992
|
+
time.sleep(0.03)
|
|
993
|
+
continue
|
|
994
|
+
t0 = time.perf_counter()
|
|
995
|
+
try:
|
|
996
|
+
out = self._renderer.render(self._source, sct, self._bbox())
|
|
997
|
+
if out is not None:
|
|
998
|
+
idle = 0
|
|
999
|
+
try:
|
|
1000
|
+
self._q.get_nowait()
|
|
1001
|
+
except queue.Empty:
|
|
1002
|
+
pass
|
|
1003
|
+
self._q.put(out)
|
|
1004
|
+
else:
|
|
1005
|
+
idle += 1
|
|
1006
|
+
except Exception as exc:
|
|
1007
|
+
print("capture error", exc, flush=True)
|
|
1008
|
+
# GPU 起不来(没有 moderngl / 显卡不支持 GL3.3)就换回 CPU,
|
|
1009
|
+
# 不然后台线程每帧刷一条,用户只看到一块永远不刷新的玻璃。
|
|
1010
|
+
if getattr(self._renderer, "broken", False):
|
|
1011
|
+
print("GPU 不可用,切换到 CPU 渲染", flush=True)
|
|
1012
|
+
self._renderer = CpuRenderer(self._geo)
|
|
1013
|
+
dt = time.perf_counter() - t0
|
|
1014
|
+
self._ema = self._ema * 0.9 + dt * 0.1
|
|
1015
|
+
if self._ema > 0.026:
|
|
1016
|
+
self._target_fps = 45
|
|
1017
|
+
elif self._ema > 0.016:
|
|
1018
|
+
self._target_fps = 60
|
|
1019
|
+
elif self._ema > 0.011:
|
|
1020
|
+
self._target_fps = 90
|
|
1021
|
+
else:
|
|
1022
|
+
self._target_fps = min(MAX_FPS, self._hz)
|
|
1023
|
+
# 静态画面:合成已经跳过,但「有没有变」只能靠抓屏看,单次 grab
|
|
1024
|
+
# 就要 ~7ms —— 按 60fps 空转等于白烧 40% 单核。探针降到 30fps,
|
|
1025
|
+
# 有变化时 idle 立刻归零、下一拍就恢复全速(最坏多等 33ms,
|
|
1026
|
+
# 拖动/滚动/播视频这类真的在动的情况根本进不到这个分支)。
|
|
1027
|
+
fps = self._target_fps if idle <= 20 else min(self._target_fps, PROBE_FPS)
|
|
1028
|
+
time.sleep(max(0.004, 1 / max(1, fps) - dt))
|
|
1029
|
+
|
|
1030
|
+
# ---- 窗口句柄 ----
|
|
1031
|
+
|
|
1032
|
+
def _hwnd_root(self):
|
|
1033
|
+
if self._hwnd is None:
|
|
1034
|
+
self._hwnd = ctypes.windll.user32.GetAncestor(self.root.winfo_id(), 2)
|
|
1035
|
+
return self._hwnd
|
|
1036
|
+
|
|
1037
|
+
def _set_exclude(self, on: bool) -> None:
|
|
1038
|
+
hwnd = self._hwnd_root()
|
|
1039
|
+
fn = ctypes.windll.user32.SetWindowDisplayAffinity
|
|
1040
|
+
fn.argtypes = [wintypes.HWND, wintypes.DWORD]
|
|
1041
|
+
fn.restype = wintypes.BOOL
|
|
1042
|
+
ok = bool(fn(hwnd, 0x11 if on else 0))
|
|
1043
|
+
self._excluded = ok and on
|
|
1044
|
+
if not ok:
|
|
1045
|
+
print("WDA failed", ctypes.GetLastError(), flush=True)
|
|
1046
|
+
|
|
1047
|
+
def _bbox(self):
|
|
1048
|
+
rc = _RECT()
|
|
1049
|
+
ctypes.windll.user32.GetWindowRect(self._hwnd_root(), ctypes.byref(rc))
|
|
1050
|
+
return (rc.left, rc.top, rc.right, rc.bottom)
|
|
1051
|
+
|
|
1052
|
+
def _sync_pos(self) -> None:
|
|
1053
|
+
"""把 Tk 当前左上角同步回 _x/_y。
|
|
1054
|
+
|
|
1055
|
+
拖动/缩放只改了 root.geometry,没改这两个缓存;折叠时如果继续用旧坐标,
|
|
1056
|
+
窗口会被拉回拖动前的位置(用户看到的就是「点收起位置跳回」)。
|
|
1057
|
+
"""
|
|
1058
|
+
try:
|
|
1059
|
+
self._x = int(self.root.winfo_x())
|
|
1060
|
+
self._y = int(self.root.winfo_y())
|
|
1061
|
+
except Exception:
|
|
1062
|
+
pass
|
|
1063
|
+
|
|
1064
|
+
def _save_geometry(self) -> None:
|
|
1065
|
+
h = self._saved_h if self._collapsed else self.root.winfo_height()
|
|
1066
|
+
write_config({"geometry": f"{self.root.winfo_width()}x{h}"
|
|
1067
|
+
f"+{self.root.winfo_x()}+{self.root.winfo_y()}"})
|
|
1068
|
+
|
|
1069
|
+
# ---- 关闭 / 隐藏 ----
|
|
1070
|
+
|
|
1071
|
+
def _close(self, _e=None):
|
|
1072
|
+
try:
|
|
1073
|
+
self._save_geometry()
|
|
1074
|
+
self._stop_mic()
|
|
1075
|
+
self._stop.set()
|
|
1076
|
+
self._pause.set()
|
|
1077
|
+
self._set_exclude(False)
|
|
1078
|
+
self._worker.join(timeout=0.3)
|
|
1079
|
+
try:
|
|
1080
|
+
self._renderer.close()
|
|
1081
|
+
except Exception:
|
|
1082
|
+
pass
|
|
1083
|
+
ctypes.windll.winmm.timeEndPeriod(1)
|
|
1084
|
+
finally:
|
|
1085
|
+
self.root.destroy()
|
|
1086
|
+
return "break"
|
|
1087
|
+
|
|
1088
|
+
# ---- 边缘缩放 / 拖动 ----
|
|
1089
|
+
|
|
1090
|
+
def _edge_at(self, x, y):
|
|
1091
|
+
w, h = self.root.winfo_width(), self.root.winfo_height()
|
|
1092
|
+
# 圆角是透明的,鼠标落在最外侧时命中的是 DWM 非客户区;
|
|
1093
|
+
# 命中带放宽到 20px,用户不必精确到 1px 也能拖边缘/四角。
|
|
1094
|
+
edge, corner = 20, 32
|
|
1095
|
+
if x <= corner and y <= corner:
|
|
1096
|
+
return "nw"
|
|
1097
|
+
if x >= w - corner and y <= corner:
|
|
1098
|
+
return "ne"
|
|
1099
|
+
if x <= corner and y >= h - corner:
|
|
1100
|
+
return "sw"
|
|
1101
|
+
if x >= w - corner and y >= h - corner:
|
|
1102
|
+
return "se"
|
|
1103
|
+
if y <= edge:
|
|
1104
|
+
return None # 顶部中间只拖动,不缩放
|
|
1105
|
+
if x <= edge:
|
|
1106
|
+
return "w"
|
|
1107
|
+
if x >= w - edge:
|
|
1108
|
+
return "e"
|
|
1109
|
+
if y >= h - edge:
|
|
1110
|
+
return "s"
|
|
1111
|
+
return None
|
|
1112
|
+
|
|
1113
|
+
@staticmethod
|
|
1114
|
+
def _cursor_for(mode):
|
|
1115
|
+
return {"nw": "size_nw_se", "se": "size_nw_se",
|
|
1116
|
+
"ne": "size_ne_sw", "sw": "size_ne_sw",
|
|
1117
|
+
"w": "size_we", "e": "size_we",
|
|
1118
|
+
"n": "size_ns", "s": "size_ns"}.get(mode, "")
|
|
1119
|
+
|
|
1120
|
+
def _hover(self, e):
|
|
1121
|
+
if self._resize:
|
|
1122
|
+
return
|
|
1123
|
+
try:
|
|
1124
|
+
self.canvas.config(cursor=self._cursor_for(self._edge_at(e.x, e.y)))
|
|
1125
|
+
except Exception:
|
|
1126
|
+
pass
|
|
1127
|
+
|
|
1128
|
+
def _leave(self, _e):
|
|
1129
|
+
if not self._resize:
|
|
1130
|
+
self.canvas.config(cursor="")
|
|
1131
|
+
|
|
1132
|
+
def _press(self, e):
|
|
1133
|
+
# 麦克风图标画在玻璃里,不在 Canvas 上单独建控件;点它不能当拖窗
|
|
1134
|
+
mx, my = self._geo.mic_pos
|
|
1135
|
+
if abs(e.x - (mx + MIC_SIZE // 2)) <= 22 and abs(e.y - (my + MIC_SIZE // 2)) <= 22:
|
|
1136
|
+
self._toggle_mic()
|
|
1137
|
+
return
|
|
1138
|
+
x0, y0, x1, y1, _r = self._geo.input_shape
|
|
1139
|
+
if x0 <= e.x <= x1 and y0 <= e.y <= y1:
|
|
1140
|
+
self._focus_input()
|
|
1141
|
+
return
|
|
1142
|
+
mode = self._edge_at(e.x, e.y)
|
|
1143
|
+
if mode:
|
|
1144
|
+
self._resize = (mode, e.x_root, e.y_root,
|
|
1145
|
+
self.root.winfo_x(), self.root.winfo_y(),
|
|
1146
|
+
self.root.winfo_width(), self.root.winfo_height())
|
|
1147
|
+
self._drag = None
|
|
1148
|
+
self._pause.set()
|
|
1149
|
+
return
|
|
1150
|
+
self._drag = (e.x_root - self.root.winfo_x(),
|
|
1151
|
+
e.y_root - self.root.winfo_y())
|
|
1152
|
+
|
|
1153
|
+
def _motion(self, e):
|
|
1154
|
+
if self._resize:
|
|
1155
|
+
mode, sx, sy, x0, y0, w0, h0 = self._resize
|
|
1156
|
+
dx, dy = e.x_root - sx, e.y_root - sy
|
|
1157
|
+
nw, nh, nx, ny = w0, h0, x0, y0
|
|
1158
|
+
if "e" in mode:
|
|
1159
|
+
nw = max(MIN_W, min(max(MIN_W, self._sw - x0), w0 + dx))
|
|
1160
|
+
if "w" in mode:
|
|
1161
|
+
nw = max(MIN_W, min(max(MIN_W, x0 + w0), w0 - dx))
|
|
1162
|
+
nx = x0 + w0 - nw
|
|
1163
|
+
if "s" in mode:
|
|
1164
|
+
nh = max(MIN_H, min(max(MIN_H, self._sh - y0), h0 + dy))
|
|
1165
|
+
if "n" in mode:
|
|
1166
|
+
nh = max(MIN_H, min(max(MIN_H, y0 + h0), h0 - dy))
|
|
1167
|
+
ny = y0 + h0 - nh
|
|
1168
|
+
nx = max(0, min(nx, max(0, self._sw - nw)))
|
|
1169
|
+
ny = max(0, min(ny, max(0, self._sh - nh)))
|
|
1170
|
+
self.root.geometry(f"{nw}x{nh}+{nx}+{ny}")
|
|
1171
|
+
now = time.time()
|
|
1172
|
+
if now - self._preview_at >= 0.04:
|
|
1173
|
+
self._preview_at = now
|
|
1174
|
+
self._preview_resize(nw, nh)
|
|
1175
|
+
return
|
|
1176
|
+
if self._drag:
|
|
1177
|
+
# 拖到屏幕外时窗口坐标会变负,抓屏区域随之越界(高度算出负数),
|
|
1178
|
+
# 背景永远画不出来 —— 表现为花屏。必须钳位。
|
|
1179
|
+
nx = max(0, min(max(0, self._sw - self.root.winfo_width()),
|
|
1180
|
+
e.x_root - self._drag[0]))
|
|
1181
|
+
ny = max(0, min(max(0, self._sh - self.root.winfo_height()),
|
|
1182
|
+
e.y_root - self._drag[1]))
|
|
1183
|
+
self.root.geometry(f"+{nx}+{ny}")
|
|
1184
|
+
|
|
1185
|
+
def _release(self, e):
|
|
1186
|
+
if self._resize:
|
|
1187
|
+
self._resize = None
|
|
1188
|
+
self._preview_at = 0.0
|
|
1189
|
+
self.root.update_idletasks()
|
|
1190
|
+
self._rebuild(max(MIN_W, self.root.winfo_width()),
|
|
1191
|
+
max(MIN_H, self.root.winfo_height()))
|
|
1192
|
+
self._sync_pos()
|
|
1193
|
+
self._hover(e)
|
|
1194
|
+
return
|
|
1195
|
+
self._drag = None
|
|
1196
|
+
self._sync_pos()
|
|
1197
|
+
|
|
1198
|
+
def run(self):
|
|
1199
|
+
self.root.bind("<Escape>", self._close)
|
|
1200
|
+
self.root.bind("<MouseWheel>", self._wheel)
|
|
1201
|
+
self.root.bind("<Motion>", self._hover, add="+")
|
|
1202
|
+
self.root.bind("<Leave>", self._leave, add="+")
|
|
1203
|
+
self.root.bind("<ButtonPress-1>", self._press, add="+")
|
|
1204
|
+
self.root.bind("<B1-Motion>", self._motion, add="+")
|
|
1205
|
+
self.root.bind("<ButtonRelease-1>", self._release, add="+")
|
|
1206
|
+
self.root.mainloop()
|
|
1207
|
+
|
|
1208
|
+
|
|
1209
|
+
if __name__ == "__main__":
|
|
1210
|
+
LiveGlass().run()
|