Skip to content

Commit 0d39d2b

Browse files
authored
Merge pull request #481 from lanxiuyun/hooks-onShow-onHide
插件支持调用 onShow onHide
2 parents 3b4ca0e + a05860b commit 0d39d2b

3 files changed

Lines changed: 22 additions & 0 deletions

File tree

public/preload.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ window.rubick = {
3939
openPlugin(plugin) {
4040
ipcSendSync('loadPlugin', plugin);
4141
},
42+
onShow(cb) {
43+
typeof cb === 'function' && (window.rubick.hooks.onShow = cb);
44+
},
45+
onHide(cb) {
46+
typeof cb === 'function' && (window.rubick.hooks.onHide = cb);
47+
},
4248
// 窗口交互
4349
hideMainWindow() {
4450
ipcSendSync('hideMainWindow');

src/main/browsers/main.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ export default () => {
5959
});
6060

6161
win.on('show', () => {
62+
// 触发主窗口的 onShow hook
6263
win.webContents.executeJavaScript(
6364
`window.rubick && window.rubick.hooks && typeof window.rubick.hooks.onShow === "function" && window.rubick.hooks.onShow()`
6465
);
@@ -67,6 +68,7 @@ export default () => {
6768
});
6869

6970
win.on('hide', () => {
71+
// 触发主窗口的 onHide hook
7072
win.webContents.executeJavaScript(
7173
`window.rubick && window.rubick.hooks && typeof window.rubick.hooks.onHide === "function" && window.rubick.hooks.onHide()`
7274
);

src/main/common/api.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,20 @@ class API extends DBInstance {
6868
mainWindow.webContents.on('before-input-event', (event, input) =>
6969
this.__EscapeKeyDown(event, input, mainWindow)
7070
);
71+
// 设置主窗口的 show/hide 事件监听
72+
this.setupMainWindowHooks(mainWindow);
73+
}
74+
75+
private setupMainWindowHooks(mainWindow: BrowserWindow) {
76+
mainWindow.on('show', () => {
77+
// 触发插件的 onShow hook
78+
runnerInstance.executeHooks('Show', null);
79+
});
80+
81+
mainWindow.on('hide', () => {
82+
// 触发插件的 onHide hook
83+
runnerInstance.executeHooks('Hide', null);
84+
});
7185
}
7286

7387
public getCurrentWindow = (window, e) => {

0 commit comments

Comments
 (0)