Lines
100 %
Functions
78.57 %
Branches
//! The buttons each kind of modal offers and the intent each one presses.
use super::Modal;
use crate::event::{BuilderKey, Intent};
/// A button label with `~X~` hotkey marks, and what pressing it does.
pub type Spec = (&'static str, Intent);
const APPLY_CLOSE: [Spec; 2] = [
("A~p~ply", Intent::Builder(BuilderKey::Apply)),
("~C~lose", Intent::CloseTopmost),
];
const YES_NO: [Spec; 2] = [
("~Y~es", Intent::ConfirmYes),
("~N~o", Intent::CloseTopmost),
const CLOSE: [Spec; 1] = [("~C~lose", Intent::CloseTopmost)];
impl Modal {
/// The button Enter and Space press: the focused one, else the one that submits, else the
/// only one.
#[must_use]
pub fn press_button(&self) -> Option<usize> {
self.focused_button()
.or_else(|| {
self.button_specs()
.iter()
.position(|&(_, intent)| intent == Intent::Submit)
})
.or_else(|| (self.button_specs().len() == 1).then_some(0))
}
/// What Enter and Space do: the intent of [`Modal::press_button`], else a submit.
pub fn press_intent(&self) -> Intent {
let spec = self
.press_button()
.and_then(|at| self.button_specs().get(at));
spec.map_or(Intent::Submit, |&(_, intent)| intent)
/// The button of each choice this modal offers, with the intent it presses.
pub fn button_specs(&self) -> &'static [Spec] {
match self {
Modal::Form(form) => form.buttons(),
Modal::FilterBuilder(_) => &APPLY_CLOSE,
Modal::Confirm { .. } => &YES_NO,
Modal::Csv(_) | Modal::Draft(_) => &CLOSE,