Skip to main content

tui/overlay/
placement.rs

1//! Where the user put a dialog and the size they gave it; none until it was moved or sized.
2
3use ratatui::layout::Rect;
4use tvui::{Change, Place, changed};
5
6#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
7pub struct Placement {
8    origin: Option<(i32, i32)>,
9    size: Option<(u16, u16)>,
10}
11
12impl Placement {
13    /// The place of a dialog that opens at `auto` inside `host`, limited as TV limits a window
14    /// within the least size `min`.
15    #[must_use]
16    pub fn place(self, auto: Rect, min: (u16, u16), host: Rect) -> Place {
17        if self == Self::default() {
18            return Place::from(auto);
19        }
20        let (width, height) = self.size.unwrap_or((auto.width, auto.height));
21        let (x, y) = self
22            .origin
23            .unwrap_or((i32::from(auto.x), i32::from(auto.y)));
24        let at = Place {
25            x,
26            y,
27            width,
28            height,
29        };
30        changed(at, host, Change::Move(0, 0), min)
31    }
32
33    /// The placement that puts a dialog opened at `auto` at `place`.
34    #[must_use]
35    pub fn of(place: Place, auto: Rect) -> Self {
36        Self {
37            origin: Some((place.x, place.y)),
38            size: Some((place.width, place.height))
39                .filter(|&size| size != (auto.width, auto.height)),
40        }
41    }
42}