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
use crossterm::style::ContentStyle;

use crate::{
    canvas::{CanvasLike, CanvasLikeExt},
    drawable::{dbox::Dbox, extended_impl::Stylable, Drawable},
    frame::Frame,
    layout::{
        axis::Axis,
        sized::{Align, Anchor, KnownHeight, KnownWidth},
        Dims, Pos,
    },
};

use super::fullscreen_popup::FullScreenPopup;

pub struct Popup {
    title: String,
    texts: Option<Vec<String>>,
    pub box_style: ContentStyle,
    pub text_style: ContentStyle,
}

impl Popup {
    pub fn new<S>(title: S) -> Self
    where
        S: Into<String>,
    {
        Self {
            title: title.into(),
            texts: None,
            box_style: ContentStyle::default(),
            text_style: ContentStyle::default(),
        }
    }

    pub fn with_texts<S, TS>(mut self, texts: TS) -> Self
    where
        S: Into<String>,
        TS: IntoIterator<Item = S>,
    {
        self.texts = Some(texts.into_iter().map(Into::into).collect());
        self
    }

    fn size(&self) -> Dims {
        let width = match self.texts {
            Some(ref texts) => {
                texts
                    .iter()
                    .map(|t| t.chars().count())
                    .max()
                    .unwrap_or(0) // longest of texts
                    .max(self.title.chars().count()) as i32
                    + 2
                    + 2
            }
            None => 2 + 2 + self.title.chars().count() as i32,
        };

        let height = match self.texts {
            Some(ref texts) => 2 + 2 + texts.len() as i32,
            None => 3,
        };

        Pos::new(width, height)
    }

    pub fn with_box_style(mut self, style: ContentStyle) -> Self {
        self.box_style = style;
        self
    }

    pub fn with_text_style(mut self, style: ContentStyle) -> Self {
        self.text_style = style;
        self
    }

    pub fn to_window(self) -> FullScreenPopup {
        FullScreenPopup::new(self)
    }
}

// We impl for ref because we don't want to move the popup after each draw
impl Drawable for &mut Popup {
    type X = Align;
    type Y = Align;

    fn draw(self, pos: impl Into<Pos<Align, Align>>, frame: &mut impl CanvasLike) {
        fn draw_inner(
            title: &str,
            texts: Option<&Vec<String>>,
            box_style: ContentStyle,
            text_style: ContentStyle,
            Pos { x, y }: Dims,
            box_size: Dims,
            frame: Frame,
        ) -> () {
            let mut frame = Frame::new(frame).ml(x).mt(y).with_size(box_size);
            let mut inner = frame.clone().mx(1).my(1);

            let title_pos = Align::new(Anchor::Center, title.w() + 2).calc(box_size.x - 2);

            frame.draw((0, 0), Dbox::new(box_size).styled(box_style));
            inner.draw((title_pos, 0), format!(" {} ", title).styled(text_style));

            if let Some(texts) = texts {
                inner.draw((0, 1), "─".repeat(box_size.x as usize - 2));
                for (i, text) in texts.iter().enumerate() {
                    inner.draw((1, i as i32 + 2), text.styled(text_style))
                }
            }
        }

        let Pos { x, y } = pos.into();

        draw_inner(
            &self.title,
            self.texts.as_ref(),
            self.box_style,
            self.text_style,
            Pos::new(x.calc(frame.w()), y.calc(frame.h())),
            self.size(),
            Frame::new(frame),
        );
    }
}

impl KnownWidth for Popup {
    fn w(&self) -> i32 {
        self.size().x
    }
}

impl KnownHeight for Popup {
    fn h(&self) -> i32 {
        self.size().y
    }
}