dsh-watch-together 0.2.15 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +82 -13
- package/README.md +2 -0
- package/dsh.plugin.json +12 -12
- package/examples/floating-chat/danmaku.py +294 -0
- package/examples/floating-chat/glass_capture.py +7 -2
- package/examples/floating-chat/tray.py +345 -0
- package/examples/floating-chat/window.py +526 -59
- package/examples/floating-chat/window_widgets.py +31 -5
- package/package.json +133 -133
- package/src/floating-window.js +46 -8
- package/src/settings/config.js +19 -0
- package/src/settings/page.html +28 -1
- package/src/tool-spec.js +12 -4
- package/src/watch-actions.js +12 -2
- package/src/window-interface.js +26 -3
- package/src/window-protocol.js +4 -0
|
@@ -0,0 +1,345 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""托盘图标:窗口"最小化到底下"之后,用户唯一能点回来的地方。
|
|
3
|
+
|
|
4
|
+
**为什么必须有它**:`overrideredirect(True)` 的窗口不会出现在任务栏,`withdraw()`
|
|
5
|
+
之后屏幕上一点痕迹都没有 —— 没有托盘就等于"窗口没了,只能杀进程"。用户的诉求是
|
|
6
|
+
"像网页那样缩到底下",网页缩下去还有任务栏按钮可点,这里对应的就是托盘图标。
|
|
7
|
+
|
|
8
|
+
零新依赖:`Shell_NotifyIconW` 走 ctypes,图标从仓库里的 `mic-dsh.png` 转一张 ico
|
|
9
|
+
缓存到 cache 目录。
|
|
10
|
+
|
|
11
|
+
**为什么消息循环在子线程**:在 Tk 主线程里用 `PeekMessage` 泵自己的窗口消息,
|
|
12
|
+
会撞上 Tk 自己的泵;更糟的是主线程的 ctypes 回调链里 `DefWindowProcW` 会释放 GIL,
|
|
13
|
+
回来时线程状态是 NULL —— 实测直接 `Fatal Python error: PyEval_RestoreThread`。
|
|
14
|
+
所以:子线程建窗口 + `GetMessage` 循环 + 回调只往 `queue` 里塞,主线程用
|
|
15
|
+
`root.after` 消费(Tk 只能在主线程碰)。回调本身从不直接操作窗口。
|
|
16
|
+
"""
|
|
17
|
+
from __future__ import annotations
|
|
18
|
+
|
|
19
|
+
import ctypes
|
|
20
|
+
import os
|
|
21
|
+
import queue
|
|
22
|
+
import threading
|
|
23
|
+
from ctypes import wintypes
|
|
24
|
+
|
|
25
|
+
IS_WIN = os.name == "nt"
|
|
26
|
+
|
|
27
|
+
WM_APP = 0x8000
|
|
28
|
+
WM_TRAY = WM_APP + 1
|
|
29
|
+
WM_QUIT = 0x0012
|
|
30
|
+
WM_LBUTTONUP = 0x0202
|
|
31
|
+
WM_LBUTTONDBLCLK = 0x0203
|
|
32
|
+
WM_RBUTTONUP = 0x0205
|
|
33
|
+
|
|
34
|
+
NIM_ADD, NIM_MODIFY, NIM_DELETE = 0, 1, 2
|
|
35
|
+
NIF_MESSAGE, NIF_ICON, NIF_TIP, NIF_SHOWTIP = 0x1, 0x2, 0x4, 0x80
|
|
36
|
+
NIF_INFO = 0x10
|
|
37
|
+
|
|
38
|
+
HWND_MESSAGE = -3
|
|
39
|
+
IMAGE_ICON, LR_LOADFROMFILE = 1, 0x10
|
|
40
|
+
IDI_APPLICATION = 32512
|
|
41
|
+
POLL_MS = 150
|
|
42
|
+
|
|
43
|
+
if IS_WIN:
|
|
44
|
+
from ctypes import WINFUNCTYPE
|
|
45
|
+
|
|
46
|
+
LRESULT = ctypes.c_ssize_t
|
|
47
|
+
WNDPROC = WINFUNCTYPE(LRESULT, wintypes.HWND, wintypes.UINT,
|
|
48
|
+
wintypes.WPARAM, wintypes.LPARAM)
|
|
49
|
+
|
|
50
|
+
class WNDCLASSW(ctypes.Structure):
|
|
51
|
+
_fields_ = [("style", wintypes.UINT),
|
|
52
|
+
("lpfnWndProc", WNDPROC),
|
|
53
|
+
("cbClsExtra", ctypes.c_int),
|
|
54
|
+
("cbWndExtra", ctypes.c_int),
|
|
55
|
+
("hInstance", wintypes.HINSTANCE),
|
|
56
|
+
("hIcon", wintypes.HICON),
|
|
57
|
+
("hCursor", wintypes.HANDLE),
|
|
58
|
+
("hbrBackground", wintypes.HBRUSH),
|
|
59
|
+
("lpszMenuName", wintypes.LPCWSTR),
|
|
60
|
+
("lpszClassName", wintypes.LPCWSTR)]
|
|
61
|
+
|
|
62
|
+
class NOTIFYICONDATAW(ctypes.Structure):
|
|
63
|
+
_fields_ = [("cbSize", wintypes.DWORD),
|
|
64
|
+
("hWnd", wintypes.HWND),
|
|
65
|
+
("uID", wintypes.UINT),
|
|
66
|
+
("uFlags", wintypes.UINT),
|
|
67
|
+
("uCallbackMessage", wintypes.UINT),
|
|
68
|
+
("hIcon", wintypes.HICON),
|
|
69
|
+
("szTip", wintypes.WCHAR * 128),
|
|
70
|
+
("dwState", wintypes.DWORD),
|
|
71
|
+
("dwStateMask", wintypes.DWORD),
|
|
72
|
+
("szInfo", wintypes.WCHAR * 256),
|
|
73
|
+
("uVersion", wintypes.UINT),
|
|
74
|
+
("szInfoTitle", wintypes.WCHAR * 64),
|
|
75
|
+
("dwInfoFlags", wintypes.DWORD),
|
|
76
|
+
("guidItem", ctypes.c_byte * 16),
|
|
77
|
+
("hBalloonIcon", wintypes.HICON)]
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def _ico_path(cache_dir, png_path) -> str:
|
|
81
|
+
"""PNG → ICO,落在 cache 里(失败返回空串,退化成系统图标)。"""
|
|
82
|
+
try:
|
|
83
|
+
from pathlib import Path
|
|
84
|
+
from PIL import Image
|
|
85
|
+
png = Path(png_path)
|
|
86
|
+
if not png.exists():
|
|
87
|
+
return ""
|
|
88
|
+
out = Path(cache_dir) / "dsh-tray.ico"
|
|
89
|
+
if out.exists() and out.stat().st_mtime >= png.stat().st_mtime:
|
|
90
|
+
return str(out)
|
|
91
|
+
out.parent.mkdir(parents=True, exist_ok=True)
|
|
92
|
+
Image.open(png).convert("RGBA").save(
|
|
93
|
+
out, format="ICO", sizes=[(16, 16), (24, 24), (32, 32), (48, 48)])
|
|
94
|
+
return str(out)
|
|
95
|
+
except Exception:
|
|
96
|
+
return ""
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
class TrayIcon:
|
|
100
|
+
"""一个图标 + 左键回主窗。没图标(非 Windows / 加载失败)时全部空转。"""
|
|
101
|
+
|
|
102
|
+
def __init__(self, root, *, tip="陪看 · 点击展开", icon_png=None,
|
|
103
|
+
cache_dir=None, on_activate=None):
|
|
104
|
+
self.root = root
|
|
105
|
+
self.tip = str(tip)[:120]
|
|
106
|
+
self.on_activate = on_activate
|
|
107
|
+
self.available = False
|
|
108
|
+
self._visible = False
|
|
109
|
+
self._hwnd = None
|
|
110
|
+
self._tid = None
|
|
111
|
+
self._job = None
|
|
112
|
+
self._wndproc = None # 必须留引用:被 GC 掉 = 回调直接崩
|
|
113
|
+
self._hicon = None
|
|
114
|
+
self._q = queue.Queue()
|
|
115
|
+
self._ready = threading.Event()
|
|
116
|
+
self._thread = None
|
|
117
|
+
if not IS_WIN:
|
|
118
|
+
return
|
|
119
|
+
try:
|
|
120
|
+
self._u = ctypes.windll.user32
|
|
121
|
+
self._shell = ctypes.windll.shell32
|
|
122
|
+
self._k32 = ctypes.windll.kernel32
|
|
123
|
+
# **每个吃指针/句柄的函数都必须设 argtypes**:不设的话 ctypes 按 c_int
|
|
124
|
+
# 传(32 位),64 位下 hInstance / hwnd 会被截断 —— 表现就是
|
|
125
|
+
# "窗口建起来了,跑一会儿 Segmentation fault",因为消息分发踩到了
|
|
126
|
+
# 别的模块地址(本机实测 2m44s 后崩)。
|
|
127
|
+
self._k32.GetModuleHandleW.argtypes = [wintypes.LPCWSTR]
|
|
128
|
+
self._k32.GetModuleHandleW.restype = wintypes.HMODULE
|
|
129
|
+
self._k32.GetCurrentThreadId.restype = wintypes.DWORD
|
|
130
|
+
self._u.DefWindowProcW.argtypes = [wintypes.HWND, wintypes.UINT,
|
|
131
|
+
wintypes.WPARAM, wintypes.LPARAM]
|
|
132
|
+
self._u.DefWindowProcW.restype = ctypes.c_ssize_t
|
|
133
|
+
self._u.RegisterClassW.argtypes = [ctypes.POINTER(WNDCLASSW)]
|
|
134
|
+
self._u.RegisterClassW.restype = wintypes.ATOM
|
|
135
|
+
self._u.CreateWindowExW.argtypes = [
|
|
136
|
+
wintypes.DWORD, wintypes.LPCWSTR, wintypes.LPCWSTR, wintypes.DWORD,
|
|
137
|
+
ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int,
|
|
138
|
+
wintypes.HWND, wintypes.HMENU, wintypes.HINSTANCE, wintypes.LPVOID]
|
|
139
|
+
self._u.CreateWindowExW.restype = wintypes.HWND
|
|
140
|
+
self._u.GetMessageW.argtypes = [ctypes.POINTER(wintypes.MSG),
|
|
141
|
+
wintypes.HWND, wintypes.UINT, wintypes.UINT]
|
|
142
|
+
self._u.GetMessageW.restype = ctypes.c_int
|
|
143
|
+
self._u.PostThreadMessageW.argtypes = [wintypes.DWORD, wintypes.UINT,
|
|
144
|
+
wintypes.WPARAM, wintypes.LPARAM]
|
|
145
|
+
self._u.LoadImageW.argtypes = [wintypes.HINSTANCE, wintypes.LPCWSTR,
|
|
146
|
+
wintypes.UINT, ctypes.c_int, ctypes.c_int,
|
|
147
|
+
wintypes.UINT]
|
|
148
|
+
self._u.LoadImageW.restype = wintypes.HANDLE
|
|
149
|
+
self._u.LoadIconW.argtypes = [wintypes.HINSTANCE, wintypes.LPVOID]
|
|
150
|
+
self._u.LoadIconW.restype = wintypes.HICON
|
|
151
|
+
self._shell.Shell_NotifyIconW.argtypes = [wintypes.DWORD,
|
|
152
|
+
ctypes.POINTER(NOTIFYICONDATAW)]
|
|
153
|
+
self._shell.Shell_NotifyIconW.restype = wintypes.BOOL
|
|
154
|
+
self._hinst = self._k32.GetModuleHandleW(None)
|
|
155
|
+
self._hicon = self._load_icon(icon_png, cache_dir)
|
|
156
|
+
self._thread = threading.Thread(target=self._run, name="dsh-tray",
|
|
157
|
+
daemon=True)
|
|
158
|
+
self._thread.start()
|
|
159
|
+
if not self._ready.wait(2.0):
|
|
160
|
+
raise OSError("tray window not ready")
|
|
161
|
+
self.available = True
|
|
162
|
+
except Exception as exc:
|
|
163
|
+
print("tray init failed", repr(exc), flush=True)
|
|
164
|
+
self.available = False
|
|
165
|
+
|
|
166
|
+
# ---- 子线程:建窗口 + 消息循环 ----
|
|
167
|
+
|
|
168
|
+
def _run(self) -> None:
|
|
169
|
+
u = self._u
|
|
170
|
+
# 类名带 pid:同名类第二次注册会失败(1410),而 `CreateWindowEx` 还是会
|
|
171
|
+
# 成功 —— 但它挂的是**上一个实例**的 WNDPROC,那个回调早就被回收了。
|
|
172
|
+
# 消息一到就是跳进野指针(段错误)。
|
|
173
|
+
cls_name = "DshFloatTrayWnd_%d" % os.getpid()
|
|
174
|
+
|
|
175
|
+
def proc(hwnd, msg, wparam, lparam):
|
|
176
|
+
if msg == WM_TRAY:
|
|
177
|
+
# 只往队列里塞,绝不碰 Tk(这里不是主线程)
|
|
178
|
+
self._q.put(int(lparam) & 0xFFFF)
|
|
179
|
+
return u.DefWindowProcW(hwnd, msg, wparam, lparam)
|
|
180
|
+
|
|
181
|
+
try:
|
|
182
|
+
self._wndproc = WNDPROC(proc)
|
|
183
|
+
wc = WNDCLASSW()
|
|
184
|
+
wc.style = 0
|
|
185
|
+
wc.lpfnWndProc = self._wndproc
|
|
186
|
+
wc.cbClsExtra = 0
|
|
187
|
+
wc.cbWndExtra = 0
|
|
188
|
+
wc.hInstance = self._hinst
|
|
189
|
+
wc.hIcon = 0
|
|
190
|
+
wc.hCursor = 0
|
|
191
|
+
wc.hbrBackground = 0
|
|
192
|
+
wc.lpszMenuName = None
|
|
193
|
+
wc.lpszClassName = cls_name
|
|
194
|
+
u.RegisterClassW(ctypes.byref(wc))
|
|
195
|
+
# 字符串必须显式 c_wchar_p:没 argtypes 时 ctypes 会按 bytes 传,
|
|
196
|
+
# 窗口类名变成乱码 → CreateWindowEx 返回 0。
|
|
197
|
+
self._hwnd = u.CreateWindowExW(
|
|
198
|
+
0, ctypes.c_wchar_p(cls_name), ctypes.c_wchar_p(cls_name), 0,
|
|
199
|
+
0, 0, 0, 0, wintypes.HWND(HWND_MESSAGE), 0, self._hinst, 0)
|
|
200
|
+
if not self._hwnd:
|
|
201
|
+
raise OSError("CreateWindowEx failed: %s" % ctypes.GetLastError())
|
|
202
|
+
self._tid = self._k32.GetCurrentThreadId()
|
|
203
|
+
except Exception as exc:
|
|
204
|
+
print("tray window failed", repr(exc), flush=True)
|
|
205
|
+
self._ready.set()
|
|
206
|
+
return
|
|
207
|
+
|
|
208
|
+
self._ready.set()
|
|
209
|
+
msg = wintypes.MSG()
|
|
210
|
+
try:
|
|
211
|
+
while True:
|
|
212
|
+
got = u.GetMessageW(ctypes.byref(msg), 0, 0, 0)
|
|
213
|
+
if got <= 0: # 0 = WM_QUIT,-1 = 出错
|
|
214
|
+
break
|
|
215
|
+
u.TranslateMessage(ctypes.byref(msg))
|
|
216
|
+
u.DispatchMessageW(ctypes.byref(msg))
|
|
217
|
+
except Exception as exc:
|
|
218
|
+
print("tray loop ended", repr(exc), flush=True)
|
|
219
|
+
|
|
220
|
+
def _load_icon(self, png, cache_dir):
|
|
221
|
+
path = _ico_path(cache_dir, png) if png and cache_dir else ""
|
|
222
|
+
if path:
|
|
223
|
+
h = self._u.LoadImageW(0, path, IMAGE_ICON, 32, 32, LR_LOADFROMFILE)
|
|
224
|
+
if h:
|
|
225
|
+
return h
|
|
226
|
+
# IDI_APPLICATION 是 MAKEINTRESOURCE,不是指针:必须包成 c_void_p 传
|
|
227
|
+
return self._u.LoadIconW(0, ctypes.c_void_p(IDI_APPLICATION))
|
|
228
|
+
|
|
229
|
+
# ---- 显隐(主线程)----
|
|
230
|
+
|
|
231
|
+
def _data(self):
|
|
232
|
+
d = NOTIFYICONDATAW()
|
|
233
|
+
d.cbSize = ctypes.sizeof(NOTIFYICONDATAW)
|
|
234
|
+
d.hWnd = self._hwnd
|
|
235
|
+
d.uID = 1
|
|
236
|
+
d.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP | NIF_SHOWTIP
|
|
237
|
+
d.uCallbackMessage = WM_TRAY
|
|
238
|
+
d.hIcon = self._hicon
|
|
239
|
+
d.szTip = self.tip
|
|
240
|
+
return d
|
|
241
|
+
|
|
242
|
+
def _notify_icon(self, action: int, d=None) -> bool:
|
|
243
|
+
"""统一入口。
|
|
244
|
+
|
|
245
|
+
⚠️ **结构体必须是活的局部变量**。`ctypes.addressof(self._data())` 拿到的
|
|
246
|
+
是临时对象的地址,表达式一结束对象就被回收 —— `Shell_NotifyIconW` 读的是
|
|
247
|
+
已释放内存(这种代码不报错,直接段错误)。`byref` 会把引用握到调用结束。
|
|
248
|
+
"""
|
|
249
|
+
if d is None:
|
|
250
|
+
d = self._data()
|
|
251
|
+
return bool(self._shell.Shell_NotifyIconW(action, ctypes.byref(d)))
|
|
252
|
+
|
|
253
|
+
def show(self) -> None:
|
|
254
|
+
if not self.available or self._visible:
|
|
255
|
+
return
|
|
256
|
+
try:
|
|
257
|
+
ok = self._notify_icon(NIM_ADD)
|
|
258
|
+
self._visible = ok
|
|
259
|
+
if not ok:
|
|
260
|
+
print("tray add failed", ctypes.GetLastError(), flush=True)
|
|
261
|
+
return
|
|
262
|
+
self._ensure_polling()
|
|
263
|
+
except Exception as exc:
|
|
264
|
+
print("tray add failed", repr(exc), flush=True)
|
|
265
|
+
|
|
266
|
+
def hide(self) -> None:
|
|
267
|
+
if not self.available or not self._visible:
|
|
268
|
+
return
|
|
269
|
+
try:
|
|
270
|
+
self._notify_icon(NIM_DELETE)
|
|
271
|
+
except Exception:
|
|
272
|
+
pass
|
|
273
|
+
self._visible = False
|
|
274
|
+
|
|
275
|
+
def set_tip(self, tip: str) -> None:
|
|
276
|
+
self.tip = str(tip)[:120]
|
|
277
|
+
if not self.available or not self._visible:
|
|
278
|
+
return
|
|
279
|
+
try:
|
|
280
|
+
self._notify_icon(NIM_MODIFY)
|
|
281
|
+
except Exception:
|
|
282
|
+
pass
|
|
283
|
+
|
|
284
|
+
def notify(self, text: str, title: str = "陪看") -> None:
|
|
285
|
+
"""最小化后弹一次系统通知。
|
|
286
|
+
|
|
287
|
+
Windows 11 默认把新图标丢进溢出区(要点 ^ 才看得见),只给一个图标
|
|
288
|
+
等于没提示;这条通知是告诉用户"东西还在、在哪"的唯一机会。
|
|
289
|
+
"""
|
|
290
|
+
if not self.available or not self._visible:
|
|
291
|
+
return
|
|
292
|
+
try:
|
|
293
|
+
d = self._data()
|
|
294
|
+
d.uFlags |= NIF_INFO
|
|
295
|
+
d.dwInfoFlags = 1 # NIIF_INFO
|
|
296
|
+
d.szInfoTitle = str(title)[:60]
|
|
297
|
+
d.szInfo = str(text)[:250]
|
|
298
|
+
self._notify_icon(NIM_MODIFY, d)
|
|
299
|
+
except Exception:
|
|
300
|
+
pass
|
|
301
|
+
|
|
302
|
+
# ---- 主线程:消费回调队列 ----
|
|
303
|
+
|
|
304
|
+
def _ensure_polling(self) -> None:
|
|
305
|
+
if self._job is not None or not self.available:
|
|
306
|
+
return
|
|
307
|
+
try:
|
|
308
|
+
self._job = self.root.after(POLL_MS, self._poll)
|
|
309
|
+
except Exception:
|
|
310
|
+
self._job = None
|
|
311
|
+
|
|
312
|
+
def _poll(self) -> None:
|
|
313
|
+
self._job = None
|
|
314
|
+
while True:
|
|
315
|
+
try:
|
|
316
|
+
kind = self._q.get_nowait()
|
|
317
|
+
except queue.Empty:
|
|
318
|
+
break
|
|
319
|
+
if kind in (WM_LBUTTONUP, WM_LBUTTONDBLCLK) and self.on_activate:
|
|
320
|
+
try:
|
|
321
|
+
self.on_activate()
|
|
322
|
+
except Exception as exc:
|
|
323
|
+
print("tray activate failed", repr(exc), flush=True)
|
|
324
|
+
if not self.available:
|
|
325
|
+
return
|
|
326
|
+
try:
|
|
327
|
+
self._job = self.root.after(POLL_MS, self._poll)
|
|
328
|
+
except Exception:
|
|
329
|
+
self._job = None
|
|
330
|
+
|
|
331
|
+
def close(self) -> None:
|
|
332
|
+
self.hide()
|
|
333
|
+
if self._job is not None:
|
|
334
|
+
try:
|
|
335
|
+
self.root.after_cancel(self._job)
|
|
336
|
+
except Exception:
|
|
337
|
+
pass
|
|
338
|
+
self._job = None
|
|
339
|
+
if self._tid:
|
|
340
|
+
try:
|
|
341
|
+
self._u.PostThreadMessageW(self._tid, WM_QUIT, 0, 0)
|
|
342
|
+
except Exception:
|
|
343
|
+
pass
|
|
344
|
+
self._tid = None
|
|
345
|
+
self.available = False
|