diff --git a/atom/browser/api/atom_api_web_contents.cc b/atom/browser/api/atom_api_web_contents.cc index 75ca85dbf57d..6ef9dc29b4ad 100644 --- a/atom/browser/api/atom_api_web_contents.cc +++ b/atom/browser/api/atom_api_web_contents.cc @@ -636,6 +636,10 @@ content::KeyboardEventProcessingResult WebContents::PreHandleKeyboardEvent( return content::KeyboardEventProcessingResult::NOT_HANDLED; } +void WebContents::ContentsZoomChange(bool zoom_in) { + Emit("zoom-changed", zoom_in ? "in" : "out"); +} + void WebContents::EnterFullscreenModeForTab( content::WebContents* source, const GURL& origin, diff --git a/atom/browser/api/atom_api_web_contents.h b/atom/browser/api/atom_api_web_contents.h index 3366f76763c7..8cfbfdc1e3ad 100644 --- a/atom/browser/api/atom_api_web_contents.h +++ b/atom/browser/api/atom_api_web_contents.h @@ -369,6 +369,7 @@ class WebContents : public mate::TrackableObject, content::KeyboardEventProcessingResult PreHandleKeyboardEvent( content::WebContents* source, const content::NativeWebKeyboardEvent& event) override; + void ContentsZoomChange(bool zoom_in) override; void EnterFullscreenModeForTab( content::WebContents* source, const GURL& origin, diff --git a/docs/api/web-contents.md b/docs/api/web-contents.md index 2016051cc1cf..fbb51c60c259 100644 --- a/docs/api/web-contents.md +++ b/docs/api/web-contents.md @@ -374,6 +374,14 @@ Emitted when the window enters a full-screen state triggered by HTML API. Emitted when the window leaves a full-screen state triggered by HTML API. +#### Event: 'zoom-changed' + +Returns: +* `event` Event +* `zoomDirection` String - Can be `in` or `out`. + +Emitted when the user is requesting to change the zoom level using the mouse wheel. + #### Event: 'devtools-opened' Emitted when DevTools is opened. diff --git a/spec/api-web-contents-spec.js b/spec/api-web-contents-spec.js index 053994d1587a..d1b8c30353d2 100644 --- a/spec/api-web-contents-spec.js +++ b/spec/api-web-contents-spec.js @@ -345,6 +345,45 @@ describe('webContents module', () => { }) }) + describe('zoom-changed', () => { + beforeEach(function () { + // On Mac, zooming isn't done with the mouse wheel. + if (process.platform === 'darwin') { + return closeWindow(w).then(() => { + w = null + this.skip() + }) + } + }) + + it('is emitted with the correct zooming info', async () => { + w.loadFile(path.join(fixtures, 'pages', 'base-page.html')) + await emittedOnce(w.webContents, 'did-finish-load') + + const testZoomChanged = async ({ zoomingIn }) => { + const promise = emittedOnce(w.webContents, 'zoom-changed') + + w.webContents.sendInputEvent({ + type: 'mousewheel', + x: 300, + y: 300, + deltaX: 0, + deltaY: zoomingIn ? 1 : -1, + wheelTicksX: 0, + wheelTicksY: zoomingIn ? 1 : -1, + phase: 'began', + modifiers: ['control', 'meta'] + }) + + const [, zoomDirection] = await promise + expect(zoomDirection).to.equal(zoomingIn ? 'in' : 'out') + } + + await testZoomChanged({ zoomingIn: true }) + await testZoomChanged({ zoomingIn: false }) + }) + }) + describe('devtools window', () => { let testFn = it if (process.platform === 'darwin' && isCi) {