thing-core: a Cells control's form is read off its laid rect in one solve, through a grid shell
Deletes the re-solve loop, mark_too_short, the compact marks and render's Bordered fallback; Device::form is the one form rule, Builder::shell the one Cells control box. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
baa358ccff
commit
eb57a4281a
4 changed files with 119 additions and 85 deletions
|
|
@ -177,6 +177,12 @@ impl Device {
|
|||
if self.cells && px > 0.0 { length.max(1.0) } else { length }
|
||||
}
|
||||
|
||||
/// A control's form, read off its laid border-box height `h` and never fed back to the solve: in
|
||||
/// Pixels always `Bordered`; in Cells `Bordered` from `BORDERED_ROWS` rows, `Compact` below.
|
||||
pub(super) fn form(self, h: f32) -> ControlForm {
|
||||
if !self.cells || h >= BORDERED_ROWS { ControlForm::Bordered } else { ControlForm::Compact }
|
||||
}
|
||||
|
||||
/// The inset of a laid control's content from its border box, `(x, y)`, in device units: the one
|
||||
/// rule, owned here. In Pixels: the 2 px of chrome. In Cells a `Bordered` control keeps one cell
|
||||
/// clear on every side, a `Compact` one a cell on each side across and none down. Never more than
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ mod style;
|
|||
mod tree;
|
||||
|
||||
use taffy::{AvailableSpace, Size, compute_leaf_layout};
|
||||
use measure::{BORDERED_ROWS, Device};
|
||||
use measure::Device;
|
||||
use thing_contract::{NodeId, OverlayNode};
|
||||
|
||||
pub use measure::{Avail, CellStyle, Constraint, FontId, Glyph, Leaf, Measure, Run, TextLayout, TextLine, TextRequest};
|
||||
|
|
@ -76,8 +76,12 @@ pub struct LaidNode {
|
|||
|
||||
/// The two forms of a control. In Pixels every control is `Bordered` (render draws chrome behind its
|
||||
/// content). In Cells a control laid 3 rows or taller is `Bordered`: its border stands in the edge
|
||||
/// cells and its content keeps one cell clear on every side. A control laid shorter keeps exactly
|
||||
/// that height as `Compact`: its one-row affordance on the first row, one cell each side.
|
||||
/// cells and its content keeps one cell clear on every side. A control laid shorter is `Compact`: its
|
||||
/// one-row affordance on the first row, one cell each side.
|
||||
///
|
||||
/// The form is a function of the laid border-box height alone (`Device::form`), read after the one
|
||||
/// solve. A content-sized control is at least 3 rows because its border rows are in its max-content
|
||||
/// height, not its min-content height: a short container still squeezes it to one compact row.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum ControlForm {
|
||||
Bordered,
|
||||
|
|
@ -148,21 +152,6 @@ pub fn layout(tree: &BoundTree, frame: &LayoutFrame, measure: &mut dyn Measure)
|
|||
Ok(LaidTree { nodes, diagnostics })
|
||||
}
|
||||
|
||||
/// Mark every `Bordered` control `built` laid under `BORDERED_ROWS` rows as `Compact`, by entry
|
||||
/// index. Returns whether any was marked: a marked control is built and solved again.
|
||||
fn mark_too_short(built: &tree::Built, compact: &mut Vec<bool>) -> bool {
|
||||
compact.resize(built.entries.len(), false);
|
||||
let mut marked = false;
|
||||
for (i, entry) in built.entries.iter().enumerate() {
|
||||
let rows = built.taffy.layout(entry.node).expect("taffy: node is in the tree").size.height;
|
||||
if entry.control.is_some() && !compact[i] && rows < BORDERED_ROWS {
|
||||
compact[i] = true;
|
||||
marked = true;
|
||||
}
|
||||
}
|
||||
marked
|
||||
}
|
||||
|
||||
/// Run the one solve over a built tree in `size` device px.
|
||||
fn compute(built: &mut tree::Built, dev: Device, size: (f32, f32), measure: &mut dyn Measure) {
|
||||
let tree::Built { taffy, root, entries, leaves, .. } = built;
|
||||
|
|
@ -194,10 +183,7 @@ fn compute(built: &mut tree::Built, dev: Device, size: (f32, f32), measure: &mut
|
|||
/// Build and solve one Taffy tree in `size` device px, its origin at `origin`, and append its laid
|
||||
/// nodes (absolute, in entry order) and its diagnostics. Returns the Overlays the tree met in flow.
|
||||
///
|
||||
/// In Cells a control is first laid `Bordered`; one the solve leaves under `BORDERED_ROWS` rows
|
||||
/// cannot hold a border and a content row, so the tree is built again with it `Compact` and solved
|
||||
/// afresh, until no bordered control is too short. A bordered control's padding floors its height
|
||||
/// at 2 rows, which is why the form is found by solving and not read off a declared height.
|
||||
/// One build, one solve: a control's form is read off the height Taffy laid it, never fed back.
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn solve<'a>(
|
||||
tree: &'a BoundTree,
|
||||
|
|
@ -209,14 +195,8 @@ fn solve<'a>(
|
|||
nodes: &mut Vec<LaidNode>,
|
||||
diagnostics: &mut Vec<Diagnostic>,
|
||||
) -> Result<Vec<&'a OverlayNode>, LayoutError> {
|
||||
let mut compact: Vec<bool> = Vec::new();
|
||||
let mut built = loop {
|
||||
let mut built = tree::build(tree, dev, top, size, measure, &compact);
|
||||
compute(&mut built, dev, size, measure);
|
||||
if !dev.cells() || !mark_too_short(&built, &mut compact) {
|
||||
break built;
|
||||
}
|
||||
};
|
||||
let mut built = tree::build(tree, dev, top, size, measure);
|
||||
compute(&mut built, dev, size, measure);
|
||||
let tree::Built { taffy, entries, diagnostics: own, overlays, .. } = &mut built;
|
||||
let base = nodes.len();
|
||||
let mut absolute: Vec<(f32, f32)> = Vec::with_capacity(entries.len());
|
||||
|
|
@ -224,8 +204,12 @@ fn solve<'a>(
|
|||
let laid = taffy.layout(entry.node).expect("taffy: node is in the tree");
|
||||
let (px, py) = entry.parent.map_or(origin, |parent| absolute[parent]);
|
||||
let (x, y) = (px + laid.location.x, py + laid.location.y);
|
||||
absolute.push((x, y));
|
||||
let (w, h) = (laid.size.width, laid.size.height);
|
||||
let form = dev.form(h);
|
||||
let (ix, iy) = entry.control.filter(|c| c.inset).map_or((0.0, 0.0), |_| dev.inset(form, w, h));
|
||||
// The origin the children are placed from: a control's content node sits where the inset
|
||||
// says, not where Taffy put it.
|
||||
absolute.push(if entry.control.is_some_and(|c| c.inner.is_some()) { (x + ix, y + iy) } else { (x, y) });
|
||||
// Taffy's content extent is measured from the padding box. `content` is never smaller than
|
||||
// the border box; `max_scroll` is what the content reaches past the padding box.
|
||||
let reached = entry.scroll.then(|| {
|
||||
|
|
@ -256,9 +240,9 @@ fn solve<'a>(
|
|||
w: clean(w - laid.border.left - laid.border.right),
|
||||
h: clean(h - laid.border.top - laid.border.bottom),
|
||||
});
|
||||
let control = entry.control.map(|form| {
|
||||
let (ix, iy) = if entry.inset { dev.inset(form, w, h) } else { (0.0, 0.0) };
|
||||
LaidControl { form, content: Rect { x: clean(x + ix), y: clean(y + iy), w: clean(w - 2.0 * ix), h: clean(h - 2.0 * iy) } }
|
||||
let control = entry.control.map(|_| LaidControl {
|
||||
form,
|
||||
content: Rect { x: clean(x + ix), y: clean(y + iy), w: clean(w - 2.0 * ix), h: clean(h - 2.0 * iy) },
|
||||
});
|
||||
nodes.push(LaidNode {
|
||||
id: entry.id.clone(),
|
||||
|
|
|
|||
|
|
@ -20,8 +20,15 @@ pub(super) struct Entry<'a> {
|
|||
pub(super) clip: bool,
|
||||
pub(super) scroll: bool,
|
||||
/// A control (Button, TextInput, Select, Toggle): its form and content box are published.
|
||||
pub(super) control: Option<ControlForm>,
|
||||
/// Whether layout reserves the control an inset (every control but a Pixels Button).
|
||||
pub(super) control: Option<Control>,
|
||||
}
|
||||
|
||||
/// What `solve` needs to state a control: whether layout reserves it an inset (every control but a
|
||||
/// Pixels Button) and, in Cells, the content node inside its shell whose children are placed from
|
||||
/// the inset origin.
|
||||
#[derive(Clone, Copy)]
|
||||
pub(super) struct Control {
|
||||
pub(super) inner: Option<taffy::NodeId>,
|
||||
pub(super) inset: bool,
|
||||
}
|
||||
|
||||
|
|
@ -41,8 +48,6 @@ struct Builder<'a, 'm> {
|
|||
surface: String,
|
||||
dev: Device,
|
||||
measure: &'m mut dyn super::Measure,
|
||||
/// The entries (by index, in document order) of the controls laid in the compact form.
|
||||
compact: &'m [bool],
|
||||
taffy: TaffyTree<usize>,
|
||||
entries: Vec<Entry<'a>>,
|
||||
leaves: Vec<Leaf<'a>>,
|
||||
|
|
@ -70,7 +75,6 @@ pub(super) fn build<'a>(
|
|||
top: Top<'a>,
|
||||
size: (f32, f32),
|
||||
measure: &mut dyn super::Measure,
|
||||
compact: &[bool],
|
||||
) -> Built<'a> {
|
||||
let document = tree.resolved().document();
|
||||
let mut builder = Builder {
|
||||
|
|
@ -78,7 +82,6 @@ pub(super) fn build<'a>(
|
|||
surface: document.record_key(),
|
||||
dev,
|
||||
measure,
|
||||
compact,
|
||||
taffy: TaffyTree::new(),
|
||||
entries: Vec::new(),
|
||||
leaves: Vec::new(),
|
||||
|
|
@ -121,17 +124,32 @@ pub(super) fn build<'a>(
|
|||
}
|
||||
|
||||
impl<'a> Builder<'a, '_> {
|
||||
/// The form entry `index` takes: compact when the solve found it laid too short to be bordered.
|
||||
fn form(&self, index: usize) -> ControlForm {
|
||||
if self.compact.get(index).copied().unwrap_or(false) { ControlForm::Compact } else { ControlForm::Bordered }
|
||||
}
|
||||
|
||||
/// A control's padding in Cells: one cell across where its brackets or border stand, and one down
|
||||
/// where a border stands (a compact form has none).
|
||||
fn chrome_padding(&self, index: usize) -> taffy::Rect<taffy::LengthPercentage> {
|
||||
let (across, down) = self.dev.inset(self.form(index), f32::INFINITY, f32::INFINITY);
|
||||
let (across, down) = (taffy::LengthPercentage::length(across), taffy::LengthPercentage::length(down));
|
||||
taffy::Rect { left: across, right: across, top: down, bottom: down }
|
||||
/// The shell of a Cells control: a grid of three rows (an edge row of up to one row above and
|
||||
/// below, the content row between) one cell in on each side, the inset rule's numbers
|
||||
/// (`Device::inset`). The content node takes the middle row and at least one row of it. The edge
|
||||
/// rows give way first, so the shell lays anywhere from one row up and the form is read off the
|
||||
/// height it ends with (`Device::form`).
|
||||
fn shell(&mut self, content: taffy::NodeId) -> taffy::NodeId {
|
||||
use taffy::style_helpers::{auto, length, line, minmax};
|
||||
let (across, down) = self.dev.inset(ControlForm::Bordered, f32::INFINITY, f32::INFINITY);
|
||||
let mut seat = self.taffy.style(content).expect("taffy: node is in the tree").clone();
|
||||
seat.min_size.height = taffy::LengthPercentageAuto::length(1.0);
|
||||
seat.grid_row = taffy::Line { start: line(2), end: auto() };
|
||||
self.taffy.set_style(content, seat).expect("taffy: style");
|
||||
let edge = || -> taffy::GridTemplateComponent<String> { minmax::<taffy::TrackSizingFunction>(length(0.0), length(down)).into() };
|
||||
let content_row: taffy::GridTemplateComponent<String> = minmax::<taffy::TrackSizingFunction>(auto(), auto()).into();
|
||||
let style = Style {
|
||||
display: taffy::Display::Grid,
|
||||
grid_template_rows: vec![edge(), content_row, edge()],
|
||||
padding: taffy::Rect {
|
||||
left: taffy::LengthPercentage::length(across),
|
||||
right: taffy::LengthPercentage::length(across),
|
||||
top: taffy::LengthPercentage::length(0.0),
|
||||
bottom: taffy::LengthPercentage::length(0.0),
|
||||
},
|
||||
..Style::default()
|
||||
};
|
||||
self.taffy.new_with_children(style, &[content]).expect("taffy: insert")
|
||||
}
|
||||
|
||||
fn hidden(&self, id: &NodeId) -> bool {
|
||||
|
|
@ -156,7 +174,7 @@ impl<'a> Builder<'a, '_> {
|
|||
fn entry(&mut self, id: &'a NodeId, parent: Option<usize>, overflow: &Overflow) -> usize {
|
||||
let clip = !matches!(overflow, Overflow::Visible);
|
||||
let scroll = matches!(overflow, Overflow::Scroll);
|
||||
self.entries.push(Entry { id, node: taffy::NodeId::from(0u64), parent, clip, scroll, control: None, inset: true });
|
||||
self.entries.push(Entry { id, node: taffy::NodeId::from(0u64), parent, clip, scroll, control: None });
|
||||
self.entries.len() - 1
|
||||
}
|
||||
|
||||
|
|
@ -177,17 +195,18 @@ impl<'a> Builder<'a, '_> {
|
|||
}
|
||||
}
|
||||
let control = matches!(leaf, Leaf::TextInput { .. } | Leaf::Toggle { .. } | Leaf::Select { .. });
|
||||
let form = self.form(self.entries.len());
|
||||
if control && self.dev.cells() {
|
||||
style.padding = self.chrome_padding(self.entries.len());
|
||||
}
|
||||
let index = self.entry(id, parent, &Overflow::Visible);
|
||||
if control {
|
||||
self.entries[index].control = Some(form);
|
||||
}
|
||||
let context = self.leaves.len();
|
||||
self.leaves.push(leaf);
|
||||
let node = self.taffy.new_leaf_with_context(style, context).expect("taffy: insert");
|
||||
let mut node = self.taffy.new_leaf_with_context(style, context).expect("taffy: insert");
|
||||
if control {
|
||||
// In Cells the leaf sits in its shell, which is the control's laid node.
|
||||
let inner = self.dev.cells().then_some(node);
|
||||
if inner.is_some() {
|
||||
node = self.shell(node);
|
||||
}
|
||||
self.entries[index].control = Some(Control { inner, inset: true });
|
||||
}
|
||||
self.entries[index].node = node;
|
||||
out.push(node);
|
||||
}
|
||||
|
|
@ -196,12 +215,27 @@ impl<'a> Builder<'a, '_> {
|
|||
&mut self,
|
||||
id: &'a NodeId,
|
||||
parent: Option<usize>,
|
||||
(style, invalid): (Style, Invalid),
|
||||
style: (Style, Invalid),
|
||||
overflow: &Overflow,
|
||||
main: Axis,
|
||||
children: &'a [Node],
|
||||
out: &mut Vec<taffy::NodeId>,
|
||||
) {
|
||||
let (index, node) = self.contain(id, parent, style, overflow, main, children);
|
||||
self.entries[index].node = node;
|
||||
out.push(node);
|
||||
}
|
||||
|
||||
/// The entry and Taffy node of a container, its children visited; the caller seats the node.
|
||||
fn contain(
|
||||
&mut self,
|
||||
id: &'a NodeId,
|
||||
parent: Option<usize>,
|
||||
(style, invalid): (Style, Invalid),
|
||||
overflow: &Overflow,
|
||||
main: Axis,
|
||||
children: &'a [Node],
|
||||
) -> (usize, taffy::NodeId) {
|
||||
self.report(id, invalid);
|
||||
let index = self.entry(id, parent, overflow);
|
||||
let mut kids = Vec::new();
|
||||
|
|
@ -210,9 +244,7 @@ impl<'a> Builder<'a, '_> {
|
|||
self.visit(child, Some(index), &mut kids);
|
||||
}
|
||||
self.main = outer;
|
||||
let node = self.taffy.new_with_children(style, &kids).expect("taffy: insert");
|
||||
self.entries[index].node = node;
|
||||
out.push(node);
|
||||
(index, self.taffy.new_with_children(style, &kids).expect("taffy: insert"))
|
||||
}
|
||||
|
||||
/// Append the Taffy nodes `node` contributes to its parent's children: one, none (hidden,
|
||||
|
|
@ -231,22 +263,22 @@ impl<'a> Builder<'a, '_> {
|
|||
}
|
||||
Node::Button(b) => {
|
||||
if !self.hidden(&b.common.id) {
|
||||
// In Cells the content keeps clear of the brackets or border on every side its
|
||||
// form has one (the inset rule, `Device::inset`), so a content-sized Button's box
|
||||
// includes its border rows. The content is a row as in Pixels: its items centred
|
||||
// between the borders of a tall bordered Button by alignment, on the first row of
|
||||
// a compact one. Pixels: render draws the chrome behind it and no inset applies.
|
||||
let index = self.entries.len();
|
||||
let form = self.form(index);
|
||||
let style = if self.dev.cells() {
|
||||
let align = if form == ControlForm::Compact { taffy::AlignItems::FLEX_START } else { taffy::AlignItems::CENTER };
|
||||
Style { align_items: align, padding: self.chrome_padding(index), ..Style::default() }
|
||||
// In Cells the Button is a shell (`Builder::shell`) around a row of its content, the
|
||||
// items centred in it, so a content-sized Button's box includes its border rows and
|
||||
// a short one squeezes to one compact row. Pixels: render draws the chrome behind
|
||||
// the content and no inset applies.
|
||||
if self.dev.cells() {
|
||||
let row = Style { align_items: taffy::AlignItems::CENTER, ..Style::default() };
|
||||
let (index, inner) = self.contain(&b.common.id, parent, (row, Vec::new()), &Overflow::Visible, Axis::X, &b.content);
|
||||
let shell = self.shell(inner);
|
||||
self.entries[index].node = shell;
|
||||
self.entries[index].control = Some(Control { inner: Some(inner), inset: true });
|
||||
out.push(shell);
|
||||
} else {
|
||||
Style::default()
|
||||
};
|
||||
self.container(&b.common.id, parent, (style, Vec::new()), &Overflow::Visible, Axis::X, &b.content, out);
|
||||
self.entries[index].control = Some(form);
|
||||
self.entries[index].inset = self.dev.cells();
|
||||
let index = self.entries.len();
|
||||
self.container(&b.common.id, parent, (Style::default(), Vec::new()), &Overflow::Visible, Axis::X, &b.content, out);
|
||||
self.entries[index].control = Some(Control { inner: None, inset: false });
|
||||
}
|
||||
}
|
||||
}
|
||||
// A Repeat has no box: its instances are its parent's children, in instance order.
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ pub use scroll::{ScrollOffsets, clamp};
|
|||
|
||||
use crate::bind::{Bound, BoundTree, Field};
|
||||
use crate::diagnostic::{Code, Diagnostic};
|
||||
use crate::layout::{ControlForm, LaidControl, LaidTree, MAX_DEVICE, Measure, Metrics, Rect};
|
||||
use crate::layout::{LaidControl, LaidTree, MAX_DEVICE, Measure, Metrics, Rect};
|
||||
use crate::resolve::grow;
|
||||
use crate::theme::{Theme, fallback};
|
||||
|
||||
|
|
@ -232,7 +232,6 @@ impl<'a> Painter<'a> {
|
|||
}
|
||||
|
||||
fn paint(&mut self, node: &'a Node, rect: Rect, control: Option<LaidControl>, text: &mut dyn Measure) {
|
||||
let whole = LaidControl { form: ControlForm::Bordered, content: rect };
|
||||
match node {
|
||||
Node::Box(b) => {
|
||||
let id = &b.common.id;
|
||||
|
|
@ -269,11 +268,24 @@ impl<'a> Painter<'a> {
|
|||
_ => {}
|
||||
}
|
||||
}
|
||||
Node::Button(n) => self.button(n, rect, control.unwrap_or(whole)),
|
||||
Node::Toggle(n) => self.toggle(n, rect, control.unwrap_or(whole)),
|
||||
// A control layout states no form for has nothing to draw as.
|
||||
Node::Button(n) => match control {
|
||||
Some(c) => self.button(n, rect, c),
|
||||
None => self.placeholder(&n.common.id, rect),
|
||||
},
|
||||
Node::Toggle(n) => match control {
|
||||
Some(c) => self.toggle(n, rect, c),
|
||||
None => self.placeholder(&n.common.id, rect),
|
||||
},
|
||||
Node::Slider(n) => self.slider(n, rect),
|
||||
Node::Select(n) => self.select(n, rect, control.unwrap_or(whole), text),
|
||||
Node::TextInput(n) => self.text_input(n, rect, control.unwrap_or(whole), text),
|
||||
Node::Select(n) => match control {
|
||||
Some(c) => self.select(n, rect, c, text),
|
||||
None => self.placeholder(&n.common.id, rect),
|
||||
},
|
||||
Node::TextInput(n) => match control {
|
||||
Some(c) => self.text_input(n, rect, c, text),
|
||||
None => self.placeholder(&n.common.id, rect),
|
||||
},
|
||||
Node::Image(n) => self.image(n, rect, text),
|
||||
Node::Plot(n) => self.plot(n, rect),
|
||||
Node::Overlay(_) | Node::Repeat(_) | Node::Slot(_) => {}
|
||||
|
|
|
|||
Loading…
Reference in a new issue