feat: expose content-bounds-updated event (#35533)

This commit is contained in:
Jeremy Rose 2022-09-13 10:49:34 -07:00 committed by GitHub
parent 200153da8e
commit 9f97c3e50a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 73 additions and 6 deletions

View file

@ -130,10 +130,6 @@ Corresponds to the points in time when the spinner of the tab stopped spinning.
#### Event: 'dom-ready' #### Event: 'dom-ready'
Returns:
* `event` Event
Emitted when the document in the top-level frame is loaded. Emitted when the document in the top-level frame is loaded.
#### Event: 'page-title-updated' #### Event: 'page-title-updated'
@ -156,6 +152,18 @@ Returns:
Emitted when page receives favicon urls. Emitted when page receives favicon urls.
#### Event: 'content-bounds-updated'
Returns:
* `event` Event
* `bounds` [Rectangle](structures/rectangle.md) - requested new content bounds
Emitted when the page calls `window.moveTo`, `window.resizeTo` or related APIs.
By default, this will move the window. To prevent that behavior, call
`event.preventDefault()`.
#### Event: 'did-create-window' #### Event: 'did-create-window'
Returns: Returns:

View file

@ -1180,6 +1180,7 @@ void WebContents::BeforeUnloadFired(content::WebContents* tab,
void WebContents::SetContentsBounds(content::WebContents* source, void WebContents::SetContentsBounds(content::WebContents* source,
const gfx::Rect& rect) { const gfx::Rect& rect) {
if (!Emit("content-bounds-updated", rect))
for (ExtendedWebContentsObserver& observer : observers_) for (ExtendedWebContentsObserver& observer : observers_)
observer.OnSetContentBounds(rect); observer.OnSetContentBounds(rect);
} }

View file

@ -2132,4 +2132,62 @@ describe('webContents module', () => {
expect(params.y).to.be.a('number'); expect(params.y).to.be.a('number');
}); });
}); });
describe('content-bounds-updated event', () => {
afterEach(closeAllWindows);
it('emits when moveTo is called', async () => {
const w = new BrowserWindow({ show: false });
w.loadURL('about:blank');
w.webContents.executeJavaScript('window.moveTo(100, 100)', true);
const [, rect] = await emittedOnce(w.webContents, 'content-bounds-updated');
const { width, height } = w.getBounds();
expect(rect).to.deep.equal({
x: 100,
y: 100,
width,
height
});
await new Promise(setImmediate);
expect(w.getBounds().x).to.equal(100);
expect(w.getBounds().y).to.equal(100);
});
it('emits when resizeTo is called', async () => {
const w = new BrowserWindow({ show: false });
w.loadURL('about:blank');
w.webContents.executeJavaScript('window.resizeTo(100, 100)', true);
const [, rect] = await emittedOnce(w.webContents, 'content-bounds-updated');
const { x, y } = w.getBounds();
expect(rect).to.deep.equal({
x,
y,
width: 100,
height: 100
});
await new Promise(setImmediate);
expect({
width: w.getBounds().width,
height: w.getBounds().height
}).to.deep.equal(process.platform === 'win32' ? {
// The width is reported as being larger on Windows? I'm not sure why
// this is.
width: 136,
height: 100
} : {
width: 100,
height: 100
});
});
it('does not change window bounds if cancelled', async () => {
const w = new BrowserWindow({ show: false });
const { width, height } = w.getBounds();
w.loadURL('about:blank');
w.webContents.once('content-bounds-updated', e => e.preventDefault());
await w.webContents.executeJavaScript('window.resizeTo(100, 100)', true);
await new Promise(setImmediate);
expect(w.getBounds().width).to.equal(width);
expect(w.getBounds().height).to.equal(height);
});
});
}); });