mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-07 04:06:45 +01:00
test and fix behaviors of some of the layout components
This commit is contained in:
parent
aaedede796
commit
11ecf669a1
4 changed files with 137 additions and 56 deletions
|
|
@ -453,11 +453,11 @@ impl<E: Engine, T: Widget<Engine = E>> Widget for Min<E::Unit, T> {
|
|||
}
|
||||
fn render (&self, to: &mut E) -> Perhaps<E::Area> {
|
||||
// 🡘 🡙 ←🡙→
|
||||
self.layout(to.area())?
|
||||
.map(|area|to.with_area(area.x(), area.y(), area.w(), area.h()))
|
||||
.map(|to|self.inner().render(to))
|
||||
.transpose()
|
||||
.map(|x|x.flatten())
|
||||
if let Some(area) = self.layout(to.area())? {
|
||||
to.render_in(area, self.inner())
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -499,11 +499,11 @@ impl<E: Engine, T: Widget<Engine = E>> Widget for Max<E:: Unit, T> {
|
|||
}
|
||||
}
|
||||
fn render (&self, to: &mut E) -> Perhaps<E::Area> {
|
||||
self.layout(to.area())?
|
||||
.map(|area|to.with_area(area.x(), area.y(), area.w(), area.h()))
|
||||
.map(|to|self.inner().render(to))
|
||||
.transpose()
|
||||
.map(|x|x.flatten())
|
||||
if let Some(area) = self.layout(to.area())? {
|
||||
to.render_in(area, self.inner())
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -530,24 +530,30 @@ impl<N: Number, T> Outset<N, T> {
|
|||
impl<E: Engine, T: Widget<Engine = E>> Widget for Outset<E::Unit, T> {
|
||||
type Engine = E;
|
||||
fn layout (&self, area: E::Area) -> Perhaps<E::Area> {
|
||||
match self {
|
||||
Self::W(w, item) => if area.x() < *w { Ok(None) } else {
|
||||
item.layout([area.x() - *w, area.y(), area.w() + *w, area.h()].into())
|
||||
Ok(match self {
|
||||
Self::W(w, item) => if area.x() < *w { None } else {
|
||||
item.layout(area)?.map(|area|[
|
||||
area.x() - *w, area.y(), area.w() + *w + *w, area.h()
|
||||
].into())
|
||||
},
|
||||
Self::H(h, item) => if area.y() < *h { Ok(None) } else {
|
||||
item.layout([area.x(), area.y() - *h, area.w(), area.h() + *h].into())
|
||||
Self::H(h, item) => if area.y() < *h { None } else {
|
||||
item.layout(area)?.map(|area|[
|
||||
area.x(), area.y() - *h, area.w(), area.h() + *h + *h
|
||||
].into())
|
||||
},
|
||||
Self::WH(w, h, item) => if area.x() < *w || area.y() < *h { Ok(None) } else {
|
||||
item.layout([area.x()- *w, area.y() - *h, area.w() + *w, area.h() + *h].into())
|
||||
Self::WH(w, h, item) => if area.x() < *w || area.y() < *h { None } else {
|
||||
item.layout(area)?.map(|area|[
|
||||
area.x()- *w, area.y() - *h, area.w() + *w + *w, area.h() + *h + *h
|
||||
].into())
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
fn render (&self, to: &mut E) -> Perhaps<E::Area> {
|
||||
self.layout(to.area())?
|
||||
.map(|area|to.with_area(area.x(), area.y(), area.w(), area.h()))
|
||||
.map(|to|self.inner().render(to))
|
||||
.transpose()
|
||||
.map(|x|x.flatten())
|
||||
if let Some(area) = self.layout(to.area())? {
|
||||
to.render_in(area, self.inner())
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -574,24 +580,30 @@ impl<N: Number, T: Widget> Inset<N, T> {
|
|||
impl<E: Engine, T: Widget<Engine = E>> Widget for Inset<E::Unit, T> {
|
||||
type Engine = E;
|
||||
fn layout (&self, area: E::Area) -> Perhaps<E::Area> {
|
||||
match self {
|
||||
Self::W(w, item) => if area.w() < *w { Ok(None) } else {
|
||||
item.layout([area.x() + *w, area.y(), area.w() - *w, area.h()].into())
|
||||
Ok(match self {
|
||||
Self::W(w, item) => if area.w() < *w { None } else {
|
||||
item.layout(area)?.map(|area|[
|
||||
area.x() + *w, area.y(), area.w() - *w, area.h()
|
||||
].into())
|
||||
},
|
||||
Self::H(h, item) => if area.h() < *h { Ok(None) } else {
|
||||
item.layout([area.x(), area.y() + *h, area.w(), area.h() - *h].into())
|
||||
Self::H(h, item) => if area.h() < *h { None } else {
|
||||
item.layout(area)?.map(|area|[
|
||||
area.x(), area.y() + *h, area.w(), area.h() - *h
|
||||
].into())
|
||||
},
|
||||
Self::WH(w, h, item) => if area.w() < *w || area.h() < *h { Ok(None) } else {
|
||||
item.layout([area.x() - *w, area.y() - *h, area.w() + *w, area.h() + *h].into())
|
||||
Self::WH(w, h, item) => if area.w() < *w || area.h() < *h { None } else {
|
||||
item.layout(area)?.map(|area|[
|
||||
area.x() - *w, area.y() - *h, area.w() + *w, area.h() + *h
|
||||
].into())
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
fn render (&self, to: &mut E) -> Perhaps<E::Area> {
|
||||
self.layout(to.area())?
|
||||
.map(|area|to.with_area(area.x(), area.y(), area.w(), area.h()))
|
||||
.map(|to|self.inner().render(to))
|
||||
.transpose()
|
||||
.map(|x|x.flatten())
|
||||
if let Some(area) = self.layout(to.area())? {
|
||||
to.render_in(area, self.inner())
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -617,22 +629,25 @@ impl<E: Engine, T: Widget<Engine = E>> Widget for Offset<E::Unit, T> {
|
|||
fn layout (&self, area: E::Area) -> Perhaps<E::Area> {
|
||||
match self {
|
||||
Self::X(x, item) => if area.w() < *x { Ok(None) } else {
|
||||
item.layout([area.x() + *x, area.y(), area.w() - *x, area.h()].into())
|
||||
let offset_area = [area.x() + *x, area.y(), area.w() - *x, area.h()];
|
||||
item.layout(offset_area.into())
|
||||
},
|
||||
Self::Y(y, item) => if area.h() < *y { Ok(None) } else {
|
||||
item.layout([area.x(), area.y() + *y, area.w(), area.h() - *y].into())
|
||||
let offset_area = [area.x(), area.y() + *y, area.w(), area.h() - *y];
|
||||
item.layout(offset_area.into())
|
||||
},
|
||||
Self::XY(x, y, item) => if area.w() < *x || area.h() < *y { Ok(None) } else {
|
||||
item.layout([area.x() + *x, area.y() + *y, area.w() - *x, area.h() - *y].into())
|
||||
let offset_area = [area.x() + *x, area.y() + *y, area.w() - *x, area.h() - *y];
|
||||
item.layout(offset_area.into())
|
||||
}
|
||||
}
|
||||
}
|
||||
fn render (&self, to: &mut E) -> Perhaps<E::Area> {
|
||||
self.layout(to.area())?
|
||||
.map(|area|to.with_area(area.x(), area.y(), area.w(), area.h()))
|
||||
.map(|to|self.inner().render(to))
|
||||
.transpose()
|
||||
.map(|x|x.flatten())
|
||||
if let Some(area) = self.layout(to.area())? {
|
||||
to.render_in(area, self.inner())
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ pub(crate) use std::thread::{spawn, JoinHandle};
|
|||
pub(crate) use std::time::Duration;
|
||||
pub(crate) use atomic_float::*;
|
||||
use better_panic::{Settings, Verbosity};
|
||||
use std::ops::{Add, Sub, Div};
|
||||
use std::ops::{Add, Sub, Mul, Div};
|
||||
use std::cmp::{Ord, Eq, PartialEq};
|
||||
use std::fmt::{Debug, Display};
|
||||
|
||||
|
|
@ -49,6 +49,7 @@ pub type Perhaps<T> = Result<Option<T>, Box<dyn Error>>;
|
|||
pub trait Number: Send + Sync + Copy
|
||||
+ Add<Self, Output=Self>
|
||||
+ Sub<Self, Output=Self>
|
||||
+ Mul<Self, Output=Self>
|
||||
+ Div<Self, Output=Self>
|
||||
+ Ord + PartialEq + Eq
|
||||
+ Debug + Display {}
|
||||
|
|
@ -57,6 +58,7 @@ impl<T> Number for T where
|
|||
T: Send + Sync + Copy
|
||||
+ Add<Self, Output=Self>
|
||||
+ Sub<Self, Output=Self>
|
||||
+ Mul<Self, Output=Self>
|
||||
+ Div<Self, Output=Self>
|
||||
+ Ord + PartialEq + Eq
|
||||
+ Debug + Display
|
||||
|
|
|
|||
|
|
@ -288,6 +288,9 @@ pub struct FillBg(pub Color);
|
|||
|
||||
impl Widget for FillBg {
|
||||
type Engine = Tui;
|
||||
fn layout (&self, [x,y,_,_]: [u16;4]) -> Perhaps<[u16;4]> {
|
||||
Ok(Some([x,y,0,0]))
|
||||
}
|
||||
fn render (&self, to: &mut Tui) -> Perhaps<[u16;4]> {
|
||||
to.fill_bg(to.area(), self.0);
|
||||
Ok(Some(to.area))
|
||||
|
|
@ -457,9 +460,8 @@ where
|
|||
if h >= to.h() {
|
||||
return Ok(())
|
||||
}
|
||||
if let Some([_, _, width, height]) = Offset::Y(
|
||||
h, component as &dyn Widget<Engine = Tui>
|
||||
).layout(to)? {
|
||||
let area = Offset::Y(h, component as &dyn Widget<Engine = Tui>).layout(to)?;
|
||||
if let Some([_, _, width, height]) = area {
|
||||
h += height;
|
||||
w = w.max(width)
|
||||
}
|
||||
|
|
@ -554,14 +556,14 @@ where
|
|||
impl<T: Widget<Engine = Tui>> Widget for Align<T> {
|
||||
type Engine = Tui;
|
||||
fn layout (&self, outer_area: [u16;4]) -> Perhaps<[u16;4]> {
|
||||
Ok(self.inner().layout(outer_area)?.map(|inner_area|match self {
|
||||
Self::Center(_) => {
|
||||
Ok(match self {
|
||||
Self::Center(_) => self.inner().layout(outer_area)?.map(|inner_area|{
|
||||
let [_, _, w, h] = inner_area.xywh();
|
||||
let offset_x = (outer_area.w() / 2).saturating_sub(w / 2);
|
||||
let offset_y = (outer_area.h() / 2).saturating_sub(h / 2);
|
||||
let result = [outer_area.x() + offset_x, outer_area.y() + offset_y, w, h];
|
||||
result
|
||||
},
|
||||
}),
|
||||
Self::NW(_) => { todo!() },
|
||||
Self::N(_) => { todo!() },
|
||||
Self::NE(_) => { todo!() },
|
||||
|
|
@ -570,7 +572,7 @@ impl<T: Widget<Engine = Tui>> Widget for Align<T> {
|
|||
Self::SW(_) => { todo!() },
|
||||
Self::S(_) => { todo!() },
|
||||
Self::SE(_) => { todo!() },
|
||||
}))
|
||||
})
|
||||
}
|
||||
fn render (&self, to: &mut Tui) -> Perhaps<[u16;4]> {
|
||||
self.layout(to.area())?
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue