mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-07 12:16:42 +01:00
fix passing numbers to edn view
This commit is contained in:
parent
07d90228d3
commit
f64a9731ce
4 changed files with 42 additions and 34 deletions
|
|
@ -11,9 +11,11 @@ pub type EdnRenderCallback<'a, O: Output, State> =
|
||||||
/// Provides values to the template
|
/// Provides values to the template
|
||||||
pub trait EdnViewData<E: Output> {
|
pub trait EdnViewData<E: Output> {
|
||||||
fn get_bool (&self, _sym: EdnItem<&str>) -> bool { false }
|
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_usize (&self, _sym: EdnItem<&str>) -> usize { 0 }
|
||||||
fn get_content <'a> (&'a self, _sym: EdnItem<&'a str>) -> RenderBox<'a, E> { Box::new(()) }
|
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.
|
/// Renders from EDN source and context.
|
||||||
|
|
|
||||||
|
|
@ -42,24 +42,20 @@ macro_rules! transform_xy_unit {
|
||||||
}
|
}
|
||||||
|
|
||||||
transform_xy_unit!(|self: Fixed, area|{
|
transform_xy_unit!(|self: Fixed, area|{
|
||||||
let [w, h] = Render::layout(&self.content(), area.center_xy(match self {
|
let [x, y, w, h] = area.xywh();
|
||||||
Self::X(fw, _) => [*fw, area.h()],
|
let fixed_area = match self {
|
||||||
Self::Y(fh, _) => [area.w(), *fh],
|
Self::X(fw, _) => [x, y, *fw, h],
|
||||||
Self::XY(fw, fh, _) => [*fw, *fh],
|
Self::Y(fh, _) => [x, y, w, *fh],
|
||||||
}).into()).wh();
|
Self::XY(fw, fh, _) => [x, y, *fw, *fh],
|
||||||
area.center_xy(match self {
|
};
|
||||||
Self::X(fw, _) => [*fw, h],
|
let [x, y, w, h] = Render::layout(&self.content(), fixed_area.into()).xywh();
|
||||||
Self::Y(fh, _) => [w, *fh],
|
let fixed_area = match self {
|
||||||
Self::XY(fw, fh, _) => [*fw, *fh],
|
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|{
|
transform_xy_unit!(|self: Min, area|{
|
||||||
let area = Render::layout(&self.content(), area);
|
let area = Render::layout(&self.content(), area);
|
||||||
match self {
|
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)]
|
Self::XY(mw, mh, _) => [area.x(), area.y(), area.w().max(*mw), area.h().max(*mh)]
|
||||||
}});
|
}});
|
||||||
transform_xy_unit!(|self: Max, area|{
|
transform_xy_unit!(|self: Max, area|{
|
||||||
let area = Render::layout(&self.content(), area);
|
let [x, y, w, h] = area.xywh();
|
||||||
match self {
|
Render::layout(&self.content(), match self {
|
||||||
Self::X(mw, _) => [area.x(), area.y(), area.w().min(*mw), area.h()],
|
Self::X(fw, _) => [x, y, *fw, h],
|
||||||
Self::Y(mh, _) => [area.x(), area.y(), area.w(), area.h().min(*mh)],
|
Self::Y(fh, _) => [x, y, w, *fh],
|
||||||
Self::XY(mw, mh, _) => [area.x(), area.y(), area.w().min(*mw), area.h().min(*mh)],
|
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|{
|
transform_xy_unit!(|self: Push, area|{
|
||||||
let area = Render::layout(&self.content(), area);
|
let area = Render::layout(&self.content(), area);
|
||||||
[area.x() + self.dx(), area.y() + self.dy(), area.w(), area.h()]
|
[area.x() + self.dx(), area.y() + self.dy(), area.w(), area.h()]
|
||||||
|
|
|
||||||
|
|
@ -21,21 +21,24 @@ pub struct Example(usize);
|
||||||
impl EdnViewData<TuiOut> for &Example {
|
impl EdnViewData<TuiOut> for &Example {
|
||||||
fn get_content <'a> (&'a self, sym: EdnItem<&'a str>) -> RenderBox<'a, TuiOut> {
|
fn get_content <'a> (&'a self, sym: EdnItem<&'a str>) -> RenderBox<'a, TuiOut> {
|
||||||
Box::new(Thunk::new(move||match sym {
|
Box::new(Thunk::new(move||match sym {
|
||||||
EdnItem::Sym(":hello-world") => "Hello world!",
|
EdnItem::Sym(":title") => Tui::bg(Color::Rgb(60,10,10), Push::y(1,
|
||||||
EdnItem::Sym(":hello") => "Hello",
|
Align::n(format!("Example {}/{}:", self.0 + 1, EDN.len())))).boxed(),
|
||||||
EdnItem::Sym(":world") => "world",
|
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<TuiOut> for Example {
|
impl Content<TuiOut> for Example {
|
||||||
fn content (&self) -> impl Render<TuiOut> {
|
fn content (&self) -> impl Render<TuiOut> {
|
||||||
Bsp::a(Push::y(1, Align::n(format!("Example {}/{}:", self.0 + 1, EDN.len()))),
|
let title = Tui::bg(Color::Rgb(60,10,10), Push::y(1, Align::n(format!("Example {}/{}:", self.0 + 1, EDN.len()))));
|
||||||
Bsp::a(Push::y(2, Align::n(format!("{}", EDN[self.0]))),
|
let code = Tui::bg(Color::Rgb(10,60,10), Push::y(2, Align::n(format!("{}", EDN[self.0]))));
|
||||||
Tui::bg(Color::Rgb(10,20,30), EdnView::from_source(self, 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))
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1 +1 @@
|
||||||
(fixed/xy 20 20 :hello-world)
|
(fixed/xy 20 10 :hello-world)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue