change Layout to output minimum size (breaks Align::Center?)

This commit is contained in:
🪞👃🪞 2024-09-15 01:31:26 +03:00
parent 0737769232
commit 1a0e06dc66
13 changed files with 232 additions and 162 deletions

View file

@ -1,10 +1,20 @@
use crate::*;
// TODO: return impl Point and impl Size instead of [N;x]
// to disambiguate between usage of 2-"tuple"s
pub trait Size<N: Number> {
fn x (&self) -> N;
fn y (&self) -> N;
fn w (&self) -> N { self.x() }
fn h (&self) -> N { self.y() }
#[inline] fn w (&self) -> N { self.x() }
#[inline] fn h (&self) -> N { self.y() }
#[inline] fn expect_min (&self, w: N, h: N) -> Usually<&Self> {
if self.w() < w || self.h() < h {
Err(format!("min {w}x{h}").into())
} else {
Ok(self)
}
}
}
impl<N: Number> Size<N> for (N, N) {
@ -24,39 +34,39 @@ pub trait Area<N: Number>: Copy {
fn h (&self) -> N;
fn x2 (&self) -> N { self.x() + self.w() }
fn y2 (&self) -> N { self.y() + self.h() }
fn wh (&self) -> [N;2] {
#[inline] fn wh (&self) -> [N;2] {
[self.w(), self.h()]
}
fn xywh (&self) -> [N;4] {
#[inline] fn xywh (&self) -> [N;4] {
[self.x(), self.y(), self.w(), self.h()]
}
fn lrtb (&self) -> [N;4] {
#[inline] fn lrtb (&self) -> [N;4] {
[self.x(), self.x2(), self.y(), self.y2()]
}
fn expect_min (&self, w: N, h: N) -> Usually<&Self> {
#[inline] fn expect_min (&self, w: N, h: N) -> Usually<&Self> {
if self.w() < w || self.h() < h {
Err(format!("min {w}x{h}").into())
} else {
Ok(self)
}
}
fn clip (&self, wh: impl Size<N>) -> [N;4] {
#[inline] fn clip (&self, wh: impl Size<N>) -> [N;4] {
[self.x(), self.y(), wh.w(), wh.h()]
}
}
impl<N: Number> Area<N> for (N, N, N, N) {
fn x (&self) -> N { self.0 }
fn y (&self) -> N { self.1 }
fn w (&self) -> N { self.2 }
fn h (&self) -> N { self.3 }
#[inline] fn x (&self) -> N { self.0 }
#[inline] fn y (&self) -> N { self.1 }
#[inline] fn w (&self) -> N { self.2 }
#[inline] fn h (&self) -> N { self.3 }
}
impl<N: Number> Area<N> for [N;4] {
fn x (&self) -> N { self[0] }
fn y (&self) -> N { self[1] }
fn w (&self) -> N { self[2] }
fn h (&self) -> N { self[3] }
#[inline] fn x (&self) -> N { self[0] }
#[inline] fn y (&self) -> N { self[1] }
#[inline] fn w (&self) -> N { self[2] }
#[inline] fn h (&self) -> N { self[3] }
}
macro_rules! impl_axis_common { ($A:ident $T:ty) => {