|
| 1 | +use ratatui::{ |
| 2 | + Frame, |
| 3 | + prelude::*, |
| 4 | + widgets::{Block, BorderType, Borders, Clear, Paragraph, Wrap}, |
| 5 | +}; |
| 6 | + |
| 7 | +use crate::ui::app::{App, DocsLang}; |
| 8 | +use crate::ui::theme; |
| 9 | +use super::get_steps; |
| 10 | + |
| 11 | +pub fn render_tutorial_overlay(f: &mut Frame, term: Rect, app: &App) { |
| 12 | + let steps = get_steps(app.tutorial.tab); |
| 13 | + if steps.is_empty() { return; } |
| 14 | + let step = &steps[app.tutorial.step_idx]; |
| 15 | + let total = steps.len(); |
| 16 | + let idx = app.tutorial.step_idx; |
| 17 | + |
| 18 | + // Highlight target area |
| 19 | + let target = (step.target)(term, app); |
| 20 | + if let Some(t) = target { |
| 21 | + let highlight = Block::default() |
| 22 | + .borders(Borders::ALL) |
| 23 | + .border_type(BorderType::Thick) |
| 24 | + .border_style(Style::default().fg(Color::Yellow)); |
| 25 | + f.render_widget(highlight, t); |
| 26 | + } |
| 27 | + |
| 28 | + let (title, body) = match app.tutorial.lang { |
| 29 | + DocsLang::En => (step.title_en, step.body_en), |
| 30 | + DocsLang::PtBr => (step.title_pt, step.body_pt), |
| 31 | + }; |
| 32 | + |
| 33 | + // Compute popup size |
| 34 | + let max_w: u16 = 64.min(term.width.saturating_sub(2)); |
| 35 | + let inner_w = max_w.saturating_sub(2) as usize; |
| 36 | + let body_lines = wrap_text(body, inner_w); |
| 37 | + let popup_h: u16 = (body_lines.len() as u16) + 6; // 2 border + 1 title + 1 blank + 1 nav + 1 blank |
| 38 | + let popup_h = popup_h.min(term.height.saturating_sub(2)); |
| 39 | + let popup_w = max_w; |
| 40 | + |
| 41 | + // Position popup |
| 42 | + let popup_rect = best_popup_rect(target, popup_w, popup_h, term); |
| 43 | + |
| 44 | + f.render_widget(Clear, popup_rect); |
| 45 | + |
| 46 | + let title_str = format!(" ▶ {} ", title); |
| 47 | + let block = Block::default() |
| 48 | + .borders(Borders::ALL) |
| 49 | + .border_type(BorderType::Rounded) |
| 50 | + .border_style(Style::default().fg(Color::Yellow)) |
| 51 | + .title(Span::styled(title_str, Style::default().fg(Color::Yellow).bold())); |
| 52 | + |
| 53 | + let inner = block.inner(popup_rect); |
| 54 | + f.render_widget(block, popup_rect); |
| 55 | + |
| 56 | + // Body text |
| 57 | + let body_area = Rect { |
| 58 | + x: inner.x, |
| 59 | + y: inner.y, |
| 60 | + width: inner.width, |
| 61 | + height: inner.height.saturating_sub(2), |
| 62 | + }; |
| 63 | + f.render_widget( |
| 64 | + Paragraph::new(body) |
| 65 | + .style(Style::default().fg(theme::TEXT)) |
| 66 | + .wrap(Wrap { trim: false }), |
| 67 | + body_area, |
| 68 | + ); |
| 69 | + |
| 70 | + // Nav hint at bottom |
| 71 | + let nav_text = match app.tutorial.lang { |
| 72 | + DocsLang::En => format!(" ← Prev → Next [L]=PT-BR Esc=close [{}/{}]", idx + 1, total), |
| 73 | + DocsLang::PtBr => format!(" ← Ant → Próx [L]=EN Esc=fechar [{}/{}]", idx + 1, total), |
| 74 | + }; |
| 75 | + let nav_area = Rect { |
| 76 | + x: inner.x, |
| 77 | + y: inner.y + inner.height.saturating_sub(1), |
| 78 | + width: inner.width, |
| 79 | + height: 1, |
| 80 | + }; |
| 81 | + f.render_widget( |
| 82 | + Paragraph::new(nav_text).style(Style::default().fg(theme::LABEL)), |
| 83 | + nav_area, |
| 84 | + ); |
| 85 | +} |
| 86 | + |
| 87 | +fn wrap_text(text: &str, width: usize) -> Vec<String> { |
| 88 | + if width == 0 { return vec![text.to_string()]; } |
| 89 | + let mut lines = Vec::new(); |
| 90 | + for paragraph in text.split('\n') { |
| 91 | + if paragraph.is_empty() { |
| 92 | + lines.push(String::new()); |
| 93 | + continue; |
| 94 | + } |
| 95 | + let mut current = String::new(); |
| 96 | + for word in paragraph.split_whitespace() { |
| 97 | + if current.is_empty() { |
| 98 | + current = word.to_string(); |
| 99 | + } else if current.len() + 1 + word.len() <= width { |
| 100 | + current.push(' '); |
| 101 | + current.push_str(word); |
| 102 | + } else { |
| 103 | + lines.push(current.clone()); |
| 104 | + current = word.to_string(); |
| 105 | + } |
| 106 | + } |
| 107 | + if !current.is_empty() { |
| 108 | + lines.push(current); |
| 109 | + } |
| 110 | + } |
| 111 | + lines |
| 112 | +} |
| 113 | + |
| 114 | +/// Try positions: below → above → right → left → centered. |
| 115 | +fn best_popup_rect(target: Option<Rect>, pw: u16, ph: u16, term: Rect) -> Rect { |
| 116 | + let Some(t) = target else { |
| 117 | + return centered(pw, ph, term); |
| 118 | + }; |
| 119 | + |
| 120 | + // Below |
| 121 | + let below_y = t.y + t.height; |
| 122 | + if below_y + ph <= term.y + term.height { |
| 123 | + let x = clamp_x(t.x, pw, term); |
| 124 | + return Rect::new(x, below_y, pw, ph); |
| 125 | + } |
| 126 | + |
| 127 | + // Above |
| 128 | + if t.y >= term.y + ph { |
| 129 | + let x = clamp_x(t.x, pw, term); |
| 130 | + return Rect::new(x, t.y - ph, pw, ph); |
| 131 | + } |
| 132 | + |
| 133 | + // Right |
| 134 | + let right_x = t.x + t.width; |
| 135 | + if right_x + pw <= term.x + term.width { |
| 136 | + let y = clamp_y(t.y, ph, term); |
| 137 | + return Rect::new(right_x, y, pw, ph); |
| 138 | + } |
| 139 | + |
| 140 | + // Left |
| 141 | + if t.x >= term.x + pw { |
| 142 | + let y = clamp_y(t.y, ph, term); |
| 143 | + return Rect::new(t.x - pw, y, pw, ph); |
| 144 | + } |
| 145 | + |
| 146 | + // Centered fallback |
| 147 | + centered(pw, ph, term) |
| 148 | +} |
| 149 | + |
| 150 | +fn centered(pw: u16, ph: u16, term: Rect) -> Rect { |
| 151 | + Rect::new( |
| 152 | + term.x + term.width.saturating_sub(pw) / 2, |
| 153 | + term.y + term.height.saturating_sub(ph) / 2, |
| 154 | + pw, |
| 155 | + ph, |
| 156 | + ) |
| 157 | +} |
| 158 | + |
| 159 | +fn clamp_x(preferred: u16, pw: u16, term: Rect) -> u16 { |
| 160 | + let max_x = (term.x + term.width).saturating_sub(pw); |
| 161 | + preferred.min(max_x).max(term.x) |
| 162 | +} |
| 163 | + |
| 164 | +fn clamp_y(preferred: u16, ph: u16, term: Rect) -> u16 { |
| 165 | + let max_y = (term.y + term.height).saturating_sub(ph); |
| 166 | + preferred.min(max_y).max(term.y) |
| 167 | +} |
0 commit comments