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
use std::io;

use crossterm::event::{read, Event, KeyCode, KeyEvent, KeyEventKind};

use crate::{
    canvas::{CanvasLike, CanvasLikeExt},
    drawable::Drawable,
    layout::{
        align::Align,
        sized::{KnownHeight, KnownWidth},
        Pos,
    },
    renderer::Renderer,
};

use super::{popup::Popup, Window};

pub struct FullScreenPopup(pub Popup);

impl FullScreenPopup {
    pub fn new(popup: Popup) -> Self {
        Self(popup)
    }
}

impl Window for FullScreenPopup {
    type Output = io::Result<KeyCode>;

    fn run(&mut self, renderer: &mut Renderer) -> Self::Output {
        loop {
            renderer
                .get_render_space()
                .show((Align::Center, Align::Center), &mut *self);
            renderer.render()?;

            let event = read()?;
            if let Event::Key(KeyEvent { code, kind, .. }) = event {
                if kind != KeyEventKind::Release {
                    break Ok(code);
                }
            }

            renderer.on_event(&event)?;
        }
    }
}

impl Drawable for &mut FullScreenPopup {
    type X = Align;
    type Y = Align;

    fn draw(&self, pos: impl Into<Pos<Align, Align>>, canvas: &mut impl CanvasLike) {
        <&Popup as Drawable>::draw(&&self.0, pos, canvas);
    }
}

impl KnownWidth for FullScreenPopup {
    fn w(&self) -> i32 {
        self.0.w()
    }
}

impl KnownHeight for FullScreenPopup {
    fn h(&self) -> i32 {
        self.0.h()
    }
}