M6 elements and window: Element/Render/views, headless Window, div, canvas

- input: mouse events, modifiers, dispatch phases.
- window: Element / AnyElement / IntoElement, Render / AnyView (entities
  render as elements), listener(), empty(); Window with the draw cycle
  (render, layout to fill the viewport, prepaint, paint, finish) and phase
  checks; content-mask / element-offset / opacity / element-id stacks;
  element state across frames (ElementStateKey); hitboxes, hit testing,
  occlusion; mouse dispatch (capture + bubble, stop_propagation); hover-
  and notify-driven invalidation of rendered views; painting: paint_quad
  with device-pixel snapping, drop/inset shadows, paint_path, paint_style
  (port of Style::paint).
- elements: div() (Styled, children, hover, mouse down/up/move/scroll,
  clicks with element state, occlude, overflow clipping, opacity) and
  canvas(); Styled extend lists generated by tools/gen_style.py.
- app: observe_notifications hook used by windows.
- Tests: end-to-end views -> scene, clicks, hover, invalidation, nested
  views, bubbling, occlusion, shadows/borders/clipping, canvas, resize,
  and aborts on misuse. The card example now renders a real view and
  simulates hover and click.
- Docs: PORTING.md M6 status and deviations; README.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
SpriteandClaude Opus 5.5 committed 2026-11-24 22:55:17 +00:00
1 parent eeab51348e
commit fbe4c081e6
24 files changed
+15532 -204

No files matched your search

+95 -159
View File
@@ -1,187 +1,123 @@
// 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.
// A headless example of what exists so far (M0-M6): a view renders a card
// with GPUI's `div()` and fluent styles; the window lays it out, paints it
// into a Scene and dispatches a simulated click. There is no renderer or
// text yet (text lines are placeholder bars), so the output is a report of
// the scene's draw batches and quads.
//
// Run with: moon run src/examples/card --target js
///|
/// The card's style, written exactly as in GPUI:
/// `div().flex().p_4().rounded_lg().border_1().shadow_md().bg(...)`.
fn card_style() -> @style.StyleRefinement {
@style.StyleRefinement::new()
.flex()
.flex_col()
.gap_2()
.p_4()
.w(@geometry.px(320.0))
.rounded_lg()
.border_1()
.border_color(@color.rgb(0xd0d7de))
.bg(@color.rgb(0xffffff))
.shadow_md()
.text_color(@color.rgb(0x1f2328))
.text_sm()
struct Card {
mut expanded : Bool
mut clicks : Int32
}
///|
/// Paints the resolved style's background, border and shadows, the way
/// GPUI's `Style::paint` will once the Window exists (M6).
fn paint_card(
scene : @scene.Scene,
style : @style.Style,
bounds : @geometry.Bounds[@geometry.Pixels],
rem_size : @geometry.Pixels,
scale_factor : Double,
) -> Unit {
let corner_radii = style.corner_radii
.absolute_to_pixels(rem_size)
.clamp_radii_for_quad_size(bounds.size)
let mask : @geometry.ContentMask[@geometry.Pixels] = {
bounds: bounds.dilate(@geometry.px(64.0)),
}
for shadow in style.box_shadow {
let shadow_bounds = bounds
.translate(shadow.offset)
.dilate(shadow.spread_radius)
scene.insert_primitive(
Shadow({
order: 0,
blur_radius: shadow.blur_radius.scale(scale_factor),
bounds: shadow_bounds.scale(scale_factor),
corner_radii: corner_radii.scale(scale_factor),
content_mask: mask.scale(scale_factor),
color: shadow.color,
shape_bounds: bounds.scale(scale_factor),
shape_corner_radii: corner_radii.scale(scale_factor),
inset: false,
/// A placeholder for a line of text (the text system arrives in M9).
fn text_bar(width : Double, color : UInt32) -> @elements.Div {
@elements.div()
.w(@geometry.px(width))
.h(@geometry.px(10.0))
.rounded_sm()
.bg(@color.rgb(color))
}
///|
impl @window.Render for Card with fn render(self, _window, cx) {
let body_lines = if self.expanded { 4 } else { 1 }
let card = @elements.div()
.id("card")
.flex()
.flex_col()
.gap_2()
.p_4()
.w(@geometry.px(320.0))
.rounded_lg()
.border_1()
.border_color(@color.rgb(0xd0d7de))
.bg(@color.rgb(0xffffff))
.shadow_md()
.hover(fn(style) { style.border_color(@color.rgb(0x0969da)) })
.on_click(
@window.listener(cx, fn(card, _, _, cx) {
card.expanded = !card.expanded
card.clicks += 1
cx.notify()
}),
)
.child(text_bar(160.0, 0x1f2328))
for _ in 0..<body_lines {
ignore(card.child(text_bar(280.0, 0x656d76)))
}
let background = match style.background {
Some(fill) => fill.color().unwrap()
None => Default::default()
}
scene.insert_primitive(
Quad({
order: 0,
border_style: style.border_style,
bounds: bounds.scale(scale_factor),
content_mask: mask.scale(scale_factor),
background,
border_color: style.border_color.unwrap_or(@color.transparent_black()),
corner_radii: corner_radii.scale(scale_factor),
border_widths: style.border_widths
.absolute_to_pixels(rem_size)
.scale(scale_factor),
}),
)
@elements.div()
.size_full()
.flex()
.items_start()
.p(@geometry.px(40.0))
.bg(@color.rgb(0xf6f8fa))
.child(card)
.into_any_element()
}
///|
/// 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
fn describe(
label : String,
scene : @scene.Scene,
lines : Array[String],
) -> Unit {
lines.push(label)
let batches = scene.batches().map(fn(b) { b.label() }).join(", ")
lines.push(" batches: \{batches}")
for quad in scene.quads {
let b = quad.bounds
let extra = if quad.border_widths.top.to_double() > 0.0 {
" border \{quad.border_color.to_rgb()}"
} else {
""
}
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))
lines.push(
" quad (\{b.origin.x.to_double()}, \{b.origin.y.to_double()}) \{b.size.width.to_double()} x \{b.size.height.to_double()}\{extra}",
)
}
}
///|
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 {
let rem_size = @geometry.px(16.0)
let scale_factor = 2.0
let style = (Default::default() : @style.Style).refined(card_style())
let text = (Default::default() : @style.TextStyle).refined(style.text)
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 app = @app.App::new()
let window = @window.Window::new(
app,
@geometry.size(@geometry.px(400.0), @geometry.px(240.0)),
)
let body = engine.request_measured_layout(
leaf_style,
rem_size,
scale_factor,
fake_text(90, 7.0, line_height),
let card : @app.Entity[Card] = app.new_entity(fn(_) {
{ expanded: false, clicks: 0, }
})
window.set_root_view(card)
let lines = []
describe("first frame", window.draw(), lines)
// Hover the card, then click it: the view expands and the window redraws.
let inside = @geometry.point(@geometry.px(100.0), @geometry.px(60.0))
ignore(
window.dispatch_mouse_event(
MouseMove({ ..Default::default(), position: inside, }),
),
)
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)),
ignore(window.draw_if_dirty())
ignore(
window.dispatch_mouse_event(
MouseDown({ ..Default::default(), position: inside, click_count: 1, }),
),
)
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,
ignore(
window.dispatch_mouse_event(
MouseUp({ ..Default::default(), position: inside, click_count: 1, }),
),
)
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, card_bounds, rem_size, scale_factor)
scene.insert_primitive(
Underline({
order: 0,
bounds: @geometry.bounds(
title_bounds.bottom_left(),
@geometry.size(title_bounds.size.width, @geometry.px(1.0)),
).scale(scale_factor),
content_mask: { bounds: card_bounds.scale(scale_factor), },
color: text.color,
thickness: @geometry.ScaledPixels(2.0),
wavy: false,
}),
let state = card.read(app)
lines.push(
"clicked: expanded = \{state.expanded}, clicks = \{state.clicks}, dirty = \{window.is_dirty()}",
)
scene.finish()
let lines = [
"card style",
" display: \{@debug.to_string(style.display)}, direction: \{@debug.to_string(style.flex_direction)}",
" 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}, 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() {
lines.push(" \{batch.label()}")
}
for quad in scene.quads {
lines.push(
" quad order \{quad.order}: \{quad.bounds.size.width.0} x \{quad.bounds.size.height.0} device px",
)
}
describe("after click (hovered)", window.draw(), lines)
lines.join("\n")
}