remove old focus implementations

This commit is contained in:
🪞👃🪞 2024-10-10 08:55:39 +03:00
parent 8aab3f4486
commit 5282dab918

View file

@ -74,129 +74,3 @@ pub trait FocusGrid<T: Copy + PartialEq> {
self.update_focus();
}
}
//pub struct FocusFrame<'a, E: Engine>(usize, Vec<FocusCell<'a, E>>);
//pub struct FocusCell<'a, E: Engine> {
//item: &'a dyn Handle<E>,
//next: Option<&'a FocusCell<'a, E>>,
//prev: Option<&'a FocusCell<'a, E>>,
//up: Option<&'a FocusCell<'a, E>>,
//down: Option<&'a FocusCell<'a, E>>,
//left: Option<&'a FocusCell<'a, E>>,
//right: Option<&'a FocusCell<'a, E>>,
//}
pub trait FocusContainer<E: Engine> {
fn focused (&mut self) -> &mut dyn Handle<E>;
fn handle_focus (&mut self, _: &E::Input) -> Perhaps<bool> {
Ok(None)
}
}
/// A component that may contain [Focusable] components.
pub trait Focus <const N: usize, E: Engine>: Widget<Engine = E> + Handle<E> {
fn focus (&self) -> usize;
fn focus_mut (&mut self) -> &mut usize;
fn focusable (&self) -> [&dyn Focusable<E>;N];
fn focusable_mut (&mut self) -> [&mut dyn Focusable<E>;N];
fn focused (&self) -> &dyn Focusable<E> {
let focus = self.focus();
self.focusable()[focus]
}
fn focused_mut (&mut self) -> &mut dyn Focusable<E> {
let focus = self.focus();
self.focusable_mut()[focus]
}
fn focus_prev (&mut self) {
let focus = self.focus();
self.focus_set(if focus > 0 { focus - 1 } else { N - 1 });
}
fn focus_next (&mut self) {
let focus = self.focus();
self.focus_set(if focus < N - 1 { focus + 1 } else { 0 });
}
fn focus_set (&mut self, focus: usize) {
*self.focus_mut() = focus;
let focusable = self.focusable_mut();
for index in 0..N {
focusable[index].set_focused(index == focus);
}
}
}
/// A component that may be focused.
pub trait Focusable<E: Engine>: Widget<Engine = E> + Handle<E> {
fn is_focused (&self) -> bool;
fn set_focused (&mut self, focused: bool);
}
impl<F: Focusable<E>, E: Engine> Focusable<E> for Option<F>
where Option<F>: Widget<Engine = E>
{
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)
}
}
}
impl<F: Focusable<E>, E: Engine> Focusable<E> for Arc<RwLock<F>> {
fn is_focused (&self) -> bool {
self.read().unwrap().is_focused()
}
fn set_focused (&mut self, focused: bool) {
self.write().unwrap().set_focused(focused)
}
}
/// Implement the [Focus] trait for a component.
#[macro_export] macro_rules! focus {
($struct:ident ($focus:ident) : $count:expr => [
$($focusable:ident),*
]) => {
impl Focus<$count, E> for $struct {
fn focus (&self) -> usize {
self.$focus
}
fn focus_mut (&mut self) -> &mut usize {
&mut self.$focus
}
fn focusable (&self) -> [&dyn Focusable<E>;$count] {
[$(&self.$focusable as &dyn Focusable<E>,)*]
}
fn focusable_mut (&mut self) -> [&mut dyn Focusable<E>;$count] {
[$(&mut self.$focusable as &mut dyn Focusable<E>,)*]
}
}
}
}
/// 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 { self.$focused }
fn set_focused (&mut self, f: bool) { self.$focused = f }
}
}
}
#[macro_export] macro_rules! focusables {
($($item:expr),* $(,)?) => { [$(&$item as &dyn Focusable<_>),+] }
}
#[macro_export] macro_rules! focusables_mut {
($($item:expr),* $(,)?) => { [$(&mut $item as &mut dyn Focusable<_>),+] }
}