From 1cb0633c8b2ed95ec2af645a4c541c5e219e98ed Mon Sep 17 00:00:00 2001 From: usrnk1 <7547651+usrnk1@users.noreply.github.com> Date: Tue, 17 Nov 2026 14:54:12 +0100 Subject: [PATCH] feat(desktop): browser toolbar with mobile and custom viewport sizes (#63819) --- .../browser-pane-restore.spec.ts | 19 +- .../app/component-tests/browser-pane.spec.ts | 265 +++++++++- packages/app/src/runtime/plugin/embeds.tsx | 57 ++- .../test-browser/browser-pane-model.test.ts | 38 +- packages/desktop/src/main/plugin/embeds.ts | 150 +++++- packages/desktop/src/main/plugin/host.ts | 3 +- .../desktop/src/shared/ipc-rpc/plugins.ts | 8 +- .../assets/viewport-device-chevron.svg | 5 + .../src/browser/assets/viewport-rotate.svg | 7 + packages/gui-plugins/src/browser/bar.css | 224 +++++++++ packages/gui-plugins/src/browser/chromium.ts | 243 +++++++--- packages/gui-plugins/src/browser/comment.css | 6 +- .../src/browser/connection.test.ts | 25 +- .../gui-plugins/src/browser/connection.ts | 4 +- packages/gui-plugins/src/browser/i18n/en.ts | 38 ++ packages/gui-plugins/src/browser/ipc.ts | 25 +- packages/gui-plugins/src/browser/keys.test.ts | 11 +- packages/gui-plugins/src/browser/keys.ts | 12 +- packages/gui-plugins/src/browser/main.ts | 4 + .../src/browser/mobile-emulation.ts | 121 +++++ packages/gui-plugins/src/browser/model.ts | 78 ++- packages/gui-plugins/src/browser/pane.ts | 19 +- .../gui-plugins/src/browser/panel.fixture.tsx | 164 +++++-- packages/gui-plugins/src/browser/panel.tsx | 452 ++++++++++++++---- packages/gui-plugins/src/browser/renderer.tsx | 18 + .../browser/viewport-controls-machine.test.ts | 70 +++ .../src/browser/viewport-controls-machine.ts | 134 ++++++ .../src/browser/viewport-controls.tsx | 170 +++++++ .../src/browser/viewport-handles.tsx | 119 +++++ .../src/browser/viewport-layout.ts | 24 + .../src/browser/viewport-native.test.ts | 56 +++ .../src/browser/viewport-native.ts | 99 ++++ .../src/browser/viewport-preset-menu.tsx | 69 +++ .../src/browser/viewport-presets.ts | 28 ++ .../browser/viewport-toolbar-machine.test.ts | 78 +++ .../src/browser/viewport-toolbar-machine.ts | 94 ++++ packages/plugin/src/gui/host.ts | 2 + 37 files changed, 2656 insertions(+), 283 deletions(-) create mode 100644 packages/gui-plugins/src/browser/assets/viewport-device-chevron.svg create mode 100644 packages/gui-plugins/src/browser/assets/viewport-rotate.svg create mode 100644 packages/gui-plugins/src/browser/mobile-emulation.ts create mode 100644 packages/gui-plugins/src/browser/viewport-controls-machine.test.ts create mode 100644 packages/gui-plugins/src/browser/viewport-controls-machine.ts create mode 100644 packages/gui-plugins/src/browser/viewport-controls.tsx create mode 100644 packages/gui-plugins/src/browser/viewport-handles.tsx create mode 100644 packages/gui-plugins/src/browser/viewport-layout.ts create mode 100644 packages/gui-plugins/src/browser/viewport-native.test.ts create mode 100644 packages/gui-plugins/src/browser/viewport-native.ts create mode 100644 packages/gui-plugins/src/browser/viewport-preset-menu.tsx create mode 100644 packages/gui-plugins/src/browser/viewport-presets.ts create mode 100644 packages/gui-plugins/src/browser/viewport-toolbar-machine.test.ts create mode 100644 packages/gui-plugins/src/browser/viewport-toolbar-machine.ts diff --git a/packages/app/component-tests/browser-pane-restore.spec.ts b/packages/app/component-tests/browser-pane-restore.spec.ts index 4662fb3105f..ff9546fb382 100644 --- a/packages/app/component-tests/browser-pane-restore.spec.ts +++ b/packages/app/component-tests/browser-pane-restore.spec.ts @@ -279,7 +279,7 @@ story("toggles the element picker from the shortcut only inside the browser pane ) }) -story("keeps in-page shortcuts like ⌘K in the page while forwarding ⌘⇧R and ⌘W to the pane", async ({ page }) => { +story("keeps in-page shortcuts in the editor and forwards toolbar, review and close shortcuts", async ({ page }) => { const root = page.getByTestId("browser-region-fixture") const mac = await page.evaluate(() => /Mac|iPhone|iPad|iPod/.test(navigator.platform)) const mod = mac ? "Meta" : "Control" @@ -299,6 +299,23 @@ story("keeps in-page shortcuts like ⌘K in the page while forwarding ⌘⇧R an await editor.press(`${mod}+k`) await expect(editor).toHaveValue("handled:k") + const toggle = root.getByRole("button", { name: "Toggle browser toolbar", exact: true }) + await editor.press(`${mod}+Shift+m`) + await expect(toggle).toHaveAttribute("aria-expanded", "true") + await expect(toggle).toBeFocused() + await root.getByRole("button", { name: "Device preset", exact: true }).press("ArrowDown") + await page.getByRole("menuitem", { name: "More devices", exact: true }).press("ArrowRight") + await expect(page.getByRole("menuitemradio", { name: "iPhone SE", exact: true })).toBeFocused() + await page.keyboard.press(`${mod}+Shift+m`) + await expect(toggle).toHaveAttribute("aria-expanded", "false") + await expect(toggle).toBeFocused() + await expect(toggle).toHaveCSS("outline-style", "solid") + await toggle.click() + await expect(toggle).toBeFocused() + await expect(toggle).toHaveCSS("outline-style", "none") + await page.keyboard.press(`${mod}+Shift+m`) + await expect(toggle).toHaveCSS("outline-style", "solid") + // Shell shortcuts like ⌘⇧R and pane-scoped ⌘W are claimed before the page sees them. await editor.press(`${mod}+Shift+r`) await expect(root.getByTestId("review-toggles")).toHaveText("1") diff --git a/packages/app/component-tests/browser-pane.spec.ts b/packages/app/component-tests/browser-pane.spec.ts index 9c6a3376c51..4af434fdaab 100644 --- a/packages/app/component-tests/browser-pane.spec.ts +++ b/packages/app/component-tests/browser-pane.spec.ts @@ -1,4 +1,5 @@ import { expect, sourceURL, story } from "../../stories/playwright/story" +import type { Locator, Page } from "@playwright/test" const source = (path: string) => sourceURL(new URL(path, import.meta.url)) @@ -876,13 +877,18 @@ story("lists recent pages on a blank tab and opens one", async ({ page }) => { await expect(address).toHaveValue("http://localhost:5173/settings") }) -story("adds the whole page to the session and keeps the draft when the button is pressed again", async ({ page }) => { - const root = page.getByTestId("browser-pane-fixture") +story("keeps a page note editable beside a tiny viewport and attaches it to the session", async ({ page }) => { + const { root, viewport, width, height } = viewportControls(page) + await viewport.click() + await width.fill("8") + await height.fill("8") + await expectSize(root.getByTestId("native-Alpha"), 8, 8) const field = root.locator('[data-component="browser-address"]') const add = root.getByRole("button", { name: "Add page to session", exact: true }) await field.hover() await add.click() const editor = root.locator('[data-slot="browser-comment-editor"] textarea') + await editor.click() await editor.fill("Check the header layout") await field.hover() await add.click() @@ -913,12 +919,51 @@ story("says a failed cookie read failed and reads again on retry", async ({ page await expect(site.getByRole("button", { name: "Clear", exact: true })).toBeEnabled() }) -story("zooms the page from the browser options in view and keeps its zoom in the address field", async ({ page }) => { +function viewportControls(page: Page) { const root = page.getByTestId("browser-pane-fixture") + + const controls = root.getByRole("region", { name: "Viewport", exact: true }) + + return { + root, + controls, + viewport: root.getByRole("button", { name: "Toggle browser toolbar", exact: true }), + preset: controls.getByRole("button", { name: "Device preset", exact: true }), + option: (name: string) => page.getByRole("menuitemradio", { name, exact: true }), + moreDevices: page.getByRole("menuitem", { name: "More devices", exact: true }), + width: controls.getByLabel("Viewport width in CSS pixels", { exact: true }), + height: controls.getByLabel("Viewport height in CSS pixels", { exact: true }), + scale: controls.getByRole("button", { name: "Preview scale", exact: true }), + rotate: controls.getByRole("button", { name: "Rotate viewport", exact: true }), + } +} + +function resizePane(root: Locator, width: number) { + return root.locator("#browser-panel").evaluate((pane, width) => { + if (!pane.parentElement) throw new Error("Missing pane container") + pane.parentElement.style.width = `${width}px` + }, width) +} + +async function expectSize(native: Locator, width: number, height: number) { + await expect(native).toHaveAttribute("data-width", String(width)) + await expect(native).toHaveAttribute("data-height", String(height)) +} + +story("zooms the page from the browser options in view and keeps its zoom in the address field", async ({ page }) => { + const { root, controls, viewport, preset, option, scale } = viewportControls(page) + await viewport.click() + await preset.click() + await option("Samsung Galaxy S26").click() + await expect(preset).toHaveText("Samsung Galaxy S26") const options = root.getByRole("button", { name: "Browser options", exact: true }) await options.click() await page.getByRole("menuitem", { name: /^Zoom in/ }).click() await expect(root.getByText("Zoom: 110", { exact: true })).toBeVisible() + await expect(controls.getByRole("status")).toContainText("The page is using the pane size because page zoom changed.") + await expect(preset).toBeDisabled() + await expect(preset).toHaveText("Responsive") + await expect(scale).toBeDisabled() // The menu covered the page with a still; it closes so the zoomed page shows at once. await expect(page.getByRole("menu")).toBeHidden() await expect(root.getByTestId("native-Alpha")).toHaveAttribute("data-visible", "true") @@ -928,4 +973,218 @@ story("zooms the page from the browser options in view and keeps its zoom in the await zoom.click() await expect(root.getByText("Zoom: 100", { exact: true })).toBeVisible() await expect(zoom).toBeHidden() + await expect(preset).toBeEnabled() +}) + +story("restores the full pane when the toolbar closes and keeps the selected device on reopen", async ({ page }) => { + const { root, controls, viewport, preset, option, moreDevices, width, height } = viewportControls(page) + + const native = root.getByTestId("native-Alpha") + + const fullWidth = await native.getAttribute("data-width") + + const fullHeight = await native.getAttribute("data-height") + + if (!fullWidth || !fullHeight) throw new Error("Missing native page bounds") + + await viewport.click() + await preset.press("ArrowDown") + const menu = page.getByRole("menu") + await expect(option("Responsive")).toBeChecked() + await expect(menu.getByRole("menuitemradio")).toHaveText([ + "Responsive", + "Samsung Galaxy S26", + "iPhone 18 Pro", + "iPad / iPad Air 11″", + "Laptop", + "Desktop", + ]) + await page.keyboard.press("End") + await expect(moreDevices).toBeFocused() + await page.keyboard.press("ArrowRight") + const mini = option("iPad mini") + await mini.click() + await expect(preset).toHaveText("iPad mini") + await expect(preset).toBeFocused() + await expect(native).not.toHaveAttribute("data-width", fullWidth) + await viewport.click() + await expect(controls).toBeHidden() + await expect(native).toHaveAttribute("data-width", fullWidth) + await expect(native).toHaveAttribute("data-height", fullHeight) + await expect(root.getByRole("separator", { name: "Resize viewport width" })).toBeHidden() + await viewport.click() + await expect(preset).toHaveText("iPad mini") + await expect(width).toHaveValue("756") + await expect(height).toHaveValue("1158") + // Matching the preset's dimensions by hand still counts as Custom. + await width.fill("700") + await width.fill("756") + await expect(preset).toHaveText("Custom") + await preset.click() + await expect(option("Custom")).toBeChecked() + await moreDevices.hover() + await expect(mini).not.toBeChecked() +}) + +story("fits an exact viewport while resizing and rejects invalid edits", async ({ page }) => { + const { root, viewport, preset, option, scale, width, height } = viewportControls(page) + + const native = root.getByTestId("native-Alpha") + await viewport.click() + await expect(width).toHaveValue("624") + await width.fill("1248") + await expect(scale).toHaveText("Fit - 50%") + await expect(width).toBeFocused() + await height.fill("5") + // Chromium needs whole native pixels: round the fitted height up. + await expectSize(native, 624, 3) + await expect(height).toBeFocused() + + await resizePane(root, 328) + await expect(scale).toHaveText("Fit - 25%") + await expectSize(native, 312, 2) + await expect(width).toHaveValue("1248") + await expect(height).toHaveValue("5") + + for (const edit of ["", "4097"]) { + await width.fill(edit) + await expect(width).toHaveValue(edit) + await expect(width).toHaveAttribute("aria-invalid", "true") + await expect(width).toHaveAccessibleDescription("Use whole numbers from 1 to 4096 for width and height.") + await expectSize(native, 312, 2) + } + + await width.blur() + await expect(width).toHaveValue("1248") + await expect(width).not.toHaveAttribute("aria-invalid", "true") + await preset.click() + await option("Responsive").click() + await expect(preset).toHaveText("Responsive") + await expect(width).toHaveValue("312") + await expect(scale).toHaveText("Fit - 100%") + await resizePane(root, 640) + await expect(width).toHaveValue("624") + await expect(native).toHaveAttribute("data-width", "624") +}) + +story("resizes a device with the RTL side grip by pointer and keyboard", async ({ page }) => { + const { root, viewport, preset, option, scale, width, height } = viewportControls(page) + await root.evaluate((element) => element.setAttribute("dir", "rtl")) + await viewport.click() + await preset.click() + await option("Samsung Galaxy S26").click() + await scale.click() + await option("50%").click() + await expectSize(root.getByTestId("native-Alpha"), 180, 390) + const grip = root.getByRole("separator", { name: "Resize viewport width", exact: true }) + + const box = await grip.boundingBox() + + if (!box) throw new Error("Missing viewport resize grip") + const x = box.x + box.width / 2 + + const y = box.y + box.height / 2 + await page.mouse.move(x, y) + await page.mouse.down() + await page.mouse.move(x - 12, y) + // Both edges move 12 screen pixels, or 24 guest CSS pixels each at 50% scale. + await expect(width).toHaveValue("408") + await expect(height).toHaveValue("780") + await expect(preset).toHaveText("Custom") + await expectSize(root.getByTestId("native-Alpha"), 204, 390) + await expect(root.getByTestId("native-Alpha")).toHaveAttribute("data-visible", "true") + await expect(root.locator('img[elementtiming="embed-snapshot"]')).toHaveCount(0) + await page.mouse.up() + await grip.press("Shift+ArrowRight") + await expect(width).toHaveValue("398") +}) + +story("pins the responsive dimensions when a manual preview scale is selected", async ({ page }) => { + const { root, viewport, preset, option, scale, width, height } = viewportControls(page) + + const native = root.getByTestId("native-Alpha") + await viewport.click() + await expect(width).toHaveValue("624") + const responsiveHeight = await height.inputValue() + await expect(scale).toHaveText("Fit - 100%") + await scale.click() + const menu = page.getByRole("menu") + await expect(menu.getByRole("menuitemradio")).toHaveText([ + "Fit to pane", + "50%", + "75%", + "100%", + "125%", + "150%", + "200%", + ]) + const fit = option("Fit to pane") + await expect(fit).toBeChecked() + const double = option("200%") + await double.click() + await resizePane(root, 400) + await expect(scale).toHaveText("200%") + await expect(preset).toHaveText("Custom") + await expect(width).toHaveValue("624") + await expect(height).toHaveValue(responsiveHeight) + + await width.fill("120") + await height.fill("80") + await expectSize(native, 240, 160) + await scale.click() + await expect(double).toBeChecked() + await fit.click() + await expect(scale).toHaveText("Fit - 100%") + await expectSize(native, 120, 80) +}) + +story("keeps the selected device and its rotated viewport with its browser tab", async ({ page }) => { + const { root, viewport, preset, moreDevices, option, width, height, scale, rotate } = viewportControls(page) + + const switchTab = root.getByRole("button", { name: "Switch browser tab", exact: true }) + await viewport.click() + await scale.click() + await option("50%").click() + await preset.click() + await moreDevices.hover() + const pixel = option("Pixel 10") + await pixel.click() + await expect(preset).toHaveText("Pixel 10") + await rotate.click() + await expectSize(root.getByTestId("native-Alpha"), 462, 206) + + await switchTab.click() + await expect(root.getByTestId("native-Second")).toHaveAttribute("data-visible", "true") + await expect(preset).toHaveText("Responsive") + await width.fill("200") + await height.fill("180") + const second = root.getByTestId("native-Second") + await expectSize(second, 200, 180) + await expect(scale).toHaveText("Fit - 100%") + + // Wrapping the toolbar moves the centered native view on both axes without changing its size. + await page.evaluate(() => document.fonts.ready.then(() => undefined)) + + const embed = root.locator('[data-component="browser-preview"] > div:first-child') + + const wide = await embed.boundingBox() + await resizePane(root, 320) + const narrow = await embed.boundingBox() + + if (!wide || !narrow) throw new Error("Missing preview bounds") + expect(narrow.x).toBeLessThan(wide.x) + expect(narrow.y).toBeGreaterThan(wide.y) + await expect(second).toHaveAttribute("data-x", String(Math.round(narrow.x))) + await expect(second).toHaveAttribute("data-y", String(Math.round(narrow.y))) + await expectSize(second, 200, 180) + + await switchTab.click() + await expect(root.getByTestId("native-Alpha")).toHaveAttribute("data-visible", "true") + await expect(width).toHaveValue("924") + await expect(height).toHaveValue("412") + await expect(preset).toHaveText("Pixel 10") + await expect(scale).toHaveText("50%") + await preset.click() + await moreDevices.hover() + await expect(pixel).toBeChecked() }) diff --git a/packages/app/src/runtime/plugin/embeds.tsx b/packages/app/src/runtime/plugin/embeds.tsx index 0ef57254ef5..16bcfa85338 100644 --- a/packages/app/src/runtime/plugin/embeds.tsx +++ b/packages/app/src/runtime/plugin/embeds.tsx @@ -3,7 +3,7 @@ import { createEventListener } from "@solid-primitives/event-listener" import { createResizeObserver } from "@solid-primitives/resize-observer" import { observeReducedMotion, prefersReducedMotion } from "@opencode/ui/motion" import type { EmbedProps, Embeds } from "@opencode/plugin/gui" -import type { Bridge } from "@opencode/plugin/gui/host" +import type { Bridge, BridgeLayout } from "@opencode/plugin/gui/host" import { createEmbedStillState, INITIAL_FLIGHT_STATE, @@ -270,6 +270,8 @@ function EmbedView(props: EmbedProps & { input: Input; bridge: Bridge }) { dispatchSnapshot({ type: "sync", id, visible: props.visible, shown, covered: cover }) } + const scrollport = () => element?.parentElement?.closest("[data-embed-clip]") + const measure = () => { if (!element) return const id = props.id @@ -281,10 +283,21 @@ function EmbedView(props: EmbedProps & { input: Input; bridge: Bridge }) { const top = Math.round(rect.top * zoom) const right = Math.round(rect.right * zoom) const bottom = Math.round(rect.bottom * zoom) - const shown = props.visible && current().documentVisible && !props.input.moving() + const port = scrollport() + const clip = port && scrollClip(port, rect, zoom) + + const shown = + props.visible && + current().documentVisible && + !props.input.moving() && + (!clip || (clip.width > 0 && clip.height > 0)) + // A dialog's scrim covers the box like floating content does, so it dims a still of the page // instead of a blank box, and the page does not blink out and back as the dialog opens and closes. - const cover = !!props.frozen || props.input.dialog() || covered(rect) + const cover = + !!props.frozen || + props.input.dialog() || + covered(clip ? new DOMRect(clip.x / zoom, clip.y / zoom, clip.width / zoom, clip.height / zoom) : rect) // Keep the embed on screen as a still under the floating content. The native view // stays visible until the still is on screen, so the box never flashes blank. @@ -299,7 +312,7 @@ function EmbedView(props: EmbedProps & { input: Input; bridge: Bridge }) { const ring = props.radius ? cardRing(element) : undefined const viewport = { width: Math.round(window.innerWidth * zoom), height: Math.round(window.innerHeight * zoom) } - const next = `${id}:${visible}:${left}:${top}:${right}:${bottom}:${viewport.width}:${viewport.height}:${color}:${ring?.color}:${ring?.width}:${window.devicePixelRatio}` + const next = `${id}:${visible}:${left}:${top}:${right}:${bottom}:${clip?.x}:${clip?.y}:${clip?.width}:${clip?.height}:${viewport.width}:${viewport.height}:${color}:${ring?.color}:${ring?.width}:${window.devicePixelRatio}` if (next === layout) return layout = next @@ -326,9 +339,10 @@ function EmbedView(props: EmbedProps & { input: Input; bridge: Bridge }) { if (!visible && selectStillOnscreen(current().still)) dispatchStill(OFFSCREEN_STILL_EVENT) - const box = { + const box: BridgeLayout = { visible, bounds: { x: left, y: top, width: Math.max(0, right - left), height: Math.max(0, bottom - top) }, + ...(clip && { clip }), viewport, radius: Math.round((props.radius ?? 0) * zoom), } @@ -390,9 +404,16 @@ function EmbedView(props: EmbedProps & { input: Input; bridge: Bridge }) { }, ), ) - // ResizeObserver runs after layout in the same frame; measuring here instead of on the next - // animation frame keeps the native view in step with a region drag. - createResizeObserver(() => element, measure) + // Measure in the same frame as a resize, including when wrapped controls move a fixed-size embed. + createResizeObserver(() => [element, element?.parentElement, scrollport()], measure) + createEventListener( + document, + "scroll", + (event) => { + if (event.target instanceof Element && event.target.contains(element ?? null)) measure() + }, + { capture: true, passive: true }, + ) createEventListener(window, "resize", () => schedule(300)) // A layout transition elsewhere, such as the chat column's width while the side pane opens, can // move the box without resizing it. Track it from when it actually runs, and settle on its end. @@ -408,7 +429,7 @@ function EmbedView(props: EmbedProps & { input: Input; bridge: Bridge }) { portals.observe(document.body, { childList: true }) onCleanup(() => portals.disconnect()) const appearance = new MutationObserver(() => schedule(300)) - appearance.observe(document.documentElement, { attributes: true, attributeFilter: ["style", "data-theme"] }) + appearance.observe(document.documentElement, { attributes: true, attributeFilter: ["style", "data-theme", "dir"] }) onCleanup(() => appearance.disconnect()) createEventListener(window.matchMedia("(prefers-color-scheme: dark)"), "change", () => schedule(300)) createEventListener(document, "visibilitychange", () => @@ -474,6 +495,24 @@ function EmbedView(props: EmbedProps & { input: Input; bridge: Bridge }) { ) } +// The client box excludes the scrollbar track, including a left-side RTL scrollbar. +function scrollClip(port: HTMLElement, rect: DOMRect, zoom: number) { + const area = port.getBoundingClientRect() + const left = area.left + port.clientLeft + const top = area.top + port.clientTop + const x = Math.max(0, Math.round(rect.left * zoom), Math.ceil(left * zoom)) + const y = Math.max(0, Math.round(rect.top * zoom), Math.ceil(top * zoom)) + const right = Math.min(window.innerWidth, left + port.clientWidth) + const bottom = Math.min(window.innerHeight, top + port.clientHeight) + + return { + x, + y, + width: Math.max(0, Math.min(Math.round(rect.right * zoom), Math.floor(right * zoom)) - x), + height: Math.max(0, Math.min(Math.round(rect.bottom * zoom), Math.floor(bottom * zoom)) - y), + } +} + // Decodes a JPEG still, so it paints in the frame it enters the DOM. Undefined when there is none or it cannot decode. async function decode(data: Uint8Array | undefined) { if (!data?.length) return undefined diff --git a/packages/app/test-browser/browser-pane-model.test.ts b/packages/app/test-browser/browser-pane-model.test.ts index ddf31f50b21..36ee59731db 100644 --- a/packages/app/test-browser/browser-pane-model.test.ts +++ b/packages/app/test-browser/browser-pane-model.test.ts @@ -45,6 +45,7 @@ function fixture() { inspect: async () => undefined, highlight: async () => undefined, zoom: async () => undefined, + setViewport: async () => undefined, site: async () => ({ cookies: 0 }), clearSite: async () => undefined, close: async () => undefined, @@ -77,19 +78,50 @@ function fixture() { const ctx = fake as unknown as SetupContext const owned = createRoot((dispose) => ({ model: createModel(ctx), dispose })) - const emit = (session: string, state: Browser.State) => { + const report = (session: string, event: PaneEvent) => { const binding = bindings.get(session) - if (binding) listeners.forEach((listener) => listener({ binding, event: { type: "state", state } })) + if (binding) listeners.forEach((listener) => listener({ binding, event })) } + const emit = (session: string, state: Browser.State) => report(session, { type: "state", state }) + // SAFETY: the model reads only the listed fields of a session ref. // oxlint-disable-next-line anti-slop/no-chained-type-assertions -- see SAFETY above const sessions = refs.map((ref) => ref as unknown as SessionRef) - return { ...owned, sessions, route: setCurrent, refs, emit } + return { ...owned, sessions, route: setCurrent, refs, emit, report } } +test("drops page details when a tab closes or its binding is suspended", () => { + const app = fixture() + + const session = app.sessions[0] + + const closed = tab("a") + + const retained = tab("b") + + const viewport = { mode: "fixed", width: 390, height: 844 } as const + + try { + app.emit(session.id, { tabs: [closed, retained], focusedTabID: closed.id }) + + for (const tab of [closed, retained]) app.report(session.id, { type: "page", tabID: tab.id, zoom: 1, viewport }) + expect(app.model.page(session, closed.id)?.viewport).toEqual(viewport) + const remaining = { tabs: [retained], focusedTabID: retained.id } + app.emit(session.id, remaining) + expect(app.model.page(session, closed.id)).toBeUndefined() + expect(app.model.page(session, retained.id)?.viewport).toEqual(viewport) + + app.report(session.id, { type: "state", state: remaining, error: "browser.pane.suspended" }) + expect(app.model.tab(session, retained.id)?.url).toBe(retained.url) + expect(app.model.page(session, retained.id)).toBeUndefined() + } finally { + app.dispose() + } +}) + test("one session's transitions leave another session's attachment as it was", async () => { const app = fixture() const [alpha, beta] = app.sessions diff --git a/packages/desktop/src/main/plugin/embeds.ts b/packages/desktop/src/main/plugin/embeds.ts index a5dfe2cd07f..3a34c3b43d2 100644 --- a/packages/desktop/src/main/plugin/embeds.ts +++ b/packages/desktop/src/main/plugin/embeds.ts @@ -1,7 +1,7 @@ import { randomUUID } from "node:crypto" -import { ImageView, screen, type BrowserWindow, type WebContentsView } from "electron" -import type { BridgeLayout } from "@opencode/plugin/gui/host" +import { BaseWindow, ImageView, screen, View, type BrowserWindow, type WebContentsView } from "electron" import type { Embed } from "@opencode/plugin/gui/main" +import type { BridgeLayout } from "@opencode/plugin/gui/host" import { createCornerImages } from "../native/corners" import type { Instance } from "./lifecycle" @@ -14,6 +14,7 @@ type Entry = { readonly corners: readonly ImageView[] cornerKey: string layout?: BridgeLayout + clip?: { root: View; window?: BaseWindow } shown: boolean /** Whether the view was on screen after the last apply. */ onscreen: boolean @@ -23,6 +24,12 @@ type Entry = { // A box edge this many CSS px from the window edge follows it while the renderer's layout catches up. const WINDOW_GUTTER = 24 +// A still above MAX_STILL_PIXELS is downscaled to it. Electron cannot capture below native density, +// so anything above MAX_CAPTURE_PIXELS is not captured at all. +const MAX_STILL_PIXELS = 16_000_000 + +const MAX_CAPTURE_PIXELS = 64_000_000 + /** * The web pages main plugins hand to the host. Each stays hidden until its window's renderer lays * it out and the plugin shows it; the renderer's bounds already include the window zoom. @@ -34,16 +41,19 @@ export function createEmbeds() { const apply = (entry: Entry) => { if (entry.window.isDestroyed()) return const layout = entry.layout?.bounds + const cutout = entry.layout?.clip const viewport = entry.layout?.viewport // The renderer measures a frame or more behind a window resize. Edges beside the window // edge follow it now, so the view and its corner masks do not trail inside the panel. // This assumes the box takes the whole change, as the side panel beside the fixed-width chat does. const [width = 0, height = 0] = entry.window.getContentSize() const zoom = entry.window.webContents.getZoomFactor() + // A child window uses screen coordinates and must stay inside its owner. + const clip = cutout && intersectWindow(cutout, width, height) // The renderer's viewport is whole CSS px, so it can be off by one CSS px plus rounding; that is not a resize. const follow = (inset: number, change: number) => - inset <= WINDOW_GUTTER * zoom && Math.abs(change) >= zoom + 1 ? change : 0 + !clip && inset <= WINDOW_GUTTER * zoom && Math.abs(change) >= zoom + 1 ? change : 0 const dx = layout && viewport ? follow(viewport.width - layout.x - layout.width, width - viewport.width) : 0 const dy = layout && viewport ? follow(viewport.height - layout.y - layout.height, height - viewport.height) : 0 @@ -56,9 +66,40 @@ export function createEmbeds() { height: Math.round(layout.height + dy), } - const placed = !!entry.layout?.visible && !!bounds && bounds.width > 0 && bounds.height > 0 + // The DOM scrollport can reflow during a window resize. Wait for its new measurements. + const measured = + !clip || + !viewport || + (Math.abs(viewport.width - width) < zoom + 1 && Math.abs(viewport.height - height) < zoom + 1) + + const placed = + !!entry.layout?.visible && + !!bounds && + bounds.width > 0 && + bounds.height > 0 && + measured && + (!clip || (clip.width > 0 && clip.height > 0)) + + if (placed) { + if ( + clip && + (clip.x !== bounds.x || clip.y !== bounds.y || clip.width !== bounds.width || clip.height !== bounds.height) + ) { + const container = (entry.clip ??= createClip(entry)) + + if (container.window) { + const origin = entry.window.getContentBounds() + + container.window.setBounds({ ...clip, x: origin.x + clip.x, y: origin.y + clip.y }) + } else container.root.setBounds(clip) + + bounds.x -= clip.x + bounds.y -= clip.y + } else if (entry.clip) { + attach(entry, entry.window.contentView) + disposeClip(entry) + } - if (placed && bounds) { entry.view.setBounds(bounds) const size = Math.min( @@ -96,6 +137,12 @@ export function createEmbeds() { const visible = placed && entry.shown entry.corners.forEach((corner) => corner.setVisible(placed && !!entry.cornerKey)) entry.view.setVisible(visible) + const child = entry.clip?.window + + if (child && !child.isDestroyed()) { + if (!visible || !entry.window.isVisible() || entry.window.isMinimized()) child.hide() + else if (!child.isVisible()) child.showInactive() + } if (visible === entry.onscreen) return entry.onscreen = visible @@ -111,8 +158,10 @@ export function createEmbeds() { if (entry.window.isDestroyed()) return entry.view.setVisible(false) - entry.corners.forEach((corner) => entry.window.contentView.removeChildView(corner)) - entry.window.contentView.removeChildView(entry.view) + const root = entry.clip?.root ?? entry.window.contentView + entry.corners.forEach((corner) => root.removeChildView(corner)) + root.removeChildView(entry.view) + disposeClip(entry) } const owned = (windowID: number, id: string) => { @@ -130,6 +179,10 @@ export function createEmbeds() { const refresh = () => entries.forEach((entry) => entry.window === window && apply(entry)) window.on("resize", refresh) window.on("move", refresh) + window.on("show", refresh) + window.on("hide", refresh) + window.on("restore", refresh) + window.on("minimize", refresh) screen.on("display-metrics-changed", refresh) window.once("closed", () => screen.off("display-metrics-changed", refresh)) } @@ -211,10 +264,89 @@ export function createEmbeds() { } } +function intersectWindow(box: Electron.Rectangle, width: number, height: number) { + const x = Math.max(0, Math.ceil(box.x)) + const y = Math.max(0, Math.ceil(box.y)) + + return { + x, + y, + width: Math.max(0, Math.min(width, Math.floor(box.x + box.width)) - x), + height: Math.max(0, Math.min(height, Math.floor(box.y + box.height)) - y), + } +} + +function attach(entry: Entry, root: View) { + root.addChildView(entry.view) + entry.corners.forEach((corner) => root.addChildView(corner)) +} + +// On Cocoa a parent View clips the page's pixels; hit testing requires an owned window. A View sized to the clip +// still routes clicks just outside the crop to the hidden page beneath an app control, so do not fold this back into +// the parent window. +function createClip(entry: Entry) { + const window = + process.platform === "darwin" + ? new BaseWindow({ + parent: entry.window, + frame: false, + roundedCorners: false, + hasShadow: false, + acceptFirstMouse: true, + resizable: false, + minimizable: false, + maximizable: false, + fullscreenable: false, + skipTaskbar: true, + show: false, + }) + : undefined + + // macOS can make the owned window key again without focusing its page, as after leaving fullscreen, and typing + // then needs a click. The window holds only the page, so it takes focus when the window does. + window?.on("focus", () => { + if (!entry.view.webContents.isDestroyed() && !entry.view.webContents.isFocused()) entry.view.webContents.focus() + }) + + const root = window?.contentView ?? new View() + + if (!window) entry.window.contentView.addChildView(root) + attach(entry, root) + + return { root, window } +} + +function disposeClip(entry: Entry) { + const clip = entry.clip + + if (!clip) return + entry.clip = undefined + + if (clip.window) { + if (!clip.window.isDestroyed()) clip.window.destroy() + } else entry.window.contentView.removeChildView(clip.root) +} + // Freezes the shown page so the renderer can paint it under DOM overlays while the view hides. async function capture(entry: Entry) { if (entry.window.isDestroyed() || entry.view.webContents.isDestroyed() || !entry.view.getVisible()) return - const image = await entry.view.webContents.capturePage() + const { width, height } = entry.view.getBounds() - return image.isEmpty() ? undefined : image + // Even a small cutout needs a still of the full view at native density. + const scale = screen.getDisplayMatching((entry.clip?.window ?? entry.window).getBounds()).scaleFactor + + if (width <= 0 || height <= 0 || Math.ceil(width * scale) * Math.ceil(height * scale) > MAX_CAPTURE_PIXELS) return + const image = await entry.view.webContents.capturePage({ x: 0, y: 0, width, height }) + + if (image.isEmpty()) return + const size = image.getSize() + const ratio = Math.sqrt(MAX_STILL_PIXELS / (size.width * size.height)) + + if (ratio >= 1) return image + + return image.resize({ + width: Math.max(1, Math.floor(size.width * ratio)), + height: Math.max(1, Math.floor(size.height * ratio)), + quality: "good", + }) } diff --git a/packages/desktop/src/main/plugin/host.ts b/packages/desktop/src/main/plugin/host.ts index 3ceaa2a1358..b0f7b171ea0 100644 --- a/packages/desktop/src/main/plugin/host.ts +++ b/packages/desktop/src/main/plugin/host.ts @@ -4,7 +4,6 @@ import { pluginIdAvailable, runsOn, shipsPluginGui, - type BridgeLayout, type BridgeMenubarItem, type Entry, type Scope, @@ -1053,7 +1052,7 @@ export function createHost(input: { throw new PluginError("output", { cause, message: String(cause) }) }) }, - embed: (window: number, id: string, layout?: BridgeLayout) => embeds.layout(window, id, layout), + embed: embeds.layout, capture: (window: number, id: string) => embeds.capture(window, id).then((image) => (image ? new Uint8Array(image.toJPEG(90)) : undefined)), listMenubarItems, diff --git a/packages/desktop/src/shared/ipc-rpc/plugins.ts b/packages/desktop/src/shared/ipc-rpc/plugins.ts index bb111274fdc..9ff77197f38 100644 --- a/packages/desktop/src/shared/ipc-rpc/plugins.ts +++ b/packages/desktop/src/shared/ipc-rpc/plugins.ts @@ -36,11 +36,13 @@ const channel = Schema.Finite.check(Schema.isBetween({ minimum: 0, maximum: 255 const rgba = Schema.Tuple([channel, channel, channel, channel]) +const bounds = Schema.Struct({ x: Schema.Finite, y: Schema.Finite, width: Schema.Finite, height: Schema.Finite }) + export const PluginLayout = Schema.Struct({ visible: Schema.Boolean, - bounds: Schema.optionalKey( - Schema.Struct({ x: Schema.Finite, y: Schema.Finite, width: Schema.Finite, height: Schema.Finite }), - ), + bounds: Schema.optionalKey(bounds), + // The intersection with the DOM scrollport, in the same window DIPs as the full bounds. + clip: Schema.optionalKey(bounds), viewport: Schema.optionalKey(Schema.Struct({ width: Schema.Finite, height: Schema.Finite })), background: Schema.optionalKey(rgba), radius: Schema.optionalKey(Schema.Finite.check(Schema.isBetween({ minimum: 0, maximum: 100 }))), diff --git a/packages/gui-plugins/src/browser/assets/viewport-device-chevron.svg b/packages/gui-plugins/src/browser/assets/viewport-device-chevron.svg new file mode 100644 index 00000000000..3b12ee5d513 --- /dev/null +++ b/packages/gui-plugins/src/browser/assets/viewport-device-chevron.svg @@ -0,0 +1,5 @@ + + + + + diff --git a/packages/gui-plugins/src/browser/assets/viewport-rotate.svg b/packages/gui-plugins/src/browser/assets/viewport-rotate.svg new file mode 100644 index 00000000000..f6866f02900 --- /dev/null +++ b/packages/gui-plugins/src/browser/assets/viewport-rotate.svg @@ -0,0 +1,7 @@ + + + + + + + diff --git a/packages/gui-plugins/src/browser/bar.css b/packages/gui-plugins/src/browser/bar.css index d6245d38875..fb6ea885af7 100644 --- a/packages/gui-plugins/src/browser/bar.css +++ b/packages/gui-plugins/src/browser/bar.css @@ -164,6 +164,230 @@ } } +[data-component="browser-preview"] { + /* Prevent the scaled page from contributing to its own Fit measurement. */ + contain: size; + display: grid; + flex: 1 1 0; + min-block-size: 0; + place-content: center; + overflow: hidden; + + &[data-fixed] { + padding-block-start: 4px; + background: var(--v2-background-bg-layer-01); + + > div:first-child { + box-shadow: 0 -1px 2px -1px var(--v2-border-border-muted), var(--v2-elevation-raised); + } + } + + /* The measured content box excludes the resize handles. */ + &[data-resizable] { + --viewport-grip: 14px; + padding-inline-end: var(--viewport-grip); + padding-block-end: var(--viewport-grip); + background: var(--v2-background-bg-layer-01); + + > div { + grid-area: 1 / 1; + } + } + + &[data-manual] { + overflow: auto; + place-content: safe center; + overscroll-behavior: contain; + } +} + +[data-slot="browser-viewport-grip"] { + z-index: 1; + display: grid; + place-items: center; + touch-action: none; + user-select: none; + color: var(--v2-icon-icon-faint); + --grip-end: 100%; + + &:dir(rtl) { + --grip-end: -100%; + } + &:is(:hover, :active, :focus-visible) { + color: var(--v2-icon-icon-base); + } + &:focus-visible { + outline: 1px solid var(--v2-border-border-focus); + } + + &::after { + content: ""; + background: currentColor; + border-radius: 2px; + } + + &[data-axis="width"] { + justify-self: end; + align-self: center; + width: var(--viewport-grip); + height: min(44px, 100%); + translate: var(--grip-end) 0; + cursor: ew-resize; + &::after { + width: 3px; + height: min(24px, 75%); + } + } + + &[data-axis="height"] { + justify-self: center; + align-self: end; + width: min(44px, 100%); + height: var(--viewport-grip); + translate: 0 100%; + cursor: ns-resize; + &::after { + width: min(24px, 75%); + height: 3px; + } + } + + &[data-axis="corner"] { + place-self: end; + width: var(--viewport-grip); + height: var(--viewport-grip); + translate: var(--grip-end) 100%; + cursor: nwse-resize; + &:dir(rtl) { + cursor: nesw-resize; + } + &::after { + place-self: start; + margin: 1px; + width: 7px; + height: 7px; + background: transparent; + border-inline-end: 2px solid currentColor; + border-block-end: 2px solid currentColor; + } + } +} + +[data-component="browser-viewport"] { + display: flex; + flex: none; + flex-wrap: wrap; + justify-content: center; + align-items: center; + gap: 6px 12px; + min-block-size: 36px; + padding: 5px 6px; + border-inline: 0.5px solid var(--v2-border-border-muted); + border-block-end: 0.5px solid var(--v2-border-border-muted); + background: var(--v2-background-bg-base); + color: var(--v2-text-text-faint); + font-size: 13px; + line-height: var(--line-height-compact); + font-variant-numeric: tabular-nums; + + [data-slot="browser-viewport-group"] { + display: flex; + align-items: center; + gap: 6px; + } + + [data-component="text-input-v2"] { + width: 50px; + + input { + text-align: center; + } + } + + [data-slot="browser-viewport-preset-trigger"], + [data-slot="browser-viewport-scale"] { + padding-inline: 8px 4px; + border-radius: 4px; + gap: 4px; + font-size: 13px; + white-space: nowrap; + } + + [data-slot="browser-viewport-preset-trigger"] { + max-inline-size: 100%; + + > span:first-child { + overflow: hidden; + text-overflow: ellipsis; + } + } + + [data-slot="browser-viewport-scale"] { + font-weight: 440; + } + + [data-slot="viewport-chevron"], + [data-slot="viewport-rotate-icon"] { + display: block; + flex: none; + width: 16px; + height: 16px; + background-color: currentColor; + } + + [data-slot="viewport-chevron"] { + mask: url("./assets/viewport-device-chevron.svg") center no-repeat; + } + + [data-slot="viewport-rotate-icon"] { + mask: url("./assets/viewport-rotate.svg") 2px 0.1px no-repeat; + } + + [data-slot="browser-viewport-message"] { + flex-basis: 100%; + } +} + +[data-component="menu-v2-content"][data-slot="browser-viewport-scale-menu"] { + inline-size: 162px; + padding: 4px; + border-radius: 9px; + + [data-component="menu-v2-item"] { + block-size: 28px; + padding-inline: 10px 8px; + } +} + +[data-component="menu-v2-content"][data-slot="browser-viewport-preset-menu"] { + inline-size: min(224px, calc(100dvw - 16px)); + min-inline-size: 0; + max-block-size: var(--kb-popper-content-available-height, calc(100dvh - 16px)); + overflow-y: auto; + overscroll-behavior: contain; + padding: 4px; + border-radius: 10px; + background: var(--v2-background-bg-base); + + &[data-device-list] { + inline-size: max-content; + min-inline-size: min(224px, calc(100dvw - 16px)); + max-inline-size: calc(100dvw - 16px); + } + + [data-component="menu-v2-item"] { + block-size: 30px; + padding-inline: 10px 8px; + border-radius: 5px; + } + + [data-slot="menu-v2-separator"] { + inline-size: 100%; + margin: 4px 0; + flex: none; + } +} + /* Suggestions from the browsing history, under the address field. */ [data-component="browser-suggestions"] { z-index: 60; diff --git a/packages/gui-plugins/src/browser/chromium.ts b/packages/gui-plugins/src/browser/chromium.ts index 58db743ef77..3d65427069e 100644 --- a/packages/gui-plugins/src/browser/chromium.ts +++ b/packages/gui-plugins/src/browser/chromium.ts @@ -48,7 +48,9 @@ import { refusal, type Policy, } from "./policy" -import type { PaneElement } from "./ipc" +import type { PaneElement, PaneEvent, Viewport } from "./ipc" +import { createViewportWriter } from "./viewport-native" +import { createDeviceEmulation } from "./mobile-emulation" type Element = { backendID: number; frameID: string; sessionID?: string } @@ -117,8 +119,7 @@ const inspectOff = { mode: "none", highlightConfig: inspectHighlight } // Chromium's zoom presets, so a step lands where it would in the system browser. const zoomSteps = [0.25, 0.33, 0.5, 0.67, 0.75, 0.8, 0.9, 1, 1.1, 1.25, 1.5, 1.75, 2, 2.5, 3, 4, 5] -/** A page's icon as a data URL, and its zoom factor, 1 at 100%. */ -export type PageDetail = { icon?: string; zoom: number } +export type PageDetail = Omit, "type" | "tabID"> /** The zoom a key press asks for: the system browser's zoom keys, with the platform's modifier already checked. */ function zoomKey(input: Electron.Input) { @@ -185,10 +186,12 @@ export function createBrowserPage( publish: (error?: string) => void /** Reports the element picker starting, stopping, or picking an element. */ inspect?: (event: { active: boolean; element?: PaneElement }) => void - /** Reports the page's icon or zoom changing. */ + /** Reports changes to the page's icon, zoom, or layout viewport. */ detail?: (detail: PageDetail) => void /** The page's zoom changed, which Chromium applies to every page of the same origin in the partition. */ zoomed?: () => void + /** Whether the build offers the device toolbar, which owns ⇧⌘M. */ + toolbar: boolean /** Hands a browser or shell shortcut to the app window. */ shortcut?: (shortcut: PageShortcut) => void /** The page took keyboard focus. */ @@ -229,6 +232,17 @@ export function createBrowserPage( const contents = view.webContents const detachNetwork = options.network?.attach(contents) + + // A cropped page can own focus in its child window. Only that child may raise the host. + function focusHost() { + if (win.isDestroyed()) return + const focused = electron.BaseWindow.getFocusedWindow() + + if (focused?.getParentWindow() === win && focused.contentView.children.includes(view)) win.focus() + + if (win.isFocused()) win.webContents.focus() + } + contents.on("before-input-event", (event, input) => { if (input.type !== "keyDown") return @@ -246,7 +260,7 @@ export function createBrowserPage( return } - const shortcut = pageShortcut(input, process.platform === "darwin") + const shortcut = pageShortcut(input, process.platform === "darwin", options.toolbar) if (shortcut) { event.preventDefault() @@ -280,28 +294,66 @@ export function createBrowserPage( if (next === current) return contents.setZoomFactor(next) + detail() options.zoomed?.() } let documentState = createDocumentState(options.restore) const closed = () => isDocumentClosed(documentState) - let reported = "" + let reported: PageDetail | undefined + const cdp = createCdp(contents) + + const sessions = new Map() + const device = createDeviceEmulation(cdp, sessions) + const metrics = createViewportWriter(device.apply, report, closed) + + function report() { + if (closed()) return + // Chromium's factor for the outermost presets can land a rounding error outside them. + const zoom = Math.min(5, Math.max(0.25, contents.getZoomFactor())) + const icon = documentIcon(documentState) + const { viewport, viewportNotice } = metrics.applied() + + if ( + reported?.zoom === zoom && + reported.icon === icon && + reported.viewport === viewport && + reported.viewportNotice === viewportNotice + ) + return + reported = { zoom, viewport, ...(icon && { icon }), ...(viewportNotice && { viewportNotice }) } + options.detail?.(reported) + } + + async function setViewport(viewport: Viewport) { + if (closed() || contents.isDestroyed()) throw new Error("browser.pane.unavailable") + const zoom = contents.getZoomFactor() + await metrics.set(viewport, view.getBounds(), zoom) + + if (closed() || contents.isDestroyed()) throw new Error("browser.pane.unavailable") + + if (viewport.mode === "fixed" && zoom !== 1) throw new Error("viewport.zoomPrompt") + } + + const resize = () => { + const viewport = metrics.requested() + + if (viewport.mode === "fixed") void setViewport(viewport).catch(() => undefined) + } const detail = () => { if (closed()) return - // Chromium's factor for the outermost presets can land a rounding error outside them. - const next: PageDetail = { zoom: Math.min(5, Math.max(0.25, contents.getZoomFactor())) } - const icon = documentIcon(documentState) + // Navigation and another tab of the same origin can both change this page's zoom. + const viewport = metrics.requested() - if (icon) next.icon = icon - const key = `${next.zoom}:${next.icon ?? ""}` - - if (key === reported) return - reported = key - options.detail?.(next) + if (viewport.mode === "fixed" && contents.getZoomFactor() !== 1) resize() + void metrics.settled().then(report) } - const cdp = createCdp(contents) + view.on("bounds-changed", resize) + cdp.on("Page.frameResized", () => { + if (!closed() && contents.getZoomFactor() !== reported?.zoom) detail() + }) const documents = new Map() const sourceURLs = () => [...new Set([contents.getURL(), ...documents.values()])].sort() const files = createBrowserFiles(sourceURLs) @@ -313,7 +365,6 @@ export function createBrowserPage( const picked = new Map() let pickerState = initialPickerState let pickerTimer: ReturnType | undefined - const sessions = new Map() const parents = new Map() const contexts = new Map() let dialogGate = createDialogGateState() @@ -393,7 +444,10 @@ export function createBrowserPage( } contents.on("did-start-loading", () => dispatch({ type: "loading.started" })) - contents.on("did-stop-loading", () => dispatch({ type: "loading.stopped", title: contents.getTitle() })) + contents.on("did-stop-loading", () => { + dispatch({ type: "loading.stopped", title: contents.getTitle() }) + detail() + }) contents.on("did-start-navigation", (event) => dispatch({ type: "navigation.started", @@ -511,7 +565,9 @@ export function createBrowserPage( if (parentID) parents.set(targetInfo.targetId, parentID) sessions.set(targetInfo.targetId, sessionId) + // An isolated frame has its own renderer and needs its own UA and touch overrides. void Promise.all([ + device.frame(sessionId), diagnostics.enable(sessionId), cdp.send("Page.enable", {}, sessionId), cdp.send( @@ -603,6 +659,8 @@ export function createBrowserPage( contents, state, ready, + focusHost, + setViewport, /** The host embed the renderer lays this page out with. */ embed: embed.id, async inspect(enabled: boolean) { @@ -647,7 +705,7 @@ export function createBrowserPage( () => undefined, ) }, - /** Reports the page's icon and zoom when either changed since the last report. */ + /** Refreshes the page presentation, including Chromium's per-origin zoom. */ detail, async execute(command: Browser.Command, signal: AbortSignal): Promise { await ready @@ -665,7 +723,14 @@ export function createBrowserPage( signal.addEventListener("abort", cancel, { once: true }) try { - return await Promise.race([perform(command, run.controller.signal), run.interrupted.promise]) + const act = () => perform(command, run.controller.signal) + + // A resize or screenshot between measuring a ref and clicking it would hit a different point. + const action = pointerOperations.includes(command.action.type) + ? metrics.exclusive(() => device.mouse(act)) + : act() + + return await Promise.race([action, run.interrupted.promise]) } finally { signal.removeEventListener("abort", cancel) runs.delete(runID) @@ -678,6 +743,7 @@ export function createBrowserPage( dispatchDialog({ type: "disposed" }) void dispatchPicker({ type: "disposed" }) clearTimeout(pickerTimer) + view.off("bounds-changed", resize) detachNetwork?.() contents.session.off("will-download", download) await profiling.dispose() @@ -692,6 +758,8 @@ export function createBrowserPage( } async function perform(command: Browser.Command, signal: AbortSignal): Promise { + abortError(signal) + if (isPickerActive(pickerState) && pointerOperations.includes(command.action.type)) await dispatchPicker({ type: "pointer.command" }) @@ -838,7 +906,7 @@ export function createBrowserPage( await point(destination) const from = await point(source) const box = await rect(destination) - const to = { x: box.x + box.width / 2, y: box.y + box.height / 2 } + const to = await elementCenter(box) const html5 = await call(source, "function() { return this.draggable; }") let data: Protocol.Input.DragData | undefined @@ -908,11 +976,10 @@ export function createBrowserPage( await key(action.key) break case "scroll": { - const bounds = view.getBounds() + const { cssVisualViewport } = await cdp.send("Page.getLayoutMetrics") await cdp.send("Input.dispatchMouseEvent", { type: "mouseWheel", - x: bounds.width / 2, - y: bounds.height / 2, + ...inputCenter({ x: 0, y: 0, width: cssVisualViewport.clientWidth, height: cssVisualViewport.clientHeight }), deltaX: action.deltaX ?? 0, deltaY: action.deltaY, }) @@ -970,41 +1037,45 @@ export function createBrowserPage( ) }, ) - const element = action.ref ? await rect(target(action.ref), true) : undefined - const metrics = await cdp.send("Page.getLayoutMetrics") - - const bounds = element - ? { - ...element, - x: element.x + metrics.cssVisualViewport.pageX, - y: element.y + metrics.cssVisualViewport.pageY, - } - : action.fullPage - ? metrics.cssContentSize - : { - x: metrics.cssVisualViewport.pageX, - y: metrics.cssVisualViewport.pageY, - width: metrics.cssVisualViewport.clientWidth, - height: metrics.cssVisualViewport.clientHeight, - } - - const pixelRatio = contents.getZoomFactor() * electron.screen.getDisplayMatching(win.getBounds()).scaleFactor - const scale = Math.min(1, (action.maxWidth ?? 2000) / (bounds.width * pixelRatio)) - - if (bounds.width <= 0 || bounds.height <= 0) - throw new Error( - "Element or page has no visible screenshot area. Take a fresh snapshot and choose a visible element, or omit ref to capture the viewport.", - ) - - if (bounds.width * bounds.height * (scale * pixelRatio) ** 2 > 16_000_000) - throw new Error("Screenshot exceeds 16 megapixels; capture an element or use a smaller maxWidth.") const format = action.format ?? "jpeg" - const capture = await cdp.send("Page.captureScreenshot", { - format, - quality: format === "png" ? undefined : (action.quality ?? 80), - captureBeyondViewport: true, - clip: { ...bounds, scale }, + const capture = await metrics.exclusive(async () => { + abortError(signal) + const element = action.ref ? await rect(target(action.ref), true) : undefined + const layout = await cdp.send("Page.getLayoutMetrics") + const viewport = layout.cssVisualViewport + + const bounds = element + ? { + ...element, + x: element.x + layout.cssLayoutViewport.pageX, + y: element.y + layout.cssLayoutViewport.pageY, + } + : action.fullPage + ? layout.cssContentSize + : { x: viewport.pageX, y: viewport.pageY, width: viewport.clientWidth, height: viewport.clientHeight } + + if (bounds.width <= 0 || bounds.height <= 0) + throw new Error( + "Element or page has no visible screenshot area. Take a fresh snapshot and choose a visible element, or omit ref to capture the viewport.", + ) + + // CDP captures the unscaled layout. Its clip is in page DIPs, even for a scaled native preview. + const zoom = viewport.zoom ?? contents.getZoomFactor() + const pixelRatio = zoom * electron.screen.getDisplayMatching(win.getBounds()).scaleFactor + // A mobile page can initially zoom out to fit a 980 CSS px layout on a phone screen. + const scale = Math.min(viewport.scale, (action.maxWidth ?? 2000) / (bounds.width * pixelRatio)) + + if (bounds.width * bounds.height * (scale * pixelRatio) ** 2 > 16_000_000) + throw new Error("Screenshot exceeds 16 megapixels; capture an element or use a smaller maxWidth.") + abortError(signal) + + return device.capture({ + format, + quality: format === "png" ? undefined : (action.quality ?? 80), + captureBeyondViewport: true, + clip: { ...scaleRect(bounds, zoom), scale }, + }) }) const id = await files.save(`screenshot.${format}`, `image/${format}`, Buffer.from(capture.data, "base64"), [ @@ -1277,9 +1348,10 @@ export function createBrowserPage( async function rect(element: Element, scroll = false) { if (scroll) { await cdp.send("DOM.scrollIntoViewIfNeeded", { backendNodeId: element.backendID }, element.sessionID) - // Force a compositor update after scrolling. requestAnimationFrame can - // stall in a hidden WebContentsView, even with background throttling off. - await contents.capturePage(undefined, { stayHidden: false, stayAwake: true }) + // A one-DIP capture flushes scrolling even in a hidden tab. A resize can briefly replace the surface. + await contents + .capturePage({ x: 0, y: 0, width: 1, height: 1 }, { stayHidden: false, stayAwake: true }) + .catch(() => painted(element.sessionID)) } const bounds = Schema.Struct({ x: Schema.Finite, y: Schema.Finite, width: Schema.Finite, height: Schema.Finite }) @@ -1330,9 +1402,22 @@ export function createBrowserPage( } async function point(element: Element) { - const box = await rect(element, true) + return elementCenter(await rect(element, true)) + } - return { x: box.x + box.width / 2, y: box.y + box.height / 2 } + async function elementCenter(box: PaneElement["rect"]) { + if (!device.mobile()) return inputCenter(box) + // Focusing a field can pan the visual viewport inside the layout viewport. CDP adds that offset on dispatch. + const { cssVisualViewport } = await cdp.send("Page.getLayoutMetrics") + + return inputCenter({ ...box, x: box.x - cssVisualViewport.offsetX, y: box.y - cssVisualViewport.offsetY }) + } + + // Input.dispatch* applies page zoom itself. Its coordinates are relative to the full scaled view, even when panned. + function inputCenter(box: PaneElement["rect"]) { + const scale = metrics.applied().scale + + return { x: (box.x + box.width / 2) * scale, y: (box.y + box.height / 2) * scale } } async function click(element: Element, button = "left", count = 1, modifiers: readonly string[] = []) { @@ -1582,19 +1667,19 @@ export function createBrowserPage( case "describe": { const attempt = effect.attempt - return describePick(effect.backendID, effect.sessionID).then( - (pick) => dispatchPicker({ type: "describe.succeeded", attempt, target: pick.target, element: pick.element }), - () => dispatchPicker({ type: "describe.failed", attempt }), - ) + return metrics + .exclusive(() => describePick(effect.backendID, effect.sessionID)) + .then( + (pick) => + dispatchPicker({ type: "describe.succeeded", attempt, target: pick.target, element: pick.element }), + () => dispatchPicker({ type: "describe.failed", attempt }), + ) } case "pick": { const ref = options.shared.ref() picked.set(ref, effect.target) - - // The pick focused the page; the comment editor opens in the app window. Focus only moves - // within a focused window, so synthetic input never raises a background window. - if (!win.isDestroyed() && win.isFocused()) win.webContents.focus() + focusHost() options.inspect?.({ active: false, element: { ref: Browser.Ref.make(ref), ...effect.element } }) return @@ -1660,16 +1745,24 @@ export function createBrowserPage( // Keep the box model on the picked element, without the tooltip, while the renderer freezes the page. await cdp.send("Overlay.highlightNode", { backendNodeId: backendID, highlightConfig: pickedHighlight }, sessionID) const target = { backendID, frameID: await frameOf(backendID, sessionID), sessionID } - const [details, box, accessible] = await Promise.all([describe(target), rect(target), accessibility(target)]) + + const [details, box, accessible, layout] = await Promise.all([ + describe(target), + rect(target), + accessibility(target), + cdp.send("Page.getLayoutMetrics"), + ]) + await painted(sessionID) - const zoom = contents.getZoomFactor() + const visual = layout.cssVisualViewport + const scale = contents.getZoomFactor() * metrics.applied().scale * visual.scale return { target, element: { ...details, ...accessible, - rect: { x: box.x * zoom, y: box.y * zoom, width: box.width * zoom, height: box.height * zoom }, + rect: scaleRect({ ...box, x: box.x - visual.offsetX, y: box.y - visual.offsetY }, scale), }, } } @@ -1807,3 +1900,7 @@ export function createBrowserPage( ]) } } + +function scaleRect(box: PaneElement["rect"], scale: number) { + return { x: box.x * scale, y: box.y * scale, width: box.width * scale, height: box.height * scale } +} diff --git a/packages/gui-plugins/src/browser/comment.css b/packages/gui-plugins/src/browser/comment.css index a0ac333aacc..08c450e4940 100644 --- a/packages/gui-plugins/src/browser/comment.css +++ b/packages/gui-plugins/src/browser/comment.css @@ -12,12 +12,13 @@ } } -[data-component="browser-comment"] { +[data-component="browser-comment"], +[data-slot="browser-comment-spotlight"] { animation: browser-comment-enter 160ms cubic-bezier(0.215, 0.61, 0.355, 1); } /* Dim the frozen page around the picked element, whose DevTools highlight stays in the still. */ -[data-component="browser-comment"] [data-slot="browser-comment-spotlight"] { +[data-slot="browser-comment-spotlight"] { box-shadow: 0 0 0 1px var(--v2-border-border-focus), 0 0 0 100vmax color-mix(in srgb, var(--v2-overlay-simple-overlay-scrim) 50%, transparent); @@ -30,6 +31,7 @@ :root[data-motion="reduce"] { [data-component="browser-comment"], + [data-slot="browser-comment-spotlight"], [data-component="browser-comment"] [data-slot="browser-comment-editor"] { animation: none; } diff --git a/packages/gui-plugins/src/browser/connection.test.ts b/packages/gui-plugins/src/browser/connection.test.ts index 50db61ddfe9..d3d023da961 100644 --- a/packages/gui-plugins/src/browser/connection.test.ts +++ b/packages/gui-plugins/src/browser/connection.test.ts @@ -1,8 +1,9 @@ import { expect, test } from "bun:test" import { Browser } from "@opencode/plugin-browser/rpc" import type { IpcClient } from "@opencode/plugin/gui" +import { Schema } from "effect" import { openBinding } from "./connection" -import type { BrowserPane, PaneEvent } from "./ipc" +import { BrowserPane, type PaneEvent } from "./ipc" type Client = IpcClient<(typeof BrowserPane)["spec"]> @@ -10,6 +11,27 @@ const tabID = Browser.TabID.make("tab_aaaaaaaa-1111-1111-1111-111111111111") const element = Browser.Ref.make("e4") +test("viewport IPC accepts Auto or two bounded whole CSS dimensions", () => { + const accepts = Schema.is(BrowserPane.spec.methods.setViewport.input) + + const message = { binding: "desktop", tabID } + expect(accepts({ ...message, viewport: { mode: "auto" } })).toBe(true) + const viewport = { mode: "fixed", width: 1, height: 4096, preset: "laptop", scale: 2 } as const + + const command = Schema.decodeUnknownSync(BrowserPane.spec.methods.setViewport.input)({ ...message, viewport }) + expect(command.viewport).toEqual(viewport) + expect( + Schema.decodeUnknownSync(BrowserPane.spec.events.event)({ + binding: message.binding, + event: { type: "page", tabID, zoom: 1, viewport: command.viewport }, + }).event, + ).toMatchObject({ viewport }) + + for (const invalid of [{ height: undefined }, { width: 1.5 }, { width: 0 }, { height: 4097 }, { scale: 3 }]) { + expect(accepts({ ...message, viewport: { ...viewport, ...invalid } })).toBe(false) + } +}) + // The client stands in for the pane's main entry; `reject` is the error its register rejects with. function fixture(reject?: Error) { const listeners = new Map void>() @@ -32,6 +54,7 @@ function fixture(reject?: Error) { highlights.push(input) }, zoom: async () => undefined, + setViewport: async () => undefined, site: async () => ({ cookies: 0 }), clearSite: async () => undefined, close: async (input) => { diff --git a/packages/gui-plugins/src/browser/connection.ts b/packages/gui-plugins/src/browser/connection.ts index f763af397fa..52669ab9bee 100644 --- a/packages/gui-plugins/src/browser/connection.ts +++ b/packages/gui-plugins/src/browser/connection.ts @@ -1,7 +1,7 @@ import type { Browser } from "@opencode/plugin-browser/rpc" import type { IpcClient } from "@opencode/plugin/gui" import { uuid } from "@opencode/util/uuid" -import type { BrowserPane, PaneEvent } from "./ipc" +import type { BrowserPane, PaneEvent, Viewport } from "./ipc" type Client = IpcClient<(typeof BrowserPane)["spec"]> @@ -20,6 +20,7 @@ export type Registration = { inspect(tabID: Browser.TabID, enabled: boolean): void highlight(tabID: Browser.TabID, ref?: Browser.Ref): void zoom(tabID: Browser.TabID, zoom: Zoom): void + setViewport(tabID: Browser.TabID, viewport: Viewport): Promise site(tabID: Browser.TabID): Promise<{ cookies: number }> clearSite(tabID: Browser.TabID): Promise close(): void @@ -75,6 +76,7 @@ export function openBinding(input: { if (status.closed) return void ready.then(() => client.zoom({ binding, tabID, zoom })).catch(() => undefined) }, + setViewport: (tabID, viewport) => ready.then(() => client.setViewport({ binding, tabID, viewport })), site: (tabID) => ready.then(() => client.site({ binding, tabID })), clearSite: (tabID) => ready.then(() => client.clearSite({ binding, tabID })), close() { diff --git a/packages/gui-plugins/src/browser/i18n/en.ts b/packages/gui-plugins/src/browser/i18n/en.ts index b33085d6a27..8ca25bff0fb 100644 --- a/packages/gui-plugins/src/browser/i18n/en.ts +++ b/packages/gui-plugins/src/browser/i18n/en.ts @@ -3,6 +3,7 @@ export default { "command.open": "Open browser", "command.reload": "Reload browser page", "command.inspect": "Select an element in the browser page", + "command.toggleToolbar": "Toggle browser toolbar", "command.address": "Focus browser address", "command.close": "Close browser tab", "command.newTab": "New browser tab", @@ -59,6 +60,43 @@ export default { "zoom.out": "Zoom out", "zoom.reset": "Reset zoom", "zoom.percent": "{{percent}}%", + "viewport.label": "Viewport", + "viewport.dimensions": "Viewport dimensions", + "viewport.width": "Viewport width in CSS pixels", + "viewport.height": "Viewport height in CSS pixels", + "viewport.resizeWidth": "Resize viewport width", + "viewport.resizeHeight": "Resize viewport height", + "viewport.rotate": "Rotate viewport", + "viewport.preset": "Device preset", + "viewport.responsive": "Responsive", + "viewport.custom": "Custom", + "viewport.moreDevices": "More devices", + "viewport.device.galaxy-s26": "Samsung Galaxy S26", + "viewport.device.compact-phone": "Samsung Galaxy A55", + "viewport.device.iphone-18-pro": "iPhone 18 Pro", + "viewport.device.iphone-se": "iPhone SE", + "viewport.device.iphone-16": "iPhone 16", + "viewport.device.iphone-16-pro-max": "iPhone 17 / 18 Max", + "viewport.device.iphone-17e": "iPhone 17e", + "viewport.device.iphone-duo-closed": "iPhone Duo (closed)", + "viewport.device.iphone-duo-open": "iPhone Duo (open)", + "viewport.device.pixel-10": "Pixel 10", + "viewport.device.pixel-9-pro-fold": "Pixel 9 Pro Fold", + "viewport.device.ipad-air-11": "iPad / iPad Air 11″", + "viewport.device.ipad-mini-a20-pro": "iPad mini", + "viewport.device.ipad-pro-13": "iPad Pro 13", + "viewport.device.surface-pro-10": "Surface Pro 10", + "viewport.device.laptop": "Laptop", + "viewport.device.desktop": "Desktop", + "viewport.scale": "Preview scale", + "viewport.fit": "Fit to pane", + "viewport.fitPercent": "Fit - {{percent}}%", + "viewport.fitBelowOne": "Fit - <1%", + "viewport.invalid": "Use whole numbers from 1 to 4096 for width and height.", + "viewport.error": "Couldn’t apply the viewport. Try again.", + "viewport.zoomPrompt": "Reset page zoom to 100% to set a fixed viewport.", + "viewport.zoomExit": "The page is using the pane size because page zoom changed.", + "viewport.fitFailed": "Couldn’t fit the preview. Use the pane size or try different dimensions.", "link.copy": "Copy link", "history.clear": "Clear history", } diff --git a/packages/gui-plugins/src/browser/ipc.ts b/packages/gui-plugins/src/browser/ipc.ts index 2bcd0e93ed8..cbb73cbcf62 100644 --- a/packages/gui-plugins/src/browser/ipc.ts +++ b/packages/gui-plugins/src/browser/ipc.ts @@ -1,6 +1,7 @@ import { Browser } from "@opencode/plugin-browser/rpc" import { Schema } from "effect" import { Ipc } from "@opencode/plugin/gui" +import { devices, previewScales } from "./viewport-presets" const text = (maximum: number) => Schema.String.check(Schema.isMinLength(1), Schema.isMaxLength(maximum)) @@ -11,6 +12,24 @@ const binding = text(128) /** The longest page icon data URL main reports: a 32px PNG is a few kilobytes; larger icons are dropped. */ export const MAX_ICON_URL = 24_576 +const dimension = Schema.Int.check(Schema.isBetween({ minimum: 1, maximum: 4096 })) + +/** The layout viewport in guest CSS pixels. Preview scaling is local to the displayed view. */ +export const Viewport = Schema.Union([ + Schema.Struct({ mode: Schema.Literal("auto") }), + Schema.Struct({ + mode: Schema.Literal("fixed"), + width: dimension, + height: dimension, + // Omit scale to fit the pane. + scale: Schema.optional(Schema.Literals(previewScales)), + // The selected device survives rotation; custom dimensions have no preset. + preset: Schema.optionalKey(Schema.Literals(devices.map((device) => device.id))), + }), +]) + +export type Viewport = typeof Viewport.Type + /** An element the user picked in the page. The ref stays valid for browser tools until the page navigates. */ export const PaneElement = Schema.Struct({ ref: Browser.Ref, @@ -42,13 +61,14 @@ export const PaneEvent = Schema.Union([ active: Schema.Boolean, element: Schema.optionalKey(PaneElement), }), - // Details only this desktop shows, kept out of the server's tab state: the page's icon as a data URL, and its zoom - // factor, 1 at 100%. + // Presentation details belong to this desktop's page, outside the server's tab inventory. Schema.Struct({ type: Schema.Literal("page"), tabID: Browser.TabID, icon: Schema.optionalKey(detail(MAX_ICON_URL).check(Schema.isStartingWith("data:image/"))), zoom: Schema.Finite.check(Schema.isBetween({ minimum: 0.25, maximum: 5 })), + viewport: Viewport, + viewportNotice: Schema.optionalKey(Schema.Literals(["zoom", "failed"])), }), // The user put keyboard focus in the page, outside the window's own document. Schema.Struct({ type: Schema.Literal("focused"), tabID: Browser.TabID }), @@ -115,6 +135,7 @@ export const BrowserPane = Ipc.define({ zoom: { input: Schema.Struct({ binding, tabID: Browser.TabID, zoom: Schema.Literals(["in", "out", "reset"]) }), }, + setViewport: { input: Schema.Struct({ binding, tabID: Browser.TabID, viewport: Viewport }) }, // The site data the page's address can read in this desktop's browser: its cookie count. Zero for a page that is // not a web page. site: { diff --git a/packages/gui-plugins/src/browser/keys.test.ts b/packages/gui-plugins/src/browser/keys.test.ts index 323b76e6bda..ee90174ef1e 100644 --- a/packages/gui-plugins/src/browser/keys.test.ts +++ b/packages/gui-plugins/src/browser/keys.test.ts @@ -59,6 +59,9 @@ test("forwards browser and shell shortcuts while leaving page editing, find, rel ["mod+shift", "R", true, "r"], ["mod+shift", "R", false, "r"], ["mod+shift", "P", true, "p"], + ["mod+shift", "M", true, "m"], + ["mod+shift", "M", false, "m"], + ["mod", "m", true, undefined], ["mod+shift", "L", true, "l"], ["mod+shift", "E", true, "e"], ["mod+shift", "{", true, "[", "BracketLeft"], @@ -128,12 +131,18 @@ test("forwards browser and shell shortcuts while leaving page editing, find, rel modifiers, key, mac, - pageShortcut(press(modifiers, key, mac, code), mac)?.key, + pageShortcut(press(modifiers, key, mac, code), mac, true)?.key, ]) expect(results).toEqual(cases.map(([modifiers, key, mac, forwarded]) => [modifiers, key, mac, forwarded])) }) +test("leaves the device toolbar chord to the page in builds without the toolbar", () => { + expect(pageShortcut(press("mod+shift", "M", true), true, false)).toBeUndefined() + expect(pageShortcut(press("mod+shift", "M", false), false, false)).toBeUndefined() + expect(pageShortcut(press("mod+shift", "T", true), true, false)?.key).toBe("t") +}) + test("reloads the focused page on F5 and the platform's reload chord, leaving other R chords to the window", () => { const cases: [modifiers: string, key: string, mac: boolean, reloads: boolean, code?: string][] = [ ["", "F5", true, true], diff --git a/packages/gui-plugins/src/browser/keys.ts b/packages/gui-plugins/src/browser/keys.ts index 2dfc6a0bdf3..bd71405377f 100644 --- a/packages/gui-plugins/src/browser/keys.ts +++ b/packages/gui-plugins/src/browser/keys.ts @@ -14,11 +14,13 @@ export type PageShortcut = { // opening, closing or switching tabs, focusing the address field, opening settings, and toggling the file tree. const shellKeys = ["t", "w", "l", "n", "1", "2", "3", "4", "5", "6", "7", "8", "9", ",", "\\"] -// Shifted chords with the platform's modifier that belong to the app shell rather than the page: reopening a tab, -// switching side-panel tabs, toggling the side panel, opening the command palette, or running session commands. New Window and Close Window -// (⇧⌘N, ⇧⌘W) stay unclaimed: only their menu accelerators handle them, and claiming a chord also blocks the menu. +// Shifted shortcuts for the shell and browser pane. Leave ⇧⌘N and ⇧⌘W to the window menu accelerators: +// forwarding either chord would block its menu action. const shellShiftKeys = ["t", "[", "]", "r", "p", "l", "e", "o", "s"] +// ⇧⌘M toggles the device toolbar, which only builds that offer the toolbar register; elsewhere the page keeps it. +const toolbarShiftKey = "m" + // The side panel's launchers: Review, Browser and Open file, with ⌃⌘ on macOS and Ctrl+Alt elsewhere. Only a typed // Latin letter counts, so AltGr (Ctrl+Alt) typing on other layouts stays with the page. const launcherKeys = ["r", "b", "p"] @@ -58,7 +60,7 @@ export function pageReload(input: PageKey, mac: boolean) { * browser pane or app shell, or `undefined` when the key belongs to the page (typing, editing, clipboard, find, * history, or in-page shortcuts like ⌘K and ⌘B). */ -export function pageShortcut(input: PageKey, mac: boolean): PageShortcut | undefined { +export function pageShortcut(input: PageKey, mac: boolean, toolbar: boolean): PageShortcut | undefined { const key = pageLetter(input) const mod = mac ? input.meta && !input.control : input.control && !input.meta const launcher = mac ? input.meta && input.control && !input.alt : input.control && input.alt && !input.meta @@ -70,7 +72,7 @@ export function pageShortcut(input: PageKey, mac: boolean): PageShortcut | undef (input.alt ? !input.shift && (key === "arrowleft" || key === "arrowright") : input.shift - ? shellShiftKeys.includes(key) + ? shellShiftKeys.includes(key) || (toolbar && key === toolbarShiftKey) : shellKeys.includes(key))) if (!matched) return undefined diff --git a/packages/gui-plugins/src/browser/main.ts b/packages/gui-plugins/src/browser/main.ts index d01f976366b..3e8a4658356 100644 --- a/packages/gui-plugins/src/browser/main.ts +++ b/packages/gui-plugins/src/browser/main.ts @@ -2,6 +2,7 @@ import { builtinMainContext } from "@opencode/plugin/gui/builtin" import type { Plugin } from "@opencode/plugin/gui/main" import type { Pane } from "./pane" import { RefsStore } from "./refs" +import { viewportToolbarAvailable } from "./viewport-toolbar-machine" import { BrowserPane } from "./ipc" import { createMainPaneState, @@ -39,6 +40,7 @@ const setup: Plugin.MainSetup = (ctx) => { storage: builtinMainContext(ctx).storage, refs, embeds: ctx.embeds, + toolbar: viewportToolbarAvailable(ctx.app.channel), emit: (window, value) => provider.emit("event", value, window), }), }) @@ -85,6 +87,8 @@ const setup: Plugin.MainSetup = (ctx) => { inspect: async (input, caller) => (await pane()).inspect(caller.window, input.binding, input.tabID, input.enabled), highlight: async (input, caller) => (await pane()).highlight(caller.window, input.binding, input.tabID, input.ref), zoom: async (input, caller) => (await pane()).zoom(caller.window, input.binding, input.tabID, input.zoom), + setViewport: async (input, caller) => + (await pane()).setViewport(caller.window, input.binding, input.tabID, input.viewport), site: async (input, caller) => (await pane()).site(caller.window, input.binding, input.tabID), clearSite: async (input, caller) => (await pane()).clearSite(caller.window, input.binding, input.tabID), close: async (input, caller) => (await pane()).close(caller.window, input.binding), diff --git a/packages/gui-plugins/src/browser/mobile-emulation.ts b/packages/gui-plugins/src/browser/mobile-emulation.ts new file mode 100644 index 00000000000..35550dcd293 --- /dev/null +++ b/packages/gui-plugins/src/browser/mobile-emulation.ts @@ -0,0 +1,121 @@ +import type { Protocol } from "devtools-protocol" +import type { Cdp } from "./cdp" +import type { Viewport } from "./ipc" +import { devices, type Device } from "./viewport-presets" + +// Apple presets select screen geometry. All pages still run Blink; expose Chromium's reduced UA and native brands. +function userAgent(profile: Device["profile"]): Protocol.Emulation.SetUserAgentOverrideRequest { + if (profile === "desktop") return { userAgent: "" } + + const phone = profile === "phone" + const windows = profile === "windows-tablet" + const os = windows ? "Windows NT 10.0; Win64; x64" : "Linux; Android 10; K" + + return { + userAgent: `Mozilla/5.0 (${os}) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/${process.versions.chrome.split(".")[0]}.0.0.0${phone ? " Mobile" : ""} Safari/537.36`, + platform: windows ? "Win32" : "Linux armv8l", + userAgentMetadata: { + platform: windows ? "Windows" : "Android", + platformVersion: "", + architecture: windows ? "x86" : "", + bitness: windows ? "64" : "", + model: "", + mobile: phone, + wow64: false, + formFactors: [phone ? "Mobile" : "Tablet"], + }, + } +} + +/** The page's viewport writer serializes these CDP overrides and rolls back to Auto on failure. */ +export function createDeviceEmulation(cdp: Cdp, sessions: ReadonlyMap) { + let applied: Device["profile"] | undefined = "desktop" + + const touch = (enabled: boolean, sessionID?: string) => + cdp.send("Emulation.setTouchEmulationEnabled", enabled ? { enabled, maxTouchPoints: 5 } : { enabled }, sessionID) + + function identity(profile: Device["profile"], sessionID?: string) { + return Promise.all([ + cdp.send("Emulation.setUserAgentOverride", userAgent(profile), sessionID), + touch(profile !== "desktop", sessionID), + ]) + } + + const emitTouch = (enabled: boolean) => + cdp.send("Emulation.setEmitTouchEventsForMouse", { enabled, configuration: "mobile" }) + + function onTargets(send: (sessionID?: string) => Promise) { + return Promise.all([ + send(), + ...Array.from(sessions, ([frame, id]) => + send(id).catch((error) => { + if (sessions.get(frame) === id) throw error + }), + ), + ]) + } + + async function apply({ viewport, scale }: { viewport: Viewport; scale: number }) { + const device = viewport.mode === "fixed" ? devices.find((device) => device.id === viewport.preset) : undefined + const profile = device?.profile ?? "desktop" + const mobile = profile !== "desktop" + + if (profile !== applied) { + // Frames that attach during these commands must read the incoming profile. + applied = profile + + try { + await Promise.all([onTargets((sessionID) => identity(profile, sessionID)), emitTouch(mobile)]) + } catch (error) { + // A partial write requires an explicit reset even if the next request is Auto. + applied = undefined + throw error + } + } + + if (viewport.mode === "auto") return cdp.send("Emulation.clearDeviceMetricsOverride") + + return cdp.send("Emulation.setDeviceMetricsOverride", { + width: viewport.width, + height: viewport.height, + scale, + mobile, + deviceScaleFactor: 0, + dontSetVisibleSize: true, + ...(mobile && { + screenWidth: viewport.width, + screenHeight: viewport.height, + screenOrientation: + viewport.width > viewport.height + ? { type: "landscapePrimary", angle: 90 } + : { type: "portraitPrimary", angle: 0 }, + }), + } satisfies Protocol.Emulation.SetDeviceMetricsOverrideRequest) + } + + return { + apply, + mobile: () => applied !== undefined && applied !== "desktop", + frame(sessionID: string) { + if (applied && applied !== "desktop") return identity(applied, sessionID) + }, + capture(params: Protocol.Page.CaptureScreenshotRequest) { + // A clipped CDP capture temporarily changes the viewport and drops the renderer's maxTouchPoints override. + return cdp.send("Page.captureScreenshot", params).finally(() => { + if (applied && applied !== "desktop") return onTargets((sessionID) => touch(true, sessionID)) + }) + }, + // Chromium's mouse-to-touch converter consumes synthetic mouse events without acknowledging them to CDP. + // User input gets touch gestures; browser tools retain their explicit mouse buttons, hover and drag semantics. + async mouse(action: () => Promise) { + if (!applied || applied === "desktop") return action() + await emitTouch(false) + + try { + return await action() + } finally { + await emitTouch(true) + } + }, + } +} diff --git a/packages/gui-plugins/src/browser/model.ts b/packages/gui-plugins/src/browser/model.ts index 5b759c73f49..5453610231e 100644 --- a/packages/gui-plugins/src/browser/model.ts +++ b/packages/gui-plugins/src/browser/model.ts @@ -16,7 +16,7 @@ import { import { recordable, remember, withIcon } from "./history" import type definition from "./index" import { workspaceFileURL } from "./link" -import { BrowserPane, type PaneEvent } from "./ipc" +import { BrowserPane, type PaneEvent, type Viewport } from "./ipc" import { addressFocusTab, createInitialState, @@ -39,6 +39,8 @@ import { type Session = Pick +type Binding = { attempt: number; registration: Registration; choices: Map } + /** A session the machine attached: its ref, and the I/O its effects drive. */ type Drive = { ref: SessionRef @@ -49,14 +51,11 @@ type Drive = { type ModelState = { machine: State - /** Each session's page icons and zoom by tab, as this desktop reports them. */ - pages: Record | undefined> + /** Presentation details for the tabs in each desktop binding. */ + pages: Record | undefined> } -/** A page's icon as a data URL, and its zoom factor, 1 at 100%. */ -type PageDetail = { icon?: string; zoom: number } - -/** A mounted pane; the newest one answers the reload and inspect commands. */ +/** A mounted pane. Commands target the newest visible pane. */ type PaneHandle = { visible: () => boolean address: () => string @@ -66,6 +65,7 @@ type PaneHandle = { inspectable: () => boolean /** Turns the element picker on or off. */ inspect: () => void + toggleToolbar: () => void /** Focuses the address field and selects its text. */ focusAddress: () => void /** Runs a key press the page handed to the app, as if it was pressed in this pane. */ @@ -105,9 +105,10 @@ export function createModel(ctx: SetupContext) { const [state, setState] = createStore({ machine: createInitialState(untrack(ipc)), pages: {} }) const machine = () => state.machine const [panes, setPanes] = createSignal([]) + const visiblePane = () => panes().find((handle) => handle.visible()) // The I/O handles the machine's effects drive. They are never read as state. const drives = new Map() - const registrations = new Map() + const registrations = new Map() const registerTimers = new Map>() const retryTimers = new Map>() const servers = new Map void>() @@ -151,6 +152,7 @@ export function createModel(ctx: SetupContext) { if (current?.attempt !== effect.attempt) return registrations.delete(effect.sessionKey) + setState("pages", effect.sessionKey, undefined) current.registration.close() return @@ -239,6 +241,7 @@ export function createModel(ctx: SetupContext) { registrations.set(sessionKey, { attempt, + choices: new Map(), registration: openBinding({ client: current, listen(binding, listener) { @@ -281,8 +284,8 @@ export function createModel(ctx: SetupContext) { // Only the pane on screen holds the page that had focus. if (sessions.current()?.key !== sessionKey) return - if (event.type === "shortcut") return panes()[0]?.press(event) - panes()[0]?.focused() + if (event.type === "shortcut") return visiblePane()?.press(event) + visiblePane()?.focused() } const send = (effect: Extract) => { @@ -324,11 +327,20 @@ export function createModel(ctx: SetupContext) { const keep = effect.keep ? new Set(effect.keep) : undefined batch(() => { - if (keep) + if (keep) { + const choices = registrations.get(effect.sessionKey)?.choices + choices?.forEach((_, id) => { + if (!keep.has(id)) choices.delete(id) + }) + Object.keys(state.pages[effect.sessionKey] ?? {}).forEach((id) => { + if (!keep.has(id)) setState("pages", effect.sessionKey, id, undefined) + }) layout .stored(ref) .filter((tabID) => !keep.has(tabID)) .forEach((tabID) => layout.close(key(tabID), ref)) + } + effect.open.forEach((tabID) => { if (layout.state(key(tabID), ref) === "closed") layout.open(key(tabID), ref, { tab: "append" }) }) @@ -359,10 +371,7 @@ export function createModel(ctx: SetupContext) { } const page = (sessionKey: string, event: PageEvent) => { - setState("pages", sessionKey, (pages) => ({ - ...pages, - [event.tabID]: event.icon ? { icon: event.icon, zoom: event.zoom } : { zoom: event.zoom }, - })) + setState("pages", sessionKey, { [event.tabID]: event }) const url = sessionTab(machine(), sessionKey, event.tabID)?.url const visits = url && event.icon ? withIcon(history.value.visits, url, event.icon) : undefined @@ -536,6 +545,14 @@ export function createModel(ctx: SetupContext) { const registration = (session: Session) => registrations.get(session.key)?.registration + const requireBinding = (session: Session) => { + const current = registrations.get(session.key) + + if (!current) throw new Error("browser.pane.unavailable") + + return current + } + const command = (session: Session, action: Browser.Action) => dispatch({ type: "session.command", sessionKey: session.key, action, focusAddress: false }) @@ -599,11 +616,38 @@ export function createModel(ctx: SetupContext) { return true }, - /** A page's icon and zoom, once this desktop reported them. */ + /** The page's presentation details, once reported by its native tab. */ page: (session: Session, tabID: string) => state.pages[session.key]?.[tabID], zoom(session: Session, tabID: Browser.TabID, zoom: Zoom) { registration(session)?.zoom(tabID, zoom) }, + async setViewport(session: Session, tabID: Browser.TabID, viewport: Viewport) { + const current = requireBinding(session) + current.choices.set(tabID, viewport) + + return current.registration.setViewport(tabID, viewport).catch((error) => { + if (current.choices.get(tabID) === viewport) current.choices.delete(tabID) + throw error + }) + }, + async toggleViewport(session: Session, tabID: Browser.TabID, open: boolean) { + const current = requireBinding(session) + + const page = state.pages[session.key]?.[tabID] + + if (open) { + const choice = current.choices.get(tabID) + + if (choice?.mode !== "fixed" || page?.zoom !== 1) return + + return current.registration.setViewport(tabID, choice) + } + + // A reattached page can already have a device. Keep that choice unless a newer edit is in flight. + if (!current.choices.has(tabID) && page?.viewport.mode === "fixed") current.choices.set(tabID, page.viewport) + + return current.registration.setViewport(tabID, { mode: "auto" }) + }, /** The page's cookie count; undefined while the pane cannot answer. */ site: (session: Session, tabID: Browser.TabID) => registration(session)?.site(tabID), /** Deletes the page's cookies and stored data, and reloads it. */ @@ -683,7 +727,7 @@ export function createModel(ctx: SetupContext) { if (target.ref) registration(session)?.highlight(item.id, target.ref) }, - pane: () => panes()[0], + pane: () => visiblePane() ?? panes()[0], mount(handle: PaneHandle) { setPanes((list) => [handle, ...list]) diff --git a/packages/gui-plugins/src/browser/pane.ts b/packages/gui-plugins/src/browser/pane.ts index 1e25a07d864..c882ecc8982 100644 --- a/packages/gui-plugins/src/browser/pane.ts +++ b/packages/gui-plugins/src/browser/pane.ts @@ -12,7 +12,7 @@ import { browserFailure } from "./errors" import { createBrowserNetwork, type BrowserNetwork } from "./network" import { destinationOrigin, fileURLWithin, refusal } from "./policy" import { createRefs } from "./refs" -import type { PaneEvent } from "./ipc" +import type { PaneEvent, Viewport } from "./ipc" import { attachmentFailure, createPaneState, @@ -68,6 +68,8 @@ export function createBrowserPane(input: { /** The declared store of reserved element refs. */ readonly refs: Persisted readonly embeds: Embeds + /** Whether the build offers the device toolbar. */ + readonly toolbar: boolean readonly emit: (window: number, value: { readonly binding: string; readonly event: PaneEvent }) => void }) { const machine: PaneMachine = { state: createPaneState() } @@ -320,6 +322,13 @@ export function createBrowserPane(input: { owned(window, binding) page(binding, tabID)?.zoom(direction) }, + async setViewport(window: number, binding: string, tabID: Browser.TabID, viewport: Viewport) { + owned(window, binding) + const target = load(binding, tabID) + + if (!target) throw new Error("browser.pane.unavailable") + await target.setViewport(viewport) + }, async site(window: number, binding: string, tabID: Browser.TabID) { owned(window, binding) @@ -520,17 +529,15 @@ export function createBrowserPane(input: { inspect: (event) => { if (current()) emit(binding, { type: "inspect", tabID: id, ...event }) }, - // Icons and zoom are local to this desktop, and follow the states reported before them, so the window files a new - // page's icon under that page's URL. + // The window files the icon under the URL from the preceding inventory update. detail: (value) => { if (current()) emit(binding, { type: "page", tabID: id, ...value }) }, zoomed: () => handles.get(binding)?.pages.forEach((page) => page.detail()), + toolbar: input.toolbar, shortcut: (shortcut) => { if (!focused() || win.isDestroyed()) return - - // The page held keyboard focus; the command runs in the app, which may move focus there, as ⌘L does. - if (win.isFocused()) win.webContents.focus() + page.focusHost() emit(binding, { type: "shortcut", ...shortcut }) }, focused: () => { diff --git a/packages/gui-plugins/src/browser/panel.fixture.tsx b/packages/gui-plugins/src/browser/panel.fixture.tsx index 0c8e26f63b8..2cec1215021 100644 --- a/packages/gui-plugins/src/browser/panel.fixture.tsx +++ b/packages/gui-plugins/src/browser/panel.fixture.tsx @@ -73,6 +73,7 @@ type PaneHost = { type PaneFixtureState = { session: string + secondTab: boolean mounted: boolean visible: boolean url: string | undefined @@ -97,8 +98,8 @@ type PaneFixtureState = { dropInspect: boolean highlights: { sessionKey: string; ref?: Browser.Ref }[] comments: ComposerNote[] - /** The page's zoom as main last reported it. */ - zoom: number + /** Presentation details last reported by each native tab. */ + pages: Record /** The browser history store. */ history: History /** URLs the pane opened in the system browser. */ @@ -127,6 +128,8 @@ type StoryHost = Pick & { }> } +type PageDetails = Pick, "zoom" | "viewport" | "icon"> + // A 16px blue square, as main reports a page icon. const icon = "data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAxNiAxNiI+PHJlY3Qgd2lkdGg9IjE2IiBoZWlnaHQ9IjE2IiByeD0iMyIgZmlsbD0iIzNiODJmNiIvPjwvc3ZnPg==" @@ -179,6 +182,7 @@ export function browserPaneFixture(input: PaneHost | StoryHost) { const [store, setStore] = createStore({ session: "Alpha", + secondTab: false, mounted: true, visible: true, url: scenario === "Empty" || scenario === "Recent" ? "about:blank" : undefined, @@ -199,7 +203,7 @@ export function browserPaneFixture(input: PaneHost | StoryHost) { dropInspect: false, highlights: [], comments: [], - zoom: 1, + pages: {}, history: { visits: scenario === "Recent" ? structuredClone(visits) : [] }, external: [], cookies: 3, @@ -218,8 +222,9 @@ export function browserPaneFixture(input: PaneHost | StoryHost) { if (binding) listeners.forEach((listener) => listener({ binding, event })) } - const tabs = ["Alpha", "Beta"].map((name) => ({ - id: Browser.TabID.make(`tab_${name === "Alpha" ? "11111111" : "22222222"}-1111-1111-1111-111111111111`), + const tabs = ["Alpha", "Beta", "Second"].map((name, index) => ({ + id: Browser.TabID.make(`tab_${String(index + 1).repeat(8)}-1111-1111-1111-111111111111`), + owner: name === "Second" ? "Alpha" : name, title: name, url: `https://${name.toLowerCase()}.example/`, loading: false, @@ -228,7 +233,24 @@ export function browserPaneFixture(input: PaneHost | StoryHost) { generation: 0, })) - const current = () => tabs.find((tab) => tab.title === store.session) ?? tabs[0] + const current = (key = store.session) => + tabs.find((tab) => tab.title === (key === "Alpha" && store.secondTab ? "Second" : key)) ?? tabs[0] + + const target = (value: { binding: string; tabID: Browser.TabID }) => { + const tab = tabs.find((item) => item.id === value.tabID && bindings.get(item.owner) === value.binding) + + if (!tab) throw new Error("Unknown browser tab") + + return tab + } + + const details = (tabID: Browser.TabID): PageDetails => store.pages[tabID] ?? { zoom: 1, viewport: { mode: "auto" } } + + const page = (tab: (typeof tabs)[number], update: Partial = {}, notice?: "zoom") => { + const next = { ...details(tab.id), ...update } + setStore("pages", (pages) => ({ ...pages, [tab.id]: next })) + emit(tab.owner, { type: "page", tabID: tab.id, ...next, ...(notice && { viewportNotice: notice }) }) + } // Main's window focus reports, which focus moving into an embed does not end. const windowFocus = new Set[0]>() @@ -274,7 +296,7 @@ export function browserPaneFixture(input: PaneHost | StoryHost) { @@ -341,7 +363,13 @@ export function browserPaneFixture(input: PaneHost | StoryHost) { }), ) - const session = () => views.get(store.session) ?? views.get("Alpha") + const session = () => { + const view = views.get(store.session) + + if (!view) throw new Error("Unknown fixture session") + + return view + } // The session screen: one object that follows the route, with the composer the pane attaches comments to. // SAFETY: the pane reads only file.search and composer.attach; the model's workspace-link paths are not invoked. @@ -355,46 +383,52 @@ export function browserPaneFixture(input: PaneHost | StoryHost) { const forward: string[] = [] const report = (key = store.session) => { - const tab = tabs.find((item) => item.title === key) + const inventory = tabs.filter((item) => item.owner === key) - if (!tab) return + if (!inventory.length) return + + const tab = current(key) emit(key, { type: "state", state: { - tabs: [ - { - ...tab, - url: store.url ?? tab.url, - canGoBack: back.length > 0, - canGoForward: forward.length > 0, - loading: store.loading, - generation: store.generation, - loadError: store.loadErrors[key], - }, - ], + tabs: inventory.map((item) => + item === tab + ? { + ...item, + url: store.url ?? item.url, + canGoBack: back.length > 0, + canGoForward: forward.length > 0, + loading: store.loading, + generation: store.generation, + loadError: store.loadErrors[key], + } + : item, + ), focusedTabID: tab.id, }, error: store.error, }) + + return tab } + const embed = (tab: (typeof tabs)[number]) => + emit(tab.owner, { type: "embed", tabID: tab.id, embed: `embed-${tab.title}` }) + // Address rejection counters and cleanup run through the real model over this main-process Ipc boundary. const pane: PaneClient = { register: async (value) => { bindings.set(value.session, value.binding) queueMicrotask(() => { - report(value.session) + const tab = report(value.session) // A suspended attachment reports its retained inventory once. The next interaction can reconnect it. - if (store.error === "browser.pane.suspended") { - setStore("error", undefined) + if (store.error === "browser.pane.suspended") return setStore("error", undefined) - return + if (tab) { + embed(tab) + page(tab) } - - const tab = tabs.find((item) => item.title === value.session) - - if (tab) emit(value.session, { type: "embed", tabID: tab.id, embed: `embed-${tab.title}` }) }) }, load: async () => undefined, @@ -456,12 +490,20 @@ export function browserPaneFixture(input: PaneHost | StoryHost) { }, // Main steps through Chromium's presets and reports the page's new zoom. zoom: async (value) => { + const tab = target(value) + + const previous = details(tab.id) + const steps = [0.9, 1, 1.1, 1.25] - const index = steps.indexOf(store.zoom) - const next = value.zoom === "reset" ? 1 : (steps[index + (value.zoom === "in" ? 1 : -1)] ?? store.zoom) - setStore("zoom", next) - emit(store.session, { type: "page", tabID: value.tabID, zoom: next }) + + const index = steps.indexOf(previous.zoom) + + const zoom = value.zoom === "reset" ? 1 : (steps[index + (value.zoom === "in" ? 1 : -1)] ?? previous.zoom) + + if (zoom === previous.zoom) return + page(tab, { zoom, viewport: { mode: "auto" } }, previous.viewport.mode === "fixed" ? "zoom" : undefined) }, + setViewport: async (value) => page(target(value), { viewport: value.viewport }), site: async () => { if (store.failSite) { setStore("failSite", false) @@ -490,6 +532,7 @@ export function browserPaneFixture(input: PaneHost | StoryHost) { const fake = { ...base, + app: { name: "opencode", version: "", channel: "dev", platform: "desktop", packaged: false }, use: () => live, sessions: { current: session, list: () => Array.from(views.values()).filter((view) => view.key !== "Empty") }, screen: { current: () => screen }, @@ -562,9 +605,7 @@ export function browserPaneFixture(input: PaneHost | StoryHost) { comments={store.comments} external={store.external.at(-1)} > - - {(view) => } - + @@ -577,6 +618,17 @@ export function browserPaneFixture(input: PaneHost | StoryHost) { Menus and suggestions portal into , under this fixture's fixed host unless raised above it. */}

Browser pane lifecycle

+

Captures: {store.captures}

-

Zoom: {Math.round(store.zoom * 100)}

+

Zoom: {Math.round(details(current().id).zoom * 100)}

Visits: {store.history.visits.map((visit) => visit.url).join(" ")}

External: {store.external.join(" ")}

Session getter reads: {reads.directory}

Picker: {store.picker[store.session] ? "on" : "off"}

- + tab.title !== "Second")}> {(tab) => (

Picker {tab.title}: {store.picker[tab.title] ? "on" : "off"} @@ -683,8 +733,8 @@ export function browserPaneFixture(input: PaneHost | StoryHost) {

Highlights: {store.highlights.map((item) => item.ref ?? "clear").join(",")}

Highlight owners: {store.highlights.map((item) => item.sessionKey).join(",")}

- - {(view) => current()} session={view()} screen={screen} model={model} />} + + - {(tab) => ( -
- {tab.title}: {store.layouts[`embed-${tab.title}`]?.visible ? "visible" : "hidden"} -
- )} + {(tab) => { + const layout = () => store.layouts[`embed-${tab.title}`] + + return ( +
+ {tab.title}: {layout()?.visible ? "visible" : "hidden"} +
+ ) + }}
@@ -1086,6 +1144,7 @@ export function mountBrowserRegion(input: RegionHost) { }, highlight: async () => undefined, zoom: async () => undefined, + setViewport: async () => undefined, site: async () => ({ cookies: 0 }), clearSite: async () => undefined, close: async () => undefined, @@ -1151,6 +1210,7 @@ export function mountBrowserRegion(input: RegionHost) { alt: event.altKey, }, mac, + true, ) const binding = latest()?.binding diff --git a/packages/gui-plugins/src/browser/panel.tsx b/packages/gui-plugins/src/browser/panel.tsx index 8e9b1b6d0b2..b48a4aa9f9c 100644 --- a/packages/gui-plugins/src/browser/panel.tsx +++ b/packages/gui-plugins/src/browser/panel.tsx @@ -12,7 +12,20 @@ import { showToast } from "@opencode/ui/toast" import { Tooltip } from "@opencode/ui/tooltip" import { createEventListener } from "@solid-primitives/event-listener" import { createResizeObserver } from "@solid-primitives/resize-observer" -import { createMemo, createSignal, For, onCleanup, Show, untrack, type Accessor, type JSX } from "solid-js" +import { debounce } from "@solid-primitives/scheduled" +import { + createMemo, + createSignal, + createUniqueId, + For, + onCleanup, + onMount, + Show, + untrack, + type Accessor, + type JSX, + type ParentProps, +} from "solid-js" import type { Browser } from "@opencode/plugin-browser/rpc" import { createKeyed, @@ -47,6 +60,16 @@ import { type OverlayEvent, } from "./overlay-machine" import { PageIcon } from "./page-icon" +import { ViewportControls } from "./viewport-controls" +import { + initialViewportToolbar, + transitionViewportToolbar, + viewportToolbarAvailable, + viewportToolbarView, + type ViewportToolbarEvent, +} from "./viewport-toolbar-machine" +import { viewportLayout } from "./viewport-layout" +import { ViewportHandles } from "./viewport-handles" import { initialSiteState, SITE_CLEAR_TIMEOUT_MS, @@ -85,7 +108,10 @@ export default function SessionBrowserPane(props: { } const [addressState, setAddressState] = createSignal(initialAddressState) - const [size, setSize] = createSignal({ width: 0, height: 0 }) + const [available, setAvailable] = createSignal({ width: 0, height: 0 }) + const [toolbar, setToolbar] = createSignal(initialViewportToolbar) + const [pointerFocusedToggle, setPointerFocusedToggle] = createSignal(false) + const viewportID = createUniqueId() const [overlay, setOverlay] = createSignal(initialOverlayState) // The page's reports, counted; a submitted address in the field ends at the next one. @@ -128,13 +154,69 @@ export default function SessionBrowserPane(props: { } let root: HTMLElement | undefined - let box: HTMLDivElement | undefined + let toolbarToggle: HTMLButtonElement | undefined let input: HTMLInputElement | undefined let anchor: HTMLFormElement | undefined const error = () => props.model.error(props.session) const inspectable = () => !!address() && !failed() && !suspended() + const windowZoom = () => desktop?.zoom() ?? 1 + + const fixedViewport = () => { + const viewport = page()?.viewport + + return inspectable() && viewport?.mode === "fixed" ? viewport : undefined + } + + const fit = createMemo(() => { + const viewport = fixedViewport() + const area = available() + + if (!viewport || area.width <= 0 || area.height <= 0) return + + return viewportLayout(viewport, area, windowZoom()) + }) + + const size = () => fit() ?? available() + const tabID = createMemo(() => state()?.id) + + // A new visit to the same tab must not receive the old visit's failure toast. + const toolbarTarget = createMemo(() => { + const id = tabID() + + return id ? { sessionKey: props.session.key, tabID: id } : undefined + }) + + const toolbarContext = () => ({ target: toolbarTarget(), available: visible() && inspectable(), page: page() }) + const toolbarView = createMemo(() => viewportToolbarView(toolbar(), toolbarContext())) + const toolbarVisible = () => toolbarView().visible + const notice = () => toolbarView().notice + + const toggleTimeout = debounce( + (attempt: number) => dispatchToolbar({ type: "timeout", attempt, context: toolbarContext() }), + 10_000, + ) + + const dispatchToolbar = (event: ViewportToolbarEvent) => { + const next = transitionViewportToolbar(untrack(toolbar), event) + setToolbar(next.state) + const effect = next.effect + + if (!effect) return + + if (effect.type === "notify-failed") return showToast({ title: plugin.t("viewport.error") }) + const { attempt, target, open } = effect + toggleTimeout(attempt) + void props.model.toggleViewport({ key: target.sessionKey }, target.tabID, open).then( + () => dispatchToolbar({ type: "succeeded", attempt }), + () => dispatchToolbar({ type: "failed", attempt, context: toolbarContext() }), + ) + } + + const toggleToolbar = () => dispatchToolbar({ type: "toggled", context: toolbarContext() }) + onCleanup(() => dispatchToolbar({ type: "disposed" })) + const overlayed = createMemo(() => overlayView(overlay(), props.session.key, state()?.id)) const picking = () => overlayed().picking const comment = createMemo(() => overlayComment(overlay(), props.session.key, state()?.id)) @@ -225,6 +307,22 @@ export default function SessionBrowserPane(props: { const zoomed = () => !!address() && (page()?.zoom ?? 1) !== 1 + // Keep the grip gutters in the measured layout while an annotation freezes the page. + const resizable = () => toolbarVisible() && !zoomed() && embed() + + const dimensions = () => ({ + width: Math.round((available().width * windowZoom()) / (page()?.zoom ?? 1)), + height: Math.round((available().height * windowZoom()) / (page()?.zoom ?? 1)), + }) + + const viewportMessage = () => { + if (notice() === "failed") return plugin.t("viewport.fitFailed") + + if (notice() === "zoom") return plugin.t("viewport.zoomExit") + + if (zoomed()) return plugin.t("viewport.zoomPrompt") + } + const zoomPage = (direction: "in" | "out" | "reset") => { const tab = state() @@ -241,38 +339,77 @@ export default function SessionBrowserPane(props: { ) } - // The picked element in surface pixels, and the editor anchored below it, above it, or over it. - const spotlight = () => { - const rect = comment()?.element.rect + // Snapshot the surface in native DIPs at pick time so the spotlight scales with the still. + const pickedRect = createMemo(() => comment()?.element.rect) + + const capture = createMemo(() => { + const rect = pickedRect() if (!rect) return - const zoom = desktop?.zoom() ?? 1 + const surface = untrack(size) + const zoom = untrack(windowZoom) - return { x: rect.x / zoom, y: rect.y / zoom, width: rect.width / zoom, height: rect.height / zoom } + return { rect, width: surface.width * zoom, height: surface.height * zoom } + }) + + const spotlight = () => { + const original = capture() + + if (!original?.width || !original.height) return + const rect = original.rect + const x = size().width / original.width + const y = size().height / original.height + + return { x: rect.x * x, y: rect.y * y, width: rect.width * x, height: rect.height * y } + } + + let preview: HTMLDivElement | undefined + let backdrop: HTMLDivElement | undefined + const [origin, setOrigin] = createSignal({ x: 0, y: 0 }) + + // The spotlight scrolls with the still. Translate its page coordinates to the visible pane for the editor. + const trackPage = (surface: HTMLDivElement) => { + const port = preview + + if (!port) return + + const measure = () => { + if (!backdrop) return + const page = surface.getBoundingClientRect() + const pane = backdrop.getBoundingClientRect() + + setOrigin({ x: page.left - pane.left, y: page.top - pane.top }) + } + + onMount(measure) + createResizeObserver([surface, port], measure) + createEventListener(port, "scroll", measure, { passive: true }) } const placement = () => { const rect = spotlight() if (!rect) return + const pane = available() const gap = 8 - const width = Math.max(0, Math.min(320, size().width - gap * 2)) + const width = Math.max(0, Math.min(320, pane.width - gap * 2)) // The editor scrolls rather than growing past the surface, so its actions stay reachable. - const surfaceHeight = Math.max(0, size().height - gap * 2) + const surfaceHeight = Math.max(0, pane.height - gap * 2) // Pick the opening side from its first measurement, then grow upward from that bottom edge. const initialHeight = Math.min(comment()?.editorInitialHeight || 80, surfaceHeight) - const left = Math.min(Math.max(gap, rect.x), Math.max(gap, size().width - width - gap)) - const below = rect.y + rect.height + gap - const above = rect.y - gap - initialHeight + const y = origin().y + rect.y + const left = Math.min(Math.max(gap, origin().x + rect.x), Math.max(gap, pane.width - width - gap)) + const below = Math.max(gap, y + rect.height + gap) + const above = y - gap - initialHeight const openingTop = - below + initialHeight <= size().height - gap + below + initialHeight <= pane.height - gap ? below : above >= gap ? above - : Math.max(gap, size().height - initialHeight - gap) + : Math.max(gap, pane.height - initialHeight - gap) - const bottom = Math.min(openingTop + initialHeight, size().height - gap) + const bottom = Math.min(openingTop + initialHeight, pane.height - gap) const maxHeight = Math.max(0, bottom - gap) const height = Math.min(comment()?.editorHeight || initialHeight, maxHeight) const top = Math.max(gap, bottom - height) @@ -296,6 +433,11 @@ export default function SessionBrowserPane(props: { }, inspectable, inspect: toggleInspect, + toggleToolbar: () => { + toggleToolbar() + setPointerFocusedToggle(false) + toolbarToggle?.focus({ preventScroll: true, focusVisible: true }) + }, focusAddress, // Dispatched inside the pane, so keybinds scoped to it, such as closing the browser tab, apply. press: (event) => { @@ -371,9 +513,50 @@ export default function SessionBrowserPane(props: { }, { capture: true }, ) + createResizeObserver( - () => box, - (rect) => setSize({ width: rect.width, height: rect.height }), + () => preview, + (rect) => setAvailable({ width: rect.width, height: rect.height }), + ) + + // The outer box anchors the editors while the preview grid scrolls underneath. + const Preview = (view: ParentProps) => ( +
+
+ {view.children} + + {(id) => ( + { + const tab = state() + + if (!tab || embed() !== id) return + void props.model + .setViewport(props.session, tab.id, { mode: "fixed", width, height, scale: fixedViewport()?.scale }) + .catch(() => { + if (embed() === id && !page()?.viewportNotice) showToast({ title: plugin.t("viewport.error") }) + }) + }} + /> + )} + +
+
) // A restored tab has no page until the pane first shows it. @@ -626,6 +809,7 @@ export default function SessionBrowserPane(props: { event.preventDefault()} onCloseAutoFocus={(event) => event.preventDefault()} > @@ -660,6 +844,36 @@ export default function SessionBrowserPane(props: { icon={} /> + + + setPointerFocusedToggle(true)} + onBlur={() => setPointerFocusedToggle(false)} + onKeyDown={() => setPointerFocusedToggle(false)} + aria-label={plugin.t("command.toggleToolbar")} + aria-expanded={toolbarVisible()} + aria-controls={toolbarVisible() ? viewportID : undefined} + disabled={!inspectable()} + state={toolbarVisible() ? "pressed" : undefined} + onClick={toggleToolbar} + icon={ + + } + /> + + - + {/* Zooming closes the menu: the page stays a still while the menu covers it, so a zoom would show only once it closed. The address field shows any zoom but 100% afterwards. */} @@ -719,89 +933,139 @@ export default function SessionBrowserPane(props: { {error()}
- -