返回 CodeWhale
interaction.rs
根目录 / crates / tui / src / tui / work_surface / interaction.rs
1 //! Typed work-surface interaction ownership (TUI-DOG-004 / 005 / 006).
2 //!
3 //! Selection, focus, and detail-open are distinct axes. Destructive lifecycle
4 //! actions live inside the inspector pager, not in compact rows.
5
6 use crate::tui::app::{App, SidebarRowAction};
7 use crate::tui::views::ModalKind;
8
9 use super::model::{RailPanel, WorkRowId};
10
11 pub(crate) fn select_dock_panel(app: &mut App, panel: RailPanel) {
12 if panel == RailPanel::Terminal {
13 app.launch.dismiss();
14 }
15 app.work_surface.panel = panel;
16 app.work_surface.explicit_view = true;
17 app.work_surface.dismissed = false;
18 app.work_surface.focused = true;
19 app.work_surface.scroll_offset = 0;
20 app.work_surface.selected = None;
21 app.needs_redraw = true;
22 }
23
24 /// Esc / the `×` tab: close the view. An explicitly opened view goes back to
25 /// the auto rule; the dock stays down until new work arrives.
26 pub(crate) fn dismiss_dock(app: &mut App) {
27 app.work_surface.explicit_view = false;
28 app.work_surface.dismissed = true;
29 // Measure the view the auto rule will show next, not the one just
30 // closed: the dock re-opens when *that* view grows.
31 super::model::resolve_view(app);
32 app.work_surface.dismissed_view = app.work_surface.panel;
33 app.work_surface.dismissed_at_rows = super::model::auto_work_rows(app);
34 app.work_surface.focused = false;
35 app.work_surface.selected = None;
36 app.needs_redraw = true;
37 }
38
39 /// Claim work-surface focus and clear competing selection owners.
40 pub fn claim_focus(app: &mut App) {
41 let was_focused = app.work_surface.focused;
42 app.work_surface.focused = true;
43 crate::tui::mouse_ui::clear_transcript_selection(app);
44 if !was_focused {
45 app.needs_redraw = true;
46 }
47 }
48
49 /// Release work-surface focus without clearing the remembered selection.
50 pub fn release_focus(app: &mut App) {
51 if !app.work_surface.focused && app.work_surface.hovered.is_none() {
52 return;
53 }
54 app.work_surface.focused = false;
55 app.work_surface.hovered = None;
56 app.needs_redraw = true;
57 }
58
59 /// Open or toggle-close the primary detail for a row.
60 ///
61 /// Enter/click on an already-opened selected row closes it. Opening a different
62 /// row updates the inspector owner.
63 pub fn activate_primary(
64 app: &mut App,
65 row_id: &WorkRowId,
66 primary: Option<SidebarRowAction>,
67 ) -> Option<SidebarRowAction> {
68 if app.work_surface.opened.as_ref() == Some(row_id) {
69 // Toggle-close only while the detail is actually on screen. When the
70 // pager closed itself (q/Esc inside it), `opened` is a stale owner —
71 // swallowing the click here would make the row look dead, so fall
72 // through and reopen instead.
73 let detail_on_screen = app.view_stack.top_kind() == Some(ModalKind::Pager);
74 close_opened(app);
75 if detail_on_screen {
76 return None;
77 }
78 }
79 app.work_surface.selected = Some(row_id.clone());
80 let action = primary?;
81 // A group heading changes the visible panel but does not open a modal.
82 // Keep `opened` reserved for a real detail owner, otherwise a later
83 // activation treats the still-visible heading as a stale pager toggle.
84 if !matches!(action, SidebarRowAction::ShowSubagentsPanel) {
85 app.work_surface.opened = Some(row_id.clone());
86 }
87 Some(action)
88 }
89
90 /// Whether the work-surface-owned detail is actually on screen. A row action
91 /// that runs a command without pushing a view (the Context panel's `/compact`
92 /// row) still records `opened`; with nothing stacked above the dock that owner
93 /// is stale, and Esc belongs to the dock again (#6502).
94 pub(crate) fn opened_detail_on_screen(app: &App) -> bool {
95 app.work_surface.opened.is_some() && !app.view_stack.is_empty()
96 }
97
98 /// Close the work-surface-owned detail (pager when we opened it).
99 pub fn close_opened(app: &mut App) {
100 if app.work_surface.opened.take().is_none() {
101 return;
102 }
103 if app.view_stack.top_kind() == Some(ModalKind::Pager) {
104 app.view_stack.pop();
105 }
106 app.needs_redraw = true;
107 }
108
109 /// Release a closed Agent Details owner without disturbing Work selection.
110 /// The modal has already popped itself before this event is handled.
111 pub(crate) fn agent_details_closed(app: &mut App, agent_id: &str) {
112 let owner = WorkRowId(format!("worker:{agent_id}"));
113 if app.work_surface.opened.as_ref() == Some(&owner) {
114 app.work_surface.opened = None;
115 app.needs_redraw = true;
116 }
117 }
118
119 #[cfg(test)]
120 mod tests {
121 use super::*;
122 use crate::tui::app::TuiOptions;
123 use codewhale_runtime::config::Config;
124 use std::path::PathBuf;
125
126 fn app() -> App {
127 let options = TuiOptions {
128 use_mouse_capture: true,
129 max_subagents: 4,
130 ..crate::tui::test_support::test_tui_options(PathBuf::from("."))
131 };
132 App::new(options, &Config::default())
133 }
134
135 #[test]
136 fn primary_toggles_opened_closed() {
137 let mut app = app();
138 let row = WorkRowId("worker:a1".into());
139 let open = SidebarRowAction::OpenAgentDetail {
140 agent_id: "a1".into(),
141 };
142 assert!(activate_primary(&mut app, &row, Some(open.clone())).is_some());
143 assert_eq!(app.work_surface.opened.as_ref(), Some(&row));
144 // With the detail pager on screen, the second activation toggles it
145 // closed; with no pager on screen (it closed itself), the activation
146 // reopens instead of going dead.
147 app.view_stack.push(crate::tui::pager::PagerView::from_text(
148 "Agent".to_string(),
149 "body",
150 40,
151 ));
152 assert!(activate_primary(&mut app, &row, Some(open.clone())).is_none());
153 assert!(app.work_surface.opened.is_none());
154 assert!(activate_primary(&mut app, &row, Some(open.clone())).is_some());
155 assert_eq!(app.work_surface.opened.as_ref(), Some(&row));
156 // Pager already gone (closed from inside): reopen, don't swallow.
157 assert!(activate_primary(&mut app, &row, Some(open)).is_some());
158 assert_eq!(app.work_surface.opened.as_ref(), Some(&row));
159 }
160
161 #[test]
162 fn claim_focus_clears_transcript_selection() {
163 use crate::tui::selection::TranscriptSelectionPoint;
164 let mut app = app();
165 app.viewport.transcript_selection.anchor = Some(TranscriptSelectionPoint {
166 line_index: 0,
167 column: 0,
168 });
169 app.viewport.transcript_selection.head = app.viewport.transcript_selection.anchor;
170 claim_focus(&mut app);
171 assert!(app.work_surface.focused);
172 assert!(!app.viewport.transcript_selection.is_active());
173 assert!(app.needs_redraw);
174 }
175 }
176
176 lines RUST