1
//! Modal overlay types.
2
//!
3
//! The TUI keeps a LIFO stack of modals via [`crate::overlay::OverlayStack`].
4
//! The topmost modal owns keyboard focus and is rendered on top of the current
5
//! tab.
6

            
7
use crate::event::Intent;
8
pub use crate::form::Form;
9
pub use buttons::Spec;
10
pub use csv::Csv;
11
pub use draft::Draft;
12
pub use filter_builder::{FilterBuilder, Opened};
13

            
14
mod buttons;
15
mod csv;
16
mod draft;
17
mod filter_builder;
18

            
19
/// Action to execute when a confirm overlay is accepted with `y` or Enter.
20
#[derive(Debug)]
21
pub enum ConfirmAction {
22
    DeleteTransaction(String),
23
    DeleteMarked(Vec<String>),
24
    /// Delete the tag with this id; `label` names it in the replies.
25
    DeleteTag {
26
        id: String,
27
        label: String,
28
    },
29
    /// Delete the template with this id; `label` names it in the replies.
30
    DeleteTemplate {
31
        id: String,
32
        label: String,
33
    },
34
    /// Delete the script with this id; `label` names it in the replies.
35
    DeleteScript {
36
        id: String,
37
        label: String,
38
    },
39
    /// Remove the SSH key with this fingerprint.
40
    RemoveSshKey(String),
41
    /// Submit the `account-merge` form; a rejection reopens it.
42
    MergeAccounts(Box<Form>),
43
    /// Close the form below this confirm and drop its edits.
44
    DiscardForm,
45
    /// Write the shown CSV over the existing file at this path.
46
    OverwriteCsv(std::path::PathBuf),
47
    /// Leave the application.
48
    Quit,
49
}
50

            
51
impl ConfirmAction {
52
    /// What Enter does: confirm, except that the discard prompt keeps editing.
53
633
    pub fn default_intent(&self) -> Intent {
54
633
        match self {
55
241
            ConfirmAction::DiscardForm => Intent::CloseTopmost,
56
392
            _ => Intent::ConfirmYes,
57
        }
58
633
    }
59
}
60

            
61
/// Anything renderable as an overlay. Kept as an enum rather than a trait
62
/// object so modal state stays `Debug` and survives through the [`crate::overlay::OverlayStack`].
63
#[derive(Debug)]
64
pub enum Modal {
65
    Form(Form),
66
    Csv(Csv),
67
    Draft(Draft),
68
    FilterBuilder(FilterBuilder),
69
    Confirm {
70
        prompt: String,
71
        action: ConfirmAction,
72
        /// The focused button of Yes/No; `None` is the one Enter presses by default.
73
        focus: Option<usize>,
74
    },
75
}
76

            
77
impl Modal {
78
    /// The one Yes/No box every confirmation is built through; `action` runs on Yes.
79
96
    pub fn confirm(prompt: impl Into<String>, action: ConfirmAction) -> Modal {
80
96
        Modal::Confirm {
81
96
            prompt: prompt.into(),
82
96
            action,
83
96
            focus: None,
84
96
        }
85
96
    }
86

            
87
    /// Whether the body scrolls with the wheel and a scrollbar: the read-only overlays.
88
    #[must_use]
89
249
    pub fn scrolls(&self) -> bool {
90
249
        matches!(self, Modal::Csv(_) | Modal::Draft(_))
91
249
    }
92

            
93
    /// Whether the dialog can be sized, as its content reflows; a confirm only moves.
94
    #[must_use]
95
22
    pub fn sizeable(&self) -> bool {
96
22
        !matches!(self, Modal::Confirm { .. })
97
22
    }
98

            
99
    /// The button the focus is on: a form's, or a confirm prompt's focused one.
100
    #[must_use]
101
1131
    pub fn focused_button(&self) -> Option<usize> {
102
1131
        match self {
103
373
            Modal::Form(form) => form.focused_button(),
104
657
            Modal::Confirm { action, focus, .. } => focus.or_else(|| {
105
633
                let default = action.default_intent();
106
633
                self.button_specs()
107
633
                    .iter()
108
874
                    .position(|&(_, intent)| intent == default)
109
633
            }),
110
101
            _ => None,
111
        }
112
1131
    }
113

            
114
    /// Moves the focus of a confirm prompt one button on, wrapping.
115
13
    pub fn cycle_confirm(&mut self, forward: bool) {
116
13
        let len = self.button_specs().len();
117
13
        let Some(current) = self.focused_button() else {
118
            return;
119
        };
120
13
        if let Modal::Confirm { focus, .. } = self {
121
13
            *focus = Some(if forward {
122
7
                (current + 1) % len
123
            } else {
124
6
                (current + len - 1) % len
125
            });
126
        }
127
13
    }
128

            
129
    /// Whether closing would drop edits.
130
    #[must_use]
131
93
    pub fn is_dirty(&self) -> bool {
132
93
        match self {
133
29
            Modal::Form(form) => form.is_dirty(),
134
7
            Modal::FilterBuilder(builder) => builder.is_dirty(),
135
57
            _ => false,
136
        }
137
93
    }
138
}