mirror of
https://github.com/l0ng-ai/openidea.git
synced 2026-11-24 16:04:21 +00:00
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
48 lines
2.1 KiB
TypeScript
48 lines
2.1 KiB
TypeScript
/**
|
||
* 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;
|
||
}
|