Add porting plan and first pure slice: geometry, color, scene

docs/PORTING.md maps GPUI's architecture onto MoonBit packages, separates
pure logic from platform code, surveys mooncakes packages and their backends,
recommends a js + Canvas2D first backend behind a small Platform trait, lists
the Rust idioms that need a different design, and gives an ordered roadmap.

First slice (pure, tested on wasm, wasm-gc, js and native):
- geometry: Pixels/ScaledPixels/DevicePixels/Rems, Absolute/Definite/Length
  with parsing, Point/Size/Bounds/Edges/Corners, Anchor, Axis, ContentMask
- color: Rgba/Hsla with conversions, hex parsing, blending, Background
  (solid, linear gradient, patterns)
- scene: primitives (Quad, Shadow, Path, Underline, sprites), transformation
  matrix, BoundsTree draw ordering, layers, finish/batches, replay

Derived from Zed's GPUI (Apache-2.0); see LICENSE and NOTICE.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
SpriteandClaude Opus 5.5 committed 2026-11-24 21:23:48 +00:00
1 parent b9d1949688
commit 8c0685fe01
24 files changed
+5856 -1

No files matched your search

+437
View File
@@ -0,0 +1,437 @@
// Scene primitives, ported from gpui/src/scene.rs (and AtlasTile & co. from
// gpui/src/platform.rs).
//
// Deviations from GPUI (see docs/PORTING.md, "Scene"):
// - Primitives are plain immutable records; GPUI's `#[repr(C)]` padding fields
// (`pad`, `PaddedBool32`) are dropped. A renderer packs them into GPU
// buffers explicitly.
// - `PaintedPath` stores flattened polygon contours instead of GPUI's tiled
// path representation (`PathTiles`, built by lyon in `path_builder.rs`);
// tiling belongs to the renderer milestone.
// - `PaintSurface` (macOS CVPixelBuffer) and `PaintNativeView` (embedded native
// views) are not ported yet. Sprite animations are not ported yet.
///|
type ScaledPixels = @geometry.ScaledPixels
///|
type Bounds = @geometry.Bounds[ScaledPixels]
///|
type ContentMask = @geometry.ContentMask[ScaledPixels]
///|
/// The kind of a primitive. The declaration order is significant: it breaks
/// ties between primitives with equal draw order (GPUI derives `Ord`).
#derive(Eq, Compare, Hash, Debug)
pub(all) enum PrimitiveKind {
Shadow
Quad
Path
Underline
MonochromeSprite
SubpixelSprite
PolychromeSprite
}
///|
/// The highest kind that may share a draw order with this kind without being
/// drawn in the wrong order. Glyph sprites can share a level with underlines
/// and with each other because they are always drawn after them.
pub fn PrimitiveKind::max_shared_kind(self : PrimitiveKind) -> PrimitiveKind {
match self {
MonochromeSprite => Underline
SubpixelSprite => MonochromeSprite
PolychromeSprite => SubpixelSprite
kind => kind
}
}
///|
#derive(Eq, Hash, Debug)
pub(all) enum BorderStyle {
Solid
Dashed
}
///|
pub impl Default for BorderStyle with fn default() {
Solid
}
///|
/// A 2D affine transformation: `[[xx, xy], [yx, yy]]` rotation/scale plus a
/// translation `(tx, ty)`.
#derive(Eq, Debug)
pub(all) struct TransformationMatrix {
xx : Double
xy : Double
yx : Double
yy : Double
tx : Double
ty : Double
}
///|
pub fn TransformationMatrix::unit() -> TransformationMatrix {
{ xx: 1.0, xy: 0.0, yx: 0.0, yy: 1.0, tx: 0.0, ty: 0.0, }
}
///|
pub impl Default for TransformationMatrix with fn default() {
TransformationMatrix::unit()
}
///|
/// `self` followed by `other` applied first (matrix product `self * other`).
pub fn TransformationMatrix::compose(
self : TransformationMatrix,
other : TransformationMatrix,
) -> TransformationMatrix {
if other == TransformationMatrix::unit() {
return self
}
{
xx: self.xx * other.xx + self.xy * other.yx,
xy: self.xx * other.xy + self.xy * other.yy,
yx: self.yx * other.xx + self.yy * other.yx,
yy: self.yx * other.xy + self.yy * other.yy,
tx: self.tx + self.xx * other.tx + self.xy * other.ty,
ty: self.ty + self.yx * other.tx + self.yy * other.ty,
}
}
///|
pub fn TransformationMatrix::translate(
self : TransformationMatrix,
point : @geometry.Point[ScaledPixels],
) -> TransformationMatrix {
self.compose({ ..TransformationMatrix::unit(), tx: point.x.0, ty: point.y.0, })
}
///|
pub fn TransformationMatrix::rotate(
self : TransformationMatrix,
angle : @geometry.Radians,
) -> TransformationMatrix {
let cos = @math.cos(angle.0)
let sin = @math.sin(angle.0)
self.compose({ xx: cos, xy: -sin, yx: sin, yy: cos, tx: 0.0, ty: 0.0, })
}
///|
pub fn TransformationMatrix::scale(
self : TransformationMatrix,
size : @geometry.Size[Double],
) -> TransformationMatrix {
self.compose({
xx: size.width,
xy: 0.0,
yx: 0.0,
yy: size.height,
tx: 0.0,
ty: 0.0,
})
}
///|
/// Applies the transformation to a point.
pub fn TransformationMatrix::apply(
self : TransformationMatrix,
point : @geometry.Point[@geometry.Pixels],
) -> @geometry.Point[@geometry.Pixels] {
{
x: @geometry.px(self.tx + self.xx * point.x.0 + self.xy * point.y.0),
y: @geometry.px(self.ty + self.yx * point.x.0 + self.yy * point.y.0),
}
}
///|
/// The axis-aligned bounding box of the transformed bounds.
pub fn TransformationMatrix::transform_bounds(
self : TransformationMatrix,
bounds : Bounds,
) -> Bounds {
if self == TransformationMatrix::unit() {
return bounds
}
let transform = fn(
corner : @geometry.Point[ScaledPixels],
) -> @geometry.Point[ScaledPixels] {
{
x: ScaledPixels(self.xx * corner.x.0 + self.xy * corner.y.0 + self.tx),
y: ScaledPixels(self.yx * corner.x.0 + self.yy * corner.y.0 + self.ty),
}
}
let first = transform(bounds.origin)
let mut top_left = first
let mut bottom_right = first
for
corner in [bounds.top_right(), bounds.bottom_left(), bounds.bottom_right()] {
let p = transform(corner)
top_left = top_left.min(p)
bottom_right = bottom_right.max(p)
}
@geometry.Bounds::from_corners(top_left, bottom_right)
}
// ---------------------------------------------------------------------------
// Atlas references (GPUI: platform.rs). Will move to the platform package once
// it exists; kept here because sprites reference them.
// ---------------------------------------------------------------------------
///|
#derive(Eq, Compare, Hash, Debug)
pub(all) enum AtlasTextureKind {
Monochrome
Polychrome
Subpixel
}
///|
#derive(Eq, Hash, Debug)
pub(all) struct AtlasTextureId {
/// The index of this texture in the atlas.
index : Int32
/// The kind of content stored in this texture.
kind : AtlasTextureKind
}
///|
/// A rectangle allocated in an atlas texture.
#derive(Eq, Hash, Debug)
pub(all) struct AtlasTile {
texture_id : AtlasTextureId
tile_id : Int32
/// Padding around the tile content, in device pixels.
padding : Int32
bounds : @geometry.Bounds[@geometry.DevicePixels]
}
// ---------------------------------------------------------------------------
// Primitives
// ---------------------------------------------------------------------------
///|
/// A rectangle with optional rounded corners, borders and a fill.
#derive(Eq, Default, Debug)
pub(all) struct Quad {
order : Int32
border_style : BorderStyle
bounds : Bounds
content_mask : ContentMask
background : @color.Background
border_color : @color.Hsla
corner_radii : @geometry.Corners[ScaledPixels]
border_widths : @geometry.Edges[ScaledPixels]
}
///|
/// A (box) shadow cast by, or inset into, a rounded rectangle.
#derive(Eq, Default, Debug)
pub(all) struct Shadow {
order : Int32
blur_radius : ScaledPixels
bounds : Bounds
corner_radii : @geometry.Corners[ScaledPixels]
content_mask : ContentMask
color : @color.Hsla
/// The shape casting the shadow (used to clip inset shadows).
shape_bounds : Bounds
shape_corner_radii : @geometry.Corners[ScaledPixels]
inset : Bool
}
///|
/// A straight or wavy text underline (also used for strikethrough).
#derive(Eq, Default, Debug)
pub(all) struct Underline {
order : Int32
bounds : Bounds
content_mask : ContentMask
color : @color.Hsla
thickness : ScaledPixels
wavy : Bool
}
///|
/// A single-channel (alpha mask) sprite tinted with `color`, e.g. a glyph.
#derive(Eq, Debug)
pub(all) struct MonochromeSprite {
order : Int32
tile : AtlasTile
bounds : Bounds
content_mask : ContentMask
color : @color.Hsla
transformation : TransformationMatrix
}
///|
/// A glyph rendered with per-channel (subpixel) coverage.
#derive(Eq, Debug)
pub(all) struct SubpixelSprite {
order : Int32
tile : AtlasTile
bounds : Bounds
content_mask : ContentMask
color : @color.Hsla
transformation : TransformationMatrix
}
///|
/// A full-color sprite, e.g. an image or an emoji.
#derive(Eq, Debug)
pub(all) struct PolychromeSprite {
order : Int32
tile : AtlasTile
grayscale : Bool
opacity : Double
bounds : Bounds
content_mask : ContentMask
corner_radii : @geometry.Corners[ScaledPixels]
}
///|
/// A filled vector path made of closed polygon contours.
#derive(Eq, Debug)
pub(all) struct PaintedPath {
order : Int32
/// Bounding box of all contours.
bounds : Bounds
content_mask : ContentMask
color : @color.Background
/// Even-odd fill rule if true, non-zero winding otherwise.
even_odd : Bool
contours : Array[Array[@geometry.Point[ScaledPixels]]]
}
///|
/// Creates a path primitive from contours given in logical pixels.
///
/// Fails loudly if there are no points: an empty path has no bounds and would
/// silently disappear.
pub fn PaintedPath::new(
contours : Array[Array[@geometry.Point[@geometry.Pixels]]],
scale_factor : Double,
content_mask : @geometry.ContentMask[@geometry.Pixels],
color : @color.Background,
even_odd~ : Bool = false,
) -> PaintedPath {
let scaled = contours.map(fn(contour) {
contour.map(fn(p) { p.scale(scale_factor) })
})
let mut top_left : @geometry.Point[ScaledPixels]? = None
let mut bottom_right : @geometry.Point[ScaledPixels]? = None
for contour in scaled {
for p in contour {
top_left = Some(
match top_left {
None => p
Some(tl) => tl.min(p)
},
)
bottom_right = Some(
match bottom_right {
None => p
Some(br) => br.max(p)
},
)
}
}
guard top_left is Some(tl) && bottom_right is Some(br) else {
abort("PaintedPath::new: path has no points")
}
{
order: 0,
bounds: @geometry.Bounds::from_corners(tl, br),
content_mask: content_mask.scale(scale_factor),
color,
even_odd,
contours: scaled,
}
}
///|
/// Any primitive that can be inserted into a `Scene`.
#derive(Eq, Debug)
pub(all) enum Primitive {
Shadow(Shadow)
Quad(Quad)
Path(PaintedPath)
Underline(Underline)
MonochromeSprite(MonochromeSprite)
SubpixelSprite(SubpixelSprite)
PolychromeSprite(PolychromeSprite)
}
///|
pub fn Primitive::kind(self : Primitive) -> PrimitiveKind {
match self {
Shadow(_) => PrimitiveKind::Shadow
Quad(_) => PrimitiveKind::Quad
Path(_) => PrimitiveKind::Path
Underline(_) => PrimitiveKind::Underline
MonochromeSprite(_) => PrimitiveKind::MonochromeSprite
SubpixelSprite(_) => PrimitiveKind::SubpixelSprite
PolychromeSprite(_) => PrimitiveKind::PolychromeSprite
}
}
///|
pub fn Primitive::bounds(self : Primitive) -> Bounds {
match self {
Shadow(p) => p.bounds
Quad(p) => p.bounds
Path(p) => p.bounds
Underline(p) => p.bounds
MonochromeSprite(p) => p.bounds
SubpixelSprite(p) => p.bounds
PolychromeSprite(p) => p.bounds
}
}
///|
pub fn Primitive::content_mask(self : Primitive) -> ContentMask {
match self {
Shadow(p) => p.content_mask
Quad(p) => p.content_mask
Path(p) => p.content_mask
Underline(p) => p.content_mask
MonochromeSprite(p) => p.content_mask
SubpixelSprite(p) => p.content_mask
PolychromeSprite(p) => p.content_mask
}
}
///|
fn Primitive::transformation(self : Primitive) -> TransformationMatrix {
match self {
MonochromeSprite(p) => p.transformation
SubpixelSprite(p) => p.transformation
_ => TransformationMatrix::unit()
}
}
///|
/// The bounds actually affected by this primitive: transformed bounds clipped
/// to the content mask.
pub fn Primitive::clipped_bounds(self : Primitive) -> Bounds {
self
.transformation()
.transform_bounds(self.bounds())
.intersect(self.content_mask().bounds)
}
///|
/// The same primitive with its draw order replaced.
fn Primitive::with_order(self : Primitive, order : Int32) -> Primitive {
match self {
Shadow(p) => Shadow({ ..p, order, })
Quad(p) => Quad({ ..p, order, })
Path(p) => Path({ ..p, order, })
Underline(p) => Underline({ ..p, order, })
MonochromeSprite(p) => MonochromeSprite({ ..p, order, })
SubpixelSprite(p) => SubpixelSprite({ ..p, order, })
PolychromeSprite(p) => PolychromeSprite({ ..p, order, })
}
}