Retain child frames relative to the container extent
This commit is contained in:
1 parent
a7307d95fd
commit
c44bd198ee
6 files changed
+316
-62
No files matched your search
+74
-35
@@ -1,9 +1,9 @@
|
||||
#[cfg(feature = "layout-diagnostics")]
|
||||
use crate::layout_diagnostics::{self as diag, Counter};
|
||||
use crate::{
|
||||
Axis, DrawRegion, Holds, LayoutLen, Len, Px, PxVec2, RegionAlign, RenderedText,
|
||||
RetainedPrimitive, Size, StrongWidget, TextAttrs, TextBuffer, TextData, TextureHandle,
|
||||
UiRegion, UiRenderState, UiRsc, UiSpan, UiVec2, Weight, WidgetId, Widgets,
|
||||
Axis, DrawRegion, ExtentPlacement, Holds, LayoutLen, Len, Px, PxVec2, RegionAlign,
|
||||
RenderedText, RetainedPrimitive, Size, StrongWidget, TextAttrs, TextBuffer, TextData,
|
||||
TextureHandle, UiRegion, UiRenderState, UiRsc, UiSpan, UiVec2, Weight, WidgetId, Widgets,
|
||||
render::{
|
||||
GlyphPrimitive, Mask, MaskIdx, MoveIdx, Primitive, PrimitiveInst, PrimitiveKind,
|
||||
TexturePrimitive,
|
||||
@@ -40,7 +40,7 @@ pub struct Painter<'a> {
|
||||
pub(super) textures: Vec<TextureHandle>,
|
||||
pub(super) primitives: Vec<RetainedPrimitive>,
|
||||
pub(super) mask_region: Option<DrawRegion>,
|
||||
pub(super) inherited_children: Vec<WidgetId>,
|
||||
pub(super) extent_children: Vec<(WidgetId, ExtentPlacement)>,
|
||||
pub(super) extent_own: [Holds; 2],
|
||||
pub(super) extent_under: [Holds; 2],
|
||||
pub(super) children: Vec<WidgetId>,
|
||||
@@ -154,7 +154,13 @@ impl<'a> Painter<'a> {
|
||||
/// around one child wants, since its box is the child's.
|
||||
pub fn widget<'s, W: ?Sized>(&'s mut self, id: &'s StrongWidget<W>) -> DrawResult<'s, 'a, W> {
|
||||
let own = self.placement;
|
||||
self.widget_at_inner(id, UiRegion::FULL, [Some(own.x), Some(own.y)], true, false)
|
||||
self.widget_at_inner(
|
||||
id,
|
||||
UiRegion::FULL,
|
||||
[Some(own.x), Some(own.y)],
|
||||
Some(ExtentPlacement::Inherit),
|
||||
false,
|
||||
)
|
||||
}
|
||||
|
||||
/// What a widget's rules declare its lengths to be, which whoever draws
|
||||
@@ -170,20 +176,29 @@ impl<'a> Painter<'a> {
|
||||
/// this frame; what it answered is still something this widget asked.
|
||||
pub fn undraw<W: ?Sized>(&mut self, id: &StrongWidget<W>) {
|
||||
self.children.retain(|child| *child != id.id());
|
||||
self.inherited_children.retain(|child| *child != id.id());
|
||||
self.extent_children.retain(|(child, _)| *child != id.id());
|
||||
self.state.undraw_rec(id.id(), self.rsc);
|
||||
}
|
||||
|
||||
/// Draws a widget somewhere within this one. `region` is in this widget's
|
||||
/// own region, and is the child's own region: what its declared lengths
|
||||
/// and its report are fractions of. Where its drawing sits inside that is
|
||||
/// its own answer placed by its alignment.
|
||||
/// Draws a child in a frame relative to this widget's frame or extent.
|
||||
/// A plain `UiRegion` is frame-relative. `DrawRegion::Extent` keeps the
|
||||
/// child's frame attached to the extent without reading `placement()`.
|
||||
/// The child places its answer within that frame by its own alignment.
|
||||
pub fn widget_within<'s, W: ?Sized>(
|
||||
&'s mut self,
|
||||
id: &'s StrongWidget<W>,
|
||||
region: UiRegion,
|
||||
region: impl Into<DrawRegion>,
|
||||
) -> DrawResult<'s, 'a, W> {
|
||||
self.widget_at(id, region, [None; 2])
|
||||
match region.into() {
|
||||
DrawRegion::Frame(region) => self.widget_at(id, region, [None; 2]),
|
||||
DrawRegion::Extent(local) => self.widget_at_inner(
|
||||
id,
|
||||
local.within(&self.placement),
|
||||
[None; 2],
|
||||
Some(ExtentPlacement::Within(local)),
|
||||
false,
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
/// Draws a widget in `region`, saying where in it the drawing goes.
|
||||
@@ -206,7 +221,7 @@ impl<'a> Painter<'a> {
|
||||
region: UiRegion,
|
||||
placement: [Option<UiSpan>; 2],
|
||||
) -> DrawResult<'s, 'a, W> {
|
||||
self.widget_at_inner(id, region, placement, false, false)
|
||||
self.widget_at_inner(id, region, placement, None, false)
|
||||
}
|
||||
|
||||
fn widget_at_inner<'s, W: ?Sized>(
|
||||
@@ -214,15 +229,12 @@ impl<'a> Painter<'a> {
|
||||
id: &'s StrongWidget<W>,
|
||||
region: UiRegion,
|
||||
placement: [Option<UiSpan>; 2],
|
||||
inherited: bool,
|
||||
extent: Option<ExtentPlacement>,
|
||||
measuring: bool,
|
||||
) -> DrawResult<'s, 'a, W> {
|
||||
if inherited {
|
||||
if !self.inherited_children.contains(&id.id()) {
|
||||
self.inherited_children.push(id.id());
|
||||
}
|
||||
} else {
|
||||
self.inherited_children.retain(|child| *child != id.id());
|
||||
self.extent_children.retain(|(child, _)| *child != id.id());
|
||||
if let Some(extent) = extent {
|
||||
self.extent_children.push((id.id(), extent));
|
||||
}
|
||||
let region_node = self.rsc.widgets().is_region_node(id.id());
|
||||
let declared = self.declared_lens(id);
|
||||
@@ -285,28 +297,53 @@ impl<'a> Painter<'a> {
|
||||
measuring,
|
||||
self.rsc,
|
||||
);
|
||||
// Whatever the child's answer holds for keeps this one to the boxes
|
||||
// that give the child a length inside it.
|
||||
for axis in AXES {
|
||||
let n = axis as usize;
|
||||
let frame = holds.frame[n].through(local.axis(axis).len());
|
||||
self.under[n] = self.under[n].and(frame);
|
||||
if inherited && declared[n].is_none() {
|
||||
self.extent_under[n] = self.extent_under[n].and(holds.extent[n]);
|
||||
self.reads_placement |= holds.placement.is_some();
|
||||
} else {
|
||||
let extent = placement[n].unwrap_or(UiSpan::FULL).len();
|
||||
self.under[n] = self.under[n].and(
|
||||
holds.extent[n]
|
||||
.through(extent)
|
||||
.through(local.axis(axis).len()),
|
||||
);
|
||||
match extent {
|
||||
Some(ExtentPlacement::Inherit) if declared[n].is_none() => {
|
||||
self.under[n] =
|
||||
self.under[n].and(holds.frame[n].through(local.axis(axis).len()));
|
||||
self.extent_under[n] = self.extent_under[n].and(holds.extent[n]);
|
||||
self.reads_placement |= holds.placement.is_some();
|
||||
}
|
||||
Some(ExtentPlacement::Within(part))
|
||||
if declared[n].is_none()
|
||||
&& part.axis(axis).start.rel == crate::Rel::ZERO
|
||||
&& part.axis(axis).end.rel == crate::Rel::ONE =>
|
||||
{
|
||||
let dependent = holds.frame[n]
|
||||
.and(holds.extent[n])
|
||||
.through(part.axis(axis).len());
|
||||
self.extent_under[n] = self.extent_under[n].and(dependent);
|
||||
}
|
||||
_ => {
|
||||
let chosen = placement[n].unwrap_or(UiSpan::FULL).len();
|
||||
self.under[n] = self.under[n]
|
||||
.and(holds.frame[n].through(local.axis(axis).len()))
|
||||
.and(
|
||||
holds.extent[n]
|
||||
.through(chosen)
|
||||
.through(local.axis(axis).len()),
|
||||
);
|
||||
// Fractional endpoints compose before pixel evaluation. Their
|
||||
// difference cannot be inverted through the extent length alone.
|
||||
if matches!(extent, Some(ExtentPlacement::Within(_))) && declared[n].is_none() {
|
||||
self.reads_placement = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// A fractional report is composed into the parent frame, so its
|
||||
// value can change with the extent even when the drawing holds.
|
||||
let reads_placement = matches!(extent, Some(ExtentPlacement::Within(_)))
|
||||
&& AXES.into_iter().any(|axis| {
|
||||
declared[axis as usize].is_none() && size.axis(axis).rel != crate::Rel::ZERO
|
||||
});
|
||||
DrawResult {
|
||||
child: id,
|
||||
painter: self,
|
||||
size: in_parent_frame(size, local.size(), declared),
|
||||
reads_placement,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -363,7 +400,7 @@ impl<'a> Painter<'a> {
|
||||
.retained_size(child.id(), px, placement, self.move_idx, self.rsc.widgets());
|
||||
let Some((size, holds)) = retained else {
|
||||
return self
|
||||
.widget_at_inner(child, region, offered, false, true)
|
||||
.widget_at_inner(child, region, offered, None, true)
|
||||
.len(axis);
|
||||
};
|
||||
#[cfg(feature = "layout-diagnostics")]
|
||||
@@ -604,6 +641,7 @@ pub struct DrawResult<'p, 'a, W: ?Sized> {
|
||||
painter: &'p mut Painter<'a>,
|
||||
child: &'p StrongWidget<W>,
|
||||
size: Size,
|
||||
reads_placement: bool,
|
||||
}
|
||||
|
||||
impl<W: ?Sized> DrawResult<'_, '_, W> {
|
||||
@@ -614,6 +652,7 @@ impl<W: ?Sized> DrawResult<'_, '_, W> {
|
||||
diag::size_read(self.child.id(), self.painter.id, self.size);
|
||||
}
|
||||
self.painter.depend_on(self.child);
|
||||
self.painter.reads_placement |= self.reads_placement;
|
||||
self.size
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user