refactor: collect collections

This commit is contained in:
🪞👃🪞 2024-09-06 23:14:27 +03:00
parent a52066f640
commit 1d21071c86
6 changed files with 78 additions and 82 deletions

View file

@ -45,8 +45,6 @@ submod! {
focus
handle
keymap
layered
layout
render
split
}

View file

@ -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<dyn Render<E> + 'a>) -> Self {
self.0 = self.0.add_box(item);
self
}
fn add_ref (mut self, item: &'a dyn Render<E>) -> 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<usize>
}
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<usize>) -> Self {
self.focus = focus;
self
}
}
impl<'a, E: Engine> Collect<'a, E> for Split<'a, E> {
fn add_box (mut self, item: Box<dyn Render<E> + 'a>) -> Self {
self.items = self.items.add_box(item);
self
}
fn add_ref (mut self, item: &'a dyn Render<E>) -> Self {
self.items = self.items.add_ref(item);
self
}
}

View file

@ -5,3 +5,13 @@ pub trait Component<E: Engine>: Render<E> + Handle<E> {}
/// Everything that implements [Render] and [Handle] is a [Component].
impl<E: Engine, C: Render<E> + Handle<E>> Component<E> for C {}
/// Marker trait for [Component]s that can [Exit]
pub trait ExitableComponent<E>: Exit + Component<E> where E: Engine {
/// Perform type erasure for collecting heterogeneous components.
fn boxed (self) -> Box<dyn ExitableComponent<E>> where Self: Sized + 'static {
Box::new(self)
}
}
impl<E: Engine, C: Component<E> + Exit> ExitableComponent<E> for C {}

View file

@ -8,16 +8,6 @@ pub trait Exit: Send {
}
}
/// Marker trait for [Component]s that can [Exit]
pub trait ExitableComponent<E>: Exit + Component<E> where E: Engine {
/// Perform type erasure for collecting heterogeneous components.
fn boxed (self) -> Box<dyn ExitableComponent<E>> where Self: Sized + 'static {
Box::new(self)
}
}
impl<E: Engine, C: Component<E> + Exit> ExitableComponent<E> for C {}
#[macro_export] macro_rules! exit {
($T:ty) => {
impl Exit for $T {

View file

@ -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<dyn Render<E> + 'a>) -> Self {
self.0 = self.0.add_box(item);
self
}
fn add_ref (mut self, item: &'a dyn Render<E>) -> Self {
self.0 = self.0.add_ref(item);
self
}
}

View file

@ -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<usize>
}
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<usize>) -> Self {
self.focus = focus;
self
}
}
impl<'a, E: Engine> Collect<'a, E> for Split<'a, E> {
fn add_box (mut self, item: Box<dyn Render<E> + 'a>) -> Self {
self.items = self.items.add_box(item);
self
}
fn add_ref (mut self, item: &'a dyn Render<E>) -> Self {
self.items = self.items.add_ref(item);
self
}
}