electron-debug
Advanced tools
Comparing version 4.0.1 to 4.1.0
@@ -23,2 +23,3 @@ import {type BrowserWindow} from 'electron'; | ||
| 'undocked' | ||
| 'left' | ||
| 'right' | ||
@@ -28,2 +29,25 @@ | 'bottom' | ||
| 'detach'; | ||
/** | ||
Specify customized options for each window. | ||
The given function receives the window to apply the filter or new options to. | ||
It must return one of these values: | ||
- `true`: To enable debug with the global options for the given window. | ||
- `false`: Disable debug for the given window (same as returning `{isEnabled: false}`). | ||
- `Partial<Options>`: Object to override global options just for the given window. It does a shallow merge. | ||
@default () => true | ||
@example | ||
``` | ||
import debug from 'electron-debug'; | ||
debug({ | ||
windowSelector: window => window.title !== 'Debug tools', | ||
}); | ||
``` | ||
*/ | ||
windowSelector?: (window: Readonly<BrowserWindow>) => boolean | Partial<Options>; | ||
}; | ||
@@ -30,0 +54,0 @@ |
92
index.js
@@ -8,3 +8,4 @@ import process from 'node:process'; | ||
const developmentToolsOptions = {}; | ||
// A Map allows each window to have its own options | ||
const developmentToolsOptions = new Map(); | ||
@@ -17,3 +18,3 @@ function toggleDevelopmentTools(win = BrowserWindow.getFocusedWindow()) { | ||
} else { | ||
webContents.openDevTools(developmentToolsOptions); | ||
webContents.openDevTools(developmentToolsOptions.get(win)); | ||
} | ||
@@ -23,2 +24,38 @@ } | ||
function shouldRun(options) { | ||
return options && (options.isEnabled === true || (options.isEnabled === null && isDev)); | ||
} | ||
function getOptionsForWindow(win, options) { | ||
if (!options.windowSelector) { | ||
return options; | ||
} | ||
const newOptions = options.windowSelector(win); | ||
return newOptions === true | ||
? options | ||
: (newOptions === false | ||
? {isEnabled: false} | ||
: {...options, ...newOptions}); | ||
} | ||
async function registerAccelerators(win = BrowserWindow.getFocusedWindow()) { | ||
await app.whenReady(); | ||
if (win) { | ||
localShortcut.register(win, 'CommandOrControl+Shift+C', inspectElements); | ||
localShortcut.register(win, isMacOS ? 'Command+Alt+I' : 'Control+Shift+I', devTools); | ||
localShortcut.register(win, 'F12', devTools); | ||
localShortcut.register(win, 'CommandOrControl+R', refresh); | ||
localShortcut.register(win, 'F5', refresh); | ||
} else { | ||
localShortcut.register('CommandOrControl+Shift+C', inspectElements); | ||
localShortcut.register(isMacOS ? 'Command+Alt+I' : 'Control+Shift+I', devTools); | ||
localShortcut.register('F12', devTools); | ||
localShortcut.register('CommandOrControl+R', refresh); | ||
localShortcut.register('F5', refresh); | ||
} | ||
} | ||
// eslint-disable-next-line unicorn/prevent-abbreviations | ||
@@ -34,3 +71,3 @@ export function devTools(win = BrowserWindow.getFocusedWindow()) { | ||
if (win) { | ||
win.webContents.openDevTools(developmentToolsOptions); | ||
win.webContents.openDevTools(developmentToolsOptions.get(win)); | ||
} | ||
@@ -69,28 +106,37 @@ } | ||
if (options.isEnabled === false || (options.isEnabled === null && !isDev)) { | ||
return; | ||
} | ||
if (!options.windowSelector) { | ||
if (!shouldRun(options)) { | ||
return; | ||
} | ||
if (options.devToolsMode !== 'previous') { | ||
developmentToolsOptions.mode = options.devToolsMode; | ||
// When there's no filter, accelerators are defined globally | ||
registerAccelerators(); | ||
} | ||
app.on('browser-window-created', (event, win) => { | ||
if (options.showDevTools) { | ||
/// Workaround for https://github.com/electron/electron/issues/12438 | ||
win.webContents.once('dom-ready', () => { | ||
openDevTools(win, options.showDevTools, false); | ||
}); | ||
} | ||
}); | ||
/// Workaround for https://github.com/electron/electron/issues/12438 | ||
win.webContents.once('dom-ready', () => { | ||
const winOptions = getOptionsForWindow(win, options); | ||
(async () => { | ||
await app.whenReady(); | ||
if (winOptions.devToolsMode !== 'previous') { | ||
developmentToolsOptions.set(win, { | ||
...developmentToolsOptions.get(win), | ||
mode: winOptions.devToolsMode, | ||
}); | ||
} | ||
localShortcut.register('CommandOrControl+Shift+C', inspectElements); | ||
localShortcut.register(isMacOS ? 'Command+Alt+I' : 'Control+Shift+I', devTools); | ||
localShortcut.register('F12', devTools); | ||
localShortcut.register('CommandOrControl+R', refresh); | ||
localShortcut.register('F5', refresh); | ||
})(); | ||
if (!shouldRun(winOptions)) { | ||
return; | ||
} | ||
if (winOptions.windowSelector) { | ||
// With filters, accelerators are defined for each window depending on their provided options | ||
registerAccelerators(win); | ||
} | ||
if (winOptions.showDevTools) { | ||
openDevTools(win); | ||
} | ||
}); | ||
}); | ||
} |
{ | ||
"name": "electron-debug", | ||
"version": "4.0.1", | ||
"version": "4.1.0", | ||
"description": "Adds useful debug features to your Electron app", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -112,2 +112,21 @@ # electron-debug | ||
### windowSelector(filter) | ||
Specify customized options for each window. | ||
#### filter | ||
Type: `(window: BrowserWindow) => boolean | Partial<Options>`\ | ||
Default: `() => true` (Use the global options for every window). | ||
##### window | ||
Window to apply the filter or new options to. | ||
##### Return value | ||
- `true`: To enable debug with the global options for the given window. | ||
- `false`: Disable debug for the given window (same as returning `{isEnabled: false}`). | ||
- `Partial<Options>`: Object to override global options just for the given window. It does a shallow merge. | ||
## Related | ||
@@ -114,0 +133,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11657
187
140