Lines
95.56 %
Functions
80 %
Branches
100 %
//! Modal overlay types.
//!
//! The TUI keeps a LIFO stack of modals via [`crate::overlay::OverlayStack`].
//! The topmost modal owns keyboard focus and is rendered on top of the current
//! tab.
use crate::event::Intent;
pub use crate::form::Form;
pub use buttons::Spec;
pub use csv::Csv;
pub use draft::Draft;
pub use filter_builder::{FilterBuilder, Opened};
mod buttons;
mod csv;
mod draft;
mod filter_builder;
/// Action to execute when a confirm overlay is accepted with `y` or Enter.
#[derive(Debug)]
pub enum ConfirmAction {
DeleteTransaction(String),
DeleteMarked(Vec<String>),
/// Delete the tag with this id; `label` names it in the replies.
DeleteTag {
id: String,
label: String,
},
/// Delete the template with this id; `label` names it in the replies.
DeleteTemplate {
/// Delete the script with this id; `label` names it in the replies.
DeleteScript {
/// Remove the SSH key with this fingerprint.
RemoveSshKey(String),
/// Submit the `account-merge` form; a rejection reopens it.
MergeAccounts(Box<Form>),
/// Close the form below this confirm and drop its edits.
DiscardForm,
/// Write the shown CSV over the existing file at this path.
OverwriteCsv(std::path::PathBuf),
/// Leave the application.
Quit,
}
impl ConfirmAction {
/// What Enter does: confirm, except that the discard prompt keeps editing.
pub fn default_intent(&self) -> Intent {
match self {
ConfirmAction::DiscardForm => Intent::CloseTopmost,
_ => Intent::ConfirmYes,
/// Anything renderable as an overlay. Kept as an enum rather than a trait
/// object so modal state stays `Debug` and survives through the [`crate::overlay::OverlayStack`].
pub enum Modal {
Form(Form),
Csv(Csv),
Draft(Draft),
FilterBuilder(FilterBuilder),
Confirm {
prompt: String,
action: ConfirmAction,
/// The focused button of Yes/No; `None` is the one Enter presses by default.
focus: Option<usize>,
impl Modal {
/// The one Yes/No box every confirmation is built through; `action` runs on Yes.
pub fn confirm(prompt: impl Into<String>, action: ConfirmAction) -> Modal {
Modal::Confirm {
prompt: prompt.into(),
action,
focus: None,
/// Whether the body scrolls with the wheel and a scrollbar: the read-only overlays.
#[must_use]
pub fn scrolls(&self) -> bool {
matches!(self, Modal::Csv(_) | Modal::Draft(_))
/// Whether the dialog can be sized, as its content reflows; a confirm only moves.
pub fn sizeable(&self) -> bool {
!matches!(self, Modal::Confirm { .. })
/// The button the focus is on: a form's, or a confirm prompt's focused one.
pub fn focused_button(&self) -> Option<usize> {
Modal::Form(form) => form.focused_button(),
Modal::Confirm { action, focus, .. } => focus.or_else(|| {
let default = action.default_intent();
self.button_specs()
.iter()
.position(|&(_, intent)| intent == default)
}),
_ => None,
/// Moves the focus of a confirm prompt one button on, wrapping.
pub fn cycle_confirm(&mut self, forward: bool) {
let len = self.button_specs().len();
let Some(current) = self.focused_button() else {
return;
};
if let Modal::Confirm { focus, .. } = self {
*focus = Some(if forward {
(current + 1) % len
} else {
(current + len - 1) % len
});
/// Whether closing would drop edits.
pub fn is_dirty(&self) -> bool {
Modal::Form(form) => form.is_dirty(),
Modal::FilterBuilder(builder) => builder.is_dirty(),
_ => false,