From f64a9731cea5fdc548996c7312803f81562c90c0 Mon Sep 17 00:00:00 2001 From: unspeaker Date: Fri, 10 Jan 2025 18:47:00 +0100 Subject: [PATCH] fix passing numbers to edn view --- output/src/edn_view.rs | 4 ++- output/src/transform_xy_unit.rs | 49 +++++++++++++++++---------------- tek/examples/edn.rs | 21 ++++++++------ tek/examples/edn04.edn | 2 +- 4 files changed, 42 insertions(+), 34 deletions(-) diff --git a/output/src/edn_view.rs b/output/src/edn_view.rs index 86dc3fb4..3a62f875 100644 --- a/output/src/edn_view.rs +++ b/output/src/edn_view.rs @@ -11,9 +11,11 @@ pub type EdnRenderCallback<'a, O: Output, State> = /// Provides values to the template pub trait EdnViewData { fn get_bool (&self, _sym: EdnItem<&str>) -> bool { false } - fn get_unit (&self, _sym: EdnItem<&str>) -> E::Unit { 0.into() } fn get_usize (&self, _sym: EdnItem<&str>) -> usize { 0 } fn get_content <'a> (&'a self, _sym: EdnItem<&'a str>) -> RenderBox<'a, E> { Box::new(()) } + fn get_unit (&self, num: EdnItem<&str>) -> E::Unit { + if let EdnItem::Num(n) = num { (n as u16).into() } else { 0.into() } + } } /// Renders from EDN source and context. diff --git a/output/src/transform_xy_unit.rs b/output/src/transform_xy_unit.rs index 3e600471..3b2863b8 100644 --- a/output/src/transform_xy_unit.rs +++ b/output/src/transform_xy_unit.rs @@ -42,24 +42,20 @@ macro_rules! transform_xy_unit { } transform_xy_unit!(|self: Fixed, area|{ - let [w, h] = Render::layout(&self.content(), area.center_xy(match self { - Self::X(fw, _) => [*fw, area.h()], - Self::Y(fh, _) => [area.w(), *fh], - Self::XY(fw, fh, _) => [*fw, *fh], - }).into()).wh(); - area.center_xy(match self { - Self::X(fw, _) => [*fw, h], - Self::Y(fh, _) => [w, *fh], - Self::XY(fw, fh, _) => [*fw, *fh], - }) + let [x, y, w, h] = area.xywh(); + let fixed_area = match self { + Self::X(fw, _) => [x, y, *fw, h], + Self::Y(fh, _) => [x, y, w, *fh], + Self::XY(fw, fh, _) => [x, y, *fw, *fh], + }; + let [x, y, w, h] = Render::layout(&self.content(), fixed_area.into()).xywh(); + let fixed_area = match self { + Self::X(fw, _) => [x, y, *fw, h], + Self::Y(fh, _) => [x, y, w, *fh], + Self::XY(fw, fh, _) => [x, y, *fw, *fh], + }; + fixed_area }); - -transform_xy_unit!(|self: Shrink, area|Render::layout(&self.content(), [ - area.x(), area.y(), area.w().minus(self.dx()), area.h().minus(self.dy()) -].into())); -transform_xy_unit!(|self: Expand, area|Render::layout(&self.content(), [ - area.x(), area.y(), area.w() + self.dx(), area.h() + self.dy() -].into())); transform_xy_unit!(|self: Min, area|{ let area = Render::layout(&self.content(), area); match self { @@ -68,12 +64,19 @@ transform_xy_unit!(|self: Min, area|{ Self::XY(mw, mh, _) => [area.x(), area.y(), area.w().max(*mw), area.h().max(*mh)] }}); transform_xy_unit!(|self: Max, area|{ - let area = Render::layout(&self.content(), area); - match self { - Self::X(mw, _) => [area.x(), area.y(), area.w().min(*mw), area.h()], - Self::Y(mh, _) => [area.x(), area.y(), area.w(), area.h().min(*mh)], - Self::XY(mw, mh, _) => [area.x(), area.y(), area.w().min(*mw), area.h().min(*mh)], - }}); + let [x, y, w, h] = area.xywh(); + Render::layout(&self.content(), match self { + Self::X(fw, _) => [x, y, *fw, h], + Self::Y(fh, _) => [x, y, w, *fh], + Self::XY(fw, fh, _) => [x, y, *fw, *fh], + }.into())}); + +transform_xy_unit!(|self: Shrink, area|Render::layout(&self.content(), [ + area.x(), area.y(), area.w().minus(self.dx()), area.h().minus(self.dy()) +].into())); +transform_xy_unit!(|self: Expand, area|Render::layout(&self.content(), [ + area.x(), area.y(), area.w() + self.dx(), area.h() + self.dy() +].into())); transform_xy_unit!(|self: Push, area|{ let area = Render::layout(&self.content(), area); [area.x() + self.dx(), area.y() + self.dy(), area.w(), area.h()] diff --git a/tek/examples/edn.rs b/tek/examples/edn.rs index f285a9a0..5c24a81d 100644 --- a/tek/examples/edn.rs +++ b/tek/examples/edn.rs @@ -21,21 +21,24 @@ pub struct Example(usize); impl EdnViewData for &Example { fn get_content <'a> (&'a self, sym: EdnItem<&'a str>) -> RenderBox<'a, TuiOut> { Box::new(Thunk::new(move||match sym { - EdnItem::Sym(":hello-world") => "Hello world!", - EdnItem::Sym(":hello") => "Hello", - EdnItem::Sym(":world") => "world", - _ => "" + EdnItem::Sym(":title") => Tui::bg(Color::Rgb(60,10,10), Push::y(1, + Align::n(format!("Example {}/{}:", self.0 + 1, EDN.len())))).boxed(), + EdnItem::Sym(":code") => Tui::bg(Color::Rgb(10,60,10), Push::y(2, + Align::n(format!("{}", EDN[self.0])))).boxed(), + EdnItem::Sym(":hello-world") => "Hello world!".boxed(), + EdnItem::Sym(":hello") => "Hello".boxed(), + EdnItem::Sym(":world") => "world".boxed(), + _ => "".boxed() })) } } impl Content for Example { fn content (&self) -> impl Render { - Bsp::a(Push::y(1, Align::n(format!("Example {}/{}:", self.0 + 1, EDN.len()))), - Bsp::a(Push::y(2, Align::n(format!("{}", EDN[self.0]))), - Tui::bg(Color::Rgb(10,20,30), EdnView::from_source(self, EDN[self.0])) - ) - ) + let title = Tui::bg(Color::Rgb(60,10,10), Push::y(1, Align::n(format!("Example {}/{}:", self.0 + 1, EDN.len())))); + let code = Tui::bg(Color::Rgb(10,60,10), Push::y(2, Align::n(format!("{}", EDN[self.0])))); + let content = Tui::bg(Color::Rgb(10,10,60), EdnView::from_source(self, EDN[self.0])); + Bsp::s(title, Bsp::n(code, content)) } } diff --git a/tek/examples/edn04.edn b/tek/examples/edn04.edn index 0a2b02b1..9393669b 100644 --- a/tek/examples/edn04.edn +++ b/tek/examples/edn04.edn @@ -1 +1 @@ -(fixed/xy 20 20 :hello-world) +(fixed/xy 20 10 :hello-world)