# Porting GPUI to MoonBit This document is the plan for porting [GPUI](https://github.com/zed-industries/zed/tree/main/crates/gpui) (Zed's UI framework, Apache-2.0) to MoonBit. It covers how GPUI is built, how its modules map to MoonBit packages, which parts are pure logic and which are platform code, the backend strategy, where Rust idioms need a different design, and an ordered roadmap. Reference snapshot: `zed-industries/zed` @ `f98c2c100d21` (2026-11-24). The `gpui` crate is about 98k lines of Rust. Platform code lives in sibling crates: `gpui_platform`, `gpui_macos`, `gpui_linux`, `gpui_windows`, `gpui_web`, `gpui_wgpu`, and `gpui_macros`. --- ## 1. GPUI architecture in one page ```mermaid flowchart TD App["App (entities, globals, effects, keymap, actions)"] --> Window Window["Window (frame loop, focus, dispatch tree, element state)"] --> Element Element["Element tree: div(), text, img, list ... (Styled + Interactive)"] --> Taffy["Layout (taffy flexbox/grid)"] Element --> Scene["Scene (Quad, Shadow, Path, Underline, Sprites)"] Window --> TextSystem["Text system (shape, wrap, glyph atlas)"] Scene --> PlatformWindow App --> Platform["Platform trait: windows, executors, clipboard, text system"] Platform --> PlatformWindow["PlatformWindow: draw(scene), input callbacks"] PlatformWindow --> Renderer["Renderer (Metal / wgpu / Blade)"] ``` **App, Context and Entity** (`app.rs`, `app/entity_map.rs`, `app/context.rs`) - All state lives in *entities* owned by `App`. `EntityMap` stores `Box` values in a slot map. An `Entity` is a refcounted, typed handle (an id plus `PhantomData`), and `WeakEntity` is its weak form. - `update_entity` *leases* the value: it removes it from the map, runs the closure with `&mut T` and `&mut Context`, and puts it back. A re-entrant update of the same entity panics. - `Context` derefs to `App` and adds entity-scoped helpers: `notify`, `emit`, `observe`, `subscribe`, `spawn`, `listener`, `on_release`, and the `*_in(window)` variants. - Side effects go through an `Effect` queue (`Notify`, `Emit`, `RefreshWindows`, `NotifyGlobalObservers`, `Defer`, `EntityCreated`). The queue is flushed when the outermost `App::update` returns. - Subscriptions are `SubscriberSet`s. A callback that returns `false` unsubscribes itself. - Globals are a `TypeId → Box` map. - `notify` marks individual views dirty. During a draw, each window records which entities it read (`AccessedEntities`). **Window** (`window.rs`, 11k lines) - `draw()` runs three phases over the root view's element tree: 1. `request_layout`, then compute layout with taffy. 2. `prepaint`: hitboxes, the dispatch tree, focus, and element state. 3. `paint`: emits primitives into the `Scene`. - Frames are double-buffered (`rendered_frame` and `next_frame`). - Cached views reuse their prepaint and paint *ranges* from the previous frame (`Scene::replay`). - Element state is keyed by `(GlobalElementId, TypeId)`. **Element** (`element.rs`, `elements/*`) - The `Element` trait has two associated state types, `RequestLayoutState` and `PrepaintState`, and three phase methods. - `AnyElement` is a type-erased element in an arena. - `IntoElement`, `ParentElement`, `Render`/`RenderOnce`, `View`, and `AnyView` build the tree. - `div()` (5.9k lines) carries an `Interactivity`: a base `StyleRefinement`, state styles (hover, active, focus, group), about 20 listener lists, scrolling, drag and drop, and tooltips. **Style and layout** (`style.rs`, `styled.rs`, `taffy.rs`) - `Style` is a CSS-like struct. - `#[derive(Refineable)]` generates `StyleRefinement`, a copy of the struct with every field optional. - `Styled` provides hundreds of Tailwind-style builder methods. Most are macro-generated (`gpui_macros::style_helpers!`). - Layout is taffy 0.15 (flexbox, block, and grid), wrapped by `TaffyLayoutEngine` with measured leaves (text) and a cache. **Scene** (`scene.rs`, `bounds_tree.rs`) - Per-kind primitive arrays. Each primitive gets a draw `order` from an R-tree: one more than the highest overlapping order, unless the kinds may share a level. - Layers group primitives into one ordering unit. - `finish()` sorts the arrays. `batches()` merges them into `PrimitiveBatch`es for the renderer. **Platform** (`platform.rs`, plus one crate per OS) - `Platform` has about 90 methods: windows, executors, the text system, clipboard, menus, and paths. - `PlatformWindow` has about 100 methods: `draw(&Scene)`, input and resize callbacks, and the IME handler. - Other platform traits: `PlatformDispatcher` (runs tasks), `PlatformTextSystem` (font ids, shaping via `layout_line`, glyph rasterization), and `PlatformAtlas` (glyph, SVG, and image tiles). - The `gpui_web` crate already targets the browser. It uses a ``, the `gpui_wgpu` renderer (WebGPU with a WebGL2 fallback), cosmic-text for text, and DOM event translation. It has a hidden `