mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-20 18:57:17 +03:00
feat(desktop): browser toolbar with mobile and custom viewport sizes (#63819)
This commit is contained in:
committed by
GitHub
Unverified
parent
0d901e2105
commit
1cb0633c8b
@@ -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")
|
||||
|
||||
@@ -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()
|
||||
})
|
||||
|
||||
@@ -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<HTMLElement>("[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
|
||||
|
||||
@@ -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<typeof definition>
|
||||
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
|
||||
|
||||
@@ -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",
|
||||
})
|
||||
}
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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 }))),
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
<svg preserveAspectRatio="none" overflow="visible" style="display: block;" width="16" height="16" viewBox="0 0 16 16" fill="none" xmlns="http://www.w3.org/2000/svg">
|
||||
<g id="outline-chevron-down">
|
||||
<path id="Vector" d="M5 6.5L8 9.5L11 6.5" stroke="#3A3A3A"/>
|
||||
</g>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 269 B |
@@ -0,0 +1,7 @@
|
||||
<svg preserveAspectRatio="none" overflow="visible" style="display: block;" width="11" height="13.8975" viewBox="0 0 11 13.8975" fill="none" xmlns="http://www.w3.org/2000/svg">
|
||||
<g id="Group">
|
||||
<path id="Vector" d="M6.5 7.39746V13.3975H0.5L0.5 7.39746H6.5Z" stroke-miterlimit="10" stroke-linecap="square" stroke="#808080"/>
|
||||
<path id="Vector_2" d="M10.5 8.33374V2.39746H4.16667H3.5" stroke-linecap="square" stroke="#808080"/>
|
||||
<path id="Vector_3" d="M4.69801 0.707107L3.00768 2.39747L4.69801 4.08782" stroke-linecap="square" stroke="#808080"/>
|
||||
</g>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 551 B |
@@ -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;
|
||||
|
||||
@@ -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<Extract<PaneEvent, { type: "page" }>, "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<string, string>()
|
||||
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<string, string>()
|
||||
const sourceURLs = () => [...new Set([contents.getURL(), ...documents.values()])].sort()
|
||||
const files = createBrowserFiles(sourceURLs)
|
||||
@@ -313,7 +365,6 @@ export function createBrowserPage(
|
||||
const picked = new Map<string, Element>()
|
||||
let pickerState = initialPickerState
|
||||
let pickerTimer: ReturnType<typeof setTimeout> | undefined
|
||||
const sessions = new Map<string, string>()
|
||||
const parents = new Map<string, string>()
|
||||
const contexts = new Map<string, { id: number; sessionID?: string }>()
|
||||
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<Browser.Result> {
|
||||
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<Browser.Result> {
|
||||
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 }
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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<string, (event: PaneEvent) => 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) => {
|
||||
|
||||
@@ -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<void>
|
||||
site(tabID: Browser.TabID): Promise<{ cookies: number }>
|
||||
clearSite(tabID: Browser.TabID): Promise<void>
|
||||
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() {
|
||||
|
||||
@@ -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",
|
||||
}
|
||||
|
||||
@@ -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: {
|
||||
|
||||
@@ -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],
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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),
|
||||
|
||||
@@ -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<string, string>) {
|
||||
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<T>(send: (sessionID?: string) => Promise<T>) {
|
||||
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<T>(action: () => Promise<T>) {
|
||||
if (!applied || applied === "desktop") return action()
|
||||
await emitTouch(false)
|
||||
|
||||
try {
|
||||
return await action()
|
||||
} finally {
|
||||
await emitTouch(true)
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -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<SessionRef, "key">
|
||||
|
||||
type Binding = { attempt: number; registration: Registration; choices: Map<Browser.TabID, Viewport> }
|
||||
|
||||
/** 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<string, Record<string, PageDetail | undefined> | undefined>
|
||||
/** Presentation details for the tabs in each desktop binding. */
|
||||
pages: Record<string, Record<string, PageEvent | undefined> | 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<typeof definition>) {
|
||||
const [state, setState] = createStore<ModelState>({ machine: createInitialState(untrack(ipc)), pages: {} })
|
||||
const machine = () => state.machine
|
||||
const [panes, setPanes] = createSignal<readonly PaneHandle[]>([])
|
||||
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<string, Drive>()
|
||||
const registrations = new Map<string, { attempt: number; registration: Registration }>()
|
||||
const registrations = new Map<string, Binding>()
|
||||
const registerTimers = new Map<string, ReturnType<typeof setTimeout>>()
|
||||
const retryTimers = new Map<string, ReturnType<typeof setTimeout>>()
|
||||
const servers = new Map<string, () => void>()
|
||||
@@ -151,6 +152,7 @@ export function createModel(ctx: SetupContext<typeof definition>) {
|
||||
|
||||
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<typeof definition>) {
|
||||
|
||||
registrations.set(sessionKey, {
|
||||
attempt,
|
||||
choices: new Map(),
|
||||
registration: openBinding({
|
||||
client: current,
|
||||
listen(binding, listener) {
|
||||
@@ -281,8 +284,8 @@ export function createModel(ctx: SetupContext<typeof definition>) {
|
||||
// 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<MachineEffect, { type: "send-command" }>) => {
|
||||
@@ -324,11 +327,20 @@ export function createModel(ctx: SetupContext<typeof definition>) {
|
||||
const keep = effect.keep ? new Set<string>(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<typeof definition>) {
|
||||
}
|
||||
|
||||
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<typeof definition>) {
|
||||
|
||||
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<typeof definition>) {
|
||||
|
||||
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<typeof definition>) {
|
||||
|
||||
if (target.ref) registration(session)?.highlight(item.id, target.ref)
|
||||
},
|
||||
pane: () => panes()[0],
|
||||
pane: () => visiblePane() ?? panes()[0],
|
||||
mount(handle: PaneHandle) {
|
||||
setPanes((list) => [handle, ...list])
|
||||
|
||||
|
||||
@@ -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<number, number>
|
||||
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: () => {
|
||||
|
||||
@@ -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<string, PageDetails | undefined>
|
||||
/** The browser history store. */
|
||||
history: History
|
||||
/** URLs the pane opened in the system browser. */
|
||||
@@ -127,6 +128,8 @@ type StoryHost = Pick<PaneHost, "useLanguage"> & {
|
||||
}>
|
||||
}
|
||||
|
||||
type PageDetails = Pick<Extract<PaneEvent, { type: "page" }>, "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<PaneFixtureState>({
|
||||
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<PageDetails> = {}, 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<Parameters<Bridge["on"]>[0]>()
|
||||
@@ -274,7 +296,7 @@ export function browserPaneFixture(input: PaneHost | StoryHost) {
|
||||
<Show when={props.id && props.visible && !store.loading && !model.suspended({ key: store.session })}>
|
||||
<input.Page
|
||||
url={store.url ?? current().url}
|
||||
zoom={store.zoom}
|
||||
zoom={details(current().id).zoom}
|
||||
picking={!!store.picker[store.session]}
|
||||
pick={pick}
|
||||
/>
|
||||
@@ -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)}
|
||||
>
|
||||
<Show when={session()}>
|
||||
{(view) => <SessionBrowserPane tab={current} session={view()} screen={screen} model={model} />}
|
||||
</Show>
|
||||
<SessionBrowserPane tab={current} session={session()} screen={screen} model={model} />
|
||||
</input.Frame>
|
||||
</PanelContext.Provider>
|
||||
</PluginContext.Provider>
|
||||
@@ -577,6 +618,17 @@ export function browserPaneFixture(input: PaneHost | StoryHost) {
|
||||
Menus and suggestions portal into <body>, under this fixture's fixed host unless raised above it. */}
|
||||
<style>{`${barStyles}\n[data-popper-positioner] { z-index: 1001 !important; }`}</style>
|
||||
<h1 style={{ "font-size": "24px", "margin-bottom": "16px" }}>Browser pane lifecycle</h1>
|
||||
<button
|
||||
style={{ position: "absolute", top: "24px", "inset-inline-end": "24px" }}
|
||||
disabled={store.session !== "Alpha"}
|
||||
onClick={() => {
|
||||
setStore("secondTab", (selected) => !selected)
|
||||
report()
|
||||
embed(current())
|
||||
}}
|
||||
>
|
||||
Switch browser tab
|
||||
</button>
|
||||
<nav style={{ display: "flex", "flex-wrap": "wrap", gap: "12px", margin: "16px 0" }}>
|
||||
<For each={["Alpha", "Beta", "Empty"]}>
|
||||
{(name) => <button onClick={() => setStore({ session: name, mounted: name !== "Empty" })}>{name}</button>}
|
||||
@@ -663,17 +715,15 @@ export function browserPaneFixture(input: PaneHost | StoryHost) {
|
||||
</select>
|
||||
<button onClick={() => setStore("history", { visits })}>Seed history</button>
|
||||
<button onClick={() => setStore("failSite", true)}>Fail next site read</button>
|
||||
<button onClick={() => emit(store.session, { type: "page", tabID: current().id, icon, zoom: store.zoom })}>
|
||||
Page icon
|
||||
</button>
|
||||
<button onClick={() => page(current(), { icon })}>Page icon</button>
|
||||
</nav>
|
||||
<p>Captures: {store.captures}</p>
|
||||
<p>Zoom: {Math.round(store.zoom * 100)}</p>
|
||||
<p>Zoom: {Math.round(details(current().id).zoom * 100)}</p>
|
||||
<p>Visits: {store.history.visits.map((visit) => visit.url).join(" ")}</p>
|
||||
<p>External: {store.external.join(" ")}</p>
|
||||
<p>Session getter reads: {reads.directory}</p>
|
||||
<p>Picker: {store.picker[store.session] ? "on" : "off"}</p>
|
||||
<For each={tabs}>
|
||||
<For each={tabs.filter((tab) => tab.title !== "Second")}>
|
||||
{(tab) => (
|
||||
<p>
|
||||
Picker {tab.title}: {store.picker[tab.title] ? "on" : "off"}
|
||||
@@ -683,8 +733,8 @@ export function browserPaneFixture(input: PaneHost | StoryHost) {
|
||||
<p>Highlights: {store.highlights.map((item) => item.ref ?? "clear").join(",")}</p>
|
||||
<p>Highlight owners: {store.highlights.map((item) => item.sessionKey).join(",")}</p>
|
||||
<div style={{ position: "relative", width: `${store.width}px`, height: "360px", border: "1px solid #555" }}>
|
||||
<Show when={store.mounted && session()}>
|
||||
{(view) => <SessionBrowserPane tab={() => current()} session={view()} screen={screen} model={model} />}
|
||||
<Show when={store.mounted}>
|
||||
<SessionBrowserPane tab={current} session={session()} screen={screen} model={model} />
|
||||
</Show>
|
||||
<ResizeHandle
|
||||
data-testid="pane-resize-handle"
|
||||
@@ -725,15 +775,23 @@ export function browserPaneFixture(input: PaneHost | StoryHost) {
|
||||
</Portal>
|
||||
</Show>
|
||||
<For each={tabs}>
|
||||
{(tab) => (
|
||||
<div
|
||||
data-testid={`native-${tab.title}`}
|
||||
data-visible={!!store.layouts[`embed-${tab.title}`]?.visible}
|
||||
style={{ padding: "12px", margin: "8px 0", border: "1px solid #555" }}
|
||||
>
|
||||
{tab.title}: {store.layouts[`embed-${tab.title}`]?.visible ? "visible" : "hidden"}
|
||||
</div>
|
||||
)}
|
||||
{(tab) => {
|
||||
const layout = () => store.layouts[`embed-${tab.title}`]
|
||||
|
||||
return (
|
||||
<div
|
||||
data-testid={`native-${tab.title}`}
|
||||
data-visible={!!layout()?.visible}
|
||||
data-x={layout()?.bounds?.x}
|
||||
data-y={layout()?.bounds?.y}
|
||||
data-width={layout()?.bounds?.width}
|
||||
data-height={layout()?.bounds?.height}
|
||||
style={{ padding: "12px", margin: "8px 0", border: "1px solid #555" }}
|
||||
>
|
||||
{tab.title}: {layout()?.visible ? "visible" : "hidden"}
|
||||
</div>
|
||||
)
|
||||
}}
|
||||
</For>
|
||||
</PanelContext.Provider>
|
||||
</PluginContext.Provider>
|
||||
@@ -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
|
||||
|
||||
@@ -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) => (
|
||||
<div class="relative flex min-h-0 flex-1 flex-col">
|
||||
<div
|
||||
ref={preview}
|
||||
data-component="browser-preview"
|
||||
data-fixed={fixedViewport() ? "" : undefined}
|
||||
data-resizable={resizable() ? "" : undefined}
|
||||
data-manual={fixedViewport()?.scale ? "" : undefined}
|
||||
data-embed-clip={fixedViewport()?.scale ? "" : undefined}
|
||||
style={{
|
||||
"grid-template-columns": fit() ? `${size().width}px` : "minmax(0, 1fr)",
|
||||
"grid-template-rows": fit() ? `${size().height}px` : "minmax(0, 1fr)",
|
||||
}}
|
||||
>
|
||||
{view.children}
|
||||
<Show when={resizable()} keyed>
|
||||
{(id) => (
|
||||
<ViewportHandles
|
||||
disabled={overlayed().frozen}
|
||||
size={fixedViewport() ?? dimensions()}
|
||||
ratio={windowZoom() / (fit()?.scale ?? 1)}
|
||||
centered={{ width: size().width <= available().width, height: size().height <= available().height }}
|
||||
resize={({ width, height }) => {
|
||||
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") })
|
||||
})
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
</Show>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
|
||||
// A restored tab has no page until the pane first shows it.
|
||||
@@ -626,6 +809,7 @@ export default function SessionBrowserPane(props: {
|
||||
<Popover.Portal>
|
||||
<Popover.Content
|
||||
data-component="browser-suggestions"
|
||||
data-browser-popup
|
||||
onOpenAutoFocus={(event) => event.preventDefault()}
|
||||
onCloseAutoFocus={(event) => event.preventDefault()}
|
||||
>
|
||||
@@ -660,6 +844,36 @@ export default function SessionBrowserPane(props: {
|
||||
icon={<Icon name="outline-browser-annotate" />}
|
||||
/>
|
||||
</Tooltip>
|
||||
<Show when={viewportToolbarAvailable(plugin.app.channel)}>
|
||||
<Tooltip
|
||||
placement="top"
|
||||
value={tip(plugin.t("command.toggleToolbar"), keybinds.keybind("browser.toggleToolbar"))}
|
||||
>
|
||||
<IconButton
|
||||
ref={toolbarToggle}
|
||||
variant="ghost-muted"
|
||||
size="large"
|
||||
classList={{ "!outline-none": pointerFocusedToggle() }}
|
||||
onPointerDown={() => 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={
|
||||
<svg width="16" height="16" viewBox="0 0 16 16" fill="none" aria-hidden="true">
|
||||
<path
|
||||
d="M1 12.5H8M13.5 4V2.5H3.49997V12M14.5 5.5H9.5C9.5 8.0362 9.5 10.9638 9.5 13.5H14.5C14.5 10.9638 14.5 8.0362 14.5 5.5Z"
|
||||
stroke="currentColor"
|
||||
/>
|
||||
</svg>
|
||||
}
|
||||
/>
|
||||
</Tooltip>
|
||||
</Show>
|
||||
<Menu gutter={4} modal={false} placement="bottom-end">
|
||||
<Tooltip placement="top" value={plugin.t("options")}>
|
||||
<Menu.Trigger
|
||||
@@ -672,7 +886,7 @@ export default function SessionBrowserPane(props: {
|
||||
/>
|
||||
</Tooltip>
|
||||
<Menu.Portal>
|
||||
<Menu.Content class="w-[172px]">
|
||||
<Menu.Content class="w-[172px]" data-browser-popup>
|
||||
{/* 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. */}
|
||||
<Menu.Group>
|
||||
@@ -719,89 +933,139 @@ export default function SessionBrowserPane(props: {
|
||||
{error()}
|
||||
</div>
|
||||
</Show>
|
||||
<embeds.View
|
||||
id={embed()}
|
||||
visible={shown()}
|
||||
frozen={overlayed().frozen}
|
||||
// A navigation, in-page or not, or a reload starting is another page.
|
||||
page={JSON.stringify([state()?.id, state()?.generation, state()?.url, !!state()?.loading])}
|
||||
radius={10}
|
||||
class="relative min-h-0 flex-1 bg-v2-background-bg-base flex items-center justify-center"
|
||||
>
|
||||
<div ref={box} aria-hidden="true" class="pointer-events-none absolute inset-0" />
|
||||
<Show when={empty() && !failed() && !suspended()}>
|
||||
<div dir="auto" class="size-full overflow-y-auto">
|
||||
<Show
|
||||
when={recent().length > 0}
|
||||
fallback={
|
||||
<div class="mx-auto flex max-w-[360px] flex-col items-center gap-4 px-6 pt-20 text-center">
|
||||
<Icon name="outline-globe" size="large" class="text-v2-icon-icon-muted" />
|
||||
<div class="flex flex-col gap-2">
|
||||
<div class="text-[13px] font-[530] leading-[var(--line-height-compact)] text-v2-text-text-base">
|
||||
{plugin.t("home.title")}
|
||||
</div>
|
||||
<div class="text-[13px] font-[440] leading-[var(--line-height-compact)] text-v2-text-text-muted">
|
||||
{plugin.t("home.description")}
|
||||
<Show when={toolbarVisible()}>
|
||||
<section id={viewportID} data-component="browser-viewport" aria-label={plugin.t("viewport.label")}>
|
||||
<Show when={state()?.id} keyed>
|
||||
{(tabID) => (
|
||||
<Show when={props.session.key} keyed>
|
||||
<ViewportControls
|
||||
viewport={page()?.viewport}
|
||||
autoWidth={dimensions().width}
|
||||
autoHeight={dimensions().height}
|
||||
effectiveScale={fit()?.scale ?? 1}
|
||||
zoomed={zoomed()}
|
||||
apply={(viewport) => props.model.setViewport(props.session, tabID, viewport)}
|
||||
/>
|
||||
</Show>
|
||||
)}
|
||||
</Show>
|
||||
<Show when={viewportMessage()}>
|
||||
<div data-slot="browser-viewport-message" role="status">
|
||||
{viewportMessage()}
|
||||
<Show when={zoomed()}>
|
||||
<Button size="small" variant="ghost" onClick={() => zoomPage("reset")}>
|
||||
{plugin.t("zoom.reset")}
|
||||
</Button>
|
||||
</Show>
|
||||
<Show when={notice()}>
|
||||
<Button
|
||||
size="small"
|
||||
variant="ghost-muted"
|
||||
onClick={() => dispatchToolbar({ type: "notice.dismissed", context: toolbarContext() })}
|
||||
>
|
||||
{plugin.t("common.dismiss")}
|
||||
</Button>
|
||||
</Show>
|
||||
</div>
|
||||
</Show>
|
||||
</section>
|
||||
</Show>
|
||||
<Preview>
|
||||
<embeds.View
|
||||
id={embed()}
|
||||
visible={shown() && (!fixedViewport() || !!fit())}
|
||||
frozen={overlayed().frozen}
|
||||
// A navigation, reload or layout viewport change invalidates stills of the previous page.
|
||||
page={JSON.stringify([
|
||||
state()?.id,
|
||||
state()?.generation,
|
||||
state()?.url,
|
||||
!!state()?.loading,
|
||||
fixedViewport()?.width,
|
||||
fixedViewport()?.height,
|
||||
fixedViewport()?.scale,
|
||||
])}
|
||||
radius={fixedViewport() ? 0 : 10}
|
||||
class="relative min-h-0 min-w-0 overflow-hidden bg-v2-background-bg-base flex items-center justify-center"
|
||||
>
|
||||
<Show when={empty() && !failed() && !suspended()}>
|
||||
<div dir="auto" class="size-full overflow-y-auto">
|
||||
<Show
|
||||
when={recent().length > 0}
|
||||
fallback={
|
||||
<div class="mx-auto flex max-w-[360px] flex-col items-center gap-4 px-6 pt-20 text-center">
|
||||
<Icon name="outline-globe" size="large" class="text-v2-icon-icon-muted" />
|
||||
<div class="flex flex-col gap-2">
|
||||
<div class="text-[13px] font-[530] leading-[var(--line-height-compact)] text-v2-text-text-base">
|
||||
{plugin.t("home.title")}
|
||||
</div>
|
||||
<div class="text-[13px] font-[440] leading-[var(--line-height-compact)] text-v2-text-text-muted">
|
||||
{plugin.t("home.description")}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
>
|
||||
{/* The pages the browser showed last; the side panel's new tab view offers the other tabs. One visit is
|
||||
enough to list; the list stops at a handful. */}
|
||||
<div class="mx-auto flex w-full max-w-[608px] flex-col gap-4 px-6 pt-12 pb-10">
|
||||
<div class="text-[13px] font-[440] leading-[var(--line-height-compact)] text-v2-text-text-muted">
|
||||
{plugin.t("home.recent")}
|
||||
</div>
|
||||
<div class="flex flex-col gap-2">
|
||||
<For each={recent()}>{(visit) => <PageRow visit={visit} onClick={() => go(visit.url)} />}</For>
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
>
|
||||
{/* The pages the browser showed last; the side panel's new tab view offers the other tabs. One visit is
|
||||
enough to list; the list stops at a handful. */}
|
||||
<div class="mx-auto flex w-full max-w-[608px] flex-col gap-4 px-6 pt-12 pb-10">
|
||||
<div class="text-[13px] font-[440] leading-[var(--line-height-compact)] text-v2-text-text-muted">
|
||||
{plugin.t("home.recent")}
|
||||
</div>
|
||||
<div class="flex flex-col gap-2">
|
||||
<For each={recent()}>{(visit) => <PageRow visit={visit} onClick={() => go(visit.url)} />}</For>
|
||||
</div>
|
||||
</div>
|
||||
</Show>
|
||||
</div>
|
||||
</Show>
|
||||
<Show when={failed() && !suspended()}>
|
||||
{/* Add the 40px toolbar to the file empty state's 160px bottom padding to align their centers. */}
|
||||
<div
|
||||
dir="auto"
|
||||
class="flex size-full flex-col items-center justify-center gap-2 p-6 pb-[200px] text-center text-text-weak"
|
||||
>
|
||||
<Icon name="globe" size="large" class="mb-2 shrink-0" />
|
||||
<div class="text-[13px] font-medium leading-[var(--line-height-compact)] text-text-strong">
|
||||
{plugin.t("failed.title")}
|
||||
</Show>
|
||||
</div>
|
||||
<div class="text-13-regular leading-[var(--line-height-base)]">{plugin.t("failed.description")}</div>
|
||||
</div>
|
||||
</Show>
|
||||
<Show when={suspended()}>
|
||||
<p class="px-6 text-center text-13-regular text-v2-text-text-subtle" role="status">
|
||||
{plugin.t("suspended")}
|
||||
</p>
|
||||
</Show>
|
||||
</Show>
|
||||
<Show when={failed() && !suspended()}>
|
||||
{/* Add the 40px toolbar to the file empty state's 160px bottom padding to align their centers. */}
|
||||
<div
|
||||
dir="auto"
|
||||
class="flex size-full flex-col items-center justify-center gap-2 p-6 pb-[200px] text-center text-text-weak"
|
||||
>
|
||||
<Icon name="globe" size="large" class="mb-2 shrink-0" />
|
||||
<div class="text-[13px] font-medium leading-[var(--line-height-compact)] text-text-strong">
|
||||
{plugin.t("failed.title")}
|
||||
</div>
|
||||
<div class="text-13-regular leading-[var(--line-height-base)]">{plugin.t("failed.description")}</div>
|
||||
</div>
|
||||
</Show>
|
||||
<Show when={suspended()}>
|
||||
<p class="px-6 text-center text-13-regular text-v2-text-text-subtle" role="status">
|
||||
{plugin.t("suspended")}
|
||||
</p>
|
||||
</Show>
|
||||
<Show when={spotlight()}>
|
||||
{(rect) => (
|
||||
<div ref={trackPage} class="pointer-events-none absolute inset-0 overflow-hidden">
|
||||
<div
|
||||
data-slot="browser-comment-spotlight"
|
||||
class="absolute rounded-[2px]"
|
||||
style={{
|
||||
left: `${rect().x}px`,
|
||||
top: `${rect().y}px`,
|
||||
width: `${rect().width}px`,
|
||||
height: `${rect().height}px`,
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
</Show>
|
||||
</embeds.View>
|
||||
<Show when={comment()}>
|
||||
{(current) => (
|
||||
<div
|
||||
ref={backdrop}
|
||||
data-component="browser-comment"
|
||||
class="absolute inset-0 z-10"
|
||||
class="absolute start-0 top-0 z-10"
|
||||
style={{ width: `${available().width}px`, height: `${available().height}px` }}
|
||||
onPointerDown={(event) => {
|
||||
// A click beside the editor dismisses it unless it would discard a draft.
|
||||
if (event.target === event.currentTarget) dispatchOverlay({ type: "backdrop.pressed" })
|
||||
}}
|
||||
>
|
||||
<Show when={spotlight()}>
|
||||
{(rect) => (
|
||||
<div
|
||||
data-slot="browser-comment-spotlight"
|
||||
class="pointer-events-none absolute rounded-[2px]"
|
||||
style={{
|
||||
left: `${rect().x}px`,
|
||||
top: `${rect().y}px`,
|
||||
width: `${rect().width}px`,
|
||||
height: `${rect().height}px`,
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
</Show>
|
||||
<Show when={placement()}>
|
||||
{(position) => (
|
||||
<div
|
||||
@@ -882,7 +1146,8 @@ export default function SessionBrowserPane(props: {
|
||||
{(current) => (
|
||||
<div
|
||||
data-component="browser-comment"
|
||||
class="absolute inset-0 z-10"
|
||||
class="absolute start-0 top-0 z-10"
|
||||
style={{ width: `${available().width}px`, height: `${available().height}px` }}
|
||||
onPointerDown={(event) => {
|
||||
if (event.target === event.currentTarget) dispatchOverlay({ type: "backdrop.pressed" })
|
||||
}}
|
||||
@@ -893,8 +1158,8 @@ export default function SessionBrowserPane(props: {
|
||||
data-prevent-autofocus
|
||||
class="absolute end-2 top-2 flex flex-col overflow-hidden rounded-[6px] shadow-[var(--v2-elevation-raised)]"
|
||||
style={{
|
||||
width: `${Math.max(0, Math.min(400, size().width - 16))}px`,
|
||||
"max-height": `${Math.max(0, size().height - 16)}px`,
|
||||
width: "min(400px, calc(100% - 16px))",
|
||||
"max-height": "calc(100% - 16px)",
|
||||
}}
|
||||
>
|
||||
<MenuScroll class="min-h-0">
|
||||
@@ -921,7 +1186,7 @@ export default function SessionBrowserPane(props: {
|
||||
</div>
|
||||
)}
|
||||
</Show>
|
||||
</embeds.View>
|
||||
</Preview>
|
||||
<p class="sr-only" role="status" aria-live="polite">
|
||||
{picking() ? plugin.t("inspect.active") : ""}
|
||||
</p>
|
||||
@@ -1107,6 +1372,7 @@ function SiteInformation(props: {
|
||||
<Popover.Portal>
|
||||
<Popover.Content
|
||||
data-component="browser-site"
|
||||
data-browser-popup
|
||||
// The panel takes focus itself rather than its close button, so a pointer opening shows no focus ring there.
|
||||
onOpenAutoFocus={(event: Event) => {
|
||||
event.preventDefault()
|
||||
|
||||
@@ -28,6 +28,7 @@ import {
|
||||
type BrowserModelEvent,
|
||||
} from "./model-machine"
|
||||
import { PageIcon } from "./page-icon"
|
||||
import { viewportToolbarAvailable } from "./viewport-toolbar-machine"
|
||||
import barStyles from "./bar.css?inline"
|
||||
import commentStyles from "./comment.css?inline"
|
||||
import tabStyles from "./tabs.css?inline"
|
||||
@@ -163,6 +164,23 @@ const setup: Plugin.Setup<typeof definition> = (ctx) => {
|
||||
}
|
||||
})
|
||||
|
||||
ctx.add(Command, (): Command | undefined => {
|
||||
const pane = model()?.pane()
|
||||
|
||||
if (!pane || !viewportToolbarAvailable(ctx.app.channel)) return undefined
|
||||
|
||||
return {
|
||||
id: "toggleToolbar",
|
||||
title: ctx.t("command.toggleToolbar"),
|
||||
group: ctx.t("command.category.view"),
|
||||
bind: "mod+shift+m",
|
||||
// Browser menus and popovers take keyboard focus in a portal outside the panel.
|
||||
scope: "#browser-panel, [data-browser-popup]",
|
||||
enabled: pane.visible() && pane.inspectable(),
|
||||
run: pane.toggleToolbar,
|
||||
}
|
||||
})
|
||||
|
||||
// Blank pages have no focused native view, so the app must claim close inside the pane too.
|
||||
ctx.add(BuiltinCommand, (): BuiltinCommand | undefined => {
|
||||
const pane = model()?.pane()
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
import { expect, test } from "bun:test"
|
||||
import { Schema } from "effect"
|
||||
import { Viewport } from "./ipc"
|
||||
import { initialViewportControls, transitionViewportControls, viewportControlsView } from "./viewport-controls-machine"
|
||||
|
||||
const report = Schema.decodeUnknownSync(Viewport)
|
||||
|
||||
const context = { viewport: report({ mode: "auto" }), autoWidth: 600, autoHeight: 400, zoomed: false }
|
||||
|
||||
test("rotate and scale use the latest edit when the page reports an earlier size and replies arrive in reverse", () => {
|
||||
const edit = transitionViewportControls(initialViewportControls, {
|
||||
type: "edited",
|
||||
axis: "width",
|
||||
value: "820",
|
||||
context,
|
||||
})
|
||||
|
||||
const rotate = transitionViewportControls(edit.state, { type: "rotated", context })
|
||||
const old = { ...context, viewport: report({ mode: "fixed", width: 820, height: 400 }) }
|
||||
const scaled = transitionViewportControls(rotate.state, { type: "scale.selected", scale: 0.5, context: old })
|
||||
|
||||
expect(scaled.apply).toEqual({ attempt: 3, viewport: { mode: "fixed", width: 400, height: 820, scale: 0.5 } })
|
||||
expect(transitionViewportControls(scaled.state, { type: "failed", attempt: 2 }).state).toBe(scaled.state)
|
||||
const latest = transitionViewportControls(scaled.state, { type: "succeeded", attempt: 3, report: old.viewport }).state
|
||||
expect(transitionViewportControls(latest, { type: "succeeded", attempt: 1, report: old.viewport }).state).toBe(latest)
|
||||
expect(viewportControlsView(latest, old).fixed).toEqual({ mode: "fixed", width: 400, height: 820, scale: 0.5 })
|
||||
|
||||
// Zoom or a later external resize wins even while the successful request is ahead of its report.
|
||||
expect(viewportControlsView(scaled.state, { ...context, zoomed: true }).fixed).toBeUndefined()
|
||||
const resized = report({ mode: "fixed", width: 900, height: 600 })
|
||||
expect(viewportControlsView(latest, { ...context, viewport: resized }).fields).toEqual({
|
||||
width: "900",
|
||||
height: "600",
|
||||
})
|
||||
const expired = transitionViewportControls(latest, { type: "timeout", attempt: 3 }).state
|
||||
expect(viewportControlsView(expired, old).fields).toEqual({ width: "820", height: "400" })
|
||||
})
|
||||
|
||||
test("an incomplete edit keeps its own validation message across failure until focus leaves the pair", () => {
|
||||
const edit = transitionViewportControls(initialViewportControls, {
|
||||
type: "edited",
|
||||
axis: "height",
|
||||
value: "380",
|
||||
context,
|
||||
})
|
||||
|
||||
const incomplete = transitionViewportControls(edit.state, { type: "edited", axis: "height", value: "", context })
|
||||
expect(incomplete.apply).toBeUndefined()
|
||||
const rejected = transitionViewportControls(incomplete.state, { type: "failed", attempt: 1 }).state
|
||||
expect(viewportControlsView(rejected, context)).toMatchObject({ error: false, invalid: true, fields: { height: "" } })
|
||||
const blurred = transitionViewportControls(rejected, { type: "blurred" }).state
|
||||
expect(viewportControlsView(blurred, context)).toMatchObject({
|
||||
error: false,
|
||||
invalid: false,
|
||||
fields: { height: "400" },
|
||||
})
|
||||
})
|
||||
|
||||
test("a stalled apply releases the optimistic viewport; retry and disposal reject late replies", () => {
|
||||
const first = transitionViewportControls(initialViewportControls, { type: "scale.selected", scale: 0.5, context })
|
||||
const expired = transitionViewportControls(first.state, { type: "timeout", attempt: 1 }).state
|
||||
expect(viewportControlsView(expired, context)).toMatchObject({ fixed: undefined, error: true })
|
||||
const retry = transitionViewportControls(expired, { type: "scale.selected", scale: 0.75, context }).state
|
||||
expect(transitionViewportControls(retry, { type: "succeeded", attempt: 1, report: context.viewport }).state).toBe(
|
||||
retry,
|
||||
)
|
||||
expect(viewportControlsView(retry, context).error).toBe(false)
|
||||
const disposed = transitionViewportControls(retry, { type: "disposed" }).state
|
||||
expect(transitionViewportControls(disposed, { type: "failed", attempt: 2 }).state).toBe(disposed)
|
||||
})
|
||||
@@ -0,0 +1,134 @@
|
||||
import type { Viewport } from "./ipc"
|
||||
import type { Device } from "./viewport-presets"
|
||||
|
||||
type Dimensions = { width: string; height: string }
|
||||
|
||||
type Fixed = Extract<Viewport, { mode: "fixed" }>
|
||||
|
||||
type Context = { viewport?: Viewport; autoWidth: number; autoHeight: number; zoomed: boolean }
|
||||
|
||||
export type ViewportControlsState =
|
||||
| ({ attempt: number; draft?: Dimensions } & (
|
||||
| { status: "ready" }
|
||||
| { status: "applying"; viewport: Viewport }
|
||||
| { status: "applied"; viewport: Viewport; report: Viewport | undefined }
|
||||
| { status: "failed" }
|
||||
))
|
||||
| { status: "disposed" }
|
||||
|
||||
export type ViewportAction =
|
||||
| { type: "edited"; axis: keyof Dimensions; value: string }
|
||||
| { type: "rotated" }
|
||||
| { type: "preset.selected"; device?: Device }
|
||||
| { type: "scale.selected"; scale?: Fixed["scale"] }
|
||||
|
||||
export type ViewportControlsEvent =
|
||||
| (ViewportAction & { context: Context })
|
||||
| { type: "blurred" }
|
||||
| { type: "succeeded"; attempt: number; report: Viewport | undefined }
|
||||
| { type: "failed" | "timeout"; attempt: number }
|
||||
| { type: "disposed" }
|
||||
|
||||
type Transition = { state: ViewportControlsState; apply?: { attempt: number; viewport: Viewport } }
|
||||
|
||||
export const initialViewportControls: ViewportControlsState = { status: "ready", attempt: 0 }
|
||||
|
||||
export function validDimension(value: string) {
|
||||
return /^\d+$/.test(value) && Number(value) >= 1 && Number(value) <= 4096
|
||||
}
|
||||
|
||||
function invalidFields(fields: Dimensions) {
|
||||
return !validDimension(fields.width) || !validDimension(fields.height)
|
||||
}
|
||||
|
||||
export function viewportControlsView(state: ViewportControlsState, context: Context) {
|
||||
// A scale change or rotation can follow an edit before its native size reaches the window.
|
||||
const optimistic = state.status === "applying" || (state.status === "applied" && state.report === context.viewport)
|
||||
const viewport = optimistic && !context.zoomed ? state.viewport : context.viewport
|
||||
const fixed = viewport?.mode === "fixed" ? viewport : undefined
|
||||
const draft = state.status === "disposed" ? undefined : state.draft
|
||||
|
||||
const fields = draft ?? {
|
||||
width: String(fixed?.width ?? context.autoWidth),
|
||||
height: String(fixed?.height ?? context.autoHeight),
|
||||
}
|
||||
|
||||
const invalid = invalidFields(fields)
|
||||
|
||||
return { fixed, fields, invalid, error: state.status === "failed" && !invalid }
|
||||
}
|
||||
|
||||
export function transitionViewportControls(state: ViewportControlsState, event: ViewportControlsEvent): Transition {
|
||||
if (state.status === "disposed") return { state }
|
||||
|
||||
switch (event.type) {
|
||||
case "disposed":
|
||||
return { state: { status: "disposed" } }
|
||||
case "blurred":
|
||||
return state.draft ? { state: { ...state, draft: undefined } } : { state }
|
||||
case "succeeded":
|
||||
if (state.status !== "applying" || state.attempt !== event.attempt) return { state }
|
||||
|
||||
return { state: { ...state, status: "applied", report: event.report } }
|
||||
case "failed":
|
||||
case "timeout": {
|
||||
// A successful reply can precede the page report. Stop holding the requested size if no report follows.
|
||||
if (event.type === "timeout" && state.status === "applied" && event.attempt === state.attempt)
|
||||
return { state: { status: "ready", attempt: state.attempt, draft: state.draft } }
|
||||
|
||||
if (state.status !== "applying" || state.attempt !== event.attempt) return { state }
|
||||
const { draft, attempt } = state
|
||||
|
||||
return { state: { status: draft && invalidFields(draft) ? "ready" : "failed", draft, attempt } }
|
||||
}
|
||||
}
|
||||
|
||||
if (event.context.zoomed) return { state }
|
||||
const { fixed, fields, invalid } = viewportControlsView(state, event.context)
|
||||
const scale = fixed?.scale
|
||||
const attempt = state.attempt + 1
|
||||
|
||||
const apply = (viewport: Viewport, draft?: Dimensions): Transition => ({
|
||||
state: { status: "applying", attempt, viewport, draft },
|
||||
apply: { attempt, viewport },
|
||||
})
|
||||
|
||||
switch (event.type) {
|
||||
case "edited": {
|
||||
const draft = { ...fields, [event.axis]: event.value }
|
||||
|
||||
if (invalidFields(draft)) {
|
||||
if (state.status === "failed") return { state: { status: "ready", attempt: state.attempt, draft } }
|
||||
|
||||
return { state: { ...state, draft } }
|
||||
}
|
||||
|
||||
return apply({ mode: "fixed", width: Number(draft.width), height: Number(draft.height), scale }, draft)
|
||||
}
|
||||
|
||||
case "rotated":
|
||||
if (invalid) return { state }
|
||||
|
||||
return apply({ mode: "fixed", ...fixed, width: Number(fields.height), height: Number(fields.width) })
|
||||
case "preset.selected": {
|
||||
const device = event.device
|
||||
|
||||
return apply(
|
||||
device
|
||||
? { mode: "fixed", width: device.width, height: device.height, preset: device.id, scale }
|
||||
: { mode: "auto" },
|
||||
)
|
||||
}
|
||||
|
||||
case "scale.selected":
|
||||
if (!fixed && event.scale === undefined) return { state }
|
||||
|
||||
return apply({
|
||||
mode: "fixed",
|
||||
width: event.context.autoWidth,
|
||||
height: event.context.autoHeight,
|
||||
...fixed,
|
||||
scale: event.scale,
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,170 @@
|
||||
import { usePlugin } from "@opencode/plugin/gui"
|
||||
import { Button } from "@opencode/ui/button"
|
||||
import { IconButton } from "@opencode/ui/icon-button"
|
||||
import { Menu } from "@opencode/ui/menu"
|
||||
import { TextInput } from "@opencode/ui/text-input"
|
||||
import { Tooltip } from "@opencode/ui/tooltip"
|
||||
import { debounce } from "@solid-primitives/scheduled"
|
||||
import { createMemo, createSignal, createUniqueId, For, onCleanup, Show, untrack } from "solid-js"
|
||||
import type { Viewport } from "./ipc"
|
||||
import {
|
||||
initialViewportControls,
|
||||
transitionViewportControls,
|
||||
validDimension,
|
||||
viewportControlsView,
|
||||
type ViewportAction,
|
||||
type ViewportControlsEvent,
|
||||
} from "./viewport-controls-machine"
|
||||
import { ViewportPresetMenu } from "./viewport-preset-menu"
|
||||
import { previewScales } from "./viewport-presets"
|
||||
|
||||
export function ViewportControls(props: {
|
||||
viewport?: Viewport
|
||||
autoWidth: number
|
||||
autoHeight: number
|
||||
effectiveScale: number
|
||||
zoomed: boolean
|
||||
apply: (viewport: Viewport) => Promise<void>
|
||||
}) {
|
||||
const plugin = usePlugin()
|
||||
const hint = createUniqueId()
|
||||
const [state, setState] = createSignal(initialViewportControls)
|
||||
const view = createMemo(() => viewportControlsView(state(), props))
|
||||
const scale = () => view().fixed?.scale
|
||||
|
||||
const scaleLabel = () => {
|
||||
const percent = Math.round((scale() ?? props.effectiveScale) * 100)
|
||||
|
||||
if (scale()) return plugin.t("zoom.percent", { percent })
|
||||
|
||||
return props.effectiveScale < 0.01 ? plugin.t("viewport.fitBelowOne") : plugin.t("viewport.fitPercent", { percent })
|
||||
}
|
||||
|
||||
const timeout = debounce((attempt: number) => dispatch({ type: "timeout", attempt }), 10_000)
|
||||
|
||||
const dispatch = (event: ViewportControlsEvent) => {
|
||||
const next = transitionViewportControls(untrack(state), event)
|
||||
setState(next.state)
|
||||
|
||||
if (!next.apply) return
|
||||
const { attempt, viewport } = next.apply
|
||||
timeout(attempt)
|
||||
void props.apply(viewport).then(
|
||||
() => dispatch({ type: "succeeded", attempt, report: props.viewport }),
|
||||
() => dispatch({ type: "failed", attempt }),
|
||||
)
|
||||
}
|
||||
|
||||
const action = (event: ViewportAction) =>
|
||||
dispatch({
|
||||
...event,
|
||||
context: {
|
||||
viewport: props.viewport,
|
||||
autoWidth: props.autoWidth,
|
||||
autoHeight: props.autoHeight,
|
||||
zoomed: props.zoomed,
|
||||
},
|
||||
})
|
||||
|
||||
onCleanup(() => dispatch({ type: "disposed" }))
|
||||
|
||||
return (
|
||||
<>
|
||||
<ViewportPresetMenu
|
||||
selected={view().fixed?.preset ?? (view().fixed ? "custom" : undefined)}
|
||||
disabled={props.zoomed}
|
||||
onAuto={() => action({ type: "preset.selected" })}
|
||||
onSelect={(device) => action({ type: "preset.selected", device })}
|
||||
/>
|
||||
<div
|
||||
data-slot="browser-viewport-group"
|
||||
role="group"
|
||||
aria-label={plugin.t("viewport.dimensions")}
|
||||
onFocusOut={(event) => {
|
||||
if (event.relatedTarget instanceof Node && event.currentTarget.contains(event.relatedTarget)) return
|
||||
dispatch({ type: "blurred" })
|
||||
}}
|
||||
>
|
||||
<For each={["width", "height"] as const}>
|
||||
{(axis) => (
|
||||
<>
|
||||
<Show when={axis === "height"}>
|
||||
<span aria-hidden="true">×</span>
|
||||
</Show>
|
||||
<TextInput
|
||||
appearance="small"
|
||||
numeric
|
||||
dir="ltr"
|
||||
inputmode="numeric"
|
||||
autocomplete="off"
|
||||
disabled={props.zoomed}
|
||||
aria-label={plugin.t(axis === "width" ? "viewport.width" : "viewport.height")}
|
||||
aria-describedby={view().invalid ? hint : undefined}
|
||||
invalid={!validDimension(view().fields[axis])}
|
||||
value={view().fields[axis]}
|
||||
onInput={(event) => action({ type: "edited", axis, value: event.currentTarget.value })}
|
||||
/>
|
||||
</>
|
||||
)}
|
||||
</For>
|
||||
</div>
|
||||
<div data-slot="browser-viewport-group">
|
||||
<Menu placement="bottom-end" gutter={4} modal={false}>
|
||||
<Menu.Trigger
|
||||
as={Button}
|
||||
size="small"
|
||||
variant="ghost-faint"
|
||||
data-slot="browser-viewport-scale"
|
||||
aria-label={plugin.t("viewport.scale")}
|
||||
aria-description={scaleLabel()}
|
||||
disabled={props.zoomed}
|
||||
>
|
||||
{scaleLabel()}
|
||||
<span aria-hidden="true" data-slot="viewport-chevron" />
|
||||
</Menu.Trigger>
|
||||
<Menu.Portal>
|
||||
<Menu.Content data-slot="browser-viewport-scale-menu" data-browser-popup>
|
||||
<Menu.RadioGroup value={String(scale() ?? "fit")}>
|
||||
<Menu.RadioItem value="fit" onSelect={() => action({ type: "scale.selected" })} closeOnSelect>
|
||||
{plugin.t("viewport.fit")}
|
||||
</Menu.RadioItem>
|
||||
<Menu.Separator />
|
||||
<For each={previewScales}>
|
||||
{(factor) => (
|
||||
<Menu.RadioItem
|
||||
value={String(factor)}
|
||||
onSelect={() => action({ type: "scale.selected", scale: factor })}
|
||||
closeOnSelect
|
||||
>
|
||||
{plugin.t("zoom.percent", { percent: factor * 100 })}
|
||||
</Menu.RadioItem>
|
||||
)}
|
||||
</For>
|
||||
</Menu.RadioGroup>
|
||||
</Menu.Content>
|
||||
</Menu.Portal>
|
||||
</Menu>
|
||||
<Tooltip placement="top" value={plugin.t("viewport.rotate")}>
|
||||
<IconButton
|
||||
variant="ghost-muted"
|
||||
size="small"
|
||||
aria-label={plugin.t("viewport.rotate")}
|
||||
disabled={view().invalid || props.zoomed}
|
||||
onClick={() => action({ type: "rotated" })}
|
||||
icon={<span aria-hidden="true" data-slot="viewport-rotate-icon" />}
|
||||
/>
|
||||
</Tooltip>
|
||||
</div>
|
||||
<Show when={view().invalid}>
|
||||
<span id={hint} data-slot="browser-viewport-message" role="status">
|
||||
{plugin.t("viewport.invalid")}
|
||||
</span>
|
||||
</Show>
|
||||
<Show when={view().error}>
|
||||
<span data-slot="browser-viewport-message" role="alert">
|
||||
{plugin.t("viewport.error")}
|
||||
</span>
|
||||
</Show>
|
||||
</>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
import { usePlugin } from "@opencode/plugin/gui"
|
||||
import { For, onCleanup } from "solid-js"
|
||||
|
||||
type Grip = "width" | "height" | "corner"
|
||||
|
||||
type Size = { width: number; height: number }
|
||||
|
||||
export function ViewportHandles(props: {
|
||||
disabled: boolean
|
||||
size: Size
|
||||
/** Guest CSS pixels per pointer pixel at the current preview and window zoom. */
|
||||
ratio: number
|
||||
centered: { width: boolean; height: boolean }
|
||||
resize: (size: Size) => void
|
||||
}) {
|
||||
const plugin = usePlugin()
|
||||
const clamp = (value: number) => Math.max(1, Math.min(4096, Math.round(value)))
|
||||
|
||||
let drag:
|
||||
| {
|
||||
id: number
|
||||
handle: HTMLElement
|
||||
x: number
|
||||
y: number
|
||||
width: number
|
||||
height: number
|
||||
dx: number
|
||||
dy: number
|
||||
last: Size
|
||||
}
|
||||
| undefined
|
||||
|
||||
const start = (event: PointerEvent & { currentTarget: HTMLElement }, grip: Grip) => {
|
||||
if (drag || event.button !== 0 || !event.isPrimary) return
|
||||
const rtl = getComputedStyle(event.currentTarget).direction === "rtl"
|
||||
|
||||
// Keep the initial scale throughout the drag. Centered previews move both edges.
|
||||
drag = {
|
||||
id: event.pointerId,
|
||||
handle: event.currentTarget,
|
||||
x: event.clientX,
|
||||
y: event.clientY,
|
||||
width: props.size.width,
|
||||
height: props.size.height,
|
||||
dx: grip === "height" ? 0 : props.ratio * (props.centered.width ? 2 : 1) * (rtl ? -1 : 1),
|
||||
dy: grip === "width" ? 0 : props.ratio * (props.centered.height ? 2 : 1),
|
||||
last: props.size,
|
||||
}
|
||||
event.currentTarget.setPointerCapture(event.pointerId)
|
||||
window.addEventListener("blur", end)
|
||||
}
|
||||
|
||||
const move = (event: PointerEvent) => {
|
||||
if (!drag || event.pointerId !== drag.id) return
|
||||
const width = clamp(drag.width + (event.clientX - drag.x) * drag.dx)
|
||||
const height = clamp(drag.height + (event.clientY - drag.y) * drag.dy)
|
||||
|
||||
if (width === drag.last.width && height === drag.last.height) return
|
||||
drag.last = { width, height }
|
||||
props.resize(drag.last)
|
||||
}
|
||||
|
||||
const end = () => {
|
||||
if (!drag) return
|
||||
const { handle, id } = drag
|
||||
drag = undefined
|
||||
window.removeEventListener("blur", end)
|
||||
|
||||
if (handle.hasPointerCapture(id)) handle.releasePointerCapture(id)
|
||||
}
|
||||
|
||||
onCleanup(end)
|
||||
|
||||
const key = (event: KeyboardEvent & { currentTarget: HTMLElement }, grip: Grip) => {
|
||||
if (grip === "corner") return
|
||||
const rtl = getComputedStyle(event.currentTarget).direction === "rtl"
|
||||
const keys = grip === "height" ? ["ArrowUp", "ArrowDown"] : ["ArrowLeft", "ArrowRight"]
|
||||
const index = keys.indexOf(event.key)
|
||||
|
||||
if (index === -1) return
|
||||
event.preventDefault()
|
||||
const step = event.shiftKey ? 10 : 1
|
||||
const increase = grip === "width" && rtl ? index === 0 : index === 1
|
||||
props.resize({ ...props.size, [grip]: clamp(props.size[grip] + (increase ? step : -step)) })
|
||||
}
|
||||
|
||||
return (
|
||||
<For each={["width", "height", "corner"] as const}>
|
||||
{(grip) => (
|
||||
<div
|
||||
data-slot="browser-viewport-grip"
|
||||
data-axis={grip}
|
||||
inert={props.disabled}
|
||||
{...(grip === "corner"
|
||||
? { "aria-hidden": true }
|
||||
: {
|
||||
role: "separator",
|
||||
tabIndex: 0,
|
||||
"aria-label": plugin.t(grip === "width" ? "viewport.resizeWidth" : "viewport.resizeHeight"),
|
||||
"aria-orientation": grip === "width" ? "vertical" : "horizontal",
|
||||
"aria-valuemin": 1,
|
||||
"aria-valuemax": 4096,
|
||||
"aria-valuenow": props.size[grip],
|
||||
})}
|
||||
onKeyDown={(event) => key(event, grip)}
|
||||
onPointerDown={(event) => start(event, grip)}
|
||||
onPointerMove={move}
|
||||
onPointerUp={(event) => {
|
||||
if (event.pointerId !== drag?.id) return
|
||||
move(event)
|
||||
end()
|
||||
}}
|
||||
onPointerCancel={(event) => event.pointerId === drag?.id && end()}
|
||||
onLostPointerCapture={(event) => event.pointerId === drag?.id && end()}
|
||||
/>
|
||||
)}
|
||||
</For>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
import type { Viewport } from "./ipc"
|
||||
|
||||
type Size = { width: number; height: number }
|
||||
|
||||
type FixedViewport = Extract<Viewport, { mode: "fixed" }>
|
||||
|
||||
/** Fit uses the whole DIPs available to the native view. Manual scales can overflow the pane. */
|
||||
export function viewportScale(viewport: FixedViewport, bounds: Size) {
|
||||
if (viewport.scale !== undefined) return viewport.scale
|
||||
|
||||
return Math.min(
|
||||
1,
|
||||
Math.max(1, Math.floor(bounds.width)) / viewport.width,
|
||||
Math.max(1, Math.floor(bounds.height)) / viewport.height,
|
||||
)
|
||||
}
|
||||
|
||||
export function viewportLayout(viewport: FixedViewport, area: Size, zoom: number) {
|
||||
const scale = viewportScale(viewport, { width: area.width * zoom, height: area.height * zoom })
|
||||
// Round up to native DIPs, then to Chromium's 1/64 CSS pixel layout unit.
|
||||
const extent = (pixels: number) => Math.ceil((Math.ceil(pixels * scale) * 64) / zoom) / 64
|
||||
|
||||
return { scale, width: extent(viewport.width), height: extent(viewport.height) }
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
import { expect, test } from "bun:test"
|
||||
import { createViewportWriter } from "./viewport-native"
|
||||
|
||||
const phone = { mode: "fixed", width: 390, height: 844 } as const
|
||||
|
||||
test("switching from Fit to the same percentage locks the scale until Fit is selected again", async () => {
|
||||
const initial = { width: 195, height: 422 }
|
||||
|
||||
const small = { width: 100, height: 211 }
|
||||
|
||||
const half = { ...phone, scale: 0.5 } as const
|
||||
|
||||
const writer = createViewportWriter(
|
||||
async () => {},
|
||||
() => {},
|
||||
() => false,
|
||||
)
|
||||
|
||||
await writer.set(phone, initial, 1)
|
||||
await writer.set(half, initial, 1)
|
||||
expect(writer.applied().viewport).toEqual(half)
|
||||
await writer.set(half, small, 1)
|
||||
expect(writer.applied().scale).toBe(0.5)
|
||||
await writer.set({ ...phone, scale: 2 }, small, 1)
|
||||
expect(writer.applied().scale).toBe(2)
|
||||
await writer.set(phone, small, 1)
|
||||
expect(writer.applied()).toEqual({ viewport: phone, scale: 0.25 })
|
||||
})
|
||||
|
||||
test("Auto wins when the pane resizes during an outstanding fixed-size command", async () => {
|
||||
const started = Promise.withResolvers<void>()
|
||||
|
||||
const finish = Promise.withResolvers<void>()
|
||||
|
||||
const commands: string[] = []
|
||||
|
||||
const writer = createViewportWriter(
|
||||
async (next) => {
|
||||
commands.push(`${next.viewport.mode}:${next.scale}`)
|
||||
started.resolve()
|
||||
await finish.promise
|
||||
},
|
||||
() => {},
|
||||
() => false,
|
||||
)
|
||||
|
||||
const first = writer.set(phone, phone, 1)
|
||||
await started.promise
|
||||
const resize = writer.set(phone, { width: 195, height: 422 }, 1)
|
||||
|
||||
const reset = writer.set({ mode: "auto" }, phone, 1)
|
||||
finish.resolve()
|
||||
await Promise.all([first, resize, reset])
|
||||
expect(commands).toEqual(["fixed:1", "auto:1"])
|
||||
expect(writer.applied().viewport).toEqual({ mode: "auto" })
|
||||
})
|
||||
@@ -0,0 +1,99 @@
|
||||
import type { PaneEvent, Viewport } from "./ipc"
|
||||
import { viewportScale } from "./viewport-layout"
|
||||
|
||||
type Preview = Pick<Extract<PaneEvent, { type: "page" }>, "viewport" | "viewportNotice"> & { scale: number }
|
||||
|
||||
type Size = { width: number; height: number }
|
||||
|
||||
const AUTO: Preview = { viewport: { mode: "auto" }, scale: 1 }
|
||||
|
||||
function preview(viewport: Viewport, bounds: Size, zoom: number): Preview {
|
||||
// Each request needs a distinct identity, including Auto → Fixed → Auto.
|
||||
if (viewport.mode === "auto") return { ...AUTO }
|
||||
|
||||
if (zoom !== 1) return { ...AUTO, viewportNotice: "zoom" }
|
||||
|
||||
return { viewport, scale: viewportScale(viewport, bounds) }
|
||||
}
|
||||
|
||||
function same(a: Preview, b: Preview) {
|
||||
if (a.scale !== b.scale || a.viewportNotice !== b.viewportNotice) return false
|
||||
|
||||
if (a.viewport.mode === "auto" || b.viewport.mode === "auto") return a.viewport.mode === b.viewport.mode
|
||||
|
||||
return (
|
||||
a.viewport.width === b.viewport.width &&
|
||||
a.viewport.height === b.viewport.height &&
|
||||
a.viewport.preset === b.viewport.preset &&
|
||||
a.viewport.scale === b.viewport.scale
|
||||
)
|
||||
}
|
||||
|
||||
/** A page has one metrics writer. Captures share its queue: CDP temporarily sets and restores metrics for a clip. */
|
||||
export function createViewportWriter(
|
||||
apply: (next: Preview) => Promise<void>,
|
||||
changed: () => void,
|
||||
closed: () => boolean,
|
||||
) {
|
||||
let requested = AUTO
|
||||
let applied = AUTO
|
||||
let queue = Promise.resolve()
|
||||
let writing = queue
|
||||
|
||||
function exclusive<T>(action: () => Promise<T>) {
|
||||
const result = queue.then(action)
|
||||
queue = result.then(
|
||||
() => undefined,
|
||||
() => undefined,
|
||||
)
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
return {
|
||||
requested: () => requested.viewport,
|
||||
applied: () => applied,
|
||||
settled: () => queue,
|
||||
exclusive,
|
||||
set(viewport: Viewport, bounds: Size, zoom: number) {
|
||||
const next = preview(viewport, bounds, zoom)
|
||||
|
||||
if (same(requested, next)) return writing
|
||||
requested = next
|
||||
|
||||
writing = exclusive(async () => {
|
||||
if (closed()) throw new Error("browser.pane.unavailable")
|
||||
|
||||
if (requested !== next) return
|
||||
|
||||
try {
|
||||
await apply(next)
|
||||
applied = next
|
||||
} catch (error) {
|
||||
if (closed() || requested !== next) throw error
|
||||
const fallback: Preview = { ...AUTO, viewportNotice: "failed" }
|
||||
|
||||
// A rejected override can have changed the renderer. Confirm the clear before reporting Auto.
|
||||
try {
|
||||
await apply(fallback)
|
||||
applied = fallback
|
||||
} catch {
|
||||
applied = { ...applied, viewportNotice: "failed" }
|
||||
}
|
||||
|
||||
if (requested === next) {
|
||||
requested = applied
|
||||
|
||||
if (!closed()) changed()
|
||||
}
|
||||
|
||||
throw error
|
||||
}
|
||||
|
||||
if (!closed() && requested === applied) changed()
|
||||
})
|
||||
|
||||
return writing
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
import { usePlugin } from "@opencode/plugin/gui"
|
||||
import { Button } from "@opencode/ui/button"
|
||||
import { Menu } from "@opencode/ui/menu"
|
||||
import { For, Show } from "solid-js"
|
||||
import { moreDevices, quickPresets, type Device } from "./viewport-presets"
|
||||
|
||||
export function ViewportPresetMenu(props: {
|
||||
selected?: Device["id"] | "custom"
|
||||
disabled: boolean
|
||||
onAuto: () => void
|
||||
onSelect: (device: Device) => void
|
||||
}) {
|
||||
const plugin = usePlugin()
|
||||
|
||||
const selectedLabel = () => {
|
||||
if (props.selected === "custom") return plugin.t("viewport.custom")
|
||||
|
||||
return props.selected ? plugin.t(`viewport.device.${props.selected}`) : plugin.t("viewport.responsive")
|
||||
}
|
||||
|
||||
const row = (device: Device) => (
|
||||
<Menu.RadioItem value={device.id} disabled={props.disabled} closeOnSelect onSelect={() => props.onSelect(device)}>
|
||||
{plugin.t(`viewport.device.${device.id}`)}
|
||||
</Menu.RadioItem>
|
||||
)
|
||||
|
||||
return (
|
||||
<Menu placement="bottom-start" gutter={4} modal={false}>
|
||||
<Menu.Trigger
|
||||
as={Button}
|
||||
variant="ghost"
|
||||
size="small"
|
||||
data-slot="browser-viewport-preset-trigger"
|
||||
aria-label={plugin.t("viewport.preset")}
|
||||
aria-description={selectedLabel()}
|
||||
disabled={props.disabled}
|
||||
>
|
||||
<span>{selectedLabel()}</span>
|
||||
<span aria-hidden="true" data-slot="viewport-chevron" />
|
||||
</Menu.Trigger>
|
||||
<Menu.Portal>
|
||||
<Menu.Content data-slot="browser-viewport-preset-menu" data-browser-popup>
|
||||
<Menu.RadioGroup value={props.selected ?? "auto"}>
|
||||
<Menu.RadioItem value="auto" closeOnSelect onSelect={props.onAuto}>
|
||||
{plugin.t("viewport.responsive")}
|
||||
</Menu.RadioItem>
|
||||
<Show when={props.selected === "custom"}>
|
||||
<Menu.RadioItem value="custom" closeOnSelect>
|
||||
{plugin.t("viewport.custom")}
|
||||
</Menu.RadioItem>
|
||||
</Show>
|
||||
<For each={quickPresets}>{row}</For>
|
||||
</Menu.RadioGroup>
|
||||
<Menu.Separator />
|
||||
<Menu.Sub gutter={4} overlap>
|
||||
<Menu.SubTrigger disabled={props.disabled}>{plugin.t("viewport.moreDevices")}</Menu.SubTrigger>
|
||||
<Menu.Portal>
|
||||
<Menu.SubContent data-slot="browser-viewport-preset-menu" data-device-list data-browser-popup>
|
||||
<Menu.RadioGroup value={props.selected ?? ""}>
|
||||
<For each={moreDevices}>{row}</For>
|
||||
</Menu.RadioGroup>
|
||||
</Menu.SubContent>
|
||||
</Menu.Portal>
|
||||
</Menu.Sub>
|
||||
</Menu.Content>
|
||||
</Menu.Portal>
|
||||
</Menu>
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
export const quickPresets = [
|
||||
{ id: "galaxy-s26", width: 360, height: 780, profile: "phone" },
|
||||
{ id: "iphone-18-pro", width: 402, height: 874, profile: "phone" },
|
||||
{ id: "ipad-air-11", width: 820, height: 1180, profile: "tablet" },
|
||||
{ id: "laptop", width: 1280, height: 720, profile: "desktop" },
|
||||
{ id: "desktop", width: 1440, height: 900, profile: "desktop" },
|
||||
] as const
|
||||
|
||||
export const moreDevices = [
|
||||
{ id: "iphone-se", width: 375, height: 667, profile: "phone" },
|
||||
{ id: "iphone-16", width: 393, height: 852, profile: "phone" },
|
||||
{ id: "iphone-17e", width: 390, height: 844, profile: "phone" },
|
||||
{ id: "iphone-16-pro-max", width: 440, height: 956, profile: "phone" },
|
||||
{ id: "iphone-duo-closed", width: 466, height: 678, profile: "phone" },
|
||||
{ id: "iphone-duo-open", width: 951, height: 669, profile: "phone" },
|
||||
{ id: "compact-phone", width: 360, height: 800, profile: "phone" },
|
||||
{ id: "pixel-10", width: 412, height: 924, profile: "phone" },
|
||||
{ id: "pixel-9-pro-fold", width: 412, height: 922, profile: "phone" },
|
||||
{ id: "ipad-mini-a20-pro", width: 756, height: 1158, profile: "tablet" },
|
||||
{ id: "ipad-pro-13", width: 1032, height: 1376, profile: "tablet" },
|
||||
{ id: "surface-pro-10", width: 960, height: 1440, profile: "windows-tablet" },
|
||||
] as const
|
||||
|
||||
export const devices = [...quickPresets, ...moreDevices] as const
|
||||
|
||||
export type Device = (typeof devices)[number]
|
||||
|
||||
export const previewScales = [0.5, 0.75, 1, 1.25, 1.5, 2] as const
|
||||
@@ -0,0 +1,78 @@
|
||||
import { expect, test } from "bun:test"
|
||||
import { Browser } from "@opencode/plugin-browser/rpc"
|
||||
import { Schema } from "effect"
|
||||
import { PaneEvent, type Viewport } from "./ipc"
|
||||
import {
|
||||
initialViewportToolbar,
|
||||
transitionViewportToolbar,
|
||||
viewportToolbarAvailable,
|
||||
viewportToolbarView,
|
||||
} from "./viewport-toolbar-machine"
|
||||
|
||||
const target = { sessionKey: "alpha", tabID: Browser.TabID.make("tab_11111111-1111-1111-1111-111111111111") }
|
||||
|
||||
function page(viewport: Viewport, viewportNotice?: "failed" | "zoom") {
|
||||
const event = Schema.decodeUnknownSync(PaneEvent)({
|
||||
type: "page",
|
||||
tabID: target.tabID,
|
||||
zoom: 1,
|
||||
viewport,
|
||||
...(viewportNotice && { viewportNotice }),
|
||||
})
|
||||
|
||||
if (event.type !== "page") throw new Error("Expected a page report")
|
||||
|
||||
return event
|
||||
}
|
||||
|
||||
const fixed = { target, available: true, page: page({ mode: "fixed", width: 320, height: 700 }) }
|
||||
|
||||
test("rapid close, reopen, close keeps the final close when the earlier replies arrive last", () => {
|
||||
const toggle = { type: "toggled", context: fixed } as const
|
||||
const close = transitionViewportToolbar(initialViewportToolbar, toggle)
|
||||
const reopen = transitionViewportToolbar(close.state, toggle)
|
||||
const last = transitionViewportToolbar(reopen.state, toggle)
|
||||
expect([close.effect, reopen.effect, last.effect]).toEqual([
|
||||
{ type: "toggle", open: false, attempt: 1, target },
|
||||
{ type: "toggle", open: true, attempt: 2, target },
|
||||
{ type: "toggle", open: false, attempt: 3, target },
|
||||
])
|
||||
expect(transitionViewportToolbar(last.state, { type: "failed", attempt: 2, context: fixed })).toEqual({
|
||||
state: last.state,
|
||||
})
|
||||
const settled = transitionViewportToolbar(last.state, { type: "succeeded", attempt: 3 }).state
|
||||
expect(viewportToolbarView(settled, fixed).visible).toBe(false)
|
||||
expect(transitionViewportToolbar(settled, { type: "failed", attempt: 1, context: fixed })).toEqual({ state: settled })
|
||||
})
|
||||
|
||||
test("closing or dismissing a notice hides only the report the user saw", () => {
|
||||
const context = { ...fixed, page: page(fixed.page.viewport, "failed") }
|
||||
const dismissed = transitionViewportToolbar(initialViewportToolbar, { type: "notice.dismissed", context })
|
||||
expect(viewportToolbarView(dismissed.state, context).notice).toBeUndefined()
|
||||
const close = transitionViewportToolbar(initialViewportToolbar, { type: "toggled", context })
|
||||
const reopen = transitionViewportToolbar(close.state, { type: "toggled", context })
|
||||
expect(viewportToolbarView(reopen.state, context).notice).toBeUndefined()
|
||||
expect(viewportToolbarView(reopen.state, { ...context, page: page(fixed.page.viewport, "failed") }).notice).toBe(
|
||||
"failed",
|
||||
)
|
||||
})
|
||||
|
||||
test("a failed close follows the actual page size and only notifies the visit that requested it", () => {
|
||||
const close = transitionViewportToolbar(initialViewportToolbar, { type: "toggled", context: fixed })
|
||||
const failed = transitionViewportToolbar(close.state, { type: "failed", attempt: 1, context: fixed })
|
||||
expect(failed.effect).toEqual({ type: "notify-failed" })
|
||||
expect(viewportToolbarView(failed.state, fixed).visible).toBe(true)
|
||||
const back = { ...fixed, target: { ...target } }
|
||||
expect(transitionViewportToolbar(close.state, { type: "timeout", attempt: 1, context: back }).effect).toBeUndefined()
|
||||
const disposed = transitionViewportToolbar(close.state, { type: "disposed" }).state
|
||||
expect(transitionViewportToolbar(disposed, { type: "failed", attempt: 1, context: fixed })).toEqual({
|
||||
state: disposed,
|
||||
})
|
||||
})
|
||||
|
||||
test("offers the toolbar only in local and dev builds", () => {
|
||||
expect(viewportToolbarAvailable("local")).toBe(true)
|
||||
expect(viewportToolbarAvailable("dev")).toBe(true)
|
||||
expect(viewportToolbarAvailable("beta")).toBe(false)
|
||||
expect(viewportToolbarAvailable("prod")).toBe(false)
|
||||
})
|
||||
@@ -0,0 +1,94 @@
|
||||
import type { App } from "@opencode/plugin/gui"
|
||||
import type { Browser } from "@opencode/plugin-browser/rpc"
|
||||
import type { PageEvent } from "./connection"
|
||||
|
||||
type Target = { sessionKey: string; tabID: Browser.TabID }
|
||||
|
||||
type Context = { target: Target | undefined; available: boolean; page: PageEvent | undefined }
|
||||
|
||||
export type ViewportToolbarState =
|
||||
| { status: "following"; attempt: number; dismissed?: PageEvent }
|
||||
| { status: "toggling" | "selected"; attempt: number; target: Target; open: boolean; dismissed?: PageEvent }
|
||||
| { status: "disposed" }
|
||||
|
||||
export type ViewportToolbarEvent =
|
||||
| { type: "toggled" | "notice.dismissed"; context: Context }
|
||||
| { type: "succeeded"; attempt: number }
|
||||
| { type: "failed" | "timeout"; attempt: number; context: Context }
|
||||
| { type: "disposed" }
|
||||
|
||||
type Effect = { type: "toggle"; attempt: number; target: Target; open: boolean } | { type: "notify-failed" }
|
||||
|
||||
type Transition = { state: ViewportToolbarState; effect?: Effect }
|
||||
|
||||
export const initialViewportToolbar: ViewportToolbarState = { status: "following", attempt: 0 }
|
||||
|
||||
/** Device previews are unreleased: only local and dev builds offer the toolbar. */
|
||||
export function viewportToolbarAvailable(channel: App["channel"]) {
|
||||
return channel === "local" || channel === "dev"
|
||||
}
|
||||
|
||||
export function viewportToolbarView(state: ViewportToolbarState, context: Context) {
|
||||
if (state.status === "disposed") return { visible: false, notice: undefined }
|
||||
const fixed = context.page?.viewport.mode === "fixed"
|
||||
|
||||
const selected =
|
||||
state.status !== "following" && state.target.sessionKey === context.target?.sessionKey ? state : undefined
|
||||
|
||||
// A close hides the controls immediately, while the page still reports its old fixed size.
|
||||
const open = selected && selected.target.tabID === context.target?.tabID ? selected.open : fixed || !!selected?.open
|
||||
|
||||
const notice = state.dismissed !== context.page ? context.page?.viewportNotice : undefined
|
||||
|
||||
return { visible: context.available && !!context.target && open, notice }
|
||||
}
|
||||
|
||||
export function transitionViewportToolbar(state: ViewportToolbarState, event: ViewportToolbarEvent): Transition {
|
||||
if (state.status === "disposed") return { state }
|
||||
|
||||
switch (event.type) {
|
||||
case "disposed":
|
||||
return { state: { status: "disposed" } }
|
||||
case "notice.dismissed":
|
||||
if (!viewportToolbarView(state, event.context).notice) return { state }
|
||||
|
||||
return { state: { ...state, dismissed: event.context.page } }
|
||||
case "toggled": {
|
||||
const { target, available, page } = event.context
|
||||
|
||||
if (!available || !target) return { state }
|
||||
const view = viewportToolbarView(state, event.context)
|
||||
const open = !view.visible
|
||||
const attempt = state.attempt + 1
|
||||
|
||||
return {
|
||||
state: {
|
||||
status: "toggling",
|
||||
target,
|
||||
attempt,
|
||||
open,
|
||||
dismissed: view.visible && view.notice ? page : state.dismissed,
|
||||
},
|
||||
effect: { type: "toggle", target, attempt, open },
|
||||
}
|
||||
}
|
||||
|
||||
case "succeeded":
|
||||
case "failed":
|
||||
case "timeout": {
|
||||
if (state.status !== "toggling" || state.attempt !== event.attempt) return { state }
|
||||
|
||||
if (event.type === "succeeded") return { state: { ...state, status: "selected" } }
|
||||
const current = state.target === event.context.target && event.context.available
|
||||
const reopen = current && event.type === "failed" && event.context.page?.viewport.mode === "fixed"
|
||||
|
||||
return {
|
||||
state:
|
||||
state.open || reopen
|
||||
? { ...state, status: "selected", open: true }
|
||||
: { status: "following", attempt: state.attempt, dismissed: state.dismissed },
|
||||
effect: current ? { type: "notify-failed" } : undefined,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -335,6 +335,8 @@ export interface BridgeLayout {
|
||||
/** Height. */
|
||||
readonly height: number
|
||||
}
|
||||
/** Visible portion of `bounds`, in the same window DIPs. Omit to show the full surface; an empty clip hides it. */
|
||||
readonly clip?: NonNullable<BridgeLayout["bounds"]>
|
||||
/** The window's content size the bounds were measured against. */
|
||||
readonly viewport?: {
|
||||
/** Content width in window pixels. */
|
||||
|
||||
Reference in New Issue
Block a user