Files
iris/core/src/orientation/len.rs
T
iris-aiandClaude Opus 5 55df32a33c Say layout's operations by name, and index a pair by its axis
Four rounds over the same idea: an expression that needed a comment to say
what it computed wanted to be a named operation.

The placement description is built by chaining off the value that says it.
`UiSpan::within_desc`/`shifted_desc` and `Len::as_desc` replace the
`PlaceDescAxis::` constructors, `PlaceDescAxis::axis` lifts one axis into a
pair with the whole box across it, and `PlaceDesc::per_axis` covers the case
where the two axes differ. `beside` is dropped: `from_axis` already said it.

Seven module-level functions become methods on the value each took first --
`Widgets::declared_lens`, `LayoutLen::fills`, `PlaceDesc::placement` and
`::rel_base_and_region`, `Size::within_box`, `UiRegion::at_origin` and
`::as_translation`.

`UiSpan::place` is the aligned-placement rule, which was written out three
times; `LayoutLen::without_leftover` is the sibling `apply_leftover` never
had, at six sites; `is_px` and `is_only_leftover` name field comparisons the
surrounding comments had to translate; `Holds::covers` was interval
containment spelled out by hand. A span's `shared` loses the two arguments
that did not vary across its loop.

`LayoutHolds` was four two-element arrays where every other pair here is a
struct of two per-axis values, so nothing it did could be written once.
It becomes `AxisHolds` on `x` and `y`, and `and`, `covers` and `contains`
lose their loops.

Every pair gets `Index<Axis>`/`IndexMut<Axis>` through one macro, and the
eighteen `axis`/`axis_mut` methods go. `const_index` keeps the accessors
usable in const context.

Cold layout is unchanged: `layout_dump` over 400 depth-5 trees is identical
to 58ce74d byte for byte, across all 34,492 boxes.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-19 20:56:59 -04:00

249 lines
6.5 KiB
Rust

