mirror of
https://github.com/rubickCenter/rubick
synced 2025-12-25 20:09:27 +08:00
:spark: 支持系统截屏功能
This commit is contained in:
@@ -1,12 +1,13 @@
|
||||
import commonConst from "@/common/utils/commonConst";
|
||||
import commonConst from '@/common/utils/commonConst';
|
||||
|
||||
export default {
|
||||
version: 4,
|
||||
version: 5,
|
||||
perf: {
|
||||
shortCut: {
|
||||
showAndHidden: "Option+R",
|
||||
separate: "Ctrl+D",
|
||||
quit: "Shift+Escape",
|
||||
showAndHidden: 'Option+R',
|
||||
separate: 'Ctrl+D',
|
||||
quit: 'Shift+Escape',
|
||||
capture: 'Ctrl+Shift+A',
|
||||
},
|
||||
common: {
|
||||
start: true,
|
||||
@@ -14,7 +15,7 @@ export default {
|
||||
// 是否失焦隐藏。默认在dev环境不隐藏,在打包后隐藏。
|
||||
hideOnBlur: commonConst.production(),
|
||||
autoPast: false,
|
||||
darkMode: false
|
||||
darkMode: false,
|
||||
},
|
||||
local: {
|
||||
search: true,
|
||||
|
||||
@@ -1,27 +1,31 @@
|
||||
import path from "path";
|
||||
import fs from "fs";
|
||||
import getLocalDataFile from "./getLocalDataFile";
|
||||
import { PluginHandler } from "@/core";
|
||||
import { PLUGIN_INSTALL_DIR as baseDir } from "@/common/constans/main";
|
||||
import API from "@/main/common/api";
|
||||
import path from 'path';
|
||||
import fs from 'fs';
|
||||
import getLocalDataFile from './getLocalDataFile';
|
||||
import { PluginHandler } from '@/core';
|
||||
import { PLUGIN_INSTALL_DIR as baseDir } from '@/common/constans/main';
|
||||
import API from '@/main/common/api';
|
||||
|
||||
const configPath = path.join(getLocalDataFile(), "./rubick-local-plugin.json");
|
||||
const configPath = path.join(getLocalDataFile(), './rubick-local-plugin.json');
|
||||
|
||||
let registry;
|
||||
let pluginInstance;
|
||||
(async () => {
|
||||
try {
|
||||
registry = (await API.dbGet({ data: { id: "rubick-localhost-config" } }))
|
||||
.data.register;
|
||||
console.log(registry);
|
||||
const res = await API.dbGet({
|
||||
data: {
|
||||
id: 'rubick-localhost-config',
|
||||
},
|
||||
});
|
||||
|
||||
registry = res && res.data.register;
|
||||
pluginInstance = new PluginHandler({
|
||||
baseDir,
|
||||
registry
|
||||
registry,
|
||||
});
|
||||
} catch (e) {
|
||||
pluginInstance = new PluginHandler({
|
||||
baseDir,
|
||||
registry
|
||||
registry,
|
||||
});
|
||||
}
|
||||
})();
|
||||
@@ -32,13 +36,13 @@ global.LOCAL_PLUGINS = {
|
||||
await pluginInstance.install([plugin.name], { isDev: plugin.isDev });
|
||||
if (plugin.isDev) {
|
||||
// 获取 dev 插件信息
|
||||
const pluginPath = path.resolve(baseDir, "node_modules", plugin.name);
|
||||
const pluginPath = path.resolve(baseDir, 'node_modules', plugin.name);
|
||||
const pluginInfo = JSON.parse(
|
||||
fs.readFileSync(path.join(pluginPath, "./package.json"), "utf8")
|
||||
fs.readFileSync(path.join(pluginPath, './package.json'), 'utf8')
|
||||
);
|
||||
plugin = {
|
||||
...plugin,
|
||||
...pluginInfo
|
||||
...pluginInfo,
|
||||
};
|
||||
}
|
||||
global.LOCAL_PLUGINS.addPlugin(plugin);
|
||||
@@ -46,18 +50,18 @@ global.LOCAL_PLUGINS = {
|
||||
},
|
||||
refreshPlugin(plugin) {
|
||||
// 获取 dev 插件信息
|
||||
const pluginPath = path.resolve(baseDir, "node_modules", plugin.name);
|
||||
const pluginPath = path.resolve(baseDir, 'node_modules', plugin.name);
|
||||
const pluginInfo = JSON.parse(
|
||||
fs.readFileSync(path.join(pluginPath, "./package.json"), "utf8")
|
||||
fs.readFileSync(path.join(pluginPath, './package.json'), 'utf8')
|
||||
);
|
||||
plugin = {
|
||||
...plugin,
|
||||
...pluginInfo
|
||||
...pluginInfo,
|
||||
};
|
||||
// 刷新
|
||||
let currentPlugins = global.LOCAL_PLUGINS.getLocalPlugins();
|
||||
|
||||
currentPlugins = currentPlugins.map(p => {
|
||||
currentPlugins = currentPlugins.map((p) => {
|
||||
if (p.name === plugin.name) {
|
||||
return plugin;
|
||||
}
|
||||
@@ -73,7 +77,7 @@ global.LOCAL_PLUGINS = {
|
||||
try {
|
||||
if (!global.LOCAL_PLUGINS.PLUGINS.length) {
|
||||
global.LOCAL_PLUGINS.PLUGINS = JSON.parse(
|
||||
fs.readFileSync(configPath, "utf-8")
|
||||
fs.readFileSync(configPath, 'utf-8')
|
||||
);
|
||||
}
|
||||
return global.LOCAL_PLUGINS.PLUGINS;
|
||||
@@ -85,7 +89,7 @@ global.LOCAL_PLUGINS = {
|
||||
addPlugin(plugin) {
|
||||
let has = false;
|
||||
const currentPlugins = global.LOCAL_PLUGINS.getLocalPlugins();
|
||||
currentPlugins.some(p => {
|
||||
currentPlugins.some((p) => {
|
||||
has = p.name === plugin.name;
|
||||
return has;
|
||||
});
|
||||
@@ -96,20 +100,22 @@ global.LOCAL_PLUGINS = {
|
||||
}
|
||||
},
|
||||
updatePlugin(plugin) {
|
||||
global.LOCAL_PLUGINS.PLUGINS = global.LOCAL_PLUGINS.PLUGINS.map(origin => {
|
||||
if (origin.name === plugin.name) {
|
||||
return plugin;
|
||||
global.LOCAL_PLUGINS.PLUGINS = global.LOCAL_PLUGINS.PLUGINS.map(
|
||||
(origin) => {
|
||||
if (origin.name === plugin.name) {
|
||||
return plugin;
|
||||
}
|
||||
return origin;
|
||||
}
|
||||
return origin;
|
||||
});
|
||||
);
|
||||
fs.writeFileSync(configPath, JSON.stringify(global.LOCAL_PLUGINS.PLUGINS));
|
||||
},
|
||||
async deletePlugin(plugin) {
|
||||
await pluginInstance.uninstall([plugin.name], { isDev: plugin.isDev });
|
||||
global.LOCAL_PLUGINS.PLUGINS = global.LOCAL_PLUGINS.PLUGINS.filter(
|
||||
p => plugin.name !== p.name
|
||||
(p) => plugin.name !== p.name
|
||||
);
|
||||
fs.writeFileSync(configPath, JSON.stringify(global.LOCAL_PLUGINS.PLUGINS));
|
||||
return global.LOCAL_PLUGINS.PLUGINS;
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
@@ -13,6 +13,7 @@ export interface DocRes {
|
||||
ok: boolean;
|
||||
rev: RevisionId;
|
||||
_id: string;
|
||||
data?: any;
|
||||
}
|
||||
|
||||
export interface DBError {
|
||||
|
||||
@@ -15,6 +15,7 @@ import { LocalDb } 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'));
|
||||
@@ -33,6 +34,14 @@ 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) => {
|
||||
@@ -313,6 +322,10 @@ class API {
|
||||
shell.beep();
|
||||
return true;
|
||||
}
|
||||
|
||||
public screenCapture() {
|
||||
screenshots.startCapture();
|
||||
}
|
||||
}
|
||||
|
||||
export default new API();
|
||||
|
||||
@@ -5,8 +5,9 @@ import {
|
||||
BrowserView,
|
||||
screen,
|
||||
ipcMain,
|
||||
app
|
||||
} from "electron";
|
||||
app,
|
||||
} from 'electron';
|
||||
import { screenshots } from './registerScreenshots';
|
||||
|
||||
const registerHotKey = (mainWindow: BrowserWindow): void => {
|
||||
// 设置开机启动
|
||||
@@ -14,7 +15,7 @@ const registerHotKey = (mainWindow: BrowserWindow): void => {
|
||||
const config = global.OP_CONFIG.get();
|
||||
app.setLoginItemSettings({
|
||||
openAtLogin: config.perf.common.start,
|
||||
openAsHidden: true
|
||||
openAsHidden: true,
|
||||
});
|
||||
};
|
||||
// 设置暗黑模式
|
||||
@@ -22,7 +23,7 @@ const registerHotKey = (mainWindow: BrowserWindow): void => {
|
||||
const config = global.OP_CONFIG.get();
|
||||
const isDark = config.perf.common.darkMode;
|
||||
if (isDark) {
|
||||
nativeTheme.themeSource = "dark";
|
||||
nativeTheme.themeSource = 'dark';
|
||||
mainWindow.webContents.executeJavaScript(
|
||||
`document.body.classList.add("dark");window.rubick.theme="dark"`
|
||||
);
|
||||
@@ -32,7 +33,7 @@ const registerHotKey = (mainWindow: BrowserWindow): void => {
|
||||
);
|
||||
});
|
||||
} else {
|
||||
nativeTheme.themeSource = "light";
|
||||
nativeTheme.themeSource = 'light';
|
||||
mainWindow.webContents.executeJavaScript(
|
||||
`document.body.classList.remove("dark");window.rubick.theme="light"`
|
||||
);
|
||||
@@ -71,12 +72,16 @@ const registerHotKey = (mainWindow: BrowserWindow): void => {
|
||||
mainWindow.setVisibleOnAllWorkspaces(true, { visibleOnFullScreen: true });
|
||||
mainWindow.focus();
|
||||
mainWindow.setVisibleOnAllWorkspaces(false, {
|
||||
visibleOnFullScreen: true
|
||||
visibleOnFullScreen: true,
|
||||
});
|
||||
mainWindow.setPosition(wx, wy);
|
||||
mainWindow.show();
|
||||
});
|
||||
|
||||
globalShortcut.register(config.perf.shortCut.capture, () => {
|
||||
screenshots.startCapture();
|
||||
});
|
||||
|
||||
// globalShortcut.register(config.perf.shortCut.separate, () => {
|
||||
//
|
||||
// });
|
||||
@@ -87,15 +92,15 @@ const registerHotKey = (mainWindow: BrowserWindow): void => {
|
||||
});
|
||||
|
||||
// 注册自定义全局快捷键
|
||||
config.global.forEach(sc => {
|
||||
config.global.forEach((sc) => {
|
||||
if (!sc.key || !sc.value) return;
|
||||
globalShortcut.register(sc.key, () => {
|
||||
mainWindow.webContents.send("global-short-key", sc.value);
|
||||
mainWindow.webContents.send('global-short-key', sc.value);
|
||||
});
|
||||
});
|
||||
};
|
||||
init();
|
||||
ipcMain.on("re-register", () => {
|
||||
ipcMain.on('re-register', () => {
|
||||
init();
|
||||
});
|
||||
};
|
||||
|
||||
8
src/main/common/registerScreenshots.ts
Normal file
8
src/main/common/registerScreenshots.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import Screenshots from 'electron-screenshots';
|
||||
let screenshots;
|
||||
|
||||
const initScreenShots = () => {
|
||||
screenshots = new Screenshots();
|
||||
};
|
||||
|
||||
export { initScreenShots, screenshots };
|
||||
@@ -17,6 +17,7 @@ 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 };
|
||||
@@ -58,6 +59,7 @@ class App {
|
||||
const readyFunction = () => {
|
||||
this.createWindow();
|
||||
const mainWindow = this.windowCreator.getWindow();
|
||||
initScreenShots();
|
||||
API.init(mainWindow);
|
||||
createTray(this.windowCreator.getWindow());
|
||||
registerHotKey(this.windowCreator.getWindow());
|
||||
|
||||
Reference in New Issue
Block a user