1
//! Where the user put a dialog and the size they gave it; none until it was moved or sized.
2

            
3
use ratatui::layout::Rect;
4
use tvui::{Change, Place, changed};
5

            
6
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
7
pub struct Placement {
8
    origin: Option<(i32, i32)>,
9
    size: Option<(u16, u16)>,
10
}
11

            
12
impl 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
749
    pub fn place(self, auto: Rect, min: (u16, u16), host: Rect) -> Place {
17
749
        if self == Self::default() {
18
665
            return Place::from(auto);
19
84
        }
20
84
        let (width, height) = self.size.unwrap_or((auto.width, auto.height));
21
84
        let (x, y) = self
22
84
            .origin
23
84
            .unwrap_or((i32::from(auto.x), i32::from(auto.y)));
24
84
        let at = Place {
25
84
            x,
26
84
            y,
27
84
            width,
28
84
            height,
29
84
        };
30
84
        changed(at, host, Change::Move(0, 0), min)
31
749
    }
32

            
33
    /// The placement that puts a dialog opened at `auto` at `place`.
34
    #[must_use]
35
47
    pub fn of(place: Place, auto: Rect) -> Self {
36
        Self {
37
47
            origin: Some((place.x, place.y)),
38
47
            size: Some((place.width, place.height))
39
47
                .filter(|&size| size != (auto.width, auto.height)),
40
        }
41
47
    }
42
}