From f7b213431063af4cd03d4b5161b4b90f28f16b63 Mon Sep 17 00:00:00 2001 From: unspeaker Date: Sun, 15 Sep 2024 19:53:20 +0300 Subject: [PATCH] generalize Layers --- crates/tek_core/src/engine.rs | 10 ++----- crates/tek_core/src/space.rs | 26 ++++++++++++++++++ crates/tek_core/src/tui.rs | 50 +++++++++++++++++------------------ 3 files changed, 53 insertions(+), 33 deletions(-) diff --git a/crates/tek_core/src/engine.rs b/crates/tek_core/src/engine.rs index 2f3dc609..5e581aa8 100644 --- a/crates/tek_core/src/engine.rs +++ b/crates/tek_core/src/engine.rs @@ -12,8 +12,8 @@ pub trait Engine: Send + Sync + Sized { type Output: Output; type Unit: Number; - type Area: Area + From<[Self::Unit;4]> + Debug; - type Size: Size + From<[Self::Unit;2]> + Debug; + type Area: Area + From<[Self::Unit;4]> + Debug + Copy; + type Size: Size + From<[Self::Unit;2]> + Debug + Copy; fn setup (&mut self) -> Usually<()> { Ok(()) } fn exited (&self) -> bool; @@ -163,13 +163,11 @@ impl + Exit> ExitableComponent for C {} pub trait Handle: Send + Sync { fn handle (&mut self, context: &E::Input) -> Perhaps; } - impl Handle for &mut H where H: Handle { fn handle (&mut self, context: &E::Input) -> Perhaps { (*self).handle(context) } } - impl Handle for Option where H: Handle { fn handle (&mut self, context: &E::Input) -> Perhaps { if let Some(ref mut handle) = self { @@ -179,25 +177,21 @@ impl Handle for Option where H: Handle { } } } - impl Handle for Mutex where H: Handle { fn handle (&mut self, context: &E::Input) -> Perhaps { self.lock().unwrap().handle(context) } } - impl Handle for Arc> where H: Handle { fn handle (&mut self, context: &E::Input) -> Perhaps { self.lock().unwrap().handle(context) } } - impl Handle for RwLock where H: Handle { fn handle (&mut self, context: &E::Input) -> Perhaps { self.write().unwrap().handle(context) } } - impl Handle for Arc> where H: Handle { fn handle (&mut self, context: &E::Input) -> Perhaps { self.write().unwrap().handle(context) diff --git a/crates/tek_core/src/space.rs b/crates/tek_core/src/space.rs index 7540bb7e..0f3b0308 100644 --- a/crates/tek_core/src/space.rs +++ b/crates/tek_core/src/space.rs @@ -145,6 +145,32 @@ impl< } } +impl Widget for Layers +where + F: Send + Sync + Fn(&mut dyn FnMut(&dyn Widget)->Usually<()>)->Usually<()> +{ + type Engine = E; + fn layout (&self, area: E::Size) -> Perhaps { + let mut w: E::Unit = 0.into(); + let mut h: E::Unit = 0.into(); + (self.0)(&mut |layer| { + if let Some(layer_area) = layer.layout(area)? { + w = w.max(layer_area.w()); + h = h.max(layer_area.h()); + } + Ok(()) + })?; + Ok(Some([w, h].into())) + } + fn render (&self, to: &mut E::Output) -> Usually<()> { + if let Some(size) = self.layout(to.area().wh().into())? { + (self.0)(&mut |layer|to.render_in(to.area().clip(size).into(), &layer)) + } else { + Ok(()) + } + } +} + //pub fn collect <'a, E: Engine, const N: usize> ( //items: &'a [&'a dyn Widget;N] //) -> impl Send + Sync + Fn(&'a mut dyn FnMut(&'a dyn Widget)->Usually<()>)->Usually<()> + '_ { diff --git a/crates/tek_core/src/tui.rs b/crates/tek_core/src/tui.rs index 2a5cbce1..ccb1716c 100644 --- a/crates/tek_core/src/tui.rs +++ b/crates/tek_core/src/tui.rs @@ -416,31 +416,31 @@ where } } -impl Widget for Layers -where - F: Send + Sync + Fn(&mut dyn FnMut(&dyn Widget)->Usually<()>)->Usually<()> -{ - type Engine = Tui; - fn layout (&self, area: [u16;2]) -> Perhaps<[u16;2]> { - let mut w = 0; - let mut h = 0; - (self.0)(&mut |layer| { - if let Some(layer_area) = layer.layout(area)? { - w = w.max(layer_area.w()); - h = h.max(layer_area.h()); - } - Ok(()) - })?; - Ok(Some([w, h])) - } - fn render (&self, to: &mut TuiOutput) -> Usually<()> { - if let Some(size) = self.layout(to.area().wh())? { - (self.0)(&mut |layer|to.render_in(to.area().clip(size), &layer)) - } else { - Ok(()) - } - } -} +//impl Widget for Layers +//where + //F: Send + Sync + Fn(&mut dyn FnMut(&dyn Widget)->Usually<()>)->Usually<()> +//{ + //type Engine = Tui; + //fn layout (&self, area: [u16;2]) -> Perhaps<[u16;2]> { + //let mut w = 0; + //let mut h = 0; + //(self.0)(&mut |layer| { + //if let Some(layer_area) = layer.layout(area)? { + //w = w.max(layer_area.w()); + //h = h.max(layer_area.h()); + //} + //Ok(()) + //})?; + //Ok(Some([w, h])) + //} + //fn render (&self, to: &mut TuiOutput) -> Usually<()> { + //if let Some(size) = self.layout(to.area().wh())? { + //(self.0)(&mut |layer|to.render_in(to.area().clip(size), &layer)) + //} else { + //Ok(()) + //} + //} +//} pub struct Border(pub S);