WIP: a widget's region stays put and its placement moves in it
The protocol split: `region` is the box a parent gives a widget -- what a fraction it declares or reports is a fraction of, and the coordinates every region it writes composes within -- and it is the same box on the ask that measures and the ask that places. `placement` is what of that region the drawing takes, chosen by the parent per axis or by the widget's own answer and alignment. That is what stops a fraction being resolved twice: the placing ask no longer hands the widget its own answer as its box, so nothing under it re-resolves against a box that came from its own report. `reports_of` and `decided` are gone, folded into the two regions; `box_of` is gone; `declared_box` becomes `ask_box`, which gives a rule the region's length and takes the position from the placement. 84 of 92 suite tests pass. Five text and region-node cases still diverge warm against cold, and three count a second widget draw where a span's measuring ask and its placing ask give different placements.
This commit is contained in:
1 parent
e44dea34b4
commit
5fcace1bfa
11 files changed
+346
-206
No files matched your search
+156
-85
@@ -2,8 +2,8 @@
|
||||
use crate::layout_diagnostics::{self as diag, Counter};
|
||||
use crate::{
|
||||
Axis, Holds, LayoutLen, Len, Px, PxVec2, RegionAlign, RenderedText, Size, StrongWidget,
|
||||
TextAttrs, TextBuffer, TextData, TextureHandle, UiRegion, UiRenderState, UiRsc, UiVec2, Weight,
|
||||
WidgetId, Widgets,
|
||||
TextAttrs, TextBuffer, TextData, TextureHandle, UiRegion, UiRenderState, UiRsc, UiSpan, UiVec2,
|
||||
Weight, WidgetId, Widgets,
|
||||
render::{
|
||||
GlyphPrimitive, Mask, MaskIdx, MoveIdx, Primitive, PrimitiveHandle, PrimitiveInst,
|
||||
PrimitiveKind, TexturePrimitive,
|
||||
@@ -17,8 +17,20 @@ pub struct Painter<'a> {
|
||||
pub(super) state: &'a mut UiRenderState,
|
||||
pub(super) rsc: &'a mut dyn UiRsc,
|
||||
|
||||
/// This widget's box, in the coordinates of `move_idx`.
|
||||
/// The box its parent gave it, in the coordinates of `move_idx`: what a
|
||||
/// fraction of this widget's area is a fraction of, and what every region
|
||||
/// it writes composes within. The same box on the ask that measures and
|
||||
/// the ask that places, which is what keeps a fraction under it from
|
||||
/// being resolved twice.
|
||||
pub(super) region: UiRegion,
|
||||
/// Where this widget's drawing sits inside that box, in the box's own
|
||||
/// coordinates: `FULL` while its answer is not yet known, and the box
|
||||
/// its answer or its parent chose once one of them has.
|
||||
pub(super) placement: UiRegion,
|
||||
/// Whether this draw read its placement, which makes the drawing one
|
||||
/// that holds for that placement alone -- the way reading a length in
|
||||
/// pixels makes it hold for that length.
|
||||
pub(super) reads_placement: bool,
|
||||
/// That box in pixels, which its children's are a length of: threaded
|
||||
/// down from the box this widget was given rather than composed back up
|
||||
/// the chain, so every length in layout is one multiply from its
|
||||
@@ -88,28 +100,37 @@ impl<'a> Painter<'a> {
|
||||
self.primitives.push(h);
|
||||
}
|
||||
|
||||
/// Writes a primitive to be rendered
|
||||
/// Writes a primitive over the whole of this widget's own box.
|
||||
pub fn primitive(&mut self, primitive: impl PrimitiveLike) {
|
||||
let at = self.placed();
|
||||
let primitive = primitive.into_primitive(self);
|
||||
self.primitive_at(primitive, self.region)
|
||||
self.primitive_at(primitive, at)
|
||||
}
|
||||
|
||||
/// Writes one somewhere in this widget's region. A widget that wants its
|
||||
/// own box rather than the region it was given composes through
|
||||
/// [`Self::placement`] first.
|
||||
pub fn primitive_within(&mut self, primitive: impl PrimitiveLike, region: UiRegion) {
|
||||
let primitive = primitive.into_primitive(self);
|
||||
self.primitive_at(primitive, region.within(&self.region));
|
||||
}
|
||||
|
||||
/// `region` is in this widget's own region, as every region it writes is.
|
||||
pub fn set_mask(&mut self, region: UiRegion) {
|
||||
assert!(self.mask == MaskIdx::NONE);
|
||||
self.mask = self.rsc.ui_mut().masks.push(Mask {
|
||||
region,
|
||||
region: region.within(&self.region),
|
||||
move_idx: self.move_idx,
|
||||
});
|
||||
}
|
||||
|
||||
/// Draws a widget within this widget's region.
|
||||
/// Draws a widget in the whole of this widget's own box: it gets the
|
||||
/// same region -- the same area for its fractions to be of -- and is put
|
||||
/// where this widget was put. What a container that is only a wrapper
|
||||
/// around one child wants, since its box is the child's.
|
||||
pub fn widget<'s, W: ?Sized>(&'s mut self, id: &'s StrongWidget<W>) -> DrawResult<'s, 'a, W> {
|
||||
self.widget_within(id, UiRegion::FULL)
|
||||
let own = self.placement();
|
||||
self.widget_at(id, UiRegion::FULL, [Some(own.x), Some(own.y)])
|
||||
}
|
||||
|
||||
/// What a widget's rules declare its lengths to be, which whoever draws
|
||||
@@ -129,51 +150,41 @@ impl<'a> Painter<'a> {
|
||||
}
|
||||
|
||||
/// Draws a widget somewhere within this one. `region` is in this widget's
|
||||
/// own coordinates, and the child's declared lengths are still to be
|
||||
/// taken from it. Where the child's drawing sits inside what it is given
|
||||
/// is the child's alignment, applied where the child is drawn, so a
|
||||
/// container positions a child either by handing it a box of exactly its
|
||||
/// length or by leaving it room and letting its alignment decide.
|
||||
/// own region, and is the child's own region: what its declared lengths
|
||||
/// and its report are fractions of. Where its drawing sits inside that is
|
||||
/// its own answer placed by its alignment.
|
||||
pub fn widget_within<'s, W: ?Sized>(
|
||||
&'s mut self,
|
||||
id: &'s StrongWidget<W>,
|
||||
region: UiRegion,
|
||||
) -> DrawResult<'s, 'a, W> {
|
||||
self.widget_at(id, region, region.size(), [false; 2])
|
||||
self.widget_at(id, region, [None; 2])
|
||||
}
|
||||
|
||||
/// Draws a widget in `region`, saying what the answer means.
|
||||
/// Draws a widget in `region`, saying where in it the drawing goes.
|
||||
///
|
||||
/// `reports_of` is what a fraction the child reports is a fraction of, as
|
||||
/// lengths of this widget's own box. It is the box the child was given
|
||||
/// wherever that box is the child's whole area -- a pad's inset, a stack
|
||||
/// child, a scroll's content -- and a span passes its own extent along
|
||||
/// the row instead: it offers each child the room left from its cursor,
|
||||
/// because a text has to wrap at the width actually there, while
|
||||
/// `rel(0.5)` still means half the span wherever the child sits in it.
|
||||
/// `region` is the child's own area: what a fraction it declares or
|
||||
/// reports is a fraction of, and the coordinates the regions it writes
|
||||
/// compose within. It is the same box on the ask that measures and the
|
||||
/// ask that places, which is what stops a fraction under it being
|
||||
/// resolved twice.
|
||||
///
|
||||
/// A `decided` axis is one where this box was chosen from the widget's
|
||||
/// own answer. On those the answer is not placed inside the box again: it
|
||||
/// already is the box, and a fraction taken of it a second time would
|
||||
/// shrink it twice. A container uses that where it hands back exactly
|
||||
/// what a child asked for -- a span placing a child at the length it
|
||||
/// reported, a scroll giving its content the content's own length.
|
||||
/// `placement` is what of that region the child's drawing takes, per
|
||||
/// axis, wherever this widget is choosing. `None` leaves the axis to the
|
||||
/// child's own answer and alignment, which is what
|
||||
/// [`Self::widget_within`] passes. A span passes the whole row as the
|
||||
/// region, so `rel(0.5)` is half the row wherever the child sits in it,
|
||||
/// and places the child by passing the slot along its axis.
|
||||
pub fn widget_at<'s, W: ?Sized>(
|
||||
&'s mut self,
|
||||
id: &'s StrongWidget<W>,
|
||||
region: UiRegion,
|
||||
reports_of: UiVec2,
|
||||
decided: [bool; 2],
|
||||
placement: [Option<UiSpan>; 2],
|
||||
) -> DrawResult<'s, 'a, W> {
|
||||
let region_node = self.rsc.widgets().is_region_node(id.id());
|
||||
let declared = self.declared_lens(id);
|
||||
let align = self.rsc.widgets().alignment(id.id());
|
||||
// Composing `FULL` through a box is not quite the identity in f32,
|
||||
// so a child with nothing declared keeps the box it would have had.
|
||||
let local = match declared.iter().any(Option::is_some) {
|
||||
true => declared_box(region, declared, align),
|
||||
false => region,
|
||||
};
|
||||
let (local, placement) = ask_box(region, declared, align, placement);
|
||||
let within = match local == UiRegion::FULL {
|
||||
true => self.region,
|
||||
false => local.within(&self.region),
|
||||
@@ -219,7 +230,7 @@ impl<'a> Painter<'a> {
|
||||
offer_len,
|
||||
px,
|
||||
offered_px,
|
||||
decided,
|
||||
placement,
|
||||
},
|
||||
None,
|
||||
self.rsc,
|
||||
@@ -235,7 +246,7 @@ impl<'a> Painter<'a> {
|
||||
DrawResult {
|
||||
child: id,
|
||||
painter: self,
|
||||
size: in_parent_frame(size, reports_of, declared),
|
||||
size: in_parent_frame(size, local.size(), declared),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -267,20 +278,18 @@ impl<'a> Painter<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
/// A child's length in the box it is about to be offered, if it can be
|
||||
/// had without drawing it: from its hint, or from a drawing it already
|
||||
/// has that holds for that box. `reports_of` is what a fraction in the
|
||||
/// answer is a fraction of, as it is for [`Self::widget_at`].
|
||||
/// A child's length in the region it is about to be offered, if it can
|
||||
/// be had without drawing it: from its hint, or from a drawing it already
|
||||
/// has that holds for that box.
|
||||
pub fn known_len<W: ?Sized>(
|
||||
&mut self,
|
||||
child: &StrongWidget<W>,
|
||||
axis: Axis,
|
||||
region: UiRegion,
|
||||
reports_of: UiVec2,
|
||||
) -> Option<LayoutLen> {
|
||||
let declared = self.declared_lens(child);
|
||||
let align = self.rsc.widgets().alignment(child.id());
|
||||
let local = declared_box(region, declared, align);
|
||||
let (local, _) = ask_box(region, declared, align, [None; 2]);
|
||||
let first_ask = self.offer(child.id());
|
||||
if first_ask && let Some(active) = self.state.active.get_mut(&child.id()) {
|
||||
active.offer_len = local.size();
|
||||
@@ -302,7 +311,7 @@ impl<'a> Painter<'a> {
|
||||
for (axis, under) in AXES.into_iter().zip(self.under.iter_mut()) {
|
||||
*under = under.and(holds[axis as usize].through(local.axis(axis).len()));
|
||||
}
|
||||
Some(in_parent_frame(size, reports_of, declared).axis(axis))
|
||||
Some(in_parent_frame(size, local.size(), declared).axis(axis))
|
||||
}
|
||||
|
||||
/// Whether this is the first box a child is asked about in during a draw
|
||||
@@ -334,8 +343,10 @@ impl<'a> Painter<'a> {
|
||||
ui.text.render(buffer, attrs, width)
|
||||
}
|
||||
|
||||
/// `origin` is in this widget's own region, as every region it writes is.
|
||||
// TODO: merge the text methods into the primitive ones.
|
||||
pub fn glyphs(&mut self, text: &RenderedText, origin: UiRegion) {
|
||||
let origin = origin.within(&self.region);
|
||||
let kind = self.rsc.ui_mut().primitives.kind::<GlyphPrimitive>();
|
||||
for glyph in text.glyphs.iter() {
|
||||
let mut region = origin;
|
||||
@@ -362,12 +373,35 @@ impl<'a> Painter<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
/// This widget's box, in the coordinates its own primitives are written
|
||||
/// in -- so a region composed `within` it may be drawn directly.
|
||||
/// The box this widget's parent gave it, in the coordinates its own
|
||||
/// primitives are written in -- so a region composed `within` it may be
|
||||
/// drawn directly. Its own box is [`Self::placement`] of this one.
|
||||
pub fn region(&self) -> UiRegion {
|
||||
self.region
|
||||
}
|
||||
|
||||
/// Where this widget's drawing goes inside the box it was given, in that
|
||||
/// box's coordinates: what its own answer took of it, or what its parent
|
||||
/// chose for it. `FULL` on the ask that measures, since nothing has been
|
||||
/// placed yet.
|
||||
///
|
||||
/// Reading it is what says the drawing depends on it, so a widget that
|
||||
/// positions its own content reads it and is drawn again once its box is
|
||||
/// known, and one that fills whatever it is given never is.
|
||||
pub fn placement(&mut self) -> UiRegion {
|
||||
self.reads_placement = true;
|
||||
self.placement
|
||||
}
|
||||
|
||||
/// This widget's own box in the coordinates its primitives are written
|
||||
/// in: its placement composed through the region it was given.
|
||||
fn placed(&mut self) -> UiRegion {
|
||||
match self.placement() == UiRegion::FULL {
|
||||
true => self.region,
|
||||
false => self.placement.within(&self.region),
|
||||
}
|
||||
}
|
||||
|
||||
/// Where this widget sits in a box longer than the length it takes. A
|
||||
/// widget that positions its own content reads it to place that content
|
||||
/// the way the box around it would have placed the widget.
|
||||
@@ -401,20 +435,47 @@ impl<'a> Painter<'a> {
|
||||
placed_box(UiRegion::FULL, lens, RegionAlign::NEAR)
|
||||
}
|
||||
|
||||
/// This widget's box in pixels. Reading it makes the drawing one that
|
||||
/// holds for this box only, until `holds` says how far it goes.
|
||||
/// This widget's own box in pixels. Reading it makes the drawing one
|
||||
/// that holds for this box only, until `holds` says how far it goes.
|
||||
pub fn px_size(&mut self) -> PxVec2 {
|
||||
for (own, len) in self.own.iter_mut().zip([self.px.x, self.px.y]) {
|
||||
if *own == Holds::ANY {
|
||||
*own = Holds::at(len);
|
||||
}
|
||||
}
|
||||
self.px
|
||||
PxVec2::new(self.px_len(Axis::X), self.px_len(Axis::Y))
|
||||
}
|
||||
|
||||
/// One axis of this widget's box in pixels. Prefer this to
|
||||
/// One axis of this widget's own box in pixels. Prefer this to
|
||||
/// [`Self::px_size`] when the other axis cannot affect the drawing.
|
||||
pub fn px_len(&mut self, axis: Axis) -> Px {
|
||||
let part = self.placement().axis(axis).len();
|
||||
let len = part.to_px(self.px.axis(axis));
|
||||
let own = &mut self.own[axis as usize];
|
||||
if *own == Holds::ANY {
|
||||
*own = Holds::at(len).through(part);
|
||||
}
|
||||
len
|
||||
}
|
||||
|
||||
/// The lengths of this widget's own box on `axis` that what it is drawing
|
||||
/// holds for -- the same primitives, in the same fractions and offsets
|
||||
/// of the box, and the same reported size. A widget that read its length
|
||||
/// in pixels holds for that one alone until it says otherwise.
|
||||
pub fn holds(&mut self, axis: Axis, holds: impl Into<Holds>) {
|
||||
let part = self.placement().axis(axis).len();
|
||||
let holds = holds.into();
|
||||
debug_assert!(
|
||||
holds.contains(part.to_px(self.px.axis(axis))),
|
||||
"'{}' ({:?}) says its drawing holds for lengths that leave out its own box",
|
||||
self.label(),
|
||||
self.id
|
||||
);
|
||||
// Kept as a range of the region's lengths, which is the one variable
|
||||
// every range here is about: its own box is a part of that box, and
|
||||
// `through` is the exact preimage of taking the part.
|
||||
self.own[axis as usize] = holds.through(part);
|
||||
}
|
||||
|
||||
/// One axis of the box this widget's parent gave it, in pixels -- what a
|
||||
/// fraction of its area resolves against, and so what a container divides
|
||||
/// among its children. Its own box is a part of this one.
|
||||
pub fn region_px_len(&mut self, axis: Axis) -> Px {
|
||||
let len = self.px.axis(axis);
|
||||
let own = &mut self.own[axis as usize];
|
||||
if *own == Holds::ANY {
|
||||
@@ -423,15 +484,14 @@ impl<'a> Painter<'a> {
|
||||
len
|
||||
}
|
||||
|
||||
/// The lengths of this widget's box on `axis` that what it is drawing
|
||||
/// holds for -- the same primitives, in the same fractions and offsets
|
||||
/// of the box, and the same reported size. A widget that read its
|
||||
/// length in pixels holds for that one alone until it says otherwise.
|
||||
pub fn holds(&mut self, axis: Axis, holds: impl Into<Holds>) {
|
||||
/// [`Self::holds`] stated about the region rather than about this
|
||||
/// widget's own box, for a container whose drawing turns on the box it
|
||||
/// was given rather than on the part of it it took.
|
||||
pub fn region_holds(&mut self, axis: Axis, holds: impl Into<Holds>) {
|
||||
let holds = holds.into();
|
||||
debug_assert!(
|
||||
holds.contains(self.px.axis(axis)),
|
||||
"'{}' ({:?}) says its drawing holds for lengths that leave out its own box",
|
||||
"'{}' ({:?}) says its drawing holds for lengths that leave out its region",
|
||||
self.label(),
|
||||
self.id
|
||||
);
|
||||
@@ -521,19 +581,16 @@ impl PrimitiveLike for &TextureHandle {
|
||||
}
|
||||
}
|
||||
|
||||
/// A child's answer as lengths of the parent's own box. A widget reports a
|
||||
/// fraction, and `reports_of` is the length that fraction is of: the box the
|
||||
/// child was given wherever that is the child's whole area, and the parent's
|
||||
/// own extent wherever the box is a positional remainder, as a span's is
|
||||
/// after an earlier child. Pixels come through untouched either way, being
|
||||
/// that many pixels wherever they end up. A declared axis is already the
|
||||
/// parent's: it resolved the rule in its own box, and the rule is what the
|
||||
/// report says.
|
||||
fn in_parent_frame(size: Size, reports_of: UiVec2, declared: [Option<LayoutLen>; 2]) -> Size {
|
||||
/// A child's answer as lengths of the parent's own region. A widget reports
|
||||
/// a fraction of its own region, and `of` is that region as a length of this
|
||||
/// one. Pixels come through untouched, being that many pixels wherever they
|
||||
/// end up. A declared axis is already the parent's: it resolved the rule in
|
||||
/// its own region, and the rule is what the report says.
|
||||
fn in_parent_frame(size: Size, of: UiVec2, declared: [Option<LayoutLen>; 2]) -> Size {
|
||||
let mut size = size;
|
||||
for (axis, declared) in AXES.into_iter().zip(declared) {
|
||||
if declared.is_none() {
|
||||
*size.axis_mut(axis) = size.axis(axis).within_len(reports_of.axis(axis));
|
||||
*size.axis_mut(axis) = size.axis(axis).within_len(of.axis(axis));
|
||||
}
|
||||
}
|
||||
size
|
||||
@@ -562,9 +619,9 @@ pub(crate) fn declared_lens(widgets: &Widgets, id: WidgetId) -> [Option<LayoutLe
|
||||
/// Whether what a widget reported along an axis is the whole of the box it
|
||||
/// is in rather than a part to be placed inside it. A share fills, because a
|
||||
/// share is a length only to whoever divides one, and whoever did is the one
|
||||
/// that handed down this box. A declared axis does too: `declared_box`
|
||||
/// already placed it, in the parent's box, and the rule's length is what the
|
||||
/// widget reports there. And an axis the parent decided from the answer is
|
||||
/// that handed down this box. A declared axis does too: the rule already gave
|
||||
/// the region its length, and the rule's length is what the widget reports
|
||||
/// there. And an axis the parent decided from the answer is
|
||||
/// the answer already.
|
||||
pub(crate) fn fills(reported: LayoutLen, declared: Option<LayoutLen>, decided: bool) -> bool {
|
||||
reported.leftover != Weight::ZERO || declared.is_some() || decided
|
||||
@@ -612,21 +669,35 @@ pub(crate) fn placed_box(region: UiRegion, lens: UiVec2, align: RegionAlign) ->
|
||||
placed
|
||||
}
|
||||
|
||||
/// Takes a widget's declared lengths in the box `region` is given in, since a
|
||||
/// fraction of a length means a fraction of that one, and puts what is left
|
||||
/// over on the side its alignment says. A caller that already reserved the
|
||||
/// space hands back the same length, so this is the identity for it.
|
||||
pub(crate) fn declared_box(
|
||||
/// A child's own region and where in it its drawing goes, from the box it is
|
||||
/// offered, the lengths its rules declare, and what its parent chose.
|
||||
///
|
||||
/// A rule gives the region its length outright -- that is what makes a rule
|
||||
/// win, and it is why a widget under one never learns of it -- and the region
|
||||
/// then sits where its parent placed it, or where its alignment says if its
|
||||
/// parent left the axis open. With no rule the region is the whole of what
|
||||
/// was offered, since that is the area a fraction under it is a fraction of,
|
||||
/// and what the parent chose is where in it the drawing goes. So the two
|
||||
/// coordinate spaces are the same one wherever a placement survives.
|
||||
pub(crate) fn ask_box(
|
||||
mut region: UiRegion,
|
||||
declared: [Option<LayoutLen>; 2],
|
||||
align: RegionAlign,
|
||||
) -> UiRegion {
|
||||
for (axis, len) in AXES.into_iter().zip(declared) {
|
||||
let Some(len) = len else { continue };
|
||||
placement: [Option<UiSpan>; 2],
|
||||
) -> (UiRegion, [Option<UiSpan>; 2]) {
|
||||
let mut placed = [None; 2];
|
||||
for (axis, (len, chosen)) in AXES.into_iter().zip(declared.into_iter().zip(placement)) {
|
||||
let Some(len) = len else {
|
||||
placed[axis as usize] = chosen;
|
||||
continue;
|
||||
};
|
||||
let span = region.axis_mut(axis);
|
||||
let len = Len::from_parts(len.rel, len.px);
|
||||
span.start += (span.len() - len).scale(align.axis(axis).rel());
|
||||
span.start = match chosen {
|
||||
Some(chosen) => chosen.start,
|
||||
None => span.start + (span.len() - len).scale(align.axis(axis).rel()),
|
||||
};
|
||||
span.end = span.start + len;
|
||||
}
|
||||
region
|
||||
(region, placed)
|
||||
}
|
||||
Reference in new issue
Block a user