截图功能优化

This commit is contained in:
muwoo
2023-04-10 14:30:51 +08:00
parent 2cd70bd386
commit d64eed6f7f
10 changed files with 61 additions and 101 deletions

View File

@@ -1,4 +1,5 @@
import PluginHandler from "@/core/plugin-handler";
import LocalDb from "@/core/db";
import PluginHandler from '@/core/plugin-handler';
import LocalDb from '@/core/db';
import screenCapture from '@/core/screen-capture';
export { PluginHandler, LocalDb };
export { PluginHandler, LocalDb, screenCapture };

View File

@@ -0,0 +1,40 @@
import { clipboard, Notification } from 'electron';
import { execFile, exec } from 'child_process';
import platform from '@/common/utils/commonConst';
import path from 'path';
// 截图方法windows
export const screenWindow = (cb) => {
const url = path.resolve(__static, 'PrintScr.exe');
const screen_window = execFile(url);
screen_window.on('exit', (code) => {
if (code) {
const image = clipboard.readImage();
cb && cb(image.isEmpty() ? '' : image.toDataURL());
}
});
};
// 截图方法mac
export const handleScreenShots = (cb) => {
exec('screencapture -i -r -c', () => {
const image = clipboard.readImage();
cb && cb(image.isEmpty() ? '' : image.toDataURL());
});
};
export default (mainWindow, cb) => {
// 接收到截图后的执行程序
mainWindow.hide();
clipboard.writeText('');
if (platform.macOS()) {
handleScreenShots(cb);
} else if (platform.windows()) {
screenWindow(cb);
} else {
new Notification({
title: '兼容性支持度不够',
body: 'Linux 系统截图暂不支持,我们将会尽快更新!',
}).show();
}
};

View File

@@ -11,11 +11,10 @@ import {
} from 'electron';
import { runner, detach } from '../browsers';
import fs from 'fs';
import { LocalDb } from '@/core';
import { LocalDb, screenCapture } from '@/core';
import plist from 'plist';
import { DECODE_KEY } from '@/common/constans/main';
import mainInstance from '../index';
import { screenshots } from './registerScreenshots';
const runnerInstance = runner();
const detachInstance = detach();
const dbInstance = new LocalDb(app.getPath('userData'));
@@ -34,14 +33,6 @@ class API {
event.returnValue = data;
// event.sender.send(`msg-back-${arg.type}`, data);
});
// 注册截屏成功回调事件
screenshots.on('ok', (e, buffer) => {
const image = nativeImage.createFromBuffer(buffer);
runnerInstance.executeHooks('ScreenCapture', {
data: image.toDataURL(),
});
});
}
public getCurrentWindow = (window, e) => {
@@ -323,8 +314,12 @@ class API {
return true;
}
public screenCapture() {
screenshots.startCapture();
public screenCapture(arg, window) {
screenCapture(window, (img) => {
runnerInstance.executeHooks('ScreenCapture', {
data: img,
});
});
}
}

View File

@@ -6,8 +6,9 @@ import {
screen,
ipcMain,
app,
Notification,
} from 'electron';
import { screenshots } from './registerScreenshots';
import screenCapture from '@/core/screen-capture';
const registerHotKey = (mainWindow: BrowserWindow): void => {
// 设置开机启动
@@ -79,7 +80,13 @@ const registerHotKey = (mainWindow: BrowserWindow): void => {
});
globalShortcut.register(config.perf.shortCut.capture, () => {
screenshots.startCapture();
screenCapture(mainWindow, (data) => {
data &&
new Notification({
title: '截图完成',
body: '截图以存储到系统剪贴板中',
}).show();
});
});
// globalShortcut.register(config.perf.shortCut.separate, () => {

View File

@@ -1,8 +0,0 @@
import Screenshots from 'electron-screenshots';
let screenshots;
const initScreenShots = () => {
screenshots = new Screenshots();
};
export { initScreenShots, screenshots };

View File

@@ -17,7 +17,6 @@ import '../common/utils/localPlugin';
import '../common/utils/localConfig';
import registerySystemPlugin from './common/registerySystemPlugin';
import { initScreenShots } from './common/registerScreenshots';
class App {
public windowCreator: { init: () => void; getWindow: () => BrowserWindow };
@@ -59,7 +58,6 @@ class App {
const readyFunction = () => {
this.createWindow();
const mainWindow = this.windowCreator.getWindow();
initScreenShots();
API.init(mainWindow);
createTray(this.windowCreator.getWindow());
registerHotKey(this.windowCreator.getWindow());