mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-07 12:16:42 +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
|
/// Enforce minimum size of drawing area
|
||||||
pub enum Min<U: Number, T> {
|
pub enum Min<U: Number, T> {
|
||||||
/// Enforce minimum width
|
/// Enforce minimum width
|
||||||
W(U, T),
|
X(U, T),
|
||||||
/// Enforce minimum height
|
/// Enforce minimum height
|
||||||
H(U, T),
|
Y(U, T),
|
||||||
/// Enforce minimum width and height
|
/// Enforce minimum width and height
|
||||||
WH(U, U, T),
|
XY(U, U, T),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<N: Number, T> Min<N, T> {
|
impl<N: Number, T> Min<N, T> {
|
||||||
fn inner (&self) -> &T {
|
fn inner (&self) -> &T {
|
||||||
match self {
|
match self { Self::X(_, i) => i, Self::Y(_, i) => i, Self::XY(_, _, i) => i, }
|
||||||
Self::W(_, inner) => inner,
|
|
||||||
Self::H(_, inner) => inner,
|
|
||||||
Self::WH(_, _, inner) => inner,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<E: Engine, T: Widget<Engine = E>> Widget for Min<E::Unit, T> {
|
impl<E: Engine, T: Widget<Engine = E>> Widget for Min<E::Unit, T> {
|
||||||
type Engine = E;
|
type Engine = E;
|
||||||
fn layout (&self, area: E::Area) -> Perhaps<E::Area> {
|
fn layout (&self, to: E::Area) -> Perhaps<E::Area> {
|
||||||
match self {
|
Ok(match self {
|
||||||
Self::W(w, item) => if area.w() < *w { Ok(None) } else {
|
Self::X(w, _) => (to.w() < *w).then(||{
|
||||||
// TODO: axis clamp (subtract from x if width goes out of area
|
[to.x() + *w, to.y(), to.w() - *w, to.h()]
|
||||||
item.layout([area.x(), area.y(), area.w().max(*w), area.h()].into())
|
}),
|
||||||
},
|
Self::Y(h, _) => (to.h() < *h).then(||{
|
||||||
Self::H(h, item) => if area.w() < *h { Ok(None) } else {
|
[to.x(), to.y() + *h, to.w(), to.h() - *h]
|
||||||
// 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::XY(w, h, _) => (to.w() < *w || to.h() < *h).then(||{
|
||||||
},
|
[to.x() + *w, to.y() + *h, to.w() - *w, to.h() - *h]
|
||||||
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())
|
}.map(|offset_area|self.inner().layout(offset_area.into())).transpose()?.flatten())
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
// TODO: 🡘 🡙 ←🡙→
|
||||||
fn render (&self, to: &mut E) -> Perhaps<E::Area> {
|
fn render (&self, to: &mut E) -> Perhaps<E::Area> {
|
||||||
// 🡘 🡙 ←🡙→
|
Ok(self.layout(to.area())?.map(|a|to.render_in(a, self.inner())).transpose()?.flatten())
|
||||||
if let Some(area) = self.layout(to.area())? {
|
|
||||||
to.render_in(area, self.inner())
|
|
||||||
} else {
|
|
||||||
Ok(None)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Enforce maximum size of drawing area
|
/// Enforce maximum size of drawing area
|
||||||
pub enum Max<U: Number, T> {
|
pub enum Max<U: Number, T> {
|
||||||
/// Enforce maximum width
|
/// Enforce maximum width
|
||||||
W(U, T),
|
X(U, T),
|
||||||
/// Enforce maximum height
|
/// Enforce maximum height
|
||||||
H(U, T),
|
Y(U, T),
|
||||||
/// Enforce maximum width and height
|
/// Enforce maximum width and height
|
||||||
WH(U, U, T),
|
XY(U, U, T),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<N: Number, T> Max<N, T> {
|
impl<N: Number, T> Max<N, T> {
|
||||||
fn inner (&self) -> &T {
|
fn inner (&self) -> &T {
|
||||||
match self {
|
match self { Self::X(_, i) => i, Self::Y(_, i) => i, Self::XY(_, _, i) => i, }
|
||||||
Self::W(_, inner) => inner,
|
|
||||||
Self::H(_, inner) => inner,
|
|
||||||
Self::WH(_, _, inner) => inner,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<E: Engine, T: Widget<Engine = E>> Widget for Max<E:: Unit, T> {
|
impl<E: Engine, T: Widget<Engine = E>> Widget for Max<E:: Unit, T> {
|
||||||
type Engine = E;
|
type Engine = E;
|
||||||
fn layout (&self, area: E::Area) -> Perhaps<E::Area> {
|
fn layout (&self, area: E::Area) -> Perhaps<E::Area> {
|
||||||
match self {
|
Ok(match self {
|
||||||
Self::W(w, item) => {
|
Self::X(w, _) => (area.w() < *w).then(||{
|
||||||
// TODO: axis clamp (subtract from x if width goes out of area
|
[area.x(), area.y(), area.w().min(*w), area.h()]
|
||||||
item.layout([area.x(), area.y(), area.w().min(*w), area.h()].into())
|
}),
|
||||||
},
|
Self::Y(h, _) => (area.h() < *h).then(||{
|
||||||
Self::H(h, item) => {
|
[area.x(), area.y(), area.w(), area.h().min(*h)]
|
||||||
// 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::XY(w, h, _) => (area.w() < *w || area.h() < *h).then(||{
|
||||||
},
|
[area.x(), area.y(), area.w().min(*w), area.h().min(*h)]
|
||||||
Self::WH(w, h, item) => {
|
})
|
||||||
item.layout([area.x(), area.y(), area.w().min(*w), area.h().min(*h)].into())
|
}.map(|offset_area|self.inner().layout(offset_area.into())).transpose()?.flatten())
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
fn render (&self, to: &mut E) -> Perhaps<E::Area> {
|
fn render (&self, to: &mut E) -> Perhaps<E::Area> {
|
||||||
if let Some(area) = self.layout(to.area())? {
|
Ok(self.layout(to.area())?.map(|a|to.render_in(a, self.inner())).transpose()?.flatten())
|
||||||
to.render_in(area, self.inner())
|
|
||||||
} else {
|
|
||||||
Ok(None)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Expand drawing area
|
/// Expand drawing area
|
||||||
pub enum Outset<N: Number, T> {
|
pub enum Outset<N: Number, T> {
|
||||||
/// Increase width
|
/// Increase width
|
||||||
W(N, T),
|
X(N, T),
|
||||||
/// Increase height
|
/// Increase height
|
||||||
H(N, T),
|
Y(N, T),
|
||||||
/// Increase width and height
|
/// Increase width and height
|
||||||
WH(N, N, T)
|
XY(N, N, T)
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<N: Number, T> Outset<N, T> {
|
impl<N: Number, T> Outset<N, T> {
|
||||||
fn inner (&self) -> &T {
|
fn inner (&self) -> &T {
|
||||||
match self {
|
match self { Self::X(_, i) => i, Self::Y(_, i) => i, Self::XY(_, _, i) => i, }
|
||||||
Self::W(_, inner) => inner,
|
|
||||||
Self::H(_, inner) => inner,
|
|
||||||
Self::WH(_, _, inner) => inner,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -522,49 +496,35 @@ impl<E: Engine, T: Widget<Engine = E>> Widget for Outset<E::Unit, T> {
|
||||||
type Engine = E;
|
type Engine = E;
|
||||||
fn layout (&self, area: E::Area) -> Perhaps<E::Area> {
|
fn layout (&self, area: E::Area) -> Perhaps<E::Area> {
|
||||||
Ok(match self {
|
Ok(match self {
|
||||||
Self::W(w, item) => if area.x() < *w { None } else {
|
Self::X(w, _) => (area.w() < *w).then(||{
|
||||||
item.layout(area)?.map(|area|[
|
[area.x() - *w, area.y(), area.w() + *w + *w, area.h()]
|
||||||
area.x() - *w, area.y(), area.w() + *w + *w, area.h()
|
}),
|
||||||
].into())
|
Self::Y(h, _) => (area.h() < *h).then(||{
|
||||||
},
|
[area.x(), area.y() - *h, area.w(), area.h() + *h + *h]
|
||||||
Self::H(h, item) => if area.y() < *h { None } else {
|
}),
|
||||||
item.layout(area)?.map(|area|[
|
Self::XY(w, h, _) => (area.w() < *w || area.h() < *h).then(||{
|
||||||
area.x(), area.y() - *h, area.w(), area.h() + *h + *h
|
[area.x()- *w, area.y() - *h, area.w() + *w + *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())
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
}.map(|offset_area|self.inner().layout(offset_area.into())).transpose()?.flatten())
|
||||||
}
|
}
|
||||||
fn render (&self, to: &mut E) -> Perhaps<E::Area> {
|
fn render (&self, to: &mut E) -> Perhaps<E::Area> {
|
||||||
if let Some(area) = self.layout(to.area())? {
|
Ok(self.layout(to.area())?.map(|a|to.render_in(a, self.inner())).transpose()?.flatten())
|
||||||
to.render_in(area, self.inner())
|
|
||||||
} else {
|
|
||||||
Ok(None)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Shrink drawing area
|
/// Shrink drawing area
|
||||||
pub enum Inset<N: Number, T> {
|
pub enum Inset<N: Number, T> {
|
||||||
/// Decrease width
|
/// Decrease width
|
||||||
W(N, T),
|
X(N, T),
|
||||||
/// Decrease height
|
/// Decrease height
|
||||||
H(N, T),
|
Y(N, T),
|
||||||
/// Decrease width and height
|
/// Decrease width and height
|
||||||
WH(N, N, T),
|
XY(N, N, T),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<N: Number, T: Widget> Inset<N, T> {
|
impl<N: Number, T: Widget> Inset<N, T> {
|
||||||
fn inner (&self) -> &T {
|
fn inner (&self) -> &T {
|
||||||
match self {
|
match self { Self::X(_, i) => i, Self::Y(_, i) => i, Self::XY(_, _, i) => i, }
|
||||||
Self::W(_, inner) => inner,
|
|
||||||
Self::H(_, inner) => inner,
|
|
||||||
Self::WH(_, _, inner) => inner,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -572,73 +532,55 @@ impl<E: Engine, T: Widget<Engine = E>> Widget for Inset<E::Unit, T> {
|
||||||
type Engine = E;
|
type Engine = E;
|
||||||
fn layout (&self, area: E::Area) -> Perhaps<E::Area> {
|
fn layout (&self, area: E::Area) -> Perhaps<E::Area> {
|
||||||
Ok(match self {
|
Ok(match self {
|
||||||
Self::W(w, item) => if area.w() < *w { None } else {
|
Self::X(w, _) => (area.w() < *w).then(||{
|
||||||
item.layout(area)?.map(|area|[
|
[area.x() + *w, area.y(), area.w() - *w, area.h()]
|
||||||
area.x() + *w, area.y(), area.w() - *w, area.h()
|
}),
|
||||||
].into())
|
Self::Y(h, _) => (area.h() < *h).then(||{
|
||||||
},
|
[area.x(), area.y() + *h, area.w(), area.h() - *h]
|
||||||
Self::H(h, item) => if area.h() < *h { None } else {
|
}),
|
||||||
item.layout(area)?.map(|area|[
|
Self::XY(w, h, _) => (area.w() < *w || area.h() < *h).then(||{
|
||||||
area.x(), area.y() + *h, area.w(), area.h() - *h
|
[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())
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
|
}.map(|offset_area|self.inner().layout(offset_area.into())).transpose()?.flatten())
|
||||||
}
|
}
|
||||||
fn render (&self, to: &mut E) -> Perhaps<E::Area> {
|
fn render (&self, to: &mut E) -> Perhaps<E::Area> {
|
||||||
if let Some(area) = self.layout(to.area())? {
|
Ok(self.layout(to.area())?.map(|a|to.render_in(a, self.inner())).transpose()?.flatten())
|
||||||
to.render_in(area, self.inner())
|
|
||||||
} else {
|
|
||||||
Ok(None)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Move origin point of drawing area
|
/// Move origin point of drawing area
|
||||||
pub enum Offset<N: Number, T: Widget> {
|
pub enum Offset<N: Number, T: Widget> {
|
||||||
|
/// Move origin to the right
|
||||||
X(N, T),
|
X(N, T),
|
||||||
|
/// Move origin downwards
|
||||||
Y(N, T),
|
Y(N, T),
|
||||||
|
/// Move origin to the right and downwards
|
||||||
XY(N, N, T),
|
XY(N, N, T),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<N: Number, T: Widget> Offset<N, T> {
|
impl<N: Number, T: Widget> Offset<N, T> {
|
||||||
fn inner (&self) -> &T {
|
fn inner (&self) -> &T {
|
||||||
match self {
|
match self { Self::X(_, i) => i, Self::Y(_, i) => i, Self::XY(_, _, i) => i, }
|
||||||
Self::X(_, inner) => inner,
|
|
||||||
Self::Y(_, inner) => inner,
|
|
||||||
Self::XY(_, _, inner) => inner,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<E: Engine, T: Widget<Engine = E>> Widget for Offset<E::Unit, T> {
|
impl<E: Engine, T: Widget<Engine = E>> Widget for Offset<E::Unit, T> {
|
||||||
type Engine = E;
|
type Engine = E;
|
||||||
fn layout (&self, area: E::Area) -> Perhaps<E::Area> {
|
fn layout (&self, area: E::Area) -> Perhaps<E::Area> {
|
||||||
match self {
|
Ok(match self {
|
||||||
Self::X(x, item) => if area.w() < *x { Ok(None) } else {
|
Self::X(w, _) => (area.w() < *w).then(||{
|
||||||
let offset_area = [area.x() + *x, area.y(), area.w() - *x, area.h()];
|
[area.x() + *w, area.y(), area.w() - *w, area.h()]
|
||||||
item.layout(offset_area.into())
|
}),
|
||||||
},
|
Self::Y(h, _) => (area.h() < *h).then(||{
|
||||||
Self::Y(y, item) => if area.h() < *y { Ok(None) } else {
|
[area.x(), area.y() + *h, area.w(), area.h() - *h]
|
||||||
let offset_area = [area.x(), area.y() + *y, area.w(), area.h() - *y];
|
}),
|
||||||
item.layout(offset_area.into())
|
Self::XY(w, h, _) => (area.w() < *w || area.h() < *h).then(||{
|
||||||
},
|
[area.x() + *w, area.y() + *h, area.w() - *w, area.h() - *h]
|
||||||
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];
|
}.map(|offset_area|self.inner().layout(offset_area.into())).transpose()?.flatten())
|
||||||
item.layout(offset_area.into())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
fn render (&self, to: &mut E) -> Perhaps<E::Area> {
|
fn render (&self, to: &mut E) -> Perhaps<E::Area> {
|
||||||
if let Some(area) = self.layout(to.area())? {
|
Ok(self.layout(to.area())?.map(|a|to.render_in(a, self.inner())).transpose()?.flatten())
|
||||||
to.render_in(area, self.inner())
|
|
||||||
} else {
|
|
||||||
Ok(None)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -506,7 +506,7 @@ impl<'a> Content for TracksHeader<'a> {
|
||||||
let Self(_offset, columns, tracks) = *self;
|
let Self(_offset, columns, tracks) = *self;
|
||||||
Split::right(move |add|{
|
Split::right(move |add|{
|
||||||
for (track, (w, _)) in tracks.iter().zip(columns) {
|
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(&FillBg(COLOR_BG1))?;
|
||||||
add(&track.name.read().unwrap().as_str())
|
add(&track.name.read().unwrap().as_str())
|
||||||
})))?;
|
})))?;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue