Skip to content

Commit 4487ff5

Browse files
fix: navigator.keyboard.lock() fullscreen exit handling (#40388)
fix: navigator.keyboard.lock() fullscreen exit handling Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com> Co-authored-by: Shelley Vohr <shelley.vohr@gmail.com>
1 parent 6b87170 commit 4487ff5

File tree

5 files changed

+86
-16
lines changed

5 files changed

+86
-16
lines changed

shell/browser/api/electron_api_web_contents.cc

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,12 +1332,6 @@ bool WebContents::HandleKeyboardEvent(
13321332
bool WebContents::PlatformHandleKeyboardEvent(
13331333
content::WebContents* source,
13341334
const content::NativeWebKeyboardEvent& event) {
1335-
// Escape exits tabbed fullscreen mode.
1336-
if (event.windows_key_code == ui::VKEY_ESCAPE && is_html_fullscreen()) {
1337-
ExitFullscreenModeForTab(source);
1338-
return true;
1339-
}
1340-
13411335
// Check if the webContents has preferences and to ignore shortcuts
13421336
auto* web_preferences = WebContentsPreferences::From(source);
13431337
if (web_preferences && web_preferences->ShouldIgnoreMenuShortcuts())

shell/browser/api/electron_api_web_contents_mac.mm

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,6 @@ - (void)redispatchKeyEvent:(NSEvent*)event;
4242
event.GetType() == content::NativeWebKeyboardEvent::Type::kChar)
4343
return false;
4444

45-
// Escape exits tabbed fullscreen mode.
46-
if (event.windows_key_code == ui::VKEY_ESCAPE && is_html_fullscreen()) {
47-
ExitFullscreenModeForTab(source);
48-
return true;
49-
}
50-
5145
// Check if the webContents has preferences and to ignore shortcuts
5246
auto* web_preferences = WebContentsPreferences::From(source);
5347
if (web_preferences && web_preferences->ShouldIgnoreMenuShortcuts())

spec/chromium-spec.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,63 @@ describe('chromium features', () => {
605605
});
606606
});
607607

608+
describe('navigator.keyboard', () => {
609+
afterEach(closeAllWindows);
610+
611+
it('getLayoutMap() should return a KeyboardLayoutMap object', async () => {
612+
const w = new BrowserWindow({ show: false });
613+
await w.loadFile(path.join(fixturesPath, 'pages', 'blank.html'));
614+
const size = await w.webContents.executeJavaScript(`
615+
navigator.keyboard.getLayoutMap().then(map => map.size)
616+
`);
617+
618+
expect(size).to.be.a('number');
619+
});
620+
621+
it('should lock the keyboard', async () => {
622+
const w = new BrowserWindow({ show: false });
623+
await w.loadFile(path.join(fixturesPath, 'pages', 'modal.html'));
624+
625+
// Test that without lock, with ESC:
626+
// - the window leaves fullscreen
627+
// - the dialog is not closed
628+
const enterFS1 = once(w, 'enter-full-screen');
629+
await w.webContents.executeJavaScript('document.body.requestFullscreen()', true);
630+
await enterFS1;
631+
632+
await w.webContents.executeJavaScript('document.getElementById(\'favDialog\').showModal()', true);
633+
const open1 = await w.webContents.executeJavaScript('document.getElementById(\'favDialog\').open');
634+
expect(open1).to.be.true();
635+
636+
w.webContents.sendInputEvent({ type: 'keyDown', keyCode: 'Escape' });
637+
await setTimeout(1000);
638+
const openAfter1 = await w.webContents.executeJavaScript('document.getElementById(\'favDialog\').open');
639+
expect(openAfter1).to.be.true();
640+
expect(w.isFullScreen()).to.be.false();
641+
642+
// Test that with lock, with ESC:
643+
// - the window does not leave fullscreen
644+
// - the dialog is closed
645+
const enterFS2 = once(w, 'enter-full-screen');
646+
await w.webContents.executeJavaScript(`
647+
navigator.keyboard.lock(['Escape']);
648+
document.body.requestFullscreen();
649+
`, true);
650+
651+
await enterFS2;
652+
653+
await w.webContents.executeJavaScript('document.getElementById(\'favDialog\').showModal()', true);
654+
const open2 = await w.webContents.executeJavaScript('document.getElementById(\'favDialog\').open');
655+
expect(open2).to.be.true();
656+
657+
w.webContents.sendInputEvent({ type: 'keyDown', keyCode: 'Escape' });
658+
await setTimeout(1000);
659+
const openAfter2 = await w.webContents.executeJavaScript('document.getElementById(\'favDialog\').open');
660+
expect(openAfter2).to.be.false();
661+
expect(w.isFullScreen()).to.be.true();
662+
});
663+
});
664+
608665
describe('navigator.languages', () => {
609666
it('should return the system locale only', async () => {
610667
const appLocale = app.getLocale();

spec/fixtures/pages/modal.html

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
4+
<body>
5+
<dialog id="favDialog">
6+
<form>
7+
<p>
8+
<label>
9+
Favorite animal:
10+
<select>
11+
<option value="default">Choose…</option>
12+
<option>Brine shrimp</option>
13+
<option>Red panda</option>
14+
<option>Spider monkey</option>
15+
</select>
16+
</label>
17+
</p>
18+
<div>
19+
<button value="cancel" formmethod="dialog">Cancel</button>
20+
<button id="confirmBtn" value="default">Confirm</button>
21+
</div>
22+
</form>
23+
</dialog>
24+
</body>
25+
26+
</html>

spec/webview-spec.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -546,17 +546,16 @@ describe('<webview> tag', function () {
546546
await close;
547547
});
548548

549-
// Sending ESC via sendInputEvent only works on Windows.
550-
ifit(process.platform === 'win32')('pressing ESC should unfullscreen window', async () => {
549+
it('pressing ESC should unfullscreen window', async () => {
551550
const [w, webview] = await loadWebViewWindow();
552551
const enterFullScreen = once(w, 'enter-full-screen');
553552
await webview.executeJavaScript('document.getElementById("div").requestFullscreen()', true);
554553
await enterFullScreen;
555554

556555
const leaveFullScreen = once(w, 'leave-full-screen');
557-
w.webContents.sendInputEvent({ type: 'keyDown', keyCode: 'Escape' });
556+
webview.sendInputEvent({ type: 'keyDown', keyCode: 'Escape' });
558557
await leaveFullScreen;
559-
await setTimeout();
558+
await setTimeout(1000);
560559
expect(w.isFullScreen()).to.be.false();
561560

562561
const close = once(w, 'closed');

0 commit comments

Comments
 (0)