Skip to content

Commit

Permalink
fix: BrowserView autoresizing conversion error
Browse files Browse the repository at this point in the history
  • Loading branch information
codebytere committed May 9, 2024
1 parent 5b60698 commit 91d535d
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
15 changes: 13 additions & 2 deletions lib/browser/api/browser-view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -71,21 +75,26 @@ 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 = {
width: this.#lastWindowSize.width / viewBounds.width,
left: this.#lastWindowSize.width / viewBounds.x
};
}

if (this.#autoResizeFlags.vertical && this.#autoVerticalProportion == null) {
const viewBounds = this.#webContentsView.getBounds();
this.#autoVerticalProportion = {
height: this.#lastWindowSize.height / viewBounds.height,
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;
Expand All @@ -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);
}
Expand Down
7 changes: 4 additions & 3 deletions shell/common/gin_converters/gfx_converter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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;
}

Expand Down
7 changes: 7 additions & 0 deletions spec/api-browser-window-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.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);
Expand Down

0 comments on commit 91d535d

Please sign in to comment.