diff --git a/packages/app/component-tests/browser-pane-restore.spec.ts b/packages/app/component-tests/browser-pane-restore.spec.ts index 100c235841d..d4aa5f675df 100644 --- a/packages/app/component-tests/browser-pane-restore.spec.ts +++ b/packages/app/component-tests/browser-pane-restore.spec.ts @@ -380,6 +380,23 @@ story("reloads the browser tab with ⌘R inside the pane and leaves it to the wi await expect(reloads).toHaveText("1") }) +story("adds a second new tab with ⌘T on a new tab page", async ({ page }) => { + const root = page.getByTestId("browser-region-fixture") + const mac = await page.evaluate(() => /Mac|iPhone|iPad|iPod/.test(navigator.platform)) + const address = root.getByRole("combobox", { name: "Browser address", exact: true }) + await expect(root.getByText("Registrations: 1", { exact: true })).toBeVisible() + await root.getByRole("button", { name: "Beta", exact: true }).click() + await expect(root.getByText("Registrations: 2", { exact: true })).toBeVisible() + await root.getByRole("button", { name: "Blank page", exact: true }).click() + await expect(root.getByRole("tab", { name: "New tab", exact: true })).toHaveAttribute("aria-selected", "true") + + await address.press(`${mac ? "Meta" : "Control"}+t`) + const tabs = root.getByRole("tab", { name: "New tab", exact: true }) + await expect(tabs).toHaveCount(2) + await expect(tabs.nth(1)).toHaveAttribute("aria-selected", "true") + await expect(root.getByTestId("browser-closes")).toHaveText("[]") +}) + story("reopens a closed browser tab with ⌘⇧T after the side panel and a session tab elsewhere", async ({ page }) => { const root = page.getByTestId("browser-region-fixture") const mac = await page.evaluate(() => /Mac|iPhone|iPad|iPod/.test(navigator.platform)) diff --git a/packages/gui-extensions/src/browser/chromium.ts b/packages/gui-extensions/src/browser/chromium.ts index f35b315ed7f..6206c7e9f4b 100644 --- a/packages/gui-extensions/src/browser/chromium.ts +++ b/packages/gui-extensions/src/browser/chromium.ts @@ -303,13 +303,15 @@ export function createBrowserPage( // through later navigations; blank and failed documents hide until a real one is ready. let content = false let failure: { url: string; message: string } | undefined + // A blank document is never shown, so its load does not count as loading: a new tab shows its new tab page at once. + let blank = options.initialize !== false && !options.restore?.url const state = (): Browser.Tab => { const page = { id: options.id, url: (failure?.url ?? contents.getURL()).slice(0, 16_384), title: contents.getTitle().slice(0, 2_048), - loading: contents.isLoading(), + loading: contents.isLoading() && !blank, } const history = { @@ -327,6 +329,7 @@ export function createBrowserPage( const reset = (event: Electron.Event<{ url: string; isMainFrame: boolean; isSameDocument: boolean }>) => { if (!event.isMainFrame || event.isSameDocument) return + blank = event.url === "about:blank" failure = undefined generation++ documents.clear() diff --git a/packages/gui-extensions/src/browser/model.ts b/packages/gui-extensions/src/browser/model.ts index 7a940872fda..4974652811e 100644 --- a/packages/gui-extensions/src/browser/model.ts +++ b/packages/gui-extensions/src/browser/model.ts @@ -302,25 +302,28 @@ export function createModel(ctx: SetupContext) { ) // A new tab holds the place of the tab the user picks next: once another tab is selected, it closes unless a - // page started loading in it. It leaves the strip in the same update, so the picked tab takes its place without - // waiting for the desktop to close its page. - createKeyed( - () => - state.attachments[id]?.browser?.tabs.find((item) => { - const placement = layout.state(key(item.id), ref) + // page started loading in it, or the pick is another new tab (mod+t on a new tab page adds one, as in a + // browser). It leaves the strip in the same update, so the picked tab takes its place without waiting for the + // desktop to close its page. + const shownBlank = () => + state.attachments[id]?.browser?.tabs.find((item) => { + const placement = layout.state(key(item.id), ref) - return blank(item) && (placement === "visible" || placement === "active") - })?.id, - (tabID) => - onCleanup(() => { - const item = state.attachments[id]?.browser?.tabs.find((candidate) => candidate.id === tabID) + return blank(item) && (placement === "visible" || placement === "active") + })?.id - if (live.get(id) !== entry || !item || !blank(item) || layout.state(key(tabID), ref) !== "open") return - layout.close(key(tabID), ref) + createKeyed(shownBlank, (tabID) => + onCleanup(() => { + const item = state.attachments[id]?.browser?.tabs.find((candidate) => candidate.id === tabID) - // The host calls the panel's `close` for the routed session only; another session's page closes here. - if (sessions.current()?.key !== ref.key) command(ref, { type: "tabs.close", tabID }) - }), + if (live.get(id) !== entry || !item || !blank(item) || layout.state(key(tabID), ref) !== "open") return + + if (shownBlank()) return + layout.close(key(tabID), ref) + + // The host calls the panel's `close` for the routed session only; another session's page closes here. + if (sessions.current()?.key !== ref.key) command(ref, { type: "tabs.close", tabID }) + }), ) // Previews made while another session was on screen open once the user returns to this one. diff --git a/packages/gui-extensions/src/browser/pane.ts b/packages/gui-extensions/src/browser/pane.ts index 078f78e5643..39ada847eaf 100644 --- a/packages/gui-extensions/src/browser/pane.ts +++ b/packages/gui-extensions/src/browser/pane.ts @@ -185,21 +185,21 @@ export function createBrowserPane(input: { rpc.result({ ...attachment, requestID, outcome: Schema.encodeSync(Browser.Outcome)(outcome) }, options), ) - // Report state before publishing it locally or completing a command. The server's copy of - // the inventory resolves every tab ID, so a state is retried until it arrives or the - // attachment ends; the results queued behind it then never name a tab the server lacks. - // "unavailable" means the server already dropped this attachment, which attach reports. + // The window hears every event at once; it never waits on the server, which can stall for + // hundreds of milliseconds while it serves the page's own traffic. The server's copy of the + // inventory resolves every tab ID, so a state is retried until it arrives or the attachment + // ends; the results queued behind it then never name a tab the server lacks. "unavailable" + // means the server already dropped this attachment, which attach reports. entry.report = (event) => { - const local = Effect.sync(() => publish(entry, event)) + publish(entry, event) - if (event.type !== "state") return send(local) + if (event.type !== "state") return send( rpc.state({ ...attachment, state: event.state ?? { tabs: [], focusedTabID: null } }, options).pipe( Effect.retry({ while: (error) => !("type" in error && error.type === "unavailable"), schedule: Schedule.min([Schedule.exponential("250 millis"), Schedule.spaced("10 seconds")]), }), - Effect.ensuring(local), ), ) } @@ -341,7 +341,11 @@ export function createBrowserPane(input: { const reason = url === undefined ? undefined : refusal(url, { fileRoots: entry.fileRoots }) if (reason) throw new Error(reason) - await execute(entry, { action: command, files: [] }, new AbortController().signal) + // A navigation that the next one replaced, such as a second submit or a redirect, rejects with ERR_ABORTED while + // the tab goes on loading. It is not a failed request; a page that fails to load reports its own error. + await execute(entry, { action: command, files: [] }, new AbortController().signal).catch((error: Error) => { + if (!("code" in error) || error.code !== "ERR_ABORTED") throw error + }) }, async close(window: number, binding: string) { const entry = owned(window, binding) @@ -494,8 +498,8 @@ export function createBrowserPane(input: { inspect: (event) => { if (entry.pages.has(id)) report(entry, { type: "inspect", tabID: id, ...event }) }, - // Icons and zoom are local to this desktop, but wait behind the states reported before them, so the window files - // a new page's icon under that page's URL. + // 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. detail: (value) => { if (entry.pages.has(id)) report(entry, { type: "page", tabID: id, ...value }) }, @@ -557,10 +561,10 @@ export function createBrowserPane(input: { if (action.focus !== false) focus(entry, page.state().id) await page.ready - await page.execute( - { action: { type: "navigate", tabID: page.state().id, url: action.url ?? "about:blank" }, files: [] }, - signal, - ) + + // A new page already loads about:blank, so only another address needs a navigation. + if (action.url !== undefined) + await page.execute({ action: { type: "navigate", tabID: page.state().id, url: action.url }, files: [] }, signal) publishState(entry) return { value: page.state(), files: [] } diff --git a/packages/gui-extensions/src/browser/panel.fixture.tsx b/packages/gui-extensions/src/browser/panel.fixture.tsx index b89b9ad50f8..8ad564aff7e 100644 --- a/packages/gui-extensions/src/browser/panel.fixture.tsx +++ b/packages/gui-extensions/src/browser/panel.fixture.tsx @@ -841,6 +841,17 @@ export function mountBrowserRegion(input: RegionHost) { const listeners = new Set<(value: { binding: string; event: PaneEvent }) => void>() + // The page main reports for a tab. + const tab = (id: Browser.TabID) => ({ + id, + url: store.blank ? "about:blank" : "http://localhost:4173/", + title: store.blank ? "" : "Preview", + loading: false, + canGoBack: false, + canGoForward: false, + generation: 0, + }) + const pane: PaneClient = { register: async (value) => { setStore("registrations", (items) => [ @@ -854,11 +865,19 @@ export function mountBrowserRegion(input: RegionHost) { if (value.command.type === "reload") setStore("reloads", (count) => count + 1) + // As main answers a new tab beside a blank page: the new blank tab joins the inventory and is selected. + if (value.command.type === "tabs.open" && !value.command.url && store.blank) { + const opened = Browser.TabID.make(`tab_44444444-4444-4444-4444-${String(store.opens).padStart(12, "0")}`) + const state = { tabs: [tab(tabID), tab(opened)], focusedTabID: opened } + listeners.forEach((listener) => listener({ binding: value.binding, event: { type: "state", state } })) + listeners.forEach((listener) => listener({ binding: value.binding, event: { type: "focus", tabID: opened } })) + } + if (value.command.type !== "tabs.close") return - const tabID = value.command.tabID + const closed = value.command.tabID - setStore("closes", (items) => [...items, tabID]) + setStore("closes", (items) => [...items, closed]) listeners.forEach((listener) => listener({ binding: value.binding, event: { type: "state", state: { tabs: [], focusedTabID: null } } }), ) @@ -909,18 +928,8 @@ export function mountBrowserRegion(input: RegionHost) { if (!binding) return - const tab = { - id: tabID, - url: store.blank ? "about:blank" : "http://localhost:4173/", - title: store.blank ? "" : "Preview", - loading: false, - canGoBack: false, - canGoForward: false, - generation: 0, - } - listeners.forEach((listener) => - listener({ binding, event: { type: "state", state: { tabs: [tab], focusedTabID: tabID } } }), + listener({ binding, event: { type: "state", state: { tabs: [tab(tabID)], focusedTabID: tabID } } }), ) }