1use ratatui::Frame;
2use ratatui::style::Style;
3use ratatui::widgets::Block;
4
5use crate::layout::MainLayout;
6use crate::state::{AppState, ModalKind};
7use crate::theme;
8use crate::widgets::delegate_bar::DelegateBar;
9use crate::widgets::tool_detail::ToolDetail;
10use crate::widgets::tool_history::{flat_rows, HistoryRow, ToolHistory};
11use crate::widgets::{DialogSelect, Footer, InfoDialog, InputBox, InputDialog, MessageList, PermissionDialog, SlashPopover, SummaryBar, ThinkingPanel, ToastStack};
12
13pub struct Renderer;
14
15impl Renderer {
16 pub fn new() -> Self { Self }
17
18 pub fn draw(frame: &mut Frame, state: &mut AppState) {
19 let full = frame.area();
20 frame.render_widget(
21 Block::default().style(Style::default().bg(theme::BACKGROUND())),
22 full,
23 );
24
25 let thinking_lines = if state.is_streaming && !state.live_thinking.trim().is_empty() {
26 state
27 .live_thinking
28 .lines()
29 .filter(|l| !l.trim().is_empty())
30 .count()
31 } else {
32 0
33 };
34
35 let delegate_lines = state.sub_agents.len();
36 let has_summary = state.last_summary.is_some();
37 let summary_items: Option<Vec<String>> = state.last_summary.clone();
38 let layout = MainLayout::split(
39 full,
40 state.input.line_count(),
41 thinking_lines,
42 delegate_lines,
43 has_summary,
44 );
45
46 let tool_area = layout.tool_history;
47 if let Some(area) = tool_area {
48 frame.render_widget(ToolHistory::new(state), area);
49 }
50
51 frame.render_widget(
52 MessageList::new(&mut state.conversation, state.spinner_frame)
53 .with_tool_details(state.tool_details)
54 .with_thinking_active(state.is_streaming && !state.live_thinking.trim().is_empty()),
55 layout.messages,
56 );
57 if let Some(thinking_area) = layout.thinking {
58 frame.render_widget(
59 ThinkingPanel::new(&state.live_thinking, state.spinner_frame),
60 thinking_area,
61 );
62 }
63 if let Some(delegate_area) = layout.delegate {
64 frame.render_widget(
65 DelegateBar::new(&state.sub_agents, state.spinner_frame),
66 delegate_area,
67 );
68 }
69 if let (Some(summary_area), Some(items)) = (layout.summary, summary_items) {
70 frame.render_widget(SummaryBar::new(&items), summary_area);
71 }
72 frame.render_widget(InputBox::new(&state.input, !state.is_streaming), layout.input);
73 if !state.input.slash_matches.is_empty() {
74 frame.render_widget(SlashPopover::new(&state.input, layout.input), full);
75 }
76 frame.render_widget(
77 Footer {
78 cwd: &state.cwd,
79 model: &state.model_name,
80 mode: &state.permission_mode,
81 cost: state.total_cost,
82 git_branch: state.git_branch.as_deref(),
83 is_streaming: state.is_streaming,
84 is_pending: state.is_pending,
85 is_paused: state.is_paused,
86 spinner_frame: state.spinner_frame,
87 elapsed_secs: state.elapsed_secs,
88 },
89 layout.footer,
90 );
91
92 frame.render_widget(ToastStack::new(&state.toasts), full);
93
94 if state.tool_history.detail_open {
95 if let Some(idx) = state.tool_history.selected {
96 let rows = flat_rows(state);
97 if let Some(row) = rows.get(idx) {
98 let (mi, ti) = match row {
99 HistoryRow::Tool { msg, tool } => (*msg, *tool),
100 HistoryRow::Sub { msg, tool, .. } => (*msg, *tool),
101 };
102 let tool = &state.conversation.messages[mi].tool_uses[ti];
103 frame.render_widget(ToolDetail::new(tool), full);
104 }
105 }
106 }
107
108 match &state.modal.active {
109 Some(ModalKind::Permission { tool_name, description, choice }) => {
110 let chat = layout.messages;
113 let dialog_area = ratatui::layout::Rect {
114 x: chat.x,
115 y: full.y,
116 width: chat.width,
117 height: full.height,
118 };
119 frame.render_widget(
120 PermissionDialog::new(tool_name, description, *choice),
121 dialog_area,
122 );
123 }
124 Some(ModalKind::Select {
125 title,
126 query,
127 options,
128 selected,
129 current_value,
130 footer_hint,
131 ..
132 }) => {
133 frame.render_widget(
134 DialogSelect::new(title, query, options, *selected)
135 .with_current(current_value.as_deref())
136 .with_footer(footer_hint.as_deref()),
137 full,
138 );
139 }
140 Some(ModalKind::Info { title, rows }) => {
141 frame.render_widget(InfoDialog::new(title, rows), full);
142 }
143 Some(ModalKind::Input { title, prompt, buffer, .. }) => {
144 frame.render_widget(InputDialog::new(title, prompt, buffer), full);
145 }
146 Some(ModalKind::QuitConfirm) => {}
147 None => {}
148 }
149 }
150}
151
152impl Default for Renderer {
153 fn default() -> Self { Self }
154}