112 lines
3.0 KiB
MoonBit
112 lines
3.0 KiB
MoonBit
// A headless example of what exists so far (M0-M7): a view renders a card
|
|
// with GPUI's `div()` and fluent styles inside a `TestPlatform` window,
|
|
// and `VisualTestContext` simulates a hover and click while pumping frames
|
|
// automatically. There is no renderer or 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
|
|
|
|
///|
|
|
struct Card {
|
|
mut expanded : Bool
|
|
mut clicks : Int32
|
|
}
|
|
|
|
///|
|
|
/// A placeholder for a line of text (the text system arrives in M9).
|
|
fn text_bar(width : Double, color : UInt32) -> @elements.Div {
|
|
@elements.div()
|
|
.w(@geometry.px(width))
|
|
.h(@geometry.px(10.0))
|
|
.rounded_sm()
|
|
.bg(@color.rgb(color))
|
|
}
|
|
|
|
///|
|
|
impl @window.Render for Card with fn render(self, _window, cx) {
|
|
let body_lines = if self.expanded { 4 } else { 1 }
|
|
let card = @elements.div()
|
|
.id("card")
|
|
.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()
|
|
.hover(fn(style) { style.border_color(@color.rgb(0x0969da)) })
|
|
.on_click(
|
|
@window.listener(cx, fn(card, _, _, cx) {
|
|
card.expanded = !card.expanded
|
|
card.clicks += 1
|
|
cx.notify()
|
|
}),
|
|
)
|
|
.child(text_bar(160.0, 0x1f2328))
|
|
for _ in 0..<body_lines {
|
|
ignore(card.child(text_bar(280.0, 0x656d76)))
|
|
}
|
|
@elements.div()
|
|
.size_full()
|
|
.flex()
|
|
.items_start()
|
|
.p(@geometry.px(40.0))
|
|
.bg(@color.rgb(0xf6f8fa))
|
|
.child(card)
|
|
.into_any_element()
|
|
}
|
|
|
|
///|
|
|
fn describe(
|
|
label : String,
|
|
scene : @scene.Scene,
|
|
lines : Array[String],
|
|
) -> Unit {
|
|
lines.push(label)
|
|
let batches = scene.batches().map(fn(b) { b.label() }).join(", ")
|
|
lines.push(" batches: \{batches}")
|
|
for quad in scene.quads {
|
|
let b = quad.bounds
|
|
let extra = if quad.border_widths.top.to_double() > 0.0 {
|
|
" border \{quad.border_color.to_rgb()}"
|
|
} else {
|
|
""
|
|
}
|
|
lines.push(
|
|
" quad (\{b.origin.x.to_double()}, \{b.origin.y.to_double()}) \{b.size.width.to_double()} x \{b.size.height.to_double()}\{extra}",
|
|
)
|
|
}
|
|
}
|
|
|
|
///|
|
|
/// Builds the report printed by `main`.
|
|
fn report() -> String {
|
|
let cx = @test_platform.TestAppContext::new()
|
|
let (card, vcx) = cx.add_window_view(
|
|
size=@geometry.size(@geometry.px(400.0), @geometry.px(240.0)),
|
|
fn(_, _) { { expanded: false, clicks: 0, } },
|
|
)
|
|
let card : @app.Entity[Card] = card
|
|
let lines = []
|
|
describe("first frame", vcx.scene(), 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(vcx.simulate_mouse_move(inside))
|
|
vcx.simulate_click(inside)
|
|
let state = cx.read_entity(card)
|
|
lines.push(
|
|
"clicked: expanded = \{state.expanded}, clicks = \{state.clicks}, frames = \{vcx.draw_count()}, dirty = \{vcx.window().is_dirty()}",
|
|
)
|
|
describe("after click (hovered)", vcx.scene(), lines)
|
|
lines.join("\n")
|
|
}
|
|
|
|
///|
|
|
fn main {
|
|
println(report())
|
|
}
|