electron-screenshots 0.5.27 → 0.6.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/README.md +248 -231
- package/lib/demo.d.ts +1 -0
- package/lib/demo.d.ts.map +1 -0
- package/lib/demo.js +16 -15
- package/lib/demo.js.map +1 -0
- package/lib/event.d.ts +1 -0
- package/lib/event.d.ts.map +1 -0
- package/lib/event.js +6 -6
- package/lib/event.js.map +1 -0
- package/lib/getDisplay.d.ts +2 -1
- package/lib/getDisplay.d.ts.map +1 -0
- package/lib/getDisplay.js +8 -7
- package/lib/getDisplay.js.map +1 -0
- package/lib/index.cjs.d.ts +2 -0
- package/lib/index.cjs.d.ts.map +1 -0
- package/lib/index.cjs.js +2 -0
- package/lib/index.cjs.js.map +1 -0
- package/lib/index.d.ts +6 -6
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +260 -395
- package/lib/index.js.map +1 -0
- package/lib/padStart.d.ts +1 -0
- package/lib/padStart.d.ts.map +1 -0
- package/lib/padStart.js +5 -6
- package/lib/padStart.js.map +1 -0
- package/lib/preload.d.ts +2 -1
- package/lib/preload.d.ts.map +1 -0
- package/lib/preload.js +18 -30
- package/lib/preload.js.map +1 -0
- package/package.json +21 -29
package/lib/index.js
CHANGED
|
@@ -1,42 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __extends = (this && this.__extends) || (function () {
|
|
3
|
-
var extendStatics = function (d, b) {
|
|
4
|
-
extendStatics = Object.setPrototypeOf ||
|
|
5
|
-
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
-
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
-
return extendStatics(d, b);
|
|
8
|
-
};
|
|
9
|
-
return function (d, b) {
|
|
10
|
-
if (typeof b !== "function" && b !== null)
|
|
11
|
-
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
-
extendStatics(d, b);
|
|
13
|
-
function __() { this.constructor = d; }
|
|
14
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
-
};
|
|
16
|
-
})();
|
|
17
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
18
|
-
if (k2 === undefined) k2 = k;
|
|
19
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
20
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
21
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
22
|
-
}
|
|
23
|
-
Object.defineProperty(o, k2, desc);
|
|
24
|
-
}) : (function(o, m, k, k2) {
|
|
25
|
-
if (k2 === undefined) k2 = k;
|
|
26
|
-
o[k2] = m[k];
|
|
27
|
-
}));
|
|
28
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
29
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
30
|
-
}) : function(o, v) {
|
|
31
|
-
o["default"] = v;
|
|
32
|
-
});
|
|
33
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
34
|
-
if (mod && mod.__esModule) return mod;
|
|
35
|
-
var result = {};
|
|
36
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
37
|
-
__setModuleDefault(result, mod);
|
|
38
|
-
return result;
|
|
39
|
-
};
|
|
40
2
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
41
3
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
42
4
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -46,419 +8,322 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
46
8
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
47
9
|
});
|
|
48
10
|
};
|
|
49
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
50
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
51
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
52
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
53
|
-
function step(op) {
|
|
54
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
55
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
56
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
57
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
58
|
-
switch (op[0]) {
|
|
59
|
-
case 0: case 1: t = op; break;
|
|
60
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
61
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
62
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
63
|
-
default:
|
|
64
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
65
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
66
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
67
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
68
|
-
if (t[2]) _.ops.pop();
|
|
69
|
-
_.trys.pop(); continue;
|
|
70
|
-
}
|
|
71
|
-
op = body.call(thisArg, _);
|
|
72
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
73
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
74
|
-
}
|
|
75
|
-
};
|
|
76
11
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
77
12
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
78
13
|
};
|
|
79
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
var _this = _super.call(this) || this;
|
|
15
|
+
const debug_1 = __importDefault(require("debug"));
|
|
16
|
+
const electron_1 = require("electron");
|
|
17
|
+
const events_1 = __importDefault(require("events"));
|
|
18
|
+
const fs_extra_1 = __importDefault(require("fs-extra"));
|
|
19
|
+
const event_js_1 = __importDefault(require("./event.js"));
|
|
20
|
+
const getDisplay_js_1 = __importDefault(require("./getDisplay.js"));
|
|
21
|
+
const padStart_js_1 = __importDefault(require("./padStart.js"));
|
|
22
|
+
class Screenshots extends events_1.default {
|
|
23
|
+
constructor(opts) {
|
|
24
|
+
super();
|
|
91
25
|
// 截图窗口对象
|
|
92
|
-
|
|
93
|
-
|
|
26
|
+
this.$win = null;
|
|
27
|
+
this.$view = new electron_1.BrowserView({
|
|
94
28
|
webPreferences: {
|
|
95
|
-
preload: require.resolve(
|
|
29
|
+
preload: require.resolve("./preload.js"),
|
|
96
30
|
nodeIntegration: false,
|
|
97
31
|
contextIsolation: true,
|
|
98
32
|
},
|
|
99
33
|
});
|
|
100
|
-
|
|
101
|
-
electron_1.ipcMain.once(
|
|
102
|
-
|
|
34
|
+
this.isReady = new Promise((resolve) => {
|
|
35
|
+
electron_1.ipcMain.once("SCREENSHOTS:ready", () => {
|
|
36
|
+
this.logger("SCREENSHOTS:ready");
|
|
103
37
|
resolve();
|
|
104
38
|
});
|
|
105
39
|
});
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
40
|
+
this.logger = (opts === null || opts === void 0 ? void 0 : opts.logger) || (0, debug_1.default)("electron-screenshots");
|
|
41
|
+
this.singleWindow = (opts === null || opts === void 0 ? void 0 : opts.singleWindow) || false;
|
|
42
|
+
this.listenIpc();
|
|
43
|
+
this.$view.webContents.loadURL(`file://${require.resolve("react-screenshots/dist/electron.html")}`);
|
|
110
44
|
if (opts === null || opts === void 0 ? void 0 : opts.lang) {
|
|
111
|
-
|
|
45
|
+
this.setLang(opts.lang);
|
|
112
46
|
}
|
|
113
|
-
return _this;
|
|
114
47
|
}
|
|
115
48
|
/**
|
|
116
49
|
* 开始截图
|
|
117
50
|
*/
|
|
118
|
-
|
|
119
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
display = (0, getDisplay_1.default)();
|
|
126
|
-
return [4 /*yield*/, Promise.all([this.capture(display), this.isReady])];
|
|
127
|
-
case 1:
|
|
128
|
-
imageUrl = (_a.sent())[0];
|
|
129
|
-
return [4 /*yield*/, this.createWindow(display)];
|
|
130
|
-
case 2:
|
|
131
|
-
_a.sent();
|
|
132
|
-
this.$view.webContents.send('SCREENSHOTS:capture', display, imageUrl);
|
|
133
|
-
return [2 /*return*/];
|
|
134
|
-
}
|
|
135
|
-
});
|
|
51
|
+
startCapture() {
|
|
52
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
53
|
+
this.logger("startCapture");
|
|
54
|
+
const display = (0, getDisplay_js_1.default)();
|
|
55
|
+
const [imageUrl] = yield Promise.all([this.capture(display), this.isReady]);
|
|
56
|
+
yield this.createWindow(display);
|
|
57
|
+
this.$view.webContents.send("SCREENSHOTS:capture", display, imageUrl);
|
|
136
58
|
});
|
|
137
|
-
}
|
|
59
|
+
}
|
|
138
60
|
/**
|
|
139
61
|
* 结束截图
|
|
140
62
|
*/
|
|
141
|
-
|
|
142
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
this.$win.hide();
|
|
161
|
-
}
|
|
162
|
-
else {
|
|
163
|
-
this.$win.destroy();
|
|
164
|
-
}
|
|
165
|
-
return [2 /*return*/];
|
|
166
|
-
}
|
|
167
|
-
});
|
|
63
|
+
endCapture() {
|
|
64
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
65
|
+
this.logger("endCapture");
|
|
66
|
+
yield this.reset();
|
|
67
|
+
if (!this.$win) {
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
// 先清除 Kiosk 模式,然后取消全屏才有效
|
|
71
|
+
this.$win.setKiosk(false);
|
|
72
|
+
this.$win.blur();
|
|
73
|
+
this.$win.blurWebView();
|
|
74
|
+
this.$win.unmaximize();
|
|
75
|
+
this.$win.removeBrowserView(this.$view);
|
|
76
|
+
if (this.singleWindow) {
|
|
77
|
+
this.$win.hide();
|
|
78
|
+
}
|
|
79
|
+
else {
|
|
80
|
+
this.$win.destroy();
|
|
81
|
+
}
|
|
168
82
|
});
|
|
169
|
-
}
|
|
83
|
+
}
|
|
170
84
|
/**
|
|
171
85
|
* 设置语言
|
|
172
86
|
*/
|
|
173
|
-
|
|
174
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
this.logger('setLang', lang);
|
|
179
|
-
return [4 /*yield*/, this.isReady];
|
|
180
|
-
case 1:
|
|
181
|
-
_a.sent();
|
|
182
|
-
this.$view.webContents.send('SCREENSHOTS:setLang', lang);
|
|
183
|
-
return [2 /*return*/];
|
|
184
|
-
}
|
|
185
|
-
});
|
|
87
|
+
setLang(lang) {
|
|
88
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
89
|
+
this.logger("setLang", lang);
|
|
90
|
+
yield this.isReady;
|
|
91
|
+
this.$view.webContents.send("SCREENSHOTS:setLang", lang);
|
|
186
92
|
});
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
electron_1.ipcMain.once('SCREENSHOTS:reset', function () { return resolve(); });
|
|
202
|
-
}),
|
|
203
|
-
])];
|
|
204
|
-
case 1:
|
|
205
|
-
// 保证 UI 有足够的时间渲染
|
|
206
|
-
_a.sent();
|
|
207
|
-
return [2 /*return*/];
|
|
208
|
-
}
|
|
209
|
-
});
|
|
93
|
+
}
|
|
94
|
+
reset() {
|
|
95
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
96
|
+
// 重置截图区域
|
|
97
|
+
this.$view.webContents.send("SCREENSHOTS:reset");
|
|
98
|
+
// 保证 UI 有足够的时间渲染
|
|
99
|
+
yield Promise.race([
|
|
100
|
+
new Promise((resolve) => {
|
|
101
|
+
setTimeout(() => resolve(), 500);
|
|
102
|
+
}),
|
|
103
|
+
new Promise((resolve) => {
|
|
104
|
+
electron_1.ipcMain.once("SCREENSHOTS:reset", () => resolve());
|
|
105
|
+
}),
|
|
106
|
+
]);
|
|
210
107
|
});
|
|
211
|
-
}
|
|
108
|
+
}
|
|
212
109
|
/**
|
|
213
110
|
* 初始化窗口
|
|
214
111
|
*/
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
}
|
|
296
|
-
this.$win.blur();
|
|
297
|
-
this.$win.setBounds(display);
|
|
298
|
-
this.$view.setBounds({
|
|
299
|
-
x: 0,
|
|
300
|
-
y: 0,
|
|
301
|
-
width: display.width,
|
|
302
|
-
height: display.height,
|
|
303
|
-
});
|
|
304
|
-
this.$win.setAlwaysOnTop(true);
|
|
305
|
-
this.$win.show();
|
|
306
|
-
return [2 /*return*/];
|
|
307
|
-
}
|
|
112
|
+
createWindow(display) {
|
|
113
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
114
|
+
var _a, _b;
|
|
115
|
+
// 重置截图区域
|
|
116
|
+
yield this.reset();
|
|
117
|
+
// 复用未销毁的窗口
|
|
118
|
+
if (!this.$win || ((_b = (_a = this.$win) === null || _a === void 0 ? void 0 : _a.isDestroyed) === null || _b === void 0 ? void 0 : _b.call(_a))) {
|
|
119
|
+
const windowTypes = {
|
|
120
|
+
darwin: "panel",
|
|
121
|
+
// linux 必须设置为 undefined,否则会在部分系统上不能触发focus 事件
|
|
122
|
+
// https://github.com/nashaofu/screenshots/issues/203#issuecomment-1518923486
|
|
123
|
+
linux: undefined,
|
|
124
|
+
win32: "toolbar",
|
|
125
|
+
};
|
|
126
|
+
this.$win = new electron_1.BrowserWindow({
|
|
127
|
+
title: "screenshots",
|
|
128
|
+
x: display.x,
|
|
129
|
+
y: display.y,
|
|
130
|
+
width: display.width,
|
|
131
|
+
height: display.height,
|
|
132
|
+
useContentSize: true,
|
|
133
|
+
type: windowTypes[process.platform],
|
|
134
|
+
frame: false,
|
|
135
|
+
show: false,
|
|
136
|
+
autoHideMenuBar: true,
|
|
137
|
+
transparent: true,
|
|
138
|
+
resizable: false,
|
|
139
|
+
movable: false,
|
|
140
|
+
minimizable: false,
|
|
141
|
+
maximizable: false,
|
|
142
|
+
// focusable 必须设置为 true, 否则窗口不能及时响应esc按键,输入框也不能输入
|
|
143
|
+
focusable: true,
|
|
144
|
+
skipTaskbar: true,
|
|
145
|
+
alwaysOnTop: true,
|
|
146
|
+
/**
|
|
147
|
+
* linux 下必须设置为false,否则不能全屏显示在最上层
|
|
148
|
+
* mac 下设置为false,否则可能会导致程序坞不恢复问题,且与 kiosk 模式冲突
|
|
149
|
+
*/
|
|
150
|
+
fullscreen: false,
|
|
151
|
+
// mac fullscreenable 设置为 true 会导致应用崩溃
|
|
152
|
+
fullscreenable: false,
|
|
153
|
+
kiosk: true,
|
|
154
|
+
backgroundColor: "#00000000",
|
|
155
|
+
titleBarStyle: "hidden",
|
|
156
|
+
hasShadow: false,
|
|
157
|
+
paintWhenInitiallyHidden: false,
|
|
158
|
+
// mac 特有的属性
|
|
159
|
+
roundedCorners: false,
|
|
160
|
+
enableLargerThanScreen: false,
|
|
161
|
+
acceptFirstMouse: true,
|
|
162
|
+
});
|
|
163
|
+
this.emit("windowCreated", this.$win);
|
|
164
|
+
this.$win.on("show", () => {
|
|
165
|
+
var _a, _b;
|
|
166
|
+
(_a = this.$win) === null || _a === void 0 ? void 0 : _a.focus();
|
|
167
|
+
(_b = this.$win) === null || _b === void 0 ? void 0 : _b.setKiosk(true);
|
|
168
|
+
});
|
|
169
|
+
this.$win.on("closed", () => {
|
|
170
|
+
this.emit("windowClosed", this.$win);
|
|
171
|
+
this.$win = null;
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
this.$win.setBrowserView(this.$view);
|
|
175
|
+
// 适定平台
|
|
176
|
+
if (process.platform === "darwin") {
|
|
177
|
+
this.$win.setWindowButtonVisibility(false);
|
|
178
|
+
}
|
|
179
|
+
if (process.platform !== "win32") {
|
|
180
|
+
this.$win.setVisibleOnAllWorkspaces(true, {
|
|
181
|
+
visibleOnFullScreen: true,
|
|
182
|
+
skipTransformProcessType: true,
|
|
183
|
+
});
|
|
184
|
+
}
|
|
185
|
+
this.$win.blur();
|
|
186
|
+
this.$win.setBounds(display);
|
|
187
|
+
this.$view.setBounds({
|
|
188
|
+
x: 0,
|
|
189
|
+
y: 0,
|
|
190
|
+
width: display.width,
|
|
191
|
+
height: display.height,
|
|
308
192
|
});
|
|
193
|
+
this.$win.setAlwaysOnTop(true);
|
|
194
|
+
this.$win.show();
|
|
309
195
|
});
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
case 2:
|
|
323
|
-
Monitor = (_a.sent()).Monitor;
|
|
324
|
-
monitor = Monitor.fromPoint(display.x + display.width / 2, display.y + display.height / 2);
|
|
325
|
-
this.logger('SCREENSHOTS:capture Monitor.fromPoint arguments %o', display);
|
|
326
|
-
this.logger('SCREENSHOTS:capture Monitor.fromPoint return %o', {
|
|
327
|
-
id: monitor === null || monitor === void 0 ? void 0 : monitor.id,
|
|
328
|
-
name: monitor === null || monitor === void 0 ? void 0 : monitor.name,
|
|
329
|
-
x: monitor === null || monitor === void 0 ? void 0 : monitor.x,
|
|
330
|
-
y: monitor === null || monitor === void 0 ? void 0 : monitor.y,
|
|
331
|
-
width: monitor === null || monitor === void 0 ? void 0 : monitor.width,
|
|
332
|
-
height: monitor === null || monitor === void 0 ? void 0 : monitor.height,
|
|
333
|
-
rotation: monitor === null || monitor === void 0 ? void 0 : monitor.rotation,
|
|
334
|
-
scaleFactor: monitor === null || monitor === void 0 ? void 0 : monitor.scaleFactor,
|
|
335
|
-
frequency: monitor === null || monitor === void 0 ? void 0 : monitor.frequency,
|
|
336
|
-
isPrimary: monitor === null || monitor === void 0 ? void 0 : monitor.isPrimary,
|
|
337
|
-
});
|
|
338
|
-
if (!monitor) {
|
|
339
|
-
throw new Error("Monitor.fromDisplay(".concat(display.id, ") get null"));
|
|
340
|
-
}
|
|
341
|
-
return [4 /*yield*/, monitor.captureImage()];
|
|
342
|
-
case 3:
|
|
343
|
-
image = _a.sent();
|
|
344
|
-
return [4 /*yield*/, image.toPng(true)];
|
|
345
|
-
case 4:
|
|
346
|
-
buffer = _a.sent();
|
|
347
|
-
return [2 /*return*/, "data:image/png;base64,".concat(buffer.toString('base64'))];
|
|
348
|
-
case 5:
|
|
349
|
-
err_1 = _a.sent();
|
|
350
|
-
this.logger('SCREENSHOTS:capture Monitor capture() error %o', err_1);
|
|
351
|
-
return [4 /*yield*/, electron_1.desktopCapturer.getSources({
|
|
352
|
-
types: ['screen'],
|
|
353
|
-
thumbnailSize: {
|
|
354
|
-
width: display.width * display.scaleFactor,
|
|
355
|
-
height: display.height * display.scaleFactor,
|
|
356
|
-
},
|
|
357
|
-
})];
|
|
358
|
-
case 6:
|
|
359
|
-
sources = _a.sent();
|
|
360
|
-
source = void 0;
|
|
361
|
-
// Linux系统上,screen.getDisplayNearestPoint 返回的 Display 对象的 id
|
|
362
|
-
// 和这里 source 对象上的 display_id(Linux上,这个值是空字符串) 或 id 的中间部分,都不一致
|
|
363
|
-
// 但是,如果只有一个显示器的话,其实不用判断,直接返回就行
|
|
364
|
-
if (sources.length === 1) {
|
|
365
|
-
source = sources[0];
|
|
366
|
-
}
|
|
367
|
-
else {
|
|
368
|
-
source = sources.find(function (item) { return item.display_id === display.id.toString()
|
|
369
|
-
|| item.id.startsWith("screen:".concat(display.id, ":")); });
|
|
370
|
-
}
|
|
371
|
-
if (!source) {
|
|
372
|
-
this.logger("SCREENSHOTS:capture Can't find screen source. sources: %o, display: %o", sources, display);
|
|
373
|
-
throw new Error("Can't find screen source");
|
|
374
|
-
}
|
|
375
|
-
return [2 /*return*/, source.thumbnail.toDataURL()];
|
|
376
|
-
case 7: return [2 /*return*/];
|
|
196
|
+
}
|
|
197
|
+
capture(display) {
|
|
198
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
199
|
+
this.logger("SCREENSHOTS:capture");
|
|
200
|
+
try {
|
|
201
|
+
const { Monitor } = yield import("node-screenshots");
|
|
202
|
+
let point = {
|
|
203
|
+
x: display.x + display.width / 2,
|
|
204
|
+
y: display.y + display.height / 2,
|
|
205
|
+
};
|
|
206
|
+
if (process.platform === "win32") {
|
|
207
|
+
point = electron_1.screen.screenToDipPoint(point);
|
|
377
208
|
}
|
|
378
|
-
|
|
209
|
+
const monitor = Monitor.fromPoint(point.x, point.y);
|
|
210
|
+
this.logger("SCREENSHOTS:capture Monitor.fromPoint arguments %o", display);
|
|
211
|
+
this.logger("SCREENSHOTS:capture Monitor.fromPoint return %o", {
|
|
212
|
+
id: monitor === null || monitor === void 0 ? void 0 : monitor.id,
|
|
213
|
+
name: monitor === null || monitor === void 0 ? void 0 : monitor.name,
|
|
214
|
+
x: monitor === null || monitor === void 0 ? void 0 : monitor.x,
|
|
215
|
+
y: monitor === null || monitor === void 0 ? void 0 : monitor.y,
|
|
216
|
+
width: monitor === null || monitor === void 0 ? void 0 : monitor.width,
|
|
217
|
+
height: monitor === null || monitor === void 0 ? void 0 : monitor.height,
|
|
218
|
+
rotation: monitor === null || monitor === void 0 ? void 0 : monitor.rotation,
|
|
219
|
+
scaleFactor: monitor === null || monitor === void 0 ? void 0 : monitor.scaleFactor,
|
|
220
|
+
frequency: monitor === null || monitor === void 0 ? void 0 : monitor.frequency,
|
|
221
|
+
isPrimary: monitor === null || monitor === void 0 ? void 0 : monitor.isPrimary,
|
|
222
|
+
});
|
|
223
|
+
if (!monitor) {
|
|
224
|
+
throw new Error(`Monitor.fromDisplay(${display.id}) get null`);
|
|
225
|
+
}
|
|
226
|
+
const image = yield monitor.captureImage();
|
|
227
|
+
const buffer = yield image.toPng(true);
|
|
228
|
+
return `data:image/png;base64,${buffer.toString("base64")}`;
|
|
229
|
+
}
|
|
230
|
+
catch (err) {
|
|
231
|
+
this.logger("SCREENSHOTS:capture Monitor capture() error %o", err);
|
|
232
|
+
const sources = yield electron_1.desktopCapturer.getSources({
|
|
233
|
+
types: ["screen"],
|
|
234
|
+
thumbnailSize: {
|
|
235
|
+
width: display.width * display.scaleFactor,
|
|
236
|
+
height: display.height * display.scaleFactor,
|
|
237
|
+
},
|
|
238
|
+
});
|
|
239
|
+
let source;
|
|
240
|
+
// Linux系统上,screen.getDisplayNearestPoint 返回的 Display 对象的 id
|
|
241
|
+
// 和这里 source 对象上的 display_id(Linux上,这个值是空字符串) 或 id 的中间部分,都不一致
|
|
242
|
+
// 但是,如果只有一个显示器的话,其实不用判断,直接返回就行
|
|
243
|
+
if (sources.length === 1) {
|
|
244
|
+
[source] = sources;
|
|
245
|
+
}
|
|
246
|
+
else {
|
|
247
|
+
source = sources.find((item) => item.display_id === display.id.toString() ||
|
|
248
|
+
item.id.startsWith(`screen:${display.id}:`));
|
|
249
|
+
}
|
|
250
|
+
if (!source) {
|
|
251
|
+
this.logger("SCREENSHOTS:capture Can't find screen source. sources: %o, display: %o", sources, display);
|
|
252
|
+
throw new Error("Can't find screen source");
|
|
253
|
+
}
|
|
254
|
+
return source.thumbnail.toDataURL();
|
|
255
|
+
}
|
|
379
256
|
});
|
|
380
|
-
}
|
|
257
|
+
}
|
|
381
258
|
/**
|
|
382
259
|
* 绑定ipc时间处理
|
|
383
260
|
*/
|
|
384
|
-
|
|
385
|
-
var _this = this;
|
|
261
|
+
listenIpc() {
|
|
386
262
|
/**
|
|
387
263
|
* OK事件
|
|
388
264
|
*/
|
|
389
|
-
electron_1.ipcMain.on(
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
265
|
+
electron_1.ipcMain.on("SCREENSHOTS:ok", (e, buffer, data) => {
|
|
266
|
+
this.logger("SCREENSHOTS:ok buffer.length %d, data: %o", buffer.length, data);
|
|
267
|
+
const event = new event_js_1.default();
|
|
268
|
+
this.emit("ok", event, buffer, data);
|
|
393
269
|
if (event.defaultPrevented) {
|
|
394
270
|
return;
|
|
395
271
|
}
|
|
396
272
|
electron_1.clipboard.writeImage(electron_1.nativeImage.createFromBuffer(buffer));
|
|
397
|
-
|
|
273
|
+
this.endCapture();
|
|
398
274
|
});
|
|
399
275
|
/**
|
|
400
276
|
* CANCEL事件
|
|
401
277
|
*/
|
|
402
|
-
electron_1.ipcMain.on(
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
278
|
+
electron_1.ipcMain.on("SCREENSHOTS:cancel", () => {
|
|
279
|
+
this.logger("SCREENSHOTS:cancel");
|
|
280
|
+
const event = new event_js_1.default();
|
|
281
|
+
this.emit("cancel", event);
|
|
406
282
|
if (event.defaultPrevented) {
|
|
407
283
|
return;
|
|
408
284
|
}
|
|
409
|
-
|
|
285
|
+
this.endCapture();
|
|
410
286
|
});
|
|
411
287
|
/**
|
|
412
288
|
* SAVE事件
|
|
413
289
|
*/
|
|
414
|
-
electron_1.ipcMain.on(
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
filters: [
|
|
437
|
-
{ name: 'Image (png)', extensions: ['png'] },
|
|
438
|
-
{ name: 'All Files', extensions: ['*'] },
|
|
439
|
-
],
|
|
440
|
-
})];
|
|
441
|
-
case 1:
|
|
442
|
-
_a = _b.sent(), canceled = _a.canceled, filePath = _a.filePath;
|
|
443
|
-
if (!this.$win) {
|
|
444
|
-
this.emit('afterSave', new event_1.default(), buffer, data, false); // isSaved = false
|
|
445
|
-
return [2 /*return*/];
|
|
446
|
-
}
|
|
447
|
-
this.$win.setAlwaysOnTop(true);
|
|
448
|
-
if (canceled || !filePath) {
|
|
449
|
-
this.emit('afterSave', new event_1.default(), buffer, data, false); // isSaved = false
|
|
450
|
-
return [2 /*return*/];
|
|
451
|
-
}
|
|
452
|
-
return [4 /*yield*/, fs_extra_1.default.writeFile(filePath, buffer)];
|
|
453
|
-
case 2:
|
|
454
|
-
_b.sent();
|
|
455
|
-
this.emit('afterSave', new event_1.default(), buffer, data, true); // isSaved = true
|
|
456
|
-
this.endCapture();
|
|
457
|
-
return [2 /*return*/];
|
|
458
|
-
}
|
|
290
|
+
electron_1.ipcMain.on("SCREENSHOTS:save", (e, buffer, data) => __awaiter(this, void 0, void 0, function* () {
|
|
291
|
+
this.logger("SCREENSHOTS:save buffer.length %d, data: %o", buffer.length, data);
|
|
292
|
+
const event = new event_js_1.default();
|
|
293
|
+
this.emit("save", event, buffer, data);
|
|
294
|
+
if (event.defaultPrevented || !this.$win) {
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
297
|
+
const time = new Date();
|
|
298
|
+
const year = time.getFullYear();
|
|
299
|
+
const month = (0, padStart_js_1.default)(time.getMonth() + 1, 2, "0");
|
|
300
|
+
const date = (0, padStart_js_1.default)(time.getDate(), 2, "0");
|
|
301
|
+
const hours = (0, padStart_js_1.default)(time.getHours(), 2, "0");
|
|
302
|
+
const minutes = (0, padStart_js_1.default)(time.getMinutes(), 2, "0");
|
|
303
|
+
const seconds = (0, padStart_js_1.default)(time.getSeconds(), 2, "0");
|
|
304
|
+
const milliseconds = (0, padStart_js_1.default)(time.getMilliseconds(), 3, "0");
|
|
305
|
+
this.$win.setAlwaysOnTop(false);
|
|
306
|
+
const { canceled, filePath } = yield electron_1.dialog.showSaveDialog(this.$win, {
|
|
307
|
+
defaultPath: `${year}${month}${date}${hours}${minutes}${seconds}${milliseconds}.png`,
|
|
308
|
+
filters: [
|
|
309
|
+
{ name: "Image (png)", extensions: ["png"] },
|
|
310
|
+
{ name: "All Files", extensions: ["*"] },
|
|
311
|
+
],
|
|
459
312
|
});
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
}
|
|
313
|
+
if (!this.$win) {
|
|
314
|
+
this.emit("afterSave", new event_js_1.default(), buffer, data, false); // isSaved = false
|
|
315
|
+
return;
|
|
316
|
+
}
|
|
317
|
+
this.$win.setAlwaysOnTop(true);
|
|
318
|
+
if (canceled || !filePath) {
|
|
319
|
+
this.emit("afterSave", new event_js_1.default(), buffer, data, false); // isSaved = false
|
|
320
|
+
return;
|
|
321
|
+
}
|
|
322
|
+
yield fs_extra_1.default.writeFile(filePath, buffer);
|
|
323
|
+
this.emit("afterSave", new event_js_1.default(), buffer, data, true); // isSaved = true
|
|
324
|
+
this.endCapture();
|
|
325
|
+
}));
|
|
326
|
+
}
|
|
327
|
+
}
|
|
464
328
|
exports.default = Screenshots;
|
|
329
|
+
//# sourceMappingURL=index.js.map
|