Files
iris/src/widget/position/stack.rs
T
iris-ai f6242aa33c Take a span child's length from its hint, and ask it once in its slot
A share child was drawn in the measuring room and again in its slot, and
one record holding two questions made every local change under it defer
to the span. Where a rule or a hint gives the length along the span, the
first ask answers nothing the rule does not, so the child is asked once,
in its slot; the widgets that always report the whole of their box now
say so. A hint with a fraction resolves against the frame and pins it.

The dump rig prints every cold layout so a change to it shows in a diff.
2026-09-19 02:09:41 -04:00

106 lines
3.3 KiB
Rust

use std::marker::PhantomData;
use crate::prelude::*;
pub struct Stack {
pub children: Vec<StrongWidget>,
pub size: StackSize,
}
impl Widget for Stack {
fn draw(&mut self, painter: &mut Painter) -> Size {
let sizing = match self.size {
StackSize::Default => None,
StackSize::Child(i) => Some(i),
};
// Whichever child sizes the stack is given the stack's whole box --
// the stack is the length that child asked for, so placing that
// answer inside the box it decided would apply it twice.
let size = match sizing.and_then(|i| self.children.get(i).map(|c| (i, c))) {
// On the layer that child ends up on, so the ask below is a reuse
// rather than a second drawing of it somewhere else: a retained
// drawing belongs to the layer it was made on.
Some((i, child)) => {
painter.child_layer_at(i);
painter
.widget_at(child, [None; 2], [Place::Fill(Part::All); 2])
.size()
}
None => Size::LEFTOVER,
};
// Every other child gets the box the sizing child decided: the
// stack is that length, so that is the box they are asked in, and a
// fraction under them is a fraction of it. A share leaves the axis
// to whoever gave the stack its box. Where a child sits in a box
// bigger than itself is its own business.
let place = [Axis::X, Axis::Y].map(|axis| {
let len = size.axis(axis);
match len.leftover == Weight::ZERO {
true => Place::Fill(Part::Sized(Len::from_parts(len.rel, len.px))),
false => Place::Within(Part::All),
}
});
for (i, child) in self.children.iter().enumerate() {
if sizing == Some(i) {
continue;
}
painter.child_layer_at(i);
painter.widget_at(child, [None; 2], place);
}
size
}
/// Without a sizing child a stack is whatever box it is given, which it
/// can say without drawing anything.
fn size_hint(&self, _: Axis) -> Option<LayoutLen> {
match self.size {
StackSize::Default => Some(LayoutLen::LEFTOVER),
StackSize::Child(_) => None,
}
}
}
#[derive(Default, Debug)]
pub enum StackSize {
#[default]
Default,
Child(usize),
}
pub struct StackBuilder<State, const LEN: usize, Wa: WidgetArrLike<State, LEN, Tag>, Tag> {
pub children: Wa,
pub size: StackSize,
_pd: PhantomData<(State, Tag)>,
}
impl<Rsc, const LEN: usize, Wa: WidgetArrLike<Rsc, LEN, Tag>, Tag> WidgetFnTrait<Rsc>
for StackBuilder<Rsc, LEN, Wa, Tag>
{
type Widget = Stack;
#[track_caller]
fn run(self, rsc: &mut Rsc) -> Self::Widget {
Stack {
children: self.children.add(rsc).arr.into_iter().collect(),
size: self.size,
}
}
}
impl<State, const LEN: usize, Wa: WidgetArrLike<State, LEN, Tag>, Tag>
StackBuilder<State, LEN, Wa, Tag>
{
pub fn new(children: Wa) -> Self {
Self {
children,
size: StackSize::default(),
_pd: PhantomData,
}
}
pub fn size(mut self, size: StackSize) -> Self {
self.size = size;
self
}
}