Repository navigation
Expand file tree
/
Copy pathdesktop-layer.js
More file actions
267 lines (254 loc) · 7.45 KB
/
Copy pathdesktop-layer.js
File metadata and controls
267 lines (254 loc) · 7.45 KB
1
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
// 桌面层锚定(WorkerW / Progman):把窗口挂进资源管理器的桌面窗口树,
// 位于桌面图标之上、所有普通窗口之下。通过 koffi 直接调用 user32,免编译原生模块。
const koffi = require("koffi");
const user32 = koffi.load("user32.dll");
const FindWindowW = user32.func(
"void * __stdcall FindWindowW(str16 cls, str16 win)",
);
const FindWindowExW = user32.func(
"void * __stdcall FindWindowExW(void *parent, void *after, str16 cls, str16 win)",
);
const SendMessageTimeoutW = user32.func(
"uintptr __stdcall SendMessageTimeoutW(void *hwnd, uint32 msg, uintptr wp, intptr lp, uint32 flags, uint32 timeout, void *result)",
);
const SetParent = user32.func(
"void * __stdcall SetParent(void *child, void *parent)",
);
const GetParent = user32.func("void * __stdcall GetParent(void *hwnd)");
const SetWindowPos = user32.func(
"int __stdcall SetWindowPos(void *hwnd, void *after, int32 x, int32 y, int32 cx, int32 cy, uint32 flags)",
);
const GetWindowLongW = user32.func(
"int32 __stdcall GetWindowLongW(void *hwnd, int32 index)",
);
const SetWindowLongW = user32.func(
"int32 __stdcall SetWindowLongW(void *hwnd, int32 index, int32 value)",
);
const IsWindow = user32.func("int __stdcall IsWindow(void *hwnd)");
const RECT = koffi.struct("RECT", {
left: "long",
top: "long",
right: "long",
bottom: "long",
});
const GetWindowRect = user32.func(
"int __stdcall GetWindowRect(void *hwnd, _Out_ RECT *rect)",
);
const GWL_STYLE = -16;
const WS_CHILD = 0x40000000;
const WS_POPUP = 0x80000000;
const HWND_TOP = 0;
const HWND_NOTOPMOST = -2;
const SWP_NOSIZE = 0x0001;
const SWP_NOMOVE = 0x0002;
const SWP_NOACTIVATE = 0x0010;
const SMTO_ABORTIFHUNG = 0x0008;
const WM_SPAWN_WORKERW = 0x052c;
// 当前锚定状态:{ hwnd, parent, defview, screen }
let attached = null;
let lastAttachError = 0;
function hwndOf(win) {
const handle = win.getNativeWindowHandle();
return handle.length === 8
? Number(handle.readBigUInt64LE(0))
: handle.readUInt32LE(0);
}
function containerRect(handle) {
const rc = { left: 0, top: 0, right: 0, bottom: 0 };
GetWindowRect(handle, rc);
return rc;
}
// 找承载桌面图标的容器:多数 Win10/11 会派生 WorkerW(内含 SHELLDLL_DefView),
// 否则图标层直接在 Progman 下,就用 Progman 本身
function findDesktopContainer() {
const progman = FindWindowW("Progman", null);
if (!progman) return null;
let defview = FindWindowExW(progman, null, "SHELLDLL_DefView", null);
if (!defview) {
try {
SendMessageTimeoutW(
progman,
WM_SPAWN_WORKERW,
0,
0,
SMTO_ABORTIFHUNG,
1000,
null,
);
} catch (_) {}
defview = FindWindowExW(progman, null, "SHELLDLL_DefView", null);
}
let container = progman;
let w = null;
for (;;) {
w = FindWindowExW(null, w, "WorkerW", null);
if (!w) break;
const dv = FindWindowExW(w, null, "SHELLDLL_DefView", null);
if (dv) {
container = w;
defview = dv;
break;
}
}
if (!defview) return null;
return { container, defview };
}
function setChildStyle(hwnd, child) {
const style = GetWindowLongW(hwnd, GWL_STYLE) >>> 0;
const next = child
? (style | WS_CHILD) & ~WS_POPUP
: (style | WS_POPUP) & ~WS_CHILD;
SetWindowLongW(hwnd, GWL_STYLE, next | 0);
}
// 挂入桌面层。抛错表示环境不支持(调用方自行回退为普通窗口)。
function attach(win) {
if (!win || win.isDestroyed()) return false;
if (
attached &&
IsWindow(attached.hwnd) &&
IsWindow(attached.parent) &&
attached.hwnd === hwndOf(win)
)
return true;
attached = null;
const target = findDesktopContainer();
if (!target) throw new Error("找不到桌面容器 (Progman/WorkerW)");
const hwnd = hwndOf(win);
const screen = win.getBounds();
const rc = containerRect(target.container);
setChildStyle(hwnd, true);
SetParent(hwnd, target.container);
if (Number(GetParent(hwnd)) !== Number(target.container)) {
setChildStyle(hwnd, false);
SetParent(hwnd, null);
win.setBounds(screen);
throw new Error("无法挂入桌面容器");
}
SetWindowPos(
hwnd,
HWND_TOP,
screen.x - rc.left,
screen.y - rc.top,
screen.width,
screen.height,
SWP_NOACTIVATE,
);
// 把图标层压到自己下方:既贴住桌面,又不被图标挡住、还能点
SetWindowPos(
target.defview,
hwnd,
0,
0,
0,
0,
SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE,
);
attached = {
hwnd,
parent: target.container,
defview: target.defview,
screen,
};
console.log(
`[desktop-layer] attached, parent=0x${Number(target.container).toString(16)}`,
);
return true;
}
// 从桌面层摘回普通窗口(坐标按容器偏移换算回屏幕坐标)
function detach(win) {
if (!attached) return false;
const state = attached;
attached = null;
if (!win || win.isDestroyed() || !IsWindow(state.hwnd)) return false;
const parentAlive = IsWindow(state.parent);
const rc = parentAlive
? containerRect(state.parent)
: { left: 0, top: 0, right: 0, bottom: 0 };
const bounds = win.getBounds();
const screenX = parentAlive ? bounds.x + rc.left : state.screen.x;
const screenY = parentAlive ? bounds.y + rc.top : state.screen.y;
setChildStyle(state.hwnd, false);
SetParent(state.hwnd, null);
SetWindowPos(
state.hwnd,
HWND_NOTOPMOST,
screenX,
screenY,
bounds.width,
bounds.height,
SWP_NOACTIVATE,
);
win.setBounds({
x: screenX,
y: screenY,
width: bounds.width,
height: bounds.height,
});
console.log("[desktop-layer] detached");
return true;
}
// 看门狗:explorer 重启会连带销毁挂在桌面树里的窗口,需要检测并交还上层处理
function ensureAttached(win) {
if (!attached) {
// 上次 attach 失败后每 60 秒静默重试一次(例如 explorer 尚未就绪)
if (Date.now() - lastAttachError > 60000) {
try {
attach(win);
lastAttachError = 0;
} catch (e) {
lastAttachError = Date.now();
}
}
return "idle";
}
const hwndAlive = IsWindow(attached.hwnd);
const parentAlive = IsWindow(attached.parent);
if (hwndAlive && !parentAlive) {
// 容器没了但窗口还在:先摘回屏幕,由调用方恢复可操作的浮动窗口。
restore(win);
lastAttachError = 0;
return "container-lost";
}
if (!hwndAlive) {
// 窗口被连带销毁:main 进程会收到 closed 并自动重建
attached = null;
return "window-destroyed";
}
return "ok";
}
function restore(win) {
const state = attached;
if (!state) return false;
attached = null;
if (!win || win.isDestroyed() || !IsWindow(state.hwnd)) return false;
const parentAlive = IsWindow(state.parent);
const rc = parentAlive
? containerRect(state.parent)
: { left: 0, top: 0, right: 0, bottom: 0 };
const bounds = win.getBounds();
const screenX = parentAlive ? bounds.x + rc.left : state.screen.x;
const screenY = parentAlive ? bounds.y + rc.top : state.screen.y;
setChildStyle(state.hwnd, false);
SetParent(state.hwnd, null);
SetWindowPos(
state.hwnd,
HWND_NOTOPMOST,
screenX,
screenY,
bounds.width,
bounds.height,
SWP_NOACTIVATE,
);
if (!win.isDestroyed())
win.setBounds({
x: screenX,
y: screenY,
width: bounds.width,
height: bounds.height,
});
return true;
}
function isAttached() {
return !!attached;
}
module.exports = { attach, detach, ensureAttached, isAttached };