Files
openidea/src/workbench/fullscreenEscape.ts
T
l0ng-ai 34f2e18ff1 fix(macos): keep full screen when Escape goes unhandled (#4006)
Escape the page leaves unhandled goes back from WebKit as the web view's doCommandBySelector:(cancelOperation:), run through NSView's own implementation, which leaves full screen directly; the window-level cancelOperation: hook from #3134 was never reached and is removed. In macOS full screen the workbench now marks Escape handled after every key handler has seen it (also when one stops it); Ctrl+Cmd+F leaves full screen.

Refs #3970, #3104
2026-11-17 20:58:44 +08:00

48 lines
2.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* macOS: an Escape key press the page leaves unhandled (not default-prevented) goes back from
* WebKit to AppKit as the web view's `cancelOperation:`, and AppKit answers it in a full-screen
* window by leaving full screen, so pressing Escape once more after closing a popup dropped the
* IDE out of full screen. While this window is in full screen, Escape is marked handled after
* everything else has seen it; View › Appearance › Toggle Full Screen (⌃⌘F) leaves full screen.
*/
import { commands, isTauri, onIdeEvent } from "@/platform/ipc";
import { usePlatform } from "@/platform/platformInfo";
/** Installs the guard on `target`; `isFullscreen` is this window's state (tests pass their own). */
export function installFullscreenEscapeGuard(
target: Window = window,
isFullscreen: () => boolean = trackFullscreen(),
): () => void {
const onCapture = (e: KeyboardEvent) => {
if (e.key !== "Escape" || e.isComposing || usePlatform.getState().os !== "macos" || !isFullscreen()) return;
// Runs last in the bubble phase: handlers that check `defaultPrevented` see the event as it was.
const handled = () => e.preventDefault();
target.addEventListener("keydown", handled, { once: true });
// A handler that stops the event has handled it; the bubble listener above won't run then.
for (const name of ["stopPropagation", "stopImmediatePropagation"] as const) {
const stop = e[name].bind(e);
e[name] = () => {
target.removeEventListener("keydown", handled);
e.preventDefault();
stop();
};
}
};
// Capture on the window before anything else is installed: it runs first.
target.addEventListener("keydown", onCapture, true);
return () => target.removeEventListener("keydown", onCapture, true);
}
function trackFullscreen(): () => boolean {
let fullscreen = false;
if (!isTauri()) return () => fullscreen;
const sync = () =>
void commands
.windowIsFullscreen()
.then((on) => (fullscreen = on))
.catch(() => {});
sync();
onIdeEvent("windowFullscreenChanged", sync);
return () => fullscreen;
}