Skip to content

Commit 36bfb85

Browse files
authored
fix(app): keep tab widths while closing tabs with the mouse (#66887)
1 parent 68ea328 commit 36bfb85

5 files changed

Lines changed: 407 additions & 0 deletions

File tree

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import { For, onCleanup } from "solid-js"
2+
import { createStore } from "solid-js/store"
3+
import { render } from "solid-js/web"
4+
import { Icon } from "@opencode/ui/icon"
5+
import { IconButton } from "@opencode/ui/icon-button"
6+
import { Tabs } from "@opencode/ui/tabs"
7+
import { createTabStripScroll } from "../src/runtime/plugin/tab-strip-scroll"
8+
import { createTabStripWidths } from "../src/runtime/plugin/tab-strip-widths"
9+
10+
const [state, setState] = createStore<{ titles: string[]; width: number }>({ titles: [], width: 0 })
11+
12+
const host = document.createElement("div")
13+
14+
/** A side pane tab strip built as `PaneTabs` builds it, with the host's tab styles, close buttons and sticky "+". */
15+
export function mountTabStrip(input: { titles: string[]; width: number; dir: "ltr" | "rtl" }) {
16+
setState({ titles: input.titles, width: input.width })
17+
host.dataset.testid = "tab-strip-fixture"
18+
host.dir = input.dir
19+
// Above the story, at a fixed place in the viewport, so pointer positions stay put.
20+
host.style.cssText = "position: fixed; top: 200px; left: 20px; z-index: 1000"
21+
document.body.appendChild(host)
22+
render(() => {
23+
const close = (title: string) => setState("titles", (titles) => titles.filter((item) => item !== title))
24+
25+
return (
26+
<aside id="review-panel" class="relative flex" style={{ width: `${state.width}px` }}>
27+
<Tabs value={state.titles[0] ?? ""}>
28+
<div class="session-review-v2-tabs-bar sticky top-0 shrink-0 flex items-center">
29+
<Tabs.List
30+
ref={(el: HTMLDivElement) => {
31+
onCleanup(createTabStripScroll({ el, lead: () => false }))
32+
onCleanup(createTabStripWidths(el))
33+
}}
34+
>
35+
<For each={state.titles}>
36+
{(title) => (
37+
<div data-pane-tab={title} class="h-full flex items-center">
38+
<div class="relative min-w-0">
39+
<Tabs.Trigger
40+
value={title}
41+
onMiddleClick={title.startsWith("pinned") ? undefined : () => close(title)}
42+
closeButton={
43+
<IconButton
44+
size="small"
45+
variant="ghost-muted"
46+
onPointerDown={(event) => {
47+
event.preventDefault()
48+
event.stopPropagation()
49+
}}
50+
onClick={(event) => {
51+
event.preventDefault()
52+
event.stopPropagation()
53+
close(title)
54+
}}
55+
icon={<Icon name="close" />}
56+
aria-label={`Close ${title}`}
57+
/>
58+
}
59+
hideCloseButton
60+
>
61+
<span class="truncate">{title}</span>
62+
</Tabs.Trigger>
63+
</div>
64+
</div>
65+
)}
66+
</For>
67+
<div class="h-full shrink-0 sticky end-0 z-10 flex items-center justify-center bg-v2-background-bg-base">
68+
<IconButton icon={<Icon name="plus" />} variant="ghost-muted" size="large" aria-label="Add tab" />
69+
</div>
70+
</Tabs.List>
71+
</div>
72+
</Tabs>
73+
</aside>
74+
)
75+
}, host)
76+
}
77+
78+
export const setTabStripWidth = (width: number) => setState("width", width)
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
import type { Page } from "@playwright/test"
2+
import { expect, sourceURL, story } from "../../stories/playwright/story"
3+
4+
const fixture = sourceURL(new URL("./tab-strip-widths.fixture.tsx", import.meta.url))
5+
6+
const long = (name: string) => `a-file-name-long-enough-to-truncate-${name}.md`
7+
8+
story.beforeEach(async ({ mount }) => {
9+
// Any story loads the app's styles; the fixture mounts the strip beside it.
10+
await mount("ui-line-comment--editor")
11+
})
12+
13+
async function mountStrip(page: Page, input: { titles: string[]; width: number; dir?: "ltr" | "rtl" }) {
14+
await page.evaluate(
15+
async ({ fixture, input }) => {
16+
const { mountTabStrip } = await import(fixture)
17+
mountTabStrip({ dir: "ltr", ...input })
18+
await document.fonts.ready
19+
},
20+
{ fixture, input },
21+
)
22+
await expect(strip(page).locator("[data-pane-tab]")).toHaveCount(input.titles.length)
23+
}
24+
25+
const strip = (page: Page) => page.getByTestId("tab-strip-fixture").locator('[data-slot="tabs-list"]')
26+
27+
const closeButton = (page: Page, title: string) =>
28+
strip(page).locator(`[data-pane-tab="${title}"] [data-slot="tabs-trigger-close-button"]`)
29+
30+
async function center(page: Page, title: string) {
31+
const box = (await closeButton(page, title).boundingBox())!
32+
33+
return { x: box.x + box.width / 2, y: box.y + box.height / 2 }
34+
}
35+
36+
// The tab whose close button is at the point.
37+
const under = (page: Page, point: { x: number; y: number }) =>
38+
page.evaluate(
39+
(point) =>
40+
document
41+
.elementFromPoint(point.x, point.y)
42+
?.closest('[data-slot="tabs-trigger-close-button"]')
43+
?.closest<HTMLElement>("[data-pane-tab]")?.dataset.paneTab,
44+
point,
45+
)
46+
47+
const hold = (page: Page) => strip(page).evaluate((el) => el.style.paddingInlineEnd)
48+
49+
const tabWidth = async (page: Page, title: string) =>
50+
(await strip(page).locator(`[data-pane-tab="${title}"]`).boundingBox())!.width
51+
52+
story(
53+
"a mouse close keeps the next close button under the pointer until the pointer leaves the strip",
54+
async ({ page }) => {
55+
const titles = [long("alpha"), "a.md", long("beta"), long("gamma"), "b.md", long("delta")]
56+
await mountStrip(page, { titles, width: 900 })
57+
const width = await tabWidth(page, long("gamma"))
58+
59+
const middle = await center(page, long("beta"))
60+
await page.mouse.click(middle.x, middle.y)
61+
await expect(strip(page).locator(`[data-pane-tab="${long("beta")}"]`)).toHaveCount(0)
62+
await expect.poll(() => under(page, middle)).toBe(long("gamma"))
63+
64+
// The short tab before the last one keeps its width, so the long ones fill the closed tab's place.
65+
const last = await center(page, long("delta"))
66+
await page.mouse.click(last.x, last.y)
67+
await expect(strip(page).locator(`[data-pane-tab="${long("delta")}"]`)).toHaveCount(0)
68+
await expect.poll(() => under(page, last)).toBe("b.md")
69+
70+
// The "+" stays a gap after the last tab, and the held strip shows no end fade.
71+
const gap = await strip(page).evaluate((el) => {
72+
const plus = el.lastElementChild!.getBoundingClientRect()
73+
const tab = el.querySelector('[data-pane-tab="b.md"]')!.getBoundingClientRect()
74+
75+
return Math.round(plus.left - tab.right)
76+
})
77+
78+
expect(gap).toBe(8)
79+
expect(await strip(page).evaluate((el) => el.style.getPropertyValue("--tab-strip-fade-end"))).toBe("0px")
80+
81+
await page.mouse.move(middle.x, middle.y + 200)
82+
await expect.poll(() => hold(page)).toBe("")
83+
await expect.poll(() => tabWidth(page, long("gamma"))).toBeGreaterThan(width + 1)
84+
},
85+
)
86+
87+
story("a middle click close holds the strip, and resizing the strip releases it", async ({ page }) => {
88+
const titles = [long("alpha"), long("beta"), long("gamma"), long("delta")]
89+
await mountStrip(page, { titles, width: 700 })
90+
const width = await tabWidth(page, long("gamma"))
91+
92+
await strip(page)
93+
.locator(`[data-pane-tab="${long("beta")}"] [role="tab"]`)
94+
.click({ button: "middle" })
95+
await expect(strip(page).locator(`[data-pane-tab="${long("beta")}"]`)).toHaveCount(0)
96+
await expect.poll(() => tabWidth(page, long("gamma"))).toBeCloseTo(width, 0)
97+
98+
// Held, the three tabs would shrink with the strip; released, they share the whole of it.
99+
await page.evaluate(async (fixture) => {
100+
const { setTabStripWidth } = await import(fixture)
101+
setTabStripWidth(650)
102+
}, fixture)
103+
await expect.poll(() => tabWidth(page, long("gamma"))).toBeGreaterThan(width + 1)
104+
expect(await hold(page)).toBe("")
105+
})
106+
107+
story("closing from the keyboard, or a middle click that closes nothing, resizes the tabs", async ({ page }) => {
108+
const titles = [long("alpha"), "pinned.md", long("beta"), long("gamma")]
109+
await mountStrip(page, { titles, width: 700 })
110+
const width = await tabWidth(page, long("gamma"))
111+
112+
// The middle click on a tab without a middle-click close arms nothing for the keyboard close that follows.
113+
await strip(page).locator('[data-pane-tab="pinned.md"] [role="tab"]').click({ button: "middle" })
114+
await closeButton(page, "pinned.md").getByRole("button").focus()
115+
await page.keyboard.press("Enter")
116+
await expect(strip(page).locator('[data-pane-tab="pinned.md"]')).toHaveCount(0)
117+
await expect.poll(() => tabWidth(page, long("gamma"))).toBeGreaterThan(width + 1)
118+
119+
const next = await tabWidth(page, long("gamma"))
120+
await closeButton(page, long("alpha")).getByRole("button").focus()
121+
await page.keyboard.press("Enter")
122+
await expect(strip(page).locator(`[data-pane-tab="${long("alpha")}"]`)).toHaveCount(0)
123+
await expect.poll(() => tabWidth(page, long("gamma"))).toBeGreaterThan(next + 1)
124+
})
125+
126+
story("in a right-to-left strip the next close button slides under the pointer", async ({ page }) => {
127+
const titles = [long("alpha"), long("beta"), long("gamma"), long("delta")]
128+
await mountStrip(page, { titles, width: 700, dir: "rtl" })
129+
130+
const middle = await center(page, long("beta"))
131+
await page.mouse.click(middle.x, middle.y)
132+
await expect(strip(page).locator(`[data-pane-tab="${long("beta")}"]`)).toHaveCount(0)
133+
await expect.poll(() => under(page, middle)).toBe(long("gamma"))
134+
})
135+
136+
story(
137+
"an overflowing strip is never held, so closing its last tab keeps the other tabs at their width",
138+
async ({ page }) => {
139+
const titles = Array.from({ length: 8 }, (_, index) => long(String(index)))
140+
await mountStrip(page, { titles, width: 400 })
141+
expect(await strip(page).evaluate((el) => el.scrollWidth > el.clientWidth)).toBe(true)
142+
const width = await tabWidth(page, titles[6])
143+
144+
await strip(page).evaluate((el) => (el.scrollLeft = el.scrollWidth))
145+
const last = await center(page, titles[7])
146+
await page.mouse.click(last.x, last.y)
147+
await expect(strip(page).locator("[data-pane-tab]")).toHaveCount(7)
148+
expect(await hold(page)).toBe("")
149+
expect(await tabWidth(page, titles[6])).toBeCloseTo(width, 0)
150+
},
151+
)
152+
153+
story.describe("touch", () => {
154+
story.use({ hasTouch: true })
155+
156+
story("a tap on a close button never holds the strip", async ({ page }) => {
157+
const titles = [long("alpha"), long("beta"), long("gamma"), long("delta")]
158+
await mountStrip(page, { titles, width: 700 })
159+
const width = await tabWidth(page, long("gamma"))
160+
161+
await closeButton(page, long("beta")).getByRole("button").tap()
162+
await expect(strip(page).locator(`[data-pane-tab="${long("beta")}"]`)).toHaveCount(0)
163+
await expect.poll(() => tabWidth(page, long("gamma"))).toBeGreaterThan(width + 1)
164+
})
165+
})

‎packages/app/e2e/regression/side-panels.spec.ts‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,74 @@ test("tabs in a narrow side strip shrink to keep their close buttons, and a tab'
275275
await expect(side.getByRole("tab", { name: "a.md", exact: true })).toHaveCount(0)
276276
})
277277

