Browse Source

feat: add webContents 'zoom-changed' event (#17747)

Milan Burda 6 years ago
parent
commit
6609138959

+ 4 - 0
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,

+ 1 - 0
atom/browser/api/atom_api_web_contents.h

@@ -369,6 +369,7 @@ class WebContents : public mate::TrackableObject<WebContents>,
   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,

+ 8 - 0
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.

+ 39 - 0
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) {