M6 elements and window: Element/Render/views, headless Window, div, canvas
- input: mouse events, modifiers, dispatch phases. - window: Element / AnyElement / IntoElement, Render / AnyView (entities render as elements), listener(), empty(); Window with the draw cycle (render, layout to fill the viewport, prepaint, paint, finish) and phase checks; content-mask / element-offset / opacity / element-id stacks; element state across frames (ElementStateKey); hitboxes, hit testing, occlusion; mouse dispatch (capture + bubble, stop_propagation); hover- and notify-driven invalidation of rendered views; painting: paint_quad with device-pixel snapping, drop/inset shadows, paint_path, paint_style (port of Style::paint). - elements: div() (Styled, children, hover, mouse down/up/move/scroll, clicks with element state, occlude, overflow clipping, opacity) and canvas(); Styled extend lists generated by tools/gen_style.py. - app: observe_notifications hook used by windows. - Tests: end-to-end views -> scene, clicks, hover, invalidation, nested views, bubbling, occlusion, shadows/borders/clipping, canvas, resize, and aborts on misuse. The card example now renders a real view and simulates hover and click. - Docs: PORTING.md M6 status and deviations; README. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
eeab51348e
commit
fbe4c081e6
24 files changed
+15532
-204
No files matched your search
@@ -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)
|
||||
}
|
||||
Reference in new issue
Block a user