278+
test("closing side tabs with the mouse keeps the next close button under the pointer until it leaves the strip", async ({
279+
page,
280+
}) => {
281+
// Wide enough for every tab, narrow enough that they shrink below their titles.
282+
await page.setViewportSize({ width: 1600, height: 700 })
283+
const directory = "C:/OpenCode/CloseWidths"
284+
const key = `local\u0000${base64Encode(directory)}/ses_closewidths`
285+
// A short tab before the last one keeps its own width while the long ones fill the strip.
286+
const files = [...Array.from({ length: 6 }, (_, index) => `notes-file-${index}.md`), "b.md", "notes-file-7.md"]
287+
288+
await openSession(page, {
289+
name: "CloseWidths",
290+
fileList: () => [],
291+
fileContent: (path) => ({ type: "text", content: `contents:${path}` }),
292+
seed: {
293+
panes: { ses_closewidths: { review: true, sessionWidth: 640 } },
294+
storage: {
295+
"opencode.global.dat:layout": {
296+
sessionTabs: { [key]: { all: files.map((file) => `file://${file}`), active: `file://${files[0]}` } },
297+
},
298+
},
299+
},
300+
})
301+
302+
const side = page.locator('[data-pane-content="side"]')
303+
const tabs = side.locator('[data-pane-strip="side"] [data-pane-tab]')
304+
305+
const close = (file: string) =>
306+
side.locator(`[data-pane-tab="file://${file}"] [data-slot="tabs-trigger-close-button"]`)
307+
308+
const center = async (file: string) => {
309+
const box = (await close(file).boundingBox())!
310+
311+
return { x: box.x + box.width / 2, y: box.y + box.height / 2 }
312+
}
313+
314+
const under = (point: { x: number; y: number }) =>
315+
page.evaluate(
316+
(point) =>
317+
document
318+
.elementFromPoint(point.x, point.y)
319+
?.closest('[data-slot="tabs-trigger-close-button"]')
320+
?.closest<HTMLElement>("[data-pane-tab]")?.dataset.paneTab,
321+
point,
322+
)
323+
324+
await expect(tabs).toHaveCount(files.length)
325+
await expect(side.getByRole("tab", { name: files[0], exact: true })).toHaveAttribute("aria-selected", "true")
326+
await page.evaluate(() => document.fonts.ready.then(() => undefined))
327+
const width = (await tabs.nth(1).boundingBox())!.width
328+
329+
// The next tab slides under the pointer at its old width.
330+
const middle = await center(files[2])
331+
await page.mouse.click(middle.x, middle.y)
332+
await expect(tabs).toHaveCount(files.length - 1)
333+
await expect.poll(() => under(middle)).toBe(`file://${files[3]}`)
334+
335+
// Closing the last tab brings the one before it under the pointer.
336+
const last = await center(files[7])
337+
await page.mouse.click(last.x, last.y)
338+
await expect(tabs).toHaveCount(files.length - 2)
339+
await expect.poll(() => under(last)).toBe("file://b.md")
340+
341+
// Once the pointer leaves the strip, the tabs fill it again.
342+
await page.mouse.move(middle.x, middle.y + 200)
343+
await expect.poll(async () => (await tabs.nth(1).boundingBox())!.width).toBeGreaterThan(width + 1)
344+
})
345+
278346
test("a reopened side tab returns to where it closed", async ({ page }) => {
279347
await page.setViewportSize({ width: 1440, height: 900 })
280348
const directory = "C:/OpenCode/ReopenPlace"

‎packages/app/src/runtime/plugin/side-region.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import { createSessionPanelMenuEntries, type SessionPanelMenuEntry } from "./pan
2929
import { RegionContent, type Region, type RegionPane } from "./panels"
3030
import { PluginSlot } from "./render"
3131
import { createTabStripScroll } from "./tab-strip-scroll"
32+
import { createTabStripWidths } from "./tab-strip-widths"
3233
import { closedTabsModifier, type ClosedSideTab } from "./closed-tabs"
3334
import { ClosedTabsSection, createDeferredMenu, NewTabButton } from "./closed-tabs-view"
3435
import { selectDeferredMenuOpen } from "./deferred-menu-machine"
@@ -310,6 +311,7 @@ export function PaneTabs(props: {
310311
capture: true,
311312
})
312313
onCleanup(createTabStripScroll({ el, lead: props.pane.lead }))
314+
onCleanup(createTabStripWidths(el))
313315
}}
314316
onContextMenu={(event: MouseEvent) => {
315317
const key =

0 commit comments

Comments
 (0)