mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-06 11:46:41 +01:00
use X/Y instead of W/H in layout widgets
- also core is <2000LoC now yay! - also using more methods and fewer conditionals
This commit is contained in:
parent
4b19abd98a
commit
02db343574
2 changed files with 81 additions and 139 deletions
|
|
@ -408,113 +408,87 @@ impl<N: Number, T> Fixed<N, T> {
|
|||
/// Enforce minimum size of drawing area
|
||||
pub enum Min<U: Number, T> {
|
||||
/// Enforce minimum width
|
||||
W(U, T),
|
||||
X(U, T),
|
||||
/// Enforce minimum height
|
||||
H(U, T),
|
||||
Y(U, T),
|
||||
/// Enforce minimum width and height
|
||||
WH(U, U, T),
|
||||
XY(U, U, T),
|
||||
}
|
||||
|
||||
impl<N: Number, T> Min<N, T> {
|
||||
fn inner (&self) -> &T {
|
||||
match self {
|
||||
Self::W(_, inner) => inner,
|
||||
Self::H(_, inner) => inner,
|
||||
Self::WH(_, _, inner) => inner,
|
||||
}
|
||||
match self { Self::X(_, i) => i, Self::Y(_, i) => i, Self::XY(_, _, i) => i, }
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: Engine, T: Widget<Engine = E>> Widget for Min<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 {
|
||||
// TODO: axis clamp (subtract from x if width goes out of area
|
||||
item.layout([area.x(), area.y(), area.w().max(*w), area.h()].into())
|
||||
},
|
||||
Self::H(h, item) => if area.w() < *h { Ok(None) } else {
|
||||
// TODO: axis clamp (subtract from x if width goes out of area
|
||||
item.layout([area.x(), area.y(), area.w(), area.h().max(*h)].into())
|
||||
},
|
||||
Self::WH(w, h, item) => if area.w() < *w || area.h() < *h { Ok(None) } else {
|
||||
item.layout([area.x(), area.y(), area.w().max(*w), area.h().max(*h)].into())
|
||||
}
|
||||
}
|
||||
fn layout (&self, to: E::Area) -> Perhaps<E::Area> {
|
||||
Ok(match self {
|
||||
Self::X(w, _) => (to.w() < *w).then(||{
|
||||
[to.x() + *w, to.y(), to.w() - *w, to.h()]
|
||||
}),
|
||||
Self::Y(h, _) => (to.h() < *h).then(||{
|
||||
[to.x(), to.y() + *h, to.w(), to.h() - *h]
|
||||
}),
|
||||
Self::XY(w, h, _) => (to.w() < *w || to.h() < *h).then(||{
|
||||
[to.x() + *w, to.y() + *h, to.w() - *w, to.h() - *h]
|
||||
})
|
||||
}.map(|offset_area|self.inner().layout(offset_area.into())).transpose()?.flatten())
|
||||
}
|
||||
// TODO: 🡘 🡙 ←🡙→
|
||||
fn render (&self, to: &mut E) -> Perhaps<E::Area> {
|
||||
// 🡘 🡙 ←🡙→
|
||||
if let Some(area) = self.layout(to.area())? {
|
||||
to.render_in(area, self.inner())
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
Ok(self.layout(to.area())?.map(|a|to.render_in(a, self.inner())).transpose()?.flatten())
|
||||
}
|
||||
}
|
||||
|
||||
/// Enforce maximum size of drawing area
|
||||
pub enum Max<U: Number, T> {
|
||||
/// Enforce maximum width
|
||||
W(U, T),
|
||||
X(U, T),
|
||||
/// Enforce maximum height
|
||||
H(U, T),
|
||||
Y(U, T),
|
||||
/// Enforce maximum width and height
|
||||
WH(U, U, T),
|
||||
XY(U, U, T),
|
||||
}
|
||||
|
||||
impl<N: Number, T> Max<N, T> {
|
||||
fn inner (&self) -> &T {
|
||||
match self {
|
||||
Self::W(_, inner) => inner,
|
||||
Self::H(_, inner) => inner,
|
||||
Self::WH(_, _, inner) => inner,
|
||||
}
|
||||
match self { Self::X(_, i) => i, Self::Y(_, i) => i, Self::XY(_, _, i) => i, }
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: Engine, T: Widget<Engine = E>> Widget for Max<E:: Unit, T> {
|
||||
type Engine = E;
|
||||
fn layout (&self, area: E::Area) -> Perhaps<E::Area> {
|
||||
match self {
|
||||
Self::W(w, item) => {
|
||||
// TODO: axis clamp (subtract from x if width goes out of area
|
||||
item.layout([area.x(), area.y(), area.w().min(*w), area.h()].into())
|
||||
},
|
||||
Self::H(h, item) => {
|
||||
// TODO: axis clamp (subtract from x if width goes out of area
|
||||
item.layout([area.x(), area.y(), area.w(), area.h().min(*h)].into())
|
||||
},
|
||||
Self::WH(w, h, item) => {
|
||||
item.layout([area.x(), area.y(), area.w().min(*w), area.h().min(*h)].into())
|
||||
}
|
||||
}
|
||||
Ok(match self {
|
||||
Self::X(w, _) => (area.w() < *w).then(||{
|
||||
[area.x(), area.y(), area.w().min(*w), area.h()]
|
||||
}),
|
||||
Self::Y(h, _) => (area.h() < *h).then(||{
|
||||
[area.x(), area.y(), area.w(), area.h().min(*h)]
|
||||
}),
|
||||
Self::XY(w, h, _) => (area.w() < *w || area.h() < *h).then(||{
|
||||
[area.x(), area.y(), area.w().min(*w), area.h().min(*h)]
|
||||
})
|
||||
}.map(|offset_area|self.inner().layout(offset_area.into())).transpose()?.flatten())
|
||||
}
|
||||
fn render (&self, to: &mut E) -> Perhaps<E::Area> {
|
||||
if let Some(area) = self.layout(to.area())? {
|
||||
to.render_in(area, self.inner())
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
Ok(self.layout(to.area())?.map(|a|to.render_in(a, self.inner())).transpose()?.flatten())
|
||||
}
|
||||
}
|
||||
|
||||
/// Expand drawing area
|
||||
pub enum Outset<N: Number, T> {
|
||||
/// Increase width
|
||||
W(N, T),
|
||||
X(N, T),
|
||||
/// Increase height
|
||||
H(N, T),
|
||||
Y(N, T),
|
||||
/// Increase width and height
|
||||
WH(N, N, T)
|
||||
XY(N, N, T)
|
||||
}
|
||||
|
||||
impl<N: Number, T> Outset<N, T> {
|
||||
fn inner (&self) -> &T {
|
||||
match self {
|
||||
Self::W(_, inner) => inner,
|
||||
Self::H(_, inner) => inner,
|
||||
Self::WH(_, _, inner) => inner,
|
||||
}
|
||||
match self { Self::X(_, i) => i, Self::Y(_, i) => i, Self::XY(_, _, i) => i, }
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -522,49 +496,35 @@ 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> {
|
||||
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 { 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 { None } else {
|
||||
item.layout(area)?.map(|area|[
|
||||
area.x()- *w, area.y() - *h, area.w() + *w + *w, area.h() + *h + *h
|
||||
].into())
|
||||
}
|
||||
})
|
||||
Self::X(w, _) => (area.w() < *w).then(||{
|
||||
[area.x() - *w, area.y(), area.w() + *w + *w, area.h()]
|
||||
}),
|
||||
Self::Y(h, _) => (area.h() < *h).then(||{
|
||||
[area.x(), area.y() - *h, area.w(), area.h() + *h + *h]
|
||||
}),
|
||||
Self::XY(w, h, _) => (area.w() < *w || area.h() < *h).then(||{
|
||||
[area.x()- *w, area.y() - *h, area.w() + *w + *w, area.h() + *h + *h]
|
||||
})
|
||||
}.map(|offset_area|self.inner().layout(offset_area.into())).transpose()?.flatten())
|
||||
}
|
||||
fn render (&self, to: &mut E) -> Perhaps<E::Area> {
|
||||
if let Some(area) = self.layout(to.area())? {
|
||||
to.render_in(area, self.inner())
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
Ok(self.layout(to.area())?.map(|a|to.render_in(a, self.inner())).transpose()?.flatten())
|
||||
}
|
||||
}
|
||||
|
||||
/// Shrink drawing area
|
||||
pub enum Inset<N: Number, T> {
|
||||
/// Decrease width
|
||||
W(N, T),
|
||||
X(N, T),
|
||||
/// Decrease height
|
||||
H(N, T),
|
||||
Y(N, T),
|
||||
/// Decrease width and height
|
||||
WH(N, N, T),
|
||||
XY(N, N, T),
|
||||
}
|
||||
|
||||
impl<N: Number, T: Widget> Inset<N, T> {
|
||||
fn inner (&self) -> &T {
|
||||
match self {
|
||||
Self::W(_, inner) => inner,
|
||||
Self::H(_, inner) => inner,
|
||||
Self::WH(_, _, inner) => inner,
|
||||
}
|
||||
match self { Self::X(_, i) => i, Self::Y(_, i) => i, Self::XY(_, _, i) => i, }
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -572,73 +532,55 @@ 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> {
|
||||
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 { 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 { None } else {
|
||||
item.layout(area)?.map(|area|[
|
||||
area.x() - *w, area.y() - *h, area.w() + *w, area.h() + *h
|
||||
].into())
|
||||
}
|
||||
})
|
||||
Self::X(w, _) => (area.w() < *w).then(||{
|
||||
[area.x() + *w, area.y(), area.w() - *w, area.h()]
|
||||
}),
|
||||
Self::Y(h, _) => (area.h() < *h).then(||{
|
||||
[area.x(), area.y() + *h, area.w(), area.h() - *h]
|
||||
}),
|
||||
Self::XY(w, h, _) => (area.w() < *w || area.h() < *h).then(||{
|
||||
[area.x() - *w, area.y() - *h, area.w() + *w, area.h() + *h]
|
||||
})
|
||||
}.map(|offset_area|self.inner().layout(offset_area.into())).transpose()?.flatten())
|
||||
}
|
||||
fn render (&self, to: &mut E) -> Perhaps<E::Area> {
|
||||
if let Some(area) = self.layout(to.area())? {
|
||||
to.render_in(area, self.inner())
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
Ok(self.layout(to.area())?.map(|a|to.render_in(a, self.inner())).transpose()?.flatten())
|
||||
}
|
||||
}
|
||||
|
||||
/// Move origin point of drawing area
|
||||
pub enum Offset<N: Number, T: Widget> {
|
||||
/// Move origin to the right
|
||||
X(N, T),
|
||||
/// Move origin downwards
|
||||
Y(N, T),
|
||||
/// Move origin to the right and downwards
|
||||
XY(N, N, T),
|
||||
}
|
||||
|
||||
impl<N: Number, T: Widget> Offset<N, T> {
|
||||
fn inner (&self) -> &T {
|
||||
match self {
|
||||
Self::X(_, inner) => inner,
|
||||
Self::Y(_, inner) => inner,
|
||||
Self::XY(_, _, inner) => inner,
|
||||
}
|
||||
match self { Self::X(_, i) => i, Self::Y(_, i) => i, Self::XY(_, _, i) => i, }
|
||||
}
|
||||
}
|
||||
|
||||
impl<E: Engine, T: Widget<Engine = E>> Widget for Offset<E::Unit, T> {
|
||||
type Engine = E;
|
||||
fn layout (&self, area: E::Area) -> Perhaps<E::Area> {
|
||||
match self {
|
||||
Self::X(x, item) => if area.w() < *x { Ok(None) } else {
|
||||
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 {
|
||||
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 {
|
||||
let offset_area = [area.x() + *x, area.y() + *y, area.w() - *x, area.h() - *y];
|
||||
item.layout(offset_area.into())
|
||||
}
|
||||
}
|
||||
Ok(match self {
|
||||
Self::X(w, _) => (area.w() < *w).then(||{
|
||||
[area.x() + *w, area.y(), area.w() - *w, area.h()]
|
||||
}),
|
||||
Self::Y(h, _) => (area.h() < *h).then(||{
|
||||
[area.x(), area.y() + *h, area.w(), area.h() - *h]
|
||||
}),
|
||||
Self::XY(w, h, _) => (area.w() < *w || area.h() < *h).then(||{
|
||||
[area.x() + *w, area.y() + *h, area.w() - *w, area.h() - *h]
|
||||
})
|
||||
}.map(|offset_area|self.inner().layout(offset_area.into())).transpose()?.flatten())
|
||||
}
|
||||
fn render (&self, to: &mut E) -> Perhaps<E::Area> {
|
||||
if let Some(area) = self.layout(to.area())? {
|
||||
to.render_in(area, self.inner())
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
Ok(self.layout(to.area())?.map(|a|to.render_in(a, self.inner())).transpose()?.flatten())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -506,7 +506,7 @@ impl<'a> Content for TracksHeader<'a> {
|
|||
let Self(_offset, columns, tracks) = *self;
|
||||
Split::right(move |add|{
|
||||
for (track, (w, _)) in tracks.iter().zip(columns) {
|
||||
add(&Min::W(*w as u16, Layers::new(|add|{
|
||||
add(&Min::X(*w as u16, Layers::new(|add|{
|
||||
add(&FillBg(COLOR_BG1))?;
|
||||
add(&track.name.read().unwrap().as_str())
|
||||
})))?;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue