joshka/focusable


License: Apache-2.0

Language: Rust


Crate Focusable

Crate badge Docs.rs Badge Deps.rs Badge License Badge Codecov.io Badge Discord Badge

GitHub Repository · API Docs · Examples · Changelog · Contributing

A library for managing focusable elements in an application.

STATUS: Experimental - expect breaking changes.

This crate implements a generic focus handling approach for use in any application. This was specifically crafted with the idea of providing a way to add focusable behavior widgets in Ratatui, but does not depend on it.

Documentation is available at docs.rs.

Usage

cargo add focusable

And then implement or derive the Focus and FocusContainer traits for your types.

Inspired by iced_focus and rat-focus.

Example

use focusable::{Focus, FocusContainer};

#[derive(Focus)]
struct Button {
    is_focused: bool,
}

#[derive(Focus)]
struct TextBox {
    is_focused: bool,
}

#[derive(Clone, Focus)]
struct Label;

#[derive(FocusContainer)]
struct App {
    children: Vec<Box<dyn Focus>>,
}

let label = Box::new(Label);
assert!(!label.can_focus(), "Label should not be focusable");

let button = Box::new(Button { is_focused: false });
assert!(button.can_focus());

let text_box = Box::new(TextBox { is_focused: false });
assert!(text_box.can_focus());

let mut app = App {
    children: vec![label.clone(), button, label, text_box],
};

app.focus_first();
assert!(app.children[1].is_focused()); // skip the first label

app.focus_next();
assert!(app.children[3].is_focused()); // skip the second label

License

Copyright (c) 2024 Josh McKinney

This project is licensed under either of:

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

See CONTRIBUTING.md.

Project Statistics

Sourcerank 4
Repository Size 117 KB
Stars 5
Forks 0
Watchers 1
Open issues 0
Dependencies 10
Contributors 3
Tags 16
Created
Last updated
Last pushed

Top Contributors See all

Josh McKinney dependabot[bot]

Packages Referencing this Repo

focusable
A trait for types that can be focused
Latest release 0.2.6 - Updated - 5 stars
focusable-derive
Derive macro for the focusable crate
Latest release 0.2.6 - Updated - 5 stars

Recent Tags See all

focusable-v0.2.6 September 02, 2024
focusable-derive-v0.2.6 September 02, 2024
focusable-derive-v0.2.6 September 02, 2024
focusable-v0.2.5 August 12, 2024
focusable-derive-v0.2.5 August 12, 2024
focusable-v0.2.4 July 22, 2024
focusable-derive-v0.2.4 July 22, 2024
focusable-v0.2.3 July 15, 2024
focusable-derive-v0.2.3 July 15, 2024
focusable-v0.2.2 July 08, 2024
focusable-derive-v0.2.2 July 08, 2024
focusable-v0.2.1 July 02, 2024
focusable-derive-v0.2.1 July 02, 2024
focusable-v0.2.0 July 02, 2024
focusable-derive-v0.2.0 July 02, 2024

Something wrong with this page? Make a suggestion

Last synced: 2024-09-02 18:15:08 UTC

Login to resync this repository