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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
use cursive_core::{
    Cursive,
    With,
    view::Nameable,
    event::{Event, Key},
    views::{EditView, Dialog, OnEventView, NamedView, Checkbox, LinearLayout, TextView},
    utils::markup::StyledString,
    theme::{
        PaletteColor,
        Color,
        BaseColor,
        ColorType,
        ColorStyle,
        BorderStyle,
        Theme
    }
};
use std::fmt::Display;
use crate::views::LoadingAnimation;

/// Convenience function that generates a better looking Cursive theme
/// 
/// # Example
/// ```
/// # use cursive_extras::*;
/// let mut root = cursive::default();
/// root.set_theme(better_theme());
/// ```
pub fn better_theme() -> Theme {
    let mut theme_def = Theme {
        shadow: false,
        .. Theme::default()
    };
    theme_def.palette[PaletteColor::Background] = Color::TerminalDefault;
    theme_def.palette[PaletteColor::Primary] = Color::Light(BaseColor::White);
    theme_def.palette[PaletteColor::View] = Color::TerminalDefault;
    theme_def.palette[PaletteColor::Highlight] = Color::Light(BaseColor::Blue);
    theme_def.palette[PaletteColor::HighlightText] = Color::Dark(BaseColor::White);
    theme_def.palette[PaletteColor::Secondary] = Color::Dark(BaseColor::Blue);
    theme_def.palette[PaletteColor::TitlePrimary] = Color::Light(BaseColor::Blue);
    theme_def.borders = BorderStyle::Outset;
    theme_def
}

/// Convenience function that creates a dialog that runs a callback if the
/// user selects "Yes"
/// 
/// # Example
/// ```
/// # use cursive_extras::*;
/// let mut root = cursive::default();
/// # root.set_theme(better_theme());
/// root.add_layer(confirm_dialog("Are you sure?", "Are you sure?", |view| view.quit()));
/// root.run();
/// ```
pub fn confirm_dialog<C, T, U>(title: T, text: U, cb: C) -> OnEventView<Dialog>
    where
        C: Fn(&mut Cursive) + 'static,
        T: Display,
        U: Display
{
    Dialog::text(text.to_string())
        .dismiss_button("No")
        .button("Yes", cb)
        .title(title.to_string())
        .wrap_with(OnEventView::new)
        .on_event(Event::Key(Key::Esc), |v| { v.pop_layer(); })
}

/// Same as `confirm_dialog()`, but accepts `StyledString`s instead for the text
pub fn confirm_dialog_styled<T:Display, C: Fn(&mut Cursive) + 'static>(title: T, text: StyledString, cb: C) -> OnEventView<Dialog> {
    Dialog::text(text)
        .dismiss_button("No")
        .button("Yes", cb)
        .title(title.to_string())
        .wrap_with(OnEventView::new)
        .on_event(Event::Key(Key::Esc), |v| { v.pop_layer(); })
}

/// Convenience function that shows a user a dialog box
/// with a message that includes a back button
/// 
/// # Example
/// ```
/// # use cursive_extras::*;
/// let mut root = cursive::default();
/// # root.set_theme(better_theme());
/// root.add_layer(info_dialog("Info", "This is important!"));
/// root.run();
/// ```
pub fn info_dialog<T: Display, U: Display>(title: T, text: U) -> OnEventView<Dialog> {
    Dialog::text(text.to_string())
        .dismiss_button("Back")
        .title(title.to_string())
        .wrap_with(OnEventView::new)
        .on_event(Event::Key(Key::Esc), |v| { v.pop_layer(); })
}

/// Sames as `info_dialog()`, but accepts `StyledString`s instead for the text
pub fn info_dialog_styled<T: Display>(title: T, text: StyledString) -> OnEventView<Dialog> {
    Dialog::text(text)
        .dismiss_button("Back")
        .title(title.to_string())
        .wrap_with(OnEventView::new)
        .on_event(Event::Key(Key::Esc), |v| { v.pop_layer(); })
}

/// Convenience function that creates a named `EditView` that has a better
/// looking style and can optionally act as a password entry box
/// 
/// # Example
/// ```
/// # use cursive::views::Dialog;
/// # use cursive_extras::*;
/// let mut root = cursive::default();
/// # root.set_theme(better_theme());
/// root.add_fullscreen_layer(
///     Dialog::around(styled_editview("yes", "edit", false))
///         .button("Quit", |view| view.quit())
///         .title("Styled EditView Example")
/// );
/// root.run();
/// ```
pub fn styled_editview<C: Display>(content: C, view_name: &'static str, password: bool) -> NamedView<EditView> {
    styled_editview_color(content, view_name, password, PaletteColor::Highlight)
}

/// Same as `styled_editview()` but allows for a color to be chosen instead of using the highlight color
pub fn styled_editview_color<T: Display, C: Into<ColorType>>(content: T, view_name: &'static str, password: bool, color: C) -> NamedView<EditView> {
    let input_style = ColorStyle::new(
        color,
        Color::Light(BaseColor::White),
    );
    let view = EditView::new().content(content.to_string()).style(input_style).filler(" ");

    if password {
        view.secret().with_name(view_name)
    }
    else {
        view.with_name(view_name)
    }
}

/// Convenience function that return the state of a named check box
/// 
/// true: the box is checked
/// 
/// false: of course it isn't checked!
pub fn get_checkbox_option(view: &mut Cursive, name: &str) -> bool {
    let cbox = view.find_name::<Checkbox>(name).unwrap();
    cbox.is_checked()
}

/// Convenience function that returns a horizontal `LinearLayout` that is a named check box with a label
pub fn labeled_checkbox(text: &str, name: &str, checked: bool) -> LinearLayout {
    labeled_checkbox_cb(text, name, checked, |_, _| { })
}

/// Same as `labeled_checkbox()` but also accepts a closure to execute when the check box's state changes
pub fn labeled_checkbox_cb<C: Fn(&mut Cursive, bool) + 'static>(text: &str, name: &str, checked: bool, callback: C) -> LinearLayout {
    LinearLayout::horizontal()
        .child(Checkbox::new().with_checked(checked).on_change(callback).with_name(name))
        .child(TextView::new(format!(" {}", text)))
}

/// Convenience function that shows a loading pop up
pub fn load_resource<T, R, F>(view: &mut Cursive, title: &str, msg: &str, task: R, finish_task: F)
    where
        T: Send + Sync + 'static,
        R: FnOnce() -> T + Send + Sync + 'static,
        F: FnOnce(&mut Cursive, T) + Send + Sync + 'static
{
    let loader = LoadingAnimation::new(msg, move || (task(), finish_task));
    view.add_layer(
        Dialog::around(loader.with_name("load")).title(title)
            .wrap_with(OnEventView::new)
            .on_event(Event::Refresh, |view: &mut Cursive| {
                let mut loader = view.find_name::<LoadingAnimation<(T, F)>>("load").unwrap();
                if loader.is_done() {
                    view.pop_layer();
                    let (val, finish_func) = loader.finish().unwrap();
                    finish_func(view, val);
                }
            })
    );
}