Files
buzz/desktop/src/app/useCloseWindowShortcut.ts
Matt Kursmark e5d1dfef7b feat(desktop): close Buzz window with Cmd+W (#6314)
## Summary

- close the current Buzz window with `Cmd-W` on macOS
- keep Buzz Term's existing `Cmd-W` behavior when the terminal owns
input, where it closes the active terminal tab
- cover the close-window chord and its modifier/composition guards with
unit tests

## Why

Buzz removes macOS's native Close Window menu accelerator so the webview
can conditionally route `Cmd-W` to Buzz Term. That also left normal Buzz
mode without the standard close-window shortcut. The new app-level
bubble-phase handler restores window closing while allowing the
terminal's capture-phase handler to retain priority.

For the main window, the existing Rust close-request handler keeps the
webview alive and hides it to the tray, matching the titlebar close
button.

## Validation

- desktop unit tests: 5,093 passed
- focused close-window and terminal shortcut tests: 28 passed at
`71a2cce42013440e297c7b237d45b20e9638078d`
- desktop TypeScript: passed
- desktop checks: passed (pre-existing warnings only)
- workspace Rust clippy: passed
- Tauri Rust clippy/check: passed
- desktop build: passed
- mobile format/analyze: passed
- Tauri tests: 2,592 desktop tests and 26/27 terminal tests passed; the
existing `default_prog_child_observes_the_login_argv0` PTY fixture
repeatedly timed out waiting for `/bin/sh`, including when run alone,
and is unrelated to this TypeScript shortcut change

---------

Signed-off-by: Matt Kursmark <kursmark@squareup.com>
Co-authored-by: Codex <noreply@openai.com>
2026-08-20 16:31:39 +01:00

56 lines
1.4 KiB
TypeScript

import * as React from "react";
import { isTauri } from "@tauri-apps/api/core";
import { getCurrentWindow } from "@tauri-apps/api/window";
import { isMacPlatform } from "@/shared/lib/platform";
type CloseWindowChord = Pick<
KeyboardEvent,
| "altKey"
| "code"
| "ctrlKey"
| "defaultPrevented"
| "isComposing"
| "metaKey"
| "repeat"
| "shiftKey"
>;
export function isCloseWindowShortcut(
event: CloseWindowChord,
isMac: boolean,
): boolean {
return (
isMac &&
event.code === "KeyW" &&
event.metaKey &&
!event.ctrlKey &&
!event.altKey &&
!event.shiftKey &&
!event.defaultPrevented &&
!event.isComposing &&
!event.repeat
);
}
/**
* Restores the standard macOS Cmd+W behavior without reclaiming the native
* menu accelerator. Buzz Term handles the chord first in capture phase while
* it owns input; otherwise this bubble-phase listener closes the current
* window. The main window's Rust close handler turns that into hide-to-tray.
*/
export function useCloseWindowShortcut() {
React.useEffect(() => {
if (!isTauri()) return;
function handleKeyDown(event: KeyboardEvent) {
if (!isCloseWindowShortcut(event, isMacPlatform())) return;
event.preventDefault();
void getCurrentWindow().close();
}
window.addEventListener("keydown", handleKeyDown);
return () => window.removeEventListener("keydown", handleKeyDown);
}, []);
}