fix: BrowserView autoresizing conversion error (#42056)

This commit is contained in:
Shelley Vohr 2024-05-13 10:27:01 +02:00 committed by GitHub
parent 3ea3b0e8e1
commit c765d57265
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 24 additions and 5 deletions

View file

@ -34,13 +34,17 @@ export default class BrowserView {
} }
setAutoResize (options: AutoResizeOptions) { 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 = { this.#autoResizeFlags = {
width: !!options.width, width: !!options.width,
height: !!options.height, height: !!options.height,
horizontal: !!options.horizontal, horizontal: !!options.horizontal,
vertical: !!options.vertical vertical: !!options.vertical
}; };
this.#autoHorizontalProportion = null; this.#autoHorizontalProportion = null;
this.#autoVerticalProportion = null; this.#autoVerticalProportion = null;
} }
@ -71,7 +75,10 @@ export default class BrowserView {
#autoHorizontalProportion: {width: number, left: number} | null = null; #autoHorizontalProportion: {width: number, left: number} | null = null;
#autoVerticalProportion: {height: number, top: number} | null = null; #autoVerticalProportion: {height: number, top: number} | null = null;
#autoResize () { #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) { if (this.#autoResizeFlags.horizontal && this.#autoHorizontalProportion == null) {
const viewBounds = this.#webContentsView.getBounds(); const viewBounds = this.#webContentsView.getBounds();
this.#autoHorizontalProportion = { this.#autoHorizontalProportion = {
@ -79,6 +86,7 @@ export default class BrowserView {
left: this.#lastWindowSize.width / viewBounds.x left: this.#lastWindowSize.width / viewBounds.x
}; };
} }
if (this.#autoResizeFlags.vertical && this.#autoVerticalProportion == null) { if (this.#autoResizeFlags.vertical && this.#autoVerticalProportion == null) {
const viewBounds = this.#webContentsView.getBounds(); const viewBounds = this.#webContentsView.getBounds();
this.#autoVerticalProportion = { this.#autoVerticalProportion = {
@ -86,6 +94,7 @@ export default class BrowserView {
top: this.#lastWindowSize.height / viewBounds.y top: this.#lastWindowSize.height / viewBounds.y
}; };
} }
const newBounds = this.ownerWindow.getBounds(); const newBounds = this.ownerWindow.getBounds();
let widthDelta = newBounds.width - this.#lastWindowSize.width; let widthDelta = newBounds.width - this.#lastWindowSize.width;
let heightDelta = newBounds.height - this.#lastWindowSize.height; let heightDelta = newBounds.height - this.#lastWindowSize.height;
@ -105,10 +114,12 @@ export default class BrowserView {
newViewBounds.width = newBounds.width / this.#autoHorizontalProportion.width; newViewBounds.width = newBounds.width / this.#autoHorizontalProportion.width;
newViewBounds.x = newBounds.width / this.#autoHorizontalProportion.left; newViewBounds.x = newBounds.width / this.#autoHorizontalProportion.left;
} }
if (this.#autoVerticalProportion) { if (this.#autoVerticalProportion) {
newViewBounds.height = newBounds.height / this.#autoVerticalProportion.height; newViewBounds.height = newBounds.height / this.#autoVerticalProportion.height;
newViewBounds.y = newBounds.y / this.#autoVerticalProportion.top; newViewBounds.y = newBounds.y / this.#autoVerticalProportion.top;
} }
if (this.#autoHorizontalProportion || this.#autoVerticalProportion) { if (this.#autoHorizontalProportion || this.#autoVerticalProportion) {
this.#webContentsView.setBounds(newViewBounds); this.#webContentsView.setBounds(newViewBounds);
} }

View file

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

View file

@ -1503,6 +1503,13 @@ describe('BrowserWindow module', () => {
expectBoundsEqual(w.getBounds(), fullBounds); 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', () => { it('sets the window bounds with partial bounds', () => {
const fullBounds = { x: 440, y: 225, width: 500, height: 400 }; const fullBounds = { x: 440, y: 225, width: 500, height: 400 };
w.setBounds(fullBounds); w.setBounds(fullBounds);