16 releases (7 breaking)

0.28.0 Nov 1, 2024
0.26.0 Oct 13, 2024
0.10.5 Jul 3, 2024

#206 in Command-line interface

Download history 14/week @ 2024-07-22 9/week @ 2024-08-12 108/week @ 2024-08-19 228/week @ 2024-08-26 5/week @ 2024-09-02 415/week @ 2024-09-09 106/week @ 2024-09-16 46/week @ 2024-09-23 49/week @ 2024-09-30 280/week @ 2024-10-07 446/week @ 2024-10-14 38/week @ 2024-10-21 169/week @ 2024-10-28 29/week @ 2024-11-04

779 downloads per month
Used in 9 crates (6 directly)

MIT/Apache

84KB
1.5K SLoC

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.

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

from <focus_input1.rs>

    fn focus_input(state: &mut State) -> Focus {
    let mut fb = FocusBuilder::default();
    fb.widget(&state.input1)
        .widget(&state.input2)
        .widget(&state.input3)
        .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 current widget list.

Event handling

React to focus events

Event handling is implemented for crossterm. It uses Tab+BackTab for navigation and handles mouse clicks on the widget's area.

Focus implements HandleEvent, and there is the fn handle_focus to invoke it.

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

It returns Outcome::Changed whenever something interesting has happened.

Widget events

If the widgets has it's own FocusFlag, it will decide the appropriate event handling using this state. No external control needed.

If it doesn't you can use a [FocusAdapter] to keep track of the focus. Use that state to call the appropriate functions defined by the widget.

Traits and Widgets

HasFocus

[HasFocus] is the interface for single widgets.

It is implemented for the widget state struct and provides access to

  • focus() - FocusFlag
  • area() - Rendered area.
  • z_areas() - Extended area info.
  • navigable() - Control flag.

The widget can then use the FocusFlag for rendering and event-handling as it sees fit.

FocusContainer

[FocusContainer] is the interface for container widgets.

This is used to recursively add widgets for focus handling.

  • build() - Uses the FocusBuilder to construct the current widget structure.
  • container() - Optional. Similar to FocusFlag, identifies the container and collects the states of the contained widgets.
  • area() - Optional. Area for the whole container. Used for mouse events too.

Dependencies

~8–17MB
~247K SLoC