Skip to content

Commit 2aaaff8

Browse files
committed
Make it harder to accidentally close the tab with Command-W
We can use a beforeunload handler and preventDefault if the navigation away is due to a command-key combo. Fixes #343
1 parent f7a034d commit 2aaaff8

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

src/emulator/emulator-ui.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -789,10 +789,30 @@ export class Emulator {
789789
return modifiers;
790790
}
791791

792-
#handleBeforeUnload = () => {
793-
// Mostly necessary for the fallback mode, otherwise the page can hang
794-
// during reload because the worker is not yielding.
795-
this.stop();
792+
#handleBeforeUnload = (event: Event) => {
793+
// On macOS hosts it's too easy to accidentally close the tab due to
794+
// muscle memory for Command-W, so we'll ask for confirmation if the
795+
// navigation away is due a command+key combination.
796+
if (
797+
navigator.platform.startsWith("Mac") &&
798+
Array.from(this.#downKeyCodes).some(code => code.startsWith("Meta"))
799+
) {
800+
event.preventDefault();
801+
802+
// We'll never get a keyup event for the command key, so we'll
803+
// need to synthesize one to avoid the guest ending up with it
804+
// stuck on.
805+
for (const code of this.#downKeyCodes) {
806+
const adbKeyCode = this.#getAdbKeyCode(code);
807+
if (adbKeyCode !== undefined) {
808+
this.#input.handleInput({
809+
type: "keyup",
810+
keyCode: adbKeyCode,
811+
});
812+
}
813+
}
814+
this.#downKeyCodes.clear();
815+
}
796816
};
797817

798818
#handleWorkerMessage = (e: MessageEvent) => {

0 commit comments

Comments
 (0)