Skip to main content

tui/
modal.rs

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
7use crate::event::Intent;
8pub use crate::form::Form;
9pub use buttons::Spec;
10pub use csv::Csv;
11pub use draft::Draft;
12pub use filter_builder::{FilterBuilder, Opened};
13
14mod buttons;
15mod csv;
16mod draft;
17mod filter_builder;
18
19/// Action to execute when a confirm overlay is accepted with `y` or Enter.
20#[derive(Debug)]
21pub 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
51impl ConfirmAction {
52    /// What Enter does: confirm, except that the discard prompt keeps editing.
53    pub fn default_intent(&self) -> Intent {
54        match self {
55            ConfirmAction::DiscardForm => Intent::CloseTopmost,
56            _ => Intent::ConfirmYes,
57        }
58    }
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)]
64pub 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
77impl Modal {
78    /// The one Yes/No box every confirmation is built through; `action` runs on Yes.
79    pub fn confirm(prompt: impl Into<String>, action: ConfirmAction) -> Modal {
80        Modal::Confirm {
81            prompt: prompt.into(),
82            action,
83            focus: None,
84        }
85    }
86
87    /// Whether the body scrolls with the wheel and a scrollbar: the read-only overlays.
88    #[must_use]
89    pub fn scrolls(&self) -> bool {
90        matches!(self, Modal::Csv(_) | Modal::Draft(_))
91    }
92
93    /// Whether the dialog can be sized, as its content reflows; a confirm only moves.
94    #[must_use]
95    pub fn sizeable(&self) -> bool {
96        !matches!(self, Modal::Confirm { .. })
97    }
98
99    /// The button the focus is on: a form's, or a confirm prompt's focused one.
100    #[must_use]
101    pub fn focused_button(&self) -> Option<usize> {
102        match self {
103            Modal::Form(form) => form.focused_button(),
104            Modal::Confirm { action, focus, .. } => focus.or_else(|| {
105                let default = action.default_intent();
106                self.button_specs()
107                    .iter()
108                    .position(|&(_, intent)| intent == default)
109            }),
110            _ => None,
111        }
112    }
113
114    /// Moves the focus of a confirm prompt one button on, wrapping.
115    pub fn cycle_confirm(&mut self, forward: bool) {
116        let len = self.button_specs().len();
117        let Some(current) = self.focused_button() else {
118            return;
119        };
120        if let Modal::Confirm { focus, .. } = self {
121            *focus = Some(if forward {
122                (current + 1) % len
123            } else {
124                (current + len - 1) % len
125            });
126        }
127    }
128
129    /// Whether closing would drop edits.
130    #[must_use]
131    pub fn is_dirty(&self) -> bool {
132        match self {
133            Modal::Form(form) => form.is_dirty(),
134            Modal::FilterBuilder(builder) => builder.is_dirty(),
135            _ => false,
136        }
137    }
138}