Skip to content

Commit

Permalink
Returns function for
Browse files Browse the repository at this point in the history
  • Loading branch information
panjiang committed Sep 4, 2019
1 parent 4e1177d commit e411dbb
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 22 deletions.
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,6 @@ let mainWindow;
});
```
*/
declare function contextMenu(options?: contextMenu.Options): void;
declare function contextMenu(options?: contextMenu.Options): Function;

export = contextMenu;
43 changes: 31 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ const removeUnusedMenuItems = menuTemplate => {
});
};

const create = (win, options) => {
webContents(win).on('context-menu', (event, props) => {
const contextMenu = (win, options) => {
return (event, props) => {
if (typeof options.shouldShowMenu === 'function' && options.shouldShowMenu(event, props) === false) {
return;
}
Expand Down Expand Up @@ -226,29 +226,48 @@ const create = (win, options) => {
*/
menu.popup(electron.remote ? electron.remote.getCurrentWindow() : win);
}
});
};
};

const create = (win, options) => {
const menu = contextMenu(win, options);
webContents(win).on('context-menu', menu);

return () => {
webContents(win).removeListener('context-menu', menu);
};
};

module.exports = (options = {}) => {
const disposeFunctions = [];
if (options.window) {
const win = options.window;

// When window is a webview that has not yet finished loading webContents is not available
if (webContents(win) === undefined) {
win.addEventListener('dom-ready', () => {
create(win, options);
disposeFunctions.push(create(win, options));
}, {once: true});
return;
} else {
disposeFunctions.push(create(win, options));
}
} else {
for (const win of (electron.BrowserWindow || electron.remote.BrowserWindow).getAllWindows()) {
disposeFunctions.push(create(win, options));
}

return create(win, options);
(electron.app || electron.remote.app).on('browser-window-created', (event, win) => {
disposeFunctions.push(create(win, options));
});
}

for (const win of (electron.BrowserWindow || electron.remote.BrowserWindow).getAllWindows()) {
create(win, options);
}
const dispose = () => {
for (const f of disposeFunctions) {
f();
}

disposeFunctions.length = 0;
};

(electron.app || electron.remote.app).on('browser-window-created', (event, win) => {
create(win, options);
});
return dispose;
};
2 changes: 1 addition & 1 deletion index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {expectType} from 'tsd';
import contextMenu = require('.');

expectType<void>(contextMenu());
expectType<Function>(contextMenu());
11 changes: 5 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
{
"name": "electron-context-menu",
"version": "0.15.0",
"name": "pj-electron-context-menu",
"version": "0.15.4",
"description": "Context menu for your Electron app",
"license": "MIT",
"repository": "sindresorhus/electron-context-menu",
"repository": "panjiang/electron-context-menu",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
"name": "Jiang Pan",
"email": "782590544@qq.com"
},
"scripts": {
"start": "electron fixture.js",
Expand Down
14 changes: 12 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# electron-context-menu [![Build Status](https://travis-ci.org/sindresorhus/electron-context-menu.svg?branch=master)](https://travis-ci.org/sindresorhus/electron-context-menu)

Fork from `electron-context-menu` for:

- Returns `dispose` function for `removeListener('context-menu', *)`, in my case of using HMR(Hot Module Replacement).

---

> Context menu for your [Electron](https://electronjs.org) app
<img src="screenshot.png" width="125" align="right">
Expand All @@ -12,7 +18,7 @@ You can use this module directly in both the main and renderer process.
## Install

```
$ npm install electron-context-menu
$ npm install pj-electron-context-menu
```

*Requires Electron 4 or later.*
Expand All @@ -24,7 +30,7 @@ $ npm install electron-context-menu
const {app, BrowserWindow} = require('electron');
const contextMenu = require('electron-context-menu');

contextMenu({
const dispose = contextMenu({
prepend: (defaultActions, params, browserWindow) => [
{
label: 'Rainbow',
Expand All @@ -47,6 +53,10 @@ let mainWindow;
await app.whenReady();
mainWindow = new BrowserWindow();
})();


// You can dispose for unmount
dispose()
```


Expand Down

0 comments on commit e411dbb

Please sign in to comment.