M6: elements and window — Element/Render, headless Window, div, canvas #6
No files matched your search
@@ -17,26 +17,35 @@ roadmap.
|
|||||||
| `experiments/gpui/style` | `style.rs`, `styled.rs`, Refineable, gpui_macros style helpers | Ported (`Style`/`StyleRefinement`, the full `Styled` fluent API) |
|
| `experiments/gpui/style` | `style.rs`, `styled.rs`, Refineable, gpui_macros style helpers | Ported (`Style`/`StyleRefinement`, the full `Styled` fluent API) |
|
||||||
| `experiments/gpui/app` | `app.rs`, `entity_map.rs`, `context.rs`, `subscription.rs` | Ported (entities, contexts, notify/observe, typed events and globals, effect flushing) |
|
| `experiments/gpui/app` | `app.rs`, `entity_map.rs`, `context.rs`, `subscription.rs` | Ported (entities, contexts, notify/observe, typed events and globals, effect flushing) |
|
||||||
| `experiments/gpui/executor` | `executor.rs`, `scheduler` crate | Ported (tasks, timers, cancellation, deterministic test dispatcher; single thread) |
|
| `experiments/gpui/executor` | `executor.rs`, `scheduler` crate | Ported (tasks, timers, cancellation, deterministic test dispatcher; single thread) |
|
||||||
|
| `experiments/gpui/input` | mouse events of `interactive.rs` | Ported (mouse events, modifiers) |
|
||||||
|
| `experiments/gpui/window` | `window.rs`, `element.rs`, `view.rs` | Ported, headless (frame lifecycle, views, hitboxes, mouse dispatch, painting) |
|
||||||
|
| `experiments/gpui/elements` | `elements/div.rs`, `elements/canvas.rs` | Ported (`div` with styles, hover, clicks and mouse listeners; `canvas`) |
|
||||||
| `experiments/gpui/layout` | `taffy.rs` | Ported on [`Milky2018/chicle`](https://mooncakes.io/docs/Milky2018/chicle) (flexbox, grid, block) |
|
| `experiments/gpui/layout` | `taffy.rs` | Ported on [`Milky2018/chicle`](https://mooncakes.io/docs/Milky2018/chicle) (flexbox, grid, block) |
|
||||||
|
|
||||||
## Example
|
## Example
|
||||||
|
|
||||||
Nothing renders yet. `src/examples/card` resolves a style written with
|
Nothing is shown on screen yet (the renderer comes with M8). `src/examples/card`
|
||||||
GPUI's fluent API, lays the card and its (fake-measured) text out with the
|
is a view rendering a `div()` card with GPUI's fluent API; a headless window
|
||||||
layout engine, paints it into a `Scene`, and prints the layout and the draw
|
lays it out, paints it into a `Scene`, and dispatches a simulated hover and
|
||||||
batches:
|
click (which expands the card). It prints the scene's draw batches and
|
||||||
|
quads:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
moon run src/examples/card --target js
|
moon run src/examples/card --target js
|
||||||
```
|
```
|
||||||
|
|
||||||
```moonbit
|
```moonbit
|
||||||
@style.StyleRefinement::new()
|
@elements.div()
|
||||||
|
.id("card")
|
||||||
.flex().flex_col().gap_2().p_4()
|
.flex().flex_col().gap_2().p_4()
|
||||||
.w(@geometry.px(320.0))
|
.w(@geometry.px(320.0))
|
||||||
.rounded_lg().border_1().border_color(@color.rgb(0xd0d7de))
|
.rounded_lg().border_1().border_color(@color.rgb(0xd0d7de))
|
||||||
.bg(@color.rgb(0xffffff)).shadow_md()
|
.bg(@color.rgb(0xffffff)).shadow_md()
|
||||||
.text_color(@color.rgb(0x1f2328)).text_sm()
|
.hover(fn(style) { style.border_color(@color.rgb(0x0969da)) })
|
||||||
|
.on_click(@window.listener(cx, fn(card, _, _, cx) {
|
||||||
|
card.expanded = !card.expanded
|
||||||
|
cx.notify()
|
||||||
|
}))
|
||||||
```
|
```
|
||||||
|
|
||||||
`src/examples/counter` shows the app model and executors: two entities
|
`src/examples/counter` shows the app model and executors: two entities
|
||||||
@@ -48,8 +57,9 @@ the roadmap.
|
|||||||
|
|
||||||
## Generated code
|
## Generated code
|
||||||
|
|
||||||
`src/style/style_generated.mbt`, `styled_generated.mbt` and
|
`src/style/style_generated.mbt`, `styled_generated.mbt`,
|
||||||
`styled_extend_generated.mbt` are generated by `tools/gen_style.py`, which
|
`styled_extend_generated.mbt`, and the `*_styled_generated.mbt` files in
|
||||||
|
`src/elements` are generated by `tools/gen_style.py`, which
|
||||||
stands in for GPUI's Rust macros. Do not edit them by hand; change the
|
stands in for GPUI's Rust macros. Do not edit them by hand; change the
|
||||||
script or `src/style/styled.mbt`, then run:
|
script or `src/style/styled.mbt`, then run:
|
||||||
|
|
||||||
|
|||||||
+53
-12
@@ -137,8 +137,9 @@ stays isolated.
|
|||||||
| `action` | `action.rs` | pure | planned |
|
| `action` | `action.rs` | pure | planned |
|
||||||
| `app` | `app.rs`, `app/entity_map.rs`, `app/context.rs`, `subscription.rs`, `global.rs` | pure | **done** (no windows, executors or actions yet) |
|
| `app` | `app.rs`, `app/entity_map.rs`, `app/context.rs`, `subscription.rs`, `global.rs` | pure | **done** (no windows, executors or actions yet) |
|
||||||
| `executor` | `executor.rs`, `scheduler` crate (`executor.rs`, `test_scheduler.rs`) | pure core + `Dispatcher` platform hook | **done** (single thread) |
|
| `executor` | `executor.rs`, `scheduler` crate (`executor.rs`, `test_scheduler.rs`) | pure core + `Dispatcher` platform hook | **done** (single thread) |
|
||||||
| `element` | `element.rs`, `view.rs`, `elements/div.rs`, `interactive.rs` | pure | planned |
|
| `input` | mouse events of `interactive.rs`, `Modifiers` | pure | **done** (mouse only; keys in M5) |
|
||||||
| `window` | `window.rs`, `key_dispatch.rs`, `tab_stop.rs` | pure (talks to platform traits) | planned |
|
| `window` | `window.rs`, `element.rs`, `view.rs`, `Style::paint` | pure | **done** (headless; no focus/keys/caching yet) |
|
||||||
|
| `elements` | `elements/div.rs` (+ `Interactivity`), `elements/canvas.rs` | pure | **done** (`div`, `canvas`) |
|
||||||
| `text` | `text_system.rs`, `text_system/*` | mixed: wrapping/caching pure, shaping platform | planned |
|
| `text` | `text_system.rs`, `text_system/*` | mixed: wrapping/caching pure, shaping platform | planned |
|
||||||
| `platform` | `platform.rs` (traits only), `interactive.rs` input types | pure (interfaces) | planned |
|
| `platform` | `platform.rs` (traits only), `interactive.rs` input types | pure (interfaces) | planned |
|
||||||
| `platform/test` | `platform/test/*` | pure | planned: headless platform for tests |
|
| `platform/test` | `platform/test/*` | pure | planned: headless platform for tests |
|
||||||
@@ -148,7 +149,7 @@ stays isolated.
|
|||||||
| `renderer/wgpu` | `gpui_wgpu` | native (+ js WebGPU) | later |
|
| `renderer/wgpu` | `gpui_wgpu` | native (+ js WebGPU) | later |
|
||||||
| `platform/native/*` | `gpui_macos`, `gpui_linux`, `gpui_windows` | native only | later |
|
| `platform/native/*` | `gpui_macos`, `gpui_linux`, `gpui_windows` | native only | later |
|
||||||
| `examples/*` | `examples/*` | executables | `card` (headless, prints a scene report) |
|
| `examples/*` | `examples/*` | executables | `card` (headless, prints a scene report) |
|
||||||
| `elements/*` | `elements/{text,img,svg,list,uniform_list,anchored,deferred,canvas,animation}.rs` | pure | later |
|
| `elements/*` | `elements/{text,img,svg,list,uniform_list,anchored,deferred,animation}.rs` | pure | later |
|
||||||
|
|
||||||
A facade package (`experiments/gpui` re-exporting the common API, similar to
|
A facade package (`experiments/gpui` re-exporting the common API, similar to
|
||||||
GPUI's `prelude.rs`) should come once there are enough packages to make
|
GPUI's `prelude.rs`) should come once there are enough packages to make
|
||||||
@@ -378,15 +379,30 @@ gantt
|
|||||||
multi-stroke matching.
|
multi-stroke matching.
|
||||||
- `ActionRegistry`.
|
- `ActionRegistry`.
|
||||||
- Tests ported from `keymap.rs` and `keymap/context.rs`.
|
- Tests ported from `keymap.rs` and `keymap/context.rs`.
|
||||||
- **M6: elements and window.**
|
- **M6: elements and window (done).**
|
||||||
- `Element`/`AnyElement`/`IntoElement`/`ParentElement`, `Render`, and
|
- `Element`/`AnyElement`/`IntoElement`, `Render`, `AnyView`, views as
|
||||||
views.
|
elements, `listener`, and `empty`.
|
||||||
- `div` with styles, hover and active styles, mouse listeners, and
|
- `Window`:
|
||||||
scrolling.
|
- `draw` (render, then layout to fill the viewport, prepaint, paint,
|
||||||
- `Window::draw` (request_layout → prepaint → paint), element ids and
|
`Scene::finish`) with phase checks, and `draw_if_dirty`.
|
||||||
state, hitboxes, the content-mask stack, and the dispatch tree.
|
- The content-mask, element-offset, opacity, and element-id stacks.
|
||||||
- Tests: build a tree, draw against the test platform, assert on the
|
- Element state across frames (`ElementStateKey`), hitboxes and hit
|
||||||
Scene.
|
testing, occlusion, and mouse dispatch (capture and bubble,
|
||||||
|
`stop_propagation`).
|
||||||
|
- Hover-driven and notify-driven invalidation, with only rendered views
|
||||||
|
invalidating the window, plus `resize`.
|
||||||
|
- Painting: `paint_quad` with device-pixel snapping, drop and inset
|
||||||
|
shadows, `paint_path`, and `paint_style` (a port of `Style::paint`).
|
||||||
|
- `div()`:
|
||||||
|
- Styles through the generated `Styled` list, plus children and hover
|
||||||
|
styles.
|
||||||
|
- Mouse down, up, move and scroll listeners, and clicks (with element
|
||||||
|
state).
|
||||||
|
- `occlude`, overflow clipping, and opacity.
|
||||||
|
- `canvas()`.
|
||||||
|
- Tests: end-to-end views to scene, clicks, hover, invalidation, nested
|
||||||
|
views, bubbling, occlusion, shadows and borders, clipping, canvas,
|
||||||
|
resize, and the aborts expected on misuse.
|
||||||
- **M7: test platform.** A headless `Platform`/`PlatformWindow` that records
|
- **M7: test platform.** A headless `Platform`/`PlatformWindow` that records
|
||||||
scenes and injects input events, mirroring GPUI's `TestPlatform` and
|
scenes and injects input events, mirroring GPUI's `TestPlatform` and
|
||||||
`VisualTestContext`. This is the base for most later tests.
|
`VisualTestContext`. This is the base for most later tests.
|
||||||
@@ -532,6 +548,31 @@ gantt
|
|||||||
- **`AsyncApp` methods return values, not `Result`s.** An app is never
|
- **`AsyncApp` methods return values, not `Result`s.** An app is never
|
||||||
dropped while a task references it.
|
dropped while a task references it.
|
||||||
|
|
||||||
|
### Deviations taken in M6 (elements and window)
|
||||||
|
- **Element state lives in the element.** `Element` has no associated state
|
||||||
|
types. Elements keep per-frame state in their own fields and are rebuilt
|
||||||
|
every frame. `render` returns `AnyElement`.
|
||||||
|
- **Element ids are strings, and state is keyed by `ElementStateKey[S]`**
|
||||||
|
(the same key pattern as globals). `Div::id` makes a div stateful, and
|
||||||
|
`on_click` aborts without an id. There is no separate `Stateful<Div>` type.
|
||||||
|
- **Hover styles apply at paint time only.** Hovering cannot change layout.
|
||||||
|
Any change in the hovered hitboxes marks the window dirty, not only for
|
||||||
|
elements with hover styles.
|
||||||
|
- **The window is headless.** It is created directly with a viewport size,
|
||||||
|
scale factor, and rem size, and `draw` returns the `Scene`. There is no
|
||||||
|
platform window or frame scheduling yet (M7/M8).
|
||||||
|
- **Invalidation without accessed-entity tracking.** A window re-renders when
|
||||||
|
a view it rendered in the last frame is notified, through
|
||||||
|
`App::observe_notifications`. There is no view caching or `Scene::replay`
|
||||||
|
reuse yet; every frame renders everything.
|
||||||
|
- **Quads are not split.** GPUI splits bordered quads into a fill and border
|
||||||
|
strips; that renderer optimization is not ported.
|
||||||
|
- **Not yet ported:**
|
||||||
|
- Focus and the key dispatch tree (M5/M10).
|
||||||
|
- Active and group styles.
|
||||||
|
- Drag and drop, tooltips, scroll state, and deferred draws.
|
||||||
|
- Text (M9).
|
||||||
|
|
||||||
## 7. Design decisions
|
## 7. Design decisions
|
||||||
|
|
||||||
Decided on 2026-11-24, following the recommendations below.
|
Decided on 2026-11-24, following the recommendations below.
|
||||||
|
|||||||
@@ -51,6 +51,8 @@ pub struct App {
|
|||||||
priv release_listeners : SubscriberSet[EntityId, (App) -> Unit]
|
priv release_listeners : SubscriberSet[EntityId, (App) -> Unit]
|
||||||
priv global_observers : SubscriberSet[Int32, (App) -> Bool]
|
priv global_observers : SubscriberSet[Int32, (App) -> Bool]
|
||||||
priv globals : Map[Int32, () -> Unit]
|
priv globals : Map[Int32, () -> Unit]
|
||||||
|
/// Called synchronously by `notify` (windows invalidate views with it).
|
||||||
|
priv notify_hooks : SubscriberSet[Unit, (EntityId) -> Unit]
|
||||||
priv executor : @executor.Executor
|
priv executor : @executor.Executor
|
||||||
/// Set when the app drives its own deterministic dispatcher (tests).
|
/// Set when the app drives its own deterministic dispatcher (tests).
|
||||||
priv test_dispatcher : @executor.TestDispatcher?
|
priv test_dispatcher : @executor.TestDispatcher?
|
||||||
@@ -92,6 +94,7 @@ pub fn App::new(dispatcher? : &@executor.Dispatcher) -> App {
|
|||||||
release_listeners: SubscriberSet::new(),
|
release_listeners: SubscriberSet::new(),
|
||||||
global_observers: SubscriberSet::new(),
|
global_observers: SubscriberSet::new(),
|
||||||
globals: {},
|
globals: {},
|
||||||
|
notify_hooks: SubscriberSet::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -274,10 +277,26 @@ pub fn App::entity_count(self : App) -> Int32 {
|
|||||||
///|
|
///|
|
||||||
/// Marks an entity as changed: its observers run during the next flush
|
/// Marks an entity as changed: its observers run during the next flush
|
||||||
/// (GPUI: `App::notify`). Repeated notifications before the flush coalesce.
|
/// (GPUI: `App::notify`). Repeated notifications before the flush coalesce.
|
||||||
|
/// Notification hooks (windows rendering the entity) run immediately.
|
||||||
pub fn App::notify(self : App, entity_id : EntityId) -> Unit {
|
pub fn App::notify(self : App, entity_id : EntityId) -> Unit {
|
||||||
|
self.notify_hooks.retain((), fn(hook) {
|
||||||
|
hook(entity_id)
|
||||||
|
true
|
||||||
|
})
|
||||||
self.update(fn(app) { app.push_effect(Notify(entity_id)) })
|
self.update(fn(app) { app.push_effect(Notify(entity_id)) })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// Calls `hook` synchronously whenever any entity is notified. Used by
|
||||||
|
/// windows to invalidate the views they rendered (GPUI keeps per-window
|
||||||
|
/// invalidators for this).
|
||||||
|
pub fn App::observe_notifications(
|
||||||
|
self : App,
|
||||||
|
hook : (EntityId) -> Unit,
|
||||||
|
) -> Subscription {
|
||||||
|
self.notify_hooks.insert((), hook)
|
||||||
|
}
|
||||||
|
|
||||||
///|
|
///|
|
||||||
fn[T] App::emit_event(
|
fn[T] App::emit_event(
|
||||||
self : App,
|
self : App,
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ pub fn[T] App::new_entity(Self, (Context[T]) -> T) -> Entity[T]
|
|||||||
pub fn App::notify(Self, EntityId) -> Unit
|
pub fn App::notify(Self, EntityId) -> Unit
|
||||||
pub fn[T] App::observe(Self, Entity[T], (Entity[T], Self) -> Unit) -> Subscription
|
pub fn[T] App::observe(Self, Entity[T], (Entity[T], Self) -> Unit) -> Subscription
|
||||||
pub fn[G] App::observe_global(Self, GlobalKey[G], (Self) -> Unit) -> Subscription
|
pub fn[G] App::observe_global(Self, GlobalKey[G], (Self) -> Unit) -> Subscription
|
||||||
|
pub fn App::observe_notifications(Self, (EntityId) -> Unit) -> Subscription
|
||||||
pub fn[T] App::observe_release(Self, Entity[T], (T, Self) -> Unit) -> Subscription
|
pub fn[T] App::observe_release(Self, Entity[T], (T, Self) -> Unit) -> Subscription
|
||||||
pub fn[T] App::read_entity(Self, Entity[T]) -> T
|
pub fn[T] App::read_entity(Self, Entity[T]) -> T
|
||||||
pub fn[T] App::release(Self, Entity[T]) -> Unit
|
pub fn[T] App::release(Self, Entity[T]) -> Unit
|
||||||
|
|||||||
@@ -0,0 +1,72 @@
|
|||||||
|
// `canvas()`, ported from gpui/src/elements/canvas.rs: an element laid out by
|
||||||
|
// its style that paints with a closure.
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// An element that paints itself with a closure (GPUI: `Canvas`).
|
||||||
|
pub struct Canvas {
|
||||||
|
priv base_style : @style.StyleRefinement
|
||||||
|
priv prepaint_fn : (
|
||||||
|
@geometry.Bounds[@geometry.Pixels],
|
||||||
|
@window.Window,
|
||||||
|
@app.App,
|
||||||
|
) -> Unit
|
||||||
|
priv paint_fn : (@geometry.Bounds[@geometry.Pixels], @window.Window, @app.App) -> Unit
|
||||||
|
priv mut layout_style : @style.Style?
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// A canvas that runs `paint` with its bounds during the paint phase, and
|
||||||
|
/// `prepaint` (if given) during prepaint. Size it with style methods, e.g.
|
||||||
|
/// `canvas(paint).size_full()`.
|
||||||
|
pub fn canvas(
|
||||||
|
paint : (@geometry.Bounds[@geometry.Pixels], @window.Window, @app.App) -> Unit,
|
||||||
|
prepaint~ : (@geometry.Bounds[@geometry.Pixels], @window.Window, @app.App) -> Unit = fn(
|
||||||
|
_,
|
||||||
|
_,
|
||||||
|
_,
|
||||||
|
) {
|
||||||
|
|
||||||
|
},
|
||||||
|
) -> Canvas {
|
||||||
|
{
|
||||||
|
base_style: @style.StyleRefinement::new(),
|
||||||
|
prepaint_fn: prepaint,
|
||||||
|
paint_fn: paint,
|
||||||
|
layout_style: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
pub impl @style.Styled for Canvas with fn style(self) {
|
||||||
|
self.base_style
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
pub impl @window.IntoElement for Canvas with fn into_any_element(self) {
|
||||||
|
@window.AnyElement::new(self)
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
pub extend Canvas with @window.IntoElement::{into_any_element}
|
||||||
|
|
||||||
|
///|
|
||||||
|
impl @window.Element for Canvas with fn request_layout(self, window, _app) {
|
||||||
|
let style = (Default::default() : @style.Style).refined(self.base_style)
|
||||||
|
self.layout_style = Some(style)
|
||||||
|
window.request_layout(style, [])
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
impl @window.Element for Canvas with fn prepaint(self, bounds, window, app) {
|
||||||
|
(self.prepaint_fn)(bounds, window, app)
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
impl @window.Element for Canvas with fn paint(self, bounds, window, app) {
|
||||||
|
guard self.layout_style is Some(style) else {
|
||||||
|
abort("canvas painted before layout")
|
||||||
|
}
|
||||||
|
window.paint_style(style, bounds, fn() {
|
||||||
|
(self.paint_fn)(bounds, window, app)
|
||||||
|
})
|
||||||
|
}
|
||||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,355 @@
|
|||||||
|
// `div()`, ported from gpui/src/elements/div.rs (`Div` and the parts of
|
||||||
|
// `Interactivity` that need neither focus nor actions).
|
||||||
|
//
|
||||||
|
// Ported: styles (via `Styled`), children, hover styles, mouse down/up/move
|
||||||
|
// and scroll listeners, clicks (which need an element id), hitbox
|
||||||
|
// occlusion, overflow clipping and opacity.
|
||||||
|
//
|
||||||
|
// Not ported yet: focus and key listeners and actions (M5/M10), active and
|
||||||
|
// group styles, drag and drop, tooltips, scrolling state, and the
|
||||||
|
// `Stateful<Div>` split (an id is set with `.id(...)` on `Div` itself, and
|
||||||
|
// `on_click` fails loudly without one).
|
||||||
|
//
|
||||||
|
// Deviation: hover styles apply at paint time only. GPUI also lays out with
|
||||||
|
// them (using the previous frame's hover state); here hovering cannot change
|
||||||
|
// an element's layout.
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// A flexible container: GPUI's workhorse element (`div()`).
|
||||||
|
pub struct Div {
|
||||||
|
priv base_style : @style.StyleRefinement
|
||||||
|
priv mut hover_style : @style.StyleRefinement?
|
||||||
|
priv mut element_id : String?
|
||||||
|
priv mut occlude_mouse : Bool
|
||||||
|
priv children : Array[@window.AnyElement]
|
||||||
|
priv mouse_down_listeners : Array[
|
||||||
|
(
|
||||||
|
@input.MouseButton,
|
||||||
|
(@input.MouseDownEvent, @window.Window, @app.App) -> Unit,
|
||||||
|
),
|
||||||
|
]
|
||||||
|
priv mouse_up_listeners : Array[
|
||||||
|
(
|
||||||
|
@input.MouseButton,
|
||||||
|
(@input.MouseUpEvent, @window.Window, @app.App) -> Unit,
|
||||||
|
),
|
||||||
|
]
|
||||||
|
priv mouse_move_listeners : Array[
|
||||||
|
(@input.MouseMoveEvent, @window.Window, @app.App) -> Unit,
|
||||||
|
]
|
||||||
|
priv scroll_listeners : Array[
|
||||||
|
(@input.ScrollWheelEvent, @window.Window, @app.App) -> Unit,
|
||||||
|
]
|
||||||
|
priv click_listeners : Array[
|
||||||
|
(@input.ClickEvent, @window.Window, @app.App) -> Unit,
|
||||||
|
]
|
||||||
|
// Per-frame state.
|
||||||
|
priv mut layout_style : @style.Style?
|
||||||
|
priv mut hitbox : @window.Hitbox?
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// A new, empty `div`.
|
||||||
|
pub fn div() -> Div {
|
||||||
|
{
|
||||||
|
base_style: @style.StyleRefinement::new(),
|
||||||
|
hover_style: None,
|
||||||
|
element_id: None,
|
||||||
|
occlude_mouse: false,
|
||||||
|
children: [],
|
||||||
|
mouse_down_listeners: [],
|
||||||
|
mouse_up_listeners: [],
|
||||||
|
mouse_move_listeners: [],
|
||||||
|
scroll_listeners: [],
|
||||||
|
click_listeners: [],
|
||||||
|
layout_style: None,
|
||||||
|
hitbox: None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
pub impl @style.Styled for Div with fn style(self) {
|
||||||
|
self.base_style
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
pub impl @window.IntoElement for Div with fn into_any_element(self) {
|
||||||
|
@window.AnyElement::new(self)
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
pub extend Div with @window.IntoElement::{into_any_element}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// Gives the element an id, which makes it stateful (needed for clicks).
|
||||||
|
/// Ids are scoped to the parent's id (GPUI: `.id(...)`).
|
||||||
|
pub fn Div::id(self : Div, id : String) -> Div {
|
||||||
|
self.element_id = Some(id)
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// Adds a child (GPUI: `ParentElement::child`).
|
||||||
|
pub fn[E : @window.IntoElement] Div::child(self : Div, child : E) -> Div {
|
||||||
|
self.children.push(child.into_any_element())
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// Adds children (GPUI: `ParentElement::children`).
|
||||||
|
pub fn[E : @window.IntoElement] Div::children(
|
||||||
|
self : Div,
|
||||||
|
children : Array[E],
|
||||||
|
) -> Div {
|
||||||
|
for child in children {
|
||||||
|
self.children.push(child.into_any_element())
|
||||||
|
}
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// Styles applied while the mouse is over the element (GPUI: `.hover(...)`).
|
||||||
|
pub fn Div::hover(
|
||||||
|
self : Div,
|
||||||
|
f : (@style.StyleRefinement) -> @style.StyleRefinement,
|
||||||
|
) -> Div {
|
||||||
|
let current = match self.hover_style {
|
||||||
|
Some(style) => style
|
||||||
|
None => @style.StyleRefinement::new()
|
||||||
|
}
|
||||||
|
self.hover_style = Some(f(current))
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// Hitboxes painted below this element are not hovered (GPUI:
|
||||||
|
/// `.occlude()`).
|
||||||
|
pub fn Div::occlude(self : Div) -> Div {
|
||||||
|
self.occlude_mouse = true
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// Called when `button` is pressed over the element (bubble phase).
|
||||||
|
pub fn Div::on_mouse_down(
|
||||||
|
self : Div,
|
||||||
|
button : @input.MouseButton,
|
||||||
|
listener : (@input.MouseDownEvent, @window.Window, @app.App) -> Unit,
|
||||||
|
) -> Div {
|
||||||
|
self.mouse_down_listeners.push((button, listener))
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// Called when `button` is released over the element (bubble phase).
|
||||||
|
pub fn Div::on_mouse_up(
|
||||||
|
self : Div,
|
||||||
|
button : @input.MouseButton,
|
||||||
|
listener : (@input.MouseUpEvent, @window.Window, @app.App) -> Unit,
|
||||||
|
) -> Div {
|
||||||
|
self.mouse_up_listeners.push((button, listener))
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// Called when the mouse moves over the element (bubble phase).
|
||||||
|
pub fn Div::on_mouse_move(
|
||||||
|
self : Div,
|
||||||
|
listener : (@input.MouseMoveEvent, @window.Window, @app.App) -> Unit,
|
||||||
|
) -> Div {
|
||||||
|
self.mouse_move_listeners.push(listener)
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// Called on scroll-wheel events over the element (bubble phase).
|
||||||
|
pub fn Div::on_scroll_wheel(
|
||||||
|
self : Div,
|
||||||
|
listener : (@input.ScrollWheelEvent, @window.Window, @app.App) -> Unit,
|
||||||
|
) -> Div {
|
||||||
|
self.scroll_listeners.push(listener)
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// Called when the left button is pressed and released over the element.
|
||||||
|
/// The element needs an id (GPUI: `StatefulInteractiveElement::on_click`).
|
||||||
|
pub fn Div::on_click(
|
||||||
|
self : Div,
|
||||||
|
listener : (@input.ClickEvent, @window.Window, @app.App) -> Unit,
|
||||||
|
) -> Div {
|
||||||
|
self.click_listeners.push(listener)
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
fn Div::is_interactive(self : Div) -> Bool {
|
||||||
|
self.hover_style is Some(_) ||
|
||||||
|
self.occlude_mouse ||
|
||||||
|
!self.mouse_down_listeners.is_empty() ||
|
||||||
|
!self.mouse_up_listeners.is_empty() ||
|
||||||
|
!self.mouse_move_listeners.is_empty() ||
|
||||||
|
!self.scroll_listeners.is_empty() ||
|
||||||
|
!self.click_listeners.is_empty()
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// The pending press of a click, kept across frames.
|
||||||
|
priv struct ClickState {
|
||||||
|
mut pending : @input.MouseDownEvent?
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
let click_state_key : @window.ElementStateKey[ClickState] = @window.ElementStateKey::new(
|
||||||
|
"ClickState",
|
||||||
|
)
|
||||||
|
|
||||||
|
///|
|
||||||
|
impl @window.Element for Div with fn request_layout(self, window, app) {
|
||||||
|
let style = (Default::default() : @style.Style).refined(self.base_style)
|
||||||
|
self.layout_style = Some(style)
|
||||||
|
window.with_element_id(self.element_id, fn() {
|
||||||
|
let child_ids = self.children.map(fn(child) {
|
||||||
|
child.request_layout(window, app)
|
||||||
|
})
|
||||||
|
window.request_layout(style, child_ids)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
impl @window.Element for Div with fn prepaint(self, bounds, window, app) {
|
||||||
|
guard self.layout_style is Some(style) else {
|
||||||
|
abort("div prepainted before layout")
|
||||||
|
}
|
||||||
|
window.with_element_id(self.element_id, fn() {
|
||||||
|
if self.is_interactive() {
|
||||||
|
let behavior : @window.HitboxBehavior = if self.occlude_mouse {
|
||||||
|
BlockMouse
|
||||||
|
} else {
|
||||||
|
Normal
|
||||||
|
}
|
||||||
|
self.hitbox = Some(window.insert_hitbox(bounds, behavior))
|
||||||
|
}
|
||||||
|
window.with_content_mask(style.overflow_mask(bounds, window.rem_size()), fn() {
|
||||||
|
for child in self.children {
|
||||||
|
child.prepaint(window, app)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
impl @window.Element for Div with fn paint(self, bounds, window, app) {
|
||||||
|
guard self.layout_style is Some(layout_style) else {
|
||||||
|
abort("div painted before layout")
|
||||||
|
}
|
||||||
|
let hovered = match self.hitbox {
|
||||||
|
Some(hitbox) => hitbox.is_hovered(window)
|
||||||
|
None => false
|
||||||
|
}
|
||||||
|
let style = match (self.hover_style, hovered) {
|
||||||
|
(Some(hover), true) => layout_style.refined(hover)
|
||||||
|
_ => layout_style
|
||||||
|
}
|
||||||
|
window.with_element_id(self.element_id, fn() {
|
||||||
|
if self.hitbox is Some(hitbox) {
|
||||||
|
self.register_listeners(hitbox, window)
|
||||||
|
}
|
||||||
|
window.with_element_opacity(style.opacity, fn() {
|
||||||
|
window.paint_style(style, bounds, fn() {
|
||||||
|
window.with_content_mask(
|
||||||
|
style.overflow_mask(bounds, window.rem_size()),
|
||||||
|
fn() {
|
||||||
|
for child in self.children {
|
||||||
|
child.paint(window, app)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
fn Div::register_listeners(
|
||||||
|
self : Div,
|
||||||
|
hitbox : @window.Hitbox,
|
||||||
|
window : @window.Window,
|
||||||
|
) -> Unit {
|
||||||
|
for entry in self.mouse_down_listeners {
|
||||||
|
let (button, listener) = entry
|
||||||
|
window.on_mouse_event(fn(event, phase, window, app) {
|
||||||
|
if phase == Bubble &&
|
||||||
|
event is MouseDown(down) &&
|
||||||
|
down.button == button &&
|
||||||
|
hitbox.is_hovered(window) {
|
||||||
|
listener(down, window, app)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
for entry in self.mouse_up_listeners {
|
||||||
|
let (button, listener) = entry
|
||||||
|
window.on_mouse_event(fn(event, phase, window, app) {
|
||||||
|
if phase == Bubble &&
|
||||||
|
event is MouseUp(up) &&
|
||||||
|
up.button == button &&
|
||||||
|
hitbox.is_hovered(window) {
|
||||||
|
listener(up, window, app)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
for listener in self.mouse_move_listeners {
|
||||||
|
window.on_mouse_event(fn(event, phase, window, app) {
|
||||||
|
if phase == Bubble &&
|
||||||
|
event is MouseMove(moved) &&
|
||||||
|
hitbox.is_hovered(window) {
|
||||||
|
listener(moved, window, app)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
for listener in self.scroll_listeners {
|
||||||
|
window.on_mouse_event(fn(event, phase, window, app) {
|
||||||
|
if phase == Bubble &&
|
||||||
|
event is ScrollWheel(scroll) &&
|
||||||
|
hitbox.is_hovered(window) {
|
||||||
|
listener(scroll, window, app)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
if !self.click_listeners.is_empty() {
|
||||||
|
guard window.global_element_id() is Some(_) else {
|
||||||
|
abort("on_click requires an element id: call .id(...) on the div")
|
||||||
|
}
|
||||||
|
// The pending press lives in element state so it survives redraws
|
||||||
|
// between mouse down and mouse up.
|
||||||
|
let state = window.with_element_state(click_state_key, fn(previous) {
|
||||||
|
let state = match previous {
|
||||||
|
Some(state) => state
|
||||||
|
None => { pending: None, }
|
||||||
|
}
|
||||||
|
(state, state)
|
||||||
|
})
|
||||||
|
let listeners = self.click_listeners
|
||||||
|
window.on_mouse_event(fn(event, phase, window, app) {
|
||||||
|
match (event, phase) {
|
||||||
|
(MouseDown(down), Bubble) =>
|
||||||
|
if down.button == Left && hitbox.is_hovered(window) {
|
||||||
|
state.pending = Some(down)
|
||||||
|
}
|
||||||
|
(MouseUp(up), Capture) =>
|
||||||
|
if up.button == Left {
|
||||||
|
match state.pending {
|
||||||
|
Some(down) if hitbox.is_hovered(window) => {
|
||||||
|
state.pending = None
|
||||||
|
for listener in listeners {
|
||||||
|
listener({ down, up, }, window, app)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => state.pending = None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_ => ()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,427 @@
|
|||||||
|
///|
|
||||||
|
fn px(v : Double) -> @geometry.Pixels {
|
||||||
|
@geometry.px(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
fn pt(x : Double, y : Double) -> @geometry.Point[@geometry.Pixels] {
|
||||||
|
@geometry.point(px(x), px(y))
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
fn mouse_down(x : Double, y : Double) -> @input.MouseEvent {
|
||||||
|
MouseDown({ ..Default::default(), position: pt(x, y), click_count: 1, })
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
fn mouse_up(x : Double, y : Double) -> @input.MouseEvent {
|
||||||
|
MouseUp({ ..Default::default(), position: pt(x, y), click_count: 1, })
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
fn mouse_move(x : Double, y : Double) -> @input.MouseEvent {
|
||||||
|
MouseMove({ ..Default::default(), position: pt(x, y), })
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
fn sp(v : Double) -> @geometry.ScaledPixels {
|
||||||
|
@geometry.ScaledPixels(v)
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
fn sbounds(
|
||||||
|
x : Double,
|
||||||
|
y : Double,
|
||||||
|
w : Double,
|
||||||
|
h : Double,
|
||||||
|
) -> @geometry.Bounds[@geometry.ScaledPixels] {
|
||||||
|
@geometry.bounds(@geometry.point(sp(x), sp(y)), @geometry.size(sp(w), sp(h)))
|
||||||
|
}
|
||||||
|
|
||||||
|
// A counter view: a row with a decrement button, a value bar and an
|
||||||
|
// increment button.
|
||||||
|
|
||||||
|
///|
|
||||||
|
struct Counter {
|
||||||
|
mut count : Int32
|
||||||
|
mut renders : Int32
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
impl @window.Render for Counter with fn render(self, _window, cx) {
|
||||||
|
self.renders += 1
|
||||||
|
@elements.div()
|
||||||
|
.flex()
|
||||||
|
.flex_row()
|
||||||
|
.gap(px(10.0))
|
||||||
|
.p(px(10.0))
|
||||||
|
.size_full()
|
||||||
|
.bg(@color.rgb(0xffffff))
|
||||||
|
.child(
|
||||||
|
@elements.div()
|
||||||
|
.id("minus")
|
||||||
|
.size(px(20.0))
|
||||||
|
.bg(@color.rgb(0xcc0000))
|
||||||
|
.on_click(
|
||||||
|
@window.listener(cx, fn(counter, _, _, cx) {
|
||||||
|
counter.count -= 1
|
||||||
|
cx.notify()
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.child(
|
||||||
|
@elements.div()
|
||||||
|
.w(px((self.count * 10).to_double()))
|
||||||
|
.h(px(20.0))
|
||||||
|
.bg(@color.rgb(0x0000cc)),
|
||||||
|
)
|
||||||
|
.child(
|
||||||
|
@elements.div()
|
||||||
|
.id("plus")
|
||||||
|
.size(px(20.0))
|
||||||
|
.bg(@color.rgb(0x00cc00))
|
||||||
|
.hover(fn(style) { style.bg(@color.rgb(0x00ff00)) })
|
||||||
|
.on_click(
|
||||||
|
@window.listener(cx, fn(counter, _, _, cx) {
|
||||||
|
counter.count += 1
|
||||||
|
cx.notify()
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.into_any_element()
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
fn setup_counter(
|
||||||
|
count~ : Int32 = 3,
|
||||||
|
scale_factor~ : Double = 1.0,
|
||||||
|
) -> (@app.App, @window.Window, @app.Entity[Counter]) {
|
||||||
|
let app = @app.App::new()
|
||||||
|
let window = @window.Window::new(
|
||||||
|
app,
|
||||||
|
@geometry.size(px(200.0), px(40.0)),
|
||||||
|
scale_factor~,
|
||||||
|
)
|
||||||
|
let counter : @app.Entity[Counter] = app.new_entity(fn(_) {
|
||||||
|
{ count, renders: 0, }
|
||||||
|
})
|
||||||
|
window.set_root_view(counter)
|
||||||
|
(app, window, counter)
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
test "a view renders a laid-out div tree into the scene" {
|
||||||
|
let (_, window, _) = setup_counter()
|
||||||
|
let scene = window.draw()
|
||||||
|
// Root background, minus, value bar (3 * 10px) and plus.
|
||||||
|
assert_eq(scene.quads.length(), 4)
|
||||||
|
let bounds = scene.quads.map(fn(q) { q.bounds })
|
||||||
|
assert_eq(bounds, [
|
||||||
|
sbounds(0.0, 0.0, 200.0, 40.0),
|
||||||
|
sbounds(10.0, 10.0, 20.0, 20.0),
|
||||||
|
sbounds(40.0, 10.0, 30.0, 20.0),
|
||||||
|
sbounds(80.0, 10.0, 20.0, 20.0),
|
||||||
|
])
|
||||||
|
assert_eq(scene.quads[1].background, @color.rgb(0xcc0000).to_background())
|
||||||
|
assert_eq(scene.batches().map(fn(b) { b.label() }), ["quads (4)"])
|
||||||
|
assert_eq(window.frame_count(), 1)
|
||||||
|
assert_false(window.is_dirty())
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
test "device pixels at 2x" {
|
||||||
|
let (_, window, _) = setup_counter(scale_factor=2.0)
|
||||||
|
let scene = window.draw()
|
||||||
|
assert_eq(scene.quads[3].bounds, sbounds(160.0, 20.0, 40.0, 40.0))
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
test "clicks update the view and invalidate the window" {
|
||||||
|
let (app, window, counter) = setup_counter()
|
||||||
|
ignore(window.draw())
|
||||||
|
// Click "plus" (80..100, 10..30).
|
||||||
|
ignore(window.dispatch_mouse_event(mouse_down(90.0, 20.0)))
|
||||||
|
ignore(window.dispatch_mouse_event(mouse_up(90.0, 20.0)))
|
||||||
|
assert_eq(counter.read(app).count, 4)
|
||||||
|
assert_true(window.is_dirty())
|
||||||
|
let scene = window.draw()
|
||||||
|
// The value bar grew to 40px and pushed "plus" to the right.
|
||||||
|
assert_eq(scene.quads[2].bounds.size.width, sp(40.0))
|
||||||
|
assert_eq(scene.quads[3].bounds.origin.x, sp(90.0))
|
||||||
|
// Click "minus" twice.
|
||||||
|
for _ in 0..<2 {
|
||||||
|
ignore(window.dispatch_mouse_event(mouse_down(15.0, 15.0)))
|
||||||
|
ignore(window.dispatch_mouse_event(mouse_up(15.0, 15.0)))
|
||||||
|
ignore(window.draw())
|
||||||
|
}
|
||||||
|
assert_eq(counter.read(app).count, 2)
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
test "a click needs press and release over the same element" {
|
||||||
|
let (app, window, counter) = setup_counter()
|
||||||
|
ignore(window.draw())
|
||||||
|
// Press on "plus", release elsewhere: no click.
|
||||||
|
ignore(window.dispatch_mouse_event(mouse_down(90.0, 20.0)))
|
||||||
|
ignore(window.dispatch_mouse_event(mouse_up(150.0, 20.0)))
|
||||||
|
assert_eq(counter.read(app).count, 3)
|
||||||
|
// Press elsewhere, release on "plus": no click either.
|
||||||
|
ignore(window.dispatch_mouse_event(mouse_down(150.0, 20.0)))
|
||||||
|
ignore(window.dispatch_mouse_event(mouse_up(90.0, 20.0)))
|
||||||
|
assert_eq(counter.read(app).count, 3)
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
test "hover styles apply under the mouse" {
|
||||||
|
let (_, window, _) = setup_counter()
|
||||||
|
ignore(window.draw())
|
||||||
|
ignore(window.dispatch_mouse_event(mouse_move(90.0, 20.0)))
|
||||||
|
// Entering a hitbox marks the window dirty.
|
||||||
|
assert_true(window.is_dirty())
|
||||||
|
let scene = window.draw()
|
||||||
|
assert_eq(scene.quads[3].background, @color.rgb(0x00ff00).to_background())
|
||||||
|
ignore(window.dispatch_mouse_event(mouse_move(150.0, 20.0)))
|
||||||
|
let scene = window.draw()
|
||||||
|
assert_eq(scene.quads[3].background, @color.rgb(0x00cc00).to_background())
|
||||||
|
// Moving within nothing interactive does not dirty the window.
|
||||||
|
ignore(window.dispatch_mouse_event(mouse_move(151.0, 20.0)))
|
||||||
|
assert_false(window.is_dirty())
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
test "only rendered views invalidate the window" {
|
||||||
|
let (app, window, counter) = setup_counter()
|
||||||
|
ignore(window.draw())
|
||||||
|
let unrelated : @app.Entity[Counter] = app.new_entity(fn(_) {
|
||||||
|
{ count: 0, renders: 0, }
|
||||||
|
})
|
||||||
|
unrelated.update(app, fn(_, cx) { cx.notify() })
|
||||||
|
assert_false(window.is_dirty())
|
||||||
|
assert_false(window.draw_if_dirty())
|
||||||
|
counter.update(app, fn(_, cx) { cx.notify() })
|
||||||
|
assert_true(window.draw_if_dirty())
|
||||||
|
assert_eq(counter.read(app).renders, 2)
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
struct Panel {
|
||||||
|
child : @app.Entity[Counter]
|
||||||
|
log : Array[String]
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
impl @window.Render for Panel with fn render(self, _window, _cx) {
|
||||||
|
let log = self.log
|
||||||
|
@elements.div()
|
||||||
|
.size_full()
|
||||||
|
.p(px(4.0))
|
||||||
|
.on_mouse_down(Left, fn(_, _, _) { log.push("panel down") })
|
||||||
|
.child(self.child)
|
||||||
|
.into_any_element()
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
test "nested views and event bubbling" {
|
||||||
|
let app = @app.App::new()
|
||||||
|
let window = @window.Window::new(app, @geometry.size(px(208.0), px(48.0)))
|
||||||
|
let child : @app.Entity[Counter] = app.new_entity(fn(_) {
|
||||||
|
{ count: 0, renders: 0, }
|
||||||
|
})
|
||||||
|
let panel : @app.Entity[Panel] = app.new_entity(fn(_) { { child, log: [], } })
|
||||||
|
window.set_root_view(panel)
|
||||||
|
let scene = window.draw()
|
||||||
|
// The child view is laid out inside the panel's padding.
|
||||||
|
assert_eq(scene.quads[0].bounds.origin, @geometry.point(sp(4.0), sp(4.0)))
|
||||||
|
// With a count of 0 the value bar is empty: "plus" spans x 54..74.
|
||||||
|
// A press on it reaches the button first, then bubbles to the panel.
|
||||||
|
ignore(window.dispatch_mouse_event(mouse_down(64.0, 24.0)))
|
||||||
|
ignore(window.dispatch_mouse_event(mouse_up(64.0, 24.0)))
|
||||||
|
assert_eq(child.read(app).count, 1)
|
||||||
|
assert_eq(panel.read(app).log, ["panel down"])
|
||||||
|
// Notifying the nested view invalidates the window too.
|
||||||
|
ignore(window.draw())
|
||||||
|
child.update(app, fn(_, cx) { cx.notify() })
|
||||||
|
assert_true(window.is_dirty())
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
struct Layers {
|
||||||
|
log : Array[String]
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
impl @window.Render for Layers with fn render(self, _window, _cx) {
|
||||||
|
let log = self.log
|
||||||
|
@elements.div()
|
||||||
|
.size_full()
|
||||||
|
.relative()
|
||||||
|
.on_mouse_down(Left, fn(_, _, _) { log.push("background") })
|
||||||
|
.child(
|
||||||
|
@elements.div()
|
||||||
|
.absolute()
|
||||||
|
.size(px(50.0))
|
||||||
|
.occlude()
|
||||||
|
.on_mouse_down(Left, fn(_, window, _) {
|
||||||
|
log.push("overlay")
|
||||||
|
window.stop_propagation()
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
.into_any_element()
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
test "occlusion and stop_propagation" {
|
||||||
|
let app = @app.App::new()
|
||||||
|
let window = @window.Window::new(app, @geometry.size(px(100.0), px(100.0)))
|
||||||
|
let view : @app.Entity[Layers] = app.new_entity(fn(_) { { log: [], } })
|
||||||
|
window.set_root_view(view)
|
||||||
|
ignore(window.draw())
|
||||||
|
let propagated = window.dispatch_mouse_event(mouse_down(10.0, 10.0))
|
||||||
|
assert_false(propagated)
|
||||||
|
ignore(window.dispatch_mouse_event(mouse_down(80.0, 80.0)))
|
||||||
|
assert_eq(view.read(app).log, ["overlay", "background"])
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
struct Styled {
|
||||||
|
opacity : Double
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
impl @window.Render for Styled with fn render(self, _window, _cx) {
|
||||||
|
@elements.div()
|
||||||
|
.size_full()
|
||||||
|
.overflow_hidden()
|
||||||
|
.opacity(self.opacity)
|
||||||
|
.shadow_sm()
|
||||||
|
.rounded_md()
|
||||||
|
.border_2()
|
||||||
|
.border_color(@color.black())
|
||||||
|
.bg(@color.rgb(0x336699))
|
||||||
|
.child(@elements.div().w(px(500.0)).h(px(10.0)).bg(@color.red()))
|
||||||
|
.into_any_element()
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
test "shadows, borders, opacity and overflow clipping" {
|
||||||
|
let app = @app.App::new()
|
||||||
|
let window = @window.Window::new(app, @geometry.size(px(100.0), px(100.0)))
|
||||||
|
let view : @app.Entity[Styled] = app.new_entity(fn(_) { { opacity: 0.5, } })
|
||||||
|
window.set_root_view(view)
|
||||||
|
let scene = window.draw()
|
||||||
|
assert_eq(scene.shadows.length(), 2)
|
||||||
|
// Background, child, border.
|
||||||
|
assert_eq(scene.quads.length(), 3)
|
||||||
|
let background = scene.quads[0]
|
||||||
|
assert_eq(background.background.solid.a, 0.5)
|
||||||
|
assert_eq(background.corner_radii.top_left, sp(6.0))
|
||||||
|
let border = scene.quads.filter(fn(q) { q.border_widths.top == sp(2.0) })
|
||||||
|
assert_eq(border.length(), 1)
|
||||||
|
assert_eq(border[0].border_color.a, 0.5)
|
||||||
|
// The wide child is clipped to the inside of the border.
|
||||||
|
let child = scene.quads.filter(fn(q) { q.bounds.size.width == sp(500.0) })
|
||||||
|
assert_eq(child[0].content_mask.bounds, sbounds(2.0, 2.0, 96.0, 96.0))
|
||||||
|
// Quads overlapping quads share a draw order and form one batch, drawn in
|
||||||
|
// insertion order: background, child, then the border on top.
|
||||||
|
assert_eq(scene.batches().map(fn(b) { b.label() }), [
|
||||||
|
"shadows (2)", "quads (3)",
|
||||||
|
])
|
||||||
|
assert_eq(scene.quads[2].border_widths.top, sp(2.0))
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
enum Drawing {
|
||||||
|
Drawing
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
impl @window.Render for Drawing with fn render(_self, _window, _cx) {
|
||||||
|
@elements.div()
|
||||||
|
.size_full()
|
||||||
|
.p(px(10.0))
|
||||||
|
.child(
|
||||||
|
@elements.canvas(fn(bounds, window, _) {
|
||||||
|
window.paint_path(
|
||||||
|
[[bounds.origin, bounds.top_right(), bounds.bottom_left()]],
|
||||||
|
@color.blue().to_background(),
|
||||||
|
)
|
||||||
|
}).size(px(30.0)),
|
||||||
|
)
|
||||||
|
.into_any_element()
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
test "canvas paints with its bounds" {
|
||||||
|
let app = @app.App::new()
|
||||||
|
let window = @window.Window::new(app, @geometry.size(px(100.0), px(100.0)))
|
||||||
|
let view : @app.Entity[Drawing] = app.new_entity(fn(_) { Drawing })
|
||||||
|
window.set_root_view(view)
|
||||||
|
let scene = window.draw()
|
||||||
|
assert_eq(scene.paths.length(), 1)
|
||||||
|
assert_eq(scene.paths[0].bounds, sbounds(10.0, 10.0, 30.0, 30.0))
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
test "resizing relayouts" {
|
||||||
|
let (_, window, _) = setup_counter()
|
||||||
|
ignore(window.draw())
|
||||||
|
window.resize(@geometry.size(px(300.0), px(60.0)), scale_factor=2.0)
|
||||||
|
assert_true(window.is_dirty())
|
||||||
|
let scene = window.draw()
|
||||||
|
assert_eq(scene.quads[0].bounds, sbounds(0.0, 0.0, 600.0, 120.0))
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
enum Broken {
|
||||||
|
Broken
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
impl @window.Render for Broken with fn render(_self, _window, _cx) {
|
||||||
|
@elements.div().on_click(fn(_, _, _) { }).into_any_element()
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
test "panic on on_click without an id" {
|
||||||
|
let app = @app.App::new()
|
||||||
|
let window = @window.Window::new(app, @geometry.size(px(10.0), px(10.0)))
|
||||||
|
let view : @app.Entity[Broken] = app.new_entity(fn(_) { Broken })
|
||||||
|
window.set_root_view(view)
|
||||||
|
ignore(window.draw())
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
test "panic on drawing without a root view" {
|
||||||
|
let window = @window.Window::new(
|
||||||
|
@app.App::new(),
|
||||||
|
@geometry.size(px(10.0), px(10.0)),
|
||||||
|
)
|
||||||
|
ignore(window.draw())
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
enum PaintsDuringLayout {
|
||||||
|
PaintsDuringLayout
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
impl @window.Render for PaintsDuringLayout with fn render(_self, window, _cx) {
|
||||||
|
window.paint_quad(
|
||||||
|
@window.fill(
|
||||||
|
@geometry.bounds(pt(0.0, 0.0), @geometry.size(px(1.0), px(1.0))),
|
||||||
|
@color.red().to_background(),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
@window.empty()
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
test "panic on painting outside the paint phase" {
|
||||||
|
let app = @app.App::new()
|
||||||
|
let window = @window.Window::new(app, @geometry.size(px(10.0), px(10.0)))
|
||||||
|
let view : @app.Entity[PaintsDuringLayout] = app.new_entity(fn(_) {
|
||||||
|
PaintsDuringLayout
|
||||||
|
})
|
||||||
|
window.set_root_view(view)
|
||||||
|
ignore(window.draw())
|
||||||
|
}
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
import {
|
||||||
|
"experiments/gpui/geometry",
|
||||||
|
"experiments/gpui/style",
|
||||||
|
"experiments/gpui/input",
|
||||||
|
"experiments/gpui/app",
|
||||||
|
"experiments/gpui/window",
|
||||||
|
}
|
||||||
|
|
||||||
|
options(
|
||||||
|
"allow-experimental-packages": true,
|
||||||
|
)
|
||||||
|
|
||||||
|
import {
|
||||||
|
"experiments/gpui/color",
|
||||||
|
} for "test"
|
||||||
File diff suppressed because it is too large.
Load diff
+95
-159
@@ -1,187 +1,123 @@
|
|||||||
// A headless example of what exists so far (M0-M2): describe a "card" with
|
// A headless example of what exists so far (M0-M6): a view renders a card
|
||||||
// GPUI's fluent style API, lay it out with the layout engine (text is a fake
|
// with GPUI's `div()` and fluent styles; the window lays it out, paints it
|
||||||
// fixed-width measure until the text system lands), and paint it into a
|
// into a Scene and dispatches a simulated click. There is no renderer or
|
||||||
// Scene. There is no renderer yet, so the output is a text report.
|
// 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
|
// Run with: moon run src/examples/card --target js
|
||||||
|
|
||||||
///|
|
///|
|
||||||
/// The card's style, written exactly as in GPUI:
|
struct Card {
|
||||||
/// `div().flex().p_4().rounded_lg().border_1().shadow_md().bg(...)`.
|
mut expanded : Bool
|
||||||
fn card_style() -> @style.StyleRefinement {
|
mut clicks : Int32
|
||||||
@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
|
/// A placeholder for a line of text (the text system arrives in M9).
|
||||||
/// GPUI's `Style::paint` will once the Window exists (M6).
|
fn text_bar(width : Double, color : UInt32) -> @elements.Div {
|
||||||
fn paint_card(
|
@elements.div()
|
||||||
scene : @scene.Scene,
|
.w(@geometry.px(width))
|
||||||
style : @style.Style,
|
.h(@geometry.px(10.0))
|
||||||
bounds : @geometry.Bounds[@geometry.Pixels],
|
.rounded_sm()
|
||||||
rem_size : @geometry.Pixels,
|
.bg(@color.rgb(color))
|
||||||
scale_factor : Double,
|
}
|
||||||
) -> Unit {
|
|
||||||
let corner_radii = style.corner_radii
|
///|
|
||||||
.absolute_to_pixels(rem_size)
|
impl @window.Render for Card with fn render(self, _window, cx) {
|
||||||
.clamp_radii_for_quad_size(bounds.size)
|
let body_lines = if self.expanded { 4 } else { 1 }
|
||||||
let mask : @geometry.ContentMask[@geometry.Pixels] = {
|
let card = @elements.div()
|
||||||
bounds: bounds.dilate(@geometry.px(64.0)),
|
.id("card")
|
||||||
}
|
.flex()
|
||||||
for shadow in style.box_shadow {
|
.flex_col()
|
||||||
let shadow_bounds = bounds
|
.gap_2()
|
||||||
.translate(shadow.offset)
|
.p_4()
|
||||||
.dilate(shadow.spread_radius)
|
.w(@geometry.px(320.0))
|
||||||
scene.insert_primitive(
|
.rounded_lg()
|
||||||
Shadow({
|
.border_1()
|
||||||
order: 0,
|
.border_color(@color.rgb(0xd0d7de))
|
||||||
blur_radius: shadow.blur_radius.scale(scale_factor),
|
.bg(@color.rgb(0xffffff))
|
||||||
bounds: shadow_bounds.scale(scale_factor),
|
.shadow_md()
|
||||||
corner_radii: corner_radii.scale(scale_factor),
|
.hover(fn(style) { style.border_color(@color.rgb(0x0969da)) })
|
||||||
content_mask: mask.scale(scale_factor),
|
.on_click(
|
||||||
color: shadow.color,
|
@window.listener(cx, fn(card, _, _, cx) {
|
||||||
shape_bounds: bounds.scale(scale_factor),
|
card.expanded = !card.expanded
|
||||||
shape_corner_radii: corner_radii.scale(scale_factor),
|
card.clicks += 1
|
||||||
inset: false,
|
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 {
|
@elements.div()
|
||||||
Some(fill) => fill.color().unwrap()
|
.size_full()
|
||||||
None => Default::default()
|
.flex()
|
||||||
}
|
.items_start()
|
||||||
scene.insert_primitive(
|
.p(@geometry.px(40.0))
|
||||||
Quad({
|
.bg(@color.rgb(0xf6f8fa))
|
||||||
order: 0,
|
.child(card)
|
||||||
border_style: style.border_style,
|
.into_any_element()
|
||||||
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),
|
|
||||||
}),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
///|
|
///|
|
||||||
/// A stand-in for text measurement (M9): `char_width` per character, wrapping
|
fn describe(
|
||||||
/// at the available width, one `line_height` per line.
|
label : String,
|
||||||
fn fake_text(
|
scene : @scene.Scene,
|
||||||
chars : Int32,
|
lines : Array[String],
|
||||||
char_width : Double,
|
) -> Unit {
|
||||||
line_height : Double,
|
lines.push(label)
|
||||||
) -> @layout.MeasureFn {
|
let batches = scene.batches().map(fn(b) { b.label() }).join(", ")
|
||||||
fn(known, available) {
|
lines.push(" batches: \{batches}")
|
||||||
let natural = chars.to_double() * char_width
|
for quad in scene.quads {
|
||||||
let wrap = match (known.width, available.width) {
|
let b = quad.bounds
|
||||||
(Some(width), _) => width.to_double()
|
let extra = if quad.border_widths.top.to_double() > 0.0 {
|
||||||
(None, Definite(width)) => width.to_double()
|
" border \{quad.border_color.to_rgb()}"
|
||||||
(None, _) => natural
|
} else {
|
||||||
|
""
|
||||||
}
|
}
|
||||||
let width = if natural < wrap { natural } else { wrap }
|
lines.push(
|
||||||
let per_line = (wrap / char_width).floor()
|
" quad (\{b.origin.x.to_double()}, \{b.origin.y.to_double()}) \{b.size.width.to_double()} x \{b.size.height.to_double()}\{extra}",
|
||||||
let lines = (chars.to_double() / per_line).ceil()
|
)
|
||||||
@geometry.size(@geometry.px(width), @geometry.px(lines * line_height))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
///|
|
|
||||||
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`.
|
/// Builds the report printed by `main`.
|
||||||
fn report() -> String {
|
fn report() -> String {
|
||||||
let rem_size = @geometry.px(16.0)
|
let app = @app.App::new()
|
||||||
let scale_factor = 2.0
|
let window = @window.Window::new(
|
||||||
let style = (Default::default() : @style.Style).refined(card_style())
|
app,
|
||||||
let text = (Default::default() : @style.TextStyle).refined(style.text)
|
@geometry.size(@geometry.px(400.0), @geometry.px(240.0)),
|
||||||
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 body = engine.request_measured_layout(
|
let card : @app.Entity[Card] = app.new_entity(fn(_) {
|
||||||
leaf_style,
|
{ expanded: false, clicks: 0, }
|
||||||
rem_size,
|
})
|
||||||
scale_factor,
|
window.set_root_view(card)
|
||||||
fake_text(90, 7.0, line_height),
|
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])
|
ignore(window.draw_if_dirty())
|
||||||
let root_style = (Default::default() : @style.Style).refined(
|
ignore(
|
||||||
@style.StyleRefinement::new().flex().items_start().p(@geometry.px(40.0)),
|
window.dispatch_mouse_event(
|
||||||
|
MouseDown({ ..Default::default(), position: inside, click_count: 1, }),
|
||||||
|
),
|
||||||
)
|
)
|
||||||
let root = engine.request_layout(root_style, rem_size, scale_factor, [card])
|
ignore(
|
||||||
let window_size = @geometry.size(@geometry.px(480.0), @geometry.px(320.0))
|
window.dispatch_mouse_event(
|
||||||
engine.stretch_auto_size_to_fill(root, window_size, scale_factor)
|
MouseUp({ ..Default::default(), position: inside, click_count: 1, }),
|
||||||
engine.compute_layout(
|
),
|
||||||
root,
|
|
||||||
@layout.AvailableSpace::from_size(window_size),
|
|
||||||
scale_factor,
|
|
||||||
)
|
)
|
||||||
let card_bounds = engine.layout_bounds(card, scale_factor)
|
let state = card.read(app)
|
||||||
let title_bounds = engine.layout_bounds(title, scale_factor)
|
lines.push(
|
||||||
let body_bounds = engine.layout_bounds(body, scale_factor)
|
"clicked: expanded = \{state.expanded}, clicks = \{state.clicks}, dirty = \{window.is_dirty()}",
|
||||||
// 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,
|
|
||||||
}),
|
|
||||||
)
|
)
|
||||||
scene.finish()
|
describe("after click (hovered)", window.draw(), lines)
|
||||||
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",
|
|
||||||
)
|
|
||||||
}
|
|
||||||
lines.join("\n")
|
lines.join("\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,22 +3,24 @@ test "card report" {
|
|||||||
inspect(
|
inspect(
|
||||||
report(),
|
report(),
|
||||||
content=(
|
content=(
|
||||||
#|card style
|
#|first frame
|
||||||
#| display: Flex, direction: Column
|
#| batches: quads (1), shadows (2), quads (4)
|
||||||
#| width: 320px, padding: 1rem, gap: 0.5rem
|
#| quad (0, 0) 400 x 240
|
||||||
#| radius: 0.5rem = 8px
|
#| quad (40, 40) 320 x 62
|
||||||
#| shadows: 2
|
#| quad (57, 57) 160 x 10
|
||||||
#| text: 14px .SystemUIFont, line height 23px, color #1f2328ff
|
#| quad (57, 75) 280 x 10
|
||||||
#|layout (logical px)
|
#| quad (40, 40) 320 x 62 border #d0d7deff
|
||||||
#| window (0px, 0px) 480px x 320px
|
#|clicked: expanded = true, clicks = 1, dirty = true
|
||||||
#| card (40px, 40px) 320px x 134px
|
#|after click (hovered)
|
||||||
#| title (57px, 57px) 286px x 23px
|
#| batches: quads (1), shadows (2), quads (7)
|
||||||
#| body (57px, 88px) 286px x 69px
|
#| quad (0, 0) 400 x 240
|
||||||
#|scene at 2x
|
#| quad (40, 40) 320 x 116
|
||||||
#| shadows (2)
|
#| quad (57, 57) 160 x 10
|
||||||
#| quads (1)
|
#| quad (57, 75) 280 x 10
|
||||||
#| underlines (1)
|
#| quad (57, 93) 280 x 10
|
||||||
#| quad order 1: 640 x 268 device px
|
#| quad (57, 111) 280 x 10
|
||||||
|
#| quad (57, 129) 280 x 10
|
||||||
|
#| quad (40, 40) 320 x 116 border #0969daff
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@@ -2,9 +2,13 @@ import {
|
|||||||
"experiments/gpui/geometry",
|
"experiments/gpui/geometry",
|
||||||
"experiments/gpui/color",
|
"experiments/gpui/color",
|
||||||
"experiments/gpui/scene",
|
"experiments/gpui/scene",
|
||||||
"experiments/gpui/style",
|
"experiments/gpui/app",
|
||||||
"experiments/gpui/layout",
|
"experiments/gpui/window",
|
||||||
"moonbitlang/core/debug",
|
"experiments/gpui/elements",
|
||||||
}
|
}
|
||||||
|
|
||||||
pkgtype(kind: "executable")
|
pkgtype(kind: "executable")
|
||||||
|
|
||||||
|
options(
|
||||||
|
"allow-experimental-packages": true,
|
||||||
|
)
|
||||||
@@ -6,6 +6,7 @@ package "experiments/gpui/examples/card"
|
|||||||
// Errors
|
// Errors
|
||||||
|
|
||||||
// Types and methods
|
// Types and methods
|
||||||
|
type Card
|
||||||
|
|
||||||
// Type aliases
|
// Type aliases
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,130 @@
|
|||||||
|
// Input event types, ported from gpui/src/interactive.rs and the Modifiers of
|
||||||
|
// gpui/src/platform/keystroke.rs. Keyboard events and keystrokes come with
|
||||||
|
// the keymap milestone (M5).
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// Modifier keys held during an event.
|
||||||
|
#derive(Eq, Hash, Default, Debug)
|
||||||
|
pub(all) struct Modifiers {
|
||||||
|
control : Bool
|
||||||
|
alt : Bool
|
||||||
|
shift : Bool
|
||||||
|
/// Command on macOS, Windows/Super elsewhere.
|
||||||
|
platform : Bool
|
||||||
|
/// The function (fn) key.
|
||||||
|
function : Bool
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// Whether any modifier is held.
|
||||||
|
pub fn Modifiers::modified(self : Modifiers) -> Bool {
|
||||||
|
self.control || self.alt || self.shift || self.platform || self.function
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
#derive(Eq, Hash, Debug)
|
||||||
|
pub(all) enum NavigationDirection {
|
||||||
|
Back
|
||||||
|
Forward
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
#derive(Eq, Hash, Debug)
|
||||||
|
pub(all) enum MouseButton {
|
||||||
|
Left
|
||||||
|
Right
|
||||||
|
Middle
|
||||||
|
Navigate(NavigationDirection)
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
pub impl Default for MouseButton with fn default() {
|
||||||
|
Left
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
#derive(Eq, Default, Debug)
|
||||||
|
pub(all) struct MouseDownEvent {
|
||||||
|
button : MouseButton
|
||||||
|
position : @geometry.Point[@geometry.Pixels]
|
||||||
|
modifiers : Modifiers
|
||||||
|
click_count : Int32
|
||||||
|
first_mouse : Bool
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
#derive(Eq, Default, Debug)
|
||||||
|
pub(all) struct MouseUpEvent {
|
||||||
|
button : MouseButton
|
||||||
|
position : @geometry.Point[@geometry.Pixels]
|
||||||
|
modifiers : Modifiers
|
||||||
|
click_count : Int32
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
#derive(Eq, Default, Debug)
|
||||||
|
pub(all) struct MouseMoveEvent {
|
||||||
|
position : @geometry.Point[@geometry.Pixels]
|
||||||
|
/// The button held during the move, if any.
|
||||||
|
pressed_button : MouseButton?
|
||||||
|
modifiers : Modifiers
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// How a scroll delta is expressed.
|
||||||
|
#derive(Eq, Debug)
|
||||||
|
pub(all) enum ScrollDelta {
|
||||||
|
/// Precise pixels (trackpads).
|
||||||
|
Pixels(@geometry.Point[@geometry.Pixels])
|
||||||
|
/// Lines (mouse wheels).
|
||||||
|
Lines(@geometry.Point[Double])
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
#derive(Eq, Debug)
|
||||||
|
pub(all) struct ScrollWheelEvent {
|
||||||
|
position : @geometry.Point[@geometry.Pixels]
|
||||||
|
delta : ScrollDelta
|
||||||
|
modifiers : Modifiers
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// A mouse click: the down and up events of one press (GPUI:
|
||||||
|
/// `MouseClickEvent`).
|
||||||
|
#derive(Eq, Default, Debug)
|
||||||
|
pub(all) struct ClickEvent {
|
||||||
|
down : MouseDownEvent
|
||||||
|
up : MouseUpEvent
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// Pointer input delivered to a window (GPUI: the mouse variants of
|
||||||
|
/// `PlatformInput`).
|
||||||
|
#derive(Eq, Debug)
|
||||||
|
pub(all) enum MouseEvent {
|
||||||
|
MouseDown(MouseDownEvent)
|
||||||
|
MouseUp(MouseUpEvent)
|
||||||
|
MouseMove(MouseMoveEvent)
|
||||||
|
ScrollWheel(ScrollWheelEvent)
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
pub fn MouseEvent::position(
|
||||||
|
self : MouseEvent,
|
||||||
|
) -> @geometry.Point[@geometry.Pixels] {
|
||||||
|
match self {
|
||||||
|
MouseDown(e) => e.position
|
||||||
|
MouseUp(e) => e.position
|
||||||
|
MouseMove(e) => e.position
|
||||||
|
ScrollWheel(e) => e.position
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// Listeners run twice per event: capture (root to leaf), then bubble (leaf
|
||||||
|
/// to root) (GPUI: `DispatchPhase`).
|
||||||
|
#derive(Eq, Hash, Debug)
|
||||||
|
pub(all) enum DispatchPhase {
|
||||||
|
Capture
|
||||||
|
Bubble
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
import {
|
||||||
|
"experiments/gpui/geometry",
|
||||||
|
}
|
||||||
@@ -0,0 +1,99 @@
|
|||||||
|
// Generated using `moon info`, DON'T EDIT IT
|
||||||
|
package "experiments/gpui/input"
|
||||||
|
|
||||||
|
import {
|
||||||
|
"experiments/gpui/geometry",
|
||||||
|
"moonbitlang/core/debug",
|
||||||
|
}
|
||||||
|
|
||||||
|
// Values
|
||||||
|
|
||||||
|
// Errors
|
||||||
|
|
||||||
|
// Types and methods
|
||||||
|
#derive(Default, Eq, @debug.Debug)
|
||||||
|
pub(all) struct ClickEvent {
|
||||||
|
down : MouseDownEvent
|
||||||
|
up : MouseUpEvent
|
||||||
|
}
|
||||||
|
|
||||||
|
#derive(Eq, Hash, @debug.Debug)
|
||||||
|
pub(all) enum DispatchPhase {
|
||||||
|
Capture
|
||||||
|
Bubble
|
||||||
|
}
|
||||||
|
|
||||||
|
#derive(Default, Eq, Hash, @debug.Debug)
|
||||||
|
pub(all) struct Modifiers {
|
||||||
|
control : Bool
|
||||||
|
alt : Bool
|
||||||
|
shift : Bool
|
||||||
|
platform : Bool
|
||||||
|
function : Bool
|
||||||
|
}
|
||||||
|
pub fn Modifiers::modified(Self) -> Bool
|
||||||
|
|
||||||
|
#derive(Eq, Hash, @debug.Debug)
|
||||||
|
pub(all) enum MouseButton {
|
||||||
|
Left
|
||||||
|
Right
|
||||||
|
Middle
|
||||||
|
Navigate(NavigationDirection)
|
||||||
|
}
|
||||||
|
pub impl Default for MouseButton
|
||||||
|
|
||||||
|
#derive(Default, Eq, @debug.Debug)
|
||||||
|
pub(all) struct MouseDownEvent {
|
||||||
|
button : MouseButton
|
||||||
|
position : @geometry.Point[@geometry.Pixels]
|
||||||
|
modifiers : Modifiers
|
||||||
|
click_count : Int32
|
||||||
|
first_mouse : Bool
|
||||||
|
}
|
||||||
|
|
||||||
|
#derive(Eq, @debug.Debug)
|
||||||
|
pub(all) enum MouseEvent {
|
||||||
|
MouseDown(MouseDownEvent)
|
||||||
|
MouseUp(MouseUpEvent)
|
||||||
|
MouseMove(MouseMoveEvent)
|
||||||
|
ScrollWheel(ScrollWheelEvent)
|
||||||
|
}
|
||||||
|
pub fn MouseEvent::position(Self) -> @geometry.Point[@geometry.Pixels]
|
||||||
|
|
||||||
|
#derive(Default, Eq, @debug.Debug)
|
||||||
|
pub(all) struct MouseMoveEvent {
|
||||||
|
position : @geometry.Point[@geometry.Pixels]
|
||||||
|
pressed_button : MouseButton?
|
||||||
|
modifiers : Modifiers
|
||||||
|
}
|
||||||
|
|
||||||
|
#derive(Default, Eq, @debug.Debug)
|
||||||
|
pub(all) struct MouseUpEvent {
|
||||||
|
button : MouseButton
|
||||||
|
position : @geometry.Point[@geometry.Pixels]
|
||||||
|
modifiers : Modifiers
|
||||||
|
click_count : Int32
|
||||||
|
}
|
||||||
|
|
||||||
|
#derive(Eq, Hash, @debug.Debug)
|
||||||
|
pub(all) enum NavigationDirection {
|
||||||
|
Back
|
||||||
|
Forward
|
||||||
|
}
|
||||||
|
|
||||||
|
#derive(Eq, @debug.Debug)
|
||||||
|
pub(all) enum ScrollDelta {
|
||||||
|
Pixels(@geometry.Point[@geometry.Pixels])
|
||||||
|
Lines(@geometry.Point[Double])
|
||||||
|
}
|
||||||
|
|
||||||
|
#derive(Eq, @debug.Debug)
|
||||||
|
pub(all) struct ScrollWheelEvent {
|
||||||
|
position : @geometry.Point[@geometry.Pixels]
|
||||||
|
delta : ScrollDelta
|
||||||
|
modifiers : Modifiers
|
||||||
|
}
|
||||||
|
|
||||||
|
// Type aliases
|
||||||
|
|
||||||
|
// Traits
|
||||||
@@ -0,0 +1,206 @@
|
|||||||
|
// Elements and views, ported from gpui/src/element.rs and gpui/src/view.rs.
|
||||||
|
//
|
||||||
|
// Deviations from GPUI (see docs/PORTING.md):
|
||||||
|
// - `Element` has no associated `RequestLayoutState`/`PrepaintState` types:
|
||||||
|
// an element keeps per-frame state in its own mutable fields, set in
|
||||||
|
// `request_layout` and read in `prepaint` and `paint`. Elements are built
|
||||||
|
// fresh every frame, as in GPUI.
|
||||||
|
// - `render` returns `AnyElement` (no `impl IntoElement` return types).
|
||||||
|
// - Global element ids are paths of strings.
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// A node of the element tree, laid out and painted each frame (GPUI:
|
||||||
|
/// `Element`).
|
||||||
|
pub(open) trait Element {
|
||||||
|
/// Requests a layout node (and those of the children).
|
||||||
|
fn request_layout(Self, Window, @app.App) -> @layout.LayoutId
|
||||||
|
/// Runs after layout with the element's bounds: hitboxes, child offsets.
|
||||||
|
fn prepaint(Self, @geometry.Bounds[@geometry.Pixels], Window, @app.App) -> Unit
|
||||||
|
/// Emits primitives into the scene and registers event listeners.
|
||||||
|
fn paint(Self, @geometry.Bounds[@geometry.Pixels], Window, @app.App) -> Unit
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// Anything that can become an element (GPUI: `IntoElement`).
|
||||||
|
pub(open) trait IntoElement {
|
||||||
|
fn into_any_element(Self) -> AnyElement
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// A type-erased element plus its per-frame layout state (GPUI:
|
||||||
|
/// `AnyElement`, with `Drawable`'s phase tracking).
|
||||||
|
pub struct AnyElement {
|
||||||
|
priv element : &Element
|
||||||
|
priv mut layout_id : @layout.LayoutId?
|
||||||
|
priv mut bounds : @geometry.Bounds[@geometry.Pixels]?
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
pub fn AnyElement::new(element : &Element) -> AnyElement {
|
||||||
|
{ element, layout_id: None, bounds: None, }
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
pub impl IntoElement for AnyElement with fn into_any_element(self) {
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// Requests layout; must be called once per frame, before `prepaint`.
|
||||||
|
pub fn AnyElement::request_layout(
|
||||||
|
self : AnyElement,
|
||||||
|
window : Window,
|
||||||
|
app : @app.App,
|
||||||
|
) -> @layout.LayoutId {
|
||||||
|
guard self.layout_id is None else {
|
||||||
|
abort("element layout requested twice in one frame")
|
||||||
|
}
|
||||||
|
let id = self.element.request_layout(window, app)
|
||||||
|
self.layout_id = Some(id)
|
||||||
|
id
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// Resolves the element's bounds from layout (plus the window's element
|
||||||
|
/// offset) and prepaints it.
|
||||||
|
pub fn AnyElement::prepaint(
|
||||||
|
self : AnyElement,
|
||||||
|
window : Window,
|
||||||
|
app : @app.App,
|
||||||
|
) -> Unit {
|
||||||
|
guard self.layout_id is Some(layout_id) else {
|
||||||
|
abort("element prepainted before its layout was requested")
|
||||||
|
}
|
||||||
|
let bounds = window.layout_bounds(layout_id)
|
||||||
|
self.bounds = Some(bounds)
|
||||||
|
self.element.prepaint(bounds, window, app)
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// Paints the element at the bounds computed in `prepaint`.
|
||||||
|
pub fn AnyElement::paint(
|
||||||
|
self : AnyElement,
|
||||||
|
window : Window,
|
||||||
|
app : @app.App,
|
||||||
|
) -> Unit {
|
||||||
|
guard self.bounds is Some(bounds) else {
|
||||||
|
abort("element painted before it was prepainted")
|
||||||
|
}
|
||||||
|
self.element.paint(bounds, window, app)
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// The bounds from the last `prepaint`, if any.
|
||||||
|
pub fn AnyElement::bounds(
|
||||||
|
self : AnyElement,
|
||||||
|
) -> @geometry.Bounds[@geometry.Pixels]? {
|
||||||
|
self.bounds
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// A view's state renders itself into an element tree each frame (GPUI:
|
||||||
|
/// `Render`).
|
||||||
|
pub(open) trait Render {
|
||||||
|
fn render(Self, Window, @app.Context[Self]) -> AnyElement
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// A type-erased view: an entity whose state implements `Render` (GPUI:
|
||||||
|
/// `AnyView`).
|
||||||
|
pub struct AnyView {
|
||||||
|
entity_id : @app.EntityId
|
||||||
|
priv render_fn : (Window, @app.App) -> AnyElement
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
pub fn[V : Render] AnyView::from_entity(entity : @app.Entity[V]) -> AnyView {
|
||||||
|
{
|
||||||
|
entity_id: entity.entity_id(),
|
||||||
|
render_fn: fn(window, app) {
|
||||||
|
app.update_entity(entity, fn(state, cx) {
|
||||||
|
Render::render(state, window, cx)
|
||||||
|
})
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// The element a view renders into for one frame.
|
||||||
|
priv struct ViewElement {
|
||||||
|
view : AnyView
|
||||||
|
mut rendered : AnyElement?
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
impl Element for ViewElement with fn request_layout(self, window, app) {
|
||||||
|
window.record_rendered_view(self.view.entity_id)
|
||||||
|
let element = (self.view.render_fn)(window, app)
|
||||||
|
self.rendered = Some(element)
|
||||||
|
element.request_layout(window, app)
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
impl Element for ViewElement with fn prepaint(self, _bounds, window, app) {
|
||||||
|
self.rendered.unwrap().prepaint(window, app)
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
impl Element for ViewElement with fn paint(self, _bounds, window, app) {
|
||||||
|
self.rendered.unwrap().paint(window, app)
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
pub impl IntoElement for AnyView with fn into_any_element(self) {
|
||||||
|
AnyElement::new(({ view: self, rendered: None, } : ViewElement))
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
pub extend AnyView with IntoElement::{into_any_element}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// Views are elements: `div().child(entity)` renders the entity.
|
||||||
|
pub impl[V : Render] IntoElement for @app.Entity[V] with fn into_any_element(
|
||||||
|
self,
|
||||||
|
) {
|
||||||
|
AnyView::from_entity(self).into_any_element()
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// Wraps a view method as an event listener that updates the view (GPUI:
|
||||||
|
/// `cx.listener`). The listener does nothing once the view is released.
|
||||||
|
pub fn[T, E] listener(
|
||||||
|
cx : @app.Context[T],
|
||||||
|
f : (T, E, Window, @app.Context[T]) -> Unit,
|
||||||
|
) -> (E, Window, @app.App) -> Unit {
|
||||||
|
let weak = cx.weak_entity()
|
||||||
|
fn(event, window, app) {
|
||||||
|
ignore(weak.update(app, fn(state, cx) { f(state, event, window, cx) }))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// An element that takes no space and paints nothing (GPUI: `Empty`).
|
||||||
|
priv enum Empty {
|
||||||
|
Empty
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
impl Element for Empty with fn request_layout(_self, window, _app) {
|
||||||
|
window.request_layout(Default::default(), [])
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
impl Element for Empty with fn prepaint(_self, _bounds, _window, _app) {
|
||||||
|
()
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
impl Element for Empty with fn paint(_self, _bounds, _window, _app) {
|
||||||
|
()
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// An empty element.
|
||||||
|
pub fn empty() -> AnyElement {
|
||||||
|
AnyElement::new(Empty::Empty)
|
||||||
|
}
|
||||||
@@ -0,0 +1,16 @@
|
|||||||
|
import {
|
||||||
|
"experiments/gpui/geometry",
|
||||||
|
"experiments/gpui/color",
|
||||||
|
"experiments/gpui/scene",
|
||||||
|
"experiments/gpui/style",
|
||||||
|
"experiments/gpui/layout",
|
||||||
|
"experiments/gpui/input",
|
||||||
|
"experiments/gpui/app",
|
||||||
|
"moonbitlang/core/hashset",
|
||||||
|
"moonbitlang/core/cmp",
|
||||||
|
"moonbitlang/core/debug",
|
||||||
|
}
|
||||||
|
|
||||||
|
options(
|
||||||
|
"allow-experimental-packages": true,
|
||||||
|
)
|
||||||
@@ -0,0 +1,301 @@
|
|||||||
|
// Painting, ported from gpui/src/window.rs (`paint_quad`, `paint_drop_shadows`,
|
||||||
|
// `paint_inset_shadows`, the snapping helpers) and `Style::paint` from
|
||||||
|
// gpui/src/style.rs.
|
||||||
|
//
|
||||||
|
// Deviation: GPUI splits bordered quads into a background quad and border
|
||||||
|
// strips (`insert_quad`) as a renderer optimization; here a quad is inserted
|
||||||
|
// as is.
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// A quad to paint, in logical pixels (GPUI: `PaintQuad`).
|
||||||
|
#derive(Eq, Debug)
|
||||||
|
pub(all) struct PaintQuad {
|
||||||
|
bounds : @geometry.Bounds[@geometry.Pixels]
|
||||||
|
corner_radii : @geometry.Corners[@geometry.Pixels]
|
||||||
|
background : @color.Background
|
||||||
|
border_widths : @geometry.Edges[@geometry.Pixels]
|
||||||
|
border_color : @color.Hsla
|
||||||
|
border_style : @scene.BorderStyle
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// A filled quad without border (GPUI: `fill`).
|
||||||
|
pub fn fill(
|
||||||
|
bounds : @geometry.Bounds[@geometry.Pixels],
|
||||||
|
background : @color.Background,
|
||||||
|
) -> PaintQuad {
|
||||||
|
{
|
||||||
|
bounds,
|
||||||
|
corner_radii: Default::default(),
|
||||||
|
background,
|
||||||
|
border_widths: Default::default(),
|
||||||
|
border_color: @color.transparent_black(),
|
||||||
|
border_style: Solid,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// A 1px outline (GPUI: `outline`).
|
||||||
|
pub fn outline(
|
||||||
|
bounds : @geometry.Bounds[@geometry.Pixels],
|
||||||
|
border_color : @color.Hsla,
|
||||||
|
border_style~ : @scene.BorderStyle = Solid,
|
||||||
|
) -> PaintQuad {
|
||||||
|
{
|
||||||
|
bounds,
|
||||||
|
corner_radii: Default::default(),
|
||||||
|
background: @color.transparent_black().to_background(),
|
||||||
|
border_widths: @geometry.Edges::all(@geometry.px(1.0)),
|
||||||
|
border_color,
|
||||||
|
border_style,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
fn round_half_toward_zero(value : Double) -> Double {
|
||||||
|
@layout.round_half_toward_zero(value)
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// Logical bounds to device pixels, edges rounded (GPUI: `snap_bounds`).
|
||||||
|
fn Window::snap_bounds(
|
||||||
|
self : Window,
|
||||||
|
bounds : @geometry.Bounds[@geometry.Pixels],
|
||||||
|
) -> @geometry.Bounds[@geometry.ScaledPixels] {
|
||||||
|
let s = self.scale_factor
|
||||||
|
let left = round_half_toward_zero(bounds.left().to_double() * s)
|
||||||
|
let top = round_half_toward_zero(bounds.top().to_double() * s)
|
||||||
|
let right = @cmp.maximum(
|
||||||
|
round_half_toward_zero(bounds.right().to_double() * s),
|
||||||
|
left,
|
||||||
|
)
|
||||||
|
let bottom = @cmp.maximum(
|
||||||
|
round_half_toward_zero(bounds.bottom().to_double() * s),
|
||||||
|
top,
|
||||||
|
)
|
||||||
|
@geometry.Bounds::from_corners(
|
||||||
|
@geometry.point(@geometry.ScaledPixels(left), @geometry.ScaledPixels(top)),
|
||||||
|
@geometry.point(
|
||||||
|
@geometry.ScaledPixels(right),
|
||||||
|
@geometry.ScaledPixels(bottom),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// Logical bounds to the device pixels they touch (GPUI: `cover_bounds`).
|
||||||
|
fn Window::cover_bounds(
|
||||||
|
self : Window,
|
||||||
|
bounds : @geometry.Bounds[@geometry.Pixels],
|
||||||
|
) -> @geometry.Bounds[@geometry.ScaledPixels] {
|
||||||
|
let s = self.scale_factor
|
||||||
|
let left = (bounds.left().to_double() * s).floor()
|
||||||
|
let top = (bounds.top().to_double() * s).floor()
|
||||||
|
if bounds.is_empty() {
|
||||||
|
return {
|
||||||
|
origin: @geometry.point(
|
||||||
|
@geometry.ScaledPixels(left),
|
||||||
|
@geometry.ScaledPixels(top),
|
||||||
|
),
|
||||||
|
size: Default::default(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let right = @cmp.maximum((bounds.right().to_double() * s).ceil(), left)
|
||||||
|
let bottom = @cmp.maximum((bounds.bottom().to_double() * s).ceil(), top)
|
||||||
|
@geometry.Bounds::from_corners(
|
||||||
|
@geometry.point(@geometry.ScaledPixels(left), @geometry.ScaledPixels(top)),
|
||||||
|
@geometry.point(
|
||||||
|
@geometry.ScaledPixels(right),
|
||||||
|
@geometry.ScaledPixels(bottom),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
fn Window::snapped_content_mask(
|
||||||
|
self : Window,
|
||||||
|
) -> @geometry.ContentMask[@geometry.ScaledPixels] {
|
||||||
|
{ bounds: self.cover_bounds(self.content_mask().bounds), }
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// Paints a quad, snapped to device pixels and faded by the element opacity
|
||||||
|
/// (GPUI: `paint_quad`). Only during paint.
|
||||||
|
pub fn Window::paint_quad(self : Window, quad : PaintQuad) -> Unit {
|
||||||
|
self.assert_phase("paint_quad", [Paint])
|
||||||
|
let opacity = self.element_opacity
|
||||||
|
let scene_quad : @scene.Quad = {
|
||||||
|
order: 0,
|
||||||
|
bounds: self.snap_bounds(quad.bounds),
|
||||||
|
content_mask: self.snapped_content_mask(),
|
||||||
|
background: quad.background.opacity(opacity),
|
||||||
|
border_color: quad.border_color.opacity(opacity),
|
||||||
|
corner_radii: quad.corner_radii.scale(self.scale_factor),
|
||||||
|
border_widths: quad.border_widths.map(fn(width) {
|
||||||
|
@geometry.ScaledPixels(
|
||||||
|
@layout.round_stroke_to_device_pixel(
|
||||||
|
width.to_double(),
|
||||||
|
self.scale_factor,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
}),
|
||||||
|
border_style: quad.border_style,
|
||||||
|
}
|
||||||
|
let has_background = !scene_quad.background.is_transparent()
|
||||||
|
let has_border = scene_quad.border_widths != Default::default() &&
|
||||||
|
!scene_quad.border_color.is_transparent()
|
||||||
|
if has_background || has_border {
|
||||||
|
self.next_scene.insert_primitive(Quad(scene_quad))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// Paints the outer (non-inset) box shadows of a rounded rectangle (GPUI:
|
||||||
|
/// `paint_drop_shadows`).
|
||||||
|
pub fn Window::paint_drop_shadows(
|
||||||
|
self : Window,
|
||||||
|
bounds : @geometry.Bounds[@geometry.Pixels],
|
||||||
|
corner_radii : @geometry.Corners[@geometry.Pixels],
|
||||||
|
shadows : Array[@style.BoxShadow],
|
||||||
|
) -> Unit {
|
||||||
|
self.assert_phase("paint_drop_shadows", [Paint])
|
||||||
|
let content_mask = self.snapped_content_mask()
|
||||||
|
for shadow in shadows {
|
||||||
|
if shadow.inset {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
let shape_bounds = self.cover_bounds(
|
||||||
|
bounds.translate(shadow.offset).dilate(shadow.spread_radius),
|
||||||
|
)
|
||||||
|
if shape_bounds.is_empty() {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
let blur_radius = shadow.blur_radius.scale(self.scale_factor)
|
||||||
|
self.next_scene.insert_primitive(
|
||||||
|
Shadow({
|
||||||
|
order: 0,
|
||||||
|
blur_radius,
|
||||||
|
bounds: shape_bounds.dilate(blur_radius.mul_scalar(3.0)),
|
||||||
|
content_mask,
|
||||||
|
corner_radii: Default::default(),
|
||||||
|
color: shadow.color.opacity(self.element_opacity),
|
||||||
|
shape_bounds,
|
||||||
|
shape_corner_radii: corner_radii.scale(self.scale_factor),
|
||||||
|
inset: false,
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// Paints the inset box shadows of a rounded rectangle (GPUI:
|
||||||
|
/// `paint_inset_shadows`).
|
||||||
|
pub fn Window::paint_inset_shadows(
|
||||||
|
self : Window,
|
||||||
|
bounds : @geometry.Bounds[@geometry.Pixels],
|
||||||
|
corner_radii : @geometry.Corners[@geometry.Pixels],
|
||||||
|
shadows : Array[@style.BoxShadow],
|
||||||
|
) -> Unit {
|
||||||
|
self.assert_phase("paint_inset_shadows", [Paint])
|
||||||
|
let content_mask = self.snapped_content_mask()
|
||||||
|
let element_bounds = self.cover_bounds(bounds)
|
||||||
|
for shadow in shadows {
|
||||||
|
if !shadow.inset {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
let hole = bounds.translate(shadow.offset).dilate(-shadow.spread_radius)
|
||||||
|
let shrink = fn(radius : @geometry.Pixels) {
|
||||||
|
@cmp.maximum(radius - shadow.spread_radius, @geometry.px(0.0))
|
||||||
|
}
|
||||||
|
self.next_scene.insert_primitive(
|
||||||
|
Shadow({
|
||||||
|
order: 0,
|
||||||
|
blur_radius: shadow.blur_radius.scale(self.scale_factor),
|
||||||
|
bounds: element_bounds,
|
||||||
|
content_mask,
|
||||||
|
corner_radii: corner_radii.scale(self.scale_factor),
|
||||||
|
color: shadow.color.opacity(self.element_opacity),
|
||||||
|
shape_bounds: self.cover_bounds(hole),
|
||||||
|
shape_corner_radii: corner_radii.map(shrink).scale(self.scale_factor),
|
||||||
|
inset: true,
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// Paints an element's box (drop shadows, background, inset shadows), runs
|
||||||
|
/// `paint_children`, then paints the border on top (GPUI: `Style::paint`).
|
||||||
|
pub fn Window::paint_style(
|
||||||
|
self : Window,
|
||||||
|
style : @style.Style,
|
||||||
|
bounds : @geometry.Bounds[@geometry.Pixels],
|
||||||
|
paint_children : () -> Unit,
|
||||||
|
) -> Unit {
|
||||||
|
self.assert_phase("paint_style", [Paint])
|
||||||
|
if style.debug {
|
||||||
|
self.paint_quad(outline(bounds, @color.red()))
|
||||||
|
}
|
||||||
|
let corner_radii = style.corner_radii
|
||||||
|
.absolute_to_pixels(self.rem_size)
|
||||||
|
.clamp_radii_for_quad_size(bounds.size)
|
||||||
|
self.paint_drop_shadows(bounds, corner_radii, style.box_shadow)
|
||||||
|
if style.background is Some(fill) &&
|
||||||
|
fill.color() is Some(background) &&
|
||||||
|
!background.is_transparent() {
|
||||||
|
let border_color = match background.tag {
|
||||||
|
LinearGradient => background.colors.0.color
|
||||||
|
_ => background.solid
|
||||||
|
}
|
||||||
|
self.paint_quad({
|
||||||
|
bounds,
|
||||||
|
corner_radii,
|
||||||
|
background,
|
||||||
|
border_widths: Default::default(),
|
||||||
|
border_color: border_color.alpha(0.0),
|
||||||
|
border_style: style.border_style,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
self.paint_inset_shadows(bounds, corner_radii, style.box_shadow)
|
||||||
|
paint_children()
|
||||||
|
if style.is_border_visible() {
|
||||||
|
let border_color = style.border_color.unwrap()
|
||||||
|
self.paint_quad({
|
||||||
|
bounds,
|
||||||
|
corner_radii,
|
||||||
|
background: border_color.alpha(0.0).to_background(),
|
||||||
|
border_widths: style.border_widths.absolute_to_pixels(self.rem_size),
|
||||||
|
border_color,
|
||||||
|
border_style: style.border_style,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// Paints an arbitrary primitive (escape hatch for custom elements such as
|
||||||
|
/// `canvas`). Only during paint.
|
||||||
|
pub fn Window::paint_primitive(
|
||||||
|
self : Window,
|
||||||
|
primitive : @scene.Primitive,
|
||||||
|
) -> Unit {
|
||||||
|
self.assert_phase("paint_primitive", [Paint])
|
||||||
|
self.next_scene.insert_primitive(primitive)
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// Paints a filled path given by polygon contours in logical pixels (GPUI:
|
||||||
|
/// `paint_path`, simplified to polygons).
|
||||||
|
pub fn Window::paint_path(
|
||||||
|
self : Window,
|
||||||
|
contours : Array[Array[@geometry.Point[@geometry.Pixels]]],
|
||||||
|
color : @color.Background,
|
||||||
|
) -> Unit {
|
||||||
|
self.assert_phase("paint_path", [Paint])
|
||||||
|
let path = @scene.PaintedPath::new(
|
||||||
|
contours,
|
||||||
|
self.scale_factor,
|
||||||
|
self.content_mask(),
|
||||||
|
color.opacity(self.element_opacity),
|
||||||
|
)
|
||||||
|
self.next_scene.insert_primitive(Path(path))
|
||||||
|
}
|
||||||
@@ -0,0 +1,134 @@
|
|||||||
|
// Generated using `moon info`, DON'T EDIT IT
|
||||||
|
package "experiments/gpui/window"
|
||||||
|
|
||||||
|
import {
|
||||||
|
"experiments/gpui/app",
|
||||||
|
"experiments/gpui/color",
|
||||||
|
"experiments/gpui/geometry",
|
||||||
|
"experiments/gpui/input",
|
||||||
|
"experiments/gpui/layout",
|
||||||
|
"experiments/gpui/scene",
|
||||||
|
"experiments/gpui/style",
|
||||||
|
"moonbitlang/core/debug",
|
||||||
|
}
|
||||||
|
|
||||||
|
// Values
|
||||||
|
pub fn empty() -> AnyElement
|
||||||
|
|
||||||
|
pub fn fill(@geometry.Bounds[@geometry.Pixels], @color.Background) -> PaintQuad
|
||||||
|
|
||||||
|
pub fn[T, E] listener(@app.Context[T], (T, E, Window, @app.Context[T]) -> Unit) -> (E, Window, @app.App) -> Unit
|
||||||
|
|
||||||
|
pub fn outline(@geometry.Bounds[@geometry.Pixels], @color.Hsla, border_style? : @scene.BorderStyle) -> PaintQuad
|
||||||
|
|
||||||
|
// Errors
|
||||||
|
|
||||||
|
// Types and methods
|
||||||
|
pub struct AnyElement {
|
||||||
|
// private fields
|
||||||
|
}
|
||||||
|
pub fn AnyElement::bounds(Self) -> @geometry.Bounds[@geometry.Pixels]?
|
||||||
|
pub fn AnyElement::new(&Element) -> Self
|
||||||
|
pub fn AnyElement::paint(Self, Window, @app.App) -> Unit
|
||||||
|
pub fn AnyElement::prepaint(Self, Window, @app.App) -> Unit
|
||||||
|
pub fn AnyElement::request_layout(Self, Window, @app.App) -> @layout.LayoutId
|
||||||
|
pub impl IntoElement for AnyElement
|
||||||
|
|
||||||
|
pub struct AnyView {
|
||||||
|
entity_id : @app.EntityId
|
||||||
|
// private fields
|
||||||
|
}
|
||||||
|
pub fn[V : Render] AnyView::from_entity(@app.Entity[V]) -> Self
|
||||||
|
pub fn AnyView::into_any_element(Self) -> AnyElement
|
||||||
|
pub impl IntoElement for AnyView
|
||||||
|
|
||||||
|
pub struct ElementStateKey[S] {
|
||||||
|
id : Int32
|
||||||
|
name : String
|
||||||
|
// private fields
|
||||||
|
}
|
||||||
|
pub fn[S] ElementStateKey::new(String) -> Self[S]
|
||||||
|
|
||||||
|
#derive(Eq, @debug.Debug)
|
||||||
|
pub(all) struct Hitbox {
|
||||||
|
id : Int32
|
||||||
|
bounds : @geometry.Bounds[@geometry.Pixels]
|
||||||
|
content_mask : @geometry.ContentMask[@geometry.Pixels]
|
||||||
|
behavior : HitboxBehavior
|
||||||
|
}
|
||||||
|
pub fn Hitbox::is_hovered(Self, Window) -> Bool
|
||||||
|
|
||||||
|
#derive(Eq, Hash, @debug.Debug)
|
||||||
|
pub(all) enum HitboxBehavior {
|
||||||
|
Normal
|
||||||
|
BlockMouse
|
||||||
|
}
|
||||||
|
|
||||||
|
#derive(Eq, @debug.Debug)
|
||||||
|
pub(all) struct PaintQuad {
|
||||||
|
bounds : @geometry.Bounds[@geometry.Pixels]
|
||||||
|
corner_radii : @geometry.Corners[@geometry.Pixels]
|
||||||
|
background : @color.Background
|
||||||
|
border_widths : @geometry.Edges[@geometry.Pixels]
|
||||||
|
border_color : @color.Hsla
|
||||||
|
border_style : @scene.BorderStyle
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Window {
|
||||||
|
// private fields
|
||||||
|
}
|
||||||
|
pub fn Window::close(Self) -> Unit
|
||||||
|
pub fn Window::content_mask(Self) -> @geometry.ContentMask[@geometry.Pixels]
|
||||||
|
pub fn Window::dispatch_mouse_event(Self, @input.MouseEvent) -> Bool
|
||||||
|
pub fn Window::draw(Self) -> @scene.Scene
|
||||||
|
pub fn Window::draw_if_dirty(Self) -> Bool
|
||||||
|
pub fn Window::element_offset(Self) -> @geometry.Point[@geometry.Pixels]
|
||||||
|
pub fn Window::element_opacity(Self) -> Double
|
||||||
|
pub fn Window::frame_count(Self) -> Int32
|
||||||
|
pub fn Window::global_element_id(Self) -> String?
|
||||||
|
pub fn Window::insert_hitbox(Self, @geometry.Bounds[@geometry.Pixels], HitboxBehavior) -> Hitbox
|
||||||
|
pub fn Window::is_dirty(Self) -> Bool
|
||||||
|
pub fn Window::layout_bounds(Self, @layout.LayoutId) -> @geometry.Bounds[@geometry.Pixels]
|
||||||
|
pub fn Window::mouse_position(Self) -> @geometry.Point[@geometry.Pixels]
|
||||||
|
pub fn Window::new(@app.App, @geometry.Size[@geometry.Pixels], scale_factor? : Double, rem_size? : @geometry.Pixels) -> Self
|
||||||
|
pub fn Window::on_mouse_event(Self, (@input.MouseEvent, @input.DispatchPhase, Self, @app.App) -> Unit) -> Unit
|
||||||
|
pub fn Window::paint_drop_shadows(Self, @geometry.Bounds[@geometry.Pixels], @geometry.Corners[@geometry.Pixels], Array[@style.BoxShadow]) -> Unit
|
||||||
|
pub fn Window::paint_inset_shadows(Self, @geometry.Bounds[@geometry.Pixels], @geometry.Corners[@geometry.Pixels], Array[@style.BoxShadow]) -> Unit
|
||||||
|
pub fn Window::paint_path(Self, Array[Array[@geometry.Point[@geometry.Pixels]]], @color.Background) -> Unit
|
||||||
|
pub fn Window::paint_primitive(Self, @scene.Primitive) -> Unit
|
||||||
|
pub fn Window::paint_quad(Self, PaintQuad) -> Unit
|
||||||
|
pub fn Window::paint_style(Self, @style.Style, @geometry.Bounds[@geometry.Pixels], () -> Unit) -> Unit
|
||||||
|
pub fn Window::refresh(Self) -> Unit
|
||||||
|
pub fn Window::rem_size(Self) -> @geometry.Pixels
|
||||||
|
pub fn Window::request_layout(Self, @style.Style, Array[@layout.LayoutId]) -> @layout.LayoutId
|
||||||
|
pub fn Window::request_measured_layout(Self, @style.Style, (@geometry.Size[@geometry.Pixels?], @geometry.Size[@layout.AvailableSpace]) -> @geometry.Size[@geometry.Pixels]) -> @layout.LayoutId
|
||||||
|
pub fn Window::resize(Self, @geometry.Size[@geometry.Pixels], scale_factor? : Double) -> Unit
|
||||||
|
pub fn Window::scale_factor(Self) -> Double
|
||||||
|
pub fn Window::scene(Self) -> @scene.Scene
|
||||||
|
pub fn Window::set_rem_size(Self, @geometry.Pixels) -> Unit
|
||||||
|
pub fn[V : Render] Window::set_root_view(Self, @app.Entity[V]) -> Unit
|
||||||
|
pub fn Window::stop_propagation(Self) -> Unit
|
||||||
|
pub fn Window::viewport_size(Self) -> @geometry.Size[@geometry.Pixels]
|
||||||
|
pub fn[R] Window::with_content_mask(Self, @geometry.ContentMask[@geometry.Pixels]?, () -> R) -> R
|
||||||
|
pub fn[R] Window::with_element_id(Self, String?, () -> R) -> R
|
||||||
|
pub fn[R] Window::with_element_offset(Self, @geometry.Point[@geometry.Pixels], () -> R) -> R
|
||||||
|
pub fn[R] Window::with_element_opacity(Self, Double?, () -> R) -> R
|
||||||
|
pub fn[S, R] Window::with_element_state(Self, ElementStateKey[S], (S?) -> (R, S)) -> R
|
||||||
|
|
||||||
|
// Type aliases
|
||||||
|
|
||||||
|
// Traits
|
||||||
|
pub(open) trait Element {
|
||||||
|
fn request_layout(Self, Window, @app.App) -> @layout.LayoutId
|
||||||
|
fn prepaint(Self, @geometry.Bounds[@geometry.Pixels], Window, @app.App) -> Unit
|
||||||
|
fn paint(Self, @geometry.Bounds[@geometry.Pixels], Window, @app.App) -> Unit
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(open) trait IntoElement {
|
||||||
|
fn into_any_element(Self) -> AnyElement
|
||||||
|
}
|
||||||
|
pub impl[V : Render] IntoElement for @app.Entity[V]
|
||||||
|
|
||||||
|
pub(open) trait Render {
|
||||||
|
fn render(Self, Window, @app.Context[Self]) -> AnyElement
|
||||||
|
}
|
||||||
@@ -0,0 +1,632 @@
|
|||||||
|
// The window, ported from gpui/src/window.rs: the frame lifecycle (layout,
|
||||||
|
// prepaint, paint), the content-mask/offset/opacity stacks, hitboxes and
|
||||||
|
// hit-testing, element state, mouse dispatch, and view invalidation.
|
||||||
|
//
|
||||||
|
// Not ported yet (see docs/PORTING.md): the platform window (M7/M8), focus,
|
||||||
|
// the key dispatch tree and actions (M5/M10), view caching and
|
||||||
|
// `Scene::replay`-based reuse, deferred draws, tooltips, drag and drop, and
|
||||||
|
// text (M9). A window is headless: `draw` returns the frame's `Scene`.
|
||||||
|
|
||||||
|
///|
|
||||||
|
#derive(Eq, Debug)
|
||||||
|
priv enum DrawPhase {
|
||||||
|
Idle
|
||||||
|
Layout
|
||||||
|
Prepaint
|
||||||
|
Paint
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// How a hitbox affects the hitboxes painted below it (GPUI:
|
||||||
|
/// `HitboxBehavior`).
|
||||||
|
#derive(Eq, Hash, Debug)
|
||||||
|
pub(all) enum HitboxBehavior {
|
||||||
|
/// Hitboxes below are also hovered.
|
||||||
|
Normal
|
||||||
|
/// Hitboxes below are not hovered (GPUI: `BlockMouse`, used by `occlude`).
|
||||||
|
BlockMouse
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// A region that receives the mouse, inserted during prepaint (GPUI:
|
||||||
|
/// `Hitbox`).
|
||||||
|
#derive(Eq, Debug)
|
||||||
|
pub(all) struct Hitbox {
|
||||||
|
id : Int32
|
||||||
|
bounds : @geometry.Bounds[@geometry.Pixels]
|
||||||
|
content_mask : @geometry.ContentMask[@geometry.Pixels]
|
||||||
|
behavior : HitboxBehavior
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// Whether the mouse is over this hitbox (as of the last mouse event, hit
|
||||||
|
/// tested against the last rendered frame).
|
||||||
|
pub fn Hitbox::is_hovered(self : Hitbox, window : Window) -> Bool {
|
||||||
|
window.hovered_hitboxes.contains(self.id)
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// Identifies one kind of per-element state kept across frames (GPUI keys
|
||||||
|
/// element state by `TypeId`).
|
||||||
|
pub struct ElementStateKey[S] {
|
||||||
|
id : Int32
|
||||||
|
name : String
|
||||||
|
priv slot : Ref[S?]
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
let next_state_key_id : Ref[Int32] = { val: 0, }
|
||||||
|
|
||||||
|
///|
|
||||||
|
pub fn[S] ElementStateKey::new(name : String) -> ElementStateKey[S] {
|
||||||
|
let id = next_state_key_id.val
|
||||||
|
next_state_key_id.val = id + 1
|
||||||
|
{ id, name, slot: { val: None, }, }
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
priv struct MouseListener {
|
||||||
|
callback : (@input.MouseEvent, @input.DispatchPhase, Window, @app.App) -> Unit
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// A headless window (GPUI: `Window`). `draw` lays out and paints the root
|
||||||
|
/// view into a `Scene`; `dispatch_mouse_event` delivers input.
|
||||||
|
pub struct Window {
|
||||||
|
priv app : @app.App
|
||||||
|
priv mut viewport_size : @geometry.Size[@geometry.Pixels]
|
||||||
|
priv mut scale_factor : Double
|
||||||
|
priv mut rem_size : @geometry.Pixels
|
||||||
|
priv layout_engine : @layout.LayoutEngine
|
||||||
|
priv mut root : AnyView?
|
||||||
|
priv mut phase : DrawPhase
|
||||||
|
priv mut dirty : Bool
|
||||||
|
priv mut drawn_frames : Int32
|
||||||
|
priv content_mask_stack : Array[@geometry.ContentMask[@geometry.Pixels]]
|
||||||
|
priv element_offset_stack : Array[@geometry.Point[@geometry.Pixels]]
|
||||||
|
priv mut element_opacity : Double
|
||||||
|
priv element_id_stack : Array[String]
|
||||||
|
priv mut next_scene : @scene.Scene
|
||||||
|
priv mut rendered_scene : @scene.Scene
|
||||||
|
priv mut next_hitboxes : Array[Hitbox]
|
||||||
|
priv mut rendered_hitboxes : Array[Hitbox]
|
||||||
|
priv mut next_mouse_listeners : Array[MouseListener]
|
||||||
|
priv mut rendered_mouse_listeners : Array[MouseListener]
|
||||||
|
priv mut next_element_states : Map[String, () -> Unit]
|
||||||
|
priv mut rendered_element_states : Map[String, () -> Unit]
|
||||||
|
priv mut next_rendered_views : @hashset.HashSet[@app.EntityId]
|
||||||
|
priv mut rendered_views : @hashset.HashSet[@app.EntityId]
|
||||||
|
priv mut mouse_position : @geometry.Point[@geometry.Pixels]
|
||||||
|
priv mut hovered_hitboxes : Array[Int32]
|
||||||
|
priv mut propagate_event : Bool
|
||||||
|
priv mut notify_subscription : @app.Subscription?
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// A window of `viewport_size` logical pixels showing nothing yet. Set its
|
||||||
|
/// content with `set_root_view`.
|
||||||
|
pub fn Window::new(
|
||||||
|
app : @app.App,
|
||||||
|
viewport_size : @geometry.Size[@geometry.Pixels],
|
||||||
|
scale_factor~ : Double = 1.0,
|
||||||
|
rem_size~ : @geometry.Pixels = @geometry.px(16.0),
|
||||||
|
) -> Window {
|
||||||
|
let window : Window = {
|
||||||
|
app,
|
||||||
|
viewport_size,
|
||||||
|
scale_factor,
|
||||||
|
rem_size,
|
||||||
|
layout_engine: @layout.LayoutEngine::new(),
|
||||||
|
root: None,
|
||||||
|
phase: Idle,
|
||||||
|
dirty: true,
|
||||||
|
drawn_frames: 0,
|
||||||
|
content_mask_stack: [],
|
||||||
|
element_offset_stack: [],
|
||||||
|
element_opacity: 1.0,
|
||||||
|
element_id_stack: [],
|
||||||
|
next_scene: @scene.Scene::new(),
|
||||||
|
rendered_scene: @scene.Scene::new(),
|
||||||
|
next_hitboxes: [],
|
||||||
|
rendered_hitboxes: [],
|
||||||
|
next_mouse_listeners: [],
|
||||||
|
rendered_mouse_listeners: [],
|
||||||
|
next_element_states: {},
|
||||||
|
rendered_element_states: {},
|
||||||
|
next_rendered_views: @hashset.HashSet([]),
|
||||||
|
rendered_views: @hashset.HashSet([]),
|
||||||
|
mouse_position: @geometry.point(@geometry.px(0.0), @geometry.px(0.0)),
|
||||||
|
hovered_hitboxes: [],
|
||||||
|
propagate_event: true,
|
||||||
|
notify_subscription: None,
|
||||||
|
}
|
||||||
|
// Invalidate when a view rendered in the last frame is notified (GPUI:
|
||||||
|
// `WindowInvalidator::invalidate_view`).
|
||||||
|
window.notify_subscription = Some(
|
||||||
|
app.observe_notifications(fn(entity_id) {
|
||||||
|
if window.rendered_views.contains(entity_id) {
|
||||||
|
window.dirty = true
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
window
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// Sets the view the window renders (GPUI: the root view of
|
||||||
|
/// `open_window`).
|
||||||
|
pub fn[V : Render] Window::set_root_view(
|
||||||
|
self : Window,
|
||||||
|
view : @app.Entity[V],
|
||||||
|
) -> Unit {
|
||||||
|
self.root = Some(AnyView::from_entity(view))
|
||||||
|
self.dirty = true
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// Stops listening to the app. Call when the window goes away.
|
||||||
|
pub fn Window::close(self : Window) -> Unit {
|
||||||
|
if self.notify_subscription is Some(subscription) {
|
||||||
|
subscription.unsubscribe()
|
||||||
|
}
|
||||||
|
self.notify_subscription = None
|
||||||
|
self.root = None
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
pub fn Window::viewport_size(self : Window) -> @geometry.Size[@geometry.Pixels] {
|
||||||
|
self.viewport_size
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
pub fn Window::scale_factor(self : Window) -> Double {
|
||||||
|
self.scale_factor
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
pub fn Window::rem_size(self : Window) -> @geometry.Pixels {
|
||||||
|
self.rem_size
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
pub fn Window::mouse_position(
|
||||||
|
self : Window,
|
||||||
|
) -> @geometry.Point[@geometry.Pixels] {
|
||||||
|
self.mouse_position
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// Resizes the viewport (and optionally changes the scale factor); the next
|
||||||
|
/// frame is laid out again.
|
||||||
|
pub fn Window::resize(
|
||||||
|
self : Window,
|
||||||
|
viewport_size : @geometry.Size[@geometry.Pixels],
|
||||||
|
scale_factor? : Double,
|
||||||
|
) -> Unit {
|
||||||
|
self.viewport_size = viewport_size
|
||||||
|
if scale_factor is Some(factor) {
|
||||||
|
self.scale_factor = factor
|
||||||
|
}
|
||||||
|
self.dirty = true
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
pub fn Window::set_rem_size(self : Window, rem_size : @geometry.Pixels) -> Unit {
|
||||||
|
self.rem_size = rem_size
|
||||||
|
self.dirty = true
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// Marks the window for redraw (GPUI: `refresh`).
|
||||||
|
pub fn Window::refresh(self : Window) -> Unit {
|
||||||
|
self.dirty = true
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// Whether something changed since the last frame: a rendered view was
|
||||||
|
/// notified, the hovered hitboxes changed, or the window was resized.
|
||||||
|
pub fn Window::is_dirty(self : Window) -> Bool {
|
||||||
|
self.dirty
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// Number of frames drawn so far.
|
||||||
|
pub fn Window::frame_count(self : Window) -> Int32 {
|
||||||
|
self.drawn_frames
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// The scene of the last drawn frame.
|
||||||
|
pub fn Window::scene(self : Window) -> @scene.Scene {
|
||||||
|
self.rendered_scene
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Frame lifecycle
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
///|
|
||||||
|
fn Window::assert_phase(
|
||||||
|
self : Window,
|
||||||
|
operation : String,
|
||||||
|
allowed : Array[DrawPhase],
|
||||||
|
) -> Unit {
|
||||||
|
guard allowed.contains(self.phase) else {
|
||||||
|
abort(
|
||||||
|
"\{operation} is not allowed in the \{@debug.to_string(self.phase)} phase",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// Draws a frame: renders the root view, lays it out to fill the viewport,
|
||||||
|
/// prepaints and paints it. Returns the frame's scene. Effects queued while
|
||||||
|
/// drawing flush afterwards (GPUI: `Window::draw`).
|
||||||
|
pub fn Window::draw(self : Window) -> @scene.Scene {
|
||||||
|
guard self.phase == Idle else { abort("Window::draw called while drawing") }
|
||||||
|
guard self.root is Some(root) else {
|
||||||
|
abort("Window::draw: no root view (call set_root_view first)")
|
||||||
|
}
|
||||||
|
self.app.update(fn(app) {
|
||||||
|
self.layout_engine.clear()
|
||||||
|
self.next_scene = @scene.Scene::new()
|
||||||
|
self.next_hitboxes = []
|
||||||
|
self.next_mouse_listeners = []
|
||||||
|
self.next_element_states = {}
|
||||||
|
self.next_rendered_views = @hashset.HashSet([])
|
||||||
|
self.content_mask_stack.clear()
|
||||||
|
self.element_offset_stack.clear()
|
||||||
|
self.element_id_stack.clear()
|
||||||
|
self.element_opacity = 1.0
|
||||||
|
let element = root.into_any_element()
|
||||||
|
self.phase = Layout
|
||||||
|
let layout_id = element.request_layout(self, app)
|
||||||
|
self.layout_engine.stretch_auto_size_to_fill(
|
||||||
|
layout_id,
|
||||||
|
self.viewport_size,
|
||||||
|
self.scale_factor,
|
||||||
|
)
|
||||||
|
self.layout_engine.compute_layout(
|
||||||
|
layout_id,
|
||||||
|
@layout.AvailableSpace::from_size(self.viewport_size),
|
||||||
|
self.scale_factor,
|
||||||
|
)
|
||||||
|
self.phase = Prepaint
|
||||||
|
element.prepaint(self, app)
|
||||||
|
self.phase = Paint
|
||||||
|
element.paint(self, app)
|
||||||
|
self.phase = Idle
|
||||||
|
self.next_scene.finish()
|
||||||
|
// Swap frames.
|
||||||
|
self.rendered_scene = self.next_scene
|
||||||
|
self.rendered_hitboxes = self.next_hitboxes
|
||||||
|
self.rendered_mouse_listeners = self.next_mouse_listeners
|
||||||
|
self.rendered_element_states = self.next_element_states
|
||||||
|
self.rendered_views = self.next_rendered_views
|
||||||
|
self.drawn_frames = self.drawn_frames + 1
|
||||||
|
self.dirty = false
|
||||||
|
// Hover state may have changed with the new layout.
|
||||||
|
self.hovered_hitboxes = self.hit_test(self.mouse_position)
|
||||||
|
})
|
||||||
|
self.rendered_scene
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// Draws a frame only if the window is dirty; returns whether it drew.
|
||||||
|
pub fn Window::draw_if_dirty(self : Window) -> Bool {
|
||||||
|
if self.dirty {
|
||||||
|
ignore(self.draw())
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
fn Window::record_rendered_view(
|
||||||
|
self : Window,
|
||||||
|
entity_id : @app.EntityId,
|
||||||
|
) -> Unit {
|
||||||
|
self.next_rendered_views.add(entity_id)
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Layout
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// Requests a layout node for `style` with `children` (GPUI:
|
||||||
|
/// `request_layout`). Only during the layout phase.
|
||||||
|
pub fn Window::request_layout(
|
||||||
|
self : Window,
|
||||||
|
style : @style.Style,
|
||||||
|
children : Array[@layout.LayoutId],
|
||||||
|
) -> @layout.LayoutId {
|
||||||
|
self.assert_phase("request_layout", [Layout])
|
||||||
|
self.layout_engine.request_layout(
|
||||||
|
style,
|
||||||
|
self.rem_size,
|
||||||
|
self.scale_factor,
|
||||||
|
children,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// Requests a leaf measured by `measure` (GPUI: `request_measured_layout`).
|
||||||
|
pub fn Window::request_measured_layout(
|
||||||
|
self : Window,
|
||||||
|
style : @style.Style,
|
||||||
|
measure : @layout.MeasureFn,
|
||||||
|
) -> @layout.LayoutId {
|
||||||
|
self.assert_phase("request_measured_layout", [Layout])
|
||||||
|
self.layout_engine.request_measured_layout(
|
||||||
|
style,
|
||||||
|
self.rem_size,
|
||||||
|
self.scale_factor,
|
||||||
|
measure,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// The bounds of a laid-out node, offset by the current element offset
|
||||||
|
/// (GPUI: `layout_bounds`).
|
||||||
|
pub fn Window::layout_bounds(
|
||||||
|
self : Window,
|
||||||
|
layout_id : @layout.LayoutId,
|
||||||
|
) -> @geometry.Bounds[@geometry.Pixels] {
|
||||||
|
self.assert_phase("layout_bounds", [Prepaint, Paint])
|
||||||
|
self.layout_engine
|
||||||
|
.layout_bounds(layout_id, self.scale_factor)
|
||||||
|
.translate(self.element_offset())
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Stacks: content mask, element offset, opacity, element ids
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// The current clip region (the viewport if nothing is pushed).
|
||||||
|
pub fn Window::content_mask(
|
||||||
|
self : Window,
|
||||||
|
) -> @geometry.ContentMask[@geometry.Pixels] {
|
||||||
|
match self.content_mask_stack.last() {
|
||||||
|
Some(mask) => mask
|
||||||
|
None =>
|
||||||
|
{
|
||||||
|
bounds: {
|
||||||
|
origin: @geometry.point(@geometry.px(0.0), @geometry.px(0.0)),
|
||||||
|
size: self.viewport_size,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// Runs `f` with painting clipped to `mask` intersected with the current
|
||||||
|
/// mask (GPUI: `with_content_mask`). `None` leaves the mask unchanged.
|
||||||
|
pub fn[R] Window::with_content_mask(
|
||||||
|
self : Window,
|
||||||
|
mask : @geometry.ContentMask[@geometry.Pixels]?,
|
||||||
|
f : () -> R,
|
||||||
|
) -> R {
|
||||||
|
self.assert_phase("with_content_mask", [Prepaint, Paint])
|
||||||
|
match mask {
|
||||||
|
None => f()
|
||||||
|
Some(mask) => {
|
||||||
|
self.content_mask_stack.push(mask.intersect(self.content_mask()))
|
||||||
|
let result = f()
|
||||||
|
ignore(self.content_mask_stack.pop())
|
||||||
|
result
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
pub fn Window::element_offset(
|
||||||
|
self : Window,
|
||||||
|
) -> @geometry.Point[@geometry.Pixels] {
|
||||||
|
match self.element_offset_stack.last() {
|
||||||
|
Some(offset) => offset
|
||||||
|
None => @geometry.point(@geometry.px(0.0), @geometry.px(0.0))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// Runs `f` with children offset by `offset` (GPUI: `with_element_offset`,
|
||||||
|
/// used for scrolling). Only during prepaint.
|
||||||
|
pub fn[R] Window::with_element_offset(
|
||||||
|
self : Window,
|
||||||
|
offset : @geometry.Point[@geometry.Pixels],
|
||||||
|
f : () -> R,
|
||||||
|
) -> R {
|
||||||
|
self.assert_phase("with_element_offset", [Prepaint])
|
||||||
|
self.element_offset_stack.push(self.element_offset() + offset)
|
||||||
|
let result = f()
|
||||||
|
ignore(self.element_offset_stack.pop())
|
||||||
|
result
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
pub fn Window::element_opacity(self : Window) -> Double {
|
||||||
|
self.element_opacity
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// Runs `f` with painting faded by `opacity` (multiplied with the current
|
||||||
|
/// opacity; GPUI: `with_element_opacity`).
|
||||||
|
pub fn[R] Window::with_element_opacity(
|
||||||
|
self : Window,
|
||||||
|
opacity : Double?,
|
||||||
|
f : () -> R,
|
||||||
|
) -> R {
|
||||||
|
match opacity {
|
||||||
|
None => f()
|
||||||
|
Some(opacity) => {
|
||||||
|
let previous = self.element_opacity
|
||||||
|
self.element_opacity = previous * opacity
|
||||||
|
let result = f()
|
||||||
|
self.element_opacity = previous
|
||||||
|
result
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// Runs `f` with `id` pushed onto the element id path (GPUI:
|
||||||
|
/// `with_element_id`). `None` leaves the path unchanged.
|
||||||
|
pub fn[R] Window::with_element_id(
|
||||||
|
self : Window,
|
||||||
|
id : String?,
|
||||||
|
f : () -> R,
|
||||||
|
) -> R {
|
||||||
|
match id {
|
||||||
|
None => f()
|
||||||
|
Some(id) => {
|
||||||
|
self.element_id_stack.push(id)
|
||||||
|
let result = f()
|
||||||
|
ignore(self.element_id_stack.pop())
|
||||||
|
result
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// The current global element id (the path of pushed ids), if any.
|
||||||
|
pub fn Window::global_element_id(self : Window) -> String? {
|
||||||
|
if self.element_id_stack.is_empty() {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
Some(self.element_id_stack.join("/"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// Gives `f` the state stored under the current element id from the
|
||||||
|
/// previous frame (or `None`) and stores the state it returns for the next
|
||||||
|
/// frame (GPUI: `with_element_state`). Fails loudly without an element id.
|
||||||
|
pub fn[S, R] Window::with_element_state(
|
||||||
|
self : Window,
|
||||||
|
key : ElementStateKey[S],
|
||||||
|
f : (S?) -> (R, S),
|
||||||
|
) -> R {
|
||||||
|
guard self.global_element_id() is Some(global_id) else {
|
||||||
|
abort("element state (\{key.name}) requires an element id (use .id(...))")
|
||||||
|
}
|
||||||
|
let storage_key = "\{global_id}#\{key.id}"
|
||||||
|
let erased = match self.next_element_states.get(storage_key) {
|
||||||
|
Some(erased) => Some(erased)
|
||||||
|
None => self.rendered_element_states.get(storage_key)
|
||||||
|
}
|
||||||
|
let previous = match erased {
|
||||||
|
Some(erased) => {
|
||||||
|
key.slot.val = None
|
||||||
|
erased()
|
||||||
|
let value = key.slot.val
|
||||||
|
key.slot.val = None
|
||||||
|
value
|
||||||
|
}
|
||||||
|
None => None
|
||||||
|
}
|
||||||
|
let (result, state) = f(previous)
|
||||||
|
self.next_element_states[storage_key] = fn() { key.slot.val = Some(state) }
|
||||||
|
result
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// Hitboxes and mouse input
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// Inserts a hitbox for this frame (GPUI: `insert_hitbox`). Only during
|
||||||
|
/// prepaint.
|
||||||
|
pub fn Window::insert_hitbox(
|
||||||
|
self : Window,
|
||||||
|
bounds : @geometry.Bounds[@geometry.Pixels],
|
||||||
|
behavior : HitboxBehavior,
|
||||||
|
) -> Hitbox {
|
||||||
|
self.assert_phase("insert_hitbox", [Prepaint])
|
||||||
|
let hitbox = {
|
||||||
|
id: self.next_hitboxes.length(),
|
||||||
|
bounds,
|
||||||
|
content_mask: self.content_mask(),
|
||||||
|
behavior,
|
||||||
|
}
|
||||||
|
self.next_hitboxes.push(hitbox)
|
||||||
|
hitbox
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// The ids of the hitboxes under `position`, topmost first, stopping below a
|
||||||
|
/// `BlockMouse` hitbox (GPUI: `Frame::hit_test`).
|
||||||
|
fn Window::hit_test(
|
||||||
|
self : Window,
|
||||||
|
position : @geometry.Point[@geometry.Pixels],
|
||||||
|
) -> Array[Int32] {
|
||||||
|
let hits = []
|
||||||
|
for i = self.rendered_hitboxes.length() - 1; i >= 0; i = i - 1 {
|
||||||
|
let hitbox = self.rendered_hitboxes[i]
|
||||||
|
let visible = hitbox.bounds.intersect(hitbox.content_mask.bounds)
|
||||||
|
if visible.contains(position) {
|
||||||
|
hits.push(hitbox.id)
|
||||||
|
if hitbox.behavior == BlockMouse {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
hits
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// Registers a listener for mouse events of the next frame's dispatch
|
||||||
|
/// (GPUI: `on_mouse_event`). Only during paint.
|
||||||
|
pub fn Window::on_mouse_event(
|
||||||
|
self : Window,
|
||||||
|
callback : (@input.MouseEvent, @input.DispatchPhase, Window, @app.App) -> Unit,
|
||||||
|
) -> Unit {
|
||||||
|
self.assert_phase("on_mouse_event", [Paint])
|
||||||
|
self.next_mouse_listeners.push({ callback, })
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// Stops the current event from reaching further listeners (GPUI:
|
||||||
|
/// `stop_propagation`).
|
||||||
|
pub fn Window::stop_propagation(self : Window) -> Unit {
|
||||||
|
self.propagate_event = false
|
||||||
|
}
|
||||||
|
|
||||||
|
///|
|
||||||
|
/// Delivers a mouse event to the listeners of the last frame: first in paint
|
||||||
|
/// order (capture), then in reverse paint order (bubble), until propagation
|
||||||
|
/// stops. Updates hover state; a change of hovered hitboxes marks the window
|
||||||
|
/// dirty. Returns whether the event propagated to the end.
|
||||||
|
pub fn Window::dispatch_mouse_event(
|
||||||
|
self : Window,
|
||||||
|
event : @input.MouseEvent,
|
||||||
|
) -> Bool {
|
||||||
|
guard self.phase == Idle else {
|
||||||
|
abort("dispatch_mouse_event called while drawing")
|
||||||
|
}
|
||||||
|
self.mouse_position = event.position()
|
||||||
|
let hovered = self.hit_test(self.mouse_position)
|
||||||
|
if hovered != self.hovered_hitboxes {
|
||||||
|
self.hovered_hitboxes = hovered
|
||||||
|
self.dirty = true
|
||||||
|
}
|
||||||
|
self.propagate_event = true
|
||||||
|
let listeners = self.rendered_mouse_listeners
|
||||||
|
self.app.update(fn(app) {
|
||||||
|
for listener in listeners {
|
||||||
|
if !self.propagate_event {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
(listener.callback)(event, Capture, self, app)
|
||||||
|
}
|
||||||
|
for i = listeners.length() - 1; i >= 0; i = i - 1 {
|
||||||
|
if !self.propagate_event {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
(listeners[i].callback)(event, Bubble, self, app)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
self.propagate_event
|
||||||
|
}
|
||||||
+18
-6
@@ -16,8 +16,9 @@ Outputs (all in src/style/):
|
|||||||
styled_extend_generated.mbt promotes all `Styled` methods on
|
styled_extend_generated.mbt promotes all `Styled` methods on
|
||||||
StyleRefinement
|
StyleRefinement
|
||||||
|
|
||||||
Other packages that implement `Styled` (e.g. `Div`) need the same `extend`
|
Other packages that implement `Styled` need the same `extend` list. The
|
||||||
list; pass `--extend Type=path/to/file.mbt` to emit it there.
|
default run also emits it for the elements package (`Div`, `Canvas`); add
|
||||||
|
more with `--extend Type=path/to/file.mbt`.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import argparse
|
import argparse
|
||||||
@@ -636,11 +637,11 @@ def gen_styled():
|
|||||||
return "".join(out), names
|
return "".join(out), names
|
||||||
|
|
||||||
|
|
||||||
def gen_extend(type_name, names):
|
def gen_extend(type_name, names, trait_ref="Styled"):
|
||||||
body = ",\n ".join(names)
|
body = ",\n ".join(names)
|
||||||
return (
|
return (
|
||||||
HEADER
|
HEADER
|
||||||
+ f"///|\npub extend {type_name} with Styled::{{\n {body},\n}}\n"
|
+ f"///|\npub extend {type_name} with {trait_ref}::{{\n {body},\n}}\n"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -650,6 +651,13 @@ def write(path, content):
|
|||||||
print(f"wrote {os.path.relpath(path, ROOT)}")
|
print(f"wrote {os.path.relpath(path, ROOT)}")
|
||||||
|
|
||||||
|
|
||||||
|
# Styled types outside the style package, emitted on every run.
|
||||||
|
DEFAULT_EXTENDS = [
|
||||||
|
"Div=src/elements/div_styled_generated.mbt",
|
||||||
|
"Canvas=src/elements/canvas_styled_generated.mbt",
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
|
parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
|
||||||
parser.add_argument(
|
parser.add_argument(
|
||||||
@@ -668,11 +676,15 @@ def main():
|
|||||||
os.path.join(STYLE_DIR, "styled_extend_generated.mbt"),
|
os.path.join(STYLE_DIR, "styled_extend_generated.mbt"),
|
||||||
gen_extend("StyleRefinement", names),
|
gen_extend("StyleRefinement", names),
|
||||||
)
|
)
|
||||||
for spec in args.extend:
|
for spec in DEFAULT_EXTENDS + args.extend:
|
||||||
if "=" not in spec:
|
if "=" not in spec:
|
||||||
sys.exit(f"gen_style: --extend expects TYPE=FILE, got {spec!r}")
|
sys.exit(f"gen_style: --extend expects TYPE=FILE, got {spec!r}")
|
||||||
type_name, path = spec.split("=", 1)
|
type_name, path = spec.split("=", 1)
|
||||||
write(os.path.join(ROOT, path), gen_extend(type_name, names))
|
full = os.path.join(ROOT, path)
|
||||||
|
# Outside the style package the trait must be package-qualified.
|
||||||
|
inside = os.path.dirname(os.path.abspath(full)) == STYLE_DIR
|
||||||
|
trait_ref = "Styled" if inside else "@style.Styled"
|
||||||
|
write(full, gen_extend(type_name, names, trait_ref))
|
||||||
print(f"{len(names)} Styled methods")
|
print(f"{len(names)} Styled methods")
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user