1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
//! Modal dialog component
//!
//! Provides overlay modals for confirmations, inputs, and messages.
use crate::tui::state::Modal;
use crate::tui::theme::Theme;
use ratatui::{
layout::{Alignment, Constraint, Direction, Layout, Rect},
style::{Modifier, Style},
text::{Line, Span},
widgets::{Block, Borders, Clear, Paragraph, Wrap},
Frame,
};
// Re-export modal types from state
pub use crate::tui::state::{ConfirmAction, InputAction};
/// Modal view component
pub struct ModalView;
impl ModalView {
/// Create new modal view
pub fn new() -> Self {
Self
}
/// Render a modal dialog (static method)
pub fn render(frame: &mut Frame, area: Rect, modal: &Modal, input_buffer: &str, theme: &Theme) {
render_modal_impl(frame, area, modal, input_buffer, theme);
}
}
impl Default for ModalView {
fn default() -> Self {
Self::new()
}
}
/// Legacy render function for backward compatibility
pub fn render(frame: &mut Frame, area: Rect, modal: &Modal, input_buffer: &str, theme: &Theme) {
render_modal_impl(frame, area, modal, input_buffer, theme);
}
/// Internal modal rendering implementation
fn render_modal_impl(
frame: &mut Frame,
area: Rect,
modal: &Modal,
input_buffer: &str,
theme: &Theme,
) {
// Create centered modal area with better proportions
let modal_area = {
let vertical = Layout::default()
.direction(Direction::Vertical)
.constraints([
Constraint::Percentage(25), // Top margin
Constraint::Min(10), // Modal content (min 10 lines)
Constraint::Percentage(25), // Bottom margin
])
.split(area);
Layout::default()
.direction(Direction::Horizontal)
.constraints([
Constraint::Percentage(15), // Left margin
Constraint::Min(40), // Modal content (min 40 chars)
Constraint::Percentage(15), // Right margin
])
.split(vertical[1])[1]
};
// Clear the background
frame.render_widget(Clear, modal_area);
match modal {
Modal::Confirm { title, message, .. } => {
let block = Block::default()
.title(title.as_str())
.borders(Borders::ALL)
.border_style(Style::default().fg(theme.warning));
let text = vec![
Line::from(""),
Line::from(message.as_str()),
Line::from(""),
Line::from(vec![
Span::styled(
"y",
Style::default()
.fg(theme.success)
.add_modifier(Modifier::BOLD),
),
Span::raw(": Yes "),
Span::styled(
"n",
Style::default()
.fg(theme.error)
.add_modifier(Modifier::BOLD),
),
Span::raw(": No"),
]),
];
let paragraph = Paragraph::new(text)
.block(block)
.alignment(Alignment::Center)
.wrap(Wrap { trim: true });
frame.render_widget(paragraph, modal_area);
}
Modal::Input { title, prompt, .. } => {
let block = Block::default()
.title(title.as_str())
.borders(Borders::ALL)
.border_style(Style::default().fg(theme.accent));
let cursor = "▌";
let text = vec![
Line::from(""),
Line::from(prompt.as_str()),
Line::from(""),
Line::from(vec![
Span::styled("> ", Style::default().fg(theme.accent)),
Span::styled(input_buffer, Style::default().fg(theme.fg)),
Span::styled(
cursor,
Style::default()
.fg(theme.accent)
.add_modifier(Modifier::SLOW_BLINK),
),
]),
Line::from(""),
Line::from(vec![
Span::styled(
"Enter",
Style::default()
.fg(theme.success)
.add_modifier(Modifier::BOLD),
),
Span::raw(": Submit "),
Span::styled(
"Esc",
Style::default()
.fg(theme.error)
.add_modifier(Modifier::BOLD),
),
Span::raw(": Cancel"),
]),
];
let paragraph = Paragraph::new(text)
.block(block)
.alignment(Alignment::Center)
.wrap(Wrap { trim: true });
frame.render_widget(paragraph, modal_area);
}
Modal::Error { title, message } => {
let block = Block::default()
.title(title.as_str())
.borders(Borders::ALL)
.border_style(Style::default().fg(theme.error));
let text = vec![
Line::from(""),
Line::from(Span::styled(
message.as_str(),
Style::default().fg(theme.error),
)),
Line::from(""),
Line::from(vec![
Span::styled(
"Enter",
Style::default()
.fg(theme.success)
.add_modifier(Modifier::BOLD),
),
Span::raw(": Close"),
]),
];
let paragraph = Paragraph::new(text)
.block(block)
.alignment(Alignment::Center)
.wrap(Wrap { trim: true });
frame.render_widget(paragraph, modal_area);
}
Modal::Info { title, message } => {
let block = Block::default()
.title(title.as_str())
.borders(Borders::ALL)
.border_style(Style::default().fg(theme.primary));
let text = vec![
Line::from(""),
Line::from(message.as_str()),
Line::from(""),
Line::from(vec![
Span::styled(
"Enter",
Style::default()
.fg(theme.success)
.add_modifier(Modifier::BOLD),
),
Span::raw(": Close"),
]),
];
let paragraph = Paragraph::new(text)
.block(block)
.alignment(Alignment::Center)
.wrap(Wrap { trim: true });
frame.render_widget(paragraph, modal_area);
}
Modal::Success { title, message } => {
let block = Block::default()
.title(title.as_str())
.borders(Borders::ALL)
.border_style(Style::default().fg(theme.success));
let text = vec![
Line::from(""),
Line::from(Span::styled(
message.as_str(),
Style::default().fg(theme.success),
)),
Line::from(""),
Line::from(vec![
Span::styled(
"Enter",
Style::default()
.fg(theme.success)
.add_modifier(Modifier::BOLD),
),
Span::raw(": Close"),
]),
];
let paragraph = Paragraph::new(text)
.block(block)
.alignment(Alignment::Center)
.wrap(Wrap { trim: true });
frame.render_widget(paragraph, modal_area);
}
}
}