Skip to main content

tui_popup/
known_size.rs

1use ratatui_core::text::Text;
2
3/// A trait for widgets that have a fixed size.
4///
5/// This trait allows the popup to automatically size itself based on the size of the body widget.
6/// Implementing this trait for a widget allows it to be used as the body of a popup. You can also
7/// wrap existing widgets in a newtype and implement this trait for the newtype to use them as the
8/// body of a popup.
9///
10/// This trait was previously called `SizedWidgetRef`, but was renamed to [`KnownSize`] to avoid
11/// confusion with the `WidgetRef` trait from `ratatui`.
12pub trait KnownSize {
13    fn width(&self) -> usize;
14    fn height(&self) -> usize;
15}
16
17impl KnownSize for Text<'_> {
18    fn width(&self) -> usize {
19        self.width()
20    }
21
22    fn height(&self) -> usize {
23        self.height()
24    }
25}
26
27impl KnownSize for &Text<'_> {
28    fn width(&self) -> usize {
29        Text::width(self)
30    }
31
32    fn height(&self) -> usize {
33        Text::height(self)
34    }
35}
36
37impl KnownSize for &str {
38    fn width(&self) -> usize {
39        Text::from(*self).width()
40    }
41
42    fn height(&self) -> usize {
43        Text::from(*self).height()
44    }
45}
46
47impl KnownSize for String {
48    fn width(&self) -> usize {
49        Text::from(self.as_str()).width()
50    }
51
52    fn height(&self) -> usize {
53        Text::from(self.as_str()).height()
54    }
55}