1
//! The script keys of the Scripts tab.
2

            
3
use crate::app::App;
4
use crate::form::Form;
5
use crate::modal::Modal;
6
use crate::tabs::scripts::ScriptsTab;
7
use crate::view::Tab;
8

            
9
/// What a script key asks for.
10
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
11
pub enum ScriptKey {
12
    /// Switch the selected script on or off.
13
    Toggle,
14
}
15

            
16
8
pub(super) fn apply_tab(app: &mut App, key: ScriptKey) {
17
8
    if !app.front_is(Tab::Scripts) {
18
        return;
19
8
    }
20
8
    match key {
21
8
        ScriptKey::Toggle => toggle_selected(app),
22
8
    }
23
8
}
24

            
25
7
pub(super) fn open_create(app: &mut App) {
26
7
    app.open_dialog_for(Tab::Scripts, Modal::Form(Form::script_create()));
27
7
}
28

            
29
/// Loads the selected script into the edit form; says why when none is selected.
30
10
pub(super) fn open_edit_for_selected(app: &mut App) {
31
10
    match app
32
10
        .front::<ScriptsTab>()
33
10
        .and_then(|tab| tab.selected_key().map(str::to_string))
34
    {
35
3
        Some(id) => app.open_script_edit_async(&id),
36
7
        None => app.set_status("no script selected"),
37
    }
38
10
}
39

            
40
/// Confirms deleting the selected script; says why when none is selected.
41
9
pub(super) fn confirm_delete_selected(app: &mut App) {
42
9
    match app
43
9
        .front::<ScriptsTab>()
44
9
        .and_then(|tab| tab.selected_row().cloned())
45
    {
46
5
        Some(row) => app.confirm_delete_script(row),
47
4
        None => app.set_status("no script selected"),
48
    }
49
9
}
50

            
51
8
fn toggle_selected(app: &mut App) {
52
8
    match app
53
8
        .front::<ScriptsTab>()
54
8
        .and_then(|tab| tab.selected_row().cloned())
55
    {
56
6
        Some(row) => app.toggle_script(&row),
57
2
        None => app.set_status("no script selected"),
58
    }
59
8
}