tui/focus.rs
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
8use crate::app::App;
9
10#[cfg(test)]
11mod tests;
12
13/// Which layer currently owns keyboard input.
14#[derive(Debug, Clone, Copy, PartialEq, Eq)]
15pub 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]
39pub fn current_focus(app: &App) -> FocusTarget {
40 if app.pending_form.is_some() {
41 FocusTarget::Pending
42 } else if app.dragging() {
43 FocusTarget::Drag
44 } else if !app.overlays.is_empty() {
45 FocusTarget::Overlay
46 } else if app.menu.is_some() {
47 FocusTarget::Menu
48 } else if app.cmdline.active {
49 FocusTarget::CmdLine
50 } else if app.console_input_focused() {
51 FocusTarget::ConsoleInput
52 } else {
53 FocusTarget::ViewPane
54 }
55}