Skip to content

Commit b66f01b

Browse files
authored
feat(tui): prototype tab scroll memory (#42375)
1 parent d2b8cb8 commit b66f01b

3 files changed

Lines changed: 117 additions & 44 deletions

File tree

‎packages/tui/src/component/dialog-experiments.tsx‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@ type Experiment = {
1313
// In-flight features anyone can opt into. Each entry is temporary: an
1414
// experiment either graduates (delete the entry, make the behavior
1515
// unconditional) or dies (delete the entry and the branch it gated).
16-
export const experiments: Experiment[] = []
16+
export const experiments: Experiment[] = [
17+
{
18+
id: "tab_scroll",
19+
title: "Remember tab scroll",
20+
description: "Keep each open tab's reading position and show a shortcut back to the bottom.",
21+
},
22+
]
1723

1824
export function DialogExperiments() {
1925
const config = useConfig()
@@ -27,7 +33,6 @@ export function DialogExperiments() {
2733
const options = createMemo(() =>
2834
experiments.map((experiment) => ({
2935
title: experiment.title,
30-
category: "Experiments",
3136
searchText: experiment.description,
3237
footer: enabled(experiment) ? "on" : "off",
3338
value: experiment,

‎packages/tui/src/context/session-tabs.tsx‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ export const { use: useSessionTabs, provider: SessionTabsProvider } = createSimp
6666
let history: SessionTabHistory = { entries: [], index: -1 }
6767
// User-closed tabs eligible for reopening; in-memory like history, deleted sessions pruned.
6868
let closedTabs: ClosedSessionTab[] = []
69+
const scrollPositions = new Map<string, number>()
70+
71+
createEffect(() => {
72+
if (config.experimental?.tab_scroll === true) return
73+
scrollPositions.clear()
74+
})
6975

7076
function state() {
7177
if (config.tabs.scope === "cwd") return store.cwd[paths.cwd] ?? fallback
@@ -231,6 +237,7 @@ export const { use: useSessionTabs, provider: SessionTabsProvider } = createSimp
231237

232238
function remove(sessionID: string, navigate: boolean) {
233239
const target = root(sessionID)
240+
scrollPositions.delete(target)
234241
const closed = closeSessionTab(state().tabs, target)
235242
const selected = navigate && current() === target
236243
if (closed.tabs === state().tabs && !selected) return
@@ -262,6 +269,19 @@ export const { use: useSessionTabs, provider: SessionTabsProvider } = createSimp
262269
},
263270
current,
264271
status,
272+
scrollPosition(sessionID: string) {
273+
const target = root(sessionID)
274+
if (!state().tabs.some((tab) => tab.sessionID === target)) return
275+
return scrollPositions.get(target)
276+
},
277+
setScrollPosition(sessionID: string, position: number | undefined) {
278+
const target = root(sessionID)
279+
if (position === undefined || !state().tabs.some((tab) => tab.sessionID === target)) {
280+
scrollPositions.delete(target)
281+
return
282+
}
283+
scrollPositions.set(target, position)
284+
},
265285
select(sessionID: string) {
266286
if (!enabled()) return
267287
route.navigate({ type: "session", sessionID: root(sessionID) })

‎packages/tui/src/routes/session/index.tsx‎

Lines changed: 90 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ export function Session() {
274274
const [navigationSlack, setNavigationSlack] = createSignal(0)
275275
const [synced, setSynced] = createSignal(false)
276276
const sessionTabs = useSessionTabs()
277+
const [awayFromBottom, setAwayFromBottom] = createSignal(false)
277278

278279
const clearMessageNavigation = () => {
279280
setNavigationSlack(0)
@@ -319,7 +320,7 @@ export function Session() {
319320
return
320321
}
321322
editor.reconnect(info.location.directory)
322-
if (route.sessionID === sessionID && scroll) scroll.scrollBy(100_000)
323+
if (route.sessionID === sessionID && scroll) restoreScrollPosition(sessionID)
323324
setSynced(true)
324325
})().catch((error) => {
325326
if (route.sessionID !== sessionID) return
@@ -335,6 +336,13 @@ export function Session() {
335336
let seeded = false
336337
let sent = false
337338
let scroll: ScrollBoxRenderable
339+
onCleanup(() => {
340+
if (!scroll || scroll.isDestroyed) return
341+
sessionTabs.setScrollPosition(
342+
route.sessionID,
343+
config.experimental?.tab_scroll === true && isAwayFromBottom() ? scroll.scrollTop : undefined,
344+
)
345+
})
338346
const [prompt, setPrompt] = createSignal<PromptRef>()
339347
const bind = (r: PromptRef | undefined) => {
340348
setPrompt(r)
@@ -387,6 +395,31 @@ export function Session() {
387395
afterLayout(continuation)
388396
}
389397

398+
function isAwayFromBottom() {
399+
return scroll.scrollTop < Math.max(0, scroll.scrollHeight - scroll.viewport.height) - 1
400+
}
401+
function updateAwayFromBottom() {
402+
if (config.experimental?.tab_scroll !== true) return
403+
setTimeout(() => {
404+
if (!scroll || scroll.isDestroyed) return
405+
const away = isAwayFromBottom()
406+
setAwayFromBottom(away)
407+
if (!away) sessionTabs.setScrollPosition(route.sessionID, undefined)
408+
})
409+
}
410+
function restoreScrollPosition(sessionID: string) {
411+
const position = config.experimental?.tab_scroll === true ? sessionTabs.scrollPosition(sessionID) : undefined
412+
if (position === undefined) {
413+
scroll.scrollTo(scroll.scrollHeight)
414+
setAwayFromBottom(false)
415+
return
416+
}
417+
ensureAllRows(() => {
418+
scroll.scrollTo(position)
419+
updateAwayFromBottom()
420+
})
421+
}
422+
390423
createEffect(() => {
391424
const current = prompt()
392425
if (sent || !current || !synced() || !local.model.ready || !local.model.catalogReady) return
@@ -495,6 +528,8 @@ export function Session() {
495528

496529
function toBottom() {
497530
clearMessageNavigation()
531+
setAwayFromBottom(false)
532+
sessionTabs.setScrollPosition(route.sessionID, undefined)
498533
setTimeout(() => {
499534
if (!scroll || scroll.isDestroyed) return
500535
scroll.scrollTo(scroll.scrollHeight)
@@ -510,6 +545,7 @@ export function Session() {
510545
run: () => {
511546
clearMessageNavigation()
512547
scroll.scrollBy(-scroll.height / 2)
548+
updateAwayFromBottom()
513549
dialog.clear()
514550
},
515551
},
@@ -521,6 +557,7 @@ export function Session() {
521557
run: () => {
522558
clearMessageNavigation()
523559
scroll.scrollBy(scroll.height / 2)
560+
updateAwayFromBottom()
524561
dialog.clear()
525562
},
526563
},
@@ -532,6 +569,7 @@ export function Session() {
532569
run: () => {
533570
clearMessageNavigation()
534571
scroll.scrollBy(-1)
572+
updateAwayFromBottom()
535573
dialog.clear()
536574
},
537575
},
@@ -543,6 +581,7 @@ export function Session() {
543581
run: () => {
544582
clearMessageNavigation()
545583
scroll.scrollBy(1)
584+
updateAwayFromBottom()
546585
dialog.clear()
547586
},
548587
},
@@ -554,6 +593,7 @@ export function Session() {
554593
run: () => {
555594
clearMessageNavigation()
556595
scroll.scrollBy(-scroll.height / 4)
596+
updateAwayFromBottom()
557597
dialog.clear()
558598
},
559599
},
@@ -565,6 +605,7 @@ export function Session() {
565605
run: () => {
566606
clearMessageNavigation()
567607
scroll.scrollBy(scroll.height / 4)
608+
updateAwayFromBottom()
568609
dialog.clear()
569610
},
570611
},
@@ -579,6 +620,7 @@ export function Session() {
579620
run: () => {
580621
clearMessageNavigation()
581622
scroll.scrollTo(0)
623+
updateAwayFromBottom()
582624
dialog.clear()
583625
},
584626
},
@@ -588,8 +630,7 @@ export function Session() {
588630
group: "Session",
589631
palette: undefined,
590632
run: () => {
591-
clearMessageNavigation()
592-
scroll.scrollTo(scroll.scrollHeight)
633+
toBottom()
593634
dialog.clear()
594635
},
595636
},
@@ -1006,8 +1047,6 @@ export function Session() {
10061047
bindings: [...baseAndUnfocusedCommands, ...baseCommands()].map((command) => command.id),
10071048
}))
10081049

1009-
// snap to bottom when session changes
1010-
createEffect(on(() => route.sessionID, toBottom))
10111050
createEffect(
10121051
on(
10131052
() => route.sessionID,
@@ -1043,47 +1082,56 @@ export function Session() {
10431082
paddingBottom={1}
10441083
paddingLeft={dimensions().width < 44 ? 1 : 2}
10451084
paddingRight={dimensions().width < 44 ? 1 : 2}
1046-
gap={1}
10471085
>
10481086
<Show when={session()}>
1049-
<scrollbox
1050-
ref={(r) => (scroll = r)}
1051-
viewportOptions={{
1052-
paddingRight: showScrollbar() ? 1 : 0,
1053-
}}
1054-
verticalScrollbarOptions={{
1055-
paddingLeft: 1,
1056-
visible: showScrollbar(),
1057-
trackOptions: {
1058-
backgroundColor: theme.raise(theme.background.surface.offset),
1059-
foregroundColor: theme.border.default,
1060-
},
1061-
}}
1062-
stickyScroll={!navigationMessage()}
1063-
stickyStart="bottom"
1064-
flexGrow={1}
1065-
scrollAcceleration={scrollAcceleration()}
1066-
>
1067-
<For each={visibleRows()}>
1068-
{(row, index) => (
1069-
<SessionRowView
1070-
row={row}
1071-
message={(messageID) => data.session.message.get(route.sessionID, messageID)}
1072-
boundaryID={boundaries()[index() + hidden()]}
1087+
<box flexGrow={1} minHeight={0} position="relative">
1088+
<scrollbox
1089+
ref={(r) => (scroll = r)}
1090+
viewportOptions={{
1091+
paddingRight: showScrollbar() ? 1 : 0,
1092+
}}
1093+
verticalScrollbarOptions={{
1094+
paddingLeft: 1,
1095+
visible: showScrollbar(),
1096+
trackOptions: {
1097+
backgroundColor: theme.raise(theme.background.surface.offset),
1098+
foregroundColor: theme.border.default,
1099+
},
1100+
}}
1101+
stickyScroll={!navigationMessage()}
1102+
stickyStart="bottom"
1103+
flexGrow={1}
1104+
scrollAcceleration={scrollAcceleration()}
1105+
onMouseScroll={updateAwayFromBottom}
1106+
>
1107+
<For each={visibleRows()}>
1108+
{(row, index) => (
1109+
<SessionRowView
1110+
row={row}
1111+
message={(messageID) => data.session.message.get(route.sessionID, messageID)}
1112+
boundaryID={boundaries()[index() + hidden()]}
1113+
/>
1114+
)}
1115+
</For>
1116+
<BackgroundToolHint messages={messages()} />
1117+
<Show when={session()?.revert?.messageID}>
1118+
<RevertMessage
1119+
count={messagesFromRevert().filter((message) => message.type === "user").length}
1120+
files={session()!.revert!.files ?? []}
10731121
/>
1074-
)}
1075-
</For>
1076-
<BackgroundToolHint messages={messages()} />
1077-
<Show when={session()?.revert?.messageID}>
1078-
<RevertMessage
1079-
count={messagesFromRevert().filter((message) => message.type === "user").length}
1080-
files={session()!.revert!.files ?? []}
1081-
/>
1082-
</Show>
1083-
<Show when={navigationSlack()}>
1084-
{(height) => <box id={NAVIGATION_SLACK_ID} height={height()} flexShrink={0} />}
1122+
</Show>
1123+
<Show when={navigationSlack()}>
1124+
{(height) => <box id={NAVIGATION_SLACK_ID} height={height()} flexShrink={0} />}
1125+
</Show>
1126+
</scrollbox>
1127+
</box>
1128+
<box height={1} flexShrink={0} flexDirection="row" justifyContent="flex-end">
1129+
<Show when={config.experimental?.tab_scroll === true && awayFromBottom()}>
1130+
<text fg={theme.text.subdued} onMouseUp={toBottom}>
1131+
Latest ↓
1132+
</text>
10851133
</Show>
1086-
</scrollbox>
1134+
</box>
10871135
<box flexShrink={0}>
10881136
<Show when={!composer.open && !disabled() && queuedPrompts().length > 0}>
10891137
<QueuedPromptDock prompts={queuedPrompts()} onOpen={openQueuedPrompts} />

0 commit comments

Comments
 (0)