🐛 修复 #221 & 性能优化,支持插件窗口池

This commit is contained in:
muwoo 2023-09-13 14:29:06 +08:00
parent 6dffd1a793
commit 61b4e37fe0
7 changed files with 68 additions and 29 deletions

View File

@ -3,7 +3,7 @@
"version": "0.1.0",
"private": true,
"scripts": {
"serve": "vue-cli-service serve --port 8083",
"serve": "vue-cli-service serve --port 8084",
"build": "vue-cli-service build",
"lint": "vue-cli-service lint"
},

View File

@ -1,6 +1,6 @@
{
"name": "rubick",
"version": "3.0.0",
"version": "3.0.1",
"author": "muwoo <2424880409@qq.com>",
"private": true,
"scripts": {

View File

@ -52,7 +52,7 @@ export default () => {
});
if (process.env.WEBPACK_DEV_SERVER_URL) {
// Load the url of the dev server if in development mode
win.loadURL('http://localhost:8083');
win.loadURL('http://localhost:8084');
} else {
win.loadURL(`file://${path.join(__static, './guide/index.html')}`);
}

View File

@ -28,12 +28,60 @@ const getPreloadPath = (plugin, pluginIndexPath) => {
return path.resolve(getRelativePath(pluginIndexPath), `../`, preload);
};
const viewPoolManager = () => {
const viewPool: any = {
views: [],
};
const maxLen = 4;
return {
getView(pluginName) {
return viewPool.views.find((view) => view.pluginName === pluginName);
},
addView(pluginName, view) {
if (this.getView(pluginName)) return;
if (viewPool.views.length > maxLen) {
viewPool.views.shift();
}
viewPool.views.push({
pluginName,
view,
});
},
};
};
export default () => {
let view;
const viewInstance = viewPoolManager();
const viewReadyFn = async (window, { pluginSetting, ext }) => {
if (!view) return;
const height = pluginSetting && pluginSetting.height;
window.setSize(800, height || 660);
view.setBounds({ x: 0, y: 60, width: 800, height: height || 600 });
view.setAutoResize({ width: true });
executeHooks('PluginEnter', ext);
executeHooks('PluginReady', ext);
const config = await localConfig.getConfig();
const darkMode = config.perf.common.darkMode;
darkMode &&
view.webContents.executeJavaScript(
`document.body.classList.add("dark");window.rubick.theme="dark"`
);
window.webContents.executeJavaScript(`window.pluginLoaded()`);
};
const init = (plugin, window: BrowserWindow) => {
if (view === null || view === undefined) {
createView(plugin, window);
if (viewInstance.getView(plugin.name) && !commonConst.dev()) {
view = viewInstance.getView(plugin.name).view;
window.setBrowserView(view);
view.inited = true;
viewReadyFn(window, plugin);
} else {
createView(plugin, window);
viewInstance.addView(plugin.name, view);
}
// eslint-disable-next-line @typescript-eslint/no-var-requires
require('@electron/remote/main').enable(view.webContents);
}
@ -75,26 +123,16 @@ export default () => {
webviewTag: true,
preload,
session: ses,
defaultFontSize: 14,
defaultFontFamily: {
standard: 'system-ui',
serif: 'system-ui',
},
},
});
window.setBrowserView(view);
view.webContents.loadURL(pluginIndexPath);
view.webContents.once('dom-ready', async () => {
if (!view) return;
const height = pluginSetting && pluginSetting.height;
window.setSize(800, height || 660);
view.setBounds({ x: 0, y: 60, width: 800, height: height || 600 });
view.setAutoResize({ width: true });
executeHooks('PluginEnter', plugin.ext);
executeHooks('PluginReady', plugin.ext);
const config = await localConfig.getConfig();
const darkMode = config.perf.common.darkMode;
darkMode &&
view.webContents.executeJavaScript(
`document.body.classList.add("dark");window.rubick.theme="dark"`
);
window.webContents.executeJavaScript(`window.pluginLoaded()`);
});
view.webContents.once('dom-ready', () => viewReadyFn(window, plugin));
// 修复请求跨域问题
view.webContents.session.webRequest.onBeforeSendHeaders(
(details, callback) => {

View File

@ -33,6 +33,10 @@ class API extends DBInstance {
event.returnValue = data;
// event.sender.send(`msg-back-${arg.type}`, data);
});
// 按 ESC 退出插件
mainWindow.webContents.on('before-input-event', (event, input) =>
this.__EscapeKeyDown(event, input, mainWindow)
);
}
public getCurrentWindow = (window, e) => {
@ -81,15 +85,12 @@ class API extends DBInstance {
})})`
);
window.show();
// 按 ESC 退出插件
window.webContents.on('before-input-event', (event, input) =>
this.__EscapeKeyDown(event, input, window)
);
runnerInstance
.getView()
.webContents.on('before-input-event', (event, input) =>
const view = runnerInstance.getView();
if (!view.inited) {
view.webContents.on('before-input-event', (event, input) =>
this.__EscapeKeyDown(event, input, window)
);
}
}
public removePlugin(e, window) {

View File

@ -19,7 +19,7 @@ function searchKeyValues(lists, value, strict = false) {
if (item.type === 'regex' && !strict) {
return formatReg(item.match).test(value);
}
if (item.type === 'over') {
if (item.type === 'over' && !strict) {
return true;
}
return false;

View File

@ -15,7 +15,7 @@ export default function pluginClickEvent({ plugin, fe, cmd, ext, openPlugin }) {
// 模板文件
if (!plugin.main) {
pluginDist.tplPath = commonConst.dev()
? 'http://localhost:8082/#/'
? 'http://localhost:8083/#/'
: `file://${__static}/tpl/index.html`;
}
// 插件市场