tui_popup/popup.rs
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
use std::fmt::Debug;
use crate::PopupState;
use derive_setters::Setters;
use ratatui::{
prelude::{Buffer, Line, Rect, Style, Text},
symbols::border::Set,
widgets::{Block, Borders, Clear, StatefulWidgetRef, Widget, WidgetRef},
};
use std::cmp::min;
/// Configuration for a popup.
///
/// This struct is used to configure a [`Popup`]. It can be created using
/// [`Popup::new`](Popup::new).
///
/// # Example
///
/// ```rust
/// use ratatui::{prelude::*, symbols::border};
/// use tui_popup::Popup;
///
/// fn render_popup(frame: &mut Frame) {
/// let popup = Popup::new("Press any key to exit")
/// .title("tui-popup demo")
/// .style(Style::new().white().on_blue())
/// .border_set(border::ROUNDED)
/// .border_style(Style::new().bold());
/// frame.render_widget(&popup, frame.size());
/// }
/// ```
#[derive(Debug, Setters)]
#[setters(into)]
#[non_exhaustive]
pub struct Popup<'content, W: SizedWidgetRef> {
/// The body of the popup.
#[setters(skip)]
pub body: W,
/// The title of the popup.
pub title: Line<'content>,
/// The style to apply to the entire popup.
pub style: Style,
/// The borders of the popup.
pub borders: Borders,
/// The symbols used to render the border.
pub border_set: Set,
/// Border style
pub border_style: Style,
}
/// A trait for widgets that have a fixed size.
///
/// This trait allows the popup to automatically size itself based on the size of the body widget.
/// Implementing this trait for a widget allows it to be used as the body of a popup. You can also
/// wrap existing widgets in a newtype and implement this trait for the newtype to use them as the
/// body of a popup.
pub trait SizedWidgetRef: WidgetRef + Debug {
fn width(&self) -> usize;
fn height(&self) -> usize;
}
impl<'content, W: SizedWidgetRef> Popup<'content, W> {
/// Create a new popup with the given title and body with all the borders.
///
/// # Parameters
///
/// - `body` - The body of the popup. This can be any type that can be converted into a
/// [`Text`].
///
/// # Example
///
/// ```rust
/// use tui_popup::Popup;
///
/// let popup = Popup::new("Press any key to exit").title("tui-popup demo");
/// ```
pub fn new(body: W) -> Self {
Self {
body,
borders: Borders::ALL,
border_set: Set::default(),
border_style: Style::default(),
title: Line::default(),
style: Style::default(),
}
}
}
impl SizedWidgetRef for Text<'_> {
fn width(&self) -> usize {
self.width()
}
fn height(&self) -> usize {
self.height()
}
}
impl SizedWidgetRef for &str {
fn width(&self) -> usize {
Text::from(*self).width()
}
fn height(&self) -> usize {
Text::from(*self).height()
}
}
#[derive(Debug)]
pub struct SizedWrapper<W: Debug> {
pub inner: W,
pub width: usize,
pub height: usize,
}
impl<W: WidgetRef + Debug> WidgetRef for SizedWrapper<W> {
fn render_ref(&self, area: Rect, buf: &mut Buffer) {
self.inner.render_ref(area, buf);
}
}
impl<W: WidgetRef + Debug> SizedWidgetRef for SizedWrapper<W> {
fn width(&self) -> usize {
self.width
}
fn height(&self) -> usize {
self.height
}
}
impl<W: SizedWidgetRef> WidgetRef for Popup<'_, W> {
fn render_ref(&self, area: Rect, buf: &mut Buffer) {
let mut state = PopupState::default();
StatefulWidgetRef::render_ref(self, area, buf, &mut state);
}
}
impl<W: SizedWidgetRef> StatefulWidgetRef for Popup<'_, W> {
type State = PopupState;
fn render_ref(&self, area: Rect, buf: &mut Buffer, state: &mut Self::State) {
let area = if let Some(next) = state.area.take() {
// ensure that the popup remains on screen
let width = min(next.width, area.width);
let height = min(next.height, area.height);
let x = next.x.clamp(buf.area.x, area.right() - width);
let y = next.y.clamp(buf.area.y, area.bottom() - height);
Rect::new(x, y, width, height)
} else {
let border_height = usize::from(self.borders.intersects(Borders::TOP))
+ usize::from(self.borders.intersects(Borders::BOTTOM));
let border_width = usize::from(self.borders.intersects(Borders::LEFT))
+ usize::from(self.borders.intersects(Borders::RIGHT));
let height = self
.body
.height()
.saturating_add(border_height)
.try_into()
.unwrap_or(area.height);
let width = self
.body
.width()
.saturating_add(border_width)
.try_into()
.unwrap_or(area.width);
centered_rect(width, height, area)
};
state.area.replace(area);
Clear.render(area, buf);
let block = Block::default()
.borders(self.borders)
.border_set(self.border_set)
.border_style(self.border_style)
.title(self.title.clone())
.style(self.style);
block.render_ref(area, buf);
self.body.render_ref(block.inner(area), buf);
}
}
/// Create a rectangle centered in the given area.
fn centered_rect(width: u16, height: u16, area: Rect) -> Rect {
Rect {
x: area.width.saturating_sub(width) / 2,
y: area.height.saturating_sub(height) / 2,
width: min(width, area.width),
height: min(height, area.height),
}
}