docs: Update the code sample of screen module

* We should not require it before the ready event;
* There is no need to use electronScreen as name in the main process.
This commit is contained in:
Cheng Zhao 2016-06-07 13:55:25 +09:00
parent 08f8e06c55
commit 1a22caabe8

View file

@ -9,44 +9,42 @@ emitted (by invoking or requiring it).
**Note:** In the renderer / DevTools, `window.screen` is a reserved DOM **Note:** In the renderer / DevTools, `window.screen` is a reserved DOM
property, so writing `let {screen} = require('electron')` will not work. property, so writing `let {screen} = require('electron')` will not work.
In our examples below, we use `electronScreen` as the variable name instead.
An example of creating a window that fills the whole screen: An example of creating a window that fills the whole screen:
```javascript ```javascript
const {app, BrowserWindow, screen: electronScreen} = require('electron'); const electron = require('electron')
const {app, BrowserWindow} = electron
let win; let win
app.on('ready', () => { app.on('ready', () => {
const {width, height} = electronScreen.getPrimaryDisplay().workAreaSize; const {width, height} = electron.screen.getPrimaryDisplay().workAreaSize
win = new BrowserWindow({width, height}); win = new BrowserWindow({width, height})
}); });
``` ```
Another example of creating a window in the external display: Another example of creating a window in the external display:
```javascript ```javascript
const {app, BrowserWindow, screen: electronScreen} = require('electron'); const electron = require('electron')
const {app, BrowserWindow} = require('electron')
let win; let win
app.on('ready', () => { app.on('ready', () => {
let displays = electronScreen.getAllDisplays(); let displays = electron.screen.getAllDisplays()
let externalDisplay = null; let externalDisplay = displays.find((display) => {
for (let i in displays) { return display.bounds.x !== 0 || display.bounds.y !== 0
if (displays[i].bounds.x !== 0 || displays[i].bounds.y !== 0) { })
externalDisplay = displays[i];
break;
}
}
if (externalDisplay) { if (externalDisplay) {
win = new BrowserWindow({ win = new BrowserWindow({
x: externalDisplay.bounds.x + 50, x: externalDisplay.bounds.x + 50,
y: externalDisplay.bounds.y + 50 y: externalDisplay.bounds.y + 50
}); })
} }
}); })
``` ```
## The `Display` object ## The `Display` object