Thing 2.0 cut 6a: layout hands render its parent chain, padding-box clip, text lines and a visible Use placeholder
LaidNode gains parent and clip becomes Option<Rect> (the padding box); Measure gains text(), the same engine's lines for a Text leaf at its final width; a Use placeholder takes 16x16 logical (1x1 cells), re-blessing layout-f10 and nothing else. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
72d93006d0
commit
57b05845d1
7 changed files with 164 additions and 26 deletions
|
|
@ -43,6 +43,8 @@ pub enum Code {
|
|||
LayoutOutOfRange,
|
||||
/// An Overlay anchored to a node that is not laid (hidden or absent); it was placed against the viewport.
|
||||
OverlayAnchorMissing,
|
||||
/// A colour or text token the theme does not hold; it drew as the fallback colour.
|
||||
ThemeTokenUnknown,
|
||||
}
|
||||
|
||||
impl Code {
|
||||
|
|
|
|||
|
|
@ -35,10 +35,62 @@ pub enum Leaf<'a> {
|
|||
Plot { node: &'a NodeId },
|
||||
}
|
||||
|
||||
/// A face the text engine drew with. Opaque to core: the backend maps it to its own font.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub struct FontId(pub u32);
|
||||
|
||||
/// One placed glyph of a line: the engine's glyph id and its position in the line, logical px.
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub struct Glyph {
|
||||
pub id: u32,
|
||||
pub x: f32,
|
||||
pub y: f32,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||||
pub struct CellStyle {
|
||||
pub bold: bool,
|
||||
pub italic: bool,
|
||||
pub underline: bool,
|
||||
}
|
||||
|
||||
/// What one line of text is made of: shaped glyphs (Pixels) or characters (Cells).
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum Run {
|
||||
Glyphs { font: FontId, size: f32, glyphs: Vec<Glyph> },
|
||||
Cells { text: String, style: CellStyle },
|
||||
}
|
||||
|
||||
/// The Text leaf a line breaker is asked to lay out.
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub struct TextRequest<'a> {
|
||||
pub node: &'a NodeId,
|
||||
pub text: &'a str,
|
||||
pub style_token: &'a str,
|
||||
pub wrap: bool,
|
||||
pub max_lines: Option<u32>,
|
||||
}
|
||||
|
||||
/// One line: `y` and `height` are measured from the top of the node, in logical px or cells.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct TextLine {
|
||||
pub y: f32,
|
||||
pub height: f32,
|
||||
pub run: Run,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, PartialEq)]
|
||||
pub struct TextLayout {
|
||||
pub lines: Vec<TextLine>,
|
||||
}
|
||||
|
||||
/// The intrinsic size `(width, height)` of a leaf under a constraint. A non-finite or negative
|
||||
/// answer is taken as zero.
|
||||
/// answer is taken as zero. `text` is the same engine's answer for a Text leaf at its final
|
||||
/// width: the lines `measure` sized the leaf for, broken and shaped. Wrap and `max_lines` are the
|
||||
/// engine's; render never breaks a line.
|
||||
pub trait Measure {
|
||||
fn measure(&mut self, leaf: &Leaf, constraint: Constraint) -> (f32, f32);
|
||||
fn text(&mut self, request: &TextRequest, width: f32) -> TextLayout;
|
||||
}
|
||||
|
||||
/// The largest length, in device px or cells, a style or a measure may state: 2^21 = 2,097,152.
|
||||
|
|
@ -47,7 +99,7 @@ pub trait Measure {
|
|||
/// maximal siblings still sum below 2^24 (whole-pixel exact), and any run of siblings whose
|
||||
/// extents sum below 2^21 keeps 1/8 px. A length beyond the bound is an invalid style value and a
|
||||
/// larger measured size is clamped to it.
|
||||
pub(super) const MAX_DEVICE: f32 = 2_097_152.0;
|
||||
pub(crate) const MAX_DEVICE: f32 = 2_097_152.0;
|
||||
|
||||
/// The smallest nonzero extent a measure may state: 1/8 px, the precision the length bound
|
||||
/// leaves. A smaller side is below the resolution layout keeps.
|
||||
|
|
@ -56,19 +108,27 @@ pub(super) const MIN_SIDE: f32 = 0.125;
|
|||
/// The one place logical units become device units and back: `k` device px per logical px
|
||||
/// (1 in Cells). Everything past this adapter, the style map and the solver included, is device.
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub(super) struct Device(f32);
|
||||
pub(super) struct Device {
|
||||
k: f32,
|
||||
cells: bool,
|
||||
}
|
||||
|
||||
impl Device {
|
||||
pub(super) fn new(k: f32) -> Self {
|
||||
Self(k)
|
||||
pub(super) fn new(k: f32, cells: bool) -> Self {
|
||||
Self { k, cells }
|
||||
}
|
||||
|
||||
/// The least a Use placeholder takes, so its error is visible: 16 logical px, one cell.
|
||||
pub(super) fn placeholder(self) -> f32 {
|
||||
if self.cells { 1.0 } else { self.px(16.0) }
|
||||
}
|
||||
|
||||
pub(super) fn px(self, logical: f32) -> f32 {
|
||||
logical * self.0
|
||||
logical * self.k
|
||||
}
|
||||
|
||||
fn logical(self, device: f32) -> f32 {
|
||||
device / self.0
|
||||
device / self.k
|
||||
}
|
||||
|
||||
fn avail(self, space: AvailableSpace) -> Avail {
|
||||
|
|
|
|||
|
|
@ -15,10 +15,11 @@ mod style;
|
|||
mod tree;
|
||||
|
||||
use taffy::{AvailableSpace, Size, compute_leaf_layout};
|
||||
use measure::{Device, MAX_DEVICE};
|
||||
use measure::Device;
|
||||
use thing_contract::{NodeId, OverlayNode};
|
||||
|
||||
pub use measure::{Avail, Constraint, Leaf, Measure};
|
||||
pub use measure::{Avail, CellStyle, Constraint, FontId, Glyph, Leaf, Measure, Run, TextLayout, TextLine, TextRequest};
|
||||
pub(crate) use measure::MAX_DEVICE;
|
||||
|
||||
use crate::bind::BoundTree;
|
||||
use crate::diagnostic::{Code, Diagnostic};
|
||||
|
|
@ -53,18 +54,21 @@ pub struct Rect {
|
|||
}
|
||||
|
||||
/// One node's geometry. `rect` is the absolute border box in device px (Pixels) or cells, and is
|
||||
/// unscrolled. `content` is the scrollable content size of an `Overflow::Scroll` node; `clip` is
|
||||
/// set for Clip and Scroll.
|
||||
/// unscrolled. `parent` indexes the nearest laid ancestor in `LaidTree.nodes` (none for the
|
||||
/// document root and every overlay root). `content` is the scrollable content size of an
|
||||
/// `Overflow::Scroll` node; `clip` is the padding box (the border box inset by the laid border)
|
||||
/// of a Clip or Scroll node.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct LaidNode {
|
||||
pub id: NodeId,
|
||||
pub parent: Option<u32>,
|
||||
pub rect: Rect,
|
||||
pub content: Option<(f32, f32)>,
|
||||
/// How far `content` reaches past the padding box, never negative: the furthest the content
|
||||
/// may scroll. Input clamps its position here; render and hit-test translate the children by
|
||||
/// it. Layout never sees a scroll position, so a scroll never re-runs layout.
|
||||
pub max_scroll: Option<(f32, f32)>,
|
||||
pub clip: bool,
|
||||
pub clip: Option<Rect>,
|
||||
}
|
||||
|
||||
/// Every laid node in paint order (document order, Repeat instances spliced), and the style
|
||||
|
|
@ -110,7 +114,7 @@ pub fn layout(tree: &BoundTree, frame: &LayoutFrame, measure: &mut dyn Measure)
|
|||
Metrics::Pixels if frame.scale.is_finite() && frame.scale > 0.0 && frame.scale <= 1024.0 => frame.scale,
|
||||
Metrics::Pixels => return Err(FrameError::Scale.into()),
|
||||
};
|
||||
let dev = Device::new(k);
|
||||
let dev = Device::new(k, frame.metrics == Metrics::Cells);
|
||||
let side = |v: f32| (v.is_finite() && v > 0.0 && dev.px(v) <= MAX_DEVICE).then(|| dev.px(v));
|
||||
let (Some(vw), Some(vh)) = (side(frame.viewport.0), side(frame.viewport.1)) else {
|
||||
return Err(FrameError::Viewport.into());
|
||||
|
|
@ -160,6 +164,7 @@ fn solve<'a>(
|
|||
})
|
||||
.expect("taffy: the root is in the tree")
|
||||
});
|
||||
let base = nodes.len();
|
||||
let mut absolute: Vec<(f32, f32)> = Vec::with_capacity(entries.len());
|
||||
for entry in entries.iter() {
|
||||
let laid = taffy.layout(entry.node).expect("taffy: node is in the tree");
|
||||
|
|
@ -191,12 +196,19 @@ fn solve<'a>(
|
|||
detail: "position or extent past the precision bound".to_string(),
|
||||
}));
|
||||
}
|
||||
let clip = entry.clip.then(|| Rect {
|
||||
x: clean(x + laid.border.left),
|
||||
y: clean(y + laid.border.top),
|
||||
w: clean(w - laid.border.left - laid.border.right),
|
||||
h: clean(h - laid.border.top - laid.border.bottom),
|
||||
});
|
||||
nodes.push(LaidNode {
|
||||
id: entry.id.clone(),
|
||||
parent: entry.parent.map(|parent| (base + parent) as u32),
|
||||
rect: Rect { x: clean(x), y: clean(y), w: clean(w), h: clean(h) },
|
||||
content,
|
||||
max_scroll,
|
||||
clip: entry.clip,
|
||||
clip,
|
||||
});
|
||||
}
|
||||
diagnostics.append(own);
|
||||
|
|
|
|||
|
|
@ -211,7 +211,9 @@ impl<'a> Builder<'a, '_> {
|
|||
Node::Use(u) => {
|
||||
if !self.hidden(&u.common.id) {
|
||||
let index = self.entry(&u.common.id, parent, &Overflow::Visible);
|
||||
let id = self.taffy.new_leaf(Style::default()).expect("taffy: insert");
|
||||
let least = taffy::LengthPercentageAuto::length(self.dev.placeholder());
|
||||
let style = Style { min_size: taffy::Size { width: least, height: least }, ..Style::default() };
|
||||
let id = self.taffy.new_leaf(style).expect("taffy: insert");
|
||||
self.entries[index].node = id;
|
||||
out.push(id);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,5 +4,5 @@ root 0 0 800 20
|
|||
mismatch 2 2 796 16
|
||||
surface thing:surface:provider.hostile.undeclared-command refused UndeclaredCommand
|
||||
surface thing:surface:provider.hostile.unresolved-use viewport 800x600 scale 1 pixels
|
||||
root 0 0 800 4
|
||||
ghost-use 2 2 796 0
|
||||
root 0 0 800 20
|
||||
ghost-use 2 2 796 16
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ use taffy::{AlignContent, AlignItems, AvailableSpace, FlexDirection, Style, Taff
|
|||
use thing_contract::*;
|
||||
use thing_core::bind::{Binder, BoundTree};
|
||||
use thing_core::diagnostic::Code;
|
||||
use thing_core::layout::{Constraint, FrameError, LaidNode, LayoutError, LaidTree, LayoutFrame, Leaf, Measure, Metrics, layout};
|
||||
use thing_core::layout::{Constraint, FrameError, LaidNode, LayoutError, LaidTree, LayoutFrame, Leaf, Measure, Metrics, TextLayout, TextRequest, layout};
|
||||
use thing_core::port::Ledger;
|
||||
use thing_core::resolve::{Limits, resolve};
|
||||
use thing_testkit::build::*;
|
||||
|
|
@ -63,12 +63,12 @@ fn overlay_anchors(node: &Node, out: &mut std::collections::HashMap<String, Stri
|
|||
|
||||
fn render(laid: &LaidTree, anchors: &std::collections::HashMap<String, String>) -> String {
|
||||
let mut out = String::new();
|
||||
for LaidNode { id, rect, content, max_scroll, clip } in &laid.nodes {
|
||||
for LaidNode { id, rect, content, max_scroll, clip, .. } in &laid.nodes {
|
||||
if let Some(anchor) = anchors.get(id.as_str()) {
|
||||
out += &format!("overlay {} anchor {anchor}\n", id.as_str());
|
||||
}
|
||||
out += &format!("{}\t{}\t{}\t{}\t{}", id.as_str(), rect.x, rect.y, rect.w, rect.h);
|
||||
if *clip {
|
||||
if clip.is_some() {
|
||||
out += "\tclip";
|
||||
}
|
||||
if let Some((w, h)) = content {
|
||||
|
|
@ -454,9 +454,9 @@ fn taffy_edges() {
|
|||
600.0,
|
||||
);
|
||||
let node = |id: &str| laid.nodes.iter().find(|n| n.id.as_str() == id).unwrap();
|
||||
assert_eq!((node("scroll").clip, node("scroll").content), (true, Some((200.0, 300.0))));
|
||||
assert_eq!((node("clip").clip, node("clip").content), (true, None));
|
||||
assert_eq!((node("root").clip, node("root").content), (false, None));
|
||||
assert_eq!((node("scroll").clip.is_some(), node("scroll").content), (true, Some((200.0, 300.0))));
|
||||
assert_eq!((node("clip").clip.is_some(), node("clip").content), (true, None));
|
||||
assert_eq!((node("root").clip.is_some(), node("root").content), (false, None));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
@ -608,6 +608,9 @@ fn embed_exact() {
|
|||
fn image_measures_through_the_measure() {
|
||||
struct Fixed;
|
||||
impl Measure for Fixed {
|
||||
fn text(&mut self, _: &TextRequest, _: f32) -> TextLayout {
|
||||
TextLayout::default()
|
||||
}
|
||||
fn measure(&mut self, leaf: &Leaf, _: Constraint) -> (f32, f32) {
|
||||
match leaf {
|
||||
Leaf::Image { .. } => (40.0, 20.0),
|
||||
|
|
@ -657,6 +660,9 @@ fn kind_style(kind: Kind) -> BoxStyle {
|
|||
fn invalid_image_measures_are_named_and_harmless() {
|
||||
struct Image((f32, f32));
|
||||
impl Measure for Image {
|
||||
fn text(&mut self, _: &TextRequest, _: f32) -> TextLayout {
|
||||
TextLayout::default()
|
||||
}
|
||||
fn measure(&mut self, leaf: &Leaf, _: Constraint) -> (f32, f32) {
|
||||
match leaf {
|
||||
Leaf::Image { .. } => self.0,
|
||||
|
|
@ -755,6 +761,9 @@ fn depth() {
|
|||
fn stack_per_level() {
|
||||
struct Probe(Option<usize>);
|
||||
impl Measure for Probe {
|
||||
fn text(&mut self, _: &TextRequest, _: f32) -> TextLayout {
|
||||
TextLayout::default()
|
||||
}
|
||||
fn measure(&mut self, _: &Leaf, _: Constraint) -> (f32, f32) {
|
||||
let left = stacker::remaining_stack().unwrap_or(0);
|
||||
self.0 = Some(self.0.map_or(left, |low| low.min(left)));
|
||||
|
|
@ -810,6 +819,9 @@ fn extreme_values_never_panic() {
|
|||
// A measure that answers beyond the bound is clamped to it.
|
||||
struct Huge;
|
||||
impl Measure for Huge {
|
||||
fn text(&mut self, _: &TextRequest, _: f32) -> TextLayout {
|
||||
TextLayout::default()
|
||||
}
|
||||
fn measure(&mut self, _: &Leaf, _: Constraint) -> (f32, f32) {
|
||||
(f32::MAX, f32::INFINITY)
|
||||
}
|
||||
|
|
@ -828,6 +840,9 @@ fn extreme_values_never_panic() {
|
|||
fn image_measures_never_resize_fixed_siblings() {
|
||||
struct Image((f32, f32));
|
||||
impl Measure for Image {
|
||||
fn text(&mut self, _: &TextRequest, _: f32) -> TextLayout {
|
||||
TextLayout::default()
|
||||
}
|
||||
fn measure(&mut self, leaf: &Leaf, _: Constraint) -> (f32, f32) {
|
||||
match leaf {
|
||||
Leaf::Image { .. } => self.0,
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
//! goldens never move when a real text measure changes.
|
||||
|
||||
use thing_core::bind::Bound;
|
||||
use thing_core::layout::{Avail, Constraint, Leaf, Measure};
|
||||
use thing_core::layout::{Avail, CellStyle, Constraint, FontId, Glyph, Leaf, Measure, Run, TextLayout, TextLine, TextRequest};
|
||||
|
||||
/// Text is 8 px wide per character and 16 px tall per line (one cell each in Cells); controls
|
||||
/// have fixed sizes; an Image is its bound catalog entry's extent (0x0 when missing); a Plot is 0x0.
|
||||
|
|
@ -19,7 +19,7 @@ impl FixedMeasure {
|
|||
Self { cells: true }
|
||||
}
|
||||
|
||||
fn text(&self, content: &Bound, wrap: bool, max_lines: Option<u32>, c: &Constraint) -> (f32, f32) {
|
||||
fn text_size(&self, content: &Bound, wrap: bool, max_lines: Option<u32>, c: &Constraint) -> (f32, f32) {
|
||||
let (cw, ch) = if self.cells { (1.0, 1.0) } else { (8.0, 16.0) };
|
||||
let text = match content {
|
||||
Bound::Text(text) => text.as_str(),
|
||||
|
|
@ -43,12 +43,59 @@ impl FixedMeasure {
|
|||
}
|
||||
}
|
||||
|
||||
impl FixedMeasure {
|
||||
/// The lines a text breaks into at `width`: the chunks `text` sizes its height for, so the
|
||||
/// measure and the layout of one Text leaf agree.
|
||||
fn lines(&self, request: &TextRequest, width: f32) -> Vec<String> {
|
||||
let cw = if self.cells { 1.0 } else { 8.0 };
|
||||
let chars: Vec<char> = request.text.chars().collect();
|
||||
let capacity = (width / cw).floor().max(1.0) as usize;
|
||||
let mut lines: Vec<String> = if request.wrap {
|
||||
chars.chunks(capacity).map(|chunk| chunk.iter().collect()).collect()
|
||||
} else {
|
||||
vec![request.text.to_string()]
|
||||
};
|
||||
if lines.is_empty() {
|
||||
lines.push(String::new());
|
||||
}
|
||||
if let Some(cap) = request.max_lines {
|
||||
lines.truncate(cap as usize);
|
||||
}
|
||||
lines
|
||||
}
|
||||
}
|
||||
|
||||
impl Measure for FixedMeasure {
|
||||
fn text(&mut self, request: &TextRequest, width: f32) -> TextLayout {
|
||||
let height = if self.cells { 1.0 } else { 16.0 };
|
||||
let lines = self.lines(request, width);
|
||||
TextLayout {
|
||||
lines: lines
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
.map(|(row, line)| TextLine {
|
||||
y: row as f32 * height,
|
||||
height,
|
||||
run: if self.cells {
|
||||
Run::Cells { text: line, style: CellStyle::default() }
|
||||
} else {
|
||||
let glyphs = line
|
||||
.chars()
|
||||
.enumerate()
|
||||
.map(|(column, c)| Glyph { id: c as u32, x: 8.0 * column as f32, y: 0.0 })
|
||||
.collect();
|
||||
Run::Glyphs { font: FontId(0), size: 16.0, glyphs }
|
||||
},
|
||||
})
|
||||
.collect(),
|
||||
}
|
||||
}
|
||||
|
||||
fn measure(&mut self, leaf: &Leaf, c: Constraint) -> (f32, f32) {
|
||||
// Pixels sizes, then the same control in cells: width / 8 and one row.
|
||||
let control = |w: f32, h: f32| if self.cells { (w / 8.0, 1.0) } else { (w, h) };
|
||||
match leaf {
|
||||
Leaf::Text { content, wrap, max_lines, .. } => self.text(content, *wrap, *max_lines, &c),
|
||||
Leaf::Text { content, wrap, max_lines, .. } => self.text_size(content, *wrap, *max_lines, &c),
|
||||
Leaf::TextInput { multiline, .. } => control(200.0, if *multiline { 72.0 } else { 24.0 }),
|
||||
Leaf::Toggle { .. } => control(40.0, 20.0),
|
||||
Leaf::Slider { .. } => control(160.0, 20.0),
|
||||
|
|
|
|||
Loading…
Reference in a new issue