Skip to main content

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 build_nav(&self, navigable: Navigation, builder: &mut FocusBuilder) { ... }
    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 { ... }
    fn has_mouse_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 rat_focus::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 rat_focus::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 rat_focus::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/widget.

Source

fn focus(&self) -> FocusFlag

Access to the focus flag.

Source

fn area(&self) -> Rect

Area for mouse focus.

Generally, this area shouldn’t overlap with other areas. If it does, you can use area_z() to give an extra z-value for mouse interactions. Default is 0, higher values mean above. If two areas with the same z overlap, the last one will be used.

Provided Methods§

Source

fn build_nav(&self, navigable: Navigation, builder: &mut FocusBuilder)

Build the focus-structure for the container/widget.

This function is called when the default navigation is overridden by calling FocusBuilder::widget_navigate. You only need to implement this function, if you have a container-widget, that wants to react to an alternate navigation.

For regular widgets this will be called too, but the overridden flag will be used by Focus, regardless of what you do. It’s only useful to get a notification of an alternate navigation.

It defaults to calling build. If you don’t have very specific requirements, you need not concern with this; just implement HasFocus::build.

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.

Source

fn navigable(&self) -> Navigation

Declares how the widget interacts with focus.

Default is Navigation::Regular.

Source

fn is_focused(&self) -> bool

Does this widget have the focus. Or, if the flag is used for a container, does any of widget inside the container have the focus.

This flag is set by [Focus::handle].

Source

fn lost_focus(&self) -> bool

This widget just lost the focus. This flag is set by [Focus::handle] if there is a focus transfer, and will be reset by the next call to [Focus::handle].

See on_lost!

Source

fn gained_focus(&self) -> bool

This widget just gained the focus. This flag is set by [Focus::handle] if there is a focus transfer, and will be reset by the next call to [Focus::handle].

See on_gained!

Source

fn has_mouse_focus(&self) -> bool

This flag is set by [Focus::handle], if a mouse-event matches one of the areas associated with a widget.

It searches all containers for an area-match. All matching areas will have the flag set. If an area with a higher z is found, all previously found areas are discarded.

The z value for the last container is taken as a baseline. Only widgets with a z greater or equal are considered. If multiple widget areas are matching, the last one will get the flag set.

This rules enable popup-windows with complex ui’s. The popup-container starts with a z=1 and all widgets within also get the same z. With the given rules, all widgets underneath the popup are ignored.

  • This flag starts with a default true. This allows widgets to work, even if Focus is not used.
  • Mouse drag events are not bound to any area. Instead, they set the mouse-focus to true for all widgets and containers.

Implementors§

Source§

impl HasFocus for ButtonState

Source§

impl HasFocus for CheckboxState

Source§

impl HasFocus for ComboboxState

Source§

impl HasFocus for DateInputState

Source§

impl HasFocus for DialogFrameState

Source§

impl HasFocus for FileDialogState

Source§

impl HasFocus for LineNumberState

Source§

impl HasFocus for MenuLineState

Source§

impl HasFocus for MenubarState

Source§

impl HasFocus for PopupMenuState

Source§

impl HasFocus for MsgDialogState

Source§

impl HasFocus for NumberInputState

Source§

impl HasFocus for ParagraphState

Source§

impl HasFocus for SplitState

Source§

impl HasFocus for StatusLineState

Source§

impl HasFocus for TabbedState

Source§

impl HasFocus for TextInputState

Source§

impl HasFocus for MaskedInputState

Source§

impl HasFocus for TextAreaState

Source§

impl HasFocus for ToolbarState

Source§

impl HasFocus for ViewState

Source§

impl HasFocus for FocusFlag

Source§

impl<S> HasFocus for EditListState<S>
where S: HasFocus,

Source§

impl<S> HasFocus for EditableTableState<S>

Source§

impl<S> HasFocus for EditableTableVecState<S>

Source§

impl<Selection> HasFocus for MonthState<Selection>

Source§

impl<Selection> HasFocus for ListState<Selection>

Source§

impl<Selection> HasFocus for TableState<Selection>

Source§

impl<T> HasFocus for ChoiceState<T>
where T: PartialEq + Clone + Default,

Source§

impl<T> HasFocus for RadioState<T>
where T: PartialEq + Clone + Default,

Source§

impl<T> HasFocus for SliderState<T>
where T: RangeOp<Step: Copy + Debug> + MapRange<u16> + Debug + Default + Copy + PartialEq, u16: MapRange<T>,

Source§

impl<W> HasFocus for ClipperState<W>
where W: Eq + Clone + Hash,

Source§

impl<W> HasFocus for FormState<W>
where W: Eq + Hash + Clone,

Source§

impl<const N: usize, Selection> HasFocus for CalendarState<N, Selection>