Lines
96.26 %
Functions
55.56 %
Branches
100 %
//! Intents applied to the focused overlay's content: forms, their widgets and text editors.
use super::Intent;
use super::widget_rows::{apply_rows_intent, apply_select_intent, apply_tags_intent};
use crate::form::Form;
use crate::modal::Modal;
use crate::widgets::{Editor, FocusedSubWidget, Widget, WidgetKind};
pub(super) fn apply_modal_intent(modal: &mut Modal, intent: Intent) {
match modal {
Modal::Form(form) => match intent {
Intent::NextField => form.cycle(true),
Intent::PrevField => form.cycle(false),
Intent::CellNext => step_cell(form, true),
Intent::CellPrev => step_cell(form, false),
Intent::MoveUp | Intent::CellUp => step_vertical(form, false, intent),
Intent::MoveDown | Intent::CellDown => step_vertical(form, true, intent),
Intent::CompleteSuggestion => form.complete_hint(),
_ => apply_to_focused(form, intent),
},
Modal::Csv(csv) => csv.scroll_by(intent),
Modal::Draft(draft) => draft.scroll_by(intent),
Modal::Confirm { .. } => match intent {
Intent::NextField => modal.cycle_confirm(true),
Intent::PrevField => modal.cycle_confirm(false),
_ => {}
Modal::FilterBuilder(_) => {}
}
/// Tab walks the cells of a Splits or Rows field and yields to the field cycle at its edge.
fn step_cell(form: &mut Form, forward: bool) {
form.close_select();
if leave_at_edge(form, forward, form.focused_cells_boundary()) {
return;
let cell = if forward {
Intent::CellNext
} else {
Intent::CellPrev
};
apply_to_focused(form, cell);
/// Moves to the next or previous control when `edges` says the focus is at that end.
fn leave_at_edge(form: &mut Form, forward: bool, edges: Option<(bool, bool)>) -> bool {
let at_edge = edges.is_some_and(|(first, last)| if forward { last } else { first });
if at_edge {
form.cycle(forward);
at_edge
/// Up/Down move inside a grid or text area and leave it at its first or last row.
fn step_vertical(form: &mut Form, down: bool, intent: Intent) {
if leave_at_edge(form, down, form.focused_vertical_boundary()) {
let intent = match intent {
Intent::CellDown => Intent::RowNext,
Intent::CellUp => Intent::RowPrev,
other => other,
apply_to_focused(form, intent);
fn apply_to_focused(form: &mut Form, intent: Intent) {
if let Some(field) = form.focused_field_mut() {
apply_widget_intent(&mut field.widget, intent);
fn apply_widget_intent(widget: &mut Widget, intent: Intent) {
match widget {
Widget::Text(ed) => apply_editor_intent(ed, intent),
Widget::Amount(aw) => match intent {
Intent::InsertChar(c) => aw.insert_char(c),
_ => apply_editor_intent(aw.as_editor_mut(), intent),
Widget::Date(dw) => match intent {
Intent::DateStepForward => dw.step(1),
Intent::DateStepBack => dw.step(-1),
_ => apply_editor_intent(dw.as_editor_mut(), intent),
Widget::Select(sw) => apply_select_intent(sw, intent),
Widget::Rows(rw) => apply_rows_intent(rw, intent),
Widget::Splits(sw) if sw.tags_open() => apply_tags_intent(sw, intent),
Widget::Splits(sw) => match intent {
Intent::ToggleSplitTags => sw.open_tags(),
Intent::CellNext => sw.advance_cell(),
Intent::CellPrev => sw.retreat_cell(),
Intent::RowNext => sw.next_row(),
Intent::RowPrev => sw.prev_row(),
Intent::SplitAddRow => sw.add_row(),
Intent::SplitRemoveRow => sw.remove_row(),
_ => match (intent, sw.focused_subwidget_kind()) {
(Intent::SelectCancel, WidgetKind::Select) => sw.cancel_select(),
(Intent::SelectOpen, WidgetKind::Select) => sw.open_select(),
(
Intent::SelectNext
| Intent::SelectPrev
| Intent::SelectPick(_)
| Intent::SelectPageUp
| Intent::SelectPageDown
| Intent::InsertChar(_)
| Intent::DeleteBackward,
WidgetKind::Select,
) => sw.edit_select(|select| apply_select_intent(select, intent)),
(Intent::SelectConfirm, WidgetKind::Select) => {
if let Some(select) = sw.focused_select_mut() {
apply_select_intent(select, intent);
(_, WidgetKind::Select) => {}
(intent, _) => {
if let Some(FocusedSubWidget::Amount(a)) = sw.focused_subwidget_mut() {
match intent {
Intent::InsertChar(c) => a.insert_char(c),
_ => apply_editor_intent(a.as_editor_mut(), intent),
pub(super) fn apply_editor_intent(editor: &mut Editor, intent: Intent) {
Intent::InsertChar(c) => editor.insert_char(c),
Intent::DeleteBackward => editor.delete_backward(),
Intent::MoveLeft => editor.move_left(),
Intent::MoveRight => editor.move_right(),
Intent::MoveHome => editor.move_home(),
Intent::MoveEnd => editor.move_end(),
Intent::MoveUp => editor.move_up(),
Intent::MoveDown => editor.move_down(),
Intent::ScrollPageUp => editor.page_up(),
Intent::ScrollPageDown => editor.page_down(),
Intent::InsertNewline => editor.insert_newline(),
Intent::KillToEnd => editor.kill_to_end(),
Intent::KillWordBackward => editor.kill_word_backward(),
Intent::MoveWordForward => editor.word_forward(),
Intent::MoveWordBackward => editor.word_backward(),
Intent::DeleteForward => editor.delete_forward(),
Intent::DeleteWordForward => editor.delete_word_forward(),