1
//! Functions that construct and push overlay modals onto the app's overlay stack.
2

            
3
use crate::tabs::ssh_keys::SshKeysTab;
4
use crate::tabs::tags::TagsTab;
5
use forms::FormValues;
6

            
7
use crate::app::App;
8
use crate::form::Form;
9
use crate::modal::Modal;
10
use crate::tabs::config::ConfigTab;
11
use crate::tabs::list::ListTab;
12
use crate::view::{Tab, ViewId};
13

            
14
13
pub(super) fn open_config_set_modal(app: &mut App, args: &[(String, String)]) {
15
13
    let mut form = Form::config_set();
16
13
    for (arg, text) in args {
17
24
        if let Some(field) = form.fields.iter_mut().find(|f| f.key == Some(arg.as_str())) {
18
16
            field.widget.set_text(text);
19
        }
20
    }
21
13
    app.open_dialog_for(ViewId::Config, Modal::Form(form));
22
13
}
23

            
24
/// Open config-set on the selected row; says why when there is none.
25
12
pub(super) fn open_config_edit_for_selected(app: &mut App) {
26
12
    let Some((key, value)) = app.front::<ConfigTab>().and_then(ConfigTab::selected_edit) else {
27
5
        let reason = if app.front::<ConfigTab>().is_some_and(ConfigTab::any_loading) {
28
1
            "config is loading"
29
        } else {
30
4
            "no config key selected"
31
        };
32
5
        app.set_status(reason);
33
5
        return;
34
    };
35
7
    open_config_set_modal(
36
7
        app,
37
7
        &[("key".to_string(), key), ("value".to_string(), value)],
38
7
    );
39
12
}
40

            
41
/// Opens the add-key form, its key field filled from a palette `key=` arg.
42
5
pub(super) fn open_ssh_key_add_modal(app: &mut App, args: &[(String, String)]) {
43
5
    let mut form = Form::ssh_key_add();
44
5
    for (arg, text) in args {
45
1
        if let Some(field) = form.fields.iter_mut().find(|f| f.key == Some(arg.as_str())) {
46
1
            field.widget.set_text(text);
47
        }
48
    }
49
5
    app.open_dialog_for(ViewId::SshKeys, Modal::Form(form));
50
5
}
51

            
52
/// Confirms removing the `fingerprint` key, or the selected one; says why when there is none.
53
7
pub(super) fn open_ssh_key_remove(app: &mut App, fingerprint: Option<String>) {
54
7
    let selected = app
55
7
        .front_is(Tab::SshKeys)
56
4
        .then(|| {
57
4
            app.front::<SshKeysTab>()
58
4
                .and_then(|keys| keys.selected_key().map(str::to_string))
59
4
        })
60
        .flatten();
61
7
    match fingerprint.or(selected) {
62
4
        Some(fingerprint) => app.confirm_remove_ssh_key(fingerprint),
63
3
        None => app.set_status("no SSH key selected"),
64
    }
65
7
}
66

            
67
9
pub(super) fn open_tag_create_modal(app: &mut App) {
68
9
    app.open_dialog_for(ViewId::Tags, Modal::Form(Form::tag_create()));
69
9
}
70

            
71
/// Load the selected tag into the edit form; sets a status when none is selected.
72
10
pub(super) fn open_tag_edit_for_selected(app: &mut App) {
73
10
    match app
74
10
        .front::<TagsTab>()
75
10
        .and_then(|tab| tab.selected_key().map(str::to_string))
76
    {
77
6
        Some(id) => app.open_tag_edit_async(&id),
78
4
        None => app.set_status("no tag selected"),
79
    }
80
10
}
81

            
82
/// Confirms deleting the selected tag; sets a status when none is selected.
83
4
pub(super) fn confirm_delete_selected_tag(app: &mut App) {
84
4
    match app
85
4
        .front::<TagsTab>()
86
4
        .and_then(|tab| tab.selected_row().cloned())
87
    {
88
3
        Some(row) => app.confirm_delete_tag(row),
89
1
        None => app.set_status("no tag selected"),
90
    }
91
4
}
92

            
93
3
pub(super) fn open_commodity_create_modal(app: &mut App) {
94
3
    app.open_dialog_for(ViewId::Commodities, Modal::Form(Form::commodity_create()));
95
3
}
96

            
97
1
pub(super) fn open_commodity_convert_modal(app: &mut App) {
98
1
    app.open_dialog_for(ViewId::Commodities, Modal::Form(Form::commodity_convert()));
99
1
    app.fetch_commodity_form_options();
100
1
}
101

            
102
1
pub(super) fn open_account_create_modal(app: &mut App) {
103
1
    app.open_dialog_for(ViewId::Accounts, Modal::Form(Form::account_create()));
104
1
    app.fetch_account_form_options();
105
1
}
106

            
107
/// Open account-merge from the selected account; sets a status when none is selected.
108
5
pub(super) fn open_account_merge_for_selected(app: &mut App) {
109
5
    let Some(id) = app.selected_id(Tab::Accounts) else {
110
1
        app.set_status("no account selected");
111
1
        return;
112
    };
113
4
    let mut values = FormValues::default();
114
4
    values.set("from", &id);
115
4
    app.open_account_merge(values);
116
5
}
117

            
118
6
pub(super) fn open_transaction_create_modal(app: &mut App) {
119
6
    app.open_dialog_for(
120
6
        ViewId::Transactions,
121
6
        Modal::Form(Form::transaction_create()),
122
6
    );
123
6
    app.fetch_transaction_form_options();
124
6
}
125

            
126
/// Open transaction-create with the selected account as the first row's From.
127
4
pub(super) fn open_transaction_create_modal_for_selected(app: &mut App) {
128
4
    let Some(id) = app.selected_id(Tab::Accounts) else {
129
1
        app.set_status("no account selected");
130
1
        return;
131
    };
132
3
    let mut form = Form::transaction_create();
133
3
    form.preselect_from_account(&id);
134
3
    app.open_dialog_for(Tab::Accounts, Modal::Form(form));
135
3
    app.fetch_transaction_form_options();
136
4
}
137

            
138
14
pub(super) fn open_transaction_filter_modal(app: &mut App) {
139
14
    let filter = app
140
14
        .front_list(Tab::Transactions)
141
14
        .map(ListTab::filter)
142
14
        .unwrap_or_default();
143
14
    app.open_dialog_for(
144
14
        ViewId::Transactions,
145
14
        Modal::Form(Form::transaction_filter(&filter)),
146
14
    );
147
14
    app.fetch_account_form_options();
148
14
}
149

            
150
/// Load the selected account into the edit form; sets a status when none is selected.
151
5
pub(super) fn open_account_edit_for_selected(app: &mut App) {
152
5
    match app.selected_id(Tab::Accounts) {
153
2
        Some(id) => app.open_account_edit_async(&id),
154
3
        None => app.set_status("no account selected"),
155
    }
156
5
}
157

            
158
/// Load the selected commodity into the edit form; sets a status when none is selected.
159
5
pub(super) fn open_commodity_edit_for_selected(app: &mut App) {
160
5
    match app.selected_id(Tab::Commodities) {
161
2
        Some(id) => app.open_commodity_edit_async(&id),
162
3
        None => app.set_status("no commodity selected"),
163
    }
164
5
}