Add style, font, and card example packages; extend geometry and color

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
SpriteandClaude Sonnet 5.5 committed 2026-11-24 21:44:05 +00:00
1 parent 8c0685fe01
commit 1e2bcde532
26 files changed
+45666 -30

No files matched your search

+132
View File
@@ -0,0 +1,132 @@
// 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.
//
// 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()
}
///|
/// 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,
}),
)
}
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),
}),
)
}
///|
/// 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 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 scene = @scene.Scene::new()
paint_card(scene, style, bounds, rem_size, scale_factor)
// A title underline, overlapping the card.
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)),
).scale(scale_factor),
content_mask: { bounds: bounds.scale(scale_factor), },
color: text.color,
thickness: @geometry.ScaledPixels(2.0),
wavy: false,
}),
)
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}, color \{text.color.to_rgb()}",
"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",
)
}
lines.join("\n")
}
///|
fn main {
println(report())
}
+19
View File
@@ -0,0 +1,19 @@
///|
test "card report" {
inspect(
report(),
content=(
#|card style
#| display: Flex, direction: Column
#| width: 320px, padding: 1rem, gap: 0.5rem
#| radius: 0.5rem = 8px
#| shadows: 2
#| text: 14px .SystemUIFont, color #1f2328ff
#|scene at 2x
#| shadows (2)
#| quads (1)
#| underlines (1)
#| quad order 1: 640 x 240 device px
),
)
}
+9
View File
@@ -0,0 +1,9 @@
import {
"experiments/gpui/geometry",
"experiments/gpui/color",
"experiments/gpui/scene",
"experiments/gpui/style",
"moonbitlang/core/debug",
}
pkgtype(kind: "executable")
+12
View File
@@ -0,0 +1,12 @@
// Generated using `moon info`, DON'T EDIT IT
package "experiments/gpui/examples/card"
// Values
// Errors
// Types and methods
// Type aliases
// Traits