Lines
92 %
Functions
66.67 %
Branches
100 %
//! Actions of the topmost overlay: close, confirm and submit.
use super::submit::{execute_confirm, submit_form};
use crate::app::App;
use crate::form::FormKind;
use crate::modal::{ConfirmAction, Modal};
use crate::widgets::WidgetKind;
/// Esc closes an open split tag list first, else the overlay.
pub(super) fn close_topmost(app: &mut App) {
if let Some(Modal::Form(form)) = app.overlays.top_mut()
&& form.close_tags()
{
return;
}
if matches!(app.overlays.top(), Some(Modal::Form(form)) if form.kind == FormKind::Options) {
return app.cancel_options();
if app.overlays.top().is_some_and(Modal::is_dirty) {
app.overlays.push(Modal::confirm(
"Discard changes?",
ConfirmAction::DiscardForm,
));
app.overlays.pop();
/// Confirm the topmost overlay, but only when it is a `Modal::Confirm`.
pub(super) fn confirm_topmost(app: &mut App) {
if !matches!(app.overlays.top(), Some(Modal::Confirm { .. })) {
act_on_top(app, |app, modal| {
if let Modal::Confirm { action, .. } = modal {
execute_confirm(app, action);
});
/// Pops the topmost overlay for `act`, during which its window still counts as the dialog's.
fn act_on_top(app: &mut App, act: impl FnOnce(&mut App, Modal)) {
let origin = app.overlays.origin();
let Some(modal) = app.overlays.pop() else {
};
app.overlays.set_origin(origin);
act(app, modal);
app.overlays.release();
/// Whether the topmost overlay is a template form.
pub(super) fn template_form_open(app: &App) -> bool {
matches!(app.overlays.top(), Some(Modal::Form(form)) if form.is_template())
/// Whether C-t does something: it tests a template form, or opens the focused split's tags.
pub(super) fn toggles_split_tags(app: &App) -> bool {
template_form_open(app)
|| matches!(app.overlays.top(), Some(Modal::Form(form))
if form.focused_widget_kind() == Some(WidgetKind::Splits))
/// Apply Enter to the topmost overlay (form submit or confirm action).
pub(super) fn submit_modal(app: &mut App) {
act_on_top(app, |app, modal| match modal {
Modal::Confirm { action, .. } => execute_confirm(app, action),
Modal::Form(form) => submit_form(app, form),
Modal::Csv(_) | Modal::Draft(_) | Modal::FilterBuilder(_) => {}