Browse Source

fix: BrowserView autoresizing conversion error (#42056)

Shelley Vohr 11 months ago
parent
commit
c765d57265

+ 13 - 2
lib/browser/api/browser-view.ts

@@ -34,13 +34,17 @@ export default class BrowserView {
   }
 
   setAutoResize (options: AutoResizeOptions) {
-    if (options == null || typeof options !== 'object') { throw new Error('Invalid auto resize options'); }
+    if (options == null || typeof options !== 'object') {
+      throw new Error('Invalid auto resize options');
+    }
+
     this.#autoResizeFlags = {
       width: !!options.width,
       height: !!options.height,
       horizontal: !!options.horizontal,
       vertical: !!options.vertical
     };
+
     this.#autoHorizontalProportion = null;
     this.#autoVerticalProportion = null;
   }
@@ -71,7 +75,10 @@ export default class BrowserView {
   #autoHorizontalProportion: {width: number, left: number} | null = null;
   #autoVerticalProportion: {height: number, top: number} | null = null;
   #autoResize () {
-    if (!this.ownerWindow) throw new Error('Electron bug: #autoResize called without owner window');
+    if (!this.ownerWindow) {
+      throw new Error('Electron bug: #autoResize called without owner window');
+    };
+
     if (this.#autoResizeFlags.horizontal && this.#autoHorizontalProportion == null) {
       const viewBounds = this.#webContentsView.getBounds();
       this.#autoHorizontalProportion = {
@@ -79,6 +86,7 @@ export default class BrowserView {
         left: this.#lastWindowSize.width / viewBounds.x
       };
     }
+
     if (this.#autoResizeFlags.vertical && this.#autoVerticalProportion == null) {
       const viewBounds = this.#webContentsView.getBounds();
       this.#autoVerticalProportion = {
@@ -86,6 +94,7 @@ export default class BrowserView {
         top: this.#lastWindowSize.height / viewBounds.y
       };
     }
+
     const newBounds = this.ownerWindow.getBounds();
     let widthDelta = newBounds.width - this.#lastWindowSize.width;
     let heightDelta = newBounds.height - this.#lastWindowSize.height;
@@ -105,10 +114,12 @@ export default class BrowserView {
       newViewBounds.width = newBounds.width / this.#autoHorizontalProportion.width;
       newViewBounds.x = newBounds.width / this.#autoHorizontalProportion.left;
     }
+
     if (this.#autoVerticalProportion) {
       newViewBounds.height = newBounds.height / this.#autoVerticalProportion.height;
       newViewBounds.y = newBounds.y / this.#autoVerticalProportion.top;
     }
+
     if (this.#autoHorizontalProportion || this.#autoVerticalProportion) {
       this.#webContentsView.setBounds(newViewBounds);
     }

+ 4 - 3
shell/common/gin_converters/gfx_converter.cc

@@ -14,7 +14,7 @@
 #include "ui/gfx/geometry/insets.h"
 #include "ui/gfx/geometry/point.h"
 #include "ui/gfx/geometry/point_f.h"
-#include "ui/gfx/geometry/rect.h"
+#include "ui/gfx/geometry/rect_conversions.h"
 #include "ui/gfx/geometry/resize_utils.h"
 #include "ui/gfx/geometry/size.h"
 
@@ -100,11 +100,12 @@ bool Converter<gfx::Rect>::FromV8(v8::Isolate* isolate,
   gin::Dictionary dict(isolate);
   if (!gin::ConvertFromV8(isolate, val, &dict))
     return false;
-  int x, y, width, height;
+  float x, y, width, height;
   if (!dict.Get("x", &x) || !dict.Get("y", &y) || !dict.Get("width", &width) ||
       !dict.Get("height", &height))
     return false;
-  *out = gfx::Rect(x, y, width, height);
+
+  *out = ToRoundedRect(gfx::RectF(x, y, width, height));
   return true;
 }
 

+ 7 - 0
spec/api-browser-window-spec.ts

@@ -1503,6 +1503,13 @@ describe('BrowserWindow module', () => {
         expectBoundsEqual(w.getBounds(), fullBounds);
       });
 
+      it('rounds non-integer bounds', () => {
+        w.setBounds({ x: 440.5, y: 225.1, width: 500.4, height: 400.9 });
+
+        const bounds = w.getBounds();
+        expect(bounds).to.deep.equal({ x: 441, y: 225, width: 500, height: 401 });
+      });
+
       it('sets the window bounds with partial bounds', () => {
         const fullBounds = { x: 440, y: 225, width: 500, height: 400 };
         w.setBounds(fullBounds);