Lines
100 %
Functions
Branches
//! Focus-target resolver.
//!
//! A single canonical precedence order governs both key translation and event
//! dispatch: **Pending > Drag > Overlay > Menu > CmdLine > ConsoleInput > ViewPane**. Both
//! [`crate::keymap::handle_key`] and [`crate::event::apply`] call
//! [`current_focus`] so neither can drift from the other.
use crate::app::App;
#[cfg(test)]
mod tests;
/// Which layer currently owns keyboard input.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FocusTarget {
/// A submitted form awaits its reply and swallows input.
Pending,
/// A window or a dialog is being moved or sized from the keyboard.
Drag,
/// An overlay (modal/form) is open and intercepts all input.
Overlay,
/// The menu drop-down is open.
Menu,
/// The command-line palette is active.
CmdLine,
/// The console input prompt has focus.
ConsoleInput,
/// The active view pane owns input (default).
ViewPane,
}
/// Canonical focus resolver — both translate and apply must call this.
///
/// Precedence: Pending > Drag > Overlay > Menu > CmdLine > ConsoleInput > ViewPane.
/// States are structurally mutually exclusive for the higher layers:
/// opening the cmdline or an overlay is the only way to gain that focus,
/// and closing it restores the layer below.
#[must_use]
pub fn current_focus(app: &App) -> FocusTarget {
if app.pending_form.is_some() {
FocusTarget::Pending
} else if app.dragging() {
FocusTarget::Drag
} else if !app.overlays.is_empty() {
FocusTarget::Overlay
} else if app.menu.is_some() {
FocusTarget::Menu
} else if app.cmdline.active {
FocusTarget::CmdLine
} else if app.console_input_focused() {
FocusTarget::ConsoleInput
} else {
FocusTarget::ViewPane