Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 47 additions & 68 deletions apps/desktop/src/preview/Manager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3926,28 +3926,20 @@ describe("PreviewManager", () => {
Effect.gen(function* () {
let failKeyDown = false;
let humanInput: ((_event: unknown, signal: unknown) => void) | undefined;
const sendCommand = vi.fn(async (method: string, params?: Record<string, unknown>) => {
if (
failKeyDown &&
method === "Input.dispatchKeyEvent" &&
(params?.["type"] === "keyDown" || params?.["type"] === "rawKeyDown")
) {
throw new Error("key dispatch failed");
}
if (
method === "Input.dispatchKeyEvent" &&
(params?.["type"] === "keyDown" || params?.["type"] === "rawKeyDown")
) {
humanInput?.(
{},
{
kind: "key",
key: params["key"],
code: params["code"] ?? "Digit1",
},
);
}
return method === "Runtime.evaluate" ? { result: { value: { ok: true } } } : undefined;
const sendCommand = vi.fn(async (method: string, _params?: Record<string, unknown>) =>
method === "Runtime.evaluate" ? { result: { value: { ok: true } } } : undefined,
);
const sendInputEvent = vi.fn((input: Electron.KeyboardInputEvent) => {
if (input.type !== "keyDown") return;
if (failKeyDown) throw new Error("key dispatch failed");
humanInput?.(
{},
{
kind: "key",
key: input.keyCode,
code: input.keyCode === "!" ? "Digit1" : `Key${input.keyCode.toUpperCase()}`,
},
);
});
const restoreFocus = vi.fn();
const focus = vi.fn();
Expand All @@ -3965,6 +3957,7 @@ describe("PreviewManager", () => {
isLoading: () => false,
isDevToolsOpened: () => false,
focus,
sendInputEvent,
getZoomFactor: () => 1,
setZoomFactor: vi.fn(),
setAudioMuted: vi.fn(),
Expand Down Expand Up @@ -4003,13 +3996,6 @@ describe("PreviewManager", () => {
([method, params]) =>
method === "Emulation.setFocusEmulationEnabled" && params?.["enabled"] === true,
);
const keyDownIndex = calls.findIndex(
([method, params]) =>
method === "Input.dispatchKeyEvent" && params?.["type"] === "keyDown",
);
const keyUpIndex = calls.findIndex(
([method, params]) => method === "Input.dispatchKeyEvent" && params?.["type"] === "keyUp",
);
const focusOffIndex = calls.findIndex(
([method, params]) =>
method === "Emulation.setFocusEmulationEnabled" && params?.["enabled"] === false,
Expand Down Expand Up @@ -4037,61 +4023,54 @@ describe("PreviewManager", () => {
expect(clearOnlyEvaluation).toBeDefined();
expect(methods).not.toContain("Input.insertText");
expect(enableIndex).toBeGreaterThanOrEqual(0);
expect(focus).toHaveBeenCalledOnce();
expect(restoreFocus).toHaveBeenCalledOnce();
expect(methods).toContain("Page.bringToFront");
expect(focus).not.toHaveBeenCalled();
expect(restoreFocus).not.toHaveBeenCalled();
expect(methods).not.toContain("Page.bringToFront");
expect(methods).not.toContain("Input.dispatchKeyEvent");
expect(enableIndex).toBeLessThan(focusOnIndex);
expect(focusOnIndex).toBeLessThan(keyDownIndex);
expect(keyDownIndex).toBeLessThan(keyUpIndex);
expect(keyUpIndex).toBeLessThan(focusOffIndex);
expect(
calls.filter(
([method, params]) =>
method === "Input.dispatchKeyEvent" && params?.["type"] === "keyUp",
),
).toHaveLength(1);
expect(sendCommand.mock.invocationCallOrder[focusOnIndex]).toBeLessThan(
sendInputEvent.mock.invocationCallOrder[0]!,
);
expect(sendInputEvent.mock.invocationCallOrder[2]).toBeLessThan(
sendCommand.mock.invocationCallOrder[focusOffIndex]!,
);
expect(sendInputEvent.mock.calls.map(([input]) => input)).toEqual([
{ type: "keyDown", keyCode: "x", modifiers: [], skipIfUnhandled: true },
{ type: "char", keyCode: "x", modifiers: [], skipIfUnhandled: true },
{ type: "keyUp", keyCode: "x", modifiers: [], skipIfUnhandled: true },
]);
expect(sendCommand).toHaveBeenCalledWith("Input.setIgnoreInputEvents", { ignore: false });

sendCommand.mockClear();
sendInputEvent.mockClear();
failKeyDown = true;
const failedPress = yield* Effect.exit(manager.automationPress("tab_input", { key: "y" }));

expect(Exit.isFailure(failedPress)).toBe(true);
expect(sendCommand).toHaveBeenCalledWith("Input.dispatchKeyEvent", {
type: "keyUp",
key: "y",
code: "KeyY",
modifiers: 0,
windowsVirtualKeyCode: 89,
location: 0,
isKeypad: false,
});
expect(sendInputEvent.mock.calls.map(([input]) => input)).toEqual([
{ type: "keyDown", keyCode: "y", modifiers: [], skipIfUnhandled: true },
{ type: "keyUp", keyCode: "y", modifiers: [], skipIfUnhandled: true },
]);
expect(sendCommand).toHaveBeenCalledWith("Emulation.setFocusEmulationEnabled", {
enabled: false,
});
expect(restoreFocus).toHaveBeenCalledTimes(2);
expect(
sendCommand.mock.calls.filter(
([method, params]) =>
method === "Input.dispatchKeyEvent" && params?.["type"] === "keyUp",
),
).toHaveLength(1);
expect(focus).not.toHaveBeenCalled();
expect(restoreFocus).not.toHaveBeenCalled();

sendCommand.mockClear();
sendInputEvent.mockClear();
failKeyDown = false;
yield* manager.automationPress("tab_input", { key: "!" });
expect(sendCommand).toHaveBeenCalledWith("Input.dispatchKeyEvent", {
type: "keyDown",
key: "!",
code: "Digit1",
modifiers: 0,
windowsVirtualKeyCode: 49,
location: 0,
isKeypad: false,
text: "!",
unmodifiedText: "!",
expect(sendInputEvent.mock.calls.map(([input]) => input)).toEqual([
{ type: "keyDown", keyCode: "!", modifiers: [], skipIfUnhandled: true },
{ type: "char", keyCode: "!", modifiers: [], skipIfUnhandled: true },
{ type: "keyUp", keyCode: "!", modifiers: [], skipIfUnhandled: true },
]);
expect(sendCommand).toHaveBeenCalledWith("Emulation.setFocusEmulationEnabled", {
enabled: false,
});
expect(restoreFocus).toHaveBeenCalledTimes(3);
expect(focus).not.toHaveBeenCalled();
expect(restoreFocus).not.toHaveBeenCalled();
}),
),
);
Expand Down
69 changes: 32 additions & 37 deletions apps/desktop/src/preview/Manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ import {
} from "./GuestProtocol.ts";
import { isPreviewAnnotationPayload } from "./PickedElementPayload.ts";
import { playwrightInjectedRuntimeInstallExpression } from "./PlaywrightInjectedRuntime.ts";
import { makePreviewAutomationKeySequence } from "./PreviewKeyboard.ts";
import {
makePreviewAutomationKeySequence,
previewAutomationEditingCommandExpression,
} from "./PreviewKeyboard.ts";
import { captureFavicon, safeHttpOrigin, selectFaviconCandidates } from "./FaviconCapture.ts";

export type PreviewNavStatus =
Expand Down Expand Up @@ -3923,45 +3926,37 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
const keySequence = makePreviewAutomationKeySequence(input, {
isMac: hostPlatform === "darwin",
});
const previouslyFocused = yield* attempt(
{ operation: "automationPress.getFocusedWebContents", tabId, webContentsId: wc.id },
() => webContents.getFocusedWebContents(),
);
let keyDownAttempted = false;
const releaseInput = Effect.gen(function* () {
if (keyDownAttempted) {
yield* sendCleanup("Input.dispatchKeyEvent", keySequence.keyUp).pipe(Effect.ignore);
}
yield* sendCleanup("Emulation.setFocusEmulationEnabled", { enabled: false }).pipe(
Effect.ignore,
);
if (previouslyFocused && previouslyFocused.id !== wc.id && !previouslyFocused.isDestroyed()) {
yield* attempt(
{
operation: "automationPress.restoreFocusedWebContents",
tabId,
webContentsId: previouslyFocused.id,
},
() => previouslyFocused.focus(),
).pipe(Effect.ignore);
}
});

// Focus the guest WebContents itself, not its containing BrowserWindow. This
// activates native keyboard behavior for hidden/background previews without
// changing which thread is mounted in the UI. Restore the previous renderer
// after dispatch so automation never leaves the app's input focus behind.
// CDP keyboard dispatch follows the embedder's focused renderer, and
// WebContents.focus() is a no-op for webview guests. Native input targets
// this guest's widget directly, so Enter cannot submit the host composer.
yield* Effect.gen(function* () {
yield* attempt(
{ operation: "automationPress.focusWebContents", tabId, webContentsId: wc.id },
() => wc.focus(),
);
yield* send("Page.bringToFront");
yield* send("Emulation.setFocusEmulationEnabled", { enabled: true });
yield* expectAgentInput(tabId, keySequence.signal);
keyDownAttempted = true;
yield* send("Input.dispatchKeyEvent", keySequence.keyDown);
}).pipe(Effect.ensuring(releaseInput));
if (keySequence.commands?.length) {
yield* evaluateWithDebugger(
tabId,
send,
previewAutomationEditingCommandExpression(input, keySequence),
true,
);
return;
}
yield* attempt(
{ operation: "automationPress.sendInputEvent", tabId, webContentsId: wc.id },
Comment thread
macroscopeapp[bot] marked this conversation as resolved.
Resolved
() => {
try {
wc.sendInputEvent(keySequence.keyDown);
if (keySequence.char) wc.sendInputEvent(keySequence.char);
Comment thread
maria-rcks marked this conversation as resolved.
Resolved
} finally {
wc.sendInputEvent(keySequence.keyUp);
}
},
);
}).pipe(
Effect.ensuring(
sendCleanup("Emulation.setFocusEmulationEnabled", { enabled: false }).pipe(Effect.ignore),
),
);
});

const automationPress = Effect.fn("PreviewManager.automationPress")(function* (
Expand Down
105 changes: 52 additions & 53 deletions apps/desktop/src/preview/PreviewKeyboard.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,78 +3,79 @@ import { describe, expect, it } from "vite-plus/test";
import { makePreviewAutomationKeySequence } from "./PreviewKeyboard.ts";

describe("preview keyboard packets", () => {
it("includes the Chromium virtual key code and Enter text", () => {
it("sends Enter directly to the guest with its character event", () => {
expect(makePreviewAutomationKeySequence({ key: "Enter" })).toEqual({
keyDown: {
type: "keyDown",
key: "Enter",
code: "Enter",
modifiers: 0,
windowsVirtualKeyCode: 13,
location: 0,
isKeypad: false,
text: "\r",
unmodifiedText: "\r",
keyCode: "Enter",
modifiers: [],
skipIfUnhandled: true,
},
char: {
type: "char",
keyCode: "\r",
modifiers: [],
skipIfUnhandled: true,
},
keyUp: {
type: "keyUp",
key: "Enter",
code: "Enter",
modifiers: 0,
windowsVirtualKeyCode: 13,
location: 0,
isKeypad: false,
keyCode: "Enter",
modifiers: [],
skipIfUnhandled: true,
},
signal: { kind: "key", key: "Enter", code: "Enter" },
});
});

it("dispatches printable keys as text key-down events", () => {
it("sends printable text separately from keydown", () => {
const sequence = makePreviewAutomationKeySequence({ key: "z" });
expect(sequence.keyDown).toMatchObject({
type: "keyDown",
key: "z",
code: "KeyZ",
windowsVirtualKeyCode: 90,
text: "z",
});
expect(sequence.keyUp).not.toHaveProperty("text");
expect(sequence.keyDown).toMatchObject({ type: "keyDown", keyCode: "z" });
expect(sequence.char).toMatchObject({ type: "char", keyCode: "z" });
expect(sequence.keyUp).toMatchObject({ type: "keyUp", keyCode: "z" });
});

it("suppresses text and uses raw key-down for shortcuts", () => {
expect(
makePreviewAutomationKeySequence({ key: "a", modifiers: ["Meta"] }, { isMac: true }).keyDown,
).toEqual({
type: "rawKeyDown",
key: "a",
code: "KeyA",
modifiers: 4,
windowsVirtualKeyCode: 65,
location: 0,
isKeypad: false,
commands: ["selectAll"],
it("suppresses text for shortcuts and retains macOS editing commands", () => {
const sequence = makePreviewAutomationKeySequence(
{ key: "a", modifiers: ["Meta"] },
{ isMac: true },
);
expect(sequence.keyDown).toEqual({
type: "keyDown",
keyCode: "a",
modifiers: ["meta"],
skipIfUnhandled: true,
});
expect(sequence.char).toBeUndefined();
expect(sequence.commands).toEqual(["selectAll"]);
});

it("maps common macOS editing shortcuts without changing other platforms", () => {
expect(
makePreviewAutomationKeySequence({ key: "z", modifiers: ["Shift", "Meta"] }, { isMac: true })
.keyDown.commands,
.commands,
).toEqual(["redo"]);
expect(
makePreviewAutomationKeySequence({ key: "a", modifiers: ["Meta"] }).keyDown,
).not.toHaveProperty("commands");
makePreviewAutomationKeySequence({ key: "a", modifiers: ["Meta"] }).commands,
).toBeUndefined();
});

it.each([
["ArrowLeft", "Left"],
["ArrowRight", "Right"],
["ArrowUp", "Up"],
["ArrowDown", "Down"],
])("maps %s to Electron's %s accelerator", (key, keyCode) => {
const sequence = makePreviewAutomationKeySequence({ key });
expect(sequence.keyDown.keyCode).toBe(keyCode);
expect(sequence.keyUp.keyCode).toBe(keyCode);
expect(sequence.signal.key).toBe(key);
expect(sequence.char).toBeUndefined();
});

it("resolves shifted printable keys to their browser values", () => {
const sequence = makePreviewAutomationKeySequence({ key: "1", modifiers: ["Shift"] });
expect(sequence.keyDown).toMatchObject({
key: "!",
code: "Digit1",
modifiers: 8,
windowsVirtualKeyCode: 49,
text: "!",
});
expect(sequence.keyDown).toMatchObject({ keyCode: "!", modifiers: ["shift"] });
expect(sequence.char).toMatchObject({ keyCode: "!" });
expect(sequence.signal).toEqual({ kind: "key", key: "!", code: "Digit1" });
});

Expand All @@ -84,14 +85,12 @@ describe("preview keyboard packets", () => {
modifiers: ["Control", "Shift"],
});
expect(sequence.keyDown).toEqual({
type: "rawKeyDown",
key: "!",
code: "Digit1",
modifiers: 10,
windowsVirtualKeyCode: 49,
location: 0,
isKeypad: false,
type: "keyDown",
keyCode: "!",
modifiers: ["control", "shift"],
skipIfUnhandled: true,
});
expect(sequence.char).toBeUndefined();
expect(sequence.signal).toEqual({ kind: "key", key: "!", code: "Digit1" });
});
});
Loading
Loading