mirror of
https://codeberg.org/unspeaker/tek.git
synced 2025-12-08 20:56:43 +01:00
still dark; refactor and document layout crate
This commit is contained in:
parent
ed72ab1635
commit
675d376100
12 changed files with 510 additions and 659 deletions
101
layout/src/transform_xy_unit.rs
Normal file
101
layout/src/transform_xy_unit.rs
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
use crate::*;
|
||||
|
||||
/// Defines an enum that parametrically transforms its content
|
||||
/// along either the X axis, the Y axis, or both.
|
||||
macro_rules! transform_xy_unit {
|
||||
(|$self:ident : $Enum:ident, $to:ident|$area:expr) => {
|
||||
pub enum $Enum<E: Engine, T: Content<E>> {
|
||||
X(E::Unit, T), Y(E::Unit, T), XY(E::Unit, E::Unit, T),
|
||||
}
|
||||
impl<E: Engine, T: Content<E>> $Enum<E, T> {
|
||||
pub fn x (x: E::Unit, item: T) -> Self { Self::X(x, item) }
|
||||
pub fn y (y: E::Unit, item: T) -> Self { Self::Y(y, item) }
|
||||
pub fn xy (x: E::Unit, y: E::Unit, item: T) -> Self { Self::XY(x, y, item) }
|
||||
pub fn dx (&self) -> E::Unit {
|
||||
match self {
|
||||
Self::X(x, _) => *x,
|
||||
Self::Y(_, _) => E::Unit::zero(),
|
||||
Self::XY(x, _, _) => *x,
|
||||
}
|
||||
}
|
||||
pub fn dy (&self) -> E::Unit {
|
||||
match self {
|
||||
Self::X(_, _) => E::Unit::zero(),
|
||||
Self::Y(y, _) => *y,
|
||||
Self::XY(_, y, _) => *y,
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<E: Engine, T: Content<E>> Content<E> for $Enum<E, T> {
|
||||
fn content (&self) -> impl Content<E> {
|
||||
Some(match self {
|
||||
Self::X(_, content) => content,
|
||||
Self::Y(_, content) => content,
|
||||
Self::XY(_, _, content) => content,
|
||||
})
|
||||
}
|
||||
fn area (&$self, $to: E::Area) -> E::Area {
|
||||
$area.into()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
transform_xy_unit!(|self: Fixed, area|{
|
||||
let area = self.content().area(area);
|
||||
match self {
|
||||
Self::X(fw, _) => [area.x(), area.y(), *fw, area.h()],
|
||||
Self::Y(fh, _) => [area.x(), area.y(), area.w(), *fh],
|
||||
Self::XY(fw, fh, _) => [area.x(), area.y(), *fw, *fh], // tagn
|
||||
}
|
||||
});
|
||||
|
||||
transform_xy_unit!(|self: Shrink, area|{
|
||||
let area = self.content().area(area);
|
||||
[area.x(), area.y(), area.w().minus(self.dx()), area.h().minus(self.dy())]
|
||||
});
|
||||
|
||||
transform_xy_unit!(|self: Expand, area|{
|
||||
let area = self.content().area(area);
|
||||
[area.x(), area.y(), area.w() + self.dx(), area.h() + self.dy()]
|
||||
});
|
||||
|
||||
transform_xy_unit!(|self: Min, area|{
|
||||
let area = self.content().area(area);
|
||||
match self {
|
||||
Self::X(mw, _) => [area.x(), area.y(), area.w().max(*mw), area.h()],
|
||||
Self::Y(mh, _) => [area.x(), area.y(), area.w(), 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|{
|
||||
let area = self.content().area(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)],
|
||||
}});
|
||||
|
||||
transform_xy_unit!(|self: Push, area|{
|
||||
let area = self.content().area(area);
|
||||
[area.x() + self.dx(), area.y() + self.dy(), area.w(), area.h()]
|
||||
});
|
||||
|
||||
transform_xy_unit!(|self: Pull, area|{
|
||||
let area = self.content().area(area);
|
||||
[area.x().minus(self.dx()), area.y().minus(self.dy()), area.w(), area.h()]
|
||||
});
|
||||
|
||||
transform_xy_unit!(|self: Margin, area|{
|
||||
let area = self.content().area(area);
|
||||
let dx = self.dx();
|
||||
let dy = self.dy();
|
||||
[area.x().minus(dx), area.y().minus(dy), area.w() + dy + dy, area.h() + dy + dy]
|
||||
});
|
||||
|
||||
transform_xy_unit!(|self: Padding, area|{
|
||||
let area = self.content().area(area);
|
||||
let dx = self.dx();
|
||||
let dy = self.dy();
|
||||
[area.x() + dx, area.y() + dy, area.w().minus(dy + dy), area.h().minus(dy + dy), ]
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue