use super::App;
use ratatui::{
Frame,
layout::{Constraint, Direction, Layout, Rect},
style::{Modifier, Style},
text::{Line, Span},
widgets::{Block, Borders, Clear, LineGauge, Paragraph, Wrap},
};
pub fn render_popup(f: &mut Frame, app: &App) {
if !app.is_installing {
return;
}
let area = centered_rect(55, 35, f.area());
f.render_widget(Clear, area);
let block = Block::default()
.title(" active command [q to abort] ")
.borders(Borders::ALL)
.border_style(Style::default().fg(app.theme.accent));
let inner_area = block.inner(area);
f.render_widget(block, area);
let chunks = Layout::default()
.direction(Direction::Vertical)
.margin(1)
.constraints(
[
Constraint::Length(1), Constraint::Length(1), Constraint::Length(1), Constraint::Length(1), Constraint::Min(0), ]
.as_ref(),
)
.split(inner_area);
let spinner_chars = crate::ui::SHARK_FRAMES;
let time_ms = std::time::SystemTime::now()
.duration_since(std::time::SystemTime::UNIX_EPOCH)
.unwrap_or_default()
.as_millis();
let spinner_idx = ((time_ms / 120) % spinner_chars.len() as u128) as usize;
let spinner = spinner_chars[spinner_idx];
let action_text = Paragraph::new(Line::from(vec![
Span::styled(spinner, Style::default().fg(app.theme.accent)),
Span::raw(" "),
Span::raw(&app.current_action),
]));
f.render_widget(action_text, chunks[0]);
if app.progress == 0 {
let width = (chunks[2].width as usize).max(4);
let pos = ((time_ms / 60) % width as u128) as usize;
let mut chars = vec!['─'; width];
for i in 0..4 {
let idx = (pos + i) % width;
chars[idx] = '━';
}
let anim_str: String = chars.into_iter().collect();
let anim_text = Paragraph::new(Line::from(vec![Span::styled(
anim_str,
Style::default()
.fg(app.theme.accent)
.add_modifier(Modifier::BOLD),
)]));
f.render_widget(anim_text, chunks[2]);
} else {
let ratio = f64::from(app.progress).clamp(0.0, 100.0) / 100.0;
let gauge = LineGauge::default()
.filled_style(
Style::default()
.fg(app.theme.accent)
.add_modifier(Modifier::BOLD),
)
.filled_symbol("━")
.unfilled_symbol("─")
.ratio(ratio);
f.render_widget(gauge, chunks[2]);
}
let log_lines: Vec<Line> = app
.transaction_logs
.iter()
.map(|log| Line::from(log.clone()))
.collect();
let logs_paragraph = Paragraph::new(log_lines)
.style(Style::default().fg(app.theme.muted))
.wrap(Wrap { trim: false })
.block(Block::default().borders(Borders::NONE));
f.render_widget(logs_paragraph, chunks[4]);
}
pub fn render_confirm_popup(f: &mut Frame, app: &App) {
if !app.show_prompt {
return;
}
let area = centered_rect(40, 20, f.area());
f.render_widget(Clear, area);
let action_color = if app.prompt_type == "install" {
app.theme.success
} else {
app.theme.error
};
let block = Block::default()
.title(" confirm transaction ")
.borders(Borders::ALL)
.border_style(Style::default().fg(action_color));
let text = vec![
Line::from(""),
Line::from(vec![
Span::raw(format!("proceed to {} ", app.prompt_type)),
Span::styled(
format!("{}", app.prompt_targets.len()),
Style::default().add_modifier(Modifier::BOLD),
),
Span::raw(" package(s)?"),
]),
Line::from(""),
Line::from(Span::styled(
" [Y]es [N]o ",
Style::default().fg(app.theme.muted),
)),
];
let paragraph = Paragraph::new(text)
.block(block)
.alignment(ratatui::layout::Alignment::Center);
f.render_widget(paragraph, area);
}
fn centered_rect(percent_x: u16, percent_y: u16, r: Rect) -> Rect {
let popup_layout = Layout::default()
.direction(Direction::Vertical)
.constraints(
[
Constraint::Percentage((100 - percent_y) / 2),
Constraint::Percentage(percent_y),
Constraint::Percentage((100 - percent_y) / 2),
]
.as_ref(),
)
.split(r);
Layout::default()
.direction(Direction::Horizontal)
.constraints(
[
Constraint::Percentage((100 - percent_x) / 2),
Constraint::Percentage(percent_x),
Constraint::Percentage((100 - percent_x) / 2),
]
.as_ref(),
)
.split(popup_layout[1])[1]
}