Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add focus and blur events for WebContents #25873

Merged
merged 1 commit into from Feb 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/api/web-contents.md
Expand Up @@ -508,6 +508,14 @@ Returns:

Emitted when the user is requesting to change the zoom level using the mouse wheel.

#### Event: 'blur'

Emitted when the `WebContents` loses focus.

#### Event: 'focus'

Emitted when the `WebContents` gains focus.

#### Event: 'devtools-opened'

Emitted when DevTools is opened.
Expand Down
10 changes: 10 additions & 0 deletions shell/browser/api/electron_api_web_contents.cc
Expand Up @@ -1629,6 +1629,16 @@ void WebContents::DidAcquireFullscreen(content::RenderFrameHost* rfh) {
set_fullscreen_frame(rfh);
}

void WebContents::OnWebContentsFocused(
content::RenderWidgetHost* render_widget_host) {
Emit("focus");
}

void WebContents::OnWebContentsLostFocus(
content::RenderWidgetHost* render_widget_host) {
Emit("blur");
}

void WebContents::DOMContentLoaded(
content::RenderFrameHost* render_frame_host) {
auto* web_frame = WebFrameMain::FromRenderFrameHost(render_frame_host);
Expand Down
4 changes: 4 additions & 0 deletions shell/browser/api/electron_api_web_contents.h
Expand Up @@ -621,6 +621,10 @@ class WebContents : public ExclusiveAccessContext,
void DidChangeThemeColor() override;
void OnCursorChanged(const content::WebCursor& cursor) override;
void DidAcquireFullscreen(content::RenderFrameHost* rfh) override;
void OnWebContentsFocused(
content::RenderWidgetHost* render_widget_host) override;
void OnWebContentsLostFocus(
content::RenderWidgetHost* render_widget_host) override;

// InspectableWebContentsDelegate:
void DevToolsReloadPage() override;
Expand Down
56 changes: 53 additions & 3 deletions spec-main/api-web-contents-spec.ts
Expand Up @@ -807,10 +807,10 @@ describe('webContents module', () => {
});
});

describe('focus()', () => {
describe('when the web contents is hidden', () => {
describe('focus APIs', () => {
describe('focus()', () => {
afterEach(closeAllWindows);
it('does not blur the focused window', async () => {
it('does not blur the focused window when the web contents is hidden', async () => {
const w = new BrowserWindow({ show: false, webPreferences: { nodeIntegration: true } });
w.show();
await w.loadURL('about:blank');
Expand All @@ -825,6 +825,56 @@ describe('webContents module', () => {
expect(childFocused).to.be.false();
});
});

describe('focus event', () => {
afterEach(closeAllWindows);
it('is triggered when web contents is focused', async () => {
const w = new BrowserWindow({ show: false });
await w.loadURL('about:blank');
const devToolsOpened = emittedOnce(w.webContents, 'devtools-opened');
w.webContents.openDevTools();
await devToolsOpened;
w.webContents.devToolsWebContents!.focus();
const focusPromise = emittedOnce(w.webContents, 'focus');
w.webContents.focus();
await expect(focusPromise).to.eventually.be.fulfilled();
});

it('is triggered when BrowserWindow is focused', async () => {
const window1 = new BrowserWindow({ show: false });
const window2 = new BrowserWindow({ show: false });

await Promise.all([
window1.loadURL('about:blank'),
window2.loadURL('about:blank')
]);

window1.showInactive();
window2.showInactive();

let focusPromise = emittedOnce(window1.webContents, 'focus');
window1.focus();
await expect(focusPromise).to.eventually.be.fulfilled();

focusPromise = emittedOnce(window2.webContents, 'focus');
window2.focus();
await expect(focusPromise).to.eventually.be.fulfilled();
});
});

describe('blur event', () => {
afterEach(closeAllWindows);
it('is triggered when web contents is blurred', async () => {
const w = new BrowserWindow({ show: true });
await w.loadURL('about:blank');
const blurPromise = emittedOnce(w.webContents, 'blur');
const devToolsOpened = emittedOnce(w.webContents, 'devtools-opened');
w.webContents.openDevTools({ mode: 'detach' });
await devToolsOpened;
w.webContents.devToolsWebContents!.focus();
await expect(blurPromise).to.eventually.be.fulfilled();
});
});
});

describe('getOSProcessId()', () => {
Expand Down