From 1d21071c86d9256520d2e145f54a3d419a0f66e1 Mon Sep 17 00:00:00 2001 From: unspeaker Date: Fri, 6 Sep 2024 23:14:27 +0300 Subject: [PATCH] refactor: collect collections --- crates/tek_core/src/engine.rs | 2 - crates/tek_core/src/engine/collect.rs | 68 +++++++++++++++++++++++++ crates/tek_core/src/engine/component.rs | 10 ++++ crates/tek_core/src/engine/exit.rs | 10 ---- crates/tek_core/src/engine/layered.rs | 20 -------- crates/tek_core/src/engine/split.rs | 50 ------------------ 6 files changed, 78 insertions(+), 82 deletions(-) delete mode 100644 crates/tek_core/src/engine/layered.rs delete mode 100644 crates/tek_core/src/engine/split.rs diff --git a/crates/tek_core/src/engine.rs b/crates/tek_core/src/engine.rs index da0e008a..79f65174 100644 --- a/crates/tek_core/src/engine.rs +++ b/crates/tek_core/src/engine.rs @@ -45,8 +45,6 @@ submod! { focus handle keymap - layered layout render - split } diff --git a/crates/tek_core/src/engine/collect.rs b/crates/tek_core/src/engine/collect.rs index 39d0c02c..f44ca701 100644 --- a/crates/tek_core/src/engine/collect.rs +++ b/crates/tek_core/src/engine/collect.rs @@ -44,3 +44,71 @@ impl<'a, E: Engine> Collect<'a, E> for Collection<'a, E> { self } } + +pub struct Layered<'a, E: Engine>(pub Collection<'a, E>); + +impl<'a, E: Engine> Layered<'a, E> { + pub fn new () -> Self { + Self(Collection::new()) + } +} + +impl<'a, E: Engine> Collect<'a, E> for Layered<'a, E> { + fn add_box (mut self, item: Box + 'a>) -> Self { + self.0 = self.0.add_box(item); + self + } + fn add_ref (mut self, item: &'a dyn Render) -> Self { + self.0 = self.0.add_ref(item); + self + } +} + +#[derive(Copy, Clone)] +pub enum Direction { Up, Down, Left, Right } + +impl Direction { + pub fn is_down (&self) -> bool { + match self { Self::Down => true, _ => false } + } + pub fn is_right (&self) -> bool { + match self { Self::Right => true, _ => false } + } +} + +pub struct Split<'a, E: Engine> { + pub items: Collection<'a, E>, + pub direction: Direction, + pub focus: Option +} + +impl<'a, E: Engine> Split<'a, E> { + pub fn new (direction: Direction) -> Self { + Self { + items: Collection::new(), + direction, + focus: None + } + } + pub fn down () -> Self { + Self::new(Direction::Down) + } + pub fn right () -> Self { + Self::new(Direction::Right) + } + pub fn focus (mut self, focus: Option) -> Self { + self.focus = focus; + self + } +} + +impl<'a, E: Engine> Collect<'a, E> for Split<'a, E> { + fn add_box (mut self, item: Box + 'a>) -> Self { + self.items = self.items.add_box(item); + self + } + fn add_ref (mut self, item: &'a dyn Render) -> Self { + self.items = self.items.add_ref(item); + self + } +} diff --git a/crates/tek_core/src/engine/component.rs b/crates/tek_core/src/engine/component.rs index 952a4d90..71087bf1 100644 --- a/crates/tek_core/src/engine/component.rs +++ b/crates/tek_core/src/engine/component.rs @@ -5,3 +5,13 @@ pub trait Component: Render + Handle {} /// Everything that implements [Render] and [Handle] is a [Component]. impl + Handle> Component for C {} + +/// Marker trait for [Component]s that can [Exit] +pub trait ExitableComponent: Exit + Component where E: Engine { + /// Perform type erasure for collecting heterogeneous components. + fn boxed (self) -> Box> where Self: Sized + 'static { + Box::new(self) + } +} + +impl + Exit> ExitableComponent for C {} diff --git a/crates/tek_core/src/engine/exit.rs b/crates/tek_core/src/engine/exit.rs index db5a0a1c..6f273dbb 100644 --- a/crates/tek_core/src/engine/exit.rs +++ b/crates/tek_core/src/engine/exit.rs @@ -8,16 +8,6 @@ pub trait Exit: Send { } } -/// Marker trait for [Component]s that can [Exit] -pub trait ExitableComponent: Exit + Component where E: Engine { - /// Perform type erasure for collecting heterogeneous components. - fn boxed (self) -> Box> where Self: Sized + 'static { - Box::new(self) - } -} - -impl + Exit> ExitableComponent for C {} - #[macro_export] macro_rules! exit { ($T:ty) => { impl Exit for $T { diff --git a/crates/tek_core/src/engine/layered.rs b/crates/tek_core/src/engine/layered.rs deleted file mode 100644 index b892ec8b..00000000 --- a/crates/tek_core/src/engine/layered.rs +++ /dev/null @@ -1,20 +0,0 @@ -use crate::*; - -pub struct Layered<'a, E: Engine>(pub Collection<'a, E>); - -impl<'a, E: Engine> Layered<'a, E> { - pub fn new () -> Self { - Self(Collection::new()) - } -} - -impl<'a, E: Engine> Collect<'a, E> for Layered<'a, E> { - fn add_box (mut self, item: Box + 'a>) -> Self { - self.0 = self.0.add_box(item); - self - } - fn add_ref (mut self, item: &'a dyn Render) -> Self { - self.0 = self.0.add_ref(item); - self - } -} diff --git a/crates/tek_core/src/engine/split.rs b/crates/tek_core/src/engine/split.rs deleted file mode 100644 index 2f7df30d..00000000 --- a/crates/tek_core/src/engine/split.rs +++ /dev/null @@ -1,50 +0,0 @@ -use crate::*; - -#[derive(Copy, Clone)] -pub enum Direction { Up, Down, Left, Right } - -impl Direction { - pub fn is_down (&self) -> bool { - match self { Self::Down => true, _ => false } - } - pub fn is_right (&self) -> bool { - match self { Self::Right => true, _ => false } - } -} - -pub struct Split<'a, E: Engine> { - pub items: Collection<'a, E>, - pub direction: Direction, - pub focus: Option -} - -impl<'a, E: Engine> Split<'a, E> { - pub fn new (direction: Direction) -> Self { - Self { - items: Collection::new(), - direction, - focus: None - } - } - pub fn down () -> Self { - Self::new(Direction::Down) - } - pub fn right () -> Self { - Self::new(Direction::Right) - } - pub fn focus (mut self, focus: Option) -> Self { - self.focus = focus; - self - } -} - -impl<'a, E: Engine> Collect<'a, E> for Split<'a, E> { - fn add_box (mut self, item: Box + 'a>) -> Self { - self.items = self.items.add_box(item); - self - } - fn add_ref (mut self, item: &'a dyn Render) -> Self { - self.items = self.items.add_ref(item); - self - } -}