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:
1 parent
b9d1949688
commit
8c0685fe01
24 files changed
+5856
-1
No files matched your search
@@ -0,0 +1,503 @@
|
||||
// Bounds, Anchor and ContentMask, ported from gpui/src/geometry.rs and
|
||||
// gpui/src/window.rs (ContentMask).
|
||||
|
||||
///|
|
||||
/// A rectangle given by its top-left origin and its size.
|
||||
#derive(Eq, Hash, Default, Debug)
|
||||
pub(all) struct Bounds[T] {
|
||||
origin : Point[T]
|
||||
size : Size[T]
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn[T] bounds(origin : Point[T], size : Size[T]) -> Bounds[T] {
|
||||
{ origin, size, }
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn[T] Bounds::new(origin : Point[T], size : Size[T]) -> Bounds[T] {
|
||||
{ origin, size, }
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn[T : Sub] Bounds::from_corners(
|
||||
top_left : Point[T],
|
||||
bottom_right : Point[T],
|
||||
) -> Bounds[T] {
|
||||
{
|
||||
origin: top_left,
|
||||
size: {
|
||||
width: bottom_right.x - top_left.x,
|
||||
height: bottom_right.y - top_left.y,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
/// A position in a rectangle: one of its four corners or the middle of one of
|
||||
/// its four sides.
|
||||
#derive(Eq, Hash, Debug)
|
||||
pub(all) enum Anchor {
|
||||
TopLeft
|
||||
TopRight
|
||||
BottomLeft
|
||||
BottomRight
|
||||
TopCenter
|
||||
BottomCenter
|
||||
LeftCenter
|
||||
RightCenter
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn Anchor::opposite(self : Anchor) -> Anchor {
|
||||
match self {
|
||||
TopLeft => BottomRight
|
||||
TopRight => BottomLeft
|
||||
BottomLeft => TopRight
|
||||
BottomRight => TopLeft
|
||||
TopCenter => BottomCenter
|
||||
BottomCenter => TopCenter
|
||||
LeftCenter => RightCenter
|
||||
RightCenter => LeftCenter
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
/// The anchor on the other side along `axis`.
|
||||
pub fn Anchor::other_side_along(self : Anchor, axis : Axis) -> Anchor {
|
||||
match axis {
|
||||
Vertical =>
|
||||
match self {
|
||||
TopLeft => BottomLeft
|
||||
TopRight => BottomRight
|
||||
BottomLeft => TopLeft
|
||||
BottomRight => TopRight
|
||||
TopCenter => BottomCenter
|
||||
BottomCenter => TopCenter
|
||||
LeftCenter => LeftCenter
|
||||
RightCenter => RightCenter
|
||||
}
|
||||
Horizontal =>
|
||||
match self {
|
||||
TopLeft => TopRight
|
||||
TopRight => TopLeft
|
||||
BottomLeft => BottomRight
|
||||
BottomRight => BottomLeft
|
||||
TopCenter => TopCenter
|
||||
BottomCenter => BottomCenter
|
||||
LeftCenter => RightCenter
|
||||
RightCenter => LeftCenter
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn Anchor::is_center(self : Anchor) -> Bool {
|
||||
self is (TopCenter | BottomCenter | LeftCenter | RightCenter)
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn Anchor::is_bottom(self : Anchor) -> Bool {
|
||||
self is (BottomCenter | BottomLeft | BottomRight)
|
||||
}
|
||||
|
||||
///|
|
||||
/// Creates bounds of `size` such that `anchor` of the result lies at `origin`.
|
||||
pub fn[T : Sub + Half] Bounds::from_anchor_and_size(
|
||||
anchor : Anchor,
|
||||
origin : Point[T],
|
||||
size : Size[T],
|
||||
) -> Bounds[T] {
|
||||
let origin : Point[T] = match anchor {
|
||||
TopLeft => origin
|
||||
TopRight => { x: origin.x - size.width, y: origin.y, }
|
||||
BottomLeft => { x: origin.x, y: origin.y - size.height, }
|
||||
BottomRight => { x: origin.x - size.width, y: origin.y - size.height, }
|
||||
TopCenter => { x: origin.x - Half::half(size.width), y: origin.y, }
|
||||
BottomCenter =>
|
||||
{ x: origin.x - Half::half(size.width), y: origin.y - size.height, }
|
||||
LeftCenter => { x: origin.x, y: origin.y - Half::half(size.height), }
|
||||
RightCenter =>
|
||||
{ x: origin.x - size.width, y: origin.y - Half::half(size.height), }
|
||||
}
|
||||
{ origin, size, }
|
||||
}
|
||||
|
||||
///|
|
||||
/// Creates bounds of `size` centered on `center`.
|
||||
pub fn[T : Sub + Half] Bounds::centered_at(
|
||||
center : Point[T],
|
||||
size : Size[T],
|
||||
) -> Bounds[T] {
|
||||
{
|
||||
origin: {
|
||||
x: center.x - Half::half(size.width),
|
||||
y: center.y - Half::half(size.height),
|
||||
},
|
||||
size,
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn[T] Bounds::top(self : Bounds[T]) -> T {
|
||||
self.origin.y
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn[T : Add] Bounds::bottom(self : Bounds[T]) -> T {
|
||||
self.origin.y + self.size.height
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn[T] Bounds::left(self : Bounds[T]) -> T {
|
||||
self.origin.x
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn[T : Add] Bounds::right(self : Bounds[T]) -> T {
|
||||
self.origin.x + self.size.width
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn[T : Add] Bounds::top_right(self : Bounds[T]) -> Point[T] {
|
||||
{ x: self.origin.x + self.size.width, y: self.origin.y, }
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn[T : Add] Bounds::bottom_right(self : Bounds[T]) -> Point[T] {
|
||||
{ x: self.origin.x + self.size.width, y: self.origin.y + self.size.height, }
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn[T : Add] Bounds::bottom_left(self : Bounds[T]) -> Point[T] {
|
||||
{ x: self.origin.x, y: self.origin.y + self.size.height, }
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn[T : Add + Half] Bounds::top_center(self : Bounds[T]) -> Point[T] {
|
||||
{ x: self.origin.x + Half::half(self.size.width), y: self.origin.y, }
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn[T : Add + Half] Bounds::bottom_center(self : Bounds[T]) -> Point[T] {
|
||||
{
|
||||
x: self.origin.x + Half::half(self.size.width),
|
||||
y: self.origin.y + self.size.height,
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn[T : Add + Half] Bounds::left_center(self : Bounds[T]) -> Point[T] {
|
||||
{ x: self.origin.x, y: self.origin.y + Half::half(self.size.height), }
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn[T : Add + Half] Bounds::right_center(self : Bounds[T]) -> Point[T] {
|
||||
{
|
||||
x: self.origin.x + self.size.width,
|
||||
y: self.origin.y + Half::half(self.size.height),
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn[T : Add + Half] Bounds::center(self : Bounds[T]) -> Point[T] {
|
||||
{
|
||||
x: self.origin.x + Half::half(self.size.width),
|
||||
y: self.origin.y + Half::half(self.size.height),
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
/// The point of these bounds at the given anchor.
|
||||
pub fn[T : Add + Half] Bounds::corner(
|
||||
self : Bounds[T],
|
||||
anchor : Anchor,
|
||||
) -> Point[T] {
|
||||
match anchor {
|
||||
TopLeft => self.origin
|
||||
TopRight => self.top_right()
|
||||
BottomLeft => self.bottom_left()
|
||||
BottomRight => self.bottom_right()
|
||||
TopCenter => self.top_center()
|
||||
BottomCenter => self.bottom_center()
|
||||
LeftCenter => self.left_center()
|
||||
RightCenter => self.right_center()
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
/// Width plus height; used as the insertion cost heuristic in the scene's
|
||||
/// bounds tree.
|
||||
pub fn[T : Add] Bounds::half_perimeter(self : Bounds[T]) -> T {
|
||||
self.size.width + self.size.height
|
||||
}
|
||||
|
||||
///|
|
||||
/// Whether the two rectangles overlap (touching edges do not count).
|
||||
pub fn[T : Add + Compare] Bounds::intersects(
|
||||
self : Bounds[T],
|
||||
other : Bounds[T],
|
||||
) -> Bool {
|
||||
let my_lower_right = self.bottom_right()
|
||||
let their_lower_right = other.bottom_right()
|
||||
self.origin.x < their_lower_right.x &&
|
||||
my_lower_right.x > other.origin.x &&
|
||||
self.origin.y < their_lower_right.y &&
|
||||
my_lower_right.y > other.origin.y
|
||||
}
|
||||
|
||||
///|
|
||||
/// The overlapping region. If the rectangles do not overlap the result is
|
||||
/// empty (zero or negative size clamped to zero).
|
||||
pub fn[T : Add + Sub + Compare] Bounds::intersect(
|
||||
self : Bounds[T],
|
||||
other : Bounds[T],
|
||||
) -> Bounds[T] {
|
||||
let upper_left = self.origin.max(other.origin)
|
||||
let bottom_right = self
|
||||
.bottom_right()
|
||||
.min(other.bottom_right())
|
||||
.max(upper_left)
|
||||
Bounds::from_corners(upper_left, bottom_right)
|
||||
}
|
||||
|
||||
///|
|
||||
/// The smallest rectangle containing both.
|
||||
pub fn[T : Add + Sub + Compare] Bounds::union(
|
||||
self : Bounds[T],
|
||||
other : Bounds[T],
|
||||
) -> Bounds[T] {
|
||||
let top_left = self.origin.min(other.origin)
|
||||
let bottom_right = self.bottom_right().max(other.bottom_right())
|
||||
Bounds::from_corners(top_left, bottom_right)
|
||||
}
|
||||
|
||||
///|
|
||||
/// Like `union`, but empty bounds are ignored.
|
||||
pub fn[T : Add + Sub + Compare + Default] Bounds::join(
|
||||
self : Bounds[T],
|
||||
other : Bounds[T],
|
||||
) -> Bounds[T] {
|
||||
if self.is_empty() {
|
||||
other
|
||||
} else if other.is_empty() {
|
||||
self
|
||||
} else {
|
||||
self.union(other)
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
/// Whether the width or height is not positive.
|
||||
pub fn[T : Compare + Default] Bounds::is_empty(self : Bounds[T]) -> Bool {
|
||||
let zero : T = Default::default()
|
||||
self.size.width <= zero || self.size.height <= zero
|
||||
}
|
||||
|
||||
///|
|
||||
/// Whether `point` is inside (left/top edges inclusive, right/bottom
|
||||
/// exclusive).
|
||||
pub fn[T : Add + Compare] Bounds::contains(
|
||||
self : Bounds[T],
|
||||
point : Point[T],
|
||||
) -> Bool {
|
||||
point.x >= self.origin.x &&
|
||||
point.x < self.origin.x + self.size.width &&
|
||||
point.y >= self.origin.y &&
|
||||
point.y < self.origin.y + self.size.height
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn[T : Add + Compare] Bounds::is_contained_within(
|
||||
self : Bounds[T],
|
||||
other : Bounds[T],
|
||||
) -> Bool {
|
||||
other.contains(self.origin) && other.contains(self.bottom_right())
|
||||
}
|
||||
|
||||
///|
|
||||
/// `point` relative to the origin, or `None` if it lies outside.
|
||||
pub fn[T : Add + Sub + Compare] Bounds::localize(
|
||||
self : Bounds[T],
|
||||
point : Point[T],
|
||||
) -> Point[T]? {
|
||||
if self.contains(point) {
|
||||
Some(point.relative_to(self.origin))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
/// Grows the bounds by `amount` on every side.
|
||||
pub fn[T : Add + Sub] Bounds::dilate(self : Bounds[T], amount : T) -> Bounds[T] {
|
||||
let double = amount + amount
|
||||
{
|
||||
origin: { x: self.origin.x - amount, y: self.origin.y - amount, },
|
||||
size: {
|
||||
width: self.size.width + double,
|
||||
height: self.size.height + double,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
/// Shrinks the bounds by `amount` on every side.
|
||||
pub fn[T : Add + Sub + Neg] Bounds::inset(
|
||||
self : Bounds[T],
|
||||
amount : T,
|
||||
) -> Bounds[T] {
|
||||
self.dilate(-amount)
|
||||
}
|
||||
|
||||
///|
|
||||
/// Grows each side by the corresponding edge amount.
|
||||
pub fn[T : Add + Sub] Bounds::extend_by(
|
||||
self : Bounds[T],
|
||||
amount : Edges[T],
|
||||
) -> Bounds[T] {
|
||||
{
|
||||
origin: { x: self.origin.x - amount.left, y: self.origin.y - amount.top, },
|
||||
size: {
|
||||
width: self.size.width + amount.left + amount.right,
|
||||
height: self.size.height + amount.top + amount.bottom,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
/// The space between these bounds and the enclosing `outer` bounds.
|
||||
pub fn[T : Add + Sub] Bounds::space_within(
|
||||
self : Bounds[T],
|
||||
outer : Bounds[T],
|
||||
) -> Edges[T] {
|
||||
{
|
||||
top: self.top() - outer.top(),
|
||||
right: outer.right() - self.right(),
|
||||
bottom: outer.bottom() - self.bottom(),
|
||||
left: self.left() - outer.left(),
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn[T, U] Bounds::map(self : Bounds[T], f : (T) -> U) -> Bounds[U] {
|
||||
{ origin: self.origin.map(f), size: self.size.map(f), }
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn[T] Bounds::map_origin(self : Bounds[T], f : (T) -> T) -> Bounds[T] {
|
||||
{ origin: self.origin.map(f), size: self.size, }
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn[T] Bounds::map_size(self : Bounds[T], f : (T) -> T) -> Bounds[T] {
|
||||
{ origin: self.origin, size: self.size.map(f), }
|
||||
}
|
||||
|
||||
///|
|
||||
/// Translates the bounds by `offset`.
|
||||
pub fn[T : Add] Bounds::translate(
|
||||
self : Bounds[T],
|
||||
offset : Point[T],
|
||||
) -> Bounds[T] {
|
||||
{ origin: self.origin + offset, size: self.size, }
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn[T : MulScalar] Bounds::mul_scalar(
|
||||
self : Bounds[T],
|
||||
factor : Double,
|
||||
) -> Bounds[T] {
|
||||
{
|
||||
origin: self.origin.mul_scalar(factor),
|
||||
size: self.size.mul_scalar(factor),
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
pub impl[T : IsZero] IsZero for Bounds[T] with fn is_zero(self) {
|
||||
IsZero::is_zero(self.size)
|
||||
}
|
||||
|
||||
///|
|
||||
pub impl[T : Show + Add] Show for Bounds[T] with fn output(self, logger) {
|
||||
logger.write_string("Bounds { ")
|
||||
Show::output(self.origin, logger)
|
||||
logger.write_string(" - ")
|
||||
Show::output(self.bottom_right(), logger)
|
||||
logger.write_string(" }")
|
||||
}
|
||||
|
||||
///|
|
||||
/// Rounds half away from zero, like Rust's `f32::round`, on every backend.
|
||||
fn round_half_away(value : Double) -> Double {
|
||||
if value >= 0.0 {
|
||||
(value + 0.5).floor()
|
||||
} else {
|
||||
-(-value + 0.5).floor()
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn Bounds::scale(
|
||||
self : Bounds[Pixels],
|
||||
factor : Double,
|
||||
) -> Bounds[ScaledPixels] {
|
||||
{ origin: self.origin.scale(factor), size: self.size.scale(factor), }
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn Bounds::to_device_pixels(
|
||||
self : Bounds[Pixels],
|
||||
factor : Double,
|
||||
) -> Bounds[DevicePixels] {
|
||||
{
|
||||
origin: {
|
||||
x: DevicePixels(round_half_away(self.origin.x.0 * factor).to_int()),
|
||||
y: DevicePixels(round_half_away(self.origin.y.0 * factor).to_int()),
|
||||
},
|
||||
size: self.size.to_device_pixels(factor),
|
||||
}
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn Bounds::to_pixels(
|
||||
self : Bounds[DevicePixels],
|
||||
scale_factor : Double,
|
||||
) -> Bounds[Pixels] {
|
||||
{
|
||||
origin: {
|
||||
x: Pixels(self.origin.x.0.to_double() / scale_factor),
|
||||
y: Pixels(self.origin.y.0.to_double() / scale_factor),
|
||||
},
|
||||
size: self.size.to_pixels(scale_factor),
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// ContentMask
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
///|
|
||||
/// The region that painting is clipped to.
|
||||
#derive(Eq, Hash, Default, Debug)
|
||||
pub(all) struct ContentMask[T] {
|
||||
bounds : Bounds[T]
|
||||
}
|
||||
|
||||
///|
|
||||
pub fn ContentMask::scale(
|
||||
self : ContentMask[Pixels],
|
||||
factor : Double,
|
||||
) -> ContentMask[ScaledPixels] {
|
||||
{ bounds: self.bounds.scale(factor), }
|
||||
}
|
||||
|
||||
///|
|
||||
/// The intersection of two content masks.
|
||||
pub fn[T : Add + Sub + Compare] ContentMask::intersect(
|
||||
self : ContentMask[T],
|
||||
other : ContentMask[T],
|
||||
) -> ContentMask[T] {
|
||||
{ bounds: self.bounds.intersect(other.bounds), }
|
||||
}
|
||||
Reference in new issue
Block a user