use super::*;
use crate::util::impl_axis_index;
use crate::{Px, PxVec2, Rel, UiNum, Weight, util::impl_op};
#[derive(Debug, Default, Clone, Copy, PartialEq)]
pub struct Size {
pub x: LayoutLen,
pub y: LayoutLen,
}
/// What a widget asks for along one axis: a [`Len`] -- pixels and a fraction
/// of the box it is given -- plus a share of whatever is left over once
/// everything fixed has been taken. The parts add up rather than choosing
/// between one another.
///
/// Only a container dividing its room can answer a share, so a length nobody
/// divides is a `Len`: a position, a padding, a cap, anything already
/// resolved.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct LayoutLen {
pub px: Px,
pub rel: Rel,
pub leftover: Weight,
}
impl<N: UiNum> From<N> for LayoutLen {
fn from(value: N) -> Self {
LayoutLen::px(value.to_f32())
}
}
impl<Nx: UiNum, Ny: UiNum> From<(Nx, Ny)> for Size {
fn from((x, y): (Nx, Ny)) -> Self {
Self {
x: x.into(),
y: y.into(),
}
}
}
/// A length with no share in it is a length a container does not have to
/// divide, which is one it can always give.
impl From<Len> for LayoutLen {
fn from(len: Len) -> Self {
Self {
px: len.px,
rel: len.rel,
leftover: Weight::ZERO,
}
}
}
impl From<LayoutLen> for Size {
fn from(value: LayoutLen) -> Self {
Self { x: value, y: value }
}
}
impl Size {
pub const ZERO: Self = Self {
x: LayoutLen::ZERO,
y: LayoutLen::ZERO,
};
pub const LEFTOVER: Self = Self {
x: LayoutLen::LEFTOVER,
y: LayoutLen::LEFTOVER,
};
/// From something measured outside layout -- a texture, a shaped line --
/// which is where a size in floats comes from.
pub fn px(v: Vec2) -> Self {
Self::from_px(PxVec2::from_f32(v))
}
pub const fn from_px(v: PxVec2) -> Self {
Self {
x: LayoutLen {
px: v.x,
..LayoutLen::ZERO
},
y: LayoutLen {
px: v.y,
..LayoutLen::ZERO
},
}
}
pub fn rel(v: Vec2) -> Self {
Self {
x: LayoutLen::rel(v.x),
y: LayoutLen::rel(v.y),
}
}
pub fn leftover(v: Vec2) -> Self {
Self {
x: LayoutLen::leftover(v.x),
y: LayoutLen::leftover(v.y),
}
}
pub fn to_uivec2(self) -> UiVec2 {
UiVec2 {
x: self.x.apply_leftover(),
y: self.y.apply_leftover(),
}
}
pub fn from_axis(axis: Axis, aligned: LayoutLen, ortho: LayoutLen) -> Self {
match axis {
Axis::X => Self {
x: aligned,
y: ortho,
},
Axis::Y => Self {
x: ortho,
y: aligned,
},
}
}
}
impl LayoutLen {
pub const ZERO: Self = Self {
px: Px::ZERO,
rel: Rel::ZERO,
leftover: Weight::ZERO,
};
pub const LEFTOVER: Self = Self {
px: Px::ZERO,
rel: Rel::ZERO,
leftover: Weight::ONE,
};
/// The whole of what is left over counts as the whole box, which is what
/// a length means to something that is not dividing a box between
/// siblings -- a scroll asking how long its content is.
pub fn apply_leftover(&self) -> Len {
let share = match self.leftover > Weight::ZERO {
true => Rel::ONE,
false => Rel::ZERO,
};
Len::from_parts(self.rel.add(share), self.px)
}
/// Only pixels: the same number of them whatever box it lands in, and
/// whatever anyone else in the row asks for. A length that is any part
/// of a box or of what is left over is not one.
pub fn is_px(self) -> bool {
self.rel == Rel::ZERO && self.leftover == Weight::ZERO
}
/// Nothing but a claim on what is left over, so there is no length here
/// at all where nothing is.
pub fn is_only_leftover(self) -> bool {
self.leftover > Weight::ZERO && self.without_leftover() == Len::ZERO
}
/// What this takes whatever is left over: the reading of a length for
/// anyone not dividing a box between siblings, where a share is a claim
/// on someone else's room rather than a length of its own.
/// [`Self::apply_leftover`] is the opposite reading of the same value.
pub const fn without_leftover(self) -> Len {
Len::from_parts(self.rel, self.px)
}
/// This length, given as a part of a box `len` long, as a part of the
/// box `len` is itself a part of. The share is untouched: it is a claim
/// on whoever divides the room, not a fraction of anything.
pub const fn within_len(self, len: Len) -> Self {
let part = self.without_leftover().within_len(len);
Self {
px: part.px,
rel: part.rel,
leftover: self.leftover,
}
}
pub fn px(px: impl UiNum) -> Self {
Self {
px: Px::from_num(px),
..Self::ZERO
}
}
pub fn rel(rel: impl UiNum) -> Self {
Self {
rel: Rel::from_num(rel),
..Self::ZERO
}
}
pub fn leftover(ratio: impl UiNum) -> Self {
Self {
leftover: Weight::from_num(ratio),
..Self::ZERO
}
}
}
pub mod len_fns {
use super::*;
pub fn px(px: impl UiNum) -> LayoutLen {
LayoutLen::px(px)
}
pub fn rel(rel: impl UiNum) -> LayoutLen {
LayoutLen::rel(rel)
}
pub fn leftover(ratio: impl UiNum) -> LayoutLen {
LayoutLen::leftover(ratio)
}
}
impl_op!(same LayoutLen Add add; px rel leftover);
impl_op!(same LayoutLen Sub sub; px rel leftover);
impl_op!(same Size Add add; x y);
impl_op!(same Size Sub sub; x y);
impl Default for LayoutLen {
fn default() -> Self {
Self::leftover(1.0)
}
}
impl std::fmt::Display for Size {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "({}, {})", self.x, self.y)
}
}
impl std::fmt::Display for LayoutLen {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if self.px != Px::ZERO {
write!(f, "{} px;", self.px)?;
}
if self.rel != Rel::ZERO {
write!(f, "{} rel;", self.rel)?;
}
if self.leftover != Weight::ZERO {
write!(f, "{} leftover;", self.leftover)?;
}
Ok(())
}
}
impl_axis_index!(Size => LayoutLen);