Compare commits
13
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
950960cccd | ||
|
|
1c80051d57 | ||
|
|
a92c6acdbf | ||
|
|
c8beca5753 | ||
|
|
4bd8607968 | ||
|
|
ffd79f32d3 | ||
|
|
0e0d4af326 | ||
|
|
ea6dbae0dc | ||
|
|
32542d0c0b | ||
|
|
5b7800264d | ||
|
|
5f16617511 | ||
|
|
45a717695b | ||
|
|
d21a21524f |
No files matched your search
@@ -109,6 +109,18 @@ impl<const SHIFT: u32> Fixed<SHIFT> {
|
||||
})
|
||||
}
|
||||
|
||||
/// The first step at or above `v`, where [`Self::from_f32`] takes the
|
||||
/// nearest one and is below it half the time. For a bound that has to
|
||||
/// admit the value it came from: a measurement rounded down is a bound
|
||||
/// that leaves out the thing it was measured from.
|
||||
pub const fn ceil_from_f32(v: f32) -> Self {
|
||||
let nearest = Self::from_f32(v);
|
||||
match nearest.to_f32() < v {
|
||||
true => nearest.next_up(),
|
||||
false => nearest,
|
||||
}
|
||||
}
|
||||
|
||||
/// From a number as it is written in source -- `16`, `1.5` -- which is
|
||||
/// the other place a value enters the grid.
|
||||
pub fn from_num(v: impl UiNum) -> Self {
|
||||
@@ -372,6 +384,12 @@ impl<const SHIFT: u32> FixedVec2<SHIFT> {
|
||||
Self::new(Fixed::from_f32(v.x), Fixed::from_f32(v.y))
|
||||
}
|
||||
|
||||
/// The first step at or above each part, for a measurement reported as a
|
||||
/// box: what it occupies is not less than what was measured.
|
||||
pub fn ceil_from_f32(v: Vec2) -> Self {
|
||||
Self::new(Fixed::ceil_from_f32(v.x), Fixed::ceil_from_f32(v.y))
|
||||
}
|
||||
|
||||
pub fn to_f32(self) -> Vec2 {
|
||||
Vec2::new(self.x.to_f32(), self.y.to_f32())
|
||||
}
|
||||
@@ -488,6 +506,25 @@ mod tests {
|
||||
assert_eq!(Px::from_int(100) / Rel::from_f32(0.5), Px::from_int(200));
|
||||
}
|
||||
|
||||
/// The bound a greedy line break needs: the width it was measured at is
|
||||
/// not on the grid, and the narrowest box the break still holds for is
|
||||
/// the step at or above it, never the one below.
|
||||
#[test]
|
||||
fn a_ceiling_never_lands_below_the_number_it_came_from() {
|
||||
let step = 1.0 / (1 << PX_SHIFT) as f32;
|
||||
for n in 0..64 {
|
||||
let v = 189.0 + n as f32 * step / 3.0;
|
||||
let up = Px::ceil_from_f32(v);
|
||||
assert!(up.to_f32() >= v, "{up:?} is below {v}");
|
||||
assert!(
|
||||
up.to_f32() - v < step,
|
||||
"{up:?} is more than a step above {v}"
|
||||
);
|
||||
}
|
||||
// An exact step is its own ceiling.
|
||||
assert_eq!(Px::ceil_from_f32(189.5), Px::from_f32(189.5));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_number_from_outside_is_clamped_to_the_grid() {
|
||||
assert_eq!(Px::from_f32(1e12), Px::MAX);
|
||||
|
||||
@@ -84,8 +84,7 @@ pub struct RegionAlign {
|
||||
}
|
||||
|
||||
impl RegionAlign {
|
||||
/// Both axes at the near edge. What a container passes as an override for
|
||||
/// a child it is going to position itself.
|
||||
/// Both axes at the near edge: the start of a box in its own orientation.
|
||||
pub const NEAR: Self = Self {
|
||||
x: AxisAlign::NEG,
|
||||
y: AxisAlign::NEG,
|
||||
|
||||
@@ -125,6 +125,13 @@ impl Size {
|
||||
Axis::Y => self.y,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn axis_mut(&mut self, axis: Axis) -> &mut LayoutLen {
|
||||
match axis {
|
||||
Axis::X => &mut self.x,
|
||||
Axis::Y => &mut self.y,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl LayoutLen {
|
||||
@@ -151,6 +158,18 @@ impl LayoutLen {
|
||||
Len::from_parts(self.rel.add(share), 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 = Len::from_parts(self.rel, self.px).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),
|
||||
|
||||
@@ -219,7 +219,7 @@ impl Len {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn within_len(&self, len: Len) -> Self {
|
||||
pub const fn within_len(&self, len: Len) -> Self {
|
||||
self.within(&UiSpan {
|
||||
start: Len::ZERO,
|
||||
end: len,
|
||||
|
||||
@@ -107,13 +107,6 @@ impl Default for TextAttrs {
|
||||
}
|
||||
}
|
||||
|
||||
/// How far below the longest line a width may fall and still be answered by
|
||||
/// the break in hand. A parent that offers a child the length it reported
|
||||
/// composes that length back through the box chain, so the two differ in the
|
||||
/// last bits -- and at exactly the longest line, that decides whether a line
|
||||
/// fits. Sub-pixel, so no break it admits is one a reader could see.
|
||||
const BREAK_EPSILON_PX: f32 = 0.05;
|
||||
|
||||
/// Keeps text and its corresponding layout from getting out of sync.
|
||||
pub struct TextBuffer {
|
||||
text: String,
|
||||
@@ -200,15 +193,19 @@ impl TextBuffer {
|
||||
// A greedy break at one width is the same break at every width down
|
||||
// to the longest line it produced: each line still fits, and none can
|
||||
// take a word that would not fit in the wider box. So the layout in
|
||||
// hand already answers, and re-breaking would only be a chance to
|
||||
// disagree with itself -- which is what happens when a parent offers
|
||||
// a child the length that child just reported, and the two land
|
||||
// either side of a float.
|
||||
// hand already answers, and re-breaking would only be work.
|
||||
//
|
||||
// At the longest line exactly, with no margin below it. A narrower
|
||||
// width really does break differently, so answering one from the
|
||||
// break in hand is how a warm tree keeps lines a cold tree would
|
||||
// never produce. The margin was here because a text reports the
|
||||
// width it used and a parent hands that back; the report is the step
|
||||
// at or above its longest line now, so what comes back fits.
|
||||
if let Some(key) = &self.layout_key
|
||||
&& key.attrs == *attrs
|
||||
&& let (Some(broke_at), Some(want)) = (key.max_width, width)
|
||||
&& want <= broke_at
|
||||
&& want + BREAK_EPSILON_PX >= self.layout.width()
|
||||
&& want >= self.layout.width()
|
||||
{
|
||||
#[cfg(feature = "layout-diagnostics")]
|
||||
diag::bump(Counter::TextShapeHits);
|
||||
|
||||
@@ -35,6 +35,10 @@ struct MoveOffset {
|
||||
// belongs to the pixel above it. Flooring the product instead drops a pixel
|
||||
// wherever a fraction divides a window exactly: a fifth of 1920 comes out of
|
||||
// `REL_STEP` as 383.99998, and five tabs each lose their last column.
|
||||
//
|
||||
// Taken over the whole coordinate, fraction and pixels summed, since a floor
|
||||
// does not distribute over a sum: floored apart, a half of one and a half of
|
||||
// the other lose the pixel the two together make.
|
||||
fn snap_floor(v: vec2<f32>) -> vec2<f32> {
|
||||
return floor(v + PX_STEP * 0.5);
|
||||
}
|
||||
@@ -147,8 +151,8 @@ fn vs_main(
|
||||
let bot_right_rel = vec2(r.x.end.rel, r.y.end.rel);
|
||||
let bot_right_px = vec2(r.x.end.px, r.y.end.px);
|
||||
|
||||
let top_left = snap_floor(top_left_rel * window.dim) + snap_floor(top_left_px);
|
||||
let bot_right = snap_floor(bot_right_rel * window.dim) + snap_floor(bot_right_px);
|
||||
let top_left = snap_floor(top_left_rel * window.dim + top_left_px);
|
||||
let bot_right = snap_floor(bot_right_rel * window.dim + bot_right_px);
|
||||
let size = bot_right - top_left;
|
||||
|
||||
let uv = vec2<f32>(
|
||||
@@ -179,8 +183,8 @@ fn masked(in: VertexOutput, color: vec4<f32>) -> vec4<f32> {
|
||||
let br = vec2(m.x.end.rel, m.y.end.rel);
|
||||
let br_px = vec2(m.x.end.px, m.y.end.px);
|
||||
|
||||
let top_left = snap_floor(tl * window.dim) + snap_floor(tl_px);
|
||||
let bot_right = snap_floor(br * window.dim) + snap_floor(br_px);
|
||||
let top_left = snap_floor(tl * window.dim + tl_px);
|
||||
let bot_right = snap_floor(br * window.dim + br_px);
|
||||
let pos = in.clip_position.xy;
|
||||
if pos.x < top_left.x || pos.x > bot_right.x || pos.y < top_left.y || pos.y > bot_right.y {
|
||||
return color * 0.0;
|
||||
|
||||
+27
-14
@@ -1,6 +1,6 @@
|
||||
use crate::{
|
||||
Holds, LayerId, LayoutLen, MaskIdx, MoveIdx, PrimitiveHandle, RegionAlign, Size, TextureHandle,
|
||||
UiRegion, WidgetId,
|
||||
UiRegion, UiVec2, WidgetId,
|
||||
};
|
||||
|
||||
/// What is kept of a widget its parent has asked about. `drawn` says whether
|
||||
@@ -11,11 +11,20 @@ pub struct ActiveData {
|
||||
pub id: WidgetId,
|
||||
/// The box its drawing is in, in `parent_move`'s coordinates.
|
||||
pub region: UiRegion,
|
||||
/// The box its parent first asked about it in, as a part of the box the
|
||||
/// parent was itself asked in. Any later box it was given was decided
|
||||
/// knowing its answer, so this is where a question about it is asked
|
||||
/// again -- and it is kept relative so that it follows the parent's.
|
||||
pub offer: UiRegion,
|
||||
/// The box its parent gave it, in the same coordinates: what it was
|
||||
/// asked about, before its own answer placed its drawing inside it.
|
||||
/// `region` is that placement, and a local redraw asks here.
|
||||
pub given: UiRegion,
|
||||
/// The same box as lengths of its parent's box, which is the one route
|
||||
/// to a box in pixels: a draw threads these down a level at a time, and
|
||||
/// [`crate::UiRenderState::redraw`] takes the same steps back up.
|
||||
pub given_len: UiVec2,
|
||||
/// The lengths of the box its parent first asked about it in, as
|
||||
/// lengths of the box the parent was itself offered. Any later box it
|
||||
/// was given was decided knowing its answer, so this is the question
|
||||
/// asked again -- and a chain of fractions has no frame in it, which is
|
||||
/// why a region node between two widgets cannot break it.
|
||||
pub offer_len: UiVec2,
|
||||
/// What it answered there: the size and what that held for.
|
||||
pub answer: (Size, [Holds; 2]),
|
||||
/// What the widget said it used of its box, the last time it drew.
|
||||
@@ -41,18 +50,22 @@ pub struct ActiveData {
|
||||
/// A change to one moves a box this widget cannot fix by drawing again,
|
||||
/// and comparing them is what says so.
|
||||
pub declared: [Option<LayoutLen>; 2],
|
||||
/// The alignment its parent asked it with. A local redraw repeats that
|
||||
/// question, including an override chosen by a container.
|
||||
pub align: RegionAlign,
|
||||
/// Whether that alignment was the parent's override rather than the
|
||||
/// widget's own property.
|
||||
pub align_override: bool,
|
||||
/// Its own alignment when it was last drawn. A change to the property is
|
||||
/// found against this even when its parent overrode the alignment.
|
||||
/// The axes along which its parent chose its box from its own answer,
|
||||
/// so a local redraw asks the question its parent asked.
|
||||
pub decided: [bool; 2],
|
||||
/// Its alignment when it was last drawn, which a change to the property
|
||||
/// is found against.
|
||||
pub own_align: RegionAlign,
|
||||
/// The movable region whose coordinates `region` uses.
|
||||
pub parent_move: MoveIdx,
|
||||
/// The mask its drawing is clipped to: one it set itself, or the one it
|
||||
/// inherited from whoever drew it.
|
||||
pub mask: MaskIdx,
|
||||
/// That inherited one. The two differ exactly where the widget set a
|
||||
/// mask of its own, which is the one it owns and the one a move rewrites
|
||||
/// -- and the one a redraw of it must not be handed back, since setting
|
||||
/// a mask asserts there is none.
|
||||
pub parent_mask: MaskIdx,
|
||||
pub layer: LayerId,
|
||||
}
|
||||
|
||||
|
||||
+43
-54
@@ -1,4 +1,4 @@
|
||||
use crate::{Len, Px, REL_SHIFT, Rel, fixed::div_toward, fixed::narrow};
|
||||
use crate::{Len, Px, REL_SHIFT, fixed::div_toward, fixed::narrow};
|
||||
use std::ops::RangeInclusive;
|
||||
|
||||
/// The lengths of a box, in pixels, that one drawing of a widget holds for:
|
||||
@@ -10,9 +10,9 @@ use std::ops::RangeInclusive;
|
||||
///
|
||||
/// The ends are lengths on the grid rather than floats with a tolerance
|
||||
/// around them: a box offered back at the length a widget reported comes back
|
||||
/// as the same number, so a range means what it says. What widening there is
|
||||
/// belongs to [`Self::through`], which has a rounding to undo, and is derived
|
||||
/// from that rounding rather than chosen.
|
||||
/// as the same number, so a range means what it says. The one place a range
|
||||
/// is wider than the length it came from is [`Self::through`], and what it is
|
||||
/// wider by is the floor that inverting a fraction undoes.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub struct Holds {
|
||||
pub lo: Px,
|
||||
@@ -41,51 +41,29 @@ impl Holds {
|
||||
}
|
||||
|
||||
/// What a box has to be for a part of it, `len` of the box long, to stay
|
||||
/// in this range. A part with no relative extent is a fixed length: it
|
||||
/// was drawn at that length and any box keeps it there.
|
||||
/// in this range: the exact preimage of `px + floor(rel * box)`, which is
|
||||
/// the one way a box in pixels is reached. A part with no relative extent
|
||||
/// is a fixed length -- it was drawn at that length and any box keeps it
|
||||
/// there.
|
||||
///
|
||||
/// The way in is `px + rel * box` taken to the nearest step, so a part
|
||||
/// of exactly `lo` came from anything within half a step of it and the
|
||||
/// answer is an interval even where this range is one length. Inverting
|
||||
/// the length alone instead gives a point that need not even contain the
|
||||
/// box the part was drawn in, which is a range excluding the drawing it
|
||||
/// was made for.
|
||||
/// The answer is an interval even where this range is a single length,
|
||||
/// because the multiply on the way in drops to the step below and many
|
||||
/// boxes therefore give one length. That is a floor rather than an
|
||||
/// allowance: inverting it is two divisions and nothing else, and the
|
||||
/// whole of a box maps back to itself.
|
||||
pub const fn through(self, len: Len) -> Self {
|
||||
let rel = len.rel.raw() as i64;
|
||||
if rel == 0 {
|
||||
return Self::ANY;
|
||||
}
|
||||
// In half steps, and no more than the arithmetic needs: too wide a
|
||||
// range admits reusing a drawing where it does not hold, and too
|
||||
// narrow a one leaves out the box a drawing was made in, which the
|
||||
// `Holds` assertion in `draw_at` catches. `ROUTES` is at that floor
|
||||
// -- two half steps fires it -- and tightening both ends moved not
|
||||
// one of the rig's work counters, so the slack is not buying reuse.
|
||||
//
|
||||
// `ROUTES` covers a box composed down the chain against the same box
|
||||
// measured against the window: two routes to one number, each
|
||||
// rounding where the other does not. `way_in` covers the multiply
|
||||
// this inverts, which drops a step and only ever downward, so it
|
||||
// belongs at the top of the range alone -- and the whole of a box has
|
||||
// no multiply in it, however many pixels were added to it, since
|
||||
// multiplying by one is exact and taking the pixels off again is too.
|
||||
// Allowing for it there anyway compounded, a step a level down a
|
||||
// chain of widgets each taking the whole of its parent.
|
||||
//
|
||||
// Shifted by half of what a `Rel` counts in, to divide by the
|
||||
// fraction: exact until the division takes it back to the grid.
|
||||
const ROUTES: i64 = 3;
|
||||
let px = len.px.raw() as i64;
|
||||
let half_rel = REL_SHIFT - 1;
|
||||
let way_in = match rel == Rel::ONE.raw() as i64 {
|
||||
true => 0,
|
||||
false => 2,
|
||||
};
|
||||
let lo = ((self.lo.raw() as i64 - px) * 2 - ROUTES) << half_rel;
|
||||
let hi = ((self.hi.raw() as i64 - px) * 2 + ROUTES + way_in) << half_rel;
|
||||
// Dividing by a negative turns the ends around, so which end each
|
||||
// bound comes from is decided before dividing rather than by taking
|
||||
// the min and max of four divisions.
|
||||
// `floor(rel * box) >= lo - px` is `rel * box >= (lo - px) << REL`, and
|
||||
// `floor(rel * box) <= hi - px` is `rel * box < (hi - px + 1) << REL`.
|
||||
let lo = (self.lo.raw() as i64 - px) << REL_SHIFT;
|
||||
let hi = (((self.hi.raw() as i64 - px) + 1) << REL_SHIFT) - 1;
|
||||
// Dividing by a negative fraction turns the ends around, so which
|
||||
// bound each comes from is decided before dividing rather than by
|
||||
// taking the min and max of four divisions.
|
||||
match rel > 0 {
|
||||
true => Self::raws(div_toward(lo, rel, true), div_toward(hi, rel, false)),
|
||||
false => Self::raws(div_toward(hi, rel, true), div_toward(lo, rel, false)),
|
||||
@@ -136,26 +114,37 @@ mod tests {
|
||||
}
|
||||
|
||||
/// A widget handed the whole of its parent's box, with or without pixels
|
||||
/// taken off it, brings no multiply of its own: only the two routes to
|
||||
/// the same length are left to allow for, and not a rounding that did
|
||||
/// not happen. Widening for it as well grew the interval a level at a
|
||||
/// time down a chain of them. Three half steps come back as one whole
|
||||
/// one, since dividing by a whole box is dividing by one.
|
||||
/// taken off it, has no fraction to invert: multiplying by one is exact
|
||||
/// and taking the pixels off again is too, so the box maps back to
|
||||
/// itself. Allowing for anything here compounded a step a level down a
|
||||
/// chain of widgets each taking the whole of its parent.
|
||||
#[test]
|
||||
fn the_whole_of_a_box_widens_by_the_routes_alone() {
|
||||
fn the_whole_of_a_box_maps_back_to_itself() {
|
||||
let at = Px::from_int(956);
|
||||
let one_step = |len: Px| Holds {
|
||||
lo: len - Px::STEP,
|
||||
hi: len + Px::STEP,
|
||||
};
|
||||
assert_eq!(Holds::at(at).through(Len::FULL), one_step(at));
|
||||
assert_eq!(Holds::at(at).through(Len::FULL), Holds::at(at));
|
||||
let less_eight = Len::from_parts(Rel::ONE, Px::from_int(-8));
|
||||
assert_eq!(
|
||||
Holds::at(at).through(less_eight),
|
||||
one_step(at + Px::from_int(8))
|
||||
Holds::at(at + Px::from_int(8))
|
||||
);
|
||||
}
|
||||
|
||||
/// The range is the exact preimage at both ends, so a box one step
|
||||
/// outside it really does give a length outside this range. What a wider
|
||||
/// range costs is a drawing reused where it does not hold.
|
||||
#[test]
|
||||
fn a_box_one_step_outside_the_range_is_outside_it() {
|
||||
let part = Len::from_parts(Rel::from_f32(1.0 / 3.0), Px::from_int(-146));
|
||||
let at = Px::from_int(300);
|
||||
let holds = Holds::at(at).through(part);
|
||||
for inside in [holds.lo, holds.hi] {
|
||||
assert_eq!(part.to_px(inside), at, "{inside:?} left out of {holds:?}");
|
||||
}
|
||||
for outside in [holds.lo.next_down(), holds.hi.next_up()] {
|
||||
assert_ne!(part.to_px(outside), at, "{outside:?} admitted by {holds:?}");
|
||||
}
|
||||
}
|
||||
|
||||
/// A truncating multiply only ever drops, so the step it needs allowing
|
||||
/// for on the way in belongs at the top of the range and not the bottom.
|
||||
#[test]
|
||||
|
||||
+12
-6
@@ -68,17 +68,24 @@ impl Moves {
|
||||
}
|
||||
}
|
||||
|
||||
/// Composes a region held in `idx`'s coordinates down the chain, which is
|
||||
/// the same walk the vertex shader does.
|
||||
/// The same walk the vertex shader does, in the same `Len` the shader is
|
||||
/// handed, for asking where a drawing will actually land -- hit testing,
|
||||
/// and nothing layout decides on. Layout threads its lengths down the
|
||||
/// draw instead, so no box it compares is composed back up this chain.
|
||||
pub fn resolve(&self, idx: MoveIdx, local: UiRegion) -> UiRegion {
|
||||
let mut region = local;
|
||||
self.walk(idx, |entry| region = region.within(entry));
|
||||
region
|
||||
}
|
||||
|
||||
fn walk(&self, idx: MoveIdx, mut step: impl FnMut(&UiRegion)) {
|
||||
let mut at = idx;
|
||||
for _ in 0..CHAIN_LIMIT {
|
||||
if at == MoveIdx::NONE {
|
||||
return region;
|
||||
return;
|
||||
}
|
||||
let entry = self.arena[at.idx()];
|
||||
region = region.within(&entry.region);
|
||||
let entry = &self.arena[at.idx()];
|
||||
step(&entry.region);
|
||||
at = entry.parent;
|
||||
}
|
||||
debug_assert!(
|
||||
@@ -86,7 +93,6 @@ impl Moves {
|
||||
"a move chain longer than {CHAIN_LIMIT} resolves to the wrong place, \
|
||||
and the shader stops at the same depth"
|
||||
);
|
||||
region
|
||||
}
|
||||
|
||||
/// How many slots a region in `idx` is composed through, which is what
|
||||
|
||||
+93
-60
@@ -1,7 +1,7 @@
|
||||
#[cfg(feature = "layout-diagnostics")]
|
||||
use crate::layout_diagnostics::{self as diag, Counter};
|
||||
use crate::{
|
||||
Axis, Holds, LayoutLen, Len, Px, PxVec2, RegionAlign, Rel, RenderedText, Size, StrongWidget,
|
||||
Axis, Holds, LayoutLen, Len, Px, PxVec2, RegionAlign, RenderedText, Size, StrongWidget,
|
||||
TextAttrs, TextBuffer, TextData, TextureHandle, UiRegion, UiRenderState, UiRsc, UiVec2, Weight,
|
||||
WidgetId, Widgets,
|
||||
render::{
|
||||
@@ -19,6 +19,11 @@ pub struct Painter<'a> {
|
||||
|
||||
/// This widget's box, in the coordinates of `move_idx`.
|
||||
pub(super) region: UiRegion,
|
||||
/// 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
|
||||
/// parent's and [`Holds::through`] inverts exactly that.
|
||||
pub(super) px: PxVec2,
|
||||
pub(super) mask: MaskIdx,
|
||||
pub(super) textures: Vec<TextureHandle>,
|
||||
pub(super) primitives: Vec<PrimitiveHandle>,
|
||||
@@ -26,10 +31,12 @@ pub struct Painter<'a> {
|
||||
/// The children asked about so far, so the first box each was asked in
|
||||
/// is the one recorded as its offer.
|
||||
pub(super) offered: Vec<WidgetId>,
|
||||
/// The box this widget was first asked about in, in pixels.
|
||||
/// The lengths of the box this widget was first asked about in, in
|
||||
/// pixels. Its children's offers are a fraction of it.
|
||||
pub(super) offered_px: PxVec2,
|
||||
/// Whether this draw is in that box, which makes the questions it asks
|
||||
/// the ones a cold layout asks and their answers the ones to keep.
|
||||
/// Whether this draw is in a box of those lengths, which makes the
|
||||
/// questions it asks the ones a cold layout asks and their answers the
|
||||
/// ones to keep.
|
||||
pub(super) at_offer: bool,
|
||||
/// The children whose size this widget read while drawing.
|
||||
pub(super) size_deps: Vec<WidgetId>,
|
||||
@@ -132,30 +139,25 @@ impl<'a> Painter<'a> {
|
||||
id: &'s StrongWidget<W>,
|
||||
region: UiRegion,
|
||||
) -> DrawResult<'s, 'a, W> {
|
||||
self.widget_at(id, region, None)
|
||||
self.widget_at(id, region, [false; 2])
|
||||
}
|
||||
|
||||
/// Draws a widget with an alignment chosen by its container rather than
|
||||
/// the widget's property. Containers use this when the box they hand down
|
||||
/// already expresses the size they report around the child.
|
||||
pub fn widget_aligned<'s, W: ?Sized>(
|
||||
/// Draws a widget in a box this widget chose from the widget's own
|
||||
/// answer along the `decided` axes. On those the answer is not placed
|
||||
/// inside the box again: it already is the box, and a fraction the
|
||||
/// widget reported, taken of this box a second time, would shrink it
|
||||
/// twice. A container uses this 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.
|
||||
pub fn widget_at<'s, W: ?Sized>(
|
||||
&'s mut self,
|
||||
id: &'s StrongWidget<W>,
|
||||
region: UiRegion,
|
||||
align: RegionAlign,
|
||||
) -> DrawResult<'s, 'a, W> {
|
||||
self.widget_at(id, region, Some(align))
|
||||
}
|
||||
|
||||
fn widget_at<'s, W: ?Sized>(
|
||||
&'s mut self,
|
||||
id: &'s StrongWidget<W>,
|
||||
region: UiRegion,
|
||||
align_override: Option<RegionAlign>,
|
||||
decided: [bool; 2],
|
||||
) -> DrawResult<'s, 'a, W> {
|
||||
let region_node = self.rsc.widgets().is_region_node(id.id());
|
||||
let declared = self.declared_lens(id);
|
||||
let align = align_override.unwrap_or_else(|| self.rsc.widgets().alignment(id.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) {
|
||||
@@ -176,11 +178,20 @@ impl<'a> Painter<'a> {
|
||||
self.children.push(id.id());
|
||||
}
|
||||
let first_ask = self.offer(id.id());
|
||||
let offer = match first_ask {
|
||||
true => local,
|
||||
false => self.state.active.get(&id.id()).map_or(local, |a| a.offer),
|
||||
let given_len = local.size();
|
||||
let offer_len = match first_ask {
|
||||
true => given_len,
|
||||
false => self
|
||||
.state
|
||||
.active
|
||||
.get(&id.id())
|
||||
.map_or(given_len, |a| a.offer_len),
|
||||
};
|
||||
let answers_offer = self.at_offer && local == offer;
|
||||
let px = given_len.to_px(self.px);
|
||||
let offered_px = offer_len.to_px(self.offered_px);
|
||||
// Whether this ask is the child's offer question, which is a question
|
||||
// about lengths: the same lengths somewhere else is the same question.
|
||||
let answers_offer = self.at_offer && px == offered_px;
|
||||
// The answer and what it holds for, both about the box asked in. The
|
||||
// child's record may say something else once its drawing has been
|
||||
// placed: a drawing made again in its placed box holds for that box.
|
||||
@@ -194,9 +205,11 @@ impl<'a> Painter<'a> {
|
||||
parent_move: self.move_idx,
|
||||
region_node,
|
||||
mask: self.mask,
|
||||
offer,
|
||||
offered_px: self.px_within_offer(offer),
|
||||
align: align_override,
|
||||
given_len,
|
||||
offer_len,
|
||||
px,
|
||||
offered_px,
|
||||
decided,
|
||||
},
|
||||
None,
|
||||
self.rsc,
|
||||
@@ -209,6 +222,14 @@ 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()));
|
||||
}
|
||||
// The answer as it was given. A fraction in it is a fraction of this
|
||||
// widget's box, which is the same thing a rule beside the child
|
||||
// means and the same thing for every box this widget hands out: a
|
||||
// span offers each child the room left from its cursor, because a
|
||||
// text has to wrap at the width actually there, and `rel(0.5)` is
|
||||
// still half the span. Padding is outside what it pads for the same
|
||||
// reason -- inset the fraction and a child's `rel` would mean the
|
||||
// inner box while its `px` meant the outer one.
|
||||
DrawResult {
|
||||
child: id,
|
||||
painter: self,
|
||||
@@ -256,15 +277,14 @@ impl<'a> Painter<'a> {
|
||||
let declared = self.declared_lens(child);
|
||||
let align = self.rsc.widgets().alignment(child.id());
|
||||
let local = declared_box(region, declared, align);
|
||||
let within = local.within(&self.region);
|
||||
let first_ask = self.offer(child.id());
|
||||
if first_ask && let Some(active) = self.state.active.get_mut(&child.id()) {
|
||||
active.offer = local;
|
||||
active.offer_len = local.size();
|
||||
}
|
||||
if let Some(hint) = self.size_hint(child, axis) {
|
||||
return Some(hint);
|
||||
}
|
||||
let px = self.state.px_of(self.move_idx, within);
|
||||
let px = local.size().to_px(self.px);
|
||||
let (size, holds) =
|
||||
self.state
|
||||
.retained_size(child.id(), px, self.move_idx, self.rsc.widgets())?;
|
||||
@@ -292,15 +312,6 @@ impl<'a> Painter<'a> {
|
||||
true
|
||||
}
|
||||
|
||||
/// The pixel size of a part of the box this widget was asked in.
|
||||
fn px_within_offer(&self, local: UiRegion) -> PxVec2 {
|
||||
let size = local.size();
|
||||
PxVec2::new(
|
||||
size.x.to_px(self.offered_px.x),
|
||||
size.y.to_px(self.offered_px.y),
|
||||
)
|
||||
}
|
||||
|
||||
fn depend_on<W: ?Sized>(&mut self, child: &StrongWidget<W>) {
|
||||
if !self.size_deps.contains(&child.id()) {
|
||||
self.size_deps.push(child.id());
|
||||
@@ -382,25 +393,25 @@ impl<'a> Painter<'a> {
|
||||
/// near edge. A container that reports one child's size gives every child
|
||||
/// this, so what it draws is inside what it says it occupies.
|
||||
pub fn box_of(&self, size: Size) -> UiRegion {
|
||||
placed_box(UiRegion::FULL, size, RegionAlign::NEAR, [None; 2])
|
||||
let lens = placed_lens(size, [None; 2], [false; 2]);
|
||||
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.
|
||||
pub fn px_size(&mut self) -> PxVec2 {
|
||||
let px = self.state.px_of(self.move_idx, self.region);
|
||||
for (own, len) in self.own.iter_mut().zip([px.x, px.y]) {
|
||||
for (own, len) in self.own.iter_mut().zip([self.px.x, self.px.y]) {
|
||||
if *own == Holds::ANY {
|
||||
*own = Holds::at(len);
|
||||
}
|
||||
}
|
||||
px
|
||||
self.px
|
||||
}
|
||||
|
||||
/// One axis of this widget's 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 len = self.state.px_of(self.move_idx, self.region).axis(axis);
|
||||
let len = self.px.axis(axis);
|
||||
let own = &mut self.own[axis as usize];
|
||||
if *own == Holds::ANY {
|
||||
*own = Holds::at(len);
|
||||
@@ -415,7 +426,7 @@ impl<'a> Painter<'a> {
|
||||
pub fn holds(&mut self, axis: Axis, holds: impl Into<Holds>) {
|
||||
let holds = holds.into();
|
||||
debug_assert!(
|
||||
holds.contains(self.state.px_of(self.move_idx, self.region).axis(axis)),
|
||||
holds.contains(self.px.axis(axis)),
|
||||
"'{}' ({:?}) says its drawing holds for lengths that leave out its own box",
|
||||
self.label(),
|
||||
self.id
|
||||
@@ -526,31 +537,53 @@ pub(crate) fn declared_lens(widgets: &Widgets, id: WidgetId) -> [Option<LayoutLe
|
||||
})
|
||||
}
|
||||
|
||||
/// The box a drawing occupies: the size the widget reported, on the side of
|
||||
/// the box it was asked in that its alignment says. An axis reported as 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 is
|
||||
/// left alone too: `declared_box` already placed it, in the parent's box,
|
||||
/// and the rule's length is what the widget reports there.
|
||||
/// 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
|
||||
/// the answer already.
|
||||
pub(crate) fn fills(reported: LayoutLen, declared: Option<LayoutLen>, decided: bool) -> bool {
|
||||
reported.leftover != Weight::ZERO || declared.is_some() || decided
|
||||
}
|
||||
|
||||
/// What of the box it was given a widget's drawing occupies, as lengths of
|
||||
/// that box: the size it reported wherever that is a part to be placed, and
|
||||
/// the whole of the box wherever the answer fills it.
|
||||
///
|
||||
/// A reported fraction is a fraction of the box the widget drew in, where a
|
||||
/// declared one is a fraction of the box its parent handed down -- a span
|
||||
/// reporting `rel(1.0)` means all of what it was given, whatever that was a
|
||||
/// fraction of. So this scales by the box rather than composing into it.
|
||||
pub(crate) fn placed_box(
|
||||
region: UiRegion,
|
||||
/// fraction of. So this is a length of the box rather than a length composed
|
||||
/// into it, and a box in pixels is this step from the given box's pixels.
|
||||
pub(crate) fn placed_lens(
|
||||
size: Size,
|
||||
align: RegionAlign,
|
||||
declared: [Option<LayoutLen>; 2],
|
||||
) -> UiRegion {
|
||||
let mut placed = region;
|
||||
for (axis, declared) in AXES.into_iter().zip(declared) {
|
||||
decided: [bool; 2],
|
||||
) -> UiVec2 {
|
||||
let mut lens = UiVec2::FULL_SIZE;
|
||||
for (axis, (declared, decided)) in AXES.into_iter().zip(declared.into_iter().zip(decided)) {
|
||||
let reported = size.axis(axis);
|
||||
if reported.leftover != Weight::ZERO || declared.is_some() {
|
||||
if !fills(reported, declared, decided) {
|
||||
*lens.axis_mut(axis) = Len::from_parts(reported.rel, reported.px);
|
||||
}
|
||||
}
|
||||
lens
|
||||
}
|
||||
|
||||
/// Where that drawing sits: those lengths taken of the box the widget was
|
||||
/// asked in, on the side of it that the widget's alignment says.
|
||||
pub(crate) fn placed_box(region: UiRegion, lens: UiVec2, align: RegionAlign) -> UiRegion {
|
||||
let mut placed = region;
|
||||
for axis in AXES {
|
||||
// The whole of the box is already where it sits, and the arithmetic
|
||||
// below is the identity for it.
|
||||
if lens.axis(axis) == Len::FULL {
|
||||
continue;
|
||||
}
|
||||
let span = placed.axis_mut(axis);
|
||||
let len = span.len().scale(reported.rel) + Len::from_parts(Rel::ZERO, reported.px);
|
||||
let len = lens.axis(axis).within_len(span.len());
|
||||
span.start += (span.len() - len).scale(align.axis(axis).rel());
|
||||
span.end = span.start + len;
|
||||
}
|
||||
|
||||
+244
-245
@@ -1,9 +1,9 @@
|
||||
#[cfg(feature = "layout-diagnostics")]
|
||||
use crate::layout_diagnostics::{self as diag, Counter, ReuseOutcome, TimerKind};
|
||||
use crate::ui::painter::{declared_box, declared_lens, placed_box};
|
||||
use crate::ui::painter::{declared_box, declared_lens, placed_box, placed_lens};
|
||||
use crate::{
|
||||
ActiveData, Axis, DrawLayers, Holds, IdLike, LayoutLen, Len, MaskIdx, MoveIdx, Moves, Painter,
|
||||
PixelRegion, Px, PxVec2, RegionAlign, Rel, Size, StrongWidget, UiRegion, UiRsc, UiSpan, Weight,
|
||||
PixelRegion, Px, PxVec2, Rel, Size, StrongWidget, UiRegion, UiRsc, UiSpan, UiVec2, Weight,
|
||||
WidgetId, Widgets,
|
||||
util::{HashMap, Vec2},
|
||||
};
|
||||
@@ -20,13 +20,20 @@ pub(super) struct DrawInfo {
|
||||
pub parent_move: MoveIdx,
|
||||
pub region_node: bool,
|
||||
pub mask: MaskIdx,
|
||||
/// The box it was first asked about in, as a part of its parent's, and
|
||||
/// that box in pixels.
|
||||
pub offer: UiRegion,
|
||||
/// The box its parent gave it, as lengths of the parent's own box, and
|
||||
/// the lengths of the box it was first asked about in the same form.
|
||||
/// Both describe the box the *parent* stated, so the second, placing ask
|
||||
/// carries them unchanged while its own region is the placement inside.
|
||||
pub given_len: UiVec2,
|
||||
pub offer_len: UiVec2,
|
||||
/// This ask's box in pixels, and the offer's: one multiply from the
|
||||
/// parent's own, which is where every pixel length in layout comes from.
|
||||
pub px: PxVec2,
|
||||
pub offered_px: PxVec2,
|
||||
/// A container's answer for where the widget sits. `None` uses the
|
||||
/// widget's own property.
|
||||
pub align: Option<RegionAlign>,
|
||||
/// The axes along which the parent chose this box from the widget's own
|
||||
/// answer, so the answer is not placed inside it again. See
|
||||
/// [`Painter::widget_at`].
|
||||
pub decided: [bool; 2],
|
||||
}
|
||||
|
||||
pub struct UiRenderState {
|
||||
@@ -35,8 +42,6 @@ pub struct UiRenderState {
|
||||
pub(super) output_size: PxVec2,
|
||||
|
||||
old_root: Option<WidgetId>,
|
||||
/// The slot every chain bottoms out in, holding the output as a box.
|
||||
root_move: MoveIdx,
|
||||
/// Whether the output has changed since the last update. A frame is
|
||||
/// owed for that whether or not anything has to be drawn again.
|
||||
resized: bool,
|
||||
@@ -50,6 +55,9 @@ pub struct UiRenderState {
|
||||
/// Whether this frame contains a declared-length change, so any dirty
|
||||
/// dependent replaces its answer too.
|
||||
replace_answers: bool,
|
||||
/// Widgets waiting for an ancestor to draw them, so the walk down the
|
||||
/// depths does not pick one up again at its own depth.
|
||||
deferred: crate::util::HashSet<WidgetId>,
|
||||
pub moves: Moves,
|
||||
}
|
||||
|
||||
@@ -63,50 +71,46 @@ impl UiRenderState {
|
||||
slots: Default::default(),
|
||||
answer_invalid: Default::default(),
|
||||
replace_answers: false,
|
||||
deferred: Default::default(),
|
||||
moves: Default::default(),
|
||||
root_move: MoveIdx::NONE,
|
||||
resized: false,
|
||||
}
|
||||
}
|
||||
|
||||
/// The window as a box, so a chain bottoms out in one rather than in a
|
||||
/// multiplication applied after it. Composing through a box held in
|
||||
/// pixels leaves everything below it in pixels, which is why nothing
|
||||
/// downstream has to know the output's size to resolve a position.
|
||||
fn write_root(&mut self) {
|
||||
let region = UiRegion::new(
|
||||
UiSpan::new(Len::ZERO, Len::from_parts(Rel::ZERO, self.output_size.x)),
|
||||
UiSpan::new(Len::ZERO, Len::from_parts(Rel::ZERO, self.output_size.y)),
|
||||
);
|
||||
match self.root_move == MoveIdx::NONE {
|
||||
true => self.root_move = self.moves.push(MoveIdx::NONE, region),
|
||||
false => self.moves.set(self.root_move, region),
|
||||
}
|
||||
}
|
||||
|
||||
/// The window, in whatever the platform measures it in, onto the grid
|
||||
/// everything below it is decided on.
|
||||
/// everything below it is decided on. No move entry holds it: a chain
|
||||
/// bottoms out in `MoveIdx::NONE`, which is the window, and the window's
|
||||
/// size is applied where a fraction becomes pixels -- here in `to_px`,
|
||||
/// and in the shader by its uniform. A resize therefore rewrites no
|
||||
/// retained entry at all.
|
||||
pub fn resize(&mut self, size: impl Into<Vec2>) {
|
||||
let size = PxVec2::from_f32(size.into());
|
||||
if size == self.output_size {
|
||||
return;
|
||||
}
|
||||
self.output_size = size;
|
||||
self.write_root();
|
||||
self.resized = true;
|
||||
}
|
||||
|
||||
fn root_info(&self) -> DrawInfo {
|
||||
/// The root is asked about in the output: the window is where a fraction
|
||||
/// becomes pixels rather than a box of its own, so the root's box is the
|
||||
/// first length threaded down. Its own rules narrow that box, and where
|
||||
/// they do the narrowed box is also the offer -- nothing above it chose
|
||||
/// anything else.
|
||||
fn root_info(&self, region: UiRegion) -> DrawInfo {
|
||||
let px = region.size().to_px(self.output_size);
|
||||
DrawInfo {
|
||||
layer: 0,
|
||||
parent: None,
|
||||
depth: 1,
|
||||
parent_move: self.root_move,
|
||||
parent_move: MoveIdx::NONE,
|
||||
region_node: false,
|
||||
mask: MaskIdx::NONE,
|
||||
offer: UiRegion::FULL,
|
||||
offered_px: self.output_size,
|
||||
align: None,
|
||||
given_len: region.size(),
|
||||
offer_len: UiVec2::FULL_SIZE,
|
||||
px,
|
||||
offered_px: px,
|
||||
decided: [false; 2],
|
||||
}
|
||||
}
|
||||
|
||||
@@ -146,8 +150,8 @@ impl UiRenderState {
|
||||
// length, found the way every other box change is found. Before
|
||||
// anything dirty settles, so that whatever a new output draws
|
||||
// again is drawn once, in the box it will have.
|
||||
let info = self.root_info();
|
||||
let region = Self::root_region(root.id(), rsc.widgets());
|
||||
let info = self.root_info(region);
|
||||
let answer = self.draw_inner(root.id(), region, info, None, rsc);
|
||||
self.active.get_mut(&root.id()).unwrap().answer = answer;
|
||||
}
|
||||
@@ -163,11 +167,9 @@ impl UiRenderState {
|
||||
#[cfg(feature = "layout-diagnostics")]
|
||||
let _layout = diag::timer(TimerKind::FullLayout);
|
||||
self.clear(rsc);
|
||||
// free all resources & cache
|
||||
self.write_root();
|
||||
if let Some(id) = root {
|
||||
let info = self.root_info();
|
||||
let region = Self::root_region(id.id(), rsc.widgets());
|
||||
let info = self.root_info(region);
|
||||
self.draw_inner(id.id(), region, info, None, rsc);
|
||||
}
|
||||
}
|
||||
@@ -191,88 +193,87 @@ impl UiRenderState {
|
||||
#[cfg(feature = "layout-diagnostics")]
|
||||
{
|
||||
diag::bump(Counter::DrawRequests);
|
||||
diag::draw_request(
|
||||
id,
|
||||
info.parent,
|
||||
region,
|
||||
self.px_of(info.parent_move, region),
|
||||
info.region_node,
|
||||
);
|
||||
diag::draw_request(id, info.parent, region, info.px, info.region_node);
|
||||
}
|
||||
let own_align = rsc.widgets().alignment(id);
|
||||
let align = info.align.unwrap_or(own_align);
|
||||
let replace_answer = self.answer_invalid.remove(&id)
|
||||
|| (self.replace_answers
|
||||
&& (rsc.widgets().needs_redraw.contains(&id)
|
||||
|| self.dirty_size_under(id, rsc.widgets())));
|
||||
let retained = match replace_answer {
|
||||
let align = rsc.widgets().alignment(id);
|
||||
// Nothing this widget has is an answer while something it measured
|
||||
// is dirty: settling that changes what it would report, and a widget
|
||||
// settled inside its parent's draw tells nobody -- the comparison
|
||||
// that marks a reader is in `redraw`, which is not what asked here.
|
||||
// Both retained routes are an answer, so the question is asked once
|
||||
// rather than by each of them.
|
||||
let stale =
|
||||
rsc.widgets().needs_redraw.contains(&id) || self.dirty_size_under(id, rsc.widgets());
|
||||
let replace_answer = self.answer_invalid.remove(&id) || (self.replace_answers && stale);
|
||||
let retained = match replace_answer || stale {
|
||||
true => None,
|
||||
false => self
|
||||
.retained_answer(id, region, info, rsc.widgets())
|
||||
.retained_answer(id, info)
|
||||
.or_else(|| self.try_reuse(id, region, info, rsc)),
|
||||
};
|
||||
let answer = retained.unwrap_or_else(|| {
|
||||
if old.is_none() {
|
||||
old = self.remove(id, false, rsc);
|
||||
}
|
||||
self.draw_at(id, region, info, align, old.take(), rsc)
|
||||
self.draw_at(id, region, info, old.take(), rsc)
|
||||
});
|
||||
|
||||
let declared = declared_lens(rsc.widgets(), id);
|
||||
// A near-edge override means the caller already chose this box from
|
||||
// the child's answer. Applying the answer again would compound the
|
||||
// placement; it is also how the second, final ask terminates.
|
||||
let placed = match info.align == Some(RegionAlign::NEAR) {
|
||||
true => region,
|
||||
false => placed_box(region, answer.0, align, declared),
|
||||
};
|
||||
// The second, final ask is in a box chosen from the answer on both
|
||||
// axes, which is also what makes it terminate.
|
||||
let lens = placed_lens(answer.0, declared, info.decided);
|
||||
let placed = placed_box(region, lens, align);
|
||||
let placed_info = DrawInfo {
|
||||
align: Some(RegionAlign::NEAR),
|
||||
px: lens.to_px(info.px),
|
||||
decided: [true; 2],
|
||||
..info
|
||||
};
|
||||
// The symbolic box can be unchanged while its parent slot changed
|
||||
// pixel size. Reuse checks the resolved box even in that case.
|
||||
if self.try_reuse(id, placed, placed_info, rsc).is_none() {
|
||||
#[cfg(feature = "layout-diagnostics")]
|
||||
diag::bump(Counter::PlaceRedraws);
|
||||
let old = self.remove(id, false, rsc);
|
||||
self.draw_at(id, placed, placed_info, RegionAlign::NEAR, old, rsc);
|
||||
}
|
||||
self.place(id, placed, placed_info, rsc);
|
||||
|
||||
// The answer is only reusable while both parts of the operation are:
|
||||
// what the widget reported in the offered box, and what it drew in
|
||||
// the box its report selected. Express the latter's contract back in
|
||||
// terms of the offered box before handing it to the parent.
|
||||
// what the widget reported in the box it was asked in, and what it
|
||||
// drew in the box its report selected. Express the latter's contract
|
||||
// back in terms of the box asked in before handing it to the parent.
|
||||
let drawing_holds = self.active[&id].holds;
|
||||
let mut settled = answer;
|
||||
for axis in AXES {
|
||||
let reported = answer.0.axis(axis);
|
||||
let placed_len =
|
||||
match reported.leftover != Weight::ZERO || declared[axis as usize].is_some() {
|
||||
true => Len::FULL,
|
||||
false => Len::from_parts(reported.rel, reported.px),
|
||||
};
|
||||
settled.1[axis as usize] =
|
||||
settled.1[axis as usize].and(drawing_holds[axis as usize].through(placed_len));
|
||||
settled.1[axis as usize].and(drawing_holds[axis as usize].through(lens.axis(axis)));
|
||||
}
|
||||
|
||||
let active = self.active.get_mut(&id).unwrap();
|
||||
active.offer = info.offer;
|
||||
// Whoever asked owns how the box was reached: the box it stated, and
|
||||
// what of that box the answer then took. A local redraw asks the
|
||||
// same question again from these.
|
||||
active.given = region;
|
||||
active.given_len = info.given_len;
|
||||
active.offer_len = info.offer_len;
|
||||
active.answer = settled;
|
||||
active.align = align;
|
||||
active.align_override = info.align.is_some();
|
||||
active.own_align = own_align;
|
||||
active.decided = info.decided;
|
||||
active.own_align = align;
|
||||
active.depth = info.depth;
|
||||
settled
|
||||
}
|
||||
|
||||
/// Draws a widget in the final box its answer chose, reusing the drawing
|
||||
/// already there where its retained contract holds for that box. The
|
||||
/// symbolic box can be unchanged while the box it sits in changed pixel
|
||||
/// length, so what reuse checks is the box in pixels.
|
||||
fn place(&mut self, id: WidgetId, placed: UiRegion, info: DrawInfo, rsc: &mut dyn UiRsc) {
|
||||
if self.try_reuse(id, placed, info, rsc).is_none() {
|
||||
#[cfg(feature = "layout-diagnostics")]
|
||||
diag::bump(Counter::PlaceRedraws);
|
||||
let old = self.remove(id, false, rsc);
|
||||
self.draw_at(id, placed, info, old, rsc);
|
||||
}
|
||||
}
|
||||
|
||||
/// Calls a widget's `draw` and keeps what it drew in `region`.
|
||||
fn draw_at(
|
||||
&mut self,
|
||||
id: WidgetId,
|
||||
region: UiRegion,
|
||||
info: DrawInfo,
|
||||
align: RegionAlign,
|
||||
old: Option<ActiveData>,
|
||||
rsc: &mut dyn UiRsc,
|
||||
) -> (Size, [Holds; 2]) {
|
||||
@@ -293,12 +294,17 @@ impl UiRenderState {
|
||||
None => (Vec::new(), None),
|
||||
};
|
||||
rsc.widgets_mut().needs_redraw.remove(&id);
|
||||
let px = self.px_of(move_idx, local);
|
||||
let at_offer = same_px(px, info.offered_px);
|
||||
// A box of the offered lengths asks the offer's question wherever it
|
||||
// sits, since what a drawing depends on is its lengths -- and
|
||||
// equality is the comparison, these being counts of a step rather
|
||||
// than floats to be compared for nearness.
|
||||
let px = info.px;
|
||||
let at_offer = px == info.offered_px;
|
||||
|
||||
let mut painter = Painter {
|
||||
state: self,
|
||||
region: local,
|
||||
px,
|
||||
mask: info.mask,
|
||||
layer: info.layer,
|
||||
own_layer: info.layer,
|
||||
@@ -332,6 +338,7 @@ impl UiRenderState {
|
||||
state: _,
|
||||
rsc: _,
|
||||
region: _,
|
||||
px: _,
|
||||
mask,
|
||||
textures,
|
||||
primitives,
|
||||
@@ -401,9 +408,11 @@ impl UiRenderState {
|
||||
parent_move: move_idx,
|
||||
region_node: false,
|
||||
mask,
|
||||
offer: UiRegion::FULL,
|
||||
given_len: UiVec2::FULL_SIZE,
|
||||
offer_len: UiVec2::FULL_SIZE,
|
||||
px,
|
||||
offered_px: px,
|
||||
align: None,
|
||||
decided: [false; 2],
|
||||
},
|
||||
rsc,
|
||||
);
|
||||
@@ -414,7 +423,12 @@ impl UiRenderState {
|
||||
let active = ActiveData {
|
||||
id,
|
||||
region,
|
||||
offer: info.offer,
|
||||
// The box a placing ask draws in is a part of the one its parent
|
||||
// gave, which `draw_inner` writes back over these once the
|
||||
// placement is done.
|
||||
given: region,
|
||||
given_len: info.given_len,
|
||||
offer_len: info.offer_len,
|
||||
// Whoever asked writes the answer, if this was the asking.
|
||||
answer: old_answer.unwrap_or((size, holds)),
|
||||
size,
|
||||
@@ -427,12 +441,12 @@ impl UiRenderState {
|
||||
children,
|
||||
size_deps,
|
||||
declared: declared_lens(rsc.widgets(), id),
|
||||
align,
|
||||
align_override: info.align.is_some(),
|
||||
decided: info.decided,
|
||||
own_align: rsc.widgets().alignment(id),
|
||||
move_idx,
|
||||
parent_move: info.parent_move,
|
||||
mask,
|
||||
parent_mask: info.mask,
|
||||
layer: info.layer,
|
||||
};
|
||||
rsc.on_draw(&active);
|
||||
@@ -460,20 +474,6 @@ impl UiRenderState {
|
||||
}
|
||||
}
|
||||
|
||||
/// The pixel size of a region held in `slot`'s coordinates.
|
||||
pub(super) fn px_of(&self, slot: MoveIdx, region: UiRegion) -> PxVec2 {
|
||||
self.moves
|
||||
.resolve(slot, region)
|
||||
.size()
|
||||
.to_px(self.output_size)
|
||||
}
|
||||
|
||||
/// Where a region held in `slot`'s coordinates lands on screen, which is
|
||||
/// the walk the vertex shader does.
|
||||
fn px_region(&self, slot: MoveIdx, region: UiRegion) -> PixelRegion {
|
||||
self.moves.resolve(slot, region).to_px(self.output_size)
|
||||
}
|
||||
|
||||
/// A clean widget's retained answer, if that answer holds for a box of
|
||||
/// `px`. This does not move its drawing, which may already be in the box
|
||||
/// that answer placed it in.
|
||||
@@ -498,17 +498,9 @@ impl UiRenderState {
|
||||
|
||||
/// The answer to an ask can be retained independently of where its
|
||||
/// drawing ended up. Alignment is exactly that case: the first box is the
|
||||
/// question and the smaller placed box holds the drawing.
|
||||
fn retained_answer(
|
||||
&self,
|
||||
id: WidgetId,
|
||||
region: UiRegion,
|
||||
info: DrawInfo,
|
||||
widgets: &Widgets,
|
||||
) -> Option<(Size, [Holds; 2])> {
|
||||
if widgets.needs_redraw.contains(&id) || self.dirty_size_under(id, widgets) {
|
||||
return None;
|
||||
}
|
||||
/// question and the smaller placed box holds the drawing. Whether the
|
||||
/// answer is stale at all is its caller's question, asked once there.
|
||||
fn retained_answer(&self, id: WidgetId, info: DrawInfo) -> Option<(Size, [Holds; 2])> {
|
||||
let active = self.active.get(&id)?;
|
||||
let has_region_node = active.move_idx != active.parent_move;
|
||||
if !active.drawn
|
||||
@@ -517,15 +509,16 @@ impl UiRenderState {
|
||||
{
|
||||
return None;
|
||||
}
|
||||
let px = self.px_of(info.parent_move, region);
|
||||
let (size, holds) = active.answer;
|
||||
(holds[0].contains(px.x) && holds[1].contains(px.y)).then_some((size, holds))
|
||||
(holds[0].contains(info.px.x) && holds[1].contains(info.px.y)).then_some((size, holds))
|
||||
}
|
||||
|
||||
/// Whether anything whose size this widget's own size was read from is
|
||||
/// dirty. Not needed for the answer to come right -- a changed size
|
||||
/// reaches its reader in any order -- but a reader that asks first
|
||||
/// lays out once rather than twice.
|
||||
/// dirty, which makes what it would answer not yet known. It also keeps
|
||||
/// a reader that asks first from laying out twice, which is all it was
|
||||
/// here for while a changed size was thought to reach its reader in any
|
||||
/// order; it does not, where the change settles inside the reader's own
|
||||
/// draw.
|
||||
fn dirty_size_under(&self, id: WidgetId, widgets: &Widgets) -> bool {
|
||||
self.active.get(&id).is_some_and(|active| {
|
||||
active.size_deps.iter().any(|child| {
|
||||
@@ -534,31 +527,39 @@ impl UiRenderState {
|
||||
})
|
||||
}
|
||||
|
||||
/// The first box a widget was asked about, re-expressed in the coordinate
|
||||
/// space its drawing uses. Keeping the relative box and composing it
|
||||
/// again avoids rebuilding a shifted box from rounded pixel lengths.
|
||||
fn offered_region(&self, id: WidgetId) -> UiRegion {
|
||||
/// The pixel lengths of the box a widget was given and of the box it was
|
||||
/// first asked about, which is what a local redraw needs to ask the
|
||||
/// question its parent asked.
|
||||
///
|
||||
/// Both are threaded down from the window a length of a box at a time,
|
||||
/// and this takes the same steps back up: a widget's box is a length of
|
||||
/// the box its parent drew in, and its offer a length of the box its
|
||||
/// parent was itself offered. Neither chain has a coordinate frame in it,
|
||||
/// so neither breaks at a region node -- and both land on the numbers a
|
||||
/// cold layout computes, rather than near them.
|
||||
fn asked_px(&self, id: WidgetId) -> (PxVec2, PxVec2) {
|
||||
let active = &self.active[&id];
|
||||
let parent_region = match active.parent.and_then(|id| self.active.get(&id)) {
|
||||
Some(parent) if parent.move_idx == active.parent_move => {
|
||||
if parent.move_idx == parent.parent_move {
|
||||
self.offered_region(parent.id)
|
||||
} else {
|
||||
UiRegion::FULL
|
||||
// Nothing above the root: the window is where a fraction becomes
|
||||
// pixels, which is also the whole of the box the root is given.
|
||||
let (parent_px, parent_offer) = match active.parent.and_then(|p| self.active.get(&p)) {
|
||||
Some(parent) => {
|
||||
let (given, offer) = self.asked_px(parent.id);
|
||||
let lens = placed_lens(parent.answer.0, parent.declared, parent.decided);
|
||||
(lens.to_px(given), offer)
|
||||
}
|
||||
}
|
||||
_ => UiRegion::FULL,
|
||||
};
|
||||
let mut offered = match active.offer == UiRegion::FULL {
|
||||
true => parent_region,
|
||||
false => active.offer.within(&parent_region),
|
||||
None => (self.output_size, self.output_size),
|
||||
};
|
||||
let px = active.given_len.to_px(parent_px);
|
||||
let mut offered = active.offer_len.to_px(parent_offer);
|
||||
for axis in AXES {
|
||||
// A declared length is resolved by whoever drew the widget, in
|
||||
// the box that widget drew in, so the box it has is the box it
|
||||
// was asked about however the offer above it moved.
|
||||
if active.declared[axis as usize].is_some() {
|
||||
*offered.axis_mut(axis) = *active.region.axis(axis);
|
||||
*offered.axis_mut(axis) = px.axis(axis);
|
||||
}
|
||||
}
|
||||
offered
|
||||
(px, offered)
|
||||
}
|
||||
|
||||
/// Reuses the actual drawing in a new box if its retained contract holds
|
||||
@@ -615,10 +616,10 @@ impl UiRenderState {
|
||||
}
|
||||
return None;
|
||||
}
|
||||
// In pixels, because `region` is a fraction of a slot's box and that
|
||||
// box may be what changed -- an unchanged fraction of a box half the
|
||||
// size is half the widget.
|
||||
if !active.holds_at(self.px_of(info.parent_move, region)) {
|
||||
// In pixels, because `region` is a fraction of the box its parent
|
||||
// drew in and that box may be what changed -- an unchanged fraction
|
||||
// of a box half the size is half the widget.
|
||||
if !active.holds_at(info.px) {
|
||||
#[cfg(feature = "layout-diagnostics")]
|
||||
{
|
||||
diag::bump(Counter::ReuseOutside);
|
||||
@@ -627,23 +628,21 @@ impl UiRenderState {
|
||||
return None;
|
||||
}
|
||||
let moved = active.region != region;
|
||||
let (answer, old_region, slot, mask) = (
|
||||
(active.size, active.holds),
|
||||
active.region,
|
||||
active.move_idx,
|
||||
info.mask,
|
||||
);
|
||||
let (answer, old_region, slot) =
|
||||
((active.size, active.holds), active.region, active.move_idx);
|
||||
if moved {
|
||||
if has_region_node {
|
||||
self.moves.set(slot, region);
|
||||
} else {
|
||||
let remap = RegionRemap::new(old_region, region)?;
|
||||
self.remap_subtree(id, &remap, info.parent_move, mask, rsc);
|
||||
self.remap_subtree(id, &remap, info.parent_move, rsc);
|
||||
}
|
||||
}
|
||||
let active = self.active.get_mut(&id).unwrap();
|
||||
active.region = region;
|
||||
active.offer = info.offer;
|
||||
active.given = region;
|
||||
active.given_len = info.given_len;
|
||||
active.offer_len = info.offer_len;
|
||||
active.depth = info.depth;
|
||||
#[cfg(feature = "layout-diagnostics")]
|
||||
{
|
||||
@@ -676,10 +675,10 @@ impl UiRenderState {
|
||||
id: WidgetId,
|
||||
remap: &RegionRemap,
|
||||
parent_move: MoveIdx,
|
||||
inherited_mask: MaskIdx,
|
||||
rsc: &mut dyn UiRsc,
|
||||
) {
|
||||
let active = self.active.get_mut(&id).unwrap();
|
||||
active.given = remap.apply(active.given);
|
||||
if active.move_idx != parent_move {
|
||||
let region = remap.apply(active.region);
|
||||
active.region = region;
|
||||
@@ -691,16 +690,18 @@ impl UiRenderState {
|
||||
*region = remap.apply(*region);
|
||||
}
|
||||
active.region = remap.apply(active.region);
|
||||
let mask = active.mask;
|
||||
let own_mask = (active.mask != active.parent_mask).then_some(active.mask);
|
||||
let children = active.children.len();
|
||||
if mask != inherited_mask && mask != MaskIdx::NONE {
|
||||
let mask = rsc.ui_mut().masks.get_mut(mask);
|
||||
// A mask the widget set itself moves with it; one it inherited
|
||||
// belongs to the widget that set it, and moves there or not at all.
|
||||
if let Some(idx) = own_mask {
|
||||
let mask = rsc.ui_mut().masks.get_mut(idx);
|
||||
debug_assert_eq!(mask.move_idx, parent_move);
|
||||
mask.region = remap.apply(mask.region);
|
||||
}
|
||||
for index in 0..children {
|
||||
let child = self.active[&id].children[index];
|
||||
self.remap_subtree(child, remap, parent_move, mask, rsc);
|
||||
self.remap_subtree(child, remap, parent_move, rsc);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -775,7 +776,9 @@ impl UiRenderState {
|
||||
ActiveData {
|
||||
id,
|
||||
region: UiRegion::FULL,
|
||||
offer: UiRegion::FULL,
|
||||
given: UiRegion::FULL,
|
||||
given_len: UiVec2::FULL_SIZE,
|
||||
offer_len: UiVec2::FULL_SIZE,
|
||||
answer: (size, [Holds::ANY; 2]),
|
||||
size,
|
||||
holds: [Holds::ANY; 2],
|
||||
@@ -788,11 +791,11 @@ impl UiRenderState {
|
||||
size_deps: Vec::new(),
|
||||
move_idx: info.parent_move,
|
||||
declared: [None; 2],
|
||||
align: RegionAlign::default(),
|
||||
align_override: false,
|
||||
decided: [false; 2],
|
||||
own_align: rsc.widgets().alignment(id),
|
||||
parent_move: info.parent_move,
|
||||
mask: info.mask,
|
||||
parent_mask: info.mask,
|
||||
layer: info.layer,
|
||||
},
|
||||
);
|
||||
@@ -808,7 +811,6 @@ impl UiRenderState {
|
||||
self.answer_invalid.clear();
|
||||
self.replace_answers = false;
|
||||
self.moves.clear();
|
||||
self.root_move = MoveIdx::NONE;
|
||||
self.layers.clear();
|
||||
rsc.widgets_mut().needs_redraw.clear();
|
||||
self.free(rsc);
|
||||
@@ -829,19 +831,34 @@ impl UiRenderState {
|
||||
pub fn redraw_updates(&mut self, rsc: &mut dyn UiRsc) {
|
||||
#[cfg(feature = "layout-diagnostics")]
|
||||
let _layout = diag::timer(TimerKind::IncrementalLayout);
|
||||
// Deepest first: a reader whose children have all settled asks each
|
||||
// once, where any other order has it lay out again for whatever
|
||||
// settles under it afterwards. Equal-depth widgets are independent,
|
||||
// so their order does not matter.
|
||||
while let Some(id) = {
|
||||
let dirty = rsc.widgets().needs_redraw.iter().copied();
|
||||
dirty.max_by_key(|&id| self.depth(id))
|
||||
} {
|
||||
// Deepest first, and strictly: a widget that cannot settle where it
|
||||
// is defers to its parent rather than drawing the parent from
|
||||
// inside itself. It marks the parent, stays marked, and waits here
|
||||
// until the walk reaches its parent's depth.
|
||||
//
|
||||
// What that buys is that nothing shallower is ever drawn while
|
||||
// anything deeper is still dirty. A parent drawing can therefore
|
||||
// trust every answer it reads without descending to check whether
|
||||
// something below is about to change it -- which is the whole class
|
||||
// of defect where a widget settles inside its parent's draw, clears
|
||||
// its mark there, and tells nobody its answer moved.
|
||||
loop {
|
||||
let next = rsc
|
||||
.widgets()
|
||||
.needs_redraw
|
||||
.iter()
|
||||
.copied()
|
||||
.filter(|id| !self.deferred.contains(id))
|
||||
.max_by_key(|&id| self.depth(id));
|
||||
let Some(id) = next else { break };
|
||||
#[cfg(feature = "layout-diagnostics")]
|
||||
diag::bump(Counter::QueuePops);
|
||||
self.redraw(id, rsc);
|
||||
if !self.redraw(id, rsc) {
|
||||
self.deferred.insert(id);
|
||||
}
|
||||
}
|
||||
self.deferred.clear();
|
||||
}
|
||||
|
||||
fn depth(&self, id: WidgetId) -> usize {
|
||||
#[cfg(feature = "layout-diagnostics")]
|
||||
@@ -909,20 +926,29 @@ impl UiRenderState {
|
||||
}
|
||||
|
||||
/// Where a widget is on screen: its box composed through the boxes it
|
||||
/// sits within. `None` for one that is not drawn.
|
||||
/// sits within, the same walk the vertex shader does. `None` for one that
|
||||
/// is not drawn.
|
||||
///
|
||||
/// This is for asking where a drawing landed: hit testing, and a test
|
||||
/// reading a box back. Layout decides on the lengths threaded down the
|
||||
/// draw instead, and a position is not one of its inputs.
|
||||
pub fn window_region(&self, id: &impl IdLike) -> Option<PixelRegion> {
|
||||
let active = self.active.get(&id.id())?;
|
||||
active
|
||||
.drawn
|
||||
.then(|| self.px_region(active.parent_move, active.region))
|
||||
active.drawn.then(|| {
|
||||
self.moves
|
||||
.resolve(active.parent_move, active.region)
|
||||
.to_px(self.output_size)
|
||||
})
|
||||
}
|
||||
|
||||
/// Settles a dirty widget: asks it again where its parent asked, and
|
||||
/// tells the parent if the answer changed.
|
||||
pub fn redraw(&mut self, id: WidgetId, rsc: &mut dyn UiRsc) {
|
||||
/// tells the parent if the answer changed. `false` where the question is
|
||||
/// its parent's rather than its own, which leaves it marked for the
|
||||
/// parent to draw when the walk reaches that depth.
|
||||
pub fn redraw(&mut self, id: WidgetId, rsc: &mut dyn UiRsc) -> bool {
|
||||
rsc.widgets_mut().needs_redraw.remove(&id);
|
||||
let Some(active) = self.active.get(&id) else {
|
||||
return;
|
||||
return true;
|
||||
};
|
||||
// Its parent resolved its declared lengths into its box and decided
|
||||
// whether to draw it at all, so a change to either is the parent's
|
||||
@@ -942,91 +968,74 @@ impl UiRenderState {
|
||||
at = self.active[&next].parent;
|
||||
}
|
||||
}
|
||||
// Both stay marked: the parent because it has this to draw, and
|
||||
// this because the parent must draw it rather than keep what it
|
||||
// has. The mark comes off in `draw_at`, where the parent draws.
|
||||
rsc.widgets_mut().needs_redraw.insert(id);
|
||||
self.redraw(parent, rsc);
|
||||
// Whatever the parent did not draw again is nothing it holds now.
|
||||
rsc.widgets_mut().needs_redraw.remove(&id);
|
||||
return;
|
||||
rsc.widgets_mut().needs_redraw.insert(parent);
|
||||
return false;
|
||||
}
|
||||
if !active.drawn {
|
||||
return;
|
||||
return true;
|
||||
}
|
||||
let region = active.region;
|
||||
let region_node = active.parent.is_some() && rsc.widgets().is_region_node(id);
|
||||
let asked_in = match active.parent {
|
||||
Some(_) => self.offered_region(id),
|
||||
None => Self::root_region(id, rsc.widgets()),
|
||||
// Nothing above the root resolved its rules or its alignment, so its
|
||||
// box is its own to work out again against the output. Every other
|
||||
// widget was given one.
|
||||
let Some(parent) = active.parent else {
|
||||
let region = Self::root_region(id, rsc.widgets());
|
||||
let info = DrawInfo {
|
||||
mask: active.parent_mask,
|
||||
..self.root_info(region)
|
||||
};
|
||||
let offered_px = self.px_of(active.parent_move, asked_in);
|
||||
// Whole boxes rather than lengths: an offer as long as the final box
|
||||
// but somewhere else is a different box, and a region node drawing at
|
||||
// its offer writes the box it drew in into its own entry.
|
||||
let at_offer = same_pixel_region(
|
||||
self.px_region(active.parent_move, region),
|
||||
self.px_region(active.parent_move, asked_in),
|
||||
);
|
||||
let parent_must_place =
|
||||
active.parent.is_some() && (!region_node || active.align_override) && !at_offer;
|
||||
// An independently positioned region node can redraw at its offer
|
||||
// and move its slot to its own placement. Every other widget needs
|
||||
// its parent to reproduce a different final position.
|
||||
if let Some(parent) = active.parent
|
||||
&& parent_must_place
|
||||
{
|
||||
#[cfg(feature = "layout-diagnostics")]
|
||||
diag::bump(Counter::LocalRedraws);
|
||||
let old = self.remove(id, false, rsc);
|
||||
self.draw_inner(id, region, info, old, rsc);
|
||||
return true;
|
||||
};
|
||||
let (given_px, offered_px) = self.asked_px(id);
|
||||
// Asked again in the box its parent gave it, which is the question
|
||||
// its parent asked only while that box is as long as the offer. Any
|
||||
// other box is a different question, so the parent asks it, with the
|
||||
// mark left on. Lengths and not whole boxes: what a drawing depends
|
||||
// on is its lengths, so the same lengths elsewhere is one question.
|
||||
if given_px != offered_px {
|
||||
rsc.widgets_mut().needs_redraw.insert(id);
|
||||
self.redraw(parent, rsc);
|
||||
rsc.widgets_mut().needs_redraw.remove(&id);
|
||||
return;
|
||||
rsc.widgets_mut().needs_redraw.insert(parent);
|
||||
return false;
|
||||
}
|
||||
let info = DrawInfo {
|
||||
layer: active.layer,
|
||||
parent: active.parent,
|
||||
depth: active.depth,
|
||||
parent_move: active.parent_move,
|
||||
region_node,
|
||||
mask: active.mask,
|
||||
offer: active.offer,
|
||||
region_node: rsc.widgets().is_region_node(id),
|
||||
mask: active.parent_mask,
|
||||
given_len: active.given_len,
|
||||
offer_len: active.offer_len,
|
||||
px: given_px,
|
||||
offered_px,
|
||||
align: active.align_override.then_some(active.align),
|
||||
decided: active.decided,
|
||||
};
|
||||
let (was_answer, was) = (active.answer, (active.size, active.holds));
|
||||
let (given, was_answer) = (active.given, active.answer);
|
||||
#[cfg(feature = "layout-diagnostics")]
|
||||
diag::bump(Counter::LocalRedraws);
|
||||
|
||||
let old = self.remove(id, false, rsc);
|
||||
let answer = self.draw_inner(id, asked_in, info, old, rsc);
|
||||
self.active.get_mut(&id).unwrap().answer = answer;
|
||||
let Some(parent) = info.parent else {
|
||||
return;
|
||||
};
|
||||
// `draw_inner` places the answer inside that box itself, which is the
|
||||
// ask that leaves the widget where its parent put it.
|
||||
let answer = self.draw_inner(id, given, info, old, rsc);
|
||||
if answer != was_answer {
|
||||
// Left where it was asked: the parent lays out again and chooses
|
||||
// its final box.
|
||||
// Its parent chose its box knowing the old answer, so it lays out
|
||||
// again and chooses the box the new one asks for.
|
||||
#[cfg(feature = "layout-diagnostics")]
|
||||
{
|
||||
diag::bump(Counter::SizeChanges);
|
||||
diag::bump(Counter::ReaderEdges);
|
||||
}
|
||||
rsc.widgets_mut().needs_redraw.insert(parent);
|
||||
return;
|
||||
}
|
||||
if at_offer {
|
||||
return;
|
||||
}
|
||||
// Then in the final box its parent chose from that answer. That box
|
||||
// is already placed, so the near edge goes with it: applying the
|
||||
// widget's own alignment to it again would place its content twice,
|
||||
// the way it did for a region node under a `Stack` once the stack
|
||||
// stopped overriding every child's alignment.
|
||||
let placed_info = DrawInfo {
|
||||
align: Some(RegionAlign::NEAR),
|
||||
..info
|
||||
};
|
||||
self.draw_inner(id, region, placed_info, None, rsc);
|
||||
let active = &self.active[&id];
|
||||
if (active.size, active.holds) != was {
|
||||
rsc.widgets_mut().needs_redraw.insert(parent);
|
||||
}
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1039,16 +1048,6 @@ fn within_box(size: Size, px: PxVec2, axis: Axis) -> bool {
|
||||
len.leftover != Weight::ZERO || box_len.mul(len.rel) + len.px <= box_len
|
||||
}
|
||||
|
||||
/// The same box is the same number of steps, both of these being lengths on
|
||||
/// the grid rather than floats to be compared for nearness.
|
||||
fn same_px(a: PxVec2, b: PxVec2) -> bool {
|
||||
a == b
|
||||
}
|
||||
|
||||
fn same_pixel_region(a: PixelRegion, b: PixelRegion) -> bool {
|
||||
same_px(a.top_left, b.top_left) && same_px(a.bot_right, b.bot_right)
|
||||
}
|
||||
|
||||
/// A retained region rewritten from one parent box into another. A fixed
|
||||
/// source extent can be translated but cannot recover fractions for a resize.
|
||||
#[derive(Clone, Copy)]
|
||||
|
||||
@@ -20,10 +20,14 @@ impl DefaultAppState for Client {
|
||||
let pad_test = (
|
||||
rrect.color(Color::BLUE),
|
||||
(
|
||||
// The square is one widget and the two shares of the row it
|
||||
// sits centred in are another: a length is a property of a
|
||||
// widget, so `.width` here would overwrite the `.sized`.
|
||||
rrect
|
||||
.color(Color::RED)
|
||||
.sized((100, 100))
|
||||
.center()
|
||||
.wrapper()
|
||||
.width(leftover(2)),
|
||||
(
|
||||
rrect.color(Color::ORANGE),
|
||||
@@ -143,7 +147,7 @@ impl DefaultAppState for Client {
|
||||
.span(Dir::DOWN)
|
||||
.add(rsc);
|
||||
|
||||
let main = WidgetPtr::new().add(rsc);
|
||||
let main = Wrapper::new().add(rsc);
|
||||
|
||||
let vals = Rc::new(RefCell::new((0, Vec::new())));
|
||||
let mut switch_button = |color, to: WeakWidget, label| {
|
||||
|
||||
+7
-3
@@ -28,10 +28,14 @@ impl DefaultAppState for State {
|
||||
.pad(16)
|
||||
.background(panel());
|
||||
|
||||
// Each one takes the whole width, because `text_align` puts the
|
||||
// glyphs somewhere in the box the text is given and a text that
|
||||
// reports the width of its own glyphs is given exactly that.
|
||||
let label = |text: &str, align| wtext(text).size(24).text_align(align).width(rel(1.0));
|
||||
let aligned = (
|
||||
wtext("left").size(24).text_align(Align::LEFT),
|
||||
wtext("centred").size(24).text_align(Align::CENTER),
|
||||
wtext("right").size(24).text_align(Align::RIGHT),
|
||||
label("left", Align::LEFT),
|
||||
label("centred", Align::H_CENTER),
|
||||
label("right", Align::RIGHT),
|
||||
)
|
||||
.span(Dir::DOWN)
|
||||
.gap(8)
|
||||
|
||||
+2
-2
@@ -1,15 +1,15 @@
|
||||
mod image;
|
||||
mod mask;
|
||||
mod position;
|
||||
mod ptr;
|
||||
mod rect;
|
||||
mod text;
|
||||
mod trait_fns;
|
||||
mod wrapper;
|
||||
|
||||
pub use image::*;
|
||||
pub use mask::*;
|
||||
pub use position::*;
|
||||
pub use ptr::*;
|
||||
pub use rect::*;
|
||||
pub use text::*;
|
||||
pub use trait_fns::*;
|
||||
pub use wrapper::*;
|
||||
@@ -9,10 +9,10 @@ impl Widget for Pad {
|
||||
fn draw(&mut self, painter: &mut Painter) -> Size {
|
||||
// The inner's own alignment, not the near edge. This reports the
|
||||
// inner's size plus the padding, so where the box is that answer the
|
||||
// inset box is exactly the inner and alignment has no room to move
|
||||
// it; where the box is bigger -- a share of a row, a rule over this
|
||||
// widget -- the slack is the inner's to sit in, and forcing the near
|
||||
// edge pinned it to a corner it had not asked for.
|
||||
// inner is exactly what it asked for and alignment has no room to
|
||||
// move it; where the box is bigger -- a share of a row, a rule over
|
||||
// this widget -- the slack is the inner's to sit in, and forcing the
|
||||
// near edge pinned it to a corner it had not asked for.
|
||||
let inner = painter
|
||||
.widget_within(&self.inner, self.padding.region())
|
||||
.size();
|
||||
@@ -29,6 +29,45 @@ impl Widget for Pad {
|
||||
}
|
||||
}
|
||||
|
||||
/// Room taken off the inside rather than added round the outside: the child
|
||||
/// draws in what is left once both edges are gone, and this widget is
|
||||
/// exactly as long as the box it was given.
|
||||
///
|
||||
/// So `rel(1.0)` under an [`Inset`] is the room inside it, where the same
|
||||
/// rule under a [`Pad`] is the pad's whole box and overflows it by the
|
||||
/// padding. Both are wanted; which one a layout means is which widget it
|
||||
/// reaches for.
|
||||
pub struct Inset {
|
||||
pub padding: Padding,
|
||||
pub inner: StrongWidget,
|
||||
}
|
||||
|
||||
impl Widget for Inset {
|
||||
fn draw(&mut self, painter: &mut Painter) -> Size {
|
||||
let region = self.padding.inset_region();
|
||||
let inner = painter.widget_within(&self.inner, region).size();
|
||||
// What a fraction the child reported is a fraction of is this
|
||||
// widget's to say, and it says the room inside: the child asked for
|
||||
// a part of the box it drew in, and that box is shorter than this
|
||||
// one by both edges. Then the edges go back on, so this widget is
|
||||
// its child and the room taken off around it.
|
||||
let (x, y) = (
|
||||
inner.x.within_len(region.x.len()),
|
||||
inner.y.within_len(region.y.len()),
|
||||
);
|
||||
Size {
|
||||
x: LayoutLen {
|
||||
px: x.px + self.padding.left + self.padding.right,
|
||||
..x
|
||||
},
|
||||
y: LayoutLen {
|
||||
px: y.px + self.padding.top + self.padding.bottom,
|
||||
..y
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Padding {
|
||||
pub left: Px,
|
||||
pub right: Px,
|
||||
@@ -53,7 +92,24 @@ impl Padding {
|
||||
bottom: amt,
|
||||
}
|
||||
}
|
||||
/// The box a [`Pad`] gives its child: as long as the pad's own, moved in
|
||||
/// by the near edge. Padding is outside what it pads, so a fraction the
|
||||
/// child asks for is a fraction of the same length whether a rule beside
|
||||
/// it states one or it reports one, and its pixels are the same pixels.
|
||||
/// Shrinking the box instead would make `rel` mean the inner box while
|
||||
/// `px` meant the outer one. [`Inset`] is the widget that shrinks.
|
||||
pub fn region(&self) -> UiRegion {
|
||||
let mut region = UiRegion::FULL;
|
||||
region.x.start.px += self.left;
|
||||
region.y.start.px += self.top;
|
||||
region.x.end.px += self.left;
|
||||
region.y.end.px += self.top;
|
||||
region
|
||||
}
|
||||
|
||||
/// The box an [`Inset`] gives its child: shorter than its own by both
|
||||
/// edges, so what the child fills is the room left inside.
|
||||
pub fn inset_region(&self) -> UiRegion {
|
||||
let mut region = UiRegion::FULL;
|
||||
region.x.start.px += self.left;
|
||||
region.y.start.px += self.top;
|
||||
|
||||
@@ -63,7 +63,7 @@ impl Widget for Scroll {
|
||||
region = region.offset(offset);
|
||||
region.axis_mut(self.axis).end = region.axis(self.axis).start.offset(self.content_len);
|
||||
}
|
||||
painter.widget_aligned(&self.inner, region, RegionAlign::NEAR);
|
||||
painter.widget_at(&self.inner, region, [true; 2]);
|
||||
// What it occupies is its box, on both axes: it clips its content to
|
||||
// that box, so it can neither take less of one nor honestly ask for
|
||||
// more. The content's length is what it scrolls through, not what it
|
||||
|
||||
@@ -20,9 +20,13 @@ impl Widget for Span {
|
||||
span.flip();
|
||||
}
|
||||
let region = UiRegion::from_axis(axis, span, UiSpan::FULL);
|
||||
// Offered the room left from the cursor, because a text has to
|
||||
// wrap at the width actually there, while what it reports is a
|
||||
// fraction of the whole row: `rel(0.5)` is half the span
|
||||
// whatever else is in it and wherever this child sits.
|
||||
let len = match painter.known_len(child, axis, region) {
|
||||
Some(len) => len,
|
||||
None => painter.widget_within(child, region).len(axis),
|
||||
None => painter.widget_at(child, region, [false; 2]).len(axis),
|
||||
};
|
||||
cursor.px += len.px + self.gap;
|
||||
cursor.rel += len.rel;
|
||||
@@ -119,7 +123,10 @@ impl Widget for Span {
|
||||
if self.dir.sign == Sign::Neg {
|
||||
region.flip(axis);
|
||||
}
|
||||
let placed = painter.widget_within(child, region);
|
||||
// Along the row this box is the child's own answer, so the answer
|
||||
// is not placed in it again; across it the child sits where its
|
||||
// alignment says.
|
||||
let placed = painter.widget_at(child, region, [axis == Axis::X, axis == Axis::Y]);
|
||||
if shrinks {
|
||||
let used = placed.len(!axis);
|
||||
// Choosing between a fixed and a relative length from the
|
||||
|
||||
@@ -35,7 +35,7 @@ impl Widget for Stack {
|
||||
// child is handed a box that owes nothing to its own answer, and
|
||||
// where it sits in one bigger than itself is its own business.
|
||||
match sizing == Some(i) {
|
||||
true => painter.widget_aligned(child, region, RegionAlign::NEAR),
|
||||
true => painter.widget_at(child, region, [true; 2]),
|
||||
false => painter.widget_within(child, region),
|
||||
};
|
||||
}
|
||||
|
||||
+12
-2
@@ -55,8 +55,13 @@ impl TextView {
|
||||
// line up to the one it was made at: each line still fits, and none
|
||||
// could take a word that did not fit in the wider box. A line too
|
||||
// long to fit at all says nothing about narrower boxes.
|
||||
//
|
||||
// The step at or above that longest line rather than the nearest
|
||||
// one, since the shaper measures in floats: the nearest step is
|
||||
// under the line half the time, and a range starting there admits a
|
||||
// box the line does not fit in, where the break is not this one.
|
||||
if let Some(width) = width {
|
||||
painter.holds(Axis::X, Px::from_f32(text.size.x).min(width)..=width);
|
||||
painter.holds(Axis::X, Px::ceil_from_f32(text.size.x).min(width)..=width);
|
||||
}
|
||||
text
|
||||
}
|
||||
@@ -78,7 +83,12 @@ impl TextView {
|
||||
|
||||
let tex = self.render(painter);
|
||||
let region = tex.size.align(align);
|
||||
let size = Size::px(tex.size);
|
||||
// The step at or above what the shaper measured, so a parent that
|
||||
// hands back the length this reports hands back a box the longest
|
||||
// line fits in. Rounded to the nearest step it is half the time a
|
||||
// hair under that line, and the break made in it is not the break a
|
||||
// cold layout makes there.
|
||||
let size = Size::from_px(PxVec2::ceil_from_f32(tex.size));
|
||||
let within = region.within(&painter.region());
|
||||
painter.glyphs(tex, within);
|
||||
(region, size)
|
||||
|
||||
+17
-3
@@ -12,6 +12,16 @@ widget_trait! {
|
||||
}
|
||||
}
|
||||
|
||||
fn inset(self, padding: impl Into<Padding>) -> impl WidgetFn<Rsc, Inset> {
|
||||
// Room taken off the inside, where `pad` adds it round the outside:
|
||||
// this is as long as the box it is given and the child fills what is
|
||||
// left of it.
|
||||
|state| Inset {
|
||||
padding: padding.into(),
|
||||
inner: self.add_strong(state),
|
||||
}
|
||||
}
|
||||
|
||||
fn align(self, align: impl Into<Align>) -> impl WidgetIdFn<Rsc, WL::Widget> {
|
||||
// An axis left out keeps whatever it had, which is centered unless
|
||||
// something else set it.
|
||||
@@ -134,9 +144,13 @@ widget_trait! {
|
||||
|state| self.add(state)
|
||||
}
|
||||
|
||||
fn set_ptr(self, ptr: WeakWidget<WidgetPtr>, state: &mut Rsc) {
|
||||
let id = self.add_strong(state);
|
||||
state.ui_mut().widgets[ptr].inner = Some(id);
|
||||
// Named for the type it makes rather than as `wrapped`, which would read
|
||||
// as the text setting. `widget_trait!` takes no attributes, so what it is
|
||||
// for is on `Wrapper` itself.
|
||||
fn wrapper(self) -> impl WidgetFn<Rsc, Wrapper> {
|
||||
|state| Wrapper {
|
||||
inner: Some(self.add_strong(state)),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,11 +1,19 @@
|
||||
use crate::prelude::*;
|
||||
use std::marker::Unsize;
|
||||
|
||||
pub struct WidgetPtr {
|
||||
/// One widget in a box of its own, doing as little as possible on the way:
|
||||
/// it draws its child in the whole of its box and reports back what the child
|
||||
/// said. It exists because a length and an alignment are properties of one
|
||||
/// widget, so a widget cannot both be 100 wide and take two shares of a row
|
||||
/// -- the two lengths need two widgets, and this is the smaller one.
|
||||
///
|
||||
/// Its child is optional so it can also be the swappable slot a tab bar
|
||||
/// needs, which is what it was written for.
|
||||
pub struct Wrapper {
|
||||
pub inner: Option<StrongWidget>,
|
||||
}
|
||||
|
||||
impl Widget for WidgetPtr {
|
||||
impl Widget for Wrapper {
|
||||
fn draw(&mut self, painter: &mut Painter) -> Size {
|
||||
match &self.inner {
|
||||
Some(id) => painter.widget(id).size(),
|
||||
@@ -14,7 +22,7 @@ impl Widget for WidgetPtr {
|
||||
}
|
||||
}
|
||||
|
||||
impl WidgetPtr {
|
||||
impl Wrapper {
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
@@ -35,7 +43,7 @@ impl WidgetPtr {
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for WidgetPtr {
|
||||
impl Default for Wrapper {
|
||||
fn default() -> Self {
|
||||
Self::empty()
|
||||
}
|
||||
+106
-11
@@ -20,6 +20,102 @@ fn a_span_gives_each_child_the_width_it_asked_for() {
|
||||
assert_corners!(h, right, (100, 0), (400, 200));
|
||||
}
|
||||
|
||||
/// A span offers each child the room left after the one before, because a
|
||||
/// text has to wrap at the width actually there, but reads what the child
|
||||
/// reports as a fraction of the whole row. So two children asking for half
|
||||
/// each take the whole row between them, however much of it was left when
|
||||
/// each was asked, and a third overflows.
|
||||
#[test]
|
||||
fn a_span_reads_a_child_report_as_a_fraction_of_the_row() {
|
||||
let mut h = Harness::new((400, 100));
|
||||
let half = rect(Color::RED).width(rel(0.5)).add(&mut h.rsc);
|
||||
let inner = rect(Color::GREEN).width(rel(0.5)).add(&mut h.rsc);
|
||||
let nested = (inner,).span(Dir::RIGHT).add(&mut h.rsc);
|
||||
let tail = rect(Color::BLUE).width(100).add(&mut h.rsc);
|
||||
h.set_root((half, nested, tail).span(Dir::RIGHT).width(rel(1.0)));
|
||||
|
||||
// The nested span is placed at the length it reported and drawn there
|
||||
// once more; half of that final box is what its own child takes.
|
||||
assert_corners!(h, nested, (200, 0), (400, 100));
|
||||
assert_corners!(h, inner, (200, 0), (300, 100));
|
||||
assert_corners!(h, tail, (400, 0), (500, 100));
|
||||
}
|
||||
|
||||
/// The same fraction either way round: after a 100 px child in a 400 px row,
|
||||
/// `rel(0.5)` is 100 to 300 whether the child's own rule says so or the child
|
||||
/// drew half of what it was offered and reported that. Half the row, not half
|
||||
/// of the 300 px left of it.
|
||||
#[test]
|
||||
fn a_reported_fraction_is_of_the_row_like_a_declared_one() {
|
||||
let mut declaring = Harness::new((400, 100));
|
||||
let head = rect(Color::RED).width(100).add(&mut declaring.rsc);
|
||||
let declared = rect(Color::GREEN).width(rel(0.5)).add(&mut declaring.rsc);
|
||||
declaring.set_root((head, declared).span(Dir::RIGHT).width(rel(1.0)));
|
||||
assert_corners!(declaring, declared, (100, 0), (300, 100));
|
||||
|
||||
let mut reporting = Harness::new((400, 100));
|
||||
let head = rect(Color::RED).width(100).add(&mut reporting.rsc);
|
||||
let inner = rect(Color::GREEN).width(rel(0.5)).add(&mut reporting.rsc);
|
||||
let reported = (inner,).span(Dir::RIGHT).add(&mut reporting.rsc);
|
||||
reporting.set_root((head, reported).span(Dir::RIGHT).width(rel(1.0)));
|
||||
assert_corners!(reporting, reported, (100, 0), (300, 100));
|
||||
}
|
||||
|
||||
/// What the fraction a child reports is of and what box it is offered are
|
||||
/// two different lengths, and only the first is the whole row: a text still
|
||||
/// wraps at the room actually left after its neighbour, so the same
|
||||
/// paragraph is taller where less of the row is left for it.
|
||||
#[test]
|
||||
fn a_text_in_a_span_wraps_at_the_room_left_rather_than_the_whole_row() {
|
||||
let paragraph = "Wrapping shapes one source into as many lines as the box \
|
||||
leaves room for, so a paragraph's height is an answer.";
|
||||
let height_after = |head_width: i32| {
|
||||
let mut h = Harness::new((400, 400));
|
||||
let head = rect(Color::RED).width(head_width).add(&mut h.rsc);
|
||||
let text = wtext(paragraph).size(16).wrap(true).add(&mut h.rsc);
|
||||
h.set_root((head, text).span(Dir::RIGHT).width(rel(1.0)));
|
||||
let region = h.region(&text).unwrap();
|
||||
(region.bot_right.y - region.top_left.y).to_f32()
|
||||
};
|
||||
|
||||
let (crowded, whole_row) = (height_after(300), height_after(0));
|
||||
assert!(crowded > whole_row, "{crowded} against {whole_row}");
|
||||
}
|
||||
|
||||
/// Padding is outside what it pads, so a fraction under one is a fraction of
|
||||
/// the box the padding is measured from: half of a 400 px row is 200, and
|
||||
/// the pad is that plus both edges. Inset it instead and `rel` would mean the
|
||||
/// inner box while `px` meant the outer one, which is the one thing a length
|
||||
/// may not do.
|
||||
#[test]
|
||||
fn a_pad_is_outside_the_fraction_its_child_asked_for() {
|
||||
let mut h = Harness::new((400, 100));
|
||||
let inner = rect(Color::GREEN).width(rel(0.5)).add(&mut h.rsc);
|
||||
let padded = (inner,).span(Dir::RIGHT).pad(10).add(&mut h.rsc);
|
||||
let tail = rect(Color::BLUE).width(100).add(&mut h.rsc);
|
||||
// Ruled to the window: a root reporting a fraction of it is otherwise
|
||||
// placed inside it by its own alignment, which is not what is under test.
|
||||
h.set_root((padded, tail).span(Dir::RIGHT).width(rel(1.0)));
|
||||
|
||||
assert_corners!(h, padded, (0, 0), (220, 100));
|
||||
assert_corners!(h, tail, (220, 0), (320, 100));
|
||||
}
|
||||
|
||||
/// The other half of the pair: an inset takes its room off the inside, so it
|
||||
/// is exactly as long as the box it was given and the fraction its child
|
||||
/// asked for is a fraction of what is left inside. Half of the 380 left in a
|
||||
/// 400 px row is 190, and the inset is the whole 400.
|
||||
#[test]
|
||||
fn an_inset_is_inside_the_fraction_its_child_asked_for() {
|
||||
let mut h = Harness::new((400, 100));
|
||||
let inner = rect(Color::GREEN).width(rel(0.5)).add(&mut h.rsc);
|
||||
let inset = (inner,).span(Dir::RIGHT).inset(10).add(&mut h.rsc);
|
||||
h.set_root((inset,).span(Dir::RIGHT).width(rel(1.0)));
|
||||
|
||||
assert_corners!(h, inset, (0, 0), (200, 100));
|
||||
assert_corners!(h, inner, (10, 0), (200, 100));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_span_ruled_across_itself_does_not_measure_its_children_there() {
|
||||
let mut h = Harness::new((400, 200));
|
||||
@@ -158,7 +254,7 @@ fn a_moved_subtree_takes_its_children_with_it() {
|
||||
let mut h = Harness::new((400, 400));
|
||||
let first = rect(Color::RED).height(40).add(&mut h.rsc);
|
||||
let inner = rect(Color::BLUE).add(&mut h.rsc);
|
||||
let row = inner.pad(10).height(40).region_node().add(&mut h.rsc);
|
||||
let row = inner.inset(10).height(40).region_node().add(&mut h.rsc);
|
||||
// 80 of fixed rows in a 400 window, so the span takes 80 and sits in the
|
||||
// middle of what it was given.
|
||||
h.set_root((first, row).span(Dir::DOWN));
|
||||
@@ -201,7 +297,7 @@ fn a_box_with_a_fixed_length_can_be_stretched_on_its_other_axis() {
|
||||
// impossible to take out of: recovering a fraction of a box needs a
|
||||
// relative extent, and it has none on that axis.
|
||||
let inner = rect(Color::BLUE).add(&mut h.rsc);
|
||||
let row = inner.pad(10).height(40).add(&mut h.rsc);
|
||||
let row = inner.inset(10).height(40).add(&mut h.rsc);
|
||||
let filler = rect(Color::GREEN).add(&mut h.rsc);
|
||||
// This column is an item in a row, so it takes the width left for it
|
||||
// rather than asking for a full row-width in addition to the bar.
|
||||
@@ -225,21 +321,21 @@ fn only_a_region_node_lengthens_the_chain_and_it_can_be_removed() {
|
||||
h.set_root((bar, buried).span(Dir::RIGHT));
|
||||
|
||||
let move_idx = h.render.active[&leaf.id()].parent_move;
|
||||
assert_eq!(h.render.moves.depth(move_idx), 1, "only the root region");
|
||||
assert_eq!(h.render.moves.depth(move_idx), 0, "the window is no entry");
|
||||
|
||||
h.rsc.widgets_mut().set_region_node(buried, true);
|
||||
h.frame();
|
||||
let move_idx = h.render.active[&leaf.id()].parent_move;
|
||||
assert_eq!(
|
||||
h.render.moves.depth(move_idx),
|
||||
2,
|
||||
"the opted-in widget's region and the root region"
|
||||
1,
|
||||
"the opted-in widget's region alone"
|
||||
);
|
||||
|
||||
h.rsc.widgets_mut().set_region_node(buried, false);
|
||||
h.frame();
|
||||
let move_idx = h.render.active[&leaf.id()].parent_move;
|
||||
assert_eq!(h.render.moves.depth(move_idx), 1);
|
||||
assert_eq!(h.render.moves.depth(move_idx), 0);
|
||||
}
|
||||
|
||||
/// A span that sizes from its children passes their `leftover` weight up
|
||||
@@ -341,16 +437,15 @@ fn a_row_of_equal_shares_fills_it_exactly() {
|
||||
}
|
||||
}
|
||||
|
||||
/// Where the shader puts an edge: the two parts of a scalar are floored
|
||||
/// apart, so a fraction and a pixel offset snap independently, and each is
|
||||
/// taken to the boundary it composes to within half a step of. Kept in step
|
||||
/// with `snap_floor` in `prelude.wgsl`.
|
||||
/// Where the shader puts an edge: the fraction resolved against the window
|
||||
/// plus the pixel offset, taken to the boundary it composes to within half
|
||||
/// a step of. Kept in step with `snap_floor` in `prelude.wgsl`.
|
||||
fn drawn_edges(h: &Harness, id: WidgetId, axis: Axis) -> (f32, f32) {
|
||||
let active = &h.render.active[&id];
|
||||
let region = h.render.moves.resolve(active.parent_move, active.region);
|
||||
let dim = h.size().axis(axis);
|
||||
let snap = |v: f32| (v + Px::STEP.to_f32() * 0.5).floor();
|
||||
let edge = |s: Len| snap(s.rel.to_f32() * dim) + snap(s.px.to_f32());
|
||||
let edge = |s: Len| snap(s.rel.to_f32() * dim + s.px.to_f32());
|
||||
let span = region.axis(axis);
|
||||
(edge(span.start), edge(span.end))
|
||||
}
|
||||
|
||||
+16
-1
@@ -462,7 +462,7 @@ fn a_change_two_levels_under_its_reader_still_reaches_it() {
|
||||
let (leaf, _) = counted(&mut h, Size::px((100, 100).into()), true);
|
||||
let padded = leaf.pad(10).add(&mut h.rsc);
|
||||
let below = rect(Color::RED).add(&mut h.rsc);
|
||||
h.set_root((padded, below).span(Dir::DOWN).pad(12));
|
||||
h.set_root((padded, below).span(Dir::DOWN).inset(12));
|
||||
assert_corners!(h, below, (12, 132), (388, 388));
|
||||
|
||||
h.rsc[leaf].size = Size::px((100, 200).into());
|
||||
@@ -613,3 +613,18 @@ fn a_stacks_sizing_child_is_drawn_once_where_it_belongs() {
|
||||
assert_ne!(layer(front.id()), layer(background.id()));
|
||||
assert_eq!(draws.get(), 1);
|
||||
}
|
||||
|
||||
/// A widget's own mask is not the one it inherited, and a redraw of it
|
||||
/// inherits the second: handing back the first is handing it its own mask to
|
||||
/// set a second time, which `set_mask` asserts against.
|
||||
#[test]
|
||||
fn a_masked_widget_redrawn_on_its_own_sets_its_mask_again() {
|
||||
let mut h = Harness::new((400, 200));
|
||||
let inner = rect(Color::BLUE).add(&mut h.rsc);
|
||||
let masked = inner.masked().add(&mut h.rsc);
|
||||
let other = rect(Color::RED).width(100).add(&mut h.rsc);
|
||||
h.set_root((other, masked).span(Dir::RIGHT));
|
||||
h.rsc.widgets_mut().get_dyn_mut(masked.id());
|
||||
h.frame();
|
||||
assert_corners!(h, inner, (100, 0), (400, 200));
|
||||
}
|
||||
+170
-10
@@ -3,13 +3,16 @@
|
||||
//! frame that had not settled: a wrapping text shaped at a width it was
|
||||
//! measured in rather than the one it was given. The rest are a widget
|
||||
//! measured again in a box its own answer had decided, where the old answer
|
||||
//! is a fixed point whatever the content now says. The last two are neither:
|
||||
//! one box length, composed two ways, landing either side of the boundary
|
||||
//! that decided whether a child was drawn at all, and one box as long as the
|
||||
//! box a widget was offered but somewhere else.
|
||||
//! is a fixed point whatever the content now says. The last three are
|
||||
//! neither: one box length, composed two ways, landing either side of the
|
||||
//! boundary that decided whether a child was drawn at all, and two boxes
|
||||
//! reached through a region node's own entry rather than through the offer
|
||||
//! that node was given. The last is a wrapping text handed back the width
|
||||
//! it measured, rounded to a step below the line it measured there.
|
||||
|
||||
use iris::harness::Harness;
|
||||
use iris::prelude::*;
|
||||
use iris::random::Branch;
|
||||
|
||||
/// Six widgets, shrunk from a 402-widget tree the fuzzer found. Nothing about
|
||||
/// the tree changes -- every widget is marked for redraw and the frame is
|
||||
@@ -428,12 +431,12 @@ fn plant_nested_scrolls(h: &mut Harness) -> Vec<WidgetId> {
|
||||
vec![text.id(), inner.id(), filler.id(), span.id(), root.id()]
|
||||
}
|
||||
|
||||
/// A local redraw asks a dirty widget in the box its parent asked it in, and
|
||||
/// then again in the box its parent chose from that answer. Skipping the
|
||||
/// second ask because the two boxes are the same *length* left this inner
|
||||
/// scroll, which owns a region node, drawn at its offer. The offer is the
|
||||
/// outer scroll's whole viewport and the final box is 24px above it -- the
|
||||
/// height of the sized child the outer scroll snaps to the end of -- so the
|
||||
/// A local redraw asks a dirty widget in the box its parent gave it, and only
|
||||
/// where that box is as long as the one it was offered; anything else is a
|
||||
/// question its parent has to ask. This inner scroll's offer is the outer
|
||||
/// scroll's whole viewport and the box it was given is 24px shorter -- the
|
||||
/// height of the sized child the outer scroll snaps to the end of -- so what
|
||||
/// it must not do is settle itself. It was drawn at its offer once, and the
|
||||
/// inner scroll and its text stayed 24px too low.
|
||||
#[test]
|
||||
fn redrawing_one_widget_does_not_move_what_scrolls_around_it() {
|
||||
@@ -454,3 +457,160 @@ fn redrawing_one_widget_does_not_move_what_scrolls_around_it() {
|
||||
}
|
||||
assert!(wrong.is_empty(), "{}", wrong.join("\n"));
|
||||
}
|
||||
|
||||
/// Ten widgets, of the shape `tests/shrink.rs` reduces the oracle's seed 220
|
||||
/// to. The pad owns a movable region and is the scroll's content, so the box
|
||||
/// the scroll places it in is as long as that content while the box it was
|
||||
/// offered is the viewport -- and with no padding to tell those two apart,
|
||||
/// the span inside it looked like it was still at its offer. So everything
|
||||
/// under the pad was asked again in the *placed* box, the offer resolving
|
||||
/// against the node's own entry, which holds that box: the texts kept the
|
||||
/// widths they had, the content stayed the length those widths make, and the
|
||||
/// old answer confirmed itself. What the branch adds is a tree that differs
|
||||
/// rather than a box that moved, since a probe measured at the wrong width
|
||||
/// takes the other side.
|
||||
fn plant_under_a_node(h: &mut Harness, swapped: bool) -> (Vec<WidgetId>, [WeakWidget<Span>; 2]) {
|
||||
let probe = rect(Color::RED).add(&mut h.rsc);
|
||||
let wide = rect(Color::GREEN).add(&mut h.rsc);
|
||||
let narrow = rect(Color::BLUE).add(&mut h.rsc);
|
||||
let branch = Branch {
|
||||
probe: probe.add_strong(&mut h.rsc),
|
||||
wide: wide.add_strong(&mut h.rsc),
|
||||
narrow: narrow.add_strong(&mut h.rsc),
|
||||
threshold: 213.0,
|
||||
}
|
||||
.add(&mut h.rsc);
|
||||
let wrapped = wtext(
|
||||
"Wrapping shapes one source into as many lines as the box \
|
||||
leaves room for, so a paragraph's height is an answer and not a setting.",
|
||||
)
|
||||
.size(16)
|
||||
.wrap(true)
|
||||
.add(&mut h.rsc);
|
||||
let plain = wtext("one line, overflowing whatever it is given")
|
||||
.size(16)
|
||||
.wrap(false)
|
||||
.add(&mut h.rsc);
|
||||
let row = |h: &mut Harness, mut children: Vec<StrongWidget>| {
|
||||
if swapped {
|
||||
children.rotate_left(1);
|
||||
}
|
||||
Span {
|
||||
children,
|
||||
dir: Dir::RIGHT,
|
||||
gap: Px::ZERO,
|
||||
}
|
||||
.add(&mut h.rsc)
|
||||
};
|
||||
let texts: Vec<StrongWidget> =
|
||||
vec![wrapped.add_strong(&mut h.rsc), plain.add_strong(&mut h.rsc)];
|
||||
let inner = row(h, texts);
|
||||
let pair: Vec<StrongWidget> = vec![branch.add_strong(&mut h.rsc), inner.add_strong(&mut h.rsc)];
|
||||
let outer = row(h, pair);
|
||||
let pad = Pad {
|
||||
padding: Padding::ZERO,
|
||||
inner: outer.add_strong(&mut h.rsc),
|
||||
}
|
||||
.add(&mut h.rsc);
|
||||
h.rsc.widgets_mut().set_region_node(pad.id(), true);
|
||||
let root = Scroll::new(pad.add_strong(&mut h.rsc), Axis::X).add(&mut h.rsc);
|
||||
h.set_root(root);
|
||||
(
|
||||
vec![
|
||||
probe.id(),
|
||||
wide.id(),
|
||||
narrow.id(),
|
||||
branch.id(),
|
||||
wrapped.id(),
|
||||
plain.id(),
|
||||
inner.id(),
|
||||
outer.id(),
|
||||
pad.id(),
|
||||
root.id(),
|
||||
],
|
||||
[outer, inner],
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_widget_under_a_region_node_is_asked_in_the_box_that_node_was_offered() {
|
||||
let mut warm = Harness::new((900, 1200));
|
||||
let (ids, spans) = plant_under_a_node(&mut warm, false);
|
||||
warm.frame();
|
||||
for span in spans {
|
||||
warm.rsc[span].children.rotate_left(1);
|
||||
}
|
||||
warm.frame();
|
||||
|
||||
let mut cold = Harness::new((900, 1200));
|
||||
let (cold_ids, _) = plant_under_a_node(&mut cold, true);
|
||||
cold.frame();
|
||||
|
||||
let mut wrong = Vec::new();
|
||||
for (i, (&w, &c)) in ids.iter().zip(&cold_ids).enumerate() {
|
||||
let (got, want) = (warm.region(&w), cold.region(&c));
|
||||
if got != want {
|
||||
wrong.push(format!("widget {i}: warm {got:?} cold {want:?}"));
|
||||
}
|
||||
}
|
||||
assert!(wrong.is_empty(), "{}", wrong.join("\n"));
|
||||
}
|
||||
|
||||
const PARAGRAPH: &str = "Wrapping shapes one source into as many lines as the \
|
||||
box leaves room for, so a paragraph's height is an answer and not a setting.";
|
||||
|
||||
/// Eight widgets, shrunk from a 118-widget tree (seed 1121, depth 4,
|
||||
/// `shuffle-swap-for-three`). The stack takes its size from the span above,
|
||||
/// the span takes its width from the longest line of the texts in it, and
|
||||
/// the text below the span is then wrapped at that width -- so a width the
|
||||
/// shaper measured comes back to it as the box to break in.
|
||||
fn plant_a_measured_width(h: &mut Harness, swapped: bool) -> (WeakWidget<Span>, WidgetId) {
|
||||
let first: StrongWidget = rect(Color::YELLOW).add_strong(&mut h.rsc);
|
||||
let mut inner = Span::empty(Dir::UP);
|
||||
inner.children = match swapped {
|
||||
true => swapped_in(h),
|
||||
false => vec![first],
|
||||
};
|
||||
let inner = inner.height(142).add(&mut h.rsc);
|
||||
let text = wtext(PARAGRAPH).size(16).wrap(true).add(&mut h.rsc);
|
||||
let stack = Stack {
|
||||
children: vec![inner.add_strong(&mut h.rsc), text.add_strong(&mut h.rsc)],
|
||||
size: StackSize::Child(0),
|
||||
}
|
||||
.add(&mut h.rsc);
|
||||
h.set_root((stack,).span(Dir::DOWN).width(195));
|
||||
(inner, text.id())
|
||||
}
|
||||
|
||||
/// What the span holds once its children have been swapped, which is what
|
||||
/// the warm tree is changed to and what the cold one is grown with.
|
||||
fn swapped_in(h: &mut Harness) -> Vec<StrongWidget> {
|
||||
let paragraph = |h: &mut Harness| -> StrongWidget {
|
||||
wtext(PARAGRAPH).size(16).wrap(true).add_strong(&mut h.rsc)
|
||||
};
|
||||
vec![
|
||||
paragraph(h),
|
||||
rect(Color::YELLOW).add_strong(&mut h.rsc),
|
||||
paragraph(h),
|
||||
]
|
||||
}
|
||||
|
||||
/// A text handed back the width it measured breaks there the way it broke
|
||||
/// when it measured it. The width the shaper answers is not on the grid, and
|
||||
/// a report rounded to the nearest step is under the longest line half the
|
||||
/// time: a warm tree then keeps a break made in a wider box while a cold one
|
||||
/// makes a narrower break in the same box, and the paragraph gains a line.
|
||||
#[test]
|
||||
fn a_text_is_given_back_a_box_the_line_it_measured_fits_in() {
|
||||
let mut warm = Harness::new((900, 1200));
|
||||
let (inner, text) = plant_a_measured_width(&mut warm, false);
|
||||
warm.frame();
|
||||
warm.rsc[inner].children = swapped_in(&mut warm);
|
||||
warm.frame();
|
||||
|
||||
let mut cold = Harness::new((900, 1200));
|
||||
let (_, cold_text) = plant_a_measured_width(&mut cold, true);
|
||||
cold.frame();
|
||||
|
||||
assert_eq!(warm.region(&text), cold.region(&cold_text));
|
||||
}
|
||||
+13
-5
@@ -42,11 +42,19 @@ const OUTER: (f32, f32) = (1920.0, 1200.0);
|
||||
const INNER: (f32, f32) = (640.0, 900.0);
|
||||
const STILL: (f32, f32) = (900.0, 1200.0);
|
||||
|
||||
/// The same box, to a step of the grid per level of nesting between the two
|
||||
/// ways of reaching it. A move, a repaint and a row of shares land on the
|
||||
/// same number; what is left is a box centred in a fraction of its parent
|
||||
/// against the same box centred in its own pixels. A step is a thousandth of
|
||||
/// a pixel, where this was a twentieth of one before any of it was on a grid.
|
||||
/// The same box, to two steps of the grid between the two ways of reaching
|
||||
/// it. A move, a repaint, a row of shares and every length in pixels land on
|
||||
/// the same number. What needs the slack is a position: a box centred in a
|
||||
/// fraction of its parent against the same box centred in its own pixels,
|
||||
/// and a box re-expressed as a fraction of a parent that changed length.
|
||||
/// A step is a thousandth of a pixel, where this was a twentieth of one
|
||||
/// before any of it was on a grid.
|
||||
///
|
||||
/// **One step is not enough**, tried 2026-09-17 once a length in pixels
|
||||
/// stopped being composed: it passes the 100-seed oracle and fails the
|
||||
/// 400-seed shrinker on `resize-size`, seeds 384 and 162, by 0.002 px. So
|
||||
/// what is left here is the resize path's own rounding rather than a length
|
||||
/// reached two ways.
|
||||
const AGREE_STEPS: i32 = 2;
|
||||
|
||||
/// A way of changing what a span holds. Each is a shape worth its own case:
|
||||
|
||||
Reference in new issue
Block a user