From 1eece51bb1c956fefdb802e48627bc23cf669c43 Mon Sep 17 00:00:00 2001 From: Simon Klee Date: Wed, 4 Nov 2026 22:40:36 +0100 Subject: [PATCH] fix(cli-rs): dispose session view keymap layers LocationMissing, the scroll controls and the transcript dropped their Disposer handles, which keeps the layers registered. After a missing location session, Enter on home opened "Choose directory", and session commands stayed in the palette. Each view now disposes its layers when it replaces or drops them (carbon-2). --- .../crates/tui/src/routes/location_missing.rs | 22 +++++++++++++++++-- .../tui/src/routes/session/messages/list.rs | 18 ++++++++++++++- .../crates/tui/src/routes/session/scroll.rs | 17 +++++++++++++- 3 files changed, 53 insertions(+), 4 deletions(-) diff --git a/packages/cli-rs/crates/tui/src/routes/location_missing.rs b/packages/cli-rs/crates/tui/src/routes/location_missing.rs index 416dc8f97e6..3e164774a3f 100644 --- a/packages/cli-rs/crates/tui/src/routes/location_missing.rs +++ b/packages/cli-rs/crates/tui/src/routes/location_missing.rs @@ -6,7 +6,7 @@ //! beside its prompt and hides the prompt while [`LocationMissing::active`]. use std::cell::{Cell, RefCell}; -use std::rc::Rc; +use std::rc::{Rc, Weak}; use std::time::Duration; use opencode_api::client::RequestOptions; @@ -22,6 +22,7 @@ use crate::app::Tui; use crate::dialogs::worktrees::Selection; pub struct LocationMissing { + tui: Weak, session: SessionID, root: View, active: Cell, @@ -36,6 +37,7 @@ impl LocationMissing { .max_height(15) .visible(false); Self { + tui: tui.weak(), session: session.clone(), root, active: Cell::new(false), @@ -67,7 +69,7 @@ impl LocationMissing { } self.active.set(missing.is_some()); self.root.clear(); - self.layer.take(); + dispose(tui, self.layer.take()); let Some(directory) = missing else { self.root.visible(false); return; @@ -97,6 +99,22 @@ impl LocationMissing { } } +impl Drop for LocationMissing { + fn drop(&mut self) { + if let Some(tui) = self.tui.upgrade() { + dispose(&tui, self.layer.take()); + } + } +} + +/// Unregisters the `return` layer. A command may drop the view while the keymap runs it, so the layer goes +/// afterwards; dropping a disposer does not unregister it. +fn dispose(tui: &Tui, layer: Option) { + if let Some(layer) = layer { + tui.app.defer(move |_| layer.dispose()); + } +} + /// `SessionLocationUnavailable`: `SessionQuestion` with the directory, the explanation, and one action. fn view(tui: &Tui, session: &SessionID, directory: &str) -> View { let app = &tui.app; diff --git a/packages/cli-rs/crates/tui/src/routes/session/messages/list.rs b/packages/cli-rs/crates/tui/src/routes/session/messages/list.rs index 737c08ce1aa..e5196597c05 100644 --- a/packages/cli-rs/crates/tui/src/routes/session/messages/list.rs +++ b/packages/cli-rs/crates/tui/src/routes/session/messages/list.rs @@ -80,6 +80,22 @@ struct Inner { layer: RefCell>, } +impl Drop for Inner { + fn drop(&mut self) { + if let Some(tui) = self.tui.upgrade() { + dispose(&tui, self.layer.take()); + } + } +} + +/// Unregisters the transcript's layer. A command may drop the view while the keymap runs it, so the layer goes +/// afterwards; dropping a disposer does not unregister it. +fn dispose(tui: &Tui, layer: Option) { + if let Some(layer) = layer { + tui.app.defer(move |_| layer.dispose()); + } +} + /// The content height before rows mounted above, and the frame it is from. struct Pin { height: f32, @@ -407,7 +423,7 @@ impl Inner { }; inner.hide_thinking.set(!inner.hide_thinking.get()); inner.render(&tui, |_| true); - inner.layer.take(); + dispose(&tui, inner.layer.take()); inner.register(&tui); tui.dialogs.clear(); }) diff --git a/packages/cli-rs/crates/tui/src/routes/session/scroll.rs b/packages/cli-rs/crates/tui/src/routes/session/scroll.rs index 70a539995cb..162f885b4df 100644 --- a/packages/cli-rs/crates/tui/src/routes/session/scroll.rs +++ b/packages/cli-rs/crates/tui/src/routes/session/scroll.rs @@ -75,6 +75,21 @@ impl Controls { } } +impl Drop for Inner { + fn drop(&mut self) { + if let Some(tui) = self.tui.upgrade() { + dispose(&tui, std::mem::take(&mut *self.layers.borrow_mut())); + } + } +} + +/// Unregisters `layers`. A command may drop the view while the keymap runs it, so they go afterwards; +/// dropping a disposer does not unregister its layer. +fn dispose(tui: &Tui, layers: Vec) { + tui.app + .defer(move |_| layers.into_iter().for_each(Disposer::dispose)); +} + impl Inner { fn install(&self, tui: &Tui) { self.configure(&tui.settings.info()); @@ -166,7 +181,7 @@ impl Inner { /// the scroll keys in every mode, `session.first` and `session.last` also while no editor has /// focus, and the rest in the base mode. pub(super) fn register(&self, tui: &Tui) { - self.layers.borrow_mut().clear(); + dispose(tui, std::mem::take(&mut *self.layers.borrow_mut())); let command = |id: &str, title: &str, run: fn(&Inner, &Tui)| { let weak = self.this.clone(); TuiCommand::new(id, move |_, _| {