electron-snip 0.0.0-stage → 0.1.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 +74 -0
- package/LICENSE +21 -0
- package/README.md +252 -2
- package/dist/annotations/annotations.cjs +1745 -0
- package/dist/annotations/annotations.cjs.map +1 -0
- package/dist/annotations/annotations.css +159 -0
- package/dist/annotations/annotations.css.map +1 -0
- package/dist/annotations/annotations.d.cts +340 -0
- package/dist/annotations/annotations.d.ts +340 -0
- package/dist/annotations/annotations.js +1722 -0
- package/dist/annotations/annotations.js.map +1 -0
- package/dist/index.cjs +1822 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +187 -0
- package/dist/index.d.ts +187 -0
- package/dist/index.js +1783 -0
- package/dist/index.js.map +1 -0
- package/dist/injector-5YZ2B5KA.js +117 -0
- package/dist/injector-5YZ2B5KA.js.map +1 -0
- package/dist/koffi-TKWNTTDX.js +434 -0
- package/dist/koffi-TKWNTTDX.js.map +1 -0
- package/dist/ocr/ocr.cjs +145 -0
- package/dist/ocr/ocr.cjs.map +1 -0
- package/dist/ocr/ocr.d.cts +3 -0
- package/dist/ocr/ocr.d.ts +3 -0
- package/dist/ocr/ocr.js +110 -0
- package/dist/ocr/ocr.js.map +1 -0
- package/dist/preload.cjs +112 -0
- package/dist/preload.cjs.map +1 -0
- package/dist/record/mp4.cjs +346 -0
- package/dist/record/mp4.cjs.map +1 -0
- package/dist/record/mp4.d.cts +28 -0
- package/dist/record/mp4.d.ts +28 -0
- package/dist/record/mp4.js +323 -0
- package/dist/record/mp4.js.map +1 -0
- package/dist/record/record.cjs +764 -0
- package/dist/record/record.cjs.map +1 -0
- package/dist/record/record.d.cts +111 -0
- package/dist/record/record.d.ts +111 -0
- package/dist/record/record.js +723 -0
- package/dist/record/record.js.map +1 -0
- package/dist/renderer/overlay.css +478 -0
- package/dist/renderer/overlay.css.map +1 -0
- package/dist/renderer/overlay.html +62 -0
- package/dist/renderer/overlay.js +2551 -0
- package/dist/renderer/overlay.js.map +1 -0
- package/dist/renderer/pin.css +283 -0
- package/dist/renderer/pin.css.map +1 -0
- package/dist/renderer/pin.html +23 -0
- package/dist/renderer/pin.js +1941 -0
- package/dist/renderer/pin.js.map +1 -0
- package/dist/renderer/record.css +80 -0
- package/dist/renderer/record.html +26 -0
- package/dist/renderer/record.js +730 -0
- package/dist/renderer/record.js.map +1 -0
- package/dist/renderer/scroll.css +155 -0
- package/dist/renderer/scroll.html +41 -0
- package/dist/renderer/scroll.js +669 -0
- package/dist/renderer/scroll.js.map +1 -0
- package/dist/scroll/chunk-QKAC7M7X.js +118 -0
- package/dist/scroll/chunk-QKAC7M7X.js.map +1 -0
- package/dist/scroll/injector-3L4F6RIJ.js +11 -0
- package/dist/scroll/injector-3L4F6RIJ.js.map +1 -0
- package/dist/scroll/scroll.cjs +779 -0
- package/dist/scroll/scroll.cjs.map +1 -0
- package/dist/scroll/scroll.d.cts +72 -0
- package/dist/scroll/scroll.d.ts +72 -0
- package/dist/scroll/scroll.js +616 -0
- package/dist/scroll/scroll.js.map +1 -0
- package/dist/utils/utils.cjs +62 -0
- package/dist/utils/utils.cjs.map +1 -0
- package/dist/utils/utils.d.cts +61 -0
- package/dist/utils/utils.d.ts +61 -0
- package/dist/utils/utils.js +39 -0
- package/dist/utils/utils.js.map +1 -0
- package/package.json +172 -4
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,1822 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var src_exports = {};
|
|
32
|
+
__export(src_exports, {
|
|
33
|
+
captureDisplays: () => captureDisplays,
|
|
34
|
+
createSnipper: () => createSnipper,
|
|
35
|
+
ensureScreenCaptureAccess: () => ensureScreenCaptureAccess
|
|
36
|
+
});
|
|
37
|
+
module.exports = __toCommonJS(src_exports);
|
|
38
|
+
|
|
39
|
+
// src/main/snipper.ts
|
|
40
|
+
var import_electron7 = require("electron");
|
|
41
|
+
|
|
42
|
+
// src/shared/channels.ts
|
|
43
|
+
var CH = {
|
|
44
|
+
overlayBoot: "snip:overlay:boot",
|
|
45
|
+
overlaySelection: "snip:overlay:selection",
|
|
46
|
+
overlayTeardown: "snip:overlay:teardown",
|
|
47
|
+
overlayReady: "snip:overlay:ready",
|
|
48
|
+
overlayInput: "snip:overlay:input",
|
|
49
|
+
/** 遮罩选了哪个标注工具(主进程广播给其余遮罩,跨屏画图要一致) */
|
|
50
|
+
overlayTool: "snip:overlay:tool",
|
|
51
|
+
/** 遮罩工具条的动作:内置(钉住 / 取消 / 复制 / 保存)或宿主注册的动作 id */
|
|
52
|
+
overlayAction: "snip:overlay:action",
|
|
53
|
+
/** 主 -> 遮罩:把合成数据推给宿主遮罩,就地拼图导出(复制 / 保存) */
|
|
54
|
+
overlayCompose: "snip:overlay:compose",
|
|
55
|
+
/** 遮罩 -> 主:合成好的 PNG,交给主进程进剪贴板 / 存盘 */
|
|
56
|
+
overlayExport: "snip:overlay:export",
|
|
57
|
+
/** 光标所在屏的遮罩请求前台 —— 保证键盘事件归正确的窗口 */
|
|
58
|
+
overlayFocus: "snip:overlay:focus",
|
|
59
|
+
/** 写入剪贴板文本(放大镜复制颜色值) */
|
|
60
|
+
overlayCopyText: "snip:overlay:copyText",
|
|
61
|
+
/** 遮罩把本屏标注(本屏 DIP)回传给主进程,确认时统一换算 */
|
|
62
|
+
overlayShapes: "snip:overlay:shapes",
|
|
63
|
+
/** 遮罩 -> 主:本屏正在拖画的草稿(本屏 DIP),主进程换算后广播给其余遮罩 */
|
|
64
|
+
overlayDraft: "snip:overlay:draft",
|
|
65
|
+
/** 遮罩 -> 主:本屏正在拖动 / 缩放的标注下标(null = 结束),广播时滤掉 */
|
|
66
|
+
overlayInFlight: "snip:overlay:inFlight",
|
|
67
|
+
/** 主 -> 遮罩:其余屏已提交的标注(虚拟屏 DIP) */
|
|
68
|
+
overlayRemoteShapes: "snip:overlay:remoteShapes",
|
|
69
|
+
/** 主 -> 遮罩:其余屏正在拖画的草稿(虚拟屏 DIP;null = 草稿结束) */
|
|
70
|
+
overlayRemoteDraft: "snip:overlay:remoteDraft",
|
|
71
|
+
/** 文字输入框开合 —— 打字时主进程别把 Enter/Esc 吃掉 */
|
|
72
|
+
overlayEditState: "snip:overlay:editState",
|
|
73
|
+
/** 主 -> 遮罩:通用结果面板(宿主动作的 loading / 结果 / 错误) */
|
|
74
|
+
overlayPanel: "snip:overlay:panel",
|
|
75
|
+
/** 长图:主 -> 控制条 单帧画面 */
|
|
76
|
+
scrollFrame: "snip:scroll:frame",
|
|
77
|
+
/** 长图:控制条 -> 主 动作(开始 / 滚动 / 收场 / 导出) */
|
|
78
|
+
scrollAction: "snip:scroll:action",
|
|
79
|
+
/** 长图:控制条 -> 主 -> 小地图 快照 */
|
|
80
|
+
scrollMinimap: "snip:scroll:minimap",
|
|
81
|
+
/** 录制:控制条 -> 主 启动(取得参数,主进程随即开始抓帧) */
|
|
82
|
+
recordBoot: "snip:record:boot",
|
|
83
|
+
/** 录制:控制条 -> 主 动作(暂停 / 继续 / 停止 / 取消) */
|
|
84
|
+
recordAction: "snip:record:action",
|
|
85
|
+
/** 录制:主 -> 控制条 单帧画面 */
|
|
86
|
+
recordFrame: "snip:record:frame",
|
|
87
|
+
/** 录制:主 -> 控制条 状态刷新 */
|
|
88
|
+
recordStatus: "snip:record:status",
|
|
89
|
+
/** 录制:主 -> 控制条 开始编码 */
|
|
90
|
+
recordEncode: "snip:record:encode",
|
|
91
|
+
/** 录制:控制条 -> 主 编码结果 */
|
|
92
|
+
recordDone: "snip:record:done",
|
|
93
|
+
pinBoot: "snip:pin:boot",
|
|
94
|
+
pinReady: "snip:pin:ready",
|
|
95
|
+
pinAction: "snip:pin:action"
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
// src/shared/geometry.ts
|
|
99
|
+
function unionBounds(displays) {
|
|
100
|
+
let minX = Infinity;
|
|
101
|
+
let minY = Infinity;
|
|
102
|
+
let maxX = -Infinity;
|
|
103
|
+
let maxY = -Infinity;
|
|
104
|
+
for (const d of displays) {
|
|
105
|
+
const b = d.bounds;
|
|
106
|
+
if (b.x < minX) minX = b.x;
|
|
107
|
+
if (b.y < minY) minY = b.y;
|
|
108
|
+
if (b.x + b.width > maxX) maxX = b.x + b.width;
|
|
109
|
+
if (b.y + b.height > maxY) maxY = b.y + b.height;
|
|
110
|
+
}
|
|
111
|
+
return { x: minX, y: minY, width: maxX - minX, height: maxY - minY };
|
|
112
|
+
}
|
|
113
|
+
function intersects(a, b) {
|
|
114
|
+
return a.x < b.x + b.width && b.x < a.x + a.width && a.y < b.y + b.height && b.y < a.y + a.height;
|
|
115
|
+
}
|
|
116
|
+
function resolveOutScale(shots, selection) {
|
|
117
|
+
let out = 0;
|
|
118
|
+
for (const shot of shots) {
|
|
119
|
+
if (!intersects(shot.bounds, selection)) continue;
|
|
120
|
+
out = Math.max(out, shot.scaleX, shot.scaleY);
|
|
121
|
+
}
|
|
122
|
+
return out > 0 ? out : 1;
|
|
123
|
+
}
|
|
124
|
+
function displayAt(displays, p) {
|
|
125
|
+
return displays.find(
|
|
126
|
+
(d) => p.x >= d.bounds.x && p.x < d.bounds.x + d.bounds.width && p.y >= d.bounds.y && p.y < d.bounds.y + d.bounds.height
|
|
127
|
+
) ?? displays[0];
|
|
128
|
+
}
|
|
129
|
+
function bestRegionOnDisplay(shots, sel) {
|
|
130
|
+
let bestIndex = -1;
|
|
131
|
+
let bestRegion = null;
|
|
132
|
+
let bestArea = 0;
|
|
133
|
+
shots.forEach((shot, index) => {
|
|
134
|
+
const b = shot.bounds;
|
|
135
|
+
const x0 = Math.max(sel.x, b.x);
|
|
136
|
+
const y0 = Math.max(sel.y, b.y);
|
|
137
|
+
const x1 = Math.min(sel.x + sel.width, b.x + b.width);
|
|
138
|
+
const y1 = Math.min(sel.y + sel.height, b.y + b.height);
|
|
139
|
+
const area = Math.max(0, x1 - x0) * Math.max(0, y1 - y0);
|
|
140
|
+
if (area > bestArea) {
|
|
141
|
+
bestArea = area;
|
|
142
|
+
bestIndex = index;
|
|
143
|
+
bestRegion = { x: x0, y: y0, width: x1 - x0, height: y1 - y0 };
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
return bestRegion ? { index: bestIndex, region: bestRegion } : null;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// src/shared/selection.ts
|
|
150
|
+
var HANDLE_IDS = ["nw", "n", "ne", "e", "se", "s", "sw", "w"];
|
|
151
|
+
var HANDLE_HIT_DIP = 12;
|
|
152
|
+
var MIN_EDGE = 1;
|
|
153
|
+
function handlePoint(rect, id) {
|
|
154
|
+
const { x, y, width, height } = rect;
|
|
155
|
+
const cx = x + width / 2;
|
|
156
|
+
const cy = y + height / 2;
|
|
157
|
+
switch (id) {
|
|
158
|
+
case "nw":
|
|
159
|
+
return { x, y };
|
|
160
|
+
case "n":
|
|
161
|
+
return { x: cx, y };
|
|
162
|
+
case "ne":
|
|
163
|
+
return { x: x + width, y };
|
|
164
|
+
case "e":
|
|
165
|
+
return { x: x + width, y: cy };
|
|
166
|
+
case "se":
|
|
167
|
+
return { x: x + width, y: y + height };
|
|
168
|
+
case "s":
|
|
169
|
+
return { x: cx, y: y + height };
|
|
170
|
+
case "sw":
|
|
171
|
+
return { x, y: y + height };
|
|
172
|
+
case "w":
|
|
173
|
+
return { x, y: cy };
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
function hitHandle(rect, p, threshold = HANDLE_HIT_DIP) {
|
|
177
|
+
if (rect.width <= 0 || rect.height <= 0) return null;
|
|
178
|
+
for (const id of HANDLE_IDS) {
|
|
179
|
+
const h = handlePoint(rect, id);
|
|
180
|
+
if (Math.hypot(h.x - p.x, h.y - p.y) <= threshold) return id;
|
|
181
|
+
}
|
|
182
|
+
return null;
|
|
183
|
+
}
|
|
184
|
+
var BORDER_HIT_DIP = 6;
|
|
185
|
+
function hitBorder(rect, p, threshold = BORDER_HIT_DIP) {
|
|
186
|
+
if (rect.width <= 0 || rect.height <= 0) return null;
|
|
187
|
+
const nearL = Math.abs(p.x - rect.x) <= threshold;
|
|
188
|
+
const nearR = Math.abs(p.x - (rect.x + rect.width)) <= threshold;
|
|
189
|
+
const nearT = Math.abs(p.y - rect.y) <= threshold;
|
|
190
|
+
const nearB = Math.abs(p.y - (rect.y + rect.height)) <= threshold;
|
|
191
|
+
if (!nearL && !nearR && !nearT && !nearB) return null;
|
|
192
|
+
const inX = p.x >= rect.x && p.x <= rect.x + rect.width;
|
|
193
|
+
const inY = p.y >= rect.y && p.y <= rect.y + rect.height;
|
|
194
|
+
if (nearT && nearL) return "nw";
|
|
195
|
+
if (nearT && nearR) return "ne";
|
|
196
|
+
if (nearB && nearL) return "sw";
|
|
197
|
+
if (nearB && nearR) return "se";
|
|
198
|
+
if (nearT && inX) return "n";
|
|
199
|
+
if (nearB && inX) return "s";
|
|
200
|
+
if (nearL && inY) return "w";
|
|
201
|
+
if (nearR && inY) return "e";
|
|
202
|
+
return null;
|
|
203
|
+
}
|
|
204
|
+
function pointInRect(rect, p) {
|
|
205
|
+
return p.x >= rect.x && p.x <= rect.x + rect.width && p.y >= rect.y && p.y <= rect.y + rect.height;
|
|
206
|
+
}
|
|
207
|
+
function moveRect(origin, start, cursor) {
|
|
208
|
+
return {
|
|
209
|
+
...origin,
|
|
210
|
+
x: origin.x + (cursor.x - start.x),
|
|
211
|
+
y: origin.y + (cursor.y - start.y)
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
function clampRect(rect, bounds) {
|
|
215
|
+
const x = Math.max(bounds.x, Math.min(rect.x, bounds.x + bounds.width - rect.width));
|
|
216
|
+
const y = Math.max(bounds.y, Math.min(rect.y, bounds.y + bounds.height - rect.height));
|
|
217
|
+
return { ...rect, x, y };
|
|
218
|
+
}
|
|
219
|
+
function resizeRect(origin, handle, cursor) {
|
|
220
|
+
let left = origin.x;
|
|
221
|
+
let top = origin.y;
|
|
222
|
+
let right = origin.x + origin.width;
|
|
223
|
+
let bottom = origin.y + origin.height;
|
|
224
|
+
if (handle.includes("w")) left = Math.min(cursor.x, right - MIN_EDGE);
|
|
225
|
+
if (handle.includes("e")) right = Math.max(cursor.x, left + MIN_EDGE);
|
|
226
|
+
if (handle.includes("n")) top = Math.min(cursor.y, bottom - MIN_EDGE);
|
|
227
|
+
if (handle.includes("s")) bottom = Math.max(cursor.y, top + MIN_EDGE);
|
|
228
|
+
return { x: left, y: top, width: right - left, height: bottom - top };
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// src/shared/pin-layout.ts
|
|
232
|
+
var PIN_SHADOW_PAD = 16;
|
|
233
|
+
var PIN_MIN_WIDTH = 120;
|
|
234
|
+
var PIN_MIN_HEIGHT = 90;
|
|
235
|
+
function computePinLayout(sel, displays) {
|
|
236
|
+
const imageW = Math.max(PIN_MIN_WIDTH, Math.round(sel.width));
|
|
237
|
+
const imageH = Math.max(PIN_MIN_HEIGHT, Math.round(sel.height));
|
|
238
|
+
const x = Math.round(sel.x);
|
|
239
|
+
const y = Math.round(sel.y);
|
|
240
|
+
const midY = y + imageH / 2;
|
|
241
|
+
const midX = x + imageW / 2;
|
|
242
|
+
const dL = displayAt(displays, { x, y: midY }).bounds;
|
|
243
|
+
const dR = displayAt(displays, { x: x + imageW, y: midY }).bounds;
|
|
244
|
+
const dT = displayAt(displays, { x: midX, y }).bounds;
|
|
245
|
+
const dB = displayAt(displays, { x: midX, y: y + imageH }).bounds;
|
|
246
|
+
const pad = (room) => Math.max(0, Math.min(PIN_SHADOW_PAD, Math.round(room)));
|
|
247
|
+
const padding = {
|
|
248
|
+
left: pad(x - dL.x),
|
|
249
|
+
right: pad(dR.x + dR.width - (x + imageW)),
|
|
250
|
+
top: pad(y - dT.y),
|
|
251
|
+
bottom: pad(dB.y + dB.height - (y + imageH))
|
|
252
|
+
};
|
|
253
|
+
return {
|
|
254
|
+
padding,
|
|
255
|
+
bounds: {
|
|
256
|
+
x: x - padding.left,
|
|
257
|
+
y: y - padding.top,
|
|
258
|
+
width: imageW + padding.left + padding.right,
|
|
259
|
+
height: imageH + padding.top + padding.bottom
|
|
260
|
+
}
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// src/shared/shape-transform.ts
|
|
265
|
+
function mapShape(shape, dx, dy, k) {
|
|
266
|
+
switch (shape.type) {
|
|
267
|
+
case "arrow":
|
|
268
|
+
case "line":
|
|
269
|
+
return {
|
|
270
|
+
...shape,
|
|
271
|
+
x1: (shape.x1 + dx) * k,
|
|
272
|
+
y1: (shape.y1 + dy) * k,
|
|
273
|
+
x2: (shape.x2 + dx) * k,
|
|
274
|
+
y2: (shape.y2 + dy) * k,
|
|
275
|
+
width: shape.width * k
|
|
276
|
+
};
|
|
277
|
+
case "counter":
|
|
278
|
+
return {
|
|
279
|
+
...shape,
|
|
280
|
+
x: (shape.x + dx) * k,
|
|
281
|
+
y: (shape.y + dy) * k
|
|
282
|
+
};
|
|
283
|
+
case "pen":
|
|
284
|
+
return {
|
|
285
|
+
...shape,
|
|
286
|
+
width: shape.width * k,
|
|
287
|
+
points: shape.points.map((p) => ({
|
|
288
|
+
x: (p.x + dx) * k,
|
|
289
|
+
y: (p.y + dy) * k
|
|
290
|
+
}))
|
|
291
|
+
};
|
|
292
|
+
case "text":
|
|
293
|
+
return {
|
|
294
|
+
...shape,
|
|
295
|
+
x: (shape.x + dx) * k,
|
|
296
|
+
y: (shape.y + dy) * k,
|
|
297
|
+
size: shape.size * k,
|
|
298
|
+
...shape.maxWidth !== void 0 ? { maxWidth: shape.maxWidth * k } : {}
|
|
299
|
+
};
|
|
300
|
+
case "mosaic":
|
|
301
|
+
if (shape.mode === "brush") {
|
|
302
|
+
return {
|
|
303
|
+
...shape,
|
|
304
|
+
radius: shape.radius * k,
|
|
305
|
+
points: shape.points.map((p) => ({
|
|
306
|
+
x: (p.x + dx) * k,
|
|
307
|
+
y: (p.y + dy) * k
|
|
308
|
+
}))
|
|
309
|
+
};
|
|
310
|
+
}
|
|
311
|
+
return {
|
|
312
|
+
...shape,
|
|
313
|
+
x: (shape.x + dx) * k,
|
|
314
|
+
y: (shape.y + dy) * k,
|
|
315
|
+
w: shape.w * k,
|
|
316
|
+
h: shape.h * k
|
|
317
|
+
};
|
|
318
|
+
default:
|
|
319
|
+
return {
|
|
320
|
+
...shape,
|
|
321
|
+
x: (shape.x + dx) * k,
|
|
322
|
+
y: (shape.y + dy) * k,
|
|
323
|
+
w: shape.w * k,
|
|
324
|
+
h: shape.h * k
|
|
325
|
+
};
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
// src/main/capture.ts
|
|
330
|
+
var import_electron = require("electron");
|
|
331
|
+
|
|
332
|
+
// src/main/log.ts
|
|
333
|
+
var logger = console;
|
|
334
|
+
function setLogger(next) {
|
|
335
|
+
logger = next;
|
|
336
|
+
}
|
|
337
|
+
var log = {
|
|
338
|
+
info: (...args) => logger.log(...args),
|
|
339
|
+
warn: (...args) => logger.warn(...args),
|
|
340
|
+
error: (...args) => logger.error(...args)
|
|
341
|
+
};
|
|
342
|
+
function forwardRendererLogs(win, tag) {
|
|
343
|
+
win.webContents.on("console-message", (details) => {
|
|
344
|
+
const { level, message, lineNumber, sourceId } = details;
|
|
345
|
+
if (level === "debug") return;
|
|
346
|
+
log.info(`[${tag}:${level}] ${message} (${sourceId}:${lineNumber})`);
|
|
347
|
+
});
|
|
348
|
+
win.webContents.on("did-fail-load", (_event, errorCode, errorDescription, validatedURL) => {
|
|
349
|
+
log.error(`[${tag}] \u9875\u9762\u52A0\u8F7D\u5931\u8D25 ${errorCode} ${errorDescription} ${validatedURL}`);
|
|
350
|
+
});
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
// src/main/capture.ts
|
|
354
|
+
function toUint8(buf) {
|
|
355
|
+
return new Uint8Array(buf.buffer, buf.byteOffset, buf.byteLength);
|
|
356
|
+
}
|
|
357
|
+
function bgraToRgba(buf) {
|
|
358
|
+
const px = toUint8(buf);
|
|
359
|
+
for (let i = 0; i < px.length; i += 4) {
|
|
360
|
+
const b = px[i];
|
|
361
|
+
px[i] = px[i + 2];
|
|
362
|
+
px[i + 2] = b;
|
|
363
|
+
}
|
|
364
|
+
return px;
|
|
365
|
+
}
|
|
366
|
+
function groupByWant(displays) {
|
|
367
|
+
const groups = /* @__PURE__ */ new Map();
|
|
368
|
+
for (const d of displays) {
|
|
369
|
+
const want = wantSize(d);
|
|
370
|
+
const key = `${want.width}x${want.height}`;
|
|
371
|
+
const list = groups.get(key);
|
|
372
|
+
if (list) list.push(d);
|
|
373
|
+
else groups.set(key, [d]);
|
|
374
|
+
}
|
|
375
|
+
return groups;
|
|
376
|
+
}
|
|
377
|
+
function wantSize(d) {
|
|
378
|
+
return {
|
|
379
|
+
width: Math.max(1, Math.round(d.bounds.width * d.scaleFactor)),
|
|
380
|
+
height: Math.max(1, Math.round(d.bounds.height * d.scaleFactor))
|
|
381
|
+
};
|
|
382
|
+
}
|
|
383
|
+
function pickSource(sources, display, displays) {
|
|
384
|
+
const byId = sources.find((s) => s.display_id === String(display.id));
|
|
385
|
+
if (byId) return byId;
|
|
386
|
+
if (sources.length !== displays.length) {
|
|
387
|
+
log.warn(`[capture] display_id \u5339\u914D\u5931\u8D25\u4E14\u6570\u91CF\u4E0D\u4E00\u81F4\uFF1Asources=${sources.length} displays=${displays.length}`);
|
|
388
|
+
return null;
|
|
389
|
+
}
|
|
390
|
+
const idx = displays.findIndex((d) => d.id === display.id);
|
|
391
|
+
log.warn(`[capture] display ${display.id} \u7684 display_id \u4E3A\u7A7A\uFF0C\u9000\u5316\u4E3A\u4E0B\u6807 ${idx}`);
|
|
392
|
+
return sources[idx] ?? null;
|
|
393
|
+
}
|
|
394
|
+
async function captureDisplays(targets, all, silent = false) {
|
|
395
|
+
const groups = groupByWant(targets);
|
|
396
|
+
const shots = [];
|
|
397
|
+
for (const [, group] of groups) {
|
|
398
|
+
const want = wantSize(group[0]);
|
|
399
|
+
const sources = await import_electron.desktopCapturer.getSources({
|
|
400
|
+
types: ["screen"],
|
|
401
|
+
thumbnailSize: want,
|
|
402
|
+
fetchWindowIcons: false
|
|
403
|
+
});
|
|
404
|
+
for (const display of group) {
|
|
405
|
+
const source = pickSource(sources, display, all);
|
|
406
|
+
if (!source) {
|
|
407
|
+
throw new Error(`\u65E0\u6CD5\u4E3A\u663E\u793A\u5668 ${display.id} \u627E\u5230\u5BF9\u5E94\u7684\u622A\u56FE\u6E90`);
|
|
408
|
+
}
|
|
409
|
+
const size = source.thumbnail.getSize();
|
|
410
|
+
const scaleX = size.width / display.bounds.width;
|
|
411
|
+
const scaleY = size.height / display.bounds.height;
|
|
412
|
+
if (!silent && Math.abs(scaleX - scaleY) > 0.02) {
|
|
413
|
+
log.warn(
|
|
414
|
+
`[capture] display ${display.id} \u7684 X/Y \u6BD4\u4F8B\u504F\u5DEE\u8FC7\u5927\uFF1AscaleX=${scaleX.toFixed(4)} scaleY=${scaleY.toFixed(4)}`
|
|
415
|
+
);
|
|
416
|
+
}
|
|
417
|
+
if (!silent) {
|
|
418
|
+
log.info(
|
|
419
|
+
`[capture] display=${display.id} bounds=${display.bounds.x},${display.bounds.y} ${display.bounds.width}x${display.bounds.height} scaleFactor=${display.scaleFactor} want=${want.width}x${want.height} actual=${size.width}x${size.height} ratio=${scaleX.toFixed(4)}/${scaleY.toFixed(4)} display_id="${source.display_id}"`
|
|
420
|
+
);
|
|
421
|
+
}
|
|
422
|
+
const pixels = bgraToRgba(source.thumbnail.toBitmap());
|
|
423
|
+
if (pixels.length !== size.width * size.height * 4) {
|
|
424
|
+
throw new Error(`display ${display.id} \u4F4D\u56FE\u5C3A\u5BF8\u4E0D\u7B26\uFF1A${pixels.length} != ${size.width}x${size.height}x4`);
|
|
425
|
+
}
|
|
426
|
+
shots.push({
|
|
427
|
+
displayId: display.id,
|
|
428
|
+
bounds: { ...display.bounds },
|
|
429
|
+
scaleX,
|
|
430
|
+
scaleY,
|
|
431
|
+
imageWidth: size.width,
|
|
432
|
+
imageHeight: size.height,
|
|
433
|
+
pixels
|
|
434
|
+
});
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
if (shots.length !== targets.length) {
|
|
438
|
+
throw new Error(`\u622A\u56FE\u6570\u91CF\u4E0E\u8BF7\u6C42\u7684\u663E\u793A\u5668\u6570\u91CF\u4E0D\u4E00\u81F4\uFF1A${shots.length} != ${targets.length}`);
|
|
439
|
+
}
|
|
440
|
+
return shots;
|
|
441
|
+
}
|
|
442
|
+
function displayAtCursor(displays) {
|
|
443
|
+
const p = import_electron.screen.getCursorScreenPoint();
|
|
444
|
+
return displays.find(
|
|
445
|
+
(d) => p.x >= d.bounds.x && p.x < d.bounds.x + d.bounds.width && p.y >= d.bounds.y && p.y < d.bounds.y + d.bounds.height
|
|
446
|
+
) ?? displays[0];
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
// src/main/permission.ts
|
|
450
|
+
var import_electron2 = require("electron");
|
|
451
|
+
var SCREEN_CAPTURE_SETTINGS_URL = "x-apple.systempreferences:com.apple.preference.security?Privacy_ScreenCapture";
|
|
452
|
+
var RESTART_LABEL = "\u6211\u5DF2\u6388\u6743\uFF0C\u91CD\u542F\u5E94\u7528";
|
|
453
|
+
var SETTINGS_LABEL = "\u6253\u5F00\u7CFB\u7EDF\u8BBE\u7F6E";
|
|
454
|
+
var LATER_LABEL = "\u6682\u4E0D";
|
|
455
|
+
var prompting = false;
|
|
456
|
+
var probed = false;
|
|
457
|
+
var guided = false;
|
|
458
|
+
async function ensureScreenCaptureAccess() {
|
|
459
|
+
if (process.platform !== "darwin") return true;
|
|
460
|
+
const status = import_electron2.systemPreferences.getMediaAccessStatus("screen");
|
|
461
|
+
log.info(`[permission] \u5C4F\u5E55\u5F55\u5236\u6743\u9650\u72B6\u6001\uFF1A${status}`);
|
|
462
|
+
if (status === "granted") {
|
|
463
|
+
return true;
|
|
464
|
+
}
|
|
465
|
+
if (!probed) {
|
|
466
|
+
probed = true;
|
|
467
|
+
await import_electron2.desktopCapturer.getSources({ types: ["screen"] }).catch((err) => log.warn("[permission] \u6293\u5C4F\u63A2\u6D4B\u5931\u8D25", err));
|
|
468
|
+
if (import_electron2.systemPreferences.getMediaAccessStatus("screen") === "granted") {
|
|
469
|
+
log.info("[permission] \u6388\u6743\u540E\u72B6\u6001\u5DF2\u53D8\u4E3A granted");
|
|
470
|
+
return true;
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
await promptScreenPermission();
|
|
474
|
+
return false;
|
|
475
|
+
}
|
|
476
|
+
async function promptScreenPermission() {
|
|
477
|
+
if (prompting) return;
|
|
478
|
+
prompting = true;
|
|
479
|
+
try {
|
|
480
|
+
const buttons = guided ? [RESTART_LABEL, SETTINGS_LABEL, LATER_LABEL] : [SETTINGS_LABEL, LATER_LABEL];
|
|
481
|
+
const { response } = await import_electron2.dialog.showMessageBox({
|
|
482
|
+
type: "info",
|
|
483
|
+
message: "\u622A\u56FE\u9700\u8981\u300C\u5C4F\u5E55\u5F55\u5236\u300D\u6743\u9650",
|
|
484
|
+
detail: guided ? "\u82E5\u5DF2\u5728\u300C\u9690\u79C1\u4E0E\u5B89\u5168\u6027 \u2192 \u5F55\u5C4F\u4E0E\u7CFB\u7EDF\u5F55\u97F3\u300D\u4E2D\u52FE\u9009\u672C\u5E94\u7528\uFF0C\u8BF7\u91CD\u542F\u5E94\u7528\u4F7F\u5176\u751F\u6548\u3002\n\n\u82E5\u5C1A\u672A\u52FE\u9009\uFF0C\u8BF7\u5148\u6253\u5F00\u7CFB\u7EDF\u8BBE\u7F6E\u5B8C\u6210\u52FE\u9009\u3002" : "macOS \u8981\u6C42\u5355\u72EC\u6388\u6743\u624D\u80FD\u6355\u6349\u5C4F\u5E55\u5185\u5BB9\uFF0C\u672A\u6388\u6743\u65F6\u53EA\u80FD\u622A\u5230\u684C\u9762\u58C1\u7EB8\u3002\n\n\u8BF7\u5728\u300C\u9690\u79C1\u4E0E\u5B89\u5168\u6027 \u2192 \u5F55\u5C4F\u4E0E\u7CFB\u7EDF\u5F55\u97F3\u300D\u4E2D\u52FE\u9009\u672C\u5E94\u7528\uFF0C\u7136\u540E\u91CD\u542F\u5E94\u7528\u751F\u6548\u3002\n\u82E5\u662F\u4ECE\u7EC8\u7AEF\u542F\u52A8\u7684\u5F00\u53D1\u5B9E\u4F8B\uFF0CmacOS \u4F1A\u628A\u6743\u9650\u5F52\u5C5E\u5230\u7EC8\u7AEF\u672C\u8EAB \u2014\u2014 \u9700\u8981\u52FE\u9009\u7684\u662F\u542F\u52A8\u5B83\u7684\u7EC8\u7AEF\u5E94\u7528\uFF08\u5982 Warp / iTerm2 / Terminal\uFF09\u3002",
|
|
485
|
+
buttons,
|
|
486
|
+
defaultId: 0,
|
|
487
|
+
cancelId: buttons.length - 1
|
|
488
|
+
});
|
|
489
|
+
const picked = buttons[response];
|
|
490
|
+
if (picked === RESTART_LABEL) {
|
|
491
|
+
log.info("[permission] \u7528\u6237\u9009\u62E9\u91CD\u542F\u5E94\u7528\u4EE5\u751F\u6548\u6388\u6743");
|
|
492
|
+
import_electron2.app.relaunch();
|
|
493
|
+
import_electron2.app.exit(0);
|
|
494
|
+
return;
|
|
495
|
+
}
|
|
496
|
+
if (picked === SETTINGS_LABEL) {
|
|
497
|
+
guided = true;
|
|
498
|
+
await import_electron2.shell.openExternal(SCREEN_CAPTURE_SETTINGS_URL);
|
|
499
|
+
}
|
|
500
|
+
} finally {
|
|
501
|
+
prompting = false;
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
// src/main/windows.ts
|
|
506
|
+
var import_electron3 = require("electron");
|
|
507
|
+
var import_get_windows = require("get-windows");
|
|
508
|
+
async function listWindowRects(excludePid) {
|
|
509
|
+
const wins = await (0, import_get_windows.openWindows)({ accessibilityPermission: false, screenRecordingPermission: false });
|
|
510
|
+
return wins.filter((w) => w.owner.processId !== excludePid && w.bounds.width > 0 && w.bounds.height > 0).map((w) => {
|
|
511
|
+
if (process.platform !== "win32") {
|
|
512
|
+
return { owner: w.owner.name, rect: { ...w.bounds } };
|
|
513
|
+
}
|
|
514
|
+
const p1 = import_electron3.screen.screenToDipPoint({ x: w.bounds.x, y: w.bounds.y });
|
|
515
|
+
const p2 = import_electron3.screen.screenToDipPoint({ x: w.bounds.x + w.bounds.width, y: w.bounds.y + w.bounds.height });
|
|
516
|
+
return { owner: w.owner.name, rect: { x: p1.x, y: p1.y, width: p2.x - p1.x, height: p2.y - p1.y } };
|
|
517
|
+
});
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
// src/main/overlay-window.ts
|
|
521
|
+
var import_electron4 = require("electron");
|
|
522
|
+
var import_node_path = __toESM(require("path"), 1);
|
|
523
|
+
var OVERLAY_LEVEL = "screen-saver";
|
|
524
|
+
var OverlayPool = class {
|
|
525
|
+
constructor(opts, hooks) {
|
|
526
|
+
this.opts = opts;
|
|
527
|
+
this.hooks = hooks;
|
|
528
|
+
}
|
|
529
|
+
opts;
|
|
530
|
+
hooks;
|
|
531
|
+
cache = /* @__PURE__ */ new Map();
|
|
532
|
+
/** 取一块屏的遮罩窗口:有缓存就复用(重设 bounds + reload),没有才新建。 */
|
|
533
|
+
acquire(shot) {
|
|
534
|
+
const b = shot.bounds;
|
|
535
|
+
const cached = this.cache.get(shot.displayId);
|
|
536
|
+
if (cached && !cached.isDestroyed()) {
|
|
537
|
+
cached.setBounds({ x: b.x, y: b.y, width: b.width, height: b.height });
|
|
538
|
+
const load = cached.webContents.loadFile(import_node_path.default.join(this.opts.rendererDir, "overlay.html")).catch((err) => {
|
|
539
|
+
log.error(`[overlay:${shot.displayId}] \u590D\u7528\u7A97\u53E3\u91CD\u8F7D\u5931\u8D25`, err);
|
|
540
|
+
});
|
|
541
|
+
return { win: cached, load };
|
|
542
|
+
}
|
|
543
|
+
const win = this.create(shot);
|
|
544
|
+
this.cache.set(shot.displayId, win);
|
|
545
|
+
return { win, load: win.loadFile(import_node_path.default.join(this.opts.rendererDir, "overlay.html")) };
|
|
546
|
+
}
|
|
547
|
+
/** 拔掉显示器 / 窗口已销毁的缓存项。 */
|
|
548
|
+
prune(liveDisplayIds) {
|
|
549
|
+
for (const [id, win] of this.cache) {
|
|
550
|
+
if (win.isDestroyed()) {
|
|
551
|
+
this.cache.delete(id);
|
|
552
|
+
continue;
|
|
553
|
+
}
|
|
554
|
+
if (!liveDisplayIds.has(id)) {
|
|
555
|
+
this.cache.delete(id);
|
|
556
|
+
win.destroy();
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
/** 该 webContents 是不是本池创建的遮罩窗口 —— IPC 发送方校验用。 */
|
|
561
|
+
owns(contentsId) {
|
|
562
|
+
for (const win of this.cache.values()) {
|
|
563
|
+
if (!win.isDestroyed() && win.webContents.id === contentsId) return true;
|
|
564
|
+
}
|
|
565
|
+
return false;
|
|
566
|
+
}
|
|
567
|
+
destroyAll() {
|
|
568
|
+
for (const win of this.cache.values()) {
|
|
569
|
+
if (!win.isDestroyed()) win.destroy();
|
|
570
|
+
}
|
|
571
|
+
this.cache.clear();
|
|
572
|
+
}
|
|
573
|
+
create(shot) {
|
|
574
|
+
const b = shot.bounds;
|
|
575
|
+
const win = new import_electron4.BrowserWindow({
|
|
576
|
+
x: b.x,
|
|
577
|
+
y: b.y,
|
|
578
|
+
width: b.width,
|
|
579
|
+
height: b.height,
|
|
580
|
+
frame: false,
|
|
581
|
+
show: false,
|
|
582
|
+
resizable: false,
|
|
583
|
+
movable: false,
|
|
584
|
+
// thickFrame:false —— 否则无边框窗口会吃到 Aero Snap / 隐形边框,bounds 会偏几像素
|
|
585
|
+
thickFrame: false,
|
|
586
|
+
hasShadow: false,
|
|
587
|
+
skipTaskbar: true,
|
|
588
|
+
// 不用 transparent:true(Windows 上不可靠);画布自己铺截图做背景
|
|
589
|
+
backgroundColor: "#000000",
|
|
590
|
+
fullscreenable: false,
|
|
591
|
+
// macOS:AppKit 会在窗口首次上屏时把 bounds 约束进工作区(菜单栏以下),
|
|
592
|
+
// 遮罩被整体下移一条菜单栏的高度 —— 真实菜单栏从遮罩顶上露出来,和抓屏
|
|
593
|
+
// 图里的菜单栏叠成两条,且顶部区域选不到。实测(CGWindowList + 探针)只有
|
|
594
|
+
// 这个开关能跳过该约束;show 后再 setBounds / type:'panel' 都无效。遮罩
|
|
595
|
+
// 恰好等于屏幕大小,"larger than screen" 的其余语义用不上,Windows 忽略它。
|
|
596
|
+
enableLargerThanScreen: true,
|
|
597
|
+
focusable: true,
|
|
598
|
+
webPreferences: {
|
|
599
|
+
preload: this.opts.preloadPath,
|
|
600
|
+
contextIsolation: true,
|
|
601
|
+
nodeIntegration: false,
|
|
602
|
+
// preload 只用 contextBridge / ipcRenderer,页面是 IIFE 产物不碰 Node ——
|
|
603
|
+
// 沙箱开得起:渲染进程被攻破也拿不到 Node 能力
|
|
604
|
+
sandbox: true
|
|
605
|
+
}
|
|
606
|
+
});
|
|
607
|
+
win.setAlwaysOnTop(true, OVERLAY_LEVEL);
|
|
608
|
+
win.setBounds({ x: b.x, y: b.y, width: b.width, height: b.height });
|
|
609
|
+
win.setContentProtection(true);
|
|
610
|
+
forwardRendererLogs(win, `overlay:${shot.displayId}`);
|
|
611
|
+
win.webContents.on("render-process-gone", (_event, details) => {
|
|
612
|
+
log.error(`[overlay:${shot.displayId}] \u6E32\u67D3\u8FDB\u7A0B\u9000\u51FA reason=${details.reason} exitCode=${details.exitCode}`);
|
|
613
|
+
this.hooks.onCrashed(shot.displayId);
|
|
614
|
+
});
|
|
615
|
+
win.on("unresponsive", () => {
|
|
616
|
+
log.warn(`[overlay:${shot.displayId}] \u7A97\u53E3\u65E0\u54CD\u5E94`);
|
|
617
|
+
});
|
|
618
|
+
const contentsId = win.webContents.id;
|
|
619
|
+
win.webContents.on("before-input-event", (event, input) => {
|
|
620
|
+
if (input.type !== "keyDown") return;
|
|
621
|
+
const kind = input.key === "Escape" ? "escape" : input.key === "Enter" || input.key === "NumpadEnter" ? "enter" : null;
|
|
622
|
+
if (kind && this.hooks.onKey(win.webContents, kind)) event.preventDefault();
|
|
623
|
+
});
|
|
624
|
+
win.on("closed", () => {
|
|
625
|
+
for (const [id, w] of this.cache) {
|
|
626
|
+
if (w === win) this.cache.delete(id);
|
|
627
|
+
}
|
|
628
|
+
this.hooks.onClosed(contentsId);
|
|
629
|
+
});
|
|
630
|
+
return win;
|
|
631
|
+
}
|
|
632
|
+
};
|
|
633
|
+
|
|
634
|
+
// src/main/pin-window.ts
|
|
635
|
+
var import_electron5 = require("electron");
|
|
636
|
+
var import_node_path2 = __toESM(require("path"), 1);
|
|
637
|
+
var PinRegistry = class {
|
|
638
|
+
constructor(opts) {
|
|
639
|
+
this.opts = opts;
|
|
640
|
+
}
|
|
641
|
+
opts;
|
|
642
|
+
/** 全部存活的贴图窗口 */
|
|
643
|
+
pins = /* @__PURE__ */ new Set();
|
|
644
|
+
/** 处于鼠标点击穿透状态的贴图窗口(isIgnoreMouseEvents 无查询接口) */
|
|
645
|
+
mouseIgnored = /* @__PURE__ */ new WeakSet();
|
|
646
|
+
create(bounds) {
|
|
647
|
+
const want = {
|
|
648
|
+
x: Math.round(bounds.x),
|
|
649
|
+
y: Math.round(bounds.y),
|
|
650
|
+
width: Math.max(120, Math.round(bounds.width)),
|
|
651
|
+
height: Math.max(90, Math.round(bounds.height))
|
|
652
|
+
};
|
|
653
|
+
const win = new import_electron5.BrowserWindow({
|
|
654
|
+
...want,
|
|
655
|
+
frame: false,
|
|
656
|
+
show: false,
|
|
657
|
+
resizable: false,
|
|
658
|
+
thickFrame: false,
|
|
659
|
+
hasShadow: false,
|
|
660
|
+
skipTaskbar: true,
|
|
661
|
+
transparent: true,
|
|
662
|
+
backgroundColor: "#00000000",
|
|
663
|
+
webPreferences: {
|
|
664
|
+
preload: this.opts.preloadPath,
|
|
665
|
+
contextIsolation: true,
|
|
666
|
+
nodeIntegration: false,
|
|
667
|
+
// preload 只用 contextBridge / ipcRenderer,页面是 IIFE 产物不碰 Node ——
|
|
668
|
+
// 沙箱开得起:渲染进程被攻破也拿不到 Node 能力
|
|
669
|
+
sandbox: true
|
|
670
|
+
}
|
|
671
|
+
});
|
|
672
|
+
win.setBounds(want);
|
|
673
|
+
log.info(
|
|
674
|
+
`[pin-window] \u8BF7\u6C42 ${want.width}x${want.height}@${want.x},${want.y} -> \u5B9E\u9645 ${JSON.stringify(win.getBounds())}`
|
|
675
|
+
);
|
|
676
|
+
win.setAlwaysOnTop(true, "floating");
|
|
677
|
+
forwardRendererLogs(win, "pin");
|
|
678
|
+
win.webContents.on("render-process-gone", (_e, details) => {
|
|
679
|
+
log.error(`[pin-window] \u6E32\u67D3\u8FDB\u7A0B\u9000\u51FA reason=${details.reason} exitCode=${details.exitCode}`);
|
|
680
|
+
});
|
|
681
|
+
win.on("unresponsive", () => log.warn("[pin-window] \u7A97\u53E3\u65E0\u54CD\u5E94"));
|
|
682
|
+
win.on("closed", () => {
|
|
683
|
+
log.info("[pin-window] closed");
|
|
684
|
+
this.pins.delete(win);
|
|
685
|
+
});
|
|
686
|
+
this.pins.add(win);
|
|
687
|
+
return win;
|
|
688
|
+
}
|
|
689
|
+
/** 该 webContents 是不是本注册表创建的贴图窗口 —— IPC 发送方校验用。 */
|
|
690
|
+
owns(contentsId) {
|
|
691
|
+
for (const win of this.pins) {
|
|
692
|
+
if (!win.isDestroyed() && win.webContents.id === contentsId) return true;
|
|
693
|
+
}
|
|
694
|
+
return false;
|
|
695
|
+
}
|
|
696
|
+
load(win) {
|
|
697
|
+
return win.loadFile(import_node_path2.default.join(this.opts.rendererDir, "pin.html"));
|
|
698
|
+
}
|
|
699
|
+
closeAll() {
|
|
700
|
+
let n = 0;
|
|
701
|
+
for (const win of this.pins) {
|
|
702
|
+
if (win.isDestroyed()) continue;
|
|
703
|
+
win.close();
|
|
704
|
+
n++;
|
|
705
|
+
}
|
|
706
|
+
this.pins.clear();
|
|
707
|
+
return n;
|
|
708
|
+
}
|
|
709
|
+
setIgnoreAll(ignore) {
|
|
710
|
+
for (const win of this.pins) {
|
|
711
|
+
if (win.isDestroyed()) continue;
|
|
712
|
+
if (ignore) this.mouseIgnored.add(win);
|
|
713
|
+
else this.mouseIgnored.delete(win);
|
|
714
|
+
win.setIgnoreMouseEvents(ignore, { forward: ignore });
|
|
715
|
+
}
|
|
716
|
+
}
|
|
717
|
+
/** Ctrl+T:切换该贴图的鼠标点击穿透,返回切换后的状态。 */
|
|
718
|
+
toggleMouseIgnore(win) {
|
|
719
|
+
const next = !this.mouseIgnored.has(win);
|
|
720
|
+
if (next) this.mouseIgnored.add(win);
|
|
721
|
+
else this.mouseIgnored.delete(win);
|
|
722
|
+
win.setIgnoreMouseEvents(next, { forward: next });
|
|
723
|
+
return next;
|
|
724
|
+
}
|
|
725
|
+
/** 按钮 / 滚轮缩放:窗口尺寸 × factor,光标下的点保持不动。 */
|
|
726
|
+
zoom(win, factor) {
|
|
727
|
+
const b = win.getBounds();
|
|
728
|
+
const cursor = import_electron5.screen.getCursorScreenPoint();
|
|
729
|
+
const f = Math.min(4, Math.max(0.25, factor));
|
|
730
|
+
win.setBounds({
|
|
731
|
+
x: Math.round(cursor.x - (cursor.x - b.x) * f),
|
|
732
|
+
y: Math.round(cursor.y - (cursor.y - b.y) * f),
|
|
733
|
+
width: Math.round(b.width * f),
|
|
734
|
+
height: Math.round(b.height * f)
|
|
735
|
+
});
|
|
736
|
+
}
|
|
737
|
+
opacity(win, delta) {
|
|
738
|
+
win.setOpacity(Math.min(1, Math.max(0.15, win.getOpacity() + delta)));
|
|
739
|
+
}
|
|
740
|
+
};
|
|
741
|
+
|
|
742
|
+
// src/main/export.ts
|
|
743
|
+
var import_electron6 = require("electron");
|
|
744
|
+
var import_node_fs = require("fs");
|
|
745
|
+
var import_node_path3 = __toESM(require("path"), 1);
|
|
746
|
+
|
|
747
|
+
// src/shared/bytes.ts
|
|
748
|
+
function toBlob(bytes, type) {
|
|
749
|
+
const copy = new Uint8Array(bytes.byteLength);
|
|
750
|
+
copy.set(bytes);
|
|
751
|
+
return new Blob([copy.buffer], { type });
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
// src/main/export.ts
|
|
755
|
+
async function copyPng(png) {
|
|
756
|
+
if (png.byteLength === 0) throw new Error("\u56FE\u7247\u6570\u636E\u4E3A\u7A7A");
|
|
757
|
+
await import_electron6.clipboard.write([new import_electron6.ClipboardItem({ "image/png": toBlob(png, "image/png") })]);
|
|
758
|
+
}
|
|
759
|
+
function copyText(text) {
|
|
760
|
+
if (!text) throw new Error("\u6587\u672C\u4E3A\u7A7A");
|
|
761
|
+
import_electron6.clipboard.writeText(text);
|
|
762
|
+
}
|
|
763
|
+
function defaultSnipName(format = "png") {
|
|
764
|
+
const now = /* @__PURE__ */ new Date();
|
|
765
|
+
const p = (n) => String(n).padStart(2, "0");
|
|
766
|
+
const ms = String(now.getMilliseconds()).padStart(3, "0");
|
|
767
|
+
return `snip_${now.getFullYear()}-${p(now.getMonth() + 1)}-${p(now.getDate())}_${p(now.getHours())}-${p(now.getMinutes())}-${p(now.getSeconds())}-${ms}.${format}`;
|
|
768
|
+
}
|
|
769
|
+
async function savePng(win, png, opts = {}) {
|
|
770
|
+
const owner = win && !win.isDestroyed() && win.isVisible() ? win : null;
|
|
771
|
+
const wasOnTop = !!owner && owner.isAlwaysOnTop();
|
|
772
|
+
if (owner && wasOnTop) owner.setAlwaysOnTop(false);
|
|
773
|
+
try {
|
|
774
|
+
const dialogOpts = {
|
|
775
|
+
title: "\u4FDD\u5B58\u622A\u56FE",
|
|
776
|
+
defaultPath: import_node_path3.default.join(
|
|
777
|
+
opts.saveDir || import_electron6.app.getPath("pictures"),
|
|
778
|
+
opts.suggestedName ?? defaultSnipName(opts.format)
|
|
779
|
+
),
|
|
780
|
+
filters: [
|
|
781
|
+
{ name: "PNG \u56FE\u7247", extensions: ["png"] },
|
|
782
|
+
{ name: "JPEG \u56FE\u7247", extensions: ["jpg", "jpeg"] }
|
|
783
|
+
]
|
|
784
|
+
};
|
|
785
|
+
const { canceled, filePath } = owner ? await import_electron6.dialog.showSaveDialog(owner, dialogOpts) : await import_electron6.dialog.showSaveDialog(dialogOpts);
|
|
786
|
+
if (canceled || !filePath) return null;
|
|
787
|
+
let data = Buffer.from(png);
|
|
788
|
+
let target = filePath;
|
|
789
|
+
if (/\.jpe?g$/i.test(target)) {
|
|
790
|
+
data = import_electron6.nativeImage.createFromBuffer(data).toJPEG(92);
|
|
791
|
+
} else if (!/\.png$/i.test(target)) {
|
|
792
|
+
target += ".png";
|
|
793
|
+
}
|
|
794
|
+
await import_node_fs.promises.writeFile(target, data);
|
|
795
|
+
import_electron6.clipboard.writeText(target);
|
|
796
|
+
return target;
|
|
797
|
+
} finally {
|
|
798
|
+
if (owner && wasOnTop) owner.setAlwaysOnTop(true, "floating");
|
|
799
|
+
}
|
|
800
|
+
}
|
|
801
|
+
|
|
802
|
+
// src/main/ipc.ts
|
|
803
|
+
function installIpc(target, h) {
|
|
804
|
+
const registered = [];
|
|
805
|
+
const track = (channel) => {
|
|
806
|
+
registered.push(channel);
|
|
807
|
+
return channel;
|
|
808
|
+
};
|
|
809
|
+
target.handle(track(CH.overlayBoot), (e) => h.overlayBoot(e.sender));
|
|
810
|
+
target.handle(track(CH.overlayReady), (e) => h.overlayReady(e.sender));
|
|
811
|
+
target.handle(track(CH.overlayInput), (e, payload) => h.overlayInput(e.sender, payload));
|
|
812
|
+
target.handle(track(CH.overlayTool), (e, payload) => h.overlayTool(e.sender, payload.tool));
|
|
813
|
+
target.handle(track(CH.overlayAction), (e, payload) => h.overlayAction(e.sender, payload.id));
|
|
814
|
+
target.handle(
|
|
815
|
+
track(CH.overlayShapes),
|
|
816
|
+
(e, payload) => h.overlayShapes(e.sender, payload.shapes)
|
|
817
|
+
);
|
|
818
|
+
target.handle(
|
|
819
|
+
track(CH.overlayDraft),
|
|
820
|
+
(e, payload) => h.overlayDraft(e.sender, payload.draft)
|
|
821
|
+
);
|
|
822
|
+
target.handle(
|
|
823
|
+
track(CH.overlayInFlight),
|
|
824
|
+
(e, payload) => h.overlayInFlight(e.sender, payload.index)
|
|
825
|
+
);
|
|
826
|
+
target.handle(
|
|
827
|
+
track(CH.overlayEditState),
|
|
828
|
+
(e, payload) => h.overlayEditState(e.sender, payload.textEditing)
|
|
829
|
+
);
|
|
830
|
+
target.handle(track(CH.overlayFocus), (e) => h.overlayFocus(e.sender));
|
|
831
|
+
target.handle(track(CH.overlayCopyText), (e, payload) => h.overlayCopyText(e.sender, payload.text));
|
|
832
|
+
target.handle(
|
|
833
|
+
track(CH.overlayExport),
|
|
834
|
+
(e, payload) => h.overlayExport(e.sender, payload.kind, payload.png)
|
|
835
|
+
);
|
|
836
|
+
target.handle(track(CH.pinBoot), (e) => h.pinBoot(e.sender));
|
|
837
|
+
target.handle(track(CH.pinReady), (e) => h.pinReady(e.sender));
|
|
838
|
+
target.handle(track(CH.pinAction), (e, payload) => h.pinAction(e.sender, payload));
|
|
839
|
+
return () => {
|
|
840
|
+
for (const ch of registered) target.removeHandler(ch);
|
|
841
|
+
};
|
|
842
|
+
}
|
|
843
|
+
|
|
844
|
+
// src/main/snipper.ts
|
|
845
|
+
var MIN_SELECTION = 3;
|
|
846
|
+
var WINDOW_CLICK_THRESHOLD = 4;
|
|
847
|
+
var HOVER_POLL_MS = 64;
|
|
848
|
+
var READY_TIMEOUT_MS = 2500;
|
|
849
|
+
var CAPTURE_WAIT_MS = 2500;
|
|
850
|
+
var PIN_TIMEOUT_MS = 8e3;
|
|
851
|
+
function createSnipper(opts) {
|
|
852
|
+
setLogger(opts.logger ?? console);
|
|
853
|
+
const windowSnap = opts.windowSnap !== false;
|
|
854
|
+
const hostActions = opts.actions ?? [];
|
|
855
|
+
let session = null;
|
|
856
|
+
let hiddenWindows = [];
|
|
857
|
+
let readyTimer = null;
|
|
858
|
+
let pendingRest = null;
|
|
859
|
+
let starting = false;
|
|
860
|
+
let epoch = 0;
|
|
861
|
+
const pinReadyWaiters = /* @__PURE__ */ new Map();
|
|
862
|
+
const pendingImages = /* @__PURE__ */ new Map();
|
|
863
|
+
let uninstallIpc = null;
|
|
864
|
+
let uninstallScreen = null;
|
|
865
|
+
const overlayHooks = {
|
|
866
|
+
onKey: (webContents, kind) => {
|
|
867
|
+
if (session?.editText.has(webContents.id)) return false;
|
|
868
|
+
onOverlayInput(webContents, { kind });
|
|
869
|
+
return true;
|
|
870
|
+
},
|
|
871
|
+
onClosed: (contentsId) => {
|
|
872
|
+
const s = session;
|
|
873
|
+
if (!s) return;
|
|
874
|
+
s.byContents.delete(contentsId);
|
|
875
|
+
s.shotByContents.delete(contentsId);
|
|
876
|
+
s.ready.delete(contentsId);
|
|
877
|
+
s.overlays = s.overlays.filter((w) => !w.isDestroyed());
|
|
878
|
+
},
|
|
879
|
+
// 某块屏的遮罩渲染进程崩溃:页面已归零、按键与鼠标全部失效,用户那块屏
|
|
880
|
+
// 只剩 backgroundColor 的全屏黑块 —— 收场会话把黑窗藏掉,还给用户屏幕
|
|
881
|
+
onCrashed: (displayId) => {
|
|
882
|
+
log.error(`[snip] \u906E\u7F69\u6E32\u67D3\u8FDB\u7A0B\u5D29\u6E83\uFF0C\u6536\u573A\u4F1A\u8BDD display=${displayId}`);
|
|
883
|
+
cancel("overlay-crashed");
|
|
884
|
+
}
|
|
885
|
+
};
|
|
886
|
+
const overlays = new OverlayPool({ preloadPath: opts.preloadPath, rendererDir: opts.rendererDir }, overlayHooks);
|
|
887
|
+
const pins = new PinRegistry({ preloadPath: opts.preloadPath, rendererDir: opts.rendererDir });
|
|
888
|
+
function rejectForeign(sender, channel) {
|
|
889
|
+
if (overlays.owns(sender.id) || pins.owns(sender.id)) return false;
|
|
890
|
+
log.warn(`[snip] \u62D2\u7EDD\u975E\u81EA\u5BB6\u7A97\u53E3\u7684 IPC channel=${channel} sender=${sender.id}`);
|
|
891
|
+
return true;
|
|
892
|
+
}
|
|
893
|
+
function hideOwnWindows() {
|
|
894
|
+
hiddenWindows = [];
|
|
895
|
+
for (const win of import_electron7.BrowserWindow.getAllWindows()) {
|
|
896
|
+
if (win.isVisible()) {
|
|
897
|
+
win.hide();
|
|
898
|
+
hiddenWindows.push(win);
|
|
899
|
+
}
|
|
900
|
+
}
|
|
901
|
+
}
|
|
902
|
+
function restoreWindows(keepHidden) {
|
|
903
|
+
const remaining = [];
|
|
904
|
+
for (const win of hiddenWindows) {
|
|
905
|
+
if (win.isDestroyed()) continue;
|
|
906
|
+
if (keepHidden(win)) {
|
|
907
|
+
remaining.push(win);
|
|
908
|
+
continue;
|
|
909
|
+
}
|
|
910
|
+
win.showInactive();
|
|
911
|
+
}
|
|
912
|
+
hiddenWindows = remaining;
|
|
913
|
+
}
|
|
914
|
+
function discardHidden() {
|
|
915
|
+
hiddenWindows = [];
|
|
916
|
+
}
|
|
917
|
+
function toolbarDisplayFor(s) {
|
|
918
|
+
if (s.pick || s.phase !== "adjusting" || !s.selection) return null;
|
|
919
|
+
const sel = s.selection;
|
|
920
|
+
const center = { x: sel.x + sel.width / 2, y: sel.y + sel.height / 2 };
|
|
921
|
+
const displays = import_electron7.screen.getAllDisplays();
|
|
922
|
+
const at = displays.find(
|
|
923
|
+
(d) => center.x >= d.bounds.x && center.x < d.bounds.x + d.bounds.width && center.y >= d.bounds.y && center.y < d.bounds.y + d.bounds.height
|
|
924
|
+
);
|
|
925
|
+
return (at ?? displayAtCursor(displays)).id;
|
|
926
|
+
}
|
|
927
|
+
function broadcast() {
|
|
928
|
+
if (!session) return;
|
|
929
|
+
const payload = {
|
|
930
|
+
selection: session.selection,
|
|
931
|
+
cursor: import_electron7.screen.getCursorScreenPoint(),
|
|
932
|
+
outScale: session.selection ? resolveOutScale(session.shots, session.selection) : 1,
|
|
933
|
+
phase: session.phase,
|
|
934
|
+
toolbarDisplayId: toolbarDisplayFor(session),
|
|
935
|
+
activeTool: session.activeTool,
|
|
936
|
+
hoverRect: session.hoverRect
|
|
937
|
+
};
|
|
938
|
+
for (const win of session.overlays) {
|
|
939
|
+
if (!win.isDestroyed()) {
|
|
940
|
+
win.webContents.send(CH.overlaySelection, payload);
|
|
941
|
+
}
|
|
942
|
+
}
|
|
943
|
+
}
|
|
944
|
+
function rectFrom(a, b) {
|
|
945
|
+
return {
|
|
946
|
+
x: Math.min(a.x, b.x),
|
|
947
|
+
y: Math.min(a.y, b.y),
|
|
948
|
+
width: Math.abs(a.x - b.x),
|
|
949
|
+
height: Math.abs(a.y - b.y)
|
|
950
|
+
};
|
|
951
|
+
}
|
|
952
|
+
function sameRect(a, b) {
|
|
953
|
+
return a.x === b.x && a.y === b.y && a.width === b.width && a.height === b.height;
|
|
954
|
+
}
|
|
955
|
+
function startHoverPoll(s) {
|
|
956
|
+
if (!windowSnap || s.hoverPoll) return;
|
|
957
|
+
s.hoverPoll = setInterval(() => {
|
|
958
|
+
void detectHoverWindow(s);
|
|
959
|
+
}, HOVER_POLL_MS);
|
|
960
|
+
}
|
|
961
|
+
function stopHoverPoll(s) {
|
|
962
|
+
if (!s) return;
|
|
963
|
+
if (s.hoverPoll) clearInterval(s.hoverPoll);
|
|
964
|
+
s.hoverPoll = null;
|
|
965
|
+
s.hoverBusy = false;
|
|
966
|
+
}
|
|
967
|
+
async function detectHoverWindow(s) {
|
|
968
|
+
const hoverIdle = session === s && s.phase === "selecting" && !s.selection && (!s.mode || s.mode.kind === "window");
|
|
969
|
+
if (!hoverIdle) {
|
|
970
|
+
if (s.hoverRect && session === s) {
|
|
971
|
+
s.hoverRect = null;
|
|
972
|
+
broadcast();
|
|
973
|
+
}
|
|
974
|
+
return;
|
|
975
|
+
}
|
|
976
|
+
if (s.hoverBusy) return;
|
|
977
|
+
s.hoverBusy = true;
|
|
978
|
+
try {
|
|
979
|
+
const cursor = import_electron7.screen.getCursorScreenPoint();
|
|
980
|
+
const rects = (await listWindowRects(process.pid)).map((w) => w.rect);
|
|
981
|
+
const candidates = rects.filter((dip) => pointInRect(dip, cursor));
|
|
982
|
+
if (session !== s) return;
|
|
983
|
+
if (candidates.length !== s.hoverCandidates.length) s.hoverIndex = 0;
|
|
984
|
+
s.hoverCandidates = candidates;
|
|
985
|
+
const rect = candidates[s.hoverIndex] ?? candidates[0] ?? null;
|
|
986
|
+
const changed = s.hoverRect === null !== (rect === null) || rect !== null && s.hoverRect !== null && !sameRect(rect, s.hoverRect);
|
|
987
|
+
if (changed) {
|
|
988
|
+
s.hoverRect = rect;
|
|
989
|
+
broadcast();
|
|
990
|
+
}
|
|
991
|
+
} catch (err) {
|
|
992
|
+
if (!s.hoverErrorLogged) {
|
|
993
|
+
s.hoverErrorLogged = true;
|
|
994
|
+
log.error("[snip] \u7A97\u53E3\u68C0\u6D4B\u4E0D\u53EF\u7528\uFF0C\u5DF2\u964D\u7EA7\u4E3A\u666E\u901A\u6846\u9009", err);
|
|
995
|
+
}
|
|
996
|
+
} finally {
|
|
997
|
+
s.hoverBusy = false;
|
|
998
|
+
}
|
|
999
|
+
}
|
|
1000
|
+
function fullDisplayAt(point) {
|
|
1001
|
+
return { ...displayAt(import_electron7.screen.getAllDisplays(), point).bounds };
|
|
1002
|
+
}
|
|
1003
|
+
function hasRealSelection(sel) {
|
|
1004
|
+
return !!sel && sel.width >= MIN_SELECTION && sel.height >= MIN_SELECTION;
|
|
1005
|
+
}
|
|
1006
|
+
function stopPoll(s) {
|
|
1007
|
+
if (!s) return;
|
|
1008
|
+
if (s.poll) clearInterval(s.poll);
|
|
1009
|
+
s.poll = null;
|
|
1010
|
+
s.mode = null;
|
|
1011
|
+
}
|
|
1012
|
+
function startPoll(s) {
|
|
1013
|
+
if (s.poll) clearInterval(s.poll);
|
|
1014
|
+
s.poll = setInterval(() => {
|
|
1015
|
+
if (!session || !session.mode) return;
|
|
1016
|
+
applyDrag(session);
|
|
1017
|
+
broadcast();
|
|
1018
|
+
}, 16);
|
|
1019
|
+
}
|
|
1020
|
+
function applyDrag(s) {
|
|
1021
|
+
const mode = s.mode;
|
|
1022
|
+
if (!mode) return;
|
|
1023
|
+
const cur = import_electron7.screen.getCursorScreenPoint();
|
|
1024
|
+
if (mode.kind === "window") {
|
|
1025
|
+
if (Math.hypot(cur.x - mode.anchor.x, cur.y - mode.anchor.y) >= WINDOW_CLICK_THRESHOLD) {
|
|
1026
|
+
s.mode = { kind: "new", anchor: mode.anchor, prev: null };
|
|
1027
|
+
s.selection = rectFrom(mode.anchor, cur);
|
|
1028
|
+
}
|
|
1029
|
+
return;
|
|
1030
|
+
}
|
|
1031
|
+
if (mode.kind === "new") {
|
|
1032
|
+
s.selection = rectFrom(mode.anchor, cur);
|
|
1033
|
+
} else if (mode.kind === "move") {
|
|
1034
|
+
s.selection = clampRect(moveRect(mode.origin, mode.start, cur), s.virtualBounds);
|
|
1035
|
+
} else {
|
|
1036
|
+
s.selection = clampRect(resizeRect(mode.origin, mode.handle, cur), s.virtualBounds);
|
|
1037
|
+
}
|
|
1038
|
+
}
|
|
1039
|
+
function finishDrag(s, mode) {
|
|
1040
|
+
if (mode.kind === "window") {
|
|
1041
|
+
s.selection = s.hoverRect ? { ...s.hoverRect } : fullDisplayAt(mode.anchor);
|
|
1042
|
+
s.hoverRect = null;
|
|
1043
|
+
s.phase = "adjusting";
|
|
1044
|
+
return;
|
|
1045
|
+
}
|
|
1046
|
+
if (mode.kind === "new" && !hasRealSelection(s.selection)) {
|
|
1047
|
+
s.selection = mode.prev && hasRealSelection(mode.prev) ? mode.prev : fullDisplayAt(mode.anchor);
|
|
1048
|
+
}
|
|
1049
|
+
s.phase = "adjusting";
|
|
1050
|
+
}
|
|
1051
|
+
async function beginSession(pickOpts) {
|
|
1052
|
+
if (starting) {
|
|
1053
|
+
log.warn("[snip] \u622A\u56FE\u6B63\u5728\u542F\u52A8\uFF0C\u5FFD\u7565\u65B0\u7684\u622A\u56FE\u8BF7\u6C42");
|
|
1054
|
+
return;
|
|
1055
|
+
}
|
|
1056
|
+
if (session?.handedOff) {
|
|
1057
|
+
log.warn("[snip] \u5DF2\u6709\u79FB\u4EA4\u7ED9\u5BBF\u4E3B\u7684\u4F1A\u8BDD\uFF0C\u5FFD\u7565\u65B0\u7684\u622A\u56FE\u8BF7\u6C42");
|
|
1058
|
+
return;
|
|
1059
|
+
}
|
|
1060
|
+
if (session) cancel("restart");
|
|
1061
|
+
starting = true;
|
|
1062
|
+
const mine = ++epoch;
|
|
1063
|
+
try {
|
|
1064
|
+
await startSession(pickOpts, () => epoch !== mine);
|
|
1065
|
+
} finally {
|
|
1066
|
+
starting = false;
|
|
1067
|
+
}
|
|
1068
|
+
}
|
|
1069
|
+
async function startSession(pickOpts, aborted) {
|
|
1070
|
+
if (!await ensureScreenCaptureAccess()) return;
|
|
1071
|
+
if (aborted()) return;
|
|
1072
|
+
hideOwnWindows();
|
|
1073
|
+
opts.onStateChange?.(true);
|
|
1074
|
+
const displays = import_electron7.screen.getAllDisplays();
|
|
1075
|
+
overlays.prune(new Set(displays.map((d) => d.id)));
|
|
1076
|
+
const primary = displayAtCursor(displays);
|
|
1077
|
+
const rest = displays.filter((d) => d.id !== primary.id);
|
|
1078
|
+
const restPromise = Promise.all(rest.map(async (d) => ({ shots: await captureDisplays([d], displays) })));
|
|
1079
|
+
restPromise.catch(() => {
|
|
1080
|
+
});
|
|
1081
|
+
const t0 = performance.now();
|
|
1082
|
+
let primaryShots;
|
|
1083
|
+
try {
|
|
1084
|
+
primaryShots = await captureDisplays([primary], displays);
|
|
1085
|
+
} catch (err) {
|
|
1086
|
+
log.error("[snip] \u6293\u5C4F\u5931\u8D25", err);
|
|
1087
|
+
restPromise.catch((cause) => {
|
|
1088
|
+
log.error("[snip] \u5176\u4F59\u5C4F\u6293\u5C4F\u5931\u8D25", cause);
|
|
1089
|
+
});
|
|
1090
|
+
restoreWindows(() => false);
|
|
1091
|
+
opts.onStateChange?.(false);
|
|
1092
|
+
opts.focusHost?.();
|
|
1093
|
+
return;
|
|
1094
|
+
}
|
|
1095
|
+
log.info(`[snip] \u6293\u5C4F\uFF08\u5149\u6807\u5C4F\uFF09${Math.round(performance.now() - t0)}ms`);
|
|
1096
|
+
if (aborted()) {
|
|
1097
|
+
log.warn("[snip] \u6293\u5C4F\u671F\u95F4\u4F1A\u8BDD\u5DF2\u6536\u573A\uFF0C\u653E\u5F03\u672C\u6B21\u622A\u56FE");
|
|
1098
|
+
opts.onStateChange?.(false);
|
|
1099
|
+
opts.focusHost?.();
|
|
1100
|
+
return;
|
|
1101
|
+
}
|
|
1102
|
+
session = {
|
|
1103
|
+
shots: [...primaryShots],
|
|
1104
|
+
virtualBounds: unionBounds(displays),
|
|
1105
|
+
overlays: [],
|
|
1106
|
+
byContents: /* @__PURE__ */ new Map(),
|
|
1107
|
+
shotByContents: /* @__PURE__ */ new Map(),
|
|
1108
|
+
ready: /* @__PURE__ */ new Set(),
|
|
1109
|
+
shown: false,
|
|
1110
|
+
mode: null,
|
|
1111
|
+
selection: null,
|
|
1112
|
+
poll: null,
|
|
1113
|
+
phase: "selecting",
|
|
1114
|
+
pendingPin: null,
|
|
1115
|
+
pinLayout: null,
|
|
1116
|
+
activeTool: "hand",
|
|
1117
|
+
editText: /* @__PURE__ */ new Set(),
|
|
1118
|
+
shapesByContents: /* @__PURE__ */ new Map(),
|
|
1119
|
+
inFlightByContents: /* @__PURE__ */ new Map(),
|
|
1120
|
+
hoverRect: null,
|
|
1121
|
+
hoverPoll: null,
|
|
1122
|
+
hoverBusy: false,
|
|
1123
|
+
hoverErrorLogged: false,
|
|
1124
|
+
hoverCandidates: [],
|
|
1125
|
+
hoverIndex: 0,
|
|
1126
|
+
pick: pickOpts,
|
|
1127
|
+
handedOff: false,
|
|
1128
|
+
startedAt: performance.now()
|
|
1129
|
+
};
|
|
1130
|
+
spawnOverlays(primaryShots);
|
|
1131
|
+
startHoverPoll(session);
|
|
1132
|
+
if (readyTimer) clearTimeout(readyTimer);
|
|
1133
|
+
readyTimer = setTimeout(() => {
|
|
1134
|
+
if (session && !session.shown) {
|
|
1135
|
+
cancel("overlay-timeout");
|
|
1136
|
+
}
|
|
1137
|
+
}, READY_TIMEOUT_MS);
|
|
1138
|
+
const own = session;
|
|
1139
|
+
void listWindowRects(process.pid).then((list) => {
|
|
1140
|
+
if (session === own) own.sourceApp = list[0]?.owner;
|
|
1141
|
+
}).catch((err) => log.warn("[snip] \u6765\u6E90\u5E94\u7528\u63A2\u6D4B\u5931\u8D25", err));
|
|
1142
|
+
const restChain = restPromise.then((list) => {
|
|
1143
|
+
if (session !== own) return;
|
|
1144
|
+
for (const { shots } of list) {
|
|
1145
|
+
own.shots.push(...shots);
|
|
1146
|
+
spawnOverlays(shots);
|
|
1147
|
+
}
|
|
1148
|
+
broadcast();
|
|
1149
|
+
}).catch((err) => log.error("[snip] \u5176\u4F59\u5C4F\u6293\u5C4F\u5931\u8D25", err)).then(() => {
|
|
1150
|
+
if (session !== own) return;
|
|
1151
|
+
pendingRest = null;
|
|
1152
|
+
restoreWindows((win) => opts.keepHiddenAfterPin?.(win) ?? false);
|
|
1153
|
+
});
|
|
1154
|
+
pendingRest = restChain;
|
|
1155
|
+
}
|
|
1156
|
+
function spawnOverlays(shots) {
|
|
1157
|
+
const s = session;
|
|
1158
|
+
if (!s) return;
|
|
1159
|
+
for (const shot of shots) {
|
|
1160
|
+
const { win, load } = overlays.acquire(shot);
|
|
1161
|
+
s.overlays.push(win);
|
|
1162
|
+
s.byContents.set(win.webContents.id, win);
|
|
1163
|
+
s.shotByContents.set(win.webContents.id, shot);
|
|
1164
|
+
void load.catch((err) => {
|
|
1165
|
+
log.error("[snip] \u906E\u7F69\u52A0\u8F7D\u5931\u8D25", err);
|
|
1166
|
+
});
|
|
1167
|
+
}
|
|
1168
|
+
}
|
|
1169
|
+
function onOverlayBoot(sender) {
|
|
1170
|
+
if (!session) return null;
|
|
1171
|
+
const shot = session.shotByContents.get(sender.id);
|
|
1172
|
+
if (!shot) return null;
|
|
1173
|
+
const actions = hostActions.map(({ id, label, icon, danger, primary }) => ({ id, label, icon, danger, primary }));
|
|
1174
|
+
return { shot, virtualBounds: session.virtualBounds, actions };
|
|
1175
|
+
}
|
|
1176
|
+
function onOverlayReady(sender) {
|
|
1177
|
+
const s = session;
|
|
1178
|
+
if (!s) return;
|
|
1179
|
+
const win = s.byContents.get(sender.id);
|
|
1180
|
+
if (!win || win.isDestroyed()) return;
|
|
1181
|
+
s.ready.add(sender.id);
|
|
1182
|
+
const first = !s.shown;
|
|
1183
|
+
s.shown = true;
|
|
1184
|
+
if (first) {
|
|
1185
|
+
if (readyTimer) {
|
|
1186
|
+
clearTimeout(readyTimer);
|
|
1187
|
+
readyTimer = null;
|
|
1188
|
+
}
|
|
1189
|
+
log.info(`[snip] \u89E6\u53D1 \u2192 \u9996\u7A97 ready \u603B\u8017\u65F6 ${Math.round(performance.now() - s.startedAt)}ms`);
|
|
1190
|
+
win.show();
|
|
1191
|
+
win.moveTop();
|
|
1192
|
+
overlayAtCursor()?.focus();
|
|
1193
|
+
} else {
|
|
1194
|
+
win.show();
|
|
1195
|
+
win.moveTop();
|
|
1196
|
+
}
|
|
1197
|
+
broadcast();
|
|
1198
|
+
for (const contentsId of s.shapesByContents.keys()) {
|
|
1199
|
+
if (contentsId === sender.id) continue;
|
|
1200
|
+
const shot = s.shotByContents.get(contentsId);
|
|
1201
|
+
if (!shot) continue;
|
|
1202
|
+
const payload = {
|
|
1203
|
+
fromDisplayId: shot.displayId,
|
|
1204
|
+
shapes: visibleRemoteShapes(s, contentsId).map((shape) => mapShape(shape, shot.bounds.x, shot.bounds.y, 1))
|
|
1205
|
+
};
|
|
1206
|
+
win.webContents.send(CH.overlayRemoteShapes, payload);
|
|
1207
|
+
}
|
|
1208
|
+
}
|
|
1209
|
+
function overlayAtCursor() {
|
|
1210
|
+
if (!session) return null;
|
|
1211
|
+
const d = displayAtCursor(import_electron7.screen.getAllDisplays());
|
|
1212
|
+
const win = session.overlays.find((w) => !w.isDestroyed() && sameRect(w.getBounds(), d.bounds));
|
|
1213
|
+
return win ?? null;
|
|
1214
|
+
}
|
|
1215
|
+
function onOverlayInput(sender, payload) {
|
|
1216
|
+
const s = session;
|
|
1217
|
+
if (!s || s.phase === "confirming") return;
|
|
1218
|
+
if (!s.byContents.has(sender.id)) return;
|
|
1219
|
+
switch (payload.kind) {
|
|
1220
|
+
case "down": {
|
|
1221
|
+
const p = import_electron7.screen.getCursorScreenPoint();
|
|
1222
|
+
stopPoll(s);
|
|
1223
|
+
const prev = s.selection;
|
|
1224
|
+
if (s.phase === "adjusting" && prev && hasRealSelection(prev)) {
|
|
1225
|
+
const handle = hitHandle(prev, p) ?? hitBorder(prev, p);
|
|
1226
|
+
if (handle) {
|
|
1227
|
+
s.mode = { kind: "resize", origin: { ...prev }, handle, start: p };
|
|
1228
|
+
startPoll(s);
|
|
1229
|
+
broadcast();
|
|
1230
|
+
break;
|
|
1231
|
+
}
|
|
1232
|
+
if (pointInRect(prev, p)) {
|
|
1233
|
+
s.mode = { kind: "move", origin: { ...prev }, start: p };
|
|
1234
|
+
startPoll(s);
|
|
1235
|
+
broadcast();
|
|
1236
|
+
break;
|
|
1237
|
+
}
|
|
1238
|
+
}
|
|
1239
|
+
if (!prev && s.hoverRect && pointInRect(s.hoverRect, p)) {
|
|
1240
|
+
s.mode = { kind: "window", anchor: p };
|
|
1241
|
+
startPoll(s);
|
|
1242
|
+
break;
|
|
1243
|
+
}
|
|
1244
|
+
s.phase = "selecting";
|
|
1245
|
+
s.selection = { x: p.x, y: p.y, width: 0, height: 0 };
|
|
1246
|
+
s.mode = { kind: "new", anchor: p, prev: prev ?? null };
|
|
1247
|
+
startPoll(s);
|
|
1248
|
+
broadcast();
|
|
1249
|
+
break;
|
|
1250
|
+
}
|
|
1251
|
+
case "up": {
|
|
1252
|
+
const mode = s.mode;
|
|
1253
|
+
if (!mode) break;
|
|
1254
|
+
applyDrag(s);
|
|
1255
|
+
stopPoll(s);
|
|
1256
|
+
finishDrag(s, mode);
|
|
1257
|
+
broadcast();
|
|
1258
|
+
break;
|
|
1259
|
+
}
|
|
1260
|
+
case "dblclick":
|
|
1261
|
+
case "enter": {
|
|
1262
|
+
if (!hasRealSelection(s.selection)) {
|
|
1263
|
+
s.selection = fullDisplayAt(import_electron7.screen.getCursorScreenPoint());
|
|
1264
|
+
}
|
|
1265
|
+
if (s.pick) {
|
|
1266
|
+
void handOff(s, s.pick);
|
|
1267
|
+
break;
|
|
1268
|
+
}
|
|
1269
|
+
requestExport("copy");
|
|
1270
|
+
break;
|
|
1271
|
+
}
|
|
1272
|
+
case "nudge": {
|
|
1273
|
+
if (s.phase !== "adjusting" || s.mode) break;
|
|
1274
|
+
const sel = s.selection;
|
|
1275
|
+
if (!hasRealSelection(sel)) break;
|
|
1276
|
+
s.selection = clampRect({ ...sel, x: sel.x + payload.dx, y: sel.y + payload.dy }, s.virtualBounds);
|
|
1277
|
+
broadcast();
|
|
1278
|
+
break;
|
|
1279
|
+
}
|
|
1280
|
+
case "cycle-window": {
|
|
1281
|
+
if (s.phase !== "selecting" || s.selection || s.mode) break;
|
|
1282
|
+
const list = s.hoverCandidates;
|
|
1283
|
+
if (list.length < 2) break;
|
|
1284
|
+
s.hoverIndex = (s.hoverIndex + 1) % list.length;
|
|
1285
|
+
s.hoverRect = list[s.hoverIndex];
|
|
1286
|
+
broadcast();
|
|
1287
|
+
break;
|
|
1288
|
+
}
|
|
1289
|
+
case "escape":
|
|
1290
|
+
cancel("escape");
|
|
1291
|
+
break;
|
|
1292
|
+
case "context":
|
|
1293
|
+
cancel("context-menu");
|
|
1294
|
+
break;
|
|
1295
|
+
}
|
|
1296
|
+
}
|
|
1297
|
+
function onOverlayTool(sender, tool) {
|
|
1298
|
+
if (rejectForeign(sender, CH.overlayTool)) return;
|
|
1299
|
+
const s = session;
|
|
1300
|
+
if (!s || s.activeTool === tool) return;
|
|
1301
|
+
s.activeTool = tool;
|
|
1302
|
+
broadcast();
|
|
1303
|
+
}
|
|
1304
|
+
function onOverlayAction(sender, id) {
|
|
1305
|
+
const s = session;
|
|
1306
|
+
if (!s || s.phase === "confirming") return;
|
|
1307
|
+
if (!s.byContents.has(sender.id)) return;
|
|
1308
|
+
log.info(`[snip] \u906E\u7F69\u5DE5\u5177\u6761\u52A8\u4F5C kind=${id} phase=${s.phase}`);
|
|
1309
|
+
if (id === "cancel") {
|
|
1310
|
+
cancel("toolbar");
|
|
1311
|
+
return;
|
|
1312
|
+
}
|
|
1313
|
+
if (id === "pin") {
|
|
1314
|
+
void confirmSnip();
|
|
1315
|
+
return;
|
|
1316
|
+
}
|
|
1317
|
+
if (id === "copy" || id === "save") {
|
|
1318
|
+
requestExport(id);
|
|
1319
|
+
return;
|
|
1320
|
+
}
|
|
1321
|
+
const action = hostActions.find((a) => a.id === id);
|
|
1322
|
+
if (!action) {
|
|
1323
|
+
log.warn(`[snip] \u672A\u77E5\u7684\u5DE5\u5177\u6761\u52A8\u4F5C ${id}`);
|
|
1324
|
+
return;
|
|
1325
|
+
}
|
|
1326
|
+
void runHostAction(s, action, sender);
|
|
1327
|
+
}
|
|
1328
|
+
async function runHostAction(s, action, sender) {
|
|
1329
|
+
const sel = s.selection;
|
|
1330
|
+
if (!hasRealSelection(sel)) return;
|
|
1331
|
+
const ctx = {
|
|
1332
|
+
selection: sel,
|
|
1333
|
+
shots: s.shots,
|
|
1334
|
+
sourceApp: s.sourceApp,
|
|
1335
|
+
pickSingleDisplay: () => {
|
|
1336
|
+
const pick = bestRegionOnDisplay(s.shots, sel);
|
|
1337
|
+
return pick ? { shot: s.shots[pick.index], region: pick.region } : null;
|
|
1338
|
+
},
|
|
1339
|
+
hideOverlays: () => hideOverlays(),
|
|
1340
|
+
end: (reason = "action") => cancel(reason),
|
|
1341
|
+
showResult: (result) => {
|
|
1342
|
+
if (!sender.isDestroyed()) sender.send(CH.overlayPanel, result);
|
|
1343
|
+
}
|
|
1344
|
+
};
|
|
1345
|
+
try {
|
|
1346
|
+
await action.run(ctx);
|
|
1347
|
+
} catch (err) {
|
|
1348
|
+
log.error(`[snip] \u52A8\u4F5C ${action.id} \u5931\u8D25`, err);
|
|
1349
|
+
ctx.showResult({ state: "error", error: err instanceof Error ? err.message : String(err) });
|
|
1350
|
+
}
|
|
1351
|
+
}
|
|
1352
|
+
function requestExport(kind) {
|
|
1353
|
+
void runExport(kind);
|
|
1354
|
+
}
|
|
1355
|
+
async function handOff(s, pick) {
|
|
1356
|
+
const sel = s.selection;
|
|
1357
|
+
if (!sel) return;
|
|
1358
|
+
stopPoll(s);
|
|
1359
|
+
s.phase = "confirming";
|
|
1360
|
+
broadcast();
|
|
1361
|
+
if (!await waitForRest(s)) return;
|
|
1362
|
+
const found = bestRegionOnDisplay(s.shots, sel);
|
|
1363
|
+
if (!found) {
|
|
1364
|
+
log.warn("[snip] \u79FB\u4EA4\u5931\u8D25\uFF1A\u9009\u533A\u4E0D\u5728\u4EFB\u4F55\u5DF2\u622A\u53D6\u7684\u5C4F\u4E0A");
|
|
1365
|
+
s.phase = "adjusting";
|
|
1366
|
+
broadcast();
|
|
1367
|
+
return;
|
|
1368
|
+
}
|
|
1369
|
+
s.handedOff = true;
|
|
1370
|
+
hideOverlays();
|
|
1371
|
+
pick.onPick({ shot: s.shots[found.index], region: found.region, sourceApp: s.sourceApp });
|
|
1372
|
+
}
|
|
1373
|
+
async function waitForRest(s) {
|
|
1374
|
+
if (!pendingRest) return true;
|
|
1375
|
+
let timer;
|
|
1376
|
+
try {
|
|
1377
|
+
await Promise.race([
|
|
1378
|
+
pendingRest,
|
|
1379
|
+
new Promise((resolve) => {
|
|
1380
|
+
timer = setTimeout(resolve, CAPTURE_WAIT_MS);
|
|
1381
|
+
})
|
|
1382
|
+
]);
|
|
1383
|
+
} finally {
|
|
1384
|
+
if (timer) clearTimeout(timer);
|
|
1385
|
+
}
|
|
1386
|
+
return session === s;
|
|
1387
|
+
}
|
|
1388
|
+
async function runExport(kind) {
|
|
1389
|
+
const s = session;
|
|
1390
|
+
if (!s || !hasRealSelection(s.selection)) {
|
|
1391
|
+
log.warn(`[snip] \u5BFC\u51FA\u88AB\u62D2\u7EDD\uFF1A\u6CA1\u6709\u6709\u6548\u9009\u533A kind=${kind}`);
|
|
1392
|
+
return;
|
|
1393
|
+
}
|
|
1394
|
+
stopPoll(s);
|
|
1395
|
+
s.phase = "adjusting";
|
|
1396
|
+
broadcast();
|
|
1397
|
+
if (!await waitForRest(s)) return;
|
|
1398
|
+
const hostDisplayId = toolbarDisplayFor(s);
|
|
1399
|
+
const data = {
|
|
1400
|
+
shots: s.shots,
|
|
1401
|
+
selection: s.selection,
|
|
1402
|
+
outScale: resolveOutScale(s.shots, s.selection),
|
|
1403
|
+
shapes: toImageShapes(s)
|
|
1404
|
+
};
|
|
1405
|
+
for (const [contentsId, shot] of s.shotByContents) {
|
|
1406
|
+
if (shot.displayId !== hostDisplayId) continue;
|
|
1407
|
+
const win = s.byContents.get(contentsId);
|
|
1408
|
+
if (!win || win.isDestroyed()) continue;
|
|
1409
|
+
log.info(`[snip] \u8BF7\u6C42\u5C31\u5730\u5408\u6210 kind=${kind} host=${hostDisplayId} shapes=${data.shapes.length}`);
|
|
1410
|
+
win.webContents.send(CH.overlayCompose, { kind, data });
|
|
1411
|
+
return;
|
|
1412
|
+
}
|
|
1413
|
+
log.warn(`[snip] \u8BF7\u6C42\u5C31\u5730\u5408\u6210\u5931\u8D25\uFF1A\u627E\u4E0D\u5230\u5BBF\u4E3B\u906E\u7F69 display=${hostDisplayId}`);
|
|
1414
|
+
}
|
|
1415
|
+
async function onOverlayExport(sender, kind, png) {
|
|
1416
|
+
if (rejectForeign(sender, CH.overlayExport)) return { ok: false, error: "\u975E\u672C\u5305\u7A97\u53E3" };
|
|
1417
|
+
if (!session) return { ok: false, error: "\u622A\u56FE\u4F1A\u8BDD\u5DF2\u7ED3\u675F" };
|
|
1418
|
+
if (png.byteLength === 0) return { ok: false, error: "\u56FE\u7247\u6570\u636E\u4E3A\u7A7A" };
|
|
1419
|
+
const sourceApp = session.sourceApp;
|
|
1420
|
+
const win = import_electron7.BrowserWindow.fromWebContents(sender);
|
|
1421
|
+
cancel(kind === "copy" ? "copy" : "save");
|
|
1422
|
+
return deliver(kind, png, win, sourceApp);
|
|
1423
|
+
}
|
|
1424
|
+
async function deliver(kind, png, win, sourceApp, suggestedName) {
|
|
1425
|
+
try {
|
|
1426
|
+
if (opts.onExport) {
|
|
1427
|
+
const ok = await opts.onExport({ kind, png, sourceApp });
|
|
1428
|
+
return ok === false ? { ok: false, error: "\u5DF2\u53D6\u6D88" } : { ok: true };
|
|
1429
|
+
}
|
|
1430
|
+
if (kind === "copy") {
|
|
1431
|
+
await copyPng(png);
|
|
1432
|
+
log.info(`[snip] \u5DF2\u590D\u5236 ${png.byteLength} \u5B57\u8282`);
|
|
1433
|
+
return { ok: true };
|
|
1434
|
+
}
|
|
1435
|
+
const saved = await savePng(win, png, {
|
|
1436
|
+
saveDir: opts.saveDir,
|
|
1437
|
+
format: opts.saveFormat,
|
|
1438
|
+
suggestedName: suggestedName ?? defaultSnipName(opts.saveFormat)
|
|
1439
|
+
});
|
|
1440
|
+
return saved ? { ok: true } : { ok: false, error: "\u5DF2\u53D6\u6D88\u4FDD\u5B58" };
|
|
1441
|
+
} catch (err) {
|
|
1442
|
+
const error = err instanceof Error ? err.message : String(err);
|
|
1443
|
+
log.error(`[snip] \u5BFC\u51FA\u5931\u8D25 kind=${kind}`, err);
|
|
1444
|
+
return { ok: false, error };
|
|
1445
|
+
}
|
|
1446
|
+
}
|
|
1447
|
+
function onOverlayShapes(sender, shapes) {
|
|
1448
|
+
if (rejectForeign(sender, CH.overlayShapes)) return;
|
|
1449
|
+
const s = session;
|
|
1450
|
+
if (!s) return;
|
|
1451
|
+
s.shapesByContents.set(sender.id, shapes);
|
|
1452
|
+
broadcastRemoteShapes(s, sender.id);
|
|
1453
|
+
}
|
|
1454
|
+
function onOverlayDraft(sender, draft) {
|
|
1455
|
+
if (rejectForeign(sender, CH.overlayDraft)) return;
|
|
1456
|
+
const s = session;
|
|
1457
|
+
if (!s) return;
|
|
1458
|
+
broadcastRemoteDraft(s, sender.id, draft);
|
|
1459
|
+
}
|
|
1460
|
+
function onOverlayInFlight(sender, index) {
|
|
1461
|
+
if (rejectForeign(sender, CH.overlayInFlight)) return;
|
|
1462
|
+
const s = session;
|
|
1463
|
+
if (!s) return;
|
|
1464
|
+
s.inFlightByContents.set(sender.id, index);
|
|
1465
|
+
broadcastRemoteShapes(s, sender.id);
|
|
1466
|
+
}
|
|
1467
|
+
function visibleRemoteShapes(s, contentsId) {
|
|
1468
|
+
const shapes = s.shapesByContents.get(contentsId) ?? [];
|
|
1469
|
+
const inFlight = s.inFlightByContents.get(contentsId);
|
|
1470
|
+
return inFlight != null ? shapes.filter((_, i) => i !== inFlight) : shapes;
|
|
1471
|
+
}
|
|
1472
|
+
function broadcastRemoteShapes(s, senderId) {
|
|
1473
|
+
const shot = s.shotByContents.get(senderId);
|
|
1474
|
+
if (!shot) return;
|
|
1475
|
+
const payload = {
|
|
1476
|
+
fromDisplayId: shot.displayId,
|
|
1477
|
+
shapes: visibleRemoteShapes(s, senderId).map((shape) => mapShape(shape, shot.bounds.x, shot.bounds.y, 1))
|
|
1478
|
+
};
|
|
1479
|
+
for (const win of s.overlays) {
|
|
1480
|
+
if (win.isDestroyed() || win.webContents.id === senderId) continue;
|
|
1481
|
+
win.webContents.send(CH.overlayRemoteShapes, payload);
|
|
1482
|
+
}
|
|
1483
|
+
}
|
|
1484
|
+
function broadcastRemoteDraft(s, senderId, draft) {
|
|
1485
|
+
const shot = s.shotByContents.get(senderId);
|
|
1486
|
+
if (!shot) return;
|
|
1487
|
+
const payload = {
|
|
1488
|
+
fromDisplayId: shot.displayId,
|
|
1489
|
+
draft: draft ? mapShape(draft, shot.bounds.x, shot.bounds.y, 1) : null
|
|
1490
|
+
};
|
|
1491
|
+
for (const win of s.overlays) {
|
|
1492
|
+
if (win.isDestroyed() || win.webContents.id === senderId) continue;
|
|
1493
|
+
win.webContents.send(CH.overlayRemoteDraft, payload);
|
|
1494
|
+
}
|
|
1495
|
+
}
|
|
1496
|
+
function onOverlayEditState(sender, textEditing) {
|
|
1497
|
+
if (rejectForeign(sender, CH.overlayEditState)) return;
|
|
1498
|
+
const s = session;
|
|
1499
|
+
if (!s) return;
|
|
1500
|
+
if (textEditing) s.editText.add(sender.id);
|
|
1501
|
+
else s.editText.delete(sender.id);
|
|
1502
|
+
}
|
|
1503
|
+
function toImageShapes(s) {
|
|
1504
|
+
const sel = s.selection;
|
|
1505
|
+
if (!sel) return [];
|
|
1506
|
+
const k = resolveOutScale(s.shots, sel);
|
|
1507
|
+
const out = [];
|
|
1508
|
+
for (const [contentsId, shapes] of s.shapesByContents) {
|
|
1509
|
+
const shot = s.shotByContents.get(contentsId);
|
|
1510
|
+
if (!shot) continue;
|
|
1511
|
+
const dx = shot.bounds.x - sel.x;
|
|
1512
|
+
const dy = shot.bounds.y - sel.y;
|
|
1513
|
+
for (const shape of shapes) out.push(mapShape(shape, dx, dy, k));
|
|
1514
|
+
}
|
|
1515
|
+
return out;
|
|
1516
|
+
}
|
|
1517
|
+
async function confirmSnip() {
|
|
1518
|
+
const s = session;
|
|
1519
|
+
if (!s || s.phase === "confirming" || !hasRealSelection(s.selection)) {
|
|
1520
|
+
return;
|
|
1521
|
+
}
|
|
1522
|
+
if (!await waitForRest(s)) return;
|
|
1523
|
+
const selection = s.selection;
|
|
1524
|
+
const outScale = resolveOutScale(s.shots, selection);
|
|
1525
|
+
log.info(
|
|
1526
|
+
`[snip] \u786E\u8BA4\u9009\u533A dip=${Math.round(selection.x)},${Math.round(selection.y)} ${Math.round(selection.width)}x${Math.round(selection.height)} outScale=${outScale}`
|
|
1527
|
+
);
|
|
1528
|
+
s.phase = "confirming";
|
|
1529
|
+
stopPoll(s);
|
|
1530
|
+
const layout = computePinLayout(selection, import_electron7.screen.getAllDisplays());
|
|
1531
|
+
s.pinLayout = layout;
|
|
1532
|
+
log.info(
|
|
1533
|
+
`[snip] \u9489\u56FE\u5E03\u5C40 pad=${JSON.stringify(layout.padding)} bounds=${layout.bounds.width}x${layout.bounds.height}@${layout.bounds.x},${layout.bounds.y}`
|
|
1534
|
+
);
|
|
1535
|
+
const pin = pins.create(layout.bounds);
|
|
1536
|
+
s.pendingPin = pin;
|
|
1537
|
+
const pinId = pin.webContents.id;
|
|
1538
|
+
let settle = () => {
|
|
1539
|
+
};
|
|
1540
|
+
const finished = new Promise((resolve) => {
|
|
1541
|
+
settle = resolve;
|
|
1542
|
+
});
|
|
1543
|
+
const state = { ok: false };
|
|
1544
|
+
const fail = () => {
|
|
1545
|
+
pinReadyWaiters.delete(pinId);
|
|
1546
|
+
settle();
|
|
1547
|
+
};
|
|
1548
|
+
pinReadyWaiters.set(pinId, (ok) => {
|
|
1549
|
+
state.ok = ok;
|
|
1550
|
+
settle();
|
|
1551
|
+
});
|
|
1552
|
+
void pins.load(pin).catch((err) => {
|
|
1553
|
+
log.error("[snip] \u9489\u56FE\u7A97\u53E3\u52A0\u8F7D\u5931\u8D25", err);
|
|
1554
|
+
fail();
|
|
1555
|
+
});
|
|
1556
|
+
const timer = setTimeout(fail, PIN_TIMEOUT_MS);
|
|
1557
|
+
await finished;
|
|
1558
|
+
clearTimeout(timer);
|
|
1559
|
+
pinReadyWaiters.delete(pinId);
|
|
1560
|
+
if (!state.ok || pin.isDestroyed()) {
|
|
1561
|
+
log.warn("[snip] \u9489\u56FE\u672A\u5C31\u7EEA\uFF0C\u53D6\u6D88\u672C\u6B21\u622A\u56FE");
|
|
1562
|
+
if (!pin.isDestroyed()) pin.close();
|
|
1563
|
+
cancel("pin-timeout");
|
|
1564
|
+
return;
|
|
1565
|
+
}
|
|
1566
|
+
log.info("[snip] \u9489\u56FE\u5C31\u7EEA\uFF0C\u66FF\u6362\u906E\u7F69");
|
|
1567
|
+
hideOverlays();
|
|
1568
|
+
pin.showInactive();
|
|
1569
|
+
pin.focus();
|
|
1570
|
+
session = null;
|
|
1571
|
+
discardHidden();
|
|
1572
|
+
opts.onStateChange?.(false);
|
|
1573
|
+
}
|
|
1574
|
+
function markPinReady(sender) {
|
|
1575
|
+
if (rejectForeign(sender, CH.pinReady)) return;
|
|
1576
|
+
const resolve = pinReadyWaiters.get(sender.id);
|
|
1577
|
+
if (resolve) {
|
|
1578
|
+
pinReadyWaiters.delete(sender.id);
|
|
1579
|
+
resolve(true);
|
|
1580
|
+
}
|
|
1581
|
+
}
|
|
1582
|
+
function takePendingImage(sender) {
|
|
1583
|
+
const pending = pendingImages.get(sender.id);
|
|
1584
|
+
if (!pending) return null;
|
|
1585
|
+
pendingImages.delete(sender.id);
|
|
1586
|
+
return {
|
|
1587
|
+
kind: "image",
|
|
1588
|
+
png: new Uint8Array(pending.png),
|
|
1589
|
+
width: pending.width,
|
|
1590
|
+
height: pending.height,
|
|
1591
|
+
editMode: pending.editMode
|
|
1592
|
+
};
|
|
1593
|
+
}
|
|
1594
|
+
async function pinImage(png, o = {}) {
|
|
1595
|
+
if (session) throw new Error("\u622A\u56FE\u4F1A\u8BDD\u8FDB\u884C\u4E2D\uFF0C\u65E0\u6CD5\u9489\u4F4F\u72EC\u7ACB\u56FE\u7247");
|
|
1596
|
+
const img = import_electron7.nativeImage.createFromBuffer(Buffer.from(png));
|
|
1597
|
+
if (img.isEmpty()) throw new Error("\u56FE\u7247\u6570\u636E\u4E3A\u7A7A");
|
|
1598
|
+
const { width, height } = img.getSize();
|
|
1599
|
+
const wa = import_electron7.screen.getPrimaryDisplay().workArea;
|
|
1600
|
+
const scale = Math.min(1, wa.width * 0.7 / width, wa.height * 0.7 / height);
|
|
1601
|
+
const w = Math.max(120, Math.round(width * scale));
|
|
1602
|
+
const h = Math.max(90, Math.round(height * scale));
|
|
1603
|
+
const bounds = {
|
|
1604
|
+
x: wa.x + Math.round((wa.width - w) / 2),
|
|
1605
|
+
y: wa.y + Math.round((wa.height - h) / 2),
|
|
1606
|
+
width: w,
|
|
1607
|
+
height: h
|
|
1608
|
+
};
|
|
1609
|
+
const pin = pins.create(bounds);
|
|
1610
|
+
const pinId = pin.webContents.id;
|
|
1611
|
+
pendingImages.set(pinId, { png: Buffer.from(png), width: w, height: h, editMode: !!o.editMode });
|
|
1612
|
+
let settle = () => {
|
|
1613
|
+
};
|
|
1614
|
+
const finished = new Promise((resolve) => {
|
|
1615
|
+
settle = resolve;
|
|
1616
|
+
});
|
|
1617
|
+
pin.once("closed", () => settle(false));
|
|
1618
|
+
pinReadyWaiters.set(pinId, (ok) => settle(ok));
|
|
1619
|
+
const timer = setTimeout(() => settle(false), PIN_TIMEOUT_MS);
|
|
1620
|
+
try {
|
|
1621
|
+
await pins.load(pin);
|
|
1622
|
+
const ok = await finished;
|
|
1623
|
+
if (!ok || pin.isDestroyed()) throw new Error("\u8D34\u56FE\u672A\u5C31\u7EEA\uFF08\u7A97\u53E3\u88AB\u5173\u95ED\u6216\u8D85\u65F6\uFF09");
|
|
1624
|
+
pin.showInactive();
|
|
1625
|
+
} catch (err) {
|
|
1626
|
+
if (!pin.isDestroyed()) pin.close();
|
|
1627
|
+
throw err;
|
|
1628
|
+
} finally {
|
|
1629
|
+
clearTimeout(timer);
|
|
1630
|
+
pinReadyWaiters.delete(pinId);
|
|
1631
|
+
pendingImages.delete(pinId);
|
|
1632
|
+
}
|
|
1633
|
+
}
|
|
1634
|
+
function onPinBoot(sender) {
|
|
1635
|
+
if (rejectForeign(sender, CH.pinBoot)) return null;
|
|
1636
|
+
const s = session;
|
|
1637
|
+
if (!s && pendingImages.has(sender.id)) return takePendingImage(sender);
|
|
1638
|
+
if (!s || s.pendingPin?.webContents.id !== sender.id) {
|
|
1639
|
+
log.warn(
|
|
1640
|
+
`[snip] pin:boot \u88AB\u62D2\u7EDD sender=${sender.id} phase=${s?.phase ?? "none"} pending=${s?.pendingPin?.webContents.id ?? "none"}`
|
|
1641
|
+
);
|
|
1642
|
+
return null;
|
|
1643
|
+
}
|
|
1644
|
+
if (!hasRealSelection(s.selection)) {
|
|
1645
|
+
log.warn("[snip] pin:boot \u88AB\u62D2\u7EDD\uFF1A\u9009\u533A\u65E0\u6548");
|
|
1646
|
+
return null;
|
|
1647
|
+
}
|
|
1648
|
+
return {
|
|
1649
|
+
kind: "snip",
|
|
1650
|
+
shots: s.shots,
|
|
1651
|
+
selection: s.selection,
|
|
1652
|
+
outScale: resolveOutScale(s.shots, s.selection),
|
|
1653
|
+
dipWidth: s.selection.width,
|
|
1654
|
+
dipHeight: s.selection.height,
|
|
1655
|
+
padding: s.pinLayout?.padding ?? { top: 0, right: 0, bottom: 0, left: 0 },
|
|
1656
|
+
// 遮罩阶段画的标注(已换算),钉图 seed 进去继续可编辑
|
|
1657
|
+
initialShapes: toImageShapes(s)
|
|
1658
|
+
};
|
|
1659
|
+
}
|
|
1660
|
+
async function onPinAction(sender, payload) {
|
|
1661
|
+
if (rejectForeign(sender, CH.pinAction)) return { ok: false, error: "\u975E\u672C\u5305\u7A97\u53E3" };
|
|
1662
|
+
const win = import_electron7.BrowserWindow.fromWebContents(sender);
|
|
1663
|
+
if (!win || win.isDestroyed()) return { ok: false, error: "\u7A97\u53E3\u5DF2\u5173\u95ED" };
|
|
1664
|
+
try {
|
|
1665
|
+
switch (payload.kind) {
|
|
1666
|
+
case "copy":
|
|
1667
|
+
return await deliver("copy", payload.png, win);
|
|
1668
|
+
case "save":
|
|
1669
|
+
return await deliver("save", payload.png, win, void 0, payload.suggestedName);
|
|
1670
|
+
case "close":
|
|
1671
|
+
win.close();
|
|
1672
|
+
return { ok: true };
|
|
1673
|
+
case "zoom":
|
|
1674
|
+
pins.zoom(win, payload.factor);
|
|
1675
|
+
return { ok: true };
|
|
1676
|
+
case "opacity":
|
|
1677
|
+
pins.opacity(win, payload.delta);
|
|
1678
|
+
return { ok: true };
|
|
1679
|
+
case "ignore":
|
|
1680
|
+
pins.toggleMouseIgnore(win);
|
|
1681
|
+
return { ok: true };
|
|
1682
|
+
default:
|
|
1683
|
+
return { ok: false, error: "\u672A\u77E5\u64CD\u4F5C" };
|
|
1684
|
+
}
|
|
1685
|
+
} catch (err) {
|
|
1686
|
+
log.error("[pin] \u64CD\u4F5C\u5931\u8D25", err);
|
|
1687
|
+
return { ok: false, error: err instanceof Error ? err.message : String(err) };
|
|
1688
|
+
}
|
|
1689
|
+
}
|
|
1690
|
+
function hideOverlays() {
|
|
1691
|
+
if (!session) return;
|
|
1692
|
+
stopPoll(session);
|
|
1693
|
+
stopHoverPoll(session);
|
|
1694
|
+
const list = session.overlays;
|
|
1695
|
+
session.overlays = [];
|
|
1696
|
+
for (const win of list) {
|
|
1697
|
+
if (win.isDestroyed()) continue;
|
|
1698
|
+
win.webContents.send(CH.overlayTeardown, { reason: "confirm" });
|
|
1699
|
+
win.hide();
|
|
1700
|
+
}
|
|
1701
|
+
}
|
|
1702
|
+
function cancel(reason = "manual") {
|
|
1703
|
+
teardown(reason, true);
|
|
1704
|
+
}
|
|
1705
|
+
function release() {
|
|
1706
|
+
teardown("released", false);
|
|
1707
|
+
}
|
|
1708
|
+
function teardown(reason, notify) {
|
|
1709
|
+
epoch++;
|
|
1710
|
+
const s = session;
|
|
1711
|
+
session = null;
|
|
1712
|
+
if (s) log.info(`[snip] \u7ED3\u675F\u622A\u56FE (${reason}) phase=${s.phase} handedOff=${s.handedOff}`);
|
|
1713
|
+
if (s && notify) {
|
|
1714
|
+
opts.onCancel?.(reason);
|
|
1715
|
+
if (!s.handedOff) s.pick?.onCancel?.(reason);
|
|
1716
|
+
}
|
|
1717
|
+
if (readyTimer) {
|
|
1718
|
+
clearTimeout(readyTimer);
|
|
1719
|
+
readyTimer = null;
|
|
1720
|
+
}
|
|
1721
|
+
if (s) {
|
|
1722
|
+
stopPoll(s);
|
|
1723
|
+
stopHoverPoll(s);
|
|
1724
|
+
for (const win of s.overlays) {
|
|
1725
|
+
if (win.isDestroyed()) continue;
|
|
1726
|
+
win.webContents.send(CH.overlayTeardown, { reason: "cancel" });
|
|
1727
|
+
win.hide();
|
|
1728
|
+
}
|
|
1729
|
+
if (s.pendingPin && !s.pendingPin.isDestroyed()) s.pendingPin.close();
|
|
1730
|
+
}
|
|
1731
|
+
for (const resolve of pinReadyWaiters.values()) resolve(false);
|
|
1732
|
+
pinReadyWaiters.clear();
|
|
1733
|
+
const hadSession = !!s;
|
|
1734
|
+
restoreWindows(() => false);
|
|
1735
|
+
if (hadSession) {
|
|
1736
|
+
opts.onStateChange?.(false);
|
|
1737
|
+
opts.focusHost?.();
|
|
1738
|
+
}
|
|
1739
|
+
}
|
|
1740
|
+
const handlers = {
|
|
1741
|
+
overlayBoot: (sender) => onOverlayBoot(sender),
|
|
1742
|
+
overlayReady: (sender) => onOverlayReady(sender),
|
|
1743
|
+
overlayInput: (sender, payload) => onOverlayInput(sender, payload),
|
|
1744
|
+
overlayTool: (sender, tool) => onOverlayTool(sender, tool),
|
|
1745
|
+
overlayAction: (sender, id) => onOverlayAction(sender, id),
|
|
1746
|
+
overlayShapes: (sender, shapes) => onOverlayShapes(sender, shapes),
|
|
1747
|
+
overlayDraft: (sender, draft) => onOverlayDraft(sender, draft),
|
|
1748
|
+
overlayInFlight: (sender, index) => onOverlayInFlight(sender, index),
|
|
1749
|
+
overlayEditState: (sender, textEditing) => onOverlayEditState(sender, textEditing),
|
|
1750
|
+
overlayExport: (sender, kind, png) => onOverlayExport(sender, kind, png),
|
|
1751
|
+
overlayFocus: (sender) => {
|
|
1752
|
+
if (rejectForeign(sender, CH.overlayFocus)) return;
|
|
1753
|
+
const win = import_electron7.BrowserWindow.fromWebContents(sender);
|
|
1754
|
+
if (win && win.isVisible()) win.focus();
|
|
1755
|
+
},
|
|
1756
|
+
overlayCopyText: async (sender, text) => {
|
|
1757
|
+
if (rejectForeign(sender, CH.overlayCopyText)) return { ok: false, error: "\u975E\u672C\u5305\u7A97\u53E3" };
|
|
1758
|
+
try {
|
|
1759
|
+
copyText(text);
|
|
1760
|
+
return { ok: true };
|
|
1761
|
+
} catch (err) {
|
|
1762
|
+
return { ok: false, error: err instanceof Error ? err.message : String(err) };
|
|
1763
|
+
}
|
|
1764
|
+
},
|
|
1765
|
+
pinBoot: (sender) => onPinBoot(sender),
|
|
1766
|
+
pinReady: (sender) => markPinReady(sender),
|
|
1767
|
+
pinAction: (sender, payload) => onPinAction(sender, payload)
|
|
1768
|
+
};
|
|
1769
|
+
return {
|
|
1770
|
+
start: () => beginSession(null),
|
|
1771
|
+
pick: (options) => beginSession(options),
|
|
1772
|
+
cancel,
|
|
1773
|
+
release,
|
|
1774
|
+
pinImage,
|
|
1775
|
+
closePins: () => pins.closeAll(),
|
|
1776
|
+
setPinsMouseIgnore: (ignore) => pins.setIgnoreAll(ignore),
|
|
1777
|
+
install(target) {
|
|
1778
|
+
uninstallScreen?.();
|
|
1779
|
+
uninstallIpc?.();
|
|
1780
|
+
uninstallScreen = null;
|
|
1781
|
+
uninstallIpc = null;
|
|
1782
|
+
const onDisplayChange = () => {
|
|
1783
|
+
if (session) cancel("display-changed");
|
|
1784
|
+
};
|
|
1785
|
+
import_electron7.screen.on("display-metrics-changed", onDisplayChange);
|
|
1786
|
+
import_electron7.screen.on("display-removed", onDisplayChange);
|
|
1787
|
+
const removeScreen = () => {
|
|
1788
|
+
import_electron7.screen.off("display-metrics-changed", onDisplayChange);
|
|
1789
|
+
import_electron7.screen.off("display-removed", onDisplayChange);
|
|
1790
|
+
};
|
|
1791
|
+
const removeIpc = installIpc(target, handlers);
|
|
1792
|
+
let live = true;
|
|
1793
|
+
const uninstall = () => {
|
|
1794
|
+
if (!live) return;
|
|
1795
|
+
live = false;
|
|
1796
|
+
removeScreen();
|
|
1797
|
+
removeIpc();
|
|
1798
|
+
if (uninstallScreen === uninstall) uninstallScreen = null;
|
|
1799
|
+
if (uninstallIpc === uninstall) uninstallIpc = null;
|
|
1800
|
+
};
|
|
1801
|
+
uninstallScreen = uninstall;
|
|
1802
|
+
uninstallIpc = uninstall;
|
|
1803
|
+
return uninstall;
|
|
1804
|
+
},
|
|
1805
|
+
dispose() {
|
|
1806
|
+
cancel("dispose");
|
|
1807
|
+
uninstallScreen?.();
|
|
1808
|
+
uninstallIpc?.();
|
|
1809
|
+
uninstallScreen = null;
|
|
1810
|
+
uninstallIpc = null;
|
|
1811
|
+
overlays.destroyAll();
|
|
1812
|
+
pins.closeAll();
|
|
1813
|
+
}
|
|
1814
|
+
};
|
|
1815
|
+
}
|
|
1816
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
1817
|
+
0 && (module.exports = {
|
|
1818
|
+
captureDisplays,
|
|
1819
|
+
createSnipper,
|
|
1820
|
+
ensureScreenCaptureAccess
|
|
1821
|
+
});
|
|
1822
|
+
//# sourceMappingURL=index.cjs.map
|