diff --git a/src/app/commands.rs b/src/app/commands.rs index 7444097..de96984 100644 --- a/src/app/commands.rs +++ b/src/app/commands.rs @@ -215,11 +215,7 @@ impl Workspace { let cwd = self.new_session_cwd(); self.act(Act::NewClaude { cwd }) } - Command::ToggleShell => { - if let Some(id) = self.active.clone() { - self.act(Act::ToggleShell(id)) - } - } + Command::ToggleShell => self.toggle_shell_here(), Command::CloseSession => { if let Some(id) = self.active.clone() { self.act(Act::CloseLive(id)) diff --git a/src/app/headless.rs b/src/app/headless.rs index b88d3e0..33bfb1b 100644 --- a/src/app/headless.rs +++ b/src/app/headless.rs @@ -383,6 +383,40 @@ fn antigravity_limits_show_one_row_per_pool_only_with_antigravity_subagents_on() assert!(h.drawn("meter-logo-antigravity-0") && h.drawn("meter-logo-antigravity-1"), "each pool's row starts with its logos"); } +/// The welcome screen has a shell of its own: it opens with no session, takes the keyboard, is resized like any +/// shell, hides while a session is shown, and is back when the last session closes. +#[test] +fn the_shell_opens_on_the_welcome_screen() { + let mut h = Harness::new(|_| vec![]); + assert!(h.ws().main == Main::Home && h.ws().active.is_none(), "starts on the welcome screen"); + h.step(|ws| ws.command(Command::ToggleShell)); + h.settle(); + assert!(h.ws().shells.get(HOME_SHELL).is_some_and(|p| p.open), "the shell opens with no session"); + assert!(h.ws().terminal_focused(&shell_key(HOME_SHELL)), "it takes the keyboard"); + assert!(h.drawn("shell-panel"), "the shell panel draws: {:?}", h.keys()); + h.step(|ws| { + ws.shell_drag = Some(SideDrag { size: SHELL_HEIGHT }); + ws.drag_shell(-100.0); + assert_eq!(ws.shells[HOME_SHELL].height, SHELL_HEIGHT + 100.0, "the divider resizes it"); + ws.shell_drag = None; + }); + + // A session shown hides it, and it keeps running; the session's own shell is separate. + let cwd = h.work.clone(); + h.step(|ws| ws.new_claude(cwd.clone())); + let id = h.ws().active.clone().expect("a session is shown"); + h.settle(); + assert!(!h.drawn("shell-panel"), "the welcome shell stays out of a session's view"); + assert!(h.ws().shells.contains_key(HOME_SHELL), "but is kept"); + h.step(|ws| ws.close_live(&id)); + h.settle(); + assert!(h.ws().main == Main::Home && h.drawn("shell-panel"), "closing the last session brings the welcome shell back"); + assert!(h.ws().terminal_focused(&shell_key(HOME_SHELL)), "and the keyboard with it"); + + h.step(|ws| ws.command(Command::ToggleShell)); + assert!(!h.ws().shells[HOME_SHELL].open, "toggled shut"); +} + /// Sessions and the shell under them, the sidebars' widths, cards and the GitHub view, with a stand-in `claude`. #[test] fn sessions_and_the_shell_panel() { diff --git a/src/app/input.rs b/src/app/input.rs index 671d51d..fad9f22 100644 --- a/src/app/input.rs +++ b/src/app/input.rs @@ -135,7 +135,10 @@ impl Workspace { pub fn poll(&mut self, tx: &mut Tx) -> bool { let mut changed = self.run_jobs(); changed |= std::mem::take(&mut self.rt.dirty); - let showing = if self.main == Main::Terminals { self.active.clone() } else { None }; + let showing = match self.main { + Main::Terminals => self.active.clone(), + Main::Home => Some(HOME_SHELL.to_string()), + }; let foreground = self.window_active; let mut next: Option = None; let mut pump = |view: &mut TerminalView, visible: bool| -> bool { diff --git a/src/app/layout.rs b/src/app/layout.rs index 6cd8270..34b12ba 100644 --- a/src/app/layout.rs +++ b/src/app/layout.rs @@ -31,6 +31,34 @@ impl Workspace { self.focus_shell(); } + /// Show or hide the shell of what is on screen: the shown session's, or on the welcome screen the one that needs + /// no session. It starts in the home folder and is kept while sessions are open. + pub fn toggle_shell_here(&mut self) { + if let Some(id) = self.active_id().cloned() { + self.toggle_shell(&id); + return; + } + if let Some(p) = self.shells.get_mut(HOME_SHELL) { + p.open = !p.open; + } else if let Some(cwd) = platform::home_dir() { + let panel = self.new_shell_panel(HOME_SHELL, cwd); + self.shells.insert(HOME_SHELL.to_string(), panel); + } else { + self.toast("Could not find the home folder for a shell.", true); + return; + } + if self.shells.get(HOME_SHELL).is_some_and(|p| p.open) { + self.focus_shell(); + } else { + self.focus_active(); + } + } + + /// The owner of the shell panel on screen: the shown session, or `HOME_SHELL` on the welcome screen. + pub fn shell_owner(&self) -> Id { + self.active_id().cloned().unwrap_or_else(|| HOME_SHELL.to_string()) + } + /// A shell for the session, started in `cwd`. When the shell ends, its panel goes away (`on_terminal_event`), with a /// toast if it never started. pub(super) fn new_shell_panel(&mut self, id: &str, cwd: PathBuf) -> ShellPanel { @@ -41,8 +69,9 @@ impl Workspace { } fn focus_shell(&mut self) { - if let Some(id) = self.active_id().filter(|id| self.shells.get(*id).is_some_and(|p| p.open)).cloned() { - self.focus_terminal(&shell_key(&id)); + let owner = self.shell_owner(); + if self.shells.get(&owner).is_some_and(|p| p.open) { + self.focus_terminal(&shell_key(&owner)); } } @@ -64,7 +93,8 @@ impl Workspace { let Some(d) = self.shell_drag else { return }; let window_h = self.window_size.1; let height = (d.size - dy).clamp(SHELL_MIN, (window_h - 200.0).max(SHELL_MIN)); - if let Some(p) = self.active.clone().and_then(|id| self.shells.get_mut(&id)) { + let owner = self.shell_owner(); + if let Some(p) = self.shells.get_mut(&owner) { p.height = height; } } diff --git a/src/app/mod.rs b/src/app/mod.rs index f0e14ab..99bc6dd 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -408,6 +408,9 @@ pub fn shell_key(id: &str) -> String { format!("shell:{id}") } +/// The owner of the shell on the welcome screen, which has no session. Session ids are UUIDs, so it never collides. +pub const HOME_SHELL: &str = "home"; + pub const SHELL_HEIGHT: f32 = 260.0; pub const SHELL_MIN: f32 = 120.0; diff --git a/src/app/sessions.rs b/src/app/sessions.rs index 41a60f0..644a165 100644 --- a/src/app/sessions.rs +++ b/src/app/sessions.rs @@ -302,12 +302,12 @@ impl Workspace { self.prune_live(); } - /// Exited terminals are only kept while shown; a shell panel goes with its session. + /// Exited terminals are only kept while shown; a shell panel goes with its session, except the welcome screen's. fn prune_live(&mut self) { let active = &self.active; self.live.retain(|l| active.as_ref() == Some(&l.id) || l.view.is_running()); let live = &self.live; - self.shells.retain(|id, _| live.iter().any(|l| &l.id == id)); + self.shells.retain(|id, _| id == HOME_SHELL || live.iter().any(|l| &l.id == id)); } /// A Claude session that never produced a transcript (opened and closed without a prompt) has nothing @@ -383,10 +383,11 @@ impl Workspace { } } - /// Give the keyboard to the shown session's terminal, or to nothing on the home page. + /// Give the keyboard to the shown session's terminal, or on the home page to its shell when one is open. pub fn focus_active(&mut self) { match (self.main, self.active.clone()) { (Main::Terminals, Some(id)) if self.live_view(&id).is_some() => self.focus_terminal(&id), + (Main::Home, _) if self.shells.get(HOME_SHELL).is_some_and(|p| p.open) => self.focus_terminal(&shell_key(HOME_SHELL)), _ => { self.keys_to = None; self.blur = true; diff --git a/src/panels/workspace_view.rs b/src/panels/workspace_view.rs index 726ed83..fd903b2 100644 --- a/src/panels/workspace_view.rs +++ b/src/panels/workspace_view.rs @@ -128,16 +128,10 @@ impl Workspace { if hints { right = right.child(key_hint("J").key("hint-shell")); } - right = right.child(if session_active { - icon_button(&icons::PANEL_BOTTOM).key("toggle-shell").help("Shell").on_press_tx(|ws: &mut Workspace, tx: &mut Tx| { - if let Some(id) = ws.active.clone() { - ws.act(Act::ToggleShell(id)); - } - ws.after_input(tx); - }) - } else { - icon_button_disabled(&icons::PANEL_BOTTOM).key("toggle-shell") - }); + right = right.child(icon_button(&icons::PANEL_BOTTOM).key("toggle-shell").help("Shell").on_press_tx(|ws: &mut Workspace, tx: &mut Tx| { + ws.toggle_shell_here(); + ws.after_input(tx); + })); if hints { right = right.child(key_hint("Shift+R").key("hint-right")); } @@ -208,9 +202,8 @@ impl Workspace { /// The session's shell as its own panel under the main one: same gutter, rounding and grip as the sidebars. It /// slides up from the bottom edge. fn render_shell(&self) -> El { - let shown = self.active.clone().filter(|_| self.main == Main::Terminals); - let panel = shown.as_ref().and_then(|id| self.shells.get(id).map(|p| (id.clone(), p))); - let Some((id, p)) = panel else { return el().key("shell-slot").h(0.0) }; + let id = self.shell_owner(); + let Some(p) = self.shells.get(&id) else { return el().key("shell-slot").h(0.0) }; let height = if p.open { p.height } else { 0.0 }; let key = crate::app::shell_key(&id); let focused = self.terminal_focused(&key); @@ -226,7 +219,7 @@ impl Workspace { .on_drag(|ws: &mut Workspace, d: &mut rune::Drag, tx: &mut Tx| { match d.phase { Phase::Down => { - let height = ws.active.as_ref().and_then(|id| ws.shells.get(id)).map_or(SHELL_HEIGHT, |p| p.height); + let height = ws.shells.get(&ws.shell_owner()).map_or(SHELL_HEIGHT, |p| p.height); ws.shell_drag = Some(SideDrag { size: height }); } Phase::Move => ws.drag_shell(d.dy),