1
//! The scopes of the keymap and the chain of them the focused layer uses,
2
//! innermost first. A chain stops at a layer that types text, so global view
3
//! keys never reach a text field.
4

            
5
use crate::app::App;
6
use crate::focus::{FocusTarget, current_focus};
7
use crate::keymap::VimState;
8
use crate::modal::Modal;
9
use crate::pane::PaneId;
10
use crate::tabs::nms::ConsoleState;
11
use crate::view::Tab;
12
use crate::viewer::{Source, Viewer};
13
use crate::widgets::{SelectWidget, WidgetKind};
14
use crate::window::WindowId;
15

            
16
#[cfg(test)]
17
mod tests;
18

            
19
pub(super) struct Scope {
20
    pub name: &'static str,
21
    /// The section name in Help.
22
    pub title: &'static str,
23
    /// Whether an unbound printable key is typed text when this scope is the innermost.
24
    pub text: bool,
25
}
26

            
27
pub(super) struct Scopes {
28
    pub names: Vec<&'static str>,
29
    /// The first of `names`.
30
    pub innermost: &'static str,
31
    pub text: bool,
32
}
33

            
34
macro_rules! scopes {
35
    ($($id:ident = $name:literal, $title:literal, $text:literal;)*) => {
36
        $(const $id: Scope = Scope { name: $name, title: $title, text: $text };)*
37
        /// Every scope a preset may bind.
38
        pub(super) const SCOPES: &[&Scope] = &[$(&$id),*];
39
    };
40
}
41

            
42
scopes! {
43
    GLOBAL = "global", "Everywhere", false;
44
    MENU = "menu", "Menu", false;
45
    DRAG = "drag", "Moving or sizing a window", false;
46
    PENDING = "pending", "While the server answers", false;
47
    GLOBAL_VIEW = "global-view", "Views", false;
48
    LIST = "list", "Lists", false;
49
    TAB_ACCOUNTS = "tab-accounts", "Accounts", false;
50
    TAB_TRANSACTIONS = "tab-transactions", "Transactions", false;
51
    TAB_COMMODITIES = "tab-commodities", "Commodities", false;
52
    TAB_REPORTS = "tab-reports", "Reports", false;
53
    TAB_CONFIG = "tab-config", "Config", false;
54
    TAB_SSH_KEYS = "tab-ssh-keys", "SSH keys", false;
55
    TAB_TAGS = "tab-tags", "Tags", false;
56
    TAB_TEMPLATES = "tab-templates", "Templates", false;
57
    TAB_SCRIPTS = "tab-scripts", "Scripts", false;
58
    TAB_CONSOLE = "tab-console", "Console view", false;
59
    LINE_EDIT = "line-edit", "Line editing", false;
60
    CMDLINE = "cmdline", "Command line", true;
61
    VIM_NORMAL = "vim-normal", "Vim normal mode", false;
62
    VIM_INSERT = "vim-insert", "Vim insert mode", true;
63
    CONSOLE = "console", "Console", false;
64
    CONSOLE_PROMPT = "console-prompt", "Console prompt", true;
65
    CONSOLE_SCROLLBACK = "console-scrollback", "Console scrollback", false;
66
    VIEWER = "viewer", "Viewer windows", false;
67
    VIEWER_DETAIL = "viewer-detail", "Transaction window", false;
68
    MODAL_VIEW = "modal-view", "Read-only dialogs", false;
69
    MODAL_CSV = "modal-csv", "CSV dialog", false;
70
    MODAL_BUILDER = "modal-builder", "Filter builder", false;
71
    MODAL_CONFIRM = "modal-confirm", "Confirmation", false;
72
    OVERLAY = "overlay", "Forms and dialogs", true;
73
    WIDGET_TEXTAREA = "widget-textarea", "Text area", true;
74
    WIDGET_DATE = "widget-date", "Date field", true;
75
    WIDGET_SELECT = "widget-select", "Open select list", true;
76
    WIDGET_SELECT_CLOSED = "widget-select-closed", "Select field", false;
77
    WIDGET_CELLS = "widget-cells", "Table cells", true;
78
    WIDGET_SPLITS = "widget-splits", "Splits", true;
79
    WIDGET_BUTTON = "widget-button", "Buttons", false;
80
}
81

            
82
const CMDLINE_PLAIN: &[&Scope] = &[&CMDLINE, &LINE_EDIT];
83
const CMDLINE_VIM: &[&Scope] = &[&VIM_INSERT, &CMDLINE, &LINE_EDIT];
84
const PROMPT: &[&Scope] = &[&CONSOLE_PROMPT, &LINE_EDIT, &CONSOLE];
85
const TEXTAREA: &[&Scope] = &[&WIDGET_TEXTAREA, &LINE_EDIT, &OVERLAY];
86
const DATE: &[&Scope] = &[&WIDGET_DATE, &LINE_EDIT, &OVERLAY];
87
const SELECT_BASE: &[&Scope] = &[&OVERLAY];
88
const SPLITS: &[&Scope] = &[&WIDGET_SPLITS, &WIDGET_CELLS, &LINE_EDIT, &OVERLAY];
89
const ROWS: &[&Scope] = &[&WIDGET_CELLS, &LINE_EDIT, &OVERLAY];
90
// Overlay stays first: it sets the text flag, which line-edit does not.
91
const FIELD: &[&Scope] = &[&OVERLAY, &LINE_EDIT];
92
const BUTTON: &[&Scope] = &[&WIDGET_BUTTON, &OVERLAY];
93

            
94
/// The chains of a form's fields; a focused select layers one of `SELECT_LAYERS` over its own.
95
const FORM_BASES: &[&[&Scope]] = &[TEXTAREA, DATE, SELECT_BASE, SPLITS, ROWS, FIELD];
96
const SELECT_LAYERS: &[&Scope] = &[&WIDGET_SELECT, &WIDGET_SELECT_CLOSED];
97

            
98
/// Whether `name` sits in a chain that types text, so a printable key bound there is untypeable.
99
46
pub(super) fn in_typing_chain(name: &str) -> bool {
100
46
    [CMDLINE_PLAIN, CMDLINE_VIM, PROMPT]
101
46
        .iter()
102
46
        .chain(FORM_BASES)
103
238
        .flat_map(|chain| chain.iter())
104
46
        .chain(SELECT_LAYERS)
105
639
        .any(|scope| scope.name == name)
106
46
}
107

            
108
impl Scopes {
109
6975
    fn of(chain: &[&Scope]) -> Scopes {
110
        Scopes {
111
6975
            innermost: chain.first().map_or(GLOBAL.name, |scope| scope.name),
112
6975
            names: chain
113
6975
                .iter()
114
6975
                .chain([&&GLOBAL])
115
24561
                .map(|scope| scope.name)
116
                .collect(),
117
6975
            text: chain.first().is_some_and(|scope| scope.text),
118
        }
119
6975
    }
120
}
121

            
122
4969
pub(super) fn chain(app: &App) -> Scopes {
123
4969
    match current_focus(app) {
124
10
        FocusTarget::Pending => Scopes::of(&[&PENDING]),
125
165
        FocusTarget::Drag => Scopes::of(&[&DRAG]),
126
118
        FocusTarget::Menu => Scopes::of(&[&MENU]),
127
717
        FocusTarget::CmdLine => cmdline(app.keys.vim()),
128
152
        FocusTarget::ConsoleInput => app
129
152
            .front::<ConsoleState>()
130
152
            .map_or_else(|| Scopes::of(&[]), |tab| console(tab.panes.focused())),
131
        FocusTarget::Overlay | FocusTarget::ViewPane => {
132
3807
            app.overlays.top().map_or_else(|| front_view(app), overlay)
133
        }
134
    }
135
4969
}
136

            
137
719
fn cmdline(vim: VimState) -> Scopes {
138
719
    match vim {
139
305
        VimState::Off => Scopes::of(CMDLINE_PLAIN),
140
311
        VimState::Insert => Scopes::of(CMDLINE_VIM),
141
103
        VimState::Normal => Scopes::of(&[&VIM_NORMAL]),
142
    }
143
719
}
144

            
145
155
fn console(pane: PaneId) -> Scopes {
146
155
    match pane {
147
143
        PaneId::Prompt => Scopes::of(PROMPT),
148
12
        PaneId::Scrollback => Scopes::of(&[&CONSOLE_SCROLLBACK, &CONSOLE]),
149
    }
150
155
}
151

            
152
1747
pub(super) fn front_view(app: &App) -> Scopes {
153
1747
    match app.front_window() {
154
1632
        Some(id) => window_view(app, id),
155
115
        None => Scopes::of(&[&GLOBAL_VIEW]),
156
    }
157
1747
}
158

            
159
1707
pub(super) fn window_view(app: &App, id: WindowId) -> Scopes {
160
1707
    if let Some(viewer) = app.win::<Viewer>(id) {
161
49
        return viewer_scopes(&viewer.source);
162
1658
    }
163
1658
    app.window_view(id)
164
1658
        .map_or_else(|| Scopes::of(&[&GLOBAL_VIEW]), view)
165
1707
}
166

            
167
49
fn viewer_scopes(source: &Source) -> Scopes {
168
49
    match source {
169
30
        Source::Detail { .. } => Scopes::of(&[&VIEWER_DETAIL, &VIEWER, &GLOBAL_VIEW]),
170
19
        Source::Help { .. } => Scopes::of(&[&VIEWER, &GLOBAL_VIEW]),
171
    }
172
49
}
173

            
174
1659
fn view(tab: Tab) -> Scopes {
175
1659
    match tab {
176
845
        Tab::Accounts => Scopes::of(&[&TAB_ACCOUNTS, &LIST, &GLOBAL_VIEW]),
177
185
        Tab::Transactions => Scopes::of(&[&TAB_TRANSACTIONS, &LIST, &GLOBAL_VIEW]),
178
34
        Tab::Commodities => Scopes::of(&[&TAB_COMMODITIES, &LIST, &GLOBAL_VIEW]),
179
292
        Tab::Reports => Scopes::of(&[&TAB_REPORTS, &GLOBAL_VIEW]),
180
66
        Tab::Config => Scopes::of(&[&TAB_CONFIG, &LIST, &GLOBAL_VIEW]),
181
23
        Tab::SshKeys => Scopes::of(&[&TAB_SSH_KEYS, &LIST, &GLOBAL_VIEW]),
182
57
        Tab::Tags => Scopes::of(&[&TAB_TAGS, &LIST, &GLOBAL_VIEW]),
183
32
        Tab::Templates => Scopes::of(&[&TAB_TEMPLATES, &LIST, &GLOBAL_VIEW]),
184
75
        Tab::Scripts => Scopes::of(&[&TAB_SCRIPTS, &LIST, &GLOBAL_VIEW]),
185
50
        Tab::Console => Scopes::of(&[&TAB_CONSOLE, &GLOBAL_VIEW]),
186
        Tab::Viewer => Scopes::of(&[&VIEWER, &GLOBAL_VIEW]),
187
    }
188
1659
}
189

            
190
3022
pub(super) fn overlay(modal: &Modal) -> Scopes {
191
3022
    match modal {
192
22
        Modal::Csv(_) => Scopes::of(&[&MODAL_CSV, &MODAL_VIEW]),
193
122
        Modal::FilterBuilder(_) => Scopes::of(&[&MODAL_BUILDER]),
194
493
        Modal::Confirm { .. } => Scopes::of(&[&MODAL_CONFIRM]),
195
113
        Modal::Draft(_) => Scopes::of(&[&MODAL_VIEW]),
196
2272
        Modal::Form(form) => form_scopes(form.focused_widget_kind(), form.focused_select()),
197
    }
198
3022
}
199

            
200
/// `None` is a focused button: a form's fields always have a widget kind. A focused select, in a
201
/// field or a grid cell, layers its open or closed scope over the chain of the widget.
202
3235
pub(super) fn form_scopes(kind: Option<WidgetKind>, select: Option<&SelectWidget>) -> Scopes {
203
3235
    let base: &[&Scope] = match kind {
204
441
        Some(WidgetKind::TextArea) => TEXTAREA,
205
288
        Some(WidgetKind::Date) => DATE,
206
491
        Some(WidgetKind::Select) => SELECT_BASE,
207
635
        Some(WidgetKind::Splits) => SPLITS,
208
621
        Some(WidgetKind::Rows) => ROWS,
209
662
        Some(WidgetKind::Text | WidgetKind::Amount) => FIELD,
210
97
        None => BUTTON,
211
    };
212
3235
    let layer: Option<&Scope> = select.map(|select| match select.open {
213
415
        true => SELECT_LAYERS[0],
214
530
        false => SELECT_LAYERS[1],
215
945
    });
216
3235
    let chain: Vec<&Scope> = layer.into_iter().chain(base.iter().copied()).collect();
217
3235
    Scopes::of(&chain)
218
3235
}