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
use crate::{Background, BootDisplay, Webpage};
use ramhorns::{Template, Content};

#[derive(Content)]
pub struct Dialog {
    #[md]
    text: String,
    left_button: String,
    right_button: String,
}

#[derive(Debug, Copy, Clone, PartialEq)]
pub enum DialogOption {
    Left,
    Right,
    Default
}

impl Dialog {
    pub fn new<S1, S2, S3>(text: S1, left_button: S2, right_button: S3) -> Self
        where S1: Into<String>,
              S2: Into<String>,
              S3: Into<String>,
    {
        Self {
            text: text.into(),
            left_button: left_button.into(),
            right_button: right_button.into()
        }
    }
    
    pub fn no_yes<S: Into<String>>(message: S) -> bool {
        match Dialog::new(message, "No", "Yes").show() {
            DialogOption::Left => false,
            DialogOption::Right => true,
            DialogOption::Default => false
        }
    }
    
    pub fn yes_no<S: Into<String>>(message: S) -> bool {
        match Dialog::new(message, "Yes", "No").show() {
            DialogOption::Left => true,
            DialogOption::Right => false,
            DialogOption::Default => false
        }
    }
    
    pub fn ok_cancel<S: Into<String>>(message: S) -> bool {
        match Dialog::new(message, "Ok", "Cancel").show() {
            DialogOption::Left => true,
            DialogOption::Right => false,
            DialogOption::Default => false
        }
    }

    pub fn show(&self) -> DialogOption {
        let tpl = Template::new(include_str!("templates/dialog.html")).unwrap();

        let response = Webpage::new()
            .background(Background::BlurredScreenshot)
            .file("index.html", &tpl.render(self))
            .boot_display(BootDisplay::BlurredScreenshot)
            .open()
            .unwrap();

        match response.get_last_url().unwrap() {
            "http://localhost/left" => DialogOption::Left,
            "http://localhost/right" => DialogOption::Right,
            // Until this is reworked to offer a default option on forceful closure
            _ => DialogOption::Default
        } 
    }
}