diff options
| author | Ogulcan Celik <ogulcancelik@gmail.com> | 2026-05-12 00:24:13 +0300 |
|---|---|---|
| committer | Ogulcan Celik <ogulcancelik@gmail.com> | 2026-05-12 00:24:13 +0300 |
| commit | 520f4272d0f332f2a98a2a7486be1797feb86dcf (patch) | |
| tree | f53830efc04e6961d7b0cd003bc453b95a714fe1 /src/app | |
| parent | 8d5dca8cfe2d580835d3392d0ebdff57f7de7298 (diff) | |
feat: add agent cycling and tab wheel switching
fixes #92
Diffstat (limited to 'src/app')
| -rw-r--r-- | src/app/actions.rs | 167 | ||||
| -rw-r--r-- | src/app/input/mouse.rs | 109 | ||||
| -rw-r--r-- | src/app/input/navigate.rs | 48 | ||||
| -rw-r--r-- | src/app/state.rs | 4 |
4 files changed, 318 insertions, 10 deletions
diff --git a/src/app/actions.rs b/src/app/actions.rs index 317bbff7..6263679c 100644 --- a/src/app/actions.rs +++ b/src/app/actions.rs @@ -323,6 +323,80 @@ impl AppState { } } + pub fn next_agent(&mut self) { + self.cycle_agent_entry(true); + } + + pub fn previous_agent(&mut self) { + self.cycle_agent_entry(false); + } + + fn cycle_agent_entry(&mut self, forward: bool) { + let entries = crate::ui::agent_panel_entries(self); + if entries.is_empty() { + return; + } + + let focused = self + .active + .and_then(|idx| self.workspaces.get(idx)) + .and_then(crate::workspace::Workspace::focused_pane_id); + let current_idx = + focused.and_then(|pane_id| entries.iter().position(|entry| entry.pane_id == pane_id)); + let target_idx = match (current_idx, forward) { + (Some(idx), true) => (idx + 1) % entries.len(), + (Some(0), false) => entries.len() - 1, + (Some(idx), false) => idx - 1, + (None, true) => 0, + (None, false) => entries.len() - 1, + }; + + let target = &entries[target_idx]; + let ws_idx = target.ws_idx; + let tab_idx = target.tab_idx; + let pane_id = target.pane_id; + + self.switch_workspace(ws_idx); + self.switch_tab(tab_idx); + if let Some(tab) = self + .workspaces + .get_mut(ws_idx) + .and_then(|ws| ws.tabs.get_mut(tab_idx)) + { + if tab.panes.contains_key(&pane_id) { + tab.layout.focus_pane(pane_id); + self.mark_session_dirty(); + } + } + self.ensure_agent_panel_entry_visible(target_idx); + } + + fn ensure_agent_panel_entry_visible(&mut self, idx: usize) { + if self.sidebar_collapsed { + return; + } + + let (_, detail_area) = crate::ui::expanded_sidebar_sections( + self.view.sidebar_rect, + self.sidebar_section_split, + ); + let metrics = crate::ui::agent_panel_scroll_metrics(self, detail_area); + let visible = metrics.viewport_rows; + if visible == 0 { + return; + } + + if idx < self.agent_panel_scroll { + self.agent_panel_scroll = idx; + } else if idx >= self.agent_panel_scroll.saturating_add(visible) { + self.agent_panel_scroll = idx.saturating_add(1).saturating_sub(visible); + } + + let max_scroll = + crate::ui::agent_panel_scroll_metrics(self, detail_area).max_offset_from_bottom; + self.agent_panel_scroll = self.agent_panel_scroll.min(max_scroll); + } + pub fn close_selected_workspace(&mut self) { if self.workspaces.is_empty() { return; @@ -871,6 +945,99 @@ mod tests { assert_eq!(toast.context, "detach, then run `herdr update`"); } + fn mark_agent(state: &mut AppState, ws_idx: usize, tab_idx: usize, pane_id: PaneId) { + state.workspaces[ws_idx].tabs[tab_idx] + .panes + .get_mut(&pane_id) + .unwrap() + .set_detected_state(Some(Agent::Pi), AgentState::Idle); + } + + #[test] + fn next_agent_cycles_agent_panel_entries_in_all_scope() { + let mut first = Workspace::test_new("one"); + let first_root = first.tabs[0].root_pane; + let first_second = first.test_split(Direction::Horizontal); + first.tabs[0].layout.focus_pane(first_root); + let second = Workspace::test_new("two"); + let second_root = second.tabs[0].root_pane; + + let mut state = AppState::test_new(); + state.workspaces = vec![first, second]; + state.active = Some(0); + state.selected = 0; + state.mode = Mode::Terminal; + state.agent_panel_scope = crate::app::state::AgentPanelScope::AllWorkspaces; + mark_agent(&mut state, 0, 0, first_root); + mark_agent(&mut state, 0, 0, first_second); + mark_agent(&mut state, 1, 0, second_root); + + state.next_agent(); + assert_eq!(state.active, Some(0)); + assert_eq!(state.workspaces[0].focused_pane_id(), Some(first_second)); + + state.next_agent(); + assert_eq!(state.active, Some(1)); + assert_eq!(state.workspaces[1].focused_pane_id(), Some(second_root)); + + state.previous_agent(); + assert_eq!(state.active, Some(0)); + assert_eq!(state.workspaces[0].focused_pane_id(), Some(first_second)); + } + + #[test] + fn next_agent_cycles_only_current_scope_entries() { + let mut first = Workspace::test_new("one"); + let first_root = first.tabs[0].root_pane; + let first_second = first.test_split(Direction::Horizontal); + first.tabs[0].layout.focus_pane(first_second); + let second = Workspace::test_new("two"); + let second_root = second.tabs[0].root_pane; + + let mut state = AppState::test_new(); + state.workspaces = vec![first, second]; + state.active = Some(0); + state.selected = 0; + state.mode = Mode::Terminal; + state.agent_panel_scope = crate::app::state::AgentPanelScope::CurrentWorkspace; + mark_agent(&mut state, 0, 0, first_root); + mark_agent(&mut state, 0, 0, first_second); + mark_agent(&mut state, 1, 0, second_root); + + state.next_agent(); + + assert_eq!(state.active, Some(0)); + assert_eq!(state.workspaces[0].focused_pane_id(), Some(first_root)); + } + + #[test] + fn previous_agent_keeps_wrapped_target_visible_in_agent_panel() { + let mut workspace = Workspace::test_new("one"); + let root = workspace.tabs[0].root_pane; + for idx in 1..8 { + workspace.test_add_tab(Some(&format!("tab-{idx}"))); + } + + let mut state = AppState::test_new(); + state.workspaces = vec![workspace]; + state.active = Some(0); + state.selected = 0; + state.mode = Mode::Terminal; + state.agent_panel_scope = crate::app::state::AgentPanelScope::CurrentWorkspace; + for tab_idx in 0..state.workspaces[0].tabs.len() { + let pane_id = state.workspaces[0].tabs[tab_idx].root_pane; + mark_agent(&mut state, 0, tab_idx, pane_id); + } + state.workspaces[0].tabs[0].layout.focus_pane(root); + crate::ui::compute_view(&mut state, ratatui::layout::Rect::new(0, 0, 80, 14)); + + state.previous_agent(); + + let last_idx = state.workspaces[0].tabs.len() - 1; + assert_eq!(state.workspaces[0].active_tab, last_idx); + assert!(state.agent_panel_scroll > 0); + } + #[test] fn switch_workspace_updates_active_and_selected() { let mut state = app_with_workspaces(&["a", "b", "c"]); diff --git a/src/app/input/mouse.rs b/src/app/input/mouse.rs index 99157d0d..779af3ed 100644 --- a/src/app/input/mouse.rs +++ b/src/app/input/mouse.rs @@ -581,19 +581,24 @@ impl AppState { } } - MouseEventKind::ScrollUp | MouseEventKind::ScrollDown if !in_sidebar => { - if self.on_tab_bar(mouse.column, mouse.row) { - match mouse.kind { - MouseEventKind::ScrollUp => self.scroll_tabs_left(), - MouseEventKind::ScrollDown => self.scroll_tabs_right(), - _ => {} - } - } else if !self.scroll_selection_with_wheel(mouse) { - self.selection = None; - self.handle_terminal_wheel(mouse); + MouseEventKind::ScrollUp | MouseEventKind::ScrollDown + if self.on_tab_bar(mouse.column, mouse.row) => + { + match mouse.kind { + MouseEventKind::ScrollUp => self.previous_tab(), + MouseEventKind::ScrollDown => self.next_tab(), + _ => {} } } + MouseEventKind::ScrollUp | MouseEventKind::ScrollDown + if !in_sidebar && self.scroll_selection_with_wheel(mouse) => {} + + MouseEventKind::ScrollUp | MouseEventKind::ScrollDown if !in_sidebar => { + self.selection = None; + self.handle_terminal_wheel(mouse); + } + MouseEventKind::ScrollUp if in_sidebar => { let agent_area = self.agent_panel_rect(); let over_agent_panel = agent_area != Rect::default() @@ -1568,6 +1573,90 @@ mod tests { } #[test] + fn wheel_over_tab_bar_switches_tabs() { + let mut app = app_for_mouse_test(); + let mut ws = Workspace::test_new("one"); + ws.test_add_tab(Some("two")); + ws.test_add_tab(Some("three")); + app.state.workspaces = vec![ws]; + app.state.active = Some(0); + app.state.selected = 0; + app.state.mode = Mode::Terminal; + + crate::ui::compute_view(&mut app.state, Rect::new(0, 0, 106, 20)); + let tab_bar = app.state.view.tab_bar_rect; + + app.handle_mouse(mouse(MouseEventKind::ScrollDown, tab_bar.x + 1, tab_bar.y)); + assert_eq!(app.state.workspaces[0].active_tab, 1); + + app.handle_mouse(mouse(MouseEventKind::ScrollUp, tab_bar.x + 1, tab_bar.y)); + assert_eq!(app.state.workspaces[0].active_tab, 0); + + app.handle_mouse(mouse(MouseEventKind::ScrollUp, tab_bar.x + 1, tab_bar.y)); + assert_eq!(app.state.workspaces[0].active_tab, 2); + + app.handle_mouse(mouse( + MouseEventKind::ScrollDown, + tab_bar.x + tab_bar.width.saturating_sub(1), + tab_bar.y, + )); + assert_eq!(app.state.workspaces[0].active_tab, 0); + } + + #[test] + fn wheel_over_overflowing_tab_bar_switches_tabs() { + let mut app = app_for_mouse_test(); + let mut ws = Workspace::test_new("one"); + ws.tabs[0].set_custom_name("very-long-one".into()); + ws.test_add_tab(Some("very-long-two")); + ws.test_add_tab(Some("very-long-three")); + app.state.workspaces = vec![ws]; + app.state.active = Some(0); + app.state.selected = 0; + app.state.mode = Mode::Terminal; + + crate::ui::compute_view(&mut app.state, Rect::new(0, 0, 65, 20)); + assert!(app.state.view.tab_scroll_right_hit_area.width > 0); + let tab_bar = app.state.view.tab_bar_rect; + + app.handle_mouse(mouse( + MouseEventKind::ScrollDown, + tab_bar.x + tab_bar.width.saturating_sub(2), + tab_bar.y, + )); + assert_eq!(app.state.workspaces[0].active_tab, 1); + + app.handle_mouse(mouse( + MouseEventKind::ScrollDown, + tab_bar.x + tab_bar.width.saturating_sub(2), + tab_bar.y, + )); + assert_eq!(app.state.workspaces[0].active_tab, 2); + } + + #[test] + fn wheel_outside_tab_bar_does_not_switch_tabs() { + let mut app = app_for_mouse_test(); + let mut ws = Workspace::test_new("one"); + ws.test_add_tab(Some("two")); + app.state.workspaces = vec![ws]; + app.state.active = Some(0); + app.state.selected = 0; + app.state.mode = Mode::Terminal; + + crate::ui::compute_view(&mut app.state, Rect::new(0, 0, 106, 20)); + let terminal = app.state.view.terminal_area; + + app.handle_mouse(mouse( + MouseEventKind::ScrollDown, + terminal.x + 1, + terminal.y + 1, + )); + + assert_eq!(app.state.workspaces[0].active_tab, 0); + } + + #[test] fn mobile_switch_button_opens_switcher_and_workspace_row_switches_workspace() { let mut app = app_for_mouse_test(); app.state.workspaces = vec![Workspace::test_new("one"), Workspace::test_new("two")]; diff --git a/src/app/input/navigate.rs b/src/app/input/navigate.rs index ce3d9cf4..6dfaf475 100644 --- a/src/app/input/navigate.rs +++ b/src/app/input/navigate.rs @@ -31,6 +31,18 @@ pub(crate) fn terminal_direct_navigation_action( return Some(NavigateAction::NextWorkspace); } if kb + .previous_agent + .is_some_and(|(code, mods)| key_matches(key, code, mods)) + { + return Some(NavigateAction::PreviousAgent); + } + if kb + .next_agent + .is_some_and(|(code, mods)| key_matches(key, code, mods)) + { + return Some(NavigateAction::NextAgent); + } + if kb .previous_tab .is_some_and(|(code, mods)| key_matches(key, code, mods)) { @@ -363,6 +375,8 @@ pub(crate) enum NavigateAction { CloseWorkspace, PreviousWorkspace, NextWorkspace, + PreviousAgent, + NextAgent, NewTab, RenameTab, PreviousTab, @@ -407,6 +421,18 @@ fn navigate_action_for_key(state: &AppState, key: &KeyEvent) -> Option<NavigateA { return Some(NavigateAction::NextWorkspace); } + if kb + .previous_agent + .is_some_and(|(code, mods)| key_matches(key, code, mods)) + { + return Some(NavigateAction::PreviousAgent); + } + if kb + .next_agent + .is_some_and(|(code, mods)| key_matches(key, code, mods)) + { + return Some(NavigateAction::NextAgent); + } if key_matches(key, kb.new_tab.0, kb.new_tab.1) { return Some(NavigateAction::NewTab); } @@ -508,6 +534,14 @@ pub(super) fn execute_navigate_action(state: &mut AppState, action: NavigateActi state.next_workspace(); leave_navigate_mode(state); } + NavigateAction::PreviousAgent => { + state.previous_agent(); + leave_navigate_mode(state); + } + NavigateAction::NextAgent => { + state.next_agent(); + leave_navigate_mode(state); + } NavigateAction::NewTab => super::modal::open_new_tab_dialog(state), NavigateAction::RenameTab => super::modal::open_rename_active_tab(state, false), NavigateAction::PreviousTab => { @@ -730,6 +764,20 @@ mod tests { } #[test] + fn terminal_direct_agent_shortcut_maps_to_navigation_action() { + let mut state = state_with_workspaces(&["test"]); + state.keybinds.next_agent = Some((KeyCode::Char('a'), KeyModifiers::ALT)); + state.keybinds.next_agent_label = Some("alt+a".into()); + + let action = terminal_direct_navigation_action( + &state, + &KeyEvent::new(KeyCode::Char('a'), KeyModifiers::ALT), + ); + + assert_eq!(action, Some(NavigateAction::NextAgent)); + } + + #[test] fn terminal_direct_focus_pane_shortcut_maps_to_navigation_action() { let mut state = state_with_workspaces(&["test"]); state.keybinds.focus_pane_left = Some((KeyCode::Left, KeyModifiers::ALT)); diff --git a/src/app/state.rs b/src/app/state.rs index 3791f674..b350235a 100644 --- a/src/app/state.rs +++ b/src/app/state.rs @@ -903,6 +903,10 @@ impl AppState { previous_workspace_label: None, next_workspace: None, next_workspace_label: None, + previous_agent: None, + previous_agent_label: None, + next_agent: None, + next_agent_label: None, new_tab: (KeyCode::Char('c'), KeyModifiers::empty()), new_tab_label: "c".into(), rename_tab: None, |
