refactor(transport): make widgets focusable

This commit is contained in:
🪞👃🪞 2024-09-01 20:29:15 +03:00
parent 2106a7c044
commit b8ac83b019
9 changed files with 204 additions and 132 deletions

View file

@ -1,5 +1,6 @@
use crate::*;
/// A component that may contain [Focusable] components.
pub trait Focus <const N: usize>: Render + Handle {
fn focus (&self) -> usize;
fn focus_mut (&mut self) -> &mut usize;
@ -26,11 +27,27 @@ pub trait Focus <const N: usize>: Render + Handle {
}
}
/// A component that may be focused.
pub trait Focusable: Render + Handle {
fn is_focused (&self) -> bool;
fn set_focused (&mut self, focused: bool);
}
impl<T: Focusable> Focusable for Option<T> {
fn is_focused (&self) -> bool {
match self {
Some(focusable) => focusable.is_focused(),
None => false
}
}
fn set_focused (&mut self, focused: bool) {
if let Some(focusable) = self {
focusable.set_focused(focused)
}
}
}
/// Implement the [Focus] trait for a component.
#[macro_export] macro_rules! focus {
($struct:ident ($focus:ident) : $count:expr => [
$($focusable:ident),*
@ -47,7 +64,7 @@ pub trait Focusable: Render + Handle {
$(&self.$focusable as &dyn Focusable,)*
]
}
fn focusable_mut (&mut self) -> [&mut dyn Focusable;2] {
fn focusable_mut (&mut self) -> [&mut dyn Focusable;$count] {
[
$(&mut self.$focusable as &mut dyn Focusable,)*
]
@ -56,7 +73,11 @@ pub trait Focusable: Render + Handle {
}
}
/// Implement the [Focusable] trait for a component.
#[macro_export] macro_rules! focusable {
($struct:ident) => {
focusable!($struct (focused));
};
($struct:ident ($focused:ident)) => {
impl Focusable for $struct {
fn is_focused (&self) -> bool {