feat: make zoomLevel/zoomFactor sync (#16410)

* feat: make zoomLevel/zoomFactor sync

* update ts defs dep
This commit is contained in:
Shelley Vohr 2019-01-20 23:40:27 -08:00 committed by GitHub
parent dacf7f8bdc
commit 3ca87d205f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 112 additions and 124 deletions

View file

@ -2159,9 +2159,9 @@ void WebContents::BuildPrototype(v8::Isolate* isolate,
#endif #endif
.SetMethod("invalidate", &WebContents::Invalidate) .SetMethod("invalidate", &WebContents::Invalidate)
.SetMethod("setZoomLevel", &WebContents::SetZoomLevel) .SetMethod("setZoomLevel", &WebContents::SetZoomLevel)
.SetMethod("_getZoomLevel", &WebContents::GetZoomLevel) .SetMethod("getZoomLevel", &WebContents::GetZoomLevel)
.SetMethod("setZoomFactor", &WebContents::SetZoomFactor) .SetMethod("setZoomFactor", &WebContents::SetZoomFactor)
.SetMethod("_getZoomFactor", &WebContents::GetZoomFactor) .SetMethod("getZoomFactor", &WebContents::GetZoomFactor)
.SetMethod("getType", &WebContents::GetType) .SetMethod("getType", &WebContents::GetType)
.SetMethod("_getPreloadPath", &WebContents::GetPreloadPath) .SetMethod("_getPreloadPath", &WebContents::GetPreloadPath)
.SetMethod("getWebPreferences", &WebContents::GetWebPreferences) .SetMethod("getWebPreferences", &WebContents::GetWebPreferences)

View file

@ -40,8 +40,6 @@ When a majority of affected functions are migrated, this flag will be enabled by
- [ses.getBlobData(identifier, callback)](https://github.com/electron/electron/blob/master/docs/api/session.md#getBlobData) - [ses.getBlobData(identifier, callback)](https://github.com/electron/electron/blob/master/docs/api/session.md#getBlobData)
- [ses.clearAuthCache(options[, callback])](https://github.com/electron/electron/blob/master/docs/api/session.md#clearAuthCache) - [ses.clearAuthCache(options[, callback])](https://github.com/electron/electron/blob/master/docs/api/session.md#clearAuthCache)
- [contents.executeJavaScript(code[, userGesture, callback])](https://github.com/electron/electron/blob/master/docs/api/web-contents.md#executeJavaScript) - [contents.executeJavaScript(code[, userGesture, callback])](https://github.com/electron/electron/blob/master/docs/api/web-contents.md#executeJavaScript)
- [contents.getZoomFactor(callback)](https://github.com/electron/electron/blob/master/docs/api/web-contents.md#getZoomFactor)
- [contents.getZoomLevel(callback)](https://github.com/electron/electron/blob/master/docs/api/web-contents.md#getZoomLevel)
- [contents.hasServiceWorker(callback)](https://github.com/electron/electron/blob/master/docs/api/web-contents.md#hasServiceWorker) - [contents.hasServiceWorker(callback)](https://github.com/electron/electron/blob/master/docs/api/web-contents.md#hasServiceWorker)
- [contents.unregisterServiceWorker(callback)](https://github.com/electron/electron/blob/master/docs/api/web-contents.md#unregisterServiceWorker) - [contents.unregisterServiceWorker(callback)](https://github.com/electron/electron/blob/master/docs/api/web-contents.md#unregisterServiceWorker)
- [contents.print([options], [callback])](https://github.com/electron/electron/blob/master/docs/api/web-contents.md#print) - [contents.print([options], [callback])](https://github.com/electron/electron/blob/master/docs/api/web-contents.md#print)
@ -51,8 +49,6 @@ When a majority of affected functions are migrated, this flag will be enabled by
- [webFrame.executeJavaScriptInIsolatedWorld(worldId, scripts[, userGesture, callback])](https://github.com/electron/electron/blob/master/docs/api/web-frame.md#executeJavaScriptInIsolatedWorld) - [webFrame.executeJavaScriptInIsolatedWorld(worldId, scripts[, userGesture, callback])](https://github.com/electron/electron/blob/master/docs/api/web-frame.md#executeJavaScriptInIsolatedWorld)
- [webviewTag.executeJavaScript(code[, userGesture, callback])](https://github.com/electron/electron/blob/master/docs/api/webview-tag.md#executeJavaScript) - [webviewTag.executeJavaScript(code[, userGesture, callback])](https://github.com/electron/electron/blob/master/docs/api/webview-tag.md#executeJavaScript)
- [webviewTag.printToPDF(options, callback)](https://github.com/electron/electron/blob/master/docs/api/webview-tag.md#printToPDF) - [webviewTag.printToPDF(options, callback)](https://github.com/electron/electron/blob/master/docs/api/webview-tag.md#printToPDF)
- [webviewTag.getZoomFactor(callback)](https://github.com/electron/electron/blob/master/docs/api/webview-tag.md#getZoomFactor)
- [webviewTag.getZoomLevel(callback)](https://github.com/electron/electron/blob/master/docs/api/webview-tag.md#getZoomLevel)
### Converted Functions ### Converted Functions

View file

@ -977,13 +977,9 @@ Returns `Boolean` - Whether audio is currently playing.
Changes the zoom factor to the specified factor. Zoom factor is Changes the zoom factor to the specified factor. Zoom factor is
zoom percent divided by 100, so 300% = 3.0. zoom percent divided by 100, so 300% = 3.0.
#### `contents.getZoomFactor(callback)` #### `contents.getZoomFactor()`
* `callback` Function Returns `Number` - the current zoom factor.
* `zoomFactor` Number
Sends a request to get current zoom factor, the `callback` will be called with
`callback(zoomFactor)`.
#### `contents.setZoomLevel(level)` #### `contents.setZoomLevel(level)`
@ -994,13 +990,9 @@ increment above or below represents zooming 20% larger or smaller to default
limits of 300% and 50% of original size, respectively. The formula for this is limits of 300% and 50% of original size, respectively. The formula for this is
`scale := 1.2 ^ level`. `scale := 1.2 ^ level`.
#### `contents.getZoomLevel(callback)` #### `contents.getZoomLevel()`
* `callback` Function Returns `Number` - the current zoom level.
* `zoomLevel` Number
Sends a request to get current zoom level, the `callback` will be called with
`callback(zoomLevel)`.
#### `contents.setVisualZoomLevelLimits(minimumLevel, maximumLevel)` #### `contents.setVisualZoomLevelLimits(minimumLevel, maximumLevel)`

View file

@ -595,21 +595,13 @@ increment above or below represents zooming 20% larger or smaller to default
limits of 300% and 50% of original size, respectively. The formula for this is limits of 300% and 50% of original size, respectively. The formula for this is
`scale := 1.2 ^ level`. `scale := 1.2 ^ level`.
### `<webview>.getZoomFactor(callback)` ### `<webview>.getZoomFactor()`
* `callback` Function Returns `Number` - the current zoom factor.
* `zoomFactor` Number
Sends a request to get current zoom factor, the `callback` will be called with ### `<webview>.getZoomLevel()`
`callback(zoomFactor)`.
### `<webview>.getZoomLevel(callback)` Returns `Number` - the current zoom level.
* `callback` Function
* `zoomLevel` Number
Sends a request to get current zoom level, the `callback` will be called with
`callback(zoomLevel)`.
### `<webview>.setVisualZoomLevelLimits(minimumLevel, maximumLevel)` ### `<webview>.setVisualZoomLevelLimits(minimumLevel, maximumLevel)`

View file

@ -156,9 +156,8 @@ const roles = {
accelerator: 'CommandOrControl+Plus', accelerator: 'CommandOrControl+Plus',
nonNativeMacOSRole: true, nonNativeMacOSRole: true,
webContentsMethod: (webContents) => { webContentsMethod: (webContents) => {
webContents.getZoomLevel((zoomLevel) => { const zoomLevel = webContents.getZoomLevel()
webContents.setZoomLevel(zoomLevel + 0.5) webContents.setZoomLevel(zoomLevel + 0.5)
})
} }
}, },
zoomout: { zoomout: {
@ -166,9 +165,8 @@ const roles = {
accelerator: 'CommandOrControl+-', accelerator: 'CommandOrControl+-',
nonNativeMacOSRole: true, nonNativeMacOSRole: true,
webContentsMethod: (webContents) => { webContentsMethod: (webContents) => {
webContents.getZoomLevel((zoomLevel) => { const zoomLevel = webContents.getZoomLevel()
webContents.setZoomLevel(zoomLevel - 0.5) webContents.setZoomLevel(zoomLevel - 0.5)
})
} }
}, },
// App submenu should be used for Mac only // App submenu should be used for Mac only

View file

@ -111,6 +111,7 @@ WebContents.prototype.send = function (channel, ...args) {
return this._send(internal, sendToAll, channel, args) return this._send(internal, sendToAll, channel, args)
} }
WebContents.prototype.sendToAll = function (channel, ...args) { WebContents.prototype.sendToAll = function (channel, ...args) {
if (typeof channel !== 'string') { if (typeof channel !== 'string') {
throw new Error('Missing required channel argument') throw new Error('Missing required channel argument')
@ -209,6 +210,30 @@ WebContents.prototype.executeJavaScript = function (code, hasUserGesture, callba
} }
} }
// TODO(codebytere): remove when promisifications is complete
const nativeZoomLevel = WebContents.prototype.getZoomLevel
WebContents.prototype.getZoomLevel = function (callback) {
if (callback == null) {
return nativeZoomLevel.call(this)
} else {
process.nextTick(() => {
callback(nativeZoomLevel.call(this))
})
}
}
// TODO(codebytere): remove when promisifications is complete
const nativeZoomFactor = WebContents.prototype.getZoomFactor
WebContents.prototype.getZoomFactor = function (callback) {
if (callback == null) {
return nativeZoomFactor.call(this)
} else {
process.nextTick(() => {
callback(nativeZoomFactor.call(this))
})
}
}
WebContents.prototype.takeHeapSnapshot = function (filePath) { WebContents.prototype.takeHeapSnapshot = function (filePath) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const channel = `ELECTRON_TAKE_HEAP_SNAPSHOT_RESULT_${getNextId()}` const channel = `ELECTRON_TAKE_HEAP_SNAPSHOT_RESULT_${getNextId()}`
@ -289,16 +314,6 @@ WebContents.prototype.getPrinters = function () {
} }
} }
WebContents.prototype.getZoomLevel = function (callback) {
if (typeof callback !== 'function') {
throw new Error('Must pass function as an argument')
}
process.nextTick(() => {
const zoomLevel = this._getZoomLevel()
callback(zoomLevel)
})
}
WebContents.prototype.loadFile = function (filePath, options = {}) { WebContents.prototype.loadFile = function (filePath, options = {}) {
if (typeof filePath !== 'string') { if (typeof filePath !== 'string') {
throw new Error('Must pass filePath as a string') throw new Error('Must pass filePath as a string')
@ -315,16 +330,6 @@ WebContents.prototype.loadFile = function (filePath, options = {}) {
})) }))
} }
WebContents.prototype.getZoomFactor = function (callback) {
if (typeof callback !== 'function') {
throw new Error('Must pass function as an argument')
}
process.nextTick(() => {
const zoomFactor = this._getZoomFactor()
callback(zoomFactor)
})
}
// Add JavaScript wrappers for WebContents class. // Add JavaScript wrappers for WebContents class.
WebContents.prototype._init = function () { WebContents.prototype._init = function () {
// The navigation controller. // The navigation controller.

View file

@ -212,7 +212,7 @@ const attachGuest = function (event, embedderFrameId, elementInstanceId, guestIn
nodeIntegration: params.nodeintegration != null ? params.nodeintegration : false, nodeIntegration: params.nodeintegration != null ? params.nodeintegration : false,
enableRemoteModule: params.enableremotemodule, enableRemoteModule: params.enableremotemodule,
plugins: params.plugins, plugins: params.plugins,
zoomFactor: embedder._getZoomFactor(), zoomFactor: embedder.getZoomFactor(),
webSecurity: !params.disablewebsecurity, webSecurity: !params.disablewebsecurity,
enableBlinkFeatures: params.blinkfeatures, enableBlinkFeatures: params.blinkfeatures,
disableBlinkFeatures: params.disableblinkfeatures disableBlinkFeatures: params.disableblinkfeatures

View file

@ -46,6 +46,8 @@ exports.syncMethods = new Set([
'downloadURL', 'downloadURL',
'inspectServiceWorker', 'inspectServiceWorker',
'showDefinitionForSelection', 'showDefinitionForSelection',
'getZoomFactor',
'getZoomLevel',
'setZoomFactor', 'setZoomFactor',
'setZoomLevel' 'setZoomLevel'
]) ])
@ -57,8 +59,6 @@ exports.asyncCallbackMethods = new Set([
'sendInputEvent', 'sendInputEvent',
'setLayoutZoomLevelLimits', 'setLayoutZoomLevelLimits',
'setVisualZoomLevelLimits', 'setVisualZoomLevelLimits',
'getZoomFactor',
'getZoomLevel',
'print', 'print',
'printToPDF' 'printToPDF'
]) ])

View file

@ -13,7 +13,7 @@
"dotenv-safe": "^4.0.4", "dotenv-safe": "^4.0.4",
"dugite": "^1.45.0", "dugite": "^1.45.0",
"electron-docs-linter": "^2.4.0", "electron-docs-linter": "^2.4.0",
"electron-typescript-definitions": "^3.0.0", "electron-typescript-definitions": "^4.0.0",
"eslint": "^5.6.0", "eslint": "^5.6.0",
"eslint-config-standard": "^12.0.0", "eslint-config-standard": "^12.0.0",
"eslint-plugin-mocha": "^5.2.0", "eslint-plugin-mocha": "^5.2.0",

View file

@ -569,9 +569,8 @@ describe('webContents module', () => {
const {ipcRenderer, remote} = require('electron') const {ipcRenderer, remote} = require('electron')
ipcRenderer.send('set-zoom', window.location.hostname) ipcRenderer.send('set-zoom', window.location.hostname)
ipcRenderer.on(window.location.hostname + '-zoom-set', () => { ipcRenderer.on(window.location.hostname + '-zoom-set', () => {
remote.getCurrentWebContents().getZoomLevel((zoomLevel) => { const zoomLevel = remote.getCurrentWebContents().getZoomLevel()
ipcRenderer.send(window.location.hostname + '-zoom-level', zoomLevel) ipcRenderer.send(window.location.hostname + '-zoom-level', zoomLevel)
})
}) })
</script>` </script>`
callback({ data: response, mimeType: 'text/html' }) callback({ data: response, mimeType: 'text/html' })
@ -584,6 +583,19 @@ describe('webContents module', () => {
}) })
it('can set the correct zoom level', async () => { it('can set the correct zoom level', async () => {
try {
await w.loadURL('about:blank')
const zoomLevel = w.webContents.getZoomLevel()
expect(zoomLevel).to.eql(0.0)
w.webContents.setZoomLevel(0.5)
const newZoomLevel = w.webContents.getZoomLevel()
expect(newZoomLevel).to.eql(0.5)
} finally {
w.webContents.setZoomLevel(0)
}
})
it('can set the correct zoom level (callback)', async () => {
try { try {
await w.loadURL('about:blank') await w.loadURL('about:blank')
const zoomLevel = await new Promise(resolve => w.webContents.getZoomLevel(resolve)) const zoomLevel = await new Promise(resolve => w.webContents.getZoomLevel(resolve))
@ -626,15 +638,14 @@ describe('webContents module', () => {
show: false show: false
}) })
w2.webContents.on('did-finish-load', () => { w2.webContents.on('did-finish-load', () => {
w.webContents.getZoomLevel((zoomLevel1) => { const zoomLevel1 = w.webContents.getZoomLevel()
assert.strictEqual(zoomLevel1, hostZoomMap.host3) assert.strictEqual(zoomLevel1, hostZoomMap.host3)
w2.webContents.getZoomLevel((zoomLevel2) => {
assert.strictEqual(zoomLevel1, zoomLevel2) const zoomLevel2 = w2.webContents.getZoomLevel()
w2.setClosable(true) assert.strictEqual(zoomLevel1, zoomLevel2)
w2.close() w2.setClosable(true)
done() w2.close()
}) done()
})
}) })
w.webContents.on('did-finish-load', () => { w.webContents.on('did-finish-load', () => {
w.webContents.setZoomLevel(hostZoomMap.host3) w.webContents.setZoomLevel(hostZoomMap.host3)
@ -656,18 +667,18 @@ describe('webContents module', () => {
}, (error) => { }, (error) => {
if (error) return done(error) if (error) return done(error)
w2.webContents.on('did-finish-load', () => { w2.webContents.on('did-finish-load', () => {
w.webContents.getZoomLevel((zoomLevel1) => { const zoomLevel1 = w.webContents.getZoomLevel()
assert.strictEqual(zoomLevel1, hostZoomMap.host3) assert.strictEqual(zoomLevel1, hostZoomMap.host3)
w2.webContents.getZoomLevel((zoomLevel2) => {
assert.strictEqual(zoomLevel2, 0) const zoomLevel2 = w2.webContents.getZoomLevel()
assert.notStrictEqual(zoomLevel1, zoomLevel2) assert.strictEqual(zoomLevel2, 0)
protocol.unregisterProtocol(zoomScheme, (error) => { assert.notStrictEqual(zoomLevel1, zoomLevel2)
if (error) return done(error)
w2.setClosable(true) protocol.unregisterProtocol(zoomScheme, (error) => {
w2.close() if (error) return done(error)
done() w2.setClosable(true)
}) w2.close()
}) done()
}) })
}) })
w.webContents.on('did-finish-load', () => { w.webContents.on('did-finish-load', () => {
@ -689,12 +700,12 @@ describe('webContents module', () => {
const content = `<iframe src=${url}></iframe>` const content = `<iframe src=${url}></iframe>`
w.webContents.on('did-frame-finish-load', (e, isMainFrame) => { w.webContents.on('did-frame-finish-load', (e, isMainFrame) => {
if (!isMainFrame) { if (!isMainFrame) {
w.webContents.getZoomLevel((zoomLevel) => { const zoomLevel = w.webContents.getZoomLevel()
assert.strictEqual(zoomLevel, 2.0) assert.strictEqual(zoomLevel, 2.0)
w.webContents.setZoomLevel(0)
server.close() w.webContents.setZoomLevel(0)
done() server.close()
}) done()
} }
}) })
w.webContents.on('dom-ready', () => { w.webContents.on('dom-ready', () => {
@ -710,16 +721,16 @@ describe('webContents module', () => {
show: false show: false
}) })
w2.webContents.on('did-finish-load', () => { w2.webContents.on('did-finish-load', () => {
w.webContents.getZoomLevel((zoomLevel1) => { const zoomLevel1 = w.webContents.getZoomLevel()
assert.strictEqual(zoomLevel1, finalZoomLevel) assert.strictEqual(zoomLevel1, finalZoomLevel)
w2.webContents.getZoomLevel((zoomLevel2) => {
assert.strictEqual(zoomLevel2, 0) const zoomLevel2 = w2.webContents.getZoomLevel()
assert.notStrictEqual(zoomLevel1, zoomLevel2) assert.strictEqual(zoomLevel2, 0)
w2.setClosable(true) assert.notStrictEqual(zoomLevel1, zoomLevel2)
w2.close()
done() w2.setClosable(true)
}) w2.close()
}) done()
}) })
ipcMain.once('temporary-zoom-set', (e, zoomLevel) => { ipcMain.once('temporary-zoom-set', (e, zoomLevel) => {
w2.loadFile(path.join(fixtures, 'pages', 'c.html')) w2.loadFile(path.join(fixtures, 'pages', 'c.html'))
@ -739,10 +750,9 @@ describe('webContents module', () => {
if (initialNavigation) { if (initialNavigation) {
w.webContents.executeJavaScript(source, () => {}) w.webContents.executeJavaScript(source, () => {})
} else { } else {
w.webContents.getZoomLevel((zoomLevel) => { const zoomLevel = w.webContents.getZoomLevel()
assert.strictEqual(zoomLevel, 0) assert.strictEqual(zoomLevel, 0)
done() done()
})
} }
}) })
ipcMain.once('zoom-level-set', (e, zoomLevel) => { ipcMain.once('zoom-level-set', (e, zoomLevel) => {

View file

View file

@ -10,17 +10,15 @@
if (!finalNavigation && !view.canGoBack()) { if (!finalNavigation && !view.canGoBack()) {
view.setZoomLevel(2.0) view.setZoomLevel(2.0)
} }
view.getZoomLevel((zoomLevel) => { const zoomLevel = view.getZoomLevel()
view.getZoomFactor((zoomFactor) => { const zoomFactor = view.getZoomFactor()
ipcRenderer.send('webview-zoom-level', zoomLevel, zoomFactor, view.canGoBack(), finalNavigation) ipcRenderer.send('webview-zoom-level', zoomLevel, zoomFactor, view.canGoBack(), finalNavigation)
if (!view.canGoBack() && !finalNavigation) { if (!view.canGoBack() && !finalNavigation) {
view.src = 'zoom://host2' view.src = 'zoom://host2'
} else if (!finalNavigation) { } else if (!finalNavigation) {
finalNavigation = true finalNavigation = true
view.goBack() view.goBack()
} }
})
})
}) })
</script> </script>
</html> </html>

View file

@ -8,12 +8,10 @@
let finalNavigation = false let finalNavigation = false
function SendZoomLevel() { function SendZoomLevel() {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
view.getZoomLevel((zoomLevel) => { const zoomLevel = view.getZoomLevel()
view.getZoomFactor((zoomFactor) => { const zoomFactor = view.getZoomFactor()
ipcRenderer.send('webview-zoom-in-page', zoomLevel, zoomFactor, finalNavigation) ipcRenderer.send('webview-zoom-in-page', zoomLevel, zoomFactor, finalNavigation)
resolve() resolve()
})
})
}) })
} }
view.addEventListener('dom-ready', () => { view.addEventListener('dom-ready', () => {

View file

@ -13,8 +13,7 @@
document.body.appendChild(view2) document.body.appendChild(view2)
}) })
view2.addEventListener('dom-ready', () => { view2.addEventListener('dom-ready', () => {
view2.getZoomLevel((level) => { const zoomLevel = view2.getZoomLevel()
ipcRenderer.send('webview-origin-zoom-level', level) ipcRenderer.send('webview-origin-zoom-level', zoomLevel)
})
}) })
</script> </script>