1
//! Focus-target resolver.
2
//!
3
//! A single canonical precedence order governs both key translation and event
4
//! dispatch: **Pending > Drag > Overlay > Menu > CmdLine > ConsoleInput > ViewPane**. Both
5
//! [`crate::keymap::handle_key`] and [`crate::event::apply`] call
6
//! [`current_focus`] so neither can drift from the other.
7

            
8
use crate::app::App;
9

            
10
#[cfg(test)]
11
mod tests;
12

            
13
/// Which layer currently owns keyboard input.
14
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
15
pub enum FocusTarget {
16
    /// A submitted form awaits its reply and swallows input.
17
    Pending,
18
    /// A window or a dialog is being moved or sized from the keyboard.
19
    Drag,
20
    /// An overlay (modal/form) is open and intercepts all input.
21
    Overlay,
22
    /// The menu drop-down is open.
23
    Menu,
24
    /// The command-line palette is active.
25
    CmdLine,
26
    /// The console input prompt has focus.
27
    ConsoleInput,
28
    /// The active view pane owns input (default).
29
    ViewPane,
30
}
31

            
32
/// Canonical focus resolver — both translate and apply must call this.
33
///
34
/// Precedence: Pending > Drag > Overlay > Menu > CmdLine > ConsoleInput > ViewPane.
35
/// States are structurally mutually exclusive for the higher layers:
36
/// opening the cmdline or an overlay is the only way to gain that focus,
37
/// and closing it restores the layer below.
38
#[must_use]
39
147182
pub fn current_focus(app: &App) -> FocusTarget {
40
147182
    if app.pending_form.is_some() {
41
860
        FocusTarget::Pending
42
146322
    } else if app.dragging() {
43
6255
        FocusTarget::Drag
44
140067
    } else if !app.overlays.is_empty() {
45
86640
        FocusTarget::Overlay
46
53427
    } else if app.menu.is_some() {
47
3015
        FocusTarget::Menu
48
50412
    } else if app.cmdline.active {
49
5927
        FocusTarget::CmdLine
50
44485
    } else if app.console_input_focused() {
51
761
        FocusTarget::ConsoleInput
52
    } else {
53
43724
        FocusTarget::ViewPane
54
    }
55
147182
}