1use 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#[derive(Debug)]
21pub enum ConfirmAction {
22 DeleteTransaction(String),
23 DeleteMarked(Vec<String>),
24 DeleteTag {
26 id: String,
27 label: String,
28 },
29 DeleteTemplate {
31 id: String,
32 label: String,
33 },
34 DeleteScript {
36 id: String,
37 label: String,
38 },
39 RemoveSshKey(String),
41 MergeAccounts(Box<Form>),
43 DiscardForm,
45 OverwriteCsv(std::path::PathBuf),
47 Quit,
49}
50
51impl ConfirmAction {
52 pub fn default_intent(&self) -> Intent {
54 match self {
55 ConfirmAction::DiscardForm => Intent::CloseTopmost,
56 _ => Intent::ConfirmYes,
57 }
58 }
59}
60
61#[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 focus: Option<usize>,
74 },
75}
76
77impl Modal {
78 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 #[must_use]
89 pub fn scrolls(&self) -> bool {
90 matches!(self, Modal::Csv(_) | Modal::Draft(_))
91 }
92
93 #[must_use]
95 pub fn sizeable(&self) -> bool {
96 !matches!(self, Modal::Confirm { .. })
97 }
98
99 #[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 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 #[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}