electron-util
Advanced tools
Comparing version 0.9.1 to 0.10.0
91
index.js
'use strict'; | ||
const path = require('path'); | ||
const url = require('url'); | ||
const electron = require('electron'); | ||
@@ -28,10 +27,2 @@ const isDev = require('electron-is-dev'); | ||
exports.appReady = new Promise(resolve => { | ||
if (api.app.isReady()) { | ||
resolve(); | ||
} else { | ||
api.app.on('ready', resolve); | ||
} | ||
}); | ||
exports.electronVersion = node.electronVersion; | ||
@@ -61,8 +52,2 @@ | ||
exports.loadFile = (win, filePath) => win.loadURL(url.format({ | ||
protocol: 'file', | ||
slashes: true, | ||
pathname: path.resolve(electron.app.getAppPath(), filePath) | ||
})); | ||
exports.runJS = (code, win = activeWindow()) => win.webContents.executeJavaScript(code); | ||
@@ -101,5 +86,6 @@ | ||
exports.getWindowBoundsCentered = options => { | ||
options = Object.assign({ | ||
window: activeWindow() | ||
}, options); | ||
options = { | ||
window: activeWindow(), | ||
...options | ||
}; | ||
@@ -121,8 +107,13 @@ const [width, height] = options.window.getSize(); | ||
exports.setWindowBounds = (bounds, options) => { | ||
options = Object.assign({ | ||
options = { | ||
window: activeWindow(), | ||
animated: false | ||
}, options); | ||
animated: false, | ||
...options | ||
}; | ||
bounds = Object.assign(options.window.getBounds(), bounds); | ||
bounds = { | ||
...options.window.getBounds(), | ||
...bounds | ||
}; | ||
options.window.setBounds(bounds, options.animated); | ||
@@ -132,6 +123,7 @@ }; | ||
exports.centerWindow = options => { | ||
options = Object.assign({ | ||
options = { | ||
window: activeWindow(), | ||
animated: false | ||
}, options); | ||
animated: false, | ||
...options | ||
}; | ||
@@ -146,3 +138,2 @@ const bounds = exports.getWindowBoundsCentered(options); | ||
webContents.setZoomFactor(1); | ||
webContents.setVisualZoomLevelLimits(1, 1); | ||
webContents.setLayoutZoomLevelLimits(0, 0); | ||
@@ -179,1 +170,49 @@ }; | ||
} | ||
exports.darkMode = { | ||
get isEnabled() { | ||
if (!is.macos) { | ||
return false; | ||
} | ||
return api.systemPreferences.isDarkMode(); | ||
}, | ||
onChange(callback) { | ||
if (!is.macos) { | ||
return () => {}; | ||
} | ||
const id = api.systemPreferences.subscribeNotification('AppleInterfaceThemeChangedNotification', () => { | ||
callback(); | ||
}); | ||
return () => { | ||
api.systemPreferences.unsubscribeNotification(id); | ||
}; | ||
} | ||
}; | ||
exports.setContentSecuriyPolicy = async (policy, options) => { | ||
await api.app.whenReady(); | ||
if (!policy.split('\n').filter(line => line.trim()).every(line => line.endsWith(';'))) { | ||
throw new Error('Each line must end in a semicolon'); | ||
} | ||
policy = policy.replace(/[\t\n]/g, '').trim(); | ||
options = { | ||
session: api.session.defaultSession, | ||
...options | ||
}; | ||
options.session.webRequest.onHeadersReceived((details, callback) => { | ||
callback({ | ||
responseHeaders: { | ||
...details.responseHeaders, | ||
'Content-Security-Policy': [policy] | ||
} | ||
}); | ||
}); | ||
}; |
{ | ||
"name": "electron-util", | ||
"version": "0.9.1", | ||
"version": "0.10.0", | ||
"description": "Useful utilities for developing Electron apps and modules", | ||
@@ -12,2 +12,5 @@ "license": "MIT", | ||
}, | ||
"engines": { | ||
"node": ">=10" | ||
}, | ||
"scripts": { | ||
@@ -33,13 +36,12 @@ "start": "electron example.js", | ||
"dependencies": { | ||
"electron-is-dev": "^0.3.0" | ||
"electron-is-dev": "^1.0.0" | ||
}, | ||
"devDependencies": { | ||
"ava": "*", | ||
"delay": "^3.0.0", | ||
"electron": "^2.0.2", | ||
"ava": "^0.25.0", | ||
"delay": "^4.0.1", | ||
"electron": "^3.0.0", | ||
"mock-require": "^3.0.2", | ||
"xo": "*" | ||
"xo": "^0.23.0" | ||
}, | ||
"xo": { | ||
"nodeVersion": ">=8", | ||
"envs": [ | ||
@@ -46,0 +48,0 @@ "node", |
102
readme.md
@@ -14,3 +14,3 @@ # electron-util [](https://travis-ci.org/sindresorhus/electron-util) | ||
*Requires Electron 2.0.0 or later.* | ||
*Requires Electron 3.0.0 or later.* | ||
@@ -21,10 +21,6 @@ | ||
```js | ||
const {appReady, is} = require('electron-util'); | ||
const {is} = require('electron-util'); | ||
(async () => { | ||
await appReady; | ||
console.log(is.macos && is.main); | ||
//=> true | ||
})(); | ||
console.log(is.macos && is.main); | ||
//=> true | ||
``` | ||
@@ -65,8 +61,2 @@ | ||
### appReady | ||
Type: `Promise` | ||
Resolves when the [app is ready](https://electronjs.org/docs/api/app/#event-ready). | ||
### electronVersion | ||
@@ -108,14 +98,2 @@ | ||
### loadFile(window, filePath) | ||
Load a file into the given window using a file path relative to the root of the app. | ||
```js | ||
loadFile(win, 'index.html'); | ||
``` | ||
You use this instead of the verbose ```win.loadURL(`file://…`);``` | ||
[Read more.](https://github.com/electron/electron/issues/11560) | ||
### runJS(code, [window]) | ||
@@ -259,2 +237,74 @@ | ||
### darkMode | ||
Type: `Object` | ||
```js | ||
const {darkMode} = require('electron-util'); | ||
console.log(darkMode.isEnabled); | ||
//=> false | ||
darkMode.onChange(() => { | ||
console.log(darkMode.isEnabled); | ||
//=> true | ||
}); | ||
``` | ||
#### isEnabled | ||
Type: `boolean` | ||
Whether the macOS dark mode is enabled. | ||
On Windows and Linux, it's `false`. | ||
#### onChange(callback) | ||
The `callback` function is called when the macOS dark mode is toggled. | ||
Returns a function, that when called, unsubscribes the listener. | ||
Calling it on Window and Linux works, but it just returns a noop function. | ||
### setContentSecurityPolicy(policy, [options]) | ||
Set a [Content Security Policy](https://developers.google.com/web/fundamentals/security/csp/) for your app. | ||
Don't forget to [validate the policy](https://csp-evaluator.withgoogle.com) after changes. | ||
```js | ||
const {setContentSecuriyPolicy} = require('electron-util'); | ||
setContentSecuriyPolicy(` | ||
default-src 'none'; | ||
script-src 'self'; | ||
img-src 'self' data:; | ||
style-src 'self'; | ||
font-src 'self'; | ||
connect-src 'self' https://api.example.com; | ||
base-uri 'none'; | ||
form-action 'none'; | ||
frame-ancestors 'none'; | ||
`); | ||
``` | ||
#### policy | ||
Type: `string` | ||
You can put rules on separate lines, but lines must end in a semicolon. | ||
#### options | ||
Type: `Object` | ||
##### session | ||
Type: [`Session`](https://electronjs.org/docs/api/session)<br> | ||
Default: [`electron.session.defaultSession`](https://electronjs.org/docs/api/session#sessiondefaultsession) | ||
The session to apply the policy to. | ||
## Node.js API | ||
@@ -261,0 +311,0 @@ |
16249
171
355
+ Addedelectron-is-dev@1.2.0(transitive)
- Removedelectron-is-dev@0.3.0(transitive)
Updatedelectron-is-dev@^1.0.0