Skip to main content

tui_kit/
popup.rs

1use ratatui::{layout::Rect, widgets::Clear, Frame};
2
3/// Computes a centered popup [`Rect`], renders a [`Clear`] over it (erasing
4/// whatever is behind), and returns the area to draw into.
5///
6/// # Parameters
7///
8/// - `width_pct`  — fraction of the screen width (0.0–1.0).
9/// - `max_width`  — hard cap in columns.
10/// - `height`     — fixed row count (clamped to screen height).
11///
12/// # Example
13///
14/// ```ignore
15/// let area = centered_popup(f, 0.7, 90, 26);
16/// f.render_widget(popup_block(title, &theme), area);
17/// ```
18pub fn centered_popup(f: &mut Frame, width_pct: f32, max_width: u16, height: u16) -> Rect {
19    let screen = f.area();
20    let width = (screen.width as f32 * width_pct).min(max_width as f32) as u16;
21    let height = height.min(screen.height);
22    let x = screen.width.saturating_sub(width) / 2;
23    let y = screen.height.saturating_sub(height) / 2;
24    let area = Rect { x, y, width, height };
25    f.render_widget(Clear, area);
26    area
27}