HasFocus

Trait HasFocus 

Source
pub trait HasFocus {
    // Required methods
    fn build(&self, builder: &mut FocusBuilder);
    fn focus(&self) -> FocusFlag;
    fn area(&self) -> Rect;

    // Provided methods
    fn id(&self) -> usize { ... }
    fn area_z(&self) -> u16 { ... }
    fn navigable(&self) -> Navigation { ... }
    fn is_focused(&self) -> bool { ... }
    fn lost_focus(&self) -> bool { ... }
    fn gained_focus(&self) -> bool { ... }
}
Expand description

Trait for a widget that takes part of focus handling.

When used for a simple widget implement

  • build()
  • focus()
  • area()

and optionally

  • area_z() and navigable()
use ratatui::layout::Rect;
use rat_focus::{FocusBuilder, FocusFlag, HasFocus};

struct MyWidgetState { pub focus: FocusFlag, pub area: Rect }

impl HasFocus for MyWidgetState {
    fn build(&self, builder: &mut FocusBuilder) {
        builder.leaf_widget(self);
    }

    fn focus(&self) -> FocusFlag {
        self.focus.clone()
    }

    fn area(&self) -> Rect {
        self.area
    }
}

When used for a container widget implement

  • build()
use ratatui::layout::Rect;
use rat_focus::{FocusBuilder, FocusFlag, HasFocus};

struct MyWidgetState { pub focus: FocusFlag, pub area: Rect }
struct SomeWidgetState { pub focus: FocusFlag, pub area: Rect, pub component_a: MyWidgetState, pub component_b: MyWidgetState }

impl HasFocus for SomeWidgetState {
    fn build(&self, builder: &mut FocusBuilder) {
        let tag = builder.start(self);
        builder.widget(&self.component_a);
        builder.widget(&self.component_b);
        builder.end(tag);
    }

    fn focus(&self) -> FocusFlag {
        self.focus.clone()
    }

    fn area(&self) -> Rect {
        self.area
    }
}

Creates a container with an identity.

Or

use ratatui::layout::Rect;
use rat_focus::{FocusBuilder, FocusFlag, HasFocus};

struct MyWidgetState { pub focus: FocusFlag, pub area: Rect }
struct SomeWidgetState { pub focus: FocusFlag, pub area: Rect, pub component_a: MyWidgetState, pub component_b: MyWidgetState }

impl HasFocus for SomeWidgetState {
    fn build(&self, builder: &mut FocusBuilder) {
        builder.widget(&self.component_a);
        builder.widget(&self.component_b);
    }

    fn focus(&self) -> FocusFlag {
        unimplemented!("not in use")
    }

    fn area(&self) -> Rect {
        unimplemented!("not in use")
    }
}

for an anonymous container.

focus(), area() and area_z() are only used for the first case. navigable() is ignored for containers, leave it at the default.

Required Methods§

Source

fn build(&self, builder: &mut FocusBuilder)

Build the focus-structure for the container.

Source

fn focus(&self) -> FocusFlag

Access to the flag for the rest.

Source

fn area(&self) -> Rect

Area for mouse focus.

This area shouldn’t overlap with areas returned by other widgets. If it does, the widget should use area_z() for clarification. Otherwise, the areas are searched in order of addition.

Provided Methods§

Source

fn id(&self) -> usize

Provide a unique id for the widget.

Source

fn area_z(&self) -> u16

Z value for the area.

When testing for mouse interactions the z-value is taken into account too.

Source

fn navigable(&self) -> Navigation

Declares how the widget interacts with focus.

Default is Navigation::Regular.

Source

fn is_focused(&self) -> bool

Focused?

Source

fn lost_focus(&self) -> bool

Just lost focus.

Source

fn gained_focus(&self) -> bool

Just gained focus.

Implementors§