Skip to content
This repository has been archived by the owner on Oct 30, 2023. It is now read-only.

Commit

Permalink
feat: expose content-bounds-updated event (electron#35533)
Browse files Browse the repository at this point in the history
  • Loading branch information
nornagon authored and khalwa committed Feb 22, 2023
1 parent f3bef1f commit fae2bf7
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 6 deletions.
16 changes: 12 additions & 4 deletions docs/api/web-contents.md
Expand Up @@ -130,10 +130,6 @@ Corresponds to the points in time when the spinner of the tab stopped spinning.

#### Event: 'dom-ready'

Returns:

* `event` Event

Emitted when the document in the top-level frame is loaded.

#### Event: 'page-title-updated'
Expand All @@ -156,6 +152,18 @@ Returns:

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'

Returns:
Expand Down
5 changes: 3 additions & 2 deletions shell/browser/api/electron_api_web_contents.cc
Expand Up @@ -1180,8 +1180,9 @@ void WebContents::BeforeUnloadFired(content::WebContents* tab,

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

void WebContents::CloseContents(content::WebContents* source) {
Expand Down
58 changes: 58 additions & 0 deletions spec/api-web-contents-spec.ts
Expand Up @@ -2132,4 +2132,62 @@ describe('webContents module', () => {
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);
});
});
});

0 comments on commit fae2bf7

Please sign in to comment.