M2: layout — LayoutEngine on Milky2018/chicle, 1,038-fixture conformance suite (#3)
This commit was merged in pull request #3.
This commit is contained in:
1 parent
10ee6d1f21
commit
b5daf13d47
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() {
|
||||
|
||||
@@ -8,12 +8,17 @@ test "card report" {
|
||||
#| width: 320px, padding: 1rem, gap: 0.5rem
|
||||
#| radius: 0.5rem = 8px
|
||||
#| shadows: 2
|
||||
#| text: 14px .SystemUIFont, color #1f2328ff
|
||||
#| text: 14px .SystemUIFont, line height 23px, color #1f2328ff
|
||||
#|layout (logical px)
|
||||
#| window (0px, 0px) 480px x 320px
|
||||
#| card (40px, 40px) 320px x 134px
|
||||
#| title (57px, 57px) 286px x 23px
|
||||
#| body (57px, 88px) 286px x 69px
|
||||
#|scene at 2x
|
||||
#| shadows (2)
|
||||
#| quads (1)
|
||||
#| underlines (1)
|
||||
#| quad order 1: 640 x 240 device px
|
||||
#| quad order 1: 640 x 268 device px
|
||||
),
|
||||
)
|
||||
}
|
||||
@@ -3,6 +3,7 @@ import {
|
||||
"experiments/gpui/color",
|
||||
"experiments/gpui/scene",
|
||||
"experiments/gpui/style",
|
||||
"experiments/gpui/layout",
|
||||
"moonbitlang/core/debug",
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,309 @@
|
||||
// Conversion from GPUI styles to the layout engine's styles, ported from
|
||||
// gpui/src/taffy.rs (`LayoutStyle::to_taffy`, `ToTaffy`) and the rounding
|
||||
// helpers in gpui/src/util.rs.
|
||||
//
|
||||
// GPUI lays out in *device* pixels: absolute lengths are resolved against the
|
||||
// rem size, multiplied by the scale factor and rounded, so that layout results
|
||||
// land on the pixel grid. `layout_bounds` divides back into logical pixels.
|
||||
|
||||
///|
|
||||
/// Rounds to the nearest integer, with ties going toward zero
|
||||
/// (GPUI: `round_half_toward_zero`).
|
||||
pub fn round_half_toward_zero(value : Double) -> Double {
|
||||
let magnitude = (value.abs() - 0.5).ceil()
|
||||
// `ceil(-0.5)` is -0.0; normalize so ties at ±0.5 give +0.0.
|
||||
let magnitude = if magnitude == 0.0 { 0.0 } else { magnitude }
|
||||
if value < 0.0 {
|
||||
-magnitude
|
||||
} else {
|
||||
magnitude
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
/// Logical pixels to device pixels, rounded (GPUI: `round_to_device_pixel`).
|
||||
pub fn round_to_device_pixel(logical : Double, scale_factor : Double) -> Double {
|
||||
round_half_toward_zero(logical * scale_factor)
|
||||
}
|
||||
|
||||
///|
|
||||
/// Like `round_to_device_pixel`, but a non-zero stroke is at least one device
|
||||
/// pixel wide (GPUI: `round_stroke_to_device_pixel`).
|
||||
pub fn round_stroke_to_device_pixel(
|
||||
logical : Double,
|
||||
scale_factor : Double,
|
||||
) -> Double {
|
||||
if logical == 0.0 {
|
||||
0.0
|
||||
} else {
|
||||
let rounded = round_to_device_pixel(
|
||||
if logical > 0.0 {
|
||||
logical
|
||||
} else {
|
||||
0.0
|
||||
},
|
||||
scale_factor,
|
||||
)
|
||||
if rounded < 1.0 {
|
||||
1.0
|
||||
} else {
|
||||
rounded
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
/// Logical pixels to device pixels, rounded up (GPUI: `ceil_to_device_pixel`).
|
||||
pub fn ceil_to_device_pixel(logical : Double, scale_factor : Double) -> Double {
|
||||
(logical * scale_factor).ceil()
|
||||
}
|
||||
|
||||
///|
|
||||
fn absolute_to_device(
|
||||
length : @geometry.AbsoluteLength,
|
||||
rem_size : @geometry.Pixels,
|
||||
scale_factor : Double,
|
||||
) -> Double {
|
||||
round_to_device_pixel(length.to_pixels(rem_size).to_double(), scale_factor)
|
||||
}
|
||||
|
||||
///|
|
||||
fn definite_to_dimension(
|
||||
length : @geometry.DefiniteLength,
|
||||
rem_size : @geometry.Pixels,
|
||||
scale_factor : Double,
|
||||
) -> @cstyle.Dimension {
|
||||
match length {
|
||||
Absolute(absolute) =>
|
||||
DimLength(absolute_to_device(absolute, rem_size, scale_factor))
|
||||
Fraction(fraction) => DimPercent(fraction)
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
fn length_to_dimension(
|
||||
length : @geometry.Length,
|
||||
rem_size : @geometry.Pixels,
|
||||
scale_factor : Double,
|
||||
) -> @cstyle.Dimension {
|
||||
match length {
|
||||
Definite(definite) =>
|
||||
definite_to_dimension(definite, rem_size, scale_factor)
|
||||
Auto => DimAuto
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
fn edges_to_rect(
|
||||
edges : @geometry.Edges[@cstyle.Dimension],
|
||||
) -> @cgeometry.Rect[@cstyle.Dimension] {
|
||||
@cgeometry.Rect(
|
||||
left=edges.left,
|
||||
right=edges.right,
|
||||
top=edges.top,
|
||||
bottom=edges.bottom,
|
||||
)
|
||||
}
|
||||
|
||||
///|
|
||||
fn size_to_engine(
|
||||
size : @geometry.Size[@cstyle.Dimension],
|
||||
) -> @cgeometry.Size[@cstyle.Dimension] {
|
||||
@cgeometry.Size(width=size.width, height=size.height)
|
||||
}
|
||||
|
||||
///|
|
||||
/// Border widths are snapped like strokes, so a hairline border stays
|
||||
/// visible (GPUI: `border_widths_to_taffy`).
|
||||
pub fn border_widths_to_engine(
|
||||
widths : @geometry.Edges[@geometry.AbsoluteLength],
|
||||
rem_size : @geometry.Pixels,
|
||||
scale_factor : Double,
|
||||
) -> @cgeometry.Rect[@cstyle.Dimension] {
|
||||
edges_to_rect(
|
||||
widths.map(fn(width) {
|
||||
DimLength(
|
||||
round_stroke_to_device_pixel(
|
||||
width.to_pixels(rem_size).to_double(),
|
||||
scale_factor,
|
||||
),
|
||||
)
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
///|
|
||||
fn display_to_engine(display : @style.Display) -> @cstyle.Display {
|
||||
match display {
|
||||
Block => DisplayBlock
|
||||
Flex => DisplayFlex
|
||||
Grid => DisplayGrid
|
||||
@style.Display::None => DisplayNone
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
fn overflow_to_engine(overflow : @style.Overflow) -> @cstyle.Overflow {
|
||||
match overflow {
|
||||
Visible => OverflowVisible
|
||||
Clip => OverflowClip
|
||||
Hidden => OverflowHidden
|
||||
Scroll => OverflowScroll
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
fn position_to_engine(position : @style.Position) -> @cstyle.Position {
|
||||
match position {
|
||||
Relative => PosRelative
|
||||
Absolute => PosAbsolute
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
fn align_items_to_engine(align : @style.AlignItems) -> @cstyle.AlignItems {
|
||||
match align {
|
||||
Start => ItemsStart
|
||||
End => ItemsEnd
|
||||
FlexStart => ItemsFlexStart
|
||||
FlexEnd => ItemsFlexEnd
|
||||
Center => ItemsCenter
|
||||
Baseline => ItemsBaseline
|
||||
Stretch => ItemsStretch
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
fn align_content_to_engine(align : @style.AlignContent) -> @cstyle.AlignContent {
|
||||
match align {
|
||||
Start => AlignStart
|
||||
End => AlignEnd
|
||||
FlexStart => AlignFlexStart
|
||||
FlexEnd => AlignFlexEnd
|
||||
Center => AlignCenter
|
||||
Stretch => AlignStretch
|
||||
SpaceBetween => AlignSpaceBetween
|
||||
SpaceEvenly => AlignSpaceEvenly
|
||||
SpaceAround => AlignSpaceAround
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
fn flex_direction_to_engine(
|
||||
direction : @style.FlexDirection,
|
||||
) -> @cstyle.FlexDirection {
|
||||
match direction {
|
||||
Row => FlexRow
|
||||
Column => FlexColumn
|
||||
RowReverse => FlexRowReverse
|
||||
ColumnReverse => FlexColumnReverse
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
fn flex_wrap_to_engine(wrap : @style.FlexWrap) -> @cstyle.FlexWrap {
|
||||
match wrap {
|
||||
NoWrap => FlexNoWrap
|
||||
Wrap => @cstyle.FlexWrap::FlexWrap
|
||||
WrapReverse => FlexWrapReverse
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
fn grid_placement_to_engine(
|
||||
placement : @geometry.GridPlacement,
|
||||
) -> @cstyle.GridPlacement {
|
||||
match placement {
|
||||
Line(line) => PlaceLine(line)
|
||||
Span(span) => PlaceSpan(span)
|
||||
Auto => PlaceAuto
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
fn grid_range_to_engine(
|
||||
range : @geometry.GridRange,
|
||||
) -> @cgeometry.Line[@cstyle.GridPlacement] {
|
||||
@cgeometry.Line(
|
||||
start=grid_placement_to_engine(range.start),
|
||||
end=grid_placement_to_engine(range.end),
|
||||
)
|
||||
}
|
||||
|
||||
///|
|
||||
/// `repeat(n, minmax(..))` tracks for GPUI's `grid_cols`/`grid_rows`.
|
||||
fn grid_template_to_engine(
|
||||
template : @style.GridTemplate?,
|
||||
) -> Array[@cstyle.Dimension] {
|
||||
match template {
|
||||
Option::None => []
|
||||
Some(template) => {
|
||||
let track : @cstyle.Dimension = match template.min_size {
|
||||
Zero => DimMinMax(DimLength(0.0), DimFr(1.0))
|
||||
MinContent => DimMinMax(DimMinContent, DimFr(1.0))
|
||||
MaxContent => DimMinMax(DimLength(0.0), DimMaxContent)
|
||||
}
|
||||
[DimRepeat(RepeatCount(template.repeat), [track])]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
/// Converts a resolved GPUI `Style` to the layout engine's style, resolving
|
||||
/// rems and snapping absolute lengths to device pixels
|
||||
/// (GPUI: `LayoutStyle::to_taffy`).
|
||||
pub fn style_to_engine(
|
||||
style : @style.Style,
|
||||
rem_size : @geometry.Pixels,
|
||||
scale_factor : Double,
|
||||
) -> @cstyle.Style {
|
||||
let length = fn(l : @geometry.Length) {
|
||||
length_to_dimension(l, rem_size, scale_factor)
|
||||
}
|
||||
let definite = fn(l : @geometry.DefiniteLength) {
|
||||
definite_to_dimension(l, rem_size, scale_factor)
|
||||
}
|
||||
let grid_location = style.grid_location
|
||||
{
|
||||
..@cstyle.Style::default(),
|
||||
display: display_to_engine(style.display),
|
||||
overflow: @cgeometry.Point(
|
||||
x=overflow_to_engine(style.overflow.x),
|
||||
y=overflow_to_engine(style.overflow.y),
|
||||
),
|
||||
scrollbar_width: absolute_to_device(
|
||||
style.scrollbar_width,
|
||||
rem_size,
|
||||
scale_factor,
|
||||
),
|
||||
position: position_to_engine(style.position),
|
||||
inset: edges_to_rect(style.inset.map(length)),
|
||||
size: size_to_engine(style.size.map(length)),
|
||||
min_size: size_to_engine(style.min_size.map(length)),
|
||||
max_size: size_to_engine(style.max_size.map(length)),
|
||||
aspect_ratio: style.aspect_ratio,
|
||||
margin: edges_to_rect(style.margin.map(length)),
|
||||
padding: edges_to_rect(style.padding.map(definite)),
|
||||
border: border_widths_to_engine(style.border_widths, rem_size, scale_factor),
|
||||
align_items: style.align_items.map(align_items_to_engine),
|
||||
align_self: style.align_self.map(align_items_to_engine),
|
||||
align_content: style.align_content.map(align_content_to_engine),
|
||||
justify_content: style.justify_content.map(align_content_to_engine),
|
||||
gap: size_to_engine(style.gap.map(definite)),
|
||||
flex_direction: flex_direction_to_engine(style.flex_direction),
|
||||
flex_wrap: flex_wrap_to_engine(style.flex_wrap),
|
||||
flex_basis: length(style.flex_basis),
|
||||
flex_grow: style.flex_grow,
|
||||
flex_shrink: style.flex_shrink,
|
||||
grid_template_rows: grid_template_to_engine(style.grid_rows),
|
||||
grid_template_columns: grid_template_to_engine(style.grid_cols),
|
||||
grid_row: match grid_location {
|
||||
Some(location) => grid_range_to_engine(location.row)
|
||||
Option::None => @cgeometry.Line(start=PlaceAuto, end=PlaceAuto)
|
||||
},
|
||||
grid_column: match grid_location {
|
||||
Some(location) => grid_range_to_engine(location.column)
|
||||
Option::None => @cgeometry.Line(start=PlaceAuto, end=PlaceAuto)
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,335 @@
|
||||
// The layout engine, ported from `TaffyLayoutEngine` in gpui/src/taffy.rs,
|
||||
// on top of `Milky2018/chicle` (a MoonBit port of taffy).
|
||||
//
|
||||
// Deviation from GPUI: there is no cross-frame node cache yet (GPUI reuses
|
||||
// nodes keyed by element path and keeps measure closures alive across
|
||||
// frames). The engine is rebuilt on `clear()`, as older GPUI versions did.
|
||||
// The cache depends on element ids and arrives with the Window (M6).
|
||||
|
||||
///|
|
||||
/// Space available to a node along one axis (GPUI: `AvailableSpace`).
|
||||
#derive(Eq, Hash, Debug)
|
||||
pub(all) enum AvailableSpace {
|
||||
/// A definite amount of space, in logical pixels.
|
||||
Definite(@geometry.Pixels)
|
||||
/// Size to the smallest size that fits the content.
|
||||
MinContent
|
||||
/// Size to the largest size the content wants.
|
||||
MaxContent
|
||||
}
|
||||
|
||||
///|
|
||||
pub impl Default for AvailableSpace with fn default() {
|
||||
MinContent
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn AvailableSpace::min_size() -> @geometry.Size[AvailableSpace] {
|
||||
{ width: MinContent, height: MinContent, }
|
||||
}
|
||||
|
||||
///|
|
||||
/// Definite space in both dimensions.
|
||||
pub fn AvailableSpace::from_size(
|
||||
size : @geometry.Size[@geometry.Pixels],
|
||||
) -> @geometry.Size[AvailableSpace] {
|
||||
{ width: Definite(size.width), height: Definite(size.height), }
|
||||
}
|
||||
|
||||
///|
|
||||
/// Identifies a node requested from the `LayoutEngine` during this frame.
|
||||
#derive(Eq, Hash, Compare, Debug)
|
||||
pub(all) struct LayoutId(Int32)
|
||||
|
||||
///|
|
||||
/// Measures a leaf (e.g. text): given the dimensions already known and the
|
||||
/// available space, in logical pixels, returns the content size.
|
||||
pub type MeasureFn = (
|
||||
@geometry.Size[@geometry.Pixels?],
|
||||
@geometry.Size[AvailableSpace],
|
||||
) -> @geometry.Size[@geometry.Pixels]
|
||||
|
||||
///|
|
||||
/// Computes flexbox/grid/block layout for a frame's element tree.
|
||||
pub struct LayoutEngine {
|
||||
priv mut tree : @ctree.ChicleTree[MeasureFn]
|
||||
/// The engine style of each node (needed by `stretch_auto_size_to_fill`).
|
||||
priv styles : Map[Int32, @cstyle.Style]
|
||||
priv children : Map[Int32, Array[Int32]]
|
||||
priv parents : Map[Int32, Int32]
|
||||
priv absolute_layout_bounds : Map[Int32, @geometry.Bounds[@geometry.Pixels]]
|
||||
/// Absolute outer origins in device pixels, unrounded.
|
||||
priv absolute_outer_origins : Map[Int32, @geometry.Point[Double]]
|
||||
priv computed_layouts : @hashset.HashSet[Int32]
|
||||
}
|
||||
|
||||
///|
|
||||
fn new_tree() -> @ctree.ChicleTree[MeasureFn] {
|
||||
let tree : @ctree.ChicleTree[MeasureFn] = @ctree.ChicleTree()
|
||||
// GPUI snaps to device pixels itself; the engine must not round.
|
||||
tree.disable_rounding()
|
||||
tree
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn LayoutEngine::new() -> LayoutEngine {
|
||||
{
|
||||
tree: new_tree(),
|
||||
styles: {},
|
||||
children: {},
|
||||
parents: {},
|
||||
absolute_layout_bounds: {},
|
||||
absolute_outer_origins: {},
|
||||
computed_layouts: @hashset.HashSet([]),
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
/// Forgets every node. Call between frames; `LayoutId`s from earlier frames
|
||||
/// become invalid.
|
||||
pub fn LayoutEngine::clear(self : LayoutEngine) -> Unit {
|
||||
self.tree = new_tree()
|
||||
self.styles.clear()
|
||||
self.children.clear()
|
||||
self.parents.clear()
|
||||
self.absolute_layout_bounds.clear()
|
||||
self.absolute_outer_origins.clear()
|
||||
self.computed_layouts.clear()
|
||||
}
|
||||
|
||||
///|
|
||||
/// Number of nodes requested since the last `clear`.
|
||||
pub fn LayoutEngine::node_count(self : LayoutEngine) -> Int32 {
|
||||
self.styles.length()
|
||||
}
|
||||
|
||||
///|
|
||||
/// GPUI expects layout errors to be impossible by construction; fail loudly
|
||||
/// if one happens anyway.
|
||||
fn engine_failure(operation : String, error : @ctree.ChicleError) -> Unit {
|
||||
ignore(error)
|
||||
abort("layout engine: \{operation} failed (invalid LayoutId?)")
|
||||
}
|
||||
|
||||
///|
|
||||
fn LayoutEngine::check_id(
|
||||
self : LayoutEngine,
|
||||
id : LayoutId,
|
||||
operation : String,
|
||||
) -> Unit {
|
||||
guard self.styles.contains(id.0) else {
|
||||
abort(
|
||||
"layout engine: \{operation}: unknown LayoutId(\{id.0}) (requested in an earlier frame?)",
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
/// Requests a node with the given children (GPUI: `request_layout`).
|
||||
pub fn LayoutEngine::request_layout(
|
||||
self : LayoutEngine,
|
||||
style : @style.Style,
|
||||
rem_size : @geometry.Pixels,
|
||||
scale_factor : Double,
|
||||
children : Array[LayoutId],
|
||||
) -> LayoutId {
|
||||
let engine_style = style_to_engine(style, rem_size, scale_factor)
|
||||
let child_ids = children.map(fn(child) {
|
||||
self.check_id(child, "request_layout")
|
||||
guard !self.parents.contains(child.0) else {
|
||||
abort("layout engine: LayoutId(\{child.0}) already has a parent")
|
||||
}
|
||||
child.0
|
||||
})
|
||||
let id = if child_ids.is_empty() {
|
||||
self.tree.new_leaf(engine_style)
|
||||
} else {
|
||||
self.tree.new_with_children(engine_style, child_ids)
|
||||
}
|
||||
self.styles[id] = engine_style
|
||||
self.children[id] = child_ids
|
||||
for child in child_ids {
|
||||
self.parents[child] = id
|
||||
}
|
||||
LayoutId(id)
|
||||
}
|
||||
|
||||
///|
|
||||
/// Requests a leaf whose content size comes from `measure`, called during
|
||||
/// `compute_layout` (GPUI: `request_measured_layout`).
|
||||
pub fn LayoutEngine::request_measured_layout(
|
||||
self : LayoutEngine,
|
||||
style : @style.Style,
|
||||
rem_size : @geometry.Pixels,
|
||||
scale_factor : Double,
|
||||
measure : MeasureFn,
|
||||
) -> LayoutId {
|
||||
let engine_style = style_to_engine(style, rem_size, scale_factor)
|
||||
let id = self.tree.new_leaf_with_context(engine_style, measure)
|
||||
self.styles[id] = engine_style
|
||||
self.children[id] = []
|
||||
LayoutId(id)
|
||||
}
|
||||
|
||||
///|
|
||||
/// Makes an `auto`-sized root fill `size` (GPUI: `stretch_auto_size_to_fill`,
|
||||
/// used for the window's root element).
|
||||
pub fn LayoutEngine::stretch_auto_size_to_fill(
|
||||
self : LayoutEngine,
|
||||
id : LayoutId,
|
||||
size : @geometry.Size[@geometry.Pixels],
|
||||
scale_factor : Double,
|
||||
) -> Unit {
|
||||
self.check_id(id, "stretch_auto_size_to_fill")
|
||||
let style = self.styles[id.0]
|
||||
let stretch_width = style.size.width is DimAuto
|
||||
let stretch_height = style.size.height is DimAuto
|
||||
if !stretch_width && !stretch_height {
|
||||
return
|
||||
}
|
||||
let width : @cstyle.Dimension = if stretch_width {
|
||||
DimLength(round_to_device_pixel(size.width.to_double(), scale_factor))
|
||||
} else {
|
||||
style.size.width
|
||||
}
|
||||
let height : @cstyle.Dimension = if stretch_height {
|
||||
DimLength(round_to_device_pixel(size.height.to_double(), scale_factor))
|
||||
} else {
|
||||
style.size.height
|
||||
}
|
||||
let stretched = { ..style, size: @cgeometry.Size(width~, height~), }
|
||||
self.styles[id.0] = stretched
|
||||
self.tree.set_style(id.0, stretched) catch {
|
||||
e => engine_failure("set_style", e)
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
fn available_to_engine(
|
||||
space : AvailableSpace,
|
||||
scale_factor : Double,
|
||||
) -> @cgeometry.AvailableSpace {
|
||||
match space {
|
||||
Definite(pixels) => AvailDefinite(pixels.to_double() * scale_factor)
|
||||
MinContent => AvailMinContent
|
||||
MaxContent => AvailMaxContent
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
fn available_from_engine(
|
||||
space : @cgeometry.AvailableSpace,
|
||||
scale_factor : Double,
|
||||
) -> AvailableSpace {
|
||||
match space {
|
||||
AvailDefinite(value) => Definite(@geometry.px(value / scale_factor))
|
||||
AvailMinContent => MinContent
|
||||
AvailMaxContent => MaxContent
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
/// Computes the layout of the subtree rooted at `id` (GPUI:
|
||||
/// `compute_layout`). Measure functions receive and return logical pixels;
|
||||
/// measured sizes are rounded up to whole device pixels.
|
||||
pub fn LayoutEngine::compute_layout(
|
||||
self : LayoutEngine,
|
||||
id : LayoutId,
|
||||
available_space : @geometry.Size[AvailableSpace],
|
||||
scale_factor : Double,
|
||||
) -> Unit {
|
||||
self.check_id(id, "compute_layout")
|
||||
// Recomputing invalidates cached bounds of the whole subtree.
|
||||
if self.computed_layouts.contains(id.0) {
|
||||
let stack = [id.0]
|
||||
while stack.pop() is Some(node) {
|
||||
self.absolute_layout_bounds.remove(node)
|
||||
self.absolute_outer_origins.remove(node)
|
||||
for child in self.children.get(node).unwrap_or([]) {
|
||||
stack.push(child)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
self.computed_layouts.add(id.0)
|
||||
}
|
||||
let available = @cgeometry.Size(
|
||||
width=available_to_engine(available_space.width, scale_factor),
|
||||
height=available_to_engine(available_space.height, scale_factor),
|
||||
)
|
||||
let measure_leaf = fn(
|
||||
known : @cgeometry.Size[Double?],
|
||||
available : @cgeometry.Size[@cgeometry.AvailableSpace],
|
||||
_node : Int32,
|
||||
context : MeasureFn?,
|
||||
_style : @cstyle.Style,
|
||||
) -> @cgeometry.Size[Double] {
|
||||
match context {
|
||||
Option::None => @cgeometry.Size(width=0.0, height=0.0)
|
||||
Some(measure) => {
|
||||
let to_logical = fn(value : Double?) {
|
||||
value.map(fn(v) { @geometry.px(v / scale_factor) })
|
||||
}
|
||||
let measured = measure(
|
||||
{ width: to_logical(known.width), height: to_logical(known.height), },
|
||||
{
|
||||
width: available_from_engine(available.width, scale_factor),
|
||||
height: available_from_engine(available.height, scale_factor),
|
||||
},
|
||||
)
|
||||
let snap = fn(d : @geometry.Pixels) {
|
||||
let d = d.to_double()
|
||||
ceil_to_device_pixel(if d > 0.0 { d } else { 0.0 }, scale_factor)
|
||||
}
|
||||
@cgeometry.Size(
|
||||
width=snap(measured.width),
|
||||
height=snap(measured.height),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
self.tree.compute_layout_with_measure(id.0, available, measure_leaf) catch {
|
||||
e => engine_failure("compute_layout", e)
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
/// The bounds of a computed node, in logical pixels relative to the root's
|
||||
/// origin (GPUI: `layout_bounds`). Corners are snapped to device pixels.
|
||||
pub fn LayoutEngine::layout_bounds(
|
||||
self : LayoutEngine,
|
||||
id : LayoutId,
|
||||
scale_factor : Double,
|
||||
) -> @geometry.Bounds[@geometry.Pixels] {
|
||||
self.check_id(id, "layout_bounds")
|
||||
if self.absolute_layout_bounds.get(id.0) is Some(bounds) {
|
||||
return bounds
|
||||
}
|
||||
let layout = self.tree.layout(id.0) catch {
|
||||
e => {
|
||||
engine_failure("layout", e)
|
||||
panic()
|
||||
}
|
||||
}
|
||||
let location : @geometry.Point[Double] = {
|
||||
x: layout.location.x,
|
||||
y: layout.location.y,
|
||||
}
|
||||
let origin = match self.parents.get(id.0) {
|
||||
Some(parent) => {
|
||||
ignore(self.layout_bounds(LayoutId(parent), scale_factor))
|
||||
guard self.absolute_outer_origins.get(parent) is Some(parent_origin) else {
|
||||
abort("layout engine: parent origin should be cached")
|
||||
}
|
||||
parent_origin + location
|
||||
}
|
||||
Option::None => location
|
||||
}
|
||||
self.absolute_outer_origins[id.0] = origin
|
||||
let far = origin + { x: layout.size.width, y: layout.size.height, }
|
||||
let snap = fn(p : @geometry.Point[Double]) {
|
||||
p.map(fn(v) { @geometry.px(round_half_toward_zero(v) / scale_factor) })
|
||||
}
|
||||
let bounds = @geometry.Bounds::from_corners(snap(origin), snap(far))
|
||||
self.absolute_layout_bounds[id.0] = bounds
|
||||
bounds
|
||||
}
|
||||
@@ -0,0 +1,359 @@
|
||||
///|
|
||||
fn st(refinement : @style.StyleRefinement) -> @style.Style {
|
||||
(Default::default() : @style.Style).refined(refinement)
|
||||
}
|
||||
|
||||
///|
|
||||
fn r() -> @style.StyleRefinement {
|
||||
@style.StyleRefinement::new()
|
||||
}
|
||||
|
||||
///|
|
||||
let rem : @geometry.Pixels = @geometry.px(16.0)
|
||||
|
||||
///|
|
||||
fn bounds(
|
||||
x : Double,
|
||||
y : Double,
|
||||
w : Double,
|
||||
h : Double,
|
||||
) -> @geometry.Bounds[@geometry.Pixels] {
|
||||
@geometry.bounds(
|
||||
@geometry.point(@geometry.px(x), @geometry.px(y)),
|
||||
@geometry.size(@geometry.px(w), @geometry.px(h)),
|
||||
)
|
||||
}
|
||||
|
||||
///|
|
||||
fn definite(w : Double, h : Double) -> @geometry.Size[@layout.AvailableSpace] {
|
||||
@layout.AvailableSpace::from_size(
|
||||
@geometry.size(@geometry.px(w), @geometry.px(h)),
|
||||
)
|
||||
}
|
||||
|
||||
///|
|
||||
/// Port of GPUI's `test_round_half_toward_zero` (util.rs).
|
||||
test "round half toward zero" {
|
||||
let cases : Array[(Double, Double)] = [
|
||||
(0.5, 0.0),
|
||||
(1.5, 1.0),
|
||||
(2.5, 2.0),
|
||||
(-0.5, 0.0),
|
||||
(-1.5, -1.0),
|
||||
(-2.5, -2.0),
|
||||
(1.5001, 2.0),
|
||||
(1.4999, 1.0),
|
||||
(-1.5001, -2.0),
|
||||
(-1.4999, -1.0),
|
||||
(0.0, 0.0),
|
||||
(3.0, 3.0),
|
||||
(-3.0, -3.0),
|
||||
(2.5001, 3.0),
|
||||
]
|
||||
for case in cases {
|
||||
let (input, expected) = case
|
||||
assert_eq(@layout.round_half_toward_zero(input), expected)
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
/// Port of GPUI's `border_widths_to_taffy_use_stroke_snapping` (taffy.rs).
|
||||
test "border widths use stroke snapping" {
|
||||
let widths = @geometry.Edges::new(
|
||||
top=@geometry.px(0.0).to_absolute(),
|
||||
right=@geometry.px(0.4).to_absolute(),
|
||||
bottom=@geometry.px(0.5).to_absolute(),
|
||||
left=@geometry.px(1.6).to_absolute(),
|
||||
)
|
||||
let border = @layout.border_widths_to_engine(widths, rem, 1.0)
|
||||
assert_true(border.top is DimLength(0.0))
|
||||
assert_true(border.right is DimLength(1.0))
|
||||
assert_true(border.bottom is DimLength(1.0))
|
||||
assert_true(border.left is DimLength(2.0))
|
||||
}
|
||||
|
||||
///|
|
||||
test "flex row distributes free space" {
|
||||
let engine = @layout.LayoutEngine::new()
|
||||
let a = engine.request_layout(
|
||||
st(r().w(@geometry.px(50.0)).h_full()),
|
||||
rem,
|
||||
1.0,
|
||||
[],
|
||||
)
|
||||
let b = engine.request_layout(st(r().flex_1()), rem, 1.0, [])
|
||||
let c = engine.request_layout(
|
||||
st(r().w(@geometry.px(100.0)).h(@geometry.px(20.0))),
|
||||
rem,
|
||||
1.0,
|
||||
[],
|
||||
)
|
||||
let root = engine.request_layout(
|
||||
st(r().flex().w(@geometry.px(300.0)).h(@geometry.px(40.0))),
|
||||
rem,
|
||||
1.0,
|
||||
[a, b, c],
|
||||
)
|
||||
engine.compute_layout(root, definite(1000.0, 1000.0), 1.0)
|
||||
assert_eq(engine.layout_bounds(root, 1.0), bounds(0.0, 0.0, 300.0, 40.0))
|
||||
assert_eq(engine.layout_bounds(a, 1.0), bounds(0.0, 0.0, 50.0, 40.0))
|
||||
// flex_1 takes the remaining 150px; items stretch on the cross axis.
|
||||
assert_eq(engine.layout_bounds(b, 1.0), bounds(50.0, 0.0, 150.0, 40.0))
|
||||
assert_eq(engine.layout_bounds(c, 1.0), bounds(200.0, 0.0, 100.0, 20.0))
|
||||
assert_eq(engine.node_count(), 4)
|
||||
}
|
||||
|
||||
///|
|
||||
test "rems, padding, gap and percentages resolve" {
|
||||
let engine = @layout.LayoutEngine::new()
|
||||
let first = engine.request_layout(st(r().w_1_2().h_4()), rem, 1.0, [])
|
||||
let second = engine.request_layout(st(r().w_full().h_8()), rem, 1.0, [])
|
||||
let root = engine.request_layout(
|
||||
st(r().flex().flex_col().p_4().gap_2().w(@geometry.px(232.0))),
|
||||
rem,
|
||||
1.0,
|
||||
[first, second],
|
||||
)
|
||||
engine.compute_layout(root, definite(500.0, 500.0), 1.0)
|
||||
// p_4 = 1rem = 16px, gap_2 = 0.5rem = 8px, h_4 = 16px, h_8 = 32px.
|
||||
assert_eq(engine.layout_bounds(first, 1.0), bounds(16.0, 16.0, 100.0, 16.0))
|
||||
assert_eq(engine.layout_bounds(second, 1.0), bounds(16.0, 40.0, 200.0, 32.0))
|
||||
// Auto height: padding + children + gap.
|
||||
assert_eq(engine.layout_bounds(root, 1.0), bounds(0.0, 0.0, 232.0, 88.0))
|
||||
}
|
||||
|
||||
///|
|
||||
test "borders take space inside the box" {
|
||||
let engine = @layout.LayoutEngine::new()
|
||||
let child = engine.request_layout(st(r().size_full()), rem, 1.0, [])
|
||||
let root = engine.request_layout(
|
||||
st(r().flex().border_2().size(@geometry.px(50.0))),
|
||||
rem,
|
||||
1.0,
|
||||
[child],
|
||||
)
|
||||
engine.compute_layout(root, definite(100.0, 100.0), 1.0)
|
||||
assert_eq(engine.layout_bounds(child, 1.0), bounds(2.0, 2.0, 46.0, 46.0))
|
||||
}
|
||||
|
||||
///|
|
||||
test "layout snaps to device pixels" {
|
||||
let engine = @layout.LayoutEngine::new()
|
||||
// 10.3px at 1.5x is 15.45 device px -> 15 -> 10px logical.
|
||||
let child = engine.request_layout(
|
||||
st(r().w(@geometry.px(10.3)).h(@geometry.px(10.0))),
|
||||
rem,
|
||||
1.5,
|
||||
[],
|
||||
)
|
||||
let root = engine.request_layout(st(r().flex()), rem, 1.5, [child])
|
||||
engine.compute_layout(root, definite(100.0, 100.0), 1.5)
|
||||
let b = engine.layout_bounds(child, 1.5)
|
||||
assert_eq(b.size.width, @geometry.px(10.0))
|
||||
assert_eq(b.size.height, @geometry.px(10.0))
|
||||
// A 1px border at 0.5x is still one device pixel wide.
|
||||
assert_eq(@layout.round_stroke_to_device_pixel(1.0, 0.5), 1.0)
|
||||
assert_eq(@layout.round_stroke_to_device_pixel(0.0, 0.5), 0.0)
|
||||
}
|
||||
|
||||
///|
|
||||
test "nested bounds are absolute" {
|
||||
let engine = @layout.LayoutEngine::new()
|
||||
let leaf = engine.request_layout(st(r().size(@geometry.px(10.0))), rem, 1.0, [])
|
||||
let middle = engine.request_layout(
|
||||
st(r().flex().p(@geometry.px(5.0))),
|
||||
rem,
|
||||
1.0,
|
||||
[leaf],
|
||||
)
|
||||
let spacer = engine.request_layout(st(r().w(@geometry.px(30.0))), rem, 1.0, [])
|
||||
let root = engine.request_layout(
|
||||
st(r().flex().p(@geometry.px(7.0))),
|
||||
rem,
|
||||
1.0,
|
||||
[spacer, middle],
|
||||
)
|
||||
engine.compute_layout(root, definite(200.0, 200.0), 1.0)
|
||||
assert_eq(engine.layout_bounds(middle, 1.0), bounds(37.0, 7.0, 20.0, 20.0))
|
||||
assert_eq(engine.layout_bounds(leaf, 1.0), bounds(42.0, 12.0, 10.0, 10.0))
|
||||
// Recomputing with less space updates cached bounds.
|
||||
engine.compute_layout(root, definite(20.0, 20.0), 1.0)
|
||||
assert_eq(engine.layout_bounds(leaf, 1.0).origin.y, @geometry.px(12.0))
|
||||
}
|
||||
|
||||
///|
|
||||
test "measured leaves get logical pixels and are ceiled to device pixels" {
|
||||
let engine = @layout.LayoutEngine::new()
|
||||
let calls : Array[(@geometry.Pixels?, @layout.AvailableSpace)] = []
|
||||
// A fake paragraph: 7.25px per character, wrapping at the available width,
|
||||
// 20px per line.
|
||||
let measure = fn(
|
||||
known : @geometry.Size[@geometry.Pixels?],
|
||||
available : @geometry.Size[@layout.AvailableSpace],
|
||||
) -> @geometry.Size[@geometry.Pixels] {
|
||||
calls.push((known.width, available.width))
|
||||
let text_width = 40.0 * 7.25
|
||||
let wrap = match (known.width, available.width) {
|
||||
(Some(width), _) => width.to_double()
|
||||
(None, Definite(width)) => width.to_double()
|
||||
(None, _) => text_width
|
||||
}
|
||||
let width = if text_width < wrap { text_width } else { wrap }
|
||||
let lines = (text_width / wrap).ceil()
|
||||
@geometry.size(@geometry.px(width), @geometry.px(lines * 20.0))
|
||||
}
|
||||
let text = engine.request_measured_layout(st(r()), rem, 2.0, measure)
|
||||
let root = engine.request_layout(
|
||||
st(r().flex().flex_col().w(@geometry.px(100.0))),
|
||||
rem,
|
||||
2.0,
|
||||
[text],
|
||||
)
|
||||
engine.compute_layout(root, definite(800.0, 600.0), 2.0)
|
||||
let b = engine.layout_bounds(text, 2.0)
|
||||
assert_eq(b.size.width, @geometry.px(100.0))
|
||||
// 290px of text in 100px -> 3 lines.
|
||||
assert_eq(b.size.height, @geometry.px(60.0))
|
||||
assert_true(calls.length() > 0)
|
||||
// The measure function sees logical, not device, pixels.
|
||||
for call in calls {
|
||||
match call {
|
||||
(Some(width), _) => assert_eq(width, @geometry.px(100.0))
|
||||
(None, Definite(width)) => assert_true(width.to_double() <= 800.0)
|
||||
_ => ()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
test "measured sizes round up to whole device pixels" {
|
||||
let engine = @layout.LayoutEngine::new()
|
||||
let leaf = engine.request_measured_layout(st(r()), rem, 2.0, fn(_, _) {
|
||||
@geometry.size(@geometry.px(10.2), @geometry.px(-5.0))
|
||||
})
|
||||
// items_start: keep the measured height instead of stretching.
|
||||
let root = engine.request_layout(st(r().flex().items_start()), rem, 2.0, [
|
||||
leaf,
|
||||
])
|
||||
engine.compute_layout(root, definite(100.0, 100.0), 2.0)
|
||||
let b = engine.layout_bounds(leaf, 2.0)
|
||||
// ceil(10.2 * 2) = 21 device px = 10.5 logical; negative clamps to 0.
|
||||
assert_eq(b.size.width, @geometry.px(10.5))
|
||||
assert_eq(b.size.height, @geometry.px(0.0))
|
||||
}
|
||||
|
||||
///|
|
||||
test "stretch auto size to fill" {
|
||||
let engine = @layout.LayoutEngine::new()
|
||||
let child = engine.request_layout(st(r().flex_1()), rem, 1.0, [])
|
||||
let root = engine.request_layout(st(r().flex()), rem, 1.0, [child])
|
||||
engine.stretch_auto_size_to_fill(
|
||||
root,
|
||||
@geometry.size(@geometry.px(640.0), @geometry.px(480.0)),
|
||||
1.0,
|
||||
)
|
||||
engine.compute_layout(root, @layout.AvailableSpace::min_size(), 1.0)
|
||||
assert_eq(engine.layout_bounds(root, 1.0), bounds(0.0, 0.0, 640.0, 480.0))
|
||||
assert_eq(engine.layout_bounds(child, 1.0), bounds(0.0, 0.0, 640.0, 480.0))
|
||||
// A definite size is left alone.
|
||||
let fixed = engine.request_layout(
|
||||
st(r().size(@geometry.px(10.0))),
|
||||
rem,
|
||||
1.0,
|
||||
[],
|
||||
)
|
||||
engine.stretch_auto_size_to_fill(
|
||||
fixed,
|
||||
@geometry.size(@geometry.px(640.0), @geometry.px(480.0)),
|
||||
1.0,
|
||||
)
|
||||
engine.compute_layout(fixed, @layout.AvailableSpace::min_size(), 1.0)
|
||||
assert_eq(engine.layout_bounds(fixed, 1.0).size.width, @geometry.px(10.0))
|
||||
}
|
||||
|
||||
///|
|
||||
test "grid columns and placement" {
|
||||
let engine = @layout.LayoutEngine::new()
|
||||
let cells = [
|
||||
engine.request_layout(st(r().h(@geometry.px(10.0))), rem, 1.0, []),
|
||||
engine.request_layout(st(r().h(@geometry.px(10.0)).col_span(2)), rem, 1.0, []),
|
||||
engine.request_layout(st(r().h(@geometry.px(10.0))), rem, 1.0, []),
|
||||
]
|
||||
let root = engine.request_layout(
|
||||
st(r().grid().grid_cols(3).w(@geometry.px(300.0))),
|
||||
rem,
|
||||
1.0,
|
||||
cells,
|
||||
)
|
||||
engine.compute_layout(root, definite(1000.0, 1000.0), 1.0)
|
||||
assert_eq(engine.layout_bounds(cells[0], 1.0), bounds(0.0, 0.0, 100.0, 10.0))
|
||||
assert_eq(
|
||||
engine.layout_bounds(cells[1], 1.0),
|
||||
bounds(100.0, 0.0, 200.0, 10.0),
|
||||
)
|
||||
assert_eq(engine.layout_bounds(cells[2], 1.0), bounds(0.0, 10.0, 100.0, 10.0))
|
||||
}
|
||||
|
||||
///|
|
||||
test "absolute positioning and display none" {
|
||||
let engine = @layout.LayoutEngine::new()
|
||||
let overlay = engine.request_layout(
|
||||
st(
|
||||
r()
|
||||
.absolute()
|
||||
.top(@geometry.px(5.0))
|
||||
.right(@geometry.px(5.0))
|
||||
.size(@geometry.px(20.0)),
|
||||
),
|
||||
rem,
|
||||
1.0,
|
||||
[],
|
||||
)
|
||||
let gone = engine.request_layout(
|
||||
st(r().hidden().size(@geometry.px(50.0))),
|
||||
rem,
|
||||
1.0,
|
||||
[],
|
||||
)
|
||||
let root = engine.request_layout(
|
||||
st(r().flex().relative().size(@geometry.px(100.0))),
|
||||
rem,
|
||||
1.0,
|
||||
[overlay, gone],
|
||||
)
|
||||
engine.compute_layout(root, definite(500.0, 500.0), 1.0)
|
||||
assert_eq(engine.layout_bounds(overlay, 1.0), bounds(75.0, 5.0, 20.0, 20.0))
|
||||
assert_eq(engine.layout_bounds(gone, 1.0).size.width, @geometry.px(0.0))
|
||||
}
|
||||
|
||||
///|
|
||||
test "alignment and justification" {
|
||||
let engine = @layout.LayoutEngine::new()
|
||||
let child = engine.request_layout(
|
||||
st(r().size(@geometry.px(20.0))),
|
||||
rem,
|
||||
1.0,
|
||||
[],
|
||||
)
|
||||
let root = engine.request_layout(
|
||||
st(r().flex().items_center().justify_center().size(@geometry.px(100.0))),
|
||||
rem,
|
||||
1.0,
|
||||
[child],
|
||||
)
|
||||
engine.compute_layout(root, definite(500.0, 500.0), 1.0)
|
||||
assert_eq(engine.layout_bounds(child, 1.0), bounds(40.0, 40.0, 20.0, 20.0))
|
||||
}
|
||||
|
||||
///|
|
||||
test "clear starts a new frame" {
|
||||
let engine = @layout.LayoutEngine::new()
|
||||
let a = engine.request_layout(st(r().size(@geometry.px(5.0))), rem, 1.0, [])
|
||||
engine.compute_layout(a, @layout.AvailableSpace::min_size(), 1.0)
|
||||
assert_eq(engine.node_count(), 1)
|
||||
engine.clear()
|
||||
assert_eq(engine.node_count(), 0)
|
||||
let b = engine.request_layout(st(r().size(@geometry.px(7.0))), rem, 1.0, [])
|
||||
engine.compute_layout(b, @layout.AvailableSpace::min_size(), 1.0)
|
||||
assert_eq(engine.layout_bounds(b, 1.0).size.width, @geometry.px(7.0))
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
import {
|
||||
"experiments/gpui/geometry",
|
||||
"experiments/gpui/style",
|
||||
"Milky2018/chicle/geometry" @cgeometry,
|
||||
"Milky2018/chicle/style" @cstyle,
|
||||
"Milky2018/chicle/tree" @ctree,
|
||||
"moonbitlang/core/hashset",
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
// Generated using `moon info`, DON'T EDIT IT
|
||||
package "experiments/gpui/layout"
|
||||
|
||||
import {
|
||||
"moonbitlang/core/debug",
|
||||
}
|
||||
|
||||
// Values
|
||||
pub fn border_widths_to_engine(@experiments/gpui/geometry.Edges[@experiments/gpui/geometry.AbsoluteLength], @experiments/gpui/geometry.Pixels, Double) -> @Milky2018/chicle/geometry.Rect[@Milky2018/chicle/style.Dimension]
|
||||
|
||||
pub fn ceil_to_device_pixel(Double, Double) -> Double
|
||||
|
||||
pub fn round_half_toward_zero(Double) -> Double
|
||||
|
||||
pub fn round_stroke_to_device_pixel(Double, Double) -> Double
|
||||
|
||||
pub fn round_to_device_pixel(Double, Double) -> Double
|
||||
|
||||
pub fn style_to_engine(@experiments/gpui/style.Style, @experiments/gpui/geometry.Pixels, Double) -> @Milky2018/chicle/style.Style
|
||||
|
||||
// Errors
|
||||
|
||||
// Types and methods
|
||||
#derive(Eq, Hash, @debug.Debug)
|
||||
pub(all) enum AvailableSpace {
|
||||
Definite(@experiments/gpui/geometry.Pixels)
|
||||
MinContent
|
||||
MaxContent
|
||||
}
|
||||
pub fn AvailableSpace::from_size(@experiments/gpui/geometry.Size[@experiments/gpui/geometry.Pixels]) -> @experiments/gpui/geometry.Size[Self]
|
||||
pub fn AvailableSpace::min_size() -> @experiments/gpui/geometry.Size[Self]
|
||||
pub impl Default for AvailableSpace
|
||||
|
||||
pub struct LayoutEngine {
|
||||
// private fields
|
||||
}
|
||||
pub fn LayoutEngine::clear(Self) -> Unit
|
||||
pub fn LayoutEngine::compute_layout(Self, LayoutId, @experiments/gpui/geometry.Size[AvailableSpace], Double) -> Unit
|
||||
pub fn LayoutEngine::layout_bounds(Self, LayoutId, Double) -> @experiments/gpui/geometry.Bounds[@experiments/gpui/geometry.Pixels]
|
||||
pub fn LayoutEngine::new() -> Self
|
||||
pub fn LayoutEngine::node_count(Self) -> Int32
|
||||
pub fn LayoutEngine::request_layout(Self, @experiments/gpui/style.Style, @experiments/gpui/geometry.Pixels, Double, Array[LayoutId]) -> LayoutId
|
||||
pub fn LayoutEngine::request_measured_layout(Self, @experiments/gpui/style.Style, @experiments/gpui/geometry.Pixels, Double, (@experiments/gpui/geometry.Size[@experiments/gpui/geometry.Pixels?], @experiments/gpui/geometry.Size[AvailableSpace]) -> @experiments/gpui/geometry.Size[@experiments/gpui/geometry.Pixels]) -> LayoutId
|
||||
pub fn LayoutEngine::stretch_auto_size_to_fill(Self, LayoutId, @experiments/gpui/geometry.Size[@experiments/gpui/geometry.Pixels], Double) -> Unit
|
||||
|
||||
#derive(Compare, Eq, Hash, @debug.Debug)
|
||||
pub(all) struct LayoutId(Int32)
|
||||
|
||||
// Type aliases
|
||||
pub type MeasureFn = (@experiments/gpui/geometry.Size[@experiments/gpui/geometry.Pixels?], @experiments/gpui/geometry.Size[AvailableSpace]) -> @experiments/gpui/geometry.Size[@experiments/gpui/geometry.Pixels]
|
||||
|
||||
// Traits
|
||||
Reference in new issue
Block a user