mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-07 04:06:45 +01:00
refactor: collect collections
This commit is contained in:
parent
a52066f640
commit
1d21071c86
6 changed files with 78 additions and 82 deletions
|
|
@ -45,8 +45,6 @@ submod! {
|
||||||
focus
|
focus
|
||||||
handle
|
handle
|
||||||
keymap
|
keymap
|
||||||
layered
|
|
||||||
layout
|
layout
|
||||||
render
|
render
|
||||||
split
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -44,3 +44,71 @@ impl<'a, E: Engine> Collect<'a, E> for Collection<'a, E> {
|
||||||
self
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,3 +5,13 @@ pub trait Component<E: Engine>: Render<E> + Handle<E> {}
|
||||||
|
|
||||||
/// Everything that implements [Render] and [Handle] is a [Component].
|
/// Everything that implements [Render] and [Handle] is a [Component].
|
||||||
impl<E: Engine, C: Render<E> + Handle<E>> Component<E> for C {}
|
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 {}
|
||||||
|
|
|
||||||
|
|
@ -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 {
|
#[macro_export] macro_rules! exit {
|
||||||
($T:ty) => {
|
($T:ty) => {
|
||||||
impl Exit for $T {
|
impl Exit for $T {
|
||||||
|
|
|
||||||
|
|
@ -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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue