Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: make zoomLevel/zoomFactor sync #16410

Merged
merged 2 commits into from Jan 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions atom/browser/api/atom_api_web_contents.cc
Expand Up @@ -2159,9 +2159,9 @@ void WebContents::BuildPrototype(v8::Isolate* isolate,
#endif
.SetMethod("invalidate", &WebContents::Invalidate)
.SetMethod("setZoomLevel", &WebContents::SetZoomLevel)
.SetMethod("_getZoomLevel", &WebContents::GetZoomLevel)
.SetMethod("getZoomLevel", &WebContents::GetZoomLevel)
.SetMethod("setZoomFactor", &WebContents::SetZoomFactor)
.SetMethod("_getZoomFactor", &WebContents::GetZoomFactor)
.SetMethod("getZoomFactor", &WebContents::GetZoomFactor)
.SetMethod("getType", &WebContents::GetType)
.SetMethod("_getPreloadPath", &WebContents::GetPreloadPath)
.SetMethod("getWebPreferences", &WebContents::GetWebPreferences)
Expand Down
4 changes: 0 additions & 4 deletions docs/api/promisification.md
Expand Up @@ -41,8 +41,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.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.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.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)
Expand All @@ -52,8 +50,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)
- [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.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

Expand Down
16 changes: 4 additions & 12 deletions docs/api/web-contents.md
Expand Up @@ -977,13 +977,9 @@ Returns `Boolean` - Whether audio is currently playing.
Changes the zoom factor to the specified factor. Zoom factor is
zoom percent divided by 100, so 300% = 3.0.

#### `contents.getZoomFactor(callback)`
#### `contents.getZoomFactor()`

* `callback` Function
* `zoomFactor` Number

Sends a request to get current zoom factor, the `callback` will be called with
`callback(zoomFactor)`.
Returns `Number` - the current zoom factor.

#### `contents.setZoomLevel(level)`

Expand All @@ -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
`scale := 1.2 ^ level`.

#### `contents.getZoomLevel(callback)`

* `callback` Function
* `zoomLevel` Number
#### `contents.getZoomLevel()`

Sends a request to get current zoom level, the `callback` will be called with
`callback(zoomLevel)`.
Returns `Number` - the current zoom level.

#### `contents.setVisualZoomLevelLimits(minimumLevel, maximumLevel)`

Expand Down
16 changes: 4 additions & 12 deletions docs/api/webview-tag.md
Expand Up @@ -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
`scale := 1.2 ^ level`.

### `<webview>.getZoomFactor(callback)`
### `<webview>.getZoomFactor()`

* `callback` Function
* `zoomFactor` Number

Sends a request to get current zoom factor, the `callback` will be called with
`callback(zoomFactor)`.
Returns `Number` - the current zoom factor.

### `<webview>.getZoomLevel(callback)`

* `callback` Function
* `zoomLevel` Number
### `<webview>.getZoomLevel()`

Sends a request to get current zoom level, the `callback` will be called with
`callback(zoomLevel)`.
Returns `Number` - the current zoom level.

### `<webview>.setVisualZoomLevelLimits(minimumLevel, maximumLevel)`

Expand Down
10 changes: 4 additions & 6 deletions lib/browser/api/menu-item-roles.js
Expand Up @@ -156,19 +156,17 @@ const roles = {
accelerator: 'CommandOrControl+Plus',
nonNativeMacOSRole: true,
webContentsMethod: (webContents) => {
webContents.getZoomLevel((zoomLevel) => {
webContents.setZoomLevel(zoomLevel + 0.5)
})
const zoomLevel = webContents.getZoomLevel()
webContents.setZoomLevel(zoomLevel + 0.5)
}
},
zoomout: {
label: 'Zoom Out',
accelerator: 'CommandOrControl+-',
nonNativeMacOSRole: true,
webContentsMethod: (webContents) => {
webContents.getZoomLevel((zoomLevel) => {
webContents.setZoomLevel(zoomLevel - 0.5)
})
const zoomLevel = webContents.getZoomLevel()
webContents.setZoomLevel(zoomLevel - 0.5)
}
},
// App submenu should be used for Mac only
Expand Down
45 changes: 25 additions & 20 deletions lib/browser/api/web-contents.js
Expand Up @@ -111,6 +111,7 @@ WebContents.prototype.send = function (channel, ...args) {

return this._send(internal, sendToAll, channel, args)
}

WebContents.prototype.sendToAll = function (channel, ...args) {
if (typeof channel !== 'string') {
throw new Error('Missing required channel argument')
Expand Down Expand Up @@ -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) {
return new Promise((resolve, reject) => {
const channel = `ELECTRON_TAKE_HEAP_SNAPSHOT_RESULT_${getNextId()}`
Expand Down Expand Up @@ -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 = {}) {
if (typeof filePath !== 'string') {
throw new Error('Must pass filePath as a string')
Expand All @@ -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.
WebContents.prototype._init = function () {
// The navigation controller.
Expand Down
2 changes: 1 addition & 1 deletion lib/browser/guest-view-manager.js
Expand Up @@ -212,7 +212,7 @@ const attachGuest = function (event, embedderFrameId, elementInstanceId, guestIn
nodeIntegration: params.nodeintegration != null ? params.nodeintegration : false,
enableRemoteModule: params.enableremotemodule,
plugins: params.plugins,
zoomFactor: embedder._getZoomFactor(),
zoomFactor: embedder.getZoomFactor(),
webSecurity: !params.disablewebsecurity,
enableBlinkFeatures: params.blinkfeatures,
disableBlinkFeatures: params.disableblinkfeatures
Expand Down
4 changes: 2 additions & 2 deletions lib/common/web-view-methods.js
Expand Up @@ -46,6 +46,8 @@ exports.syncMethods = new Set([
'downloadURL',
'inspectServiceWorker',
'showDefinitionForSelection',
'getZoomFactor',
'getZoomLevel',
'setZoomFactor',
'setZoomLevel'
])
Expand All @@ -57,8 +59,6 @@ exports.asyncCallbackMethods = new Set([
'sendInputEvent',
'setLayoutZoomLevelLimits',
'setVisualZoomLevelLimits',
'getZoomFactor',
'getZoomLevel',
'print',
'printToPDF'
])
Expand Down
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -13,7 +13,7 @@
"dotenv-safe": "^4.0.4",
"dugite": "^1.45.0",
"electron-docs-linter": "^2.4.0",
"electron-typescript-definitions": "^3.0.0",
"electron-typescript-definitions": "^4.0.0",
"eslint": "^5.6.0",
"eslint-config-standard": "^12.0.0",
"eslint-plugin-mocha": "^5.2.0",
Expand Down
98 changes: 54 additions & 44 deletions spec/api-web-contents-spec.js
Expand Up @@ -569,9 +569,8 @@ describe('webContents module', () => {
const {ipcRenderer, remote} = require('electron')
ipcRenderer.send('set-zoom', window.location.hostname)
ipcRenderer.on(window.location.hostname + '-zoom-set', () => {
remote.getCurrentWebContents().getZoomLevel((zoomLevel) => {
ipcRenderer.send(window.location.hostname + '-zoom-level', zoomLevel)
})
const zoomLevel = remote.getCurrentWebContents().getZoomLevel()
ipcRenderer.send(window.location.hostname + '-zoom-level', zoomLevel)
})
</script>`
callback({ data: response, mimeType: 'text/html' })
Expand All @@ -584,6 +583,19 @@ describe('webContents module', () => {
})

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 {
await w.loadURL('about:blank')
const zoomLevel = await new Promise(resolve => w.webContents.getZoomLevel(resolve))
Expand Down Expand Up @@ -626,15 +638,14 @@ describe('webContents module', () => {
show: false
})
w2.webContents.on('did-finish-load', () => {
w.webContents.getZoomLevel((zoomLevel1) => {
assert.strictEqual(zoomLevel1, hostZoomMap.host3)
w2.webContents.getZoomLevel((zoomLevel2) => {
assert.strictEqual(zoomLevel1, zoomLevel2)
w2.setClosable(true)
w2.close()
done()
})
})
const zoomLevel1 = w.webContents.getZoomLevel()
assert.strictEqual(zoomLevel1, hostZoomMap.host3)

const zoomLevel2 = w2.webContents.getZoomLevel()
assert.strictEqual(zoomLevel1, zoomLevel2)
w2.setClosable(true)
w2.close()
done()
})
w.webContents.on('did-finish-load', () => {
w.webContents.setZoomLevel(hostZoomMap.host3)
Expand All @@ -656,18 +667,18 @@ describe('webContents module', () => {
}, (error) => {
if (error) return done(error)
w2.webContents.on('did-finish-load', () => {
w.webContents.getZoomLevel((zoomLevel1) => {
assert.strictEqual(zoomLevel1, hostZoomMap.host3)
w2.webContents.getZoomLevel((zoomLevel2) => {
assert.strictEqual(zoomLevel2, 0)
assert.notStrictEqual(zoomLevel1, zoomLevel2)
protocol.unregisterProtocol(zoomScheme, (error) => {
if (error) return done(error)
w2.setClosable(true)
w2.close()
done()
})
})
const zoomLevel1 = w.webContents.getZoomLevel()
assert.strictEqual(zoomLevel1, hostZoomMap.host3)

const zoomLevel2 = w2.webContents.getZoomLevel()
assert.strictEqual(zoomLevel2, 0)
assert.notStrictEqual(zoomLevel1, zoomLevel2)

protocol.unregisterProtocol(zoomScheme, (error) => {
if (error) return done(error)
w2.setClosable(true)
w2.close()
done()
})
})
w.webContents.on('did-finish-load', () => {
Expand All @@ -689,12 +700,12 @@ describe('webContents module', () => {
const content = `<iframe src=${url}></iframe>`
w.webContents.on('did-frame-finish-load', (e, isMainFrame) => {
if (!isMainFrame) {
w.webContents.getZoomLevel((zoomLevel) => {
assert.strictEqual(zoomLevel, 2.0)
w.webContents.setZoomLevel(0)
server.close()
done()
})
const zoomLevel = w.webContents.getZoomLevel()
assert.strictEqual(zoomLevel, 2.0)

w.webContents.setZoomLevel(0)
server.close()
done()
}
})
w.webContents.on('dom-ready', () => {
Expand All @@ -710,16 +721,16 @@ describe('webContents module', () => {
show: false
})
w2.webContents.on('did-finish-load', () => {
w.webContents.getZoomLevel((zoomLevel1) => {
assert.strictEqual(zoomLevel1, finalZoomLevel)
w2.webContents.getZoomLevel((zoomLevel2) => {
assert.strictEqual(zoomLevel2, 0)
assert.notStrictEqual(zoomLevel1, zoomLevel2)
w2.setClosable(true)
w2.close()
done()
})
})
const zoomLevel1 = w.webContents.getZoomLevel()
assert.strictEqual(zoomLevel1, finalZoomLevel)

const zoomLevel2 = w2.webContents.getZoomLevel()
assert.strictEqual(zoomLevel2, 0)
assert.notStrictEqual(zoomLevel1, zoomLevel2)

w2.setClosable(true)
w2.close()
done()
})
ipcMain.once('temporary-zoom-set', (e, zoomLevel) => {
w2.loadFile(path.join(fixtures, 'pages', 'c.html'))
Expand All @@ -739,10 +750,9 @@ describe('webContents module', () => {
if (initialNavigation) {
w.webContents.executeJavaScript(source, () => {})
} else {
w.webContents.getZoomLevel((zoomLevel) => {
assert.strictEqual(zoomLevel, 0)
done()
})
const zoomLevel = w.webContents.getZoomLevel()
assert.strictEqual(zoomLevel, 0)
done()
}
})
ipcMain.once('zoom-level-set', (e, zoomLevel) => {
Expand Down
Empty file.
20 changes: 9 additions & 11 deletions spec/fixtures/pages/webview-custom-zoom-level.html
Expand Up @@ -10,17 +10,15 @@
if (!finalNavigation && !view.canGoBack()) {
view.setZoomLevel(2.0)
}
view.getZoomLevel((zoomLevel) => {
view.getZoomFactor((zoomFactor) => {
ipcRenderer.send('webview-zoom-level', zoomLevel, zoomFactor, view.canGoBack(), finalNavigation)
if (!view.canGoBack() && !finalNavigation) {
view.src = 'zoom://host2'
} else if (!finalNavigation) {
finalNavigation = true
view.goBack()
}
})
})
const zoomLevel = view.getZoomLevel()
const zoomFactor = view.getZoomFactor()
ipcRenderer.send('webview-zoom-level', zoomLevel, zoomFactor, view.canGoBack(), finalNavigation)
if (!view.canGoBack() && !finalNavigation) {
view.src = 'zoom://host2'
} else if (!finalNavigation) {
finalNavigation = true
view.goBack()
}
})
</script>
</html>