//! `AppState` — the top-level mutable state of the running TUI. //! //! v1 keeps it small on purpose: one pane containing a session or draft form, //! focus split between sidebar and pane, the current input mode, and an optional overlay (kill confirm, //! help, transient toast). The spawn form is no longer an overlay — an empty //! pane is a draft pane (issue #63). //! //! v1.5 will replace `Tab::pane`'s single slot with a split tree. use std::collections::HashSet; use std::path::PathBuf; use uuid::Uuid; use crate::draft::DraftPane; use crate::keymap::Mode; #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum Focus { Sidebar, Pane, } /// One selectable item in the rendered sidebar sequence. /// /// Group paths, unlike a session's position in the catalog, survive catalog /// redraws and make a highlighted collapsed group unambiguous. #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub enum SidebarItem { Group(PathBuf), Session(Uuid), } /// What a tab's pane slot holds: a live session, or an inline draft form. // The draft variant dwarfs `Session(Uuid)`; the pane is moved rarely enough // that boxing every access costs more than the copy does. #[allow(clippy::large_enum_variant)] #[derive(Debug, Clone, PartialEq)] pub enum PaneRef { Session(Uuid), Draft(DraftPane), } #[derive(Debug, Clone)] pub struct Tab { pub name: String, pub pane: Option, } impl Tab { /// The session this tab shows, if it shows one. Draft panes have none. pub fn session(&self) -> Option { match &self.pane { Some(PaneRef::Session(sid)) => Some(*sid), _ => None, } } } #[derive(Debug, Clone, PartialEq, Eq)] pub enum OverlayKind { KillConfirm(Uuid), /// Rename prompt for this session (#125). The edit buffer lives on `App`, /// so this stays comparable. Rename(Uuid), Help, Toast(String), } #[derive(Debug)] pub struct AppState { pub tabs: Vec, pub active_tab_idx: usize, pub focus: Focus, pub mode: Mode, pub sidebar_visible: bool, pub sidebar_highlight: Option, /// Group paths whose cards are hidden. This is UI state rather than /// catalog state, so it survives every catalog redraw. pub collapsed_sidebar_groups: HashSet, /// Private: every transition goes through [`AppState::show_overlay`], /// [`AppState::dismiss_overlay`] or [`AppState::toast`]. Poking the field /// from four call sites is what left a toast on screen with no way to /// clear it. overlay: Option, } impl Default for AppState { fn default() -> Self { Self::new() } } impl AppState { pub fn new() -> Self { Self { tabs: vec![Tab { name: "1".into(), pane: None, }], active_tab_idx: 0, focus: Focus::Sidebar, mode: Mode::Normal, sidebar_visible: true, sidebar_highlight: None, collapsed_sidebar_groups: HashSet::new(), overlay: None, } } pub fn overlay(&self) -> Option<&OverlayKind> { self.overlay.as_ref() } pub fn show_overlay(&mut self, kind: OverlayKind) { self.overlay = Some(kind); } /// Clear whatever overlay is up. Returns true if one was showing. pub fn dismiss_overlay(&mut self) -> bool { self.overlay.take().is_some() } /// Surface a transient message. Replaces any overlay currently up. pub fn toast(&mut self, message: impl Into) { self.overlay = Some(OverlayKind::Toast(message.into())); } pub fn active_tab(&self) -> &Tab { &self.tabs[self.active_tab_idx] } pub fn active_tab_mut(&mut self) -> &mut Tab { &mut self.tabs[self.active_tab_idx] } /// Bind a session to the active tab, move focus into the pane, and /// drop straight into Insert mode so keystrokes go to the session /// without an extra `i` press. The leader returns to Normal. pub fn attach_to_focused(&mut self, session_id: Uuid) { self.active_tab_mut().pane = Some(PaneRef::Session(session_id)); self.focus = Focus::Pane; self.mode = Mode::Insert; } /// Replace the pane with a draft form. Focus lands in the pane, Insert /// mode on, so typing starts immediately, same flow as /// [`Self::attach_to_focused`]. pub fn open_draft_pane(&mut self) { self.active_tab_mut().pane = Some(PaneRef::Draft(DraftPane::default())); self.focus = Focus::Pane; self.mode = Mode::Insert; } /// A draft submit succeeded: replace whatever the active tab showed with /// the new session in place, matching the web client's behaviour (#57). pub fn complete_spawn(&mut self, sid: Uuid) { self.active_tab_mut().pane = Some(PaneRef::Session(sid)); self.focus = Focus::Pane; self.mode = Mode::Insert; } /// The session shown by the active tab's pane, if any. pub fn active_session(&self) -> Option { self.active_tab().session() } /// Move the sidebar highlight one step through the rendered item /// sequence, in the direction of `delta` (positive = down), landing only /// on items `is_stop` accepts. Only the sign of `delta` matters. Wraps. /// No-op when `order` holds no stop. /// /// Anchoring walks the *full* rendered order, not just the stops, so a /// highlight parked on a skipped row — an expanded group header, which a /// mouse click can still select — steps to its neighbour instead of /// jumping to the far end of the list. pub fn move_sidebar_highlight( &mut self, order: &[SidebarItem], delta: i32, is_stop: impl Fn(&SidebarItem) -> bool, ) { if order.is_empty() || delta == 0 { return; } let len = order.len() as i32; let step = delta.signum(); let cur = self .sidebar_highlight .as_ref() .and_then(|id| order.iter().position(|x| x == id)) .map(|i| i as i32) // Nothing highlighted: start one off the end we walk from, so the // first step lands on the first (or last) item. .unwrap_or(if step > 0 { -1 } else { 0 }); let mut idx = cur; for _ in 0..len { idx = (idx + step).rem_euclid(len); if is_stop(&order[idx as usize]) { self.sidebar_highlight = Some(order[idx as usize].clone()); return; } } } /// Toggle a group. When collapsing a group containing the current card, /// leave the highlight on the group row that remains visible. pub fn toggle_sidebar_group(&mut self, key: PathBuf, contains_highlight: bool) { if self.collapsed_sidebar_groups.remove(&key) { return; } self.collapsed_sidebar_groups.insert(key.clone()); if contains_highlight { self.sidebar_highlight = Some(SidebarItem::Group(key)); } } /// Toggle sidebar visibility. pub fn toggle_sidebar(&mut self) { self.sidebar_visible = !self.sidebar_visible; if !self.sidebar_visible { self.focus = Focus::Pane; } } } #[cfg(test)] mod tests { use super::*; #[test] fn new_app_starts_with_one_empty_tab_focused_on_sidebar() { let state = AppState::new(); assert_eq!(state.tabs.len(), 1); assert_eq!(state.tabs[0].pane, None); assert_eq!(state.focus, Focus::Sidebar); assert_eq!(state.mode, Mode::Normal); assert!(state.sidebar_visible); assert!(state.overlay.is_none()); } #[test] fn move_sidebar_highlight_walks_order_and_wraps() { let mut state = AppState::new(); let a = Uuid::from_u128(1); let b = Uuid::from_u128(2); let c = Uuid::from_u128(3); let order = vec![ SidebarItem::Session(a), SidebarItem::Group("repo".into()), SidebarItem::Session(b), SidebarItem::Session(c), ]; // No prior highlight + delta +1 lands on first item. state.move_sidebar_highlight(&order, 1, |_| true); assert_eq!(state.sidebar_highlight, Some(SidebarItem::Session(a))); state.move_sidebar_highlight(&order, 1, |_| true); assert_eq!( state.sidebar_highlight, Some(SidebarItem::Group("repo".into())) ); state.move_sidebar_highlight(&order, 1, |_| true); assert_eq!(state.sidebar_highlight, Some(SidebarItem::Session(b))); state.move_sidebar_highlight(&order, 1, |_| true); assert_eq!(state.sidebar_highlight, Some(SidebarItem::Session(c))); // Wraps forward. state.move_sidebar_highlight(&order, 1, |_| true); assert_eq!(state.sidebar_highlight, Some(SidebarItem::Session(a))); // Wraps backward. state.move_sidebar_highlight(&order, -1, |_| true); assert_eq!(state.sidebar_highlight, Some(SidebarItem::Session(c))); } #[test] fn move_sidebar_highlight_skips_rows_the_filter_rejects() { let mut state = AppState::new(); let a = Uuid::from_u128(1); let b = Uuid::from_u128(2); let order = vec![ SidebarItem::Group("repo".into()), SidebarItem::Session(a), SidebarItem::Group("other".into()), SidebarItem::Session(b), ]; let sessions_only = |item: &SidebarItem| matches!(item, SidebarItem::Session(_)); state.move_sidebar_highlight(&order, 1, sessions_only); assert_eq!(state.sidebar_highlight, Some(SidebarItem::Session(a))); state.move_sidebar_highlight(&order, 1, sessions_only); assert_eq!(state.sidebar_highlight, Some(SidebarItem::Session(b))); state.move_sidebar_highlight(&order, -1, sessions_only); assert_eq!(state.sidebar_highlight, Some(SidebarItem::Session(a))); // Backwards from nothing lands on the last stop, not the last row. state.sidebar_highlight = None; state.move_sidebar_highlight(&order, -1, sessions_only); assert_eq!(state.sidebar_highlight, Some(SidebarItem::Session(b))); } /// A highlight sitting on a skipped row still moves to its neighbour. #[test] fn move_sidebar_highlight_anchors_on_a_skipped_row() { let mut state = AppState::new(); let a = Uuid::from_u128(1); let b = Uuid::from_u128(2); let order = vec![ SidebarItem::Session(a), SidebarItem::Group("repo".into()), SidebarItem::Session(b), ]; let sessions_only = |item: &SidebarItem| matches!(item, SidebarItem::Session(_)); state.sidebar_highlight = Some(SidebarItem::Group("repo".into())); state.move_sidebar_highlight(&order, 1, sessions_only); assert_eq!(state.sidebar_highlight, Some(SidebarItem::Session(b))); state.sidebar_highlight = Some(SidebarItem::Group("repo".into())); state.move_sidebar_highlight(&order, -1, sessions_only); assert_eq!(state.sidebar_highlight, Some(SidebarItem::Session(a))); } #[test] fn move_sidebar_highlight_is_noop_when_nothing_is_a_stop() { let mut state = AppState::new(); let order = vec![SidebarItem::Group("repo".into())]; state.move_sidebar_highlight(&order, 1, |_| false); assert_eq!(state.sidebar_highlight, None); } #[test] fn move_sidebar_highlight_is_noop_on_empty_order() { let mut state = AppState::new(); state.move_sidebar_highlight(&[], 1, |_| true); assert_eq!(state.sidebar_highlight, None); } #[test] fn collapsing_highlighted_group_moves_a_card_highlight_to_the_group() { let key = PathBuf::from("/repo"); let mut state = AppState::new(); state.sidebar_highlight = Some(SidebarItem::Session(Uuid::from_u128(9))); state.toggle_sidebar_group(key.clone(), true); assert_eq!( state.sidebar_highlight, Some(SidebarItem::Group(key.clone())) ); assert!(state.collapsed_sidebar_groups.contains(&key)); state.toggle_sidebar_group(key.clone(), false); assert!(!state.collapsed_sidebar_groups.contains(&key)); assert_eq!(state.sidebar_highlight, Some(SidebarItem::Group(key))); } #[test] fn attach_binds_focuses_pane_and_enters_insert_mode() { let mut state = AppState::new(); let sid = Uuid::from_u128(7); state.attach_to_focused(sid); assert_eq!(state.tabs[0].session(), Some(sid)); assert_eq!(state.focus, Focus::Pane); // The user expects to type into the session right away; the leader // (default ctrl-a) is the way back. assert_eq!(state.mode, Mode::Insert); } #[test] fn toggle_sidebar_moves_focus_to_pane_when_hiding() { let mut state = AppState::new(); state.toggle_sidebar(); assert!(!state.sidebar_visible); assert_eq!(state.focus, Focus::Pane); state.toggle_sidebar(); assert!(state.sidebar_visible); // Doesn't force focus back; sidebar shown but focus stays where it was. assert_eq!(state.focus, Focus::Pane); } #[test] fn dismiss_overlay_reports_whether_one_was_showing() { let mut state = AppState::new(); assert!(!state.dismiss_overlay()); state.toast("boom"); assert!(state.dismiss_overlay()); assert!(state.overlay().is_none()); } /// Issue #63: `s` over an empty pane turns that pane into a draft — /// focused, Insert mode on, typing starts immediately. #[test] fn open_draft_reuses_the_empty_active_pane() { let mut state = AppState::new(); state.open_draft_pane(); assert!(matches!(state.active_tab().pane, Some(PaneRef::Draft(_)))); assert_eq!(state.focus, Focus::Pane); assert_eq!(state.mode, Mode::Insert); assert_eq!(state.tabs.len(), 1); } #[test] fn open_draft_over_a_session_reuses_the_pane() { let mut state = AppState::new(); state.attach_to_focused(Uuid::from_u128(9)); state.open_draft_pane(); assert_eq!(state.tabs.len(), 1); assert_eq!(state.active_tab_idx, 0); assert!(matches!(state.active_tab().pane, Some(PaneRef::Draft(_)))); } /// Web parity (#57): submitting fills the pane in place. #[test] fn complete_spawn_replaces_the_draft_with_the_session() { let mut state = AppState::new(); state.open_draft_pane(); let sid = Uuid::from_u128(21); state.complete_spawn(sid); assert_eq!(state.active_tab().session(), Some(sid)); assert_eq!(state.focus, Focus::Pane); assert_eq!(state.mode, Mode::Insert); } }