Skip to main content

Crate rat_focus

Crate rat_focus 

Source
Expand description

semver stable crates.io Documentation License License

This crate is a part of rat-salsa.

For examples see rat-focus GitHub.

§Focus handling for ratatui

This crate works by adding a FocusFlag to each widget’s state.

Then FocusBuilder is used to collect an ordered list of all widgets that should be considered for focus handling. It builds the Focus which has the functions next, prev and focus_at that can do the navigation.

from <focus_input1.rs>

fn focus_input(state: &mut State) -> Focus {
    let mut fb = FocusBuilder::default();
    fb.widget(&state.input1);
    fb.widget(&state.input2);
    fb.widget(&state.input3);
    fb.widget(&state.input4);
    fb.build()
}

fn handle_input(
    event: &crossterm::event::Event,
    _data: &mut Data,
    _istate: &mut MiniSalsaState,
    state: &mut State,
) -> Result<Outcome, anyhow::Error> {

    // Handle events for focus.
    let f = focus_input(state).handle(event, Regular);

    // ...

    Ok(f)
}
  • Keeps the focus-state close to the widgets code.
  • Rebuilt for each event.
    • No need to update the widget list when the application state changes.
    • FocusBuilder can be passed on all over the application to build the widget tree.

Modules§

doc
Documentation details.
ratatui
Reexport of types used by a macro.

Macros§

impl_has_focus
Create the implementation of HasFocus for the given list of struct members.
match_focus
Does a match on several fields and can return a result. Does a widget_state.is_focused() for each field and returns the first that is true. There is an else branch too.
on_gained
Does a match on the state struct of a widget. If widget_state.gained_focus() is true the block is executed. This requires that widget_state implements HasFocus, but that’s the basic requirement for this whole crate.
on_lost
Does a match on the state struct of a widget. If widget_state.lost_focus() is true the block is executed. This requires that widget_state implements HasFocus, but that’s the basic requirement for this whole crate.

Structs§

Focus
Focus deals with all focus-related issues.
FocusBuilder
Builder for the Focus.
FocusFlag
Holds the flags for the focus.

Enums§

Navigation
Focus navigation for widgets.

Traits§

HasFocus
Trait for a widget that takes part of focus handling.

Functions§

handle_focus
Handle all events.