feat: 增加 touchbar 支持

This commit is contained in:
muwoo 2021-08-18 15:03:18 +08:00
parent 39422860d7
commit ef7c829af7
2 changed files with 45 additions and 2 deletions

View File

@ -1,9 +1,11 @@
import {app, BrowserWindow, clipboard, globalShortcut, ipcMain, Notification, screen} from "electron"; import {app, nativeImage, BrowserWindow, clipboard, globalShortcut, ipcMain, Notification, screen, TouchBar} from "electron";
import {exec, spawn} from "child_process"; import {exec, spawn} from "child_process";
import robot from "robotjs"; import robot from "robotjs";
import Api from "./api"; import Api from "./api";
import ioHook from 'iohook'; import ioHook from 'iohook';
import {throttle, commonConst} from './utils'; import {throttle, commonConst} from './utils';
import path from 'path';
import fs from "fs";
const browsers = require("../browsers")(); const browsers = require("../browsers")();
const {picker, separator, superPanel} = browsers; const {picker, separator, superPanel} = browsers;
@ -102,6 +104,7 @@ class Listener {
this.lockScreen(); this.lockScreen();
this.separate(); this.separate();
this.initCapture(); this.initCapture();
this.initTouchBar(mainWindow);
this.superPanel(mainWindow); this.superPanel(mainWindow);
this.reRegisterShortCut(mainWindow); this.reRegisterShortCut(mainWindow);
this.changeSize(mainWindow); this.changeSize(mainWindow);
@ -183,6 +186,43 @@ class Listener {
} }
} }
initTouchBar(mainWindow) {
const { TouchBarButton, TouchBarGroup, TouchBarPopover } = TouchBar;
let items = [];
ipcMain.on('pluginInit', (e, args) => {
this.optionPlugin = args;
items = args.plugins.map((item) => {
const iconPath = path.join(item.sourceFile, '../', item.logo);
if (!fs.existsSync(iconPath)) return false;
const icon = nativeImage.createFromPath(iconPath);
return new TouchBarButton({
icon,
backgroundColor: '#ff9fb4',
click() {
mainWindow.webContents.send('superPanel-openPlugin', {
cmd: item.features[0].cmds.filter(cmd => typeof cmd === 'string')[0],
plugin: item,
feature: item.features,
});
}
})
}).filter(Boolean);
const touchBarPopover = new TouchBarPopover({
items: new TouchBar({
items,
}),
label: '插件',
showCloseButton: true
})
const touchBar = new TouchBar({
items: [touchBarPopover]
});
mainWindow.setTouchBar(touchBar);
});
}
initPlugin() { initPlugin() {
ipcMain.on('optionPlugin', (e, args) => { ipcMain.on('optionPlugin', (e, args) => {
this.optionPlugin = args; this.optionPlugin = args;

View File

@ -130,7 +130,10 @@ function mergePlugins(plugins) {
return hasOption; return hasOption;
}) })
}); });
ipcRenderer &&
ipcRenderer.send('pluginInit', {
plugins: target
});
return target; return target;
} }