Skip to content
TypeScript

fix(web): mod+L focuses the browser address bar from a focused page (#22328)

John Nunemaker9cb59bf8587d
diff --git a/apps/desktop/src/preview/Manager.test.ts b/apps/desktop/src/preview/Manager.test.ts
index 41ac8abd378..d917af72d38 100644
--- a/apps/desktop/src/preview/Manager.test.ts
+++ b/apps/desktop/src/preview/Manager.test.ts
@@ -619,7 +619,8 @@ describe("PreviewManager", () => {
withManager((manager) =>
Effect.gen(function* () {
const send = vi.fn();
- const host = { isDestroyed: () => false, send };
+ const focus = vi.fn();
+ const host = { isDestroyed: () => false, send, focus };
const older = makeFaviconWebContents({ id: 42 });
const newer = makeFaviconWebContents({ id: 43 });
for (const tab of [older, newer]) Object.assign(tab.webContents, { hostWebContents: host });
@@ -648,6 +649,22 @@ describe("PreviewManager", () => {
expect(preventDefault).toHaveBeenCalledOnce();
expect(send).toHaveBeenCalledExactlyOnceWith("desktop:menu-action", "view.reopenClosed");
+ yield* manager.setForwardedShortcuts([
+ { command: "preview.focusUrl", shortcut: parseKeybindingShortcut("mod+l")! },
+ ]);
+ beforeInput({ preventDefault } as never, { ...key, key: "l", shift: false } as never);
+ expect(focus).toHaveBeenCalledOnce();
+ expect(send).toHaveBeenLastCalledWith("desktop:menu-action", "preview.focusUrl");
+ expect(focus.mock.invocationCallOrder[0]).toBeLessThan(send.mock.invocationCallOrder[1]!);
+ beforeInput(
+ { preventDefault } as never,
+ { ...key, key: "l", shift: false, isAutoRepeat: true } as never,
+ );
+ expect(focus).toHaveBeenCalledOnce();
+ expect(send).toHaveBeenCalledTimes(2);
+ send.mockClear();
+ yield* manager.setForwardedShortcuts([{ command: "view.reopenClosed", shortcut }]);
+
Object.assign(newer.webContents, { focusedFrame: null });
const agentInput = newer.listeners.get("before-input-event")!;
const agentPreventDefault = vi.fn();
@@ -663,7 +680,7 @@ describe("PreviewManager", () => {
{ ...key, key: "T", shift: true } as never,
);
expect(agentPreventDefault).not.toHaveBeenCalled();
- expect(send).toHaveBeenCalledOnce();
+ expect(send).not.toHaveBeenCalled();
}),
),
);
diff --git a/apps/desktop/src/preview/Manager.ts b/apps/desktop/src/preview/Manager.ts
index dc05f1a8c82..0087444a841 100644
--- a/apps/desktop/src/preview/Manager.ts
+++ b/apps/desktop/src/preview/Manager.ts
@@ -1478,7 +1478,8 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
if (!input.isAutoRepeat) {
if (
forwarded.command === "workspace.focusNext" ||
- forwarded.command === "workspace.focusPrevious"
+ forwarded.command === "workspace.focusPrevious" ||
+ forwarded.command === "preview.focusUrl"
) {
// DOM focus in the renderer cannot by itself release the guest's native keyboard focus.
host.focus();
diff --git a/apps/web/src/components/ReopenClosedViewShortcut.browser.test.tsx b/apps/web/src/components/ReopenClosedViewShortcut.browser.test.tsx
index c388ec33af0..5dc65e57f1c 100644
--- a/apps/web/src/components/ReopenClosedViewShortcut.browser.test.tsx
+++ b/apps/web/src/components/ReopenClosedViewShortcut.browser.test.tsx
@@ -265,4 +265,11 @@ describe("root reopen shortcut", () => {
expect(useClosedViewStore.getState().entries).toEqual([]);
},
);
+ it("forwards address-bar focus from a focused desktop page", async () => {
+ setup();
+ await using _shortcut = await renderShortcut();
+ expect(state.setShortcuts).toHaveBeenLastCalledWith(
+ expect.arrayContaining([expect.objectContaining({ command: "preview.focusUrl" })]),
+ );
+ });
});
diff --git a/apps/web/src/components/ReopenClosedViewShortcut.tsx b/apps/web/src/components/ReopenClosedViewShortcut.tsx
index ce187f5db5e..77cc7b3d306 100644
--- a/apps/web/src/components/ReopenClosedViewShortcut.tsx
+++ b/apps/web/src/components/ReopenClosedViewShortcut.tsx
@@ -43,12 +43,13 @@ import {
} from "../threadRoutes";
import { toastManager } from "./ui/toast";
-const PREVIEW_FORWARDED_LAYOUT_COMMANDS = [
+const PREVIEW_FORWARDED_COMMANDS = [
"workspace.focusNext",
"workspace.focusPrevious",
"sidebar.toggle",
"rightPanel.toggle",
"rightPanel.toggleMaximized",
+ "preview.focusUrl",
] as const;
const isGlobalPullRequests = (ref: ScopedThreadRef) =>
@@ -205,7 +206,7 @@ export function ReopenClosedViewShortcut() {
// Guest browser processes miss app keydown listeners; desktop forwards the chords as menu actions.
const commands = [
...(hasHistory ? (["view.reopenClosed"] as const) : []),
- ...PREVIEW_FORWARDED_LAYOUT_COMMANDS,
+ ...PREVIEW_FORWARDED_COMMANDS,
];
void preview
.setForwardedShortcuts(
diff --git a/apps/web/src/components/preview/PreviewView.browser.test.tsx b/apps/web/src/components/preview/PreviewView.browser.test.tsx
index 43f7845d10d..cdc49b336f4 100644
--- a/apps/web/src/components/preview/PreviewView.browser.test.tsx
+++ b/apps/web/src/components/preview/PreviewView.browser.test.tsx
@@ -286,6 +286,49 @@ describe("PreviewView", () => {
expect(mocks.addImage).not.toHaveBeenCalled();
});
+ it("focuses and selects the address from forwarded desktop actions only while visible", async () => {
+ setup();
+ using stack = new DisposableStack();
+ const previousBridge = window.desktopBridge;
+ stack.defer(() => {
+ if (previousBridge) window.desktopBridge = previousBridge;
+ else delete window.desktopBridge;
+ });
+ const listeners = new Set<(action: string) => void>();
+ window.desktopBridge = {
+ getWindowZoomFactor: () => 1,
+ onMenuAction: (listener: (action: string) => void) => {
+ listeners.add(listener);
+ return () => {
+ listeners.delete(listener);
+ };
+ },
+ } as NonNullable<typeof window.desktopBridge>;
+ await using screen = await render(view());
+ const address = page.getByRole("textbox");
+ const input = address.element() as HTMLInputElement;
+ const forward = () =>
+ act(() => {
+ listeners.forEach((listener) => listener("preview.focusUrl"));
+ });
+
+ await forward();
+ await expect.element(address).toHaveFocus();
+ expect(input.selectionStart).toBe(0);
+ expect(input.selectionEnd).toBe(input.value.length);
+ await act(() => input.blur());
+ await forward();
+ await expect.element(address).toHaveFocus();
+
+ await act(() => input.blur());
+ await screen.rerender(
+ <PreviewView threadRef={TEST_THREAD_REF} tabId="tab-1" visible={false} />,
+ );
+ await forward();
+ await expect.element(address).not.toHaveFocus();
+ expect(listeners.size).toBe(0);
+ });
+
it("focuses the address bar each time the focus-url shortcut fires", async () => {
setup();
await using _screen = await render(view());
diff --git a/apps/web/src/components/preview/PreviewView.tsx b/apps/web/src/components/preview/PreviewView.tsx
index 5126b18ec7a..865448e1f6d 100644
--- a/apps/web/src/components/preview/PreviewView.tsx
+++ b/apps/web/src/components/preview/PreviewView.tsx
@@ -860,7 +860,14 @@ export function PreviewView({
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- subscribes to preview actions while the view is visible
useEffect(() => {
if (!visible) return;
- return subscribePreviewAction((action) => onPreviewAction(action));
+ const unsubscribeAction = subscribePreviewAction((action) => onPreviewAction(action));
+ const unsubscribeMenu = window.desktopBridge?.onMenuAction((action) => {
+ if (action === "preview.focusUrl") onPreviewAction("focus-url");
+ });
+ return () => {
+ unsubscribeAction();
+ unsubscribeMenu?.();
+ };
}, [visible]);
return (