Skip to content

Commit f6d855e

Browse files
committed
Add options.windowSelector
1 parent efebe6f commit f6d855e

File tree

3 files changed

+96
-23
lines changed

3 files changed

+96
-23
lines changed

index.d.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,17 @@ export type Options = {
2525
| 'bottom'
2626
| 'previous'
2727
| 'detach';
28+
29+
/**
30+
Allows to select windows to apply the debug options to.
31+
Accepts a function to filter each window and returns:
32+
- `true` to apply the given options
33+
- `false` to skip the window (not apply debug)
34+
- `Options` to override the global options
35+
36+
@default `() => true`
37+
*/
38+
windowSelector?: (window: Readonly<BrowserWindow>) => boolean | Partial<Options>;
2839
};
2940

3041
/**

index.js

Lines changed: 71 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,58 @@ import isDev from 'electron-is-dev';
55

66
const isMacOS = process.platform === 'darwin';
77

8-
const developmentToolsOptions = {};
8+
// A Map allows each window to have its own options
9+
const developmentToolsOptions = new Map();
910

1011
function toggleDevelopmentTools(win = BrowserWindow.getFocusedWindow()) {
1112
if (win) {
1213
const {webContents} = win;
1314
if (webContents.isDevToolsOpened()) {
1415
webContents.closeDevTools();
1516
} else {
16-
webContents.openDevTools(developmentToolsOptions);
17+
webContents.openDevTools(developmentToolsOptions.get(win));
1718
}
1819
}
1920
}
2021

22+
function shouldRun(options) {
23+
return options && (options.isEnabled === true || (options.isEnabled === null && isDev));
24+
}
25+
26+
function getOptionsForWindow(win, options) {
27+
if (!options.windowSelector) {
28+
return options;
29+
}
30+
31+
const newOptions = options.windowSelector(win);
32+
33+
return newOptions === true
34+
? options
35+
: (newOptions === false
36+
? {isEnabled: false}
37+
: {...options, ...newOptions});
38+
}
39+
40+
function registerAccelerators(win = BrowserWindow.getFocusedWindow()) {
41+
(async () => {
42+
await app.whenReady();
43+
44+
if (win) {
45+
localShortcut.register(win, 'CommandOrControl+Shift+C', inspectElements);
46+
localShortcut.register(win, isMacOS ? 'Command+Alt+I' : 'Control+Shift+I', devTools);
47+
localShortcut.register(win, 'F12', devTools);
48+
localShortcut.register(win, 'CommandOrControl+R', refresh);
49+
localShortcut.register(win, 'F5', refresh);
50+
} else {
51+
localShortcut.register('CommandOrControl+Shift+C', inspectElements);
52+
localShortcut.register(isMacOS ? 'Command+Alt+I' : 'Control+Shift+I', devTools);
53+
localShortcut.register('F12', devTools);
54+
localShortcut.register('CommandOrControl+R', refresh);
55+
localShortcut.register('F5', refresh);
56+
}
57+
})();
58+
}
59+
2160
// eslint-disable-next-line unicorn/prevent-abbreviations
2261
export function devTools(win = BrowserWindow.getFocusedWindow()) {
2362
if (win) {
@@ -28,7 +67,7 @@ export function devTools(win = BrowserWindow.getFocusedWindow()) {
2867
// eslint-disable-next-line unicorn/prevent-abbreviations
2968
export function openDevTools(win = BrowserWindow.getFocusedWindow()) {
3069
if (win) {
31-
win.webContents.openDevTools(developmentToolsOptions);
70+
win.webContents.openDevTools(developmentToolsOptions.get(win));
3271
}
3372
}
3473

@@ -62,30 +101,39 @@ export default function debug(options) {
62101
...options,
63102
};
64103

65-
if (options.isEnabled === false || (options.isEnabled === null && !isDev)) {
66-
return;
67-
}
104+
if (!options.windowSelector) {
105+
if (!shouldRun(options)) {
106+
return;
107+
}
68108

69-
if (options.devToolsMode !== 'previous') {
70-
developmentToolsOptions.mode = options.devToolsMode;
109+
// When there's no filter, accelerators are defined globally
110+
registerAccelerators();
71111
}
72112

73113
app.on('browser-window-created', (event, win) => {
74-
if (options.showDevTools) {
75-
/// Workaround for https://github.com/electron/electron/issues/12438
76-
win.webContents.once('dom-ready', () => {
77-
openDevTools(win, options.showDevTools, false);
78-
});
79-
}
80-
});
114+
/// Workaround for https://github.com/electron/electron/issues/12438
115+
win.webContents.once('dom-ready', () => {
116+
const winOptions = getOptionsForWindow(win, options);
81117

82-
(async () => {
83-
await app.whenReady();
118+
if (winOptions.devToolsMode !== 'previous') {
119+
developmentToolsOptions.set(win, {
120+
...developmentToolsOptions.get(win),
121+
mode: winOptions.devToolsMode,
122+
});
123+
}
84124

85-
localShortcut.register('CommandOrControl+Shift+C', inspectElements);
86-
localShortcut.register(isMacOS ? 'Command+Alt+I' : 'Control+Shift+I', devTools);
87-
localShortcut.register('F12', devTools);
88-
localShortcut.register('CommandOrControl+R', refresh);
89-
localShortcut.register('F5', refresh);
90-
})();
125+
if (!shouldRun(winOptions)) {
126+
return;
127+
}
128+
129+
if (winOptions.windowSelector) {
130+
// With filters, accelerators are defined for each window depending on their provided options
131+
registerAccelerators(win);
132+
}
133+
134+
if (winOptions.showDevTools) {
135+
openDevTools(win);
136+
}
137+
});
138+
});
91139
}

readme.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,20 @@ Open DevTools for the specified `BrowserWindow` instance or the focused one.
109109
Type: `BrowserWindow`\
110110
Default: The focused `BrowserWindow`
111111

112+
### windowSelector(filter)
113+
114+
Allows to specify customized options for each window.
115+
116+
#### filter
117+
118+
Type: `(window: BrowserWindow) => boolean | Partial<Options>`\
119+
Default: `() => true` (Use the global options for every window).
120+
121+
It can return:
122+
- `true`: Means `debug` is enabled for every window with the global options
123+
- `false`: Disable `debug` for the given window (maintain the global options for the rest)
124+
- `Partial<Options>`: Object to override the global options just for the given window
125+
112126
## Related
113127

114128
- [electron-util](https://github.com/sindresorhus/electron-util) - Useful utilities for developing Electron apps and modules

0 commit comments

Comments
 (0)