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,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
Reference in new issue
Block a user