diff --git a/lib/browser/api/browser-view.ts b/lib/browser/api/browser-view.ts index 24829b811d10a..2c633f735a385 100644 --- a/lib/browser/api/browser-view.ts +++ b/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); } diff --git a/shell/common/gin_converters/gfx_converter.cc b/shell/common/gin_converters/gfx_converter.cc index 9c3dbd7e7304e..c6c75da3f1379 100644 --- a/shell/common/gin_converters/gfx_converter.cc +++ b/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::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; } diff --git a/spec/api-browser-window-spec.ts b/spec/api-browser-window-spec.ts index f5f63cc402c20..d16025e19d6ae 100644 --- a/spec/api-browser-window-spec.ts +++ b/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);