M2 layout: LayoutEngine on Milky2018/chicle, conformance suite
- layout: port of GPUI's TaffyLayoutEngine (request_layout, request_measured_layout, compute_layout, layout_bounds, stretch_auto_size_to_fill) and LayoutStyle::to_taffy, with rem resolution and device-pixel snapping (round_half_toward_zero, stroke snapping, ceiled measured sizes). No cross-frame node cache yet (needs element ids, M6). - Depend on Milky2018/chicle 0.6.3, the maintained successor of moon_taffy (which no longer compiles), instead of vendoring. - conformance/chicle: standalone module running moon_taffy's 1,038 taffy fixtures against chicle; all pass on wasm, wasm-gc, js and native. - card example now lays out its content with the engine. - Docs: PORTING.md M2 status, revised decision 3, M2 deviations; README; NOTICE attribution for the conformance fixtures. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
10ee6d1f21
commit
cca9a49c51
60 files changed
+46155
-27
No files matched your search
+73
-14
@@ -1,7 +1,7 @@
|
||||
// A headless example of what exists so far (M0 + M1): describe a "card" with
|
||||
// GPUI's fluent style API, resolve it, and paint it into a Scene by hand.
|
||||
// There is no layout engine or renderer yet, so bounds are fixed and the
|
||||
// output is a text report of the scene's draw batches.
|
||||
// A headless example of what exists so far (M0-M2): describe a "card" with
|
||||
// GPUI's fluent style API, lay it out with the layout engine (text is a fake
|
||||
// fixed-width measure until the text system lands), and paint it into a
|
||||
// Scene. There is no renderer yet, so the output is a text report.
|
||||
//
|
||||
// Run with: moon run src/examples/card --target js
|
||||
|
||||
@@ -78,6 +78,33 @@ fn paint_card(
|
||||
)
|
||||
}
|
||||
|
||||
///|
|
||||
/// A stand-in for text measurement (M9): `char_width` per character, wrapping
|
||||
/// at the available width, one `line_height` per line.
|
||||
fn fake_text(
|
||||
chars : Int32,
|
||||
char_width : Double,
|
||||
line_height : Double,
|
||||
) -> @layout.MeasureFn {
|
||||
fn(known, available) {
|
||||
let natural = chars.to_double() * char_width
|
||||
let wrap = match (known.width, available.width) {
|
||||
(Some(width), _) => width.to_double()
|
||||
(None, Definite(width)) => width.to_double()
|
||||
(None, _) => natural
|
||||
}
|
||||
let width = if natural < wrap { natural } else { wrap }
|
||||
let per_line = (wrap / char_width).floor()
|
||||
let lines = (chars.to_double() / per_line).ceil()
|
||||
@geometry.size(@geometry.px(width), @geometry.px(lines * line_height))
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
fn show_bounds(bounds : @geometry.Bounds[@geometry.Pixels]) -> String {
|
||||
"(\{bounds.origin.x}, \{bounds.origin.y}) \{bounds.size.width} x \{bounds.size.height}"
|
||||
}
|
||||
|
||||
///|
|
||||
/// Builds the report printed by `main`.
|
||||
fn report() -> String {
|
||||
@@ -85,21 +112,48 @@ fn report() -> String {
|
||||
let scale_factor = 2.0
|
||||
let style = (Default::default() : @style.Style).refined(card_style())
|
||||
let text = (Default::default() : @style.TextStyle).refined(style.text)
|
||||
let bounds : @geometry.Bounds[@geometry.Pixels] = {
|
||||
origin: @geometry.point(@geometry.px(40.0), @geometry.px(40.0)),
|
||||
size: @geometry.size(@geometry.px(320.0), @geometry.px(120.0)),
|
||||
}
|
||||
let line_height = text.line_height_in_pixels(rem_size).to_double()
|
||||
// Layout: window root > card > (title, body).
|
||||
let engine = @layout.LayoutEngine::new()
|
||||
let leaf_style : @style.Style = Default::default()
|
||||
let title = engine.request_measured_layout(
|
||||
leaf_style,
|
||||
rem_size,
|
||||
scale_factor,
|
||||
fake_text(16, 9.0, line_height),
|
||||
)
|
||||
let body = engine.request_measured_layout(
|
||||
leaf_style,
|
||||
rem_size,
|
||||
scale_factor,
|
||||
fake_text(90, 7.0, line_height),
|
||||
)
|
||||
let card = engine.request_layout(style, rem_size, scale_factor, [title, body])
|
||||
let root_style = (Default::default() : @style.Style).refined(
|
||||
@style.StyleRefinement::new().flex().items_start().p(@geometry.px(40.0)),
|
||||
)
|
||||
let root = engine.request_layout(root_style, rem_size, scale_factor, [card])
|
||||
let window_size = @geometry.size(@geometry.px(480.0), @geometry.px(320.0))
|
||||
engine.stretch_auto_size_to_fill(root, window_size, scale_factor)
|
||||
engine.compute_layout(
|
||||
root,
|
||||
@layout.AvailableSpace::from_size(window_size),
|
||||
scale_factor,
|
||||
)
|
||||
let card_bounds = engine.layout_bounds(card, scale_factor)
|
||||
let title_bounds = engine.layout_bounds(title, scale_factor)
|
||||
let body_bounds = engine.layout_bounds(body, scale_factor)
|
||||
// Paint.
|
||||
let scene = @scene.Scene::new()
|
||||
paint_card(scene, style, bounds, rem_size, scale_factor)
|
||||
// A title underline, overlapping the card.
|
||||
paint_card(scene, style, card_bounds, rem_size, scale_factor)
|
||||
scene.insert_primitive(
|
||||
Underline({
|
||||
order: 0,
|
||||
bounds: @geometry.bounds(
|
||||
@geometry.point(@geometry.px(56.0), @geometry.px(76.0)),
|
||||
@geometry.size(@geometry.px(120.0), @geometry.px(1.0)),
|
||||
title_bounds.bottom_left(),
|
||||
@geometry.size(title_bounds.size.width, @geometry.px(1.0)),
|
||||
).scale(scale_factor),
|
||||
content_mask: { bounds: bounds.scale(scale_factor), },
|
||||
content_mask: { bounds: card_bounds.scale(scale_factor), },
|
||||
color: text.color,
|
||||
thickness: @geometry.ScaledPixels(2.0),
|
||||
wavy: false,
|
||||
@@ -112,7 +166,12 @@ fn report() -> String {
|
||||
" width: \{style.size.width}, padding: \{style.padding.top}, gap: \{style.gap.width}",
|
||||
" radius: \{style.corner_radii.top_left} = \{style.corner_radii.top_left.to_pixels(rem_size)}",
|
||||
" shadows: \{style.box_shadow.length()}",
|
||||
" text: \{text.font_size.to_pixels(rem_size)} \{text.font_family}, color \{text.color.to_rgb()}",
|
||||
" text: \{text.font_size.to_pixels(rem_size)} \{text.font_family}, line height \{line_height}px, color \{text.color.to_rgb()}",
|
||||
"layout (logical px)",
|
||||
" window \{show_bounds(engine.layout_bounds(root, scale_factor))}",
|
||||
" card \{show_bounds(card_bounds)}",
|
||||
" title \{show_bounds(title_bounds)}",
|
||||
" body \{show_bounds(body_bounds)}",
|
||||
"scene at \{scale_factor}x",
|
||||
]
|
||||
for batch in scene.batches() {
|
||||
|
||||
Reference in new issue
Block a user