fix(cli-rs): keep the shown session when its tab is picked again

Clicking the open tab, cycling a single tab, or picking the current
session in the session list, the open dialog, a session link or a
tui.session.select event called Tui::navigate with the route already
shown. navigate treats that as a remount (theme and plugin changes rely
on it), so the transcript mounted again at its end and then restored
its scroll anchor: a flicker on every click.

TS's route.navigate reconciles the route store, so the same session is
a no-op. Tui::show_session does the same for the user's picks; the
deliberate remounts keep calling navigate.
This commit is contained in:
Simon Klee
2026-11-18 21:12:40 +01:00
Unverified
parent ff1de3ca62
commit dc60f4ac42
8 changed files with 66 additions and 12 deletions
+11
View File
@@ -182,6 +182,17 @@ impl Tui {
self.theme.borrow().theme().page()
}
/// Shows a session the user picked (a tab, the session list, a link). TS's `route.navigate` reconciles the route
/// store, so the session on screen keeps its view, its scroll and its prompt; [`Tui::navigate`] to the route shown
/// mounts it again, which only a theme or plugin change wants.
pub fn show_session(&self, id: SessionID) {
let route = Route::Session { id };
if self.view.borrow().is_some() && self.route() == route {
return;
}
self.navigate(route);
}
pub fn navigate(&self, route: Route) {
// Going home from home to another location is a navigation, not a remount.
let remount =
@@ -749,7 +749,7 @@ impl Open {
tui.app.defer(bind!(tui, |tui, _| {
tui.dialogs.clear();
match target {
Target::Session(id) => tui.navigate(Route::Session { id }),
Target::Session(id) => tui.show_session(id),
Target::Project { directory, .. } => {
crate::features::location::navigate_home(&tui, Some(opencode_store::Location::new(directory)))
}
@@ -74,7 +74,7 @@ pub fn show(tui: &Tui) {
app.defer(bind!(tui, |tui, _| {
// Closed first, so the new view takes the focus.
tui.dialogs.clear();
tui.navigate(Route::Session { id });
tui.show_session(id);
}));
})));
let tabs = list.tui.upgrade().is_some_and(|tui| tui.config().tabs.enabled);
@@ -233,7 +233,7 @@ fn handle(tui: &Tui, event: &Handled) {
tui.local.load_usage(true);
}
Handled::TuiSessionSelect(event) if here(tui, event.location.as_ref()) => {
tui.navigate(Route::Session { id: event.data.session_id.clone() });
tui.show_session(event.data.session_id.clone());
}
Handled::SessionDeleted(event) => {
let Route::Session { id } = tui.route() else { return };
@@ -30,10 +30,7 @@ pub fn install(tui: &Rc<Tui>) {
/// `quickSwitch`: opens the session in `slot` (1-based), unless it is open already.
pub fn quick_switch(tui: &Tui, slot: usize) {
let Some(target) = pins::slots(tui).into_iter().nth(slot - 1) else { return };
if tui.route() == (Route::Session { id: target.clone() }) {
return;
}
tui.navigate(Route::Session { id: target });
tui.show_session(target);
}
pub(crate) fn event(tui: &Tui, event: &Handled) {
@@ -41,7 +41,7 @@ pub fn attach(tui: &Tui, markdown: &Markdown, renderers: &CodeBlockRenderers) ->
if tui.dialogs.is_open() {
tui.dialogs.clear();
}
tui.navigate(Route::Session { id: SessionID(id) });
tui.show_session(SessionID(id));
}));
}
});
+3 -4
View File
@@ -274,7 +274,7 @@ fn install_commands(tui: &Rc<Tui>) {
false,
Rc::new(move |tui| {
if let Some(tab) = tui.tabs.tabs(tui).get(index) {
tui.navigate(Route::Session { id: SessionID(tab.session_id.clone()) });
tui.show_session(SessionID(tab.session_id.clone()));
}
}),
)
@@ -568,8 +568,7 @@ pub fn select(tui: &Tui, session: &str) {
if !Tabs::enabled(tui) {
return;
}
let root = tui.data.read(|state| state.root(&SessionID(session.to_owned())));
tui.navigate(Route::Session { id: root });
tui.show_session(tui.data.read(|state| state.root(&SessionID(session.to_owned()))));
}
/// `open`: a tab for the session's root, without showing it.
@@ -659,7 +658,7 @@ fn cycle(tui: &Tui, forward: bool, unread: bool) {
})
.map(|tab| tab.session_id.clone());
if let Some(session) = target {
tui.navigate(Route::Session { id: SessionID(session) });
tui.show_session(SessionID(session));
}
}
@@ -448,6 +448,53 @@ fn a_busy_tab_paints_on_its_own_and_stops_when_idle() {
}
}
// `tabs.select` is `route.navigate`, a reconcile of the route store: the session already shown stays as it is. A
// remount scrolled the transcript to its end for a frame, then back to the row it was read at (a flicker).
#[test]
fn clicking_the_open_tab_leaves_the_transcript_as_it_is() {
let mut routes = routes();
let messages: Vec<Value> = (1..=40)
.rev()
.map(|n| json!({ "id": format!("msg_{n:02}"), "type": "user", "text": format!("Line {n}"), "time": { "created": n } }))
.collect();
let path = "/api/session/ses_a/message";
routes.insert(0, MemoryRoute::json("GET", path, 200, &json!({ "data": messages, "cursor": {} })));
let home = opencode_util::temp::tempdir().unwrap();
let paths = support::home_paths(home.path());
let config = json!({ "animations": false, "session": { "sidebar": "hide" }, "tabs": { "layout": "vertical" } });
let deps = Deps {
config: support::config(support::decode(&config.to_string())),
roots: support::roots(paths, CHANNEL),
..support::deps(Arc::new(Memory::new(routes)))
};
let h = support::mount(120, 30, deps);
h.wait_for("Line 40");
for _ in 0..5 {
h.app.scroll(80, 10, false);
}
let tab = || {
let text = h.app.render().text();
let (row, line) = text.lines().enumerate().find(|(_, line)| line.contains("Fix the parser")).unwrap();
(line[..line.find("Fix the parser").unwrap()].chars().count() as u16 + 2, row as u16)
};
let (x, y) = tab();
h.app.hover(x, y);
let before = h.app.render().text();
assert!(!before.contains("Line 40"), "not scrolled up:\n{before}");
h.app.click(x, y);
let mut painted = Vec::new();
for _ in 0..500 {
h.app.advance(std::time::Duration::from_millis(1));
if h.app.needs_frame() {
painted.push(h.app.render().text());
}
}
painted.push(h.app.render().text());
let changed = painted.iter().find(|text| **text != before);
assert!(changed.is_none(), "before:\n{before}\nafter:\n{}", changed.unwrap());
}
/// The frames painted over 8 s without input, one each time the app asks for one, as `App::event_loop` does, in
/// milliseconds from the first. The requests in flight land first: their replies are output, not idle.
fn frames(h: &Tabs) -> Vec<u128> {