1
//! Intents applied to the focused overlay's content: forms, their widgets and text editors.
2

            
3
use super::Intent;
4
use super::widget_rows::{apply_rows_intent, apply_select_intent, apply_tags_intent};
5
use crate::form::Form;
6
use crate::modal::Modal;
7
use crate::widgets::{Editor, FocusedSubWidget, Widget, WidgetKind};
8

            
9
1429
pub(super) fn apply_modal_intent(modal: &mut Modal, intent: Intent) {
10
1429
    match modal {
11
1389
        Modal::Form(form) => match intent {
12
222
            Intent::NextField => form.cycle(true),
13
31
            Intent::PrevField => form.cycle(false),
14
25
            Intent::CellNext => step_cell(form, true),
15
4
            Intent::CellPrev => step_cell(form, false),
16
20
            Intent::MoveUp | Intent::CellUp => step_vertical(form, false, intent),
17
33
            Intent::MoveDown | Intent::CellDown => step_vertical(form, true, intent),
18
1
            Intent::CompleteSuggestion => form.complete_hint(),
19
1053
            _ => apply_to_focused(form, intent),
20
        },
21
1
        Modal::Csv(csv) => csv.scroll_by(intent),
22
25
        Modal::Draft(draft) => draft.scroll_by(intent),
23
14
        Modal::Confirm { .. } => match intent {
24
7
            Intent::NextField => modal.cycle_confirm(true),
25
6
            Intent::PrevField => modal.cycle_confirm(false),
26
1
            _ => {}
27
        },
28
        Modal::FilterBuilder(_) => {}
29
    }
30
1429
}
31

            
32
/// Tab walks the cells of a Splits or Rows field and yields to the field cycle at its edge.
33
29
fn step_cell(form: &mut Form, forward: bool) {
34
29
    form.close_select();
35
29
    if leave_at_edge(form, forward, form.focused_cells_boundary()) {
36
3
        return;
37
26
    }
38
26
    let cell = if forward {
39
23
        Intent::CellNext
40
    } else {
41
3
        Intent::CellPrev
42
    };
43
26
    apply_to_focused(form, cell);
44
29
}
45

            
46
/// Moves to the next or previous control when `edges` says the focus is at that end.
47
82
fn leave_at_edge(form: &mut Form, forward: bool, edges: Option<(bool, bool)>) -> bool {
48
82
    let at_edge = edges.is_some_and(|(first, last)| if forward { last } else { first });
49
82
    if at_edge {
50
33
        form.cycle(forward);
51
49
    }
52
82
    at_edge
53
82
}
54

            
55
/// Up/Down move inside a grid or text area and leave it at its first or last row.
56
53
fn step_vertical(form: &mut Form, down: bool, intent: Intent) {
57
53
    if leave_at_edge(form, down, form.focused_vertical_boundary()) {
58
30
        return;
59
23
    }
60
23
    let intent = match intent {
61
9
        Intent::CellDown => Intent::RowNext,
62
4
        Intent::CellUp => Intent::RowPrev,
63
10
        other => other,
64
    };
65
23
    apply_to_focused(form, intent);
66
53
}
67

            
68
1102
fn apply_to_focused(form: &mut Form, intent: Intent) {
69
1102
    if let Some(field) = form.focused_field_mut() {
70
1102
        apply_widget_intent(&mut field.widget, intent);
71
    }
72
1102
}
73

            
74
1102
fn apply_widget_intent(widget: &mut Widget, intent: Intent) {
75
1102
    match widget {
76
524
        Widget::Text(ed) => apply_editor_intent(ed, intent),
77
7
        Widget::Amount(aw) => match intent {
78
6
            Intent::InsertChar(c) => aw.insert_char(c),
79
1
            _ => apply_editor_intent(aw.as_editor_mut(), intent),
80
        },
81
92
        Widget::Date(dw) => match intent {
82
6
            Intent::DateStepForward => dw.step(1),
83
8
            Intent::DateStepBack => dw.step(-1),
84
78
            _ => apply_editor_intent(dw.as_editor_mut(), intent),
85
        },
86
163
        Widget::Select(sw) => apply_select_intent(sw, intent),
87
38
        Widget::Rows(rw) => apply_rows_intent(rw, intent),
88
278
        Widget::Splits(sw) if sw.tags_open() => apply_tags_intent(sw, intent),
89
221
        Widget::Splits(sw) => match intent {
90
14
            Intent::ToggleSplitTags => sw.open_tags(),
91
5
            Intent::CellNext => sw.advance_cell(),
92
2
            Intent::CellPrev => sw.retreat_cell(),
93
10
            Intent::RowNext => sw.next_row(),
94
4
            Intent::RowPrev => sw.prev_row(),
95
3
            Intent::SplitAddRow => sw.add_row(),
96
2
            Intent::SplitRemoveRow => sw.remove_row(),
97
181
            _ => match (intent, sw.focused_subwidget_kind()) {
98
12
                (Intent::SelectCancel, WidgetKind::Select) => sw.cancel_select(),
99
31
                (Intent::SelectOpen, WidgetKind::Select) => sw.open_select(),
100
                (
101
                    Intent::SelectNext
102
                    | Intent::SelectPrev
103
                    | Intent::SelectPick(_)
104
                    | Intent::SelectPageUp
105
                    | Intent::SelectPageDown
106
                    | Intent::InsertChar(_)
107
                    | Intent::DeleteBackward,
108
                    WidgetKind::Select,
109
74
                ) => sw.edit_select(|select| apply_select_intent(select, intent)),
110
                (Intent::SelectConfirm, WidgetKind::Select) => {
111
7
                    if let Some(select) = sw.focused_select_mut() {
112
7
                        apply_select_intent(select, intent);
113
                    }
114
                }
115
12
                (_, WidgetKind::Select) => {}
116
                (intent, _) => {
117
45
                    if let Some(FocusedSubWidget::Amount(a)) = sw.focused_subwidget_mut() {
118
45
                        match intent {
119
27
                            Intent::InsertChar(c) => a.insert_char(c),
120
18
                            _ => apply_editor_intent(a.as_editor_mut(), intent),
121
                        }
122
                    }
123
                }
124
            },
125
        },
126
    }
127
1102
}
128

            
129
4501
pub(super) fn apply_editor_intent(editor: &mut Editor, intent: Intent) {
130
4501
    match intent {
131
4299
        Intent::InsertChar(c) => editor.insert_char(c),
132
3
        Intent::DeleteBackward => editor.delete_backward(),
133
105
        Intent::MoveLeft => editor.move_left(),
134
12
        Intent::MoveRight => editor.move_right(),
135
13
        Intent::MoveHome => editor.move_home(),
136
10
        Intent::MoveEnd => editor.move_end(),
137
7
        Intent::MoveUp => editor.move_up(),
138
3
        Intent::MoveDown => editor.move_down(),
139
12
        Intent::ScrollPageUp => editor.page_up(),
140
2
        Intent::ScrollPageDown => editor.page_down(),
141
9
        Intent::InsertNewline => editor.insert_newline(),
142
2
        Intent::KillToEnd => editor.kill_to_end(),
143
2
        Intent::KillWordBackward => editor.kill_word_backward(),
144
3
        Intent::MoveWordForward => editor.word_forward(),
145
4
        Intent::MoveWordBackward => editor.word_backward(),
146
7
        Intent::DeleteForward => editor.delete_forward(),
147
2
        Intent::DeleteWordForward => editor.delete_word_forward(),
148
6
        _ => {}
149
    }
150
4501
}