///| struct VisualCounter { mut count : Int32 } ///| impl @window.Render for VisualCounter with fn render(self, _window, cx) { @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() } ///| test "VisualTestContext renders div counter and tracks scene history" { let cx = @test_platform.TestAppContext::new() let (counter, vcx) = cx.add_window_view( size=@geometry.size(px(200.0), px(40.0)), fn(_, _) { { count: 3, } }, ) assert_eq(vcx.draw_count(), 1) assert_eq(vcx.scene().quads.length(), 4) assert_eq(vcx.scene().quads[2].bounds.size.width, sp(30.0)) assert_eq( vcx.scene().quads[3].background, @color.rgb(0x00cc00).to_background(), ) // Moving onto "plus" triggers hover redraw automatically. ignore(vcx.simulate_mouse_move(pt(90.0, 20.0))) assert_eq(vcx.draw_count(), 2) assert_eq( vcx.scene().quads[3].background, @color.rgb(0x00ff00).to_background(), ) // Clicking "plus" updates the view and draws the expanded value bar. vcx.simulate_click(pt(90.0, 20.0)) assert_eq(cx.read_entity(counter).count, 4) assert_eq(vcx.draw_count(), 3) assert_eq(vcx.drawn_scenes().length(), 3) assert_eq(vcx.scene().quads[2].bounds.size.width, sp(40.0)) assert_eq(vcx.scene().quads[3].bounds.origin.x, sp(90.0)) } ///| enum VisualCanvasView { VisualCanvasView } ///| impl @window.Render for VisualCanvasView with fn render(_self, _window, _cx) { @elements.div() .flex() .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_full(), ) .into_any_element() } ///| test "VisualTestContext resize, scale factor, and canvas view" { let cx = @test_platform.TestAppContext::new() let (_, vcx) = cx.add_window_view(size=@geometry.size(px(100.0), px(80.0)), fn( _, _, ) { VisualCanvasView }) assert_eq(vcx.scene().paths.length(), 1) assert_eq(vcx.scene().paths[0].bounds, sbounds(10.0, 10.0, 80.0, 60.0)) vcx.simulate_resize(@geometry.size(px(150.0), px(100.0))) vcx.simulate_scale_factor_change(2.0) assert_eq(vcx.scene().paths[0].bounds, sbounds(20.0, 20.0, 260.0, 160.0)) } ///| struct VisualTicker { mut ticks : Int32 } ///| impl @window.Render for VisualTicker with fn render(self, _window, _cx) { @elements.div() .size_full() .child( @elements.div() .w(px((self.ticks * 20).to_double())) .h(px(10.0)) .bg(@color.red()), ) .into_any_element() } ///| test "VisualTestContext async ticker updates div layout on virtual clock" { let cx = @test_platform.TestAppContext::new() let (ticker, vcx) = cx.add_window_view( size=@geometry.size(px(200.0), px(40.0)), fn(_, ctx) { let weak : @app.WeakEntity[VisualTicker] = ctx.weak_entity() ctx .spawn(async fn(_, async_app) { while true { async_app.background_executor().sleep(100) let alive = weak.update(async_app.app, fn(t : VisualTicker, cx) { t.ticks = t.ticks + 1 cx.notify() }) if alive is None { break } } }) .detach() { ticks: 0, } }, ) assert_eq(vcx.draw_count(), 1) assert_eq(vcx.scene().quads.length(), 0) cx.advance_clock(250) assert_eq(cx.read_entity(ticker).ticks, 2) assert_true(vcx.draw_count() >= 2) assert_eq(vcx.scene().quads.length(), 1) assert_eq(vcx.scene().quads[0].bounds.size.width, sp(40.0)) } ///| test "closing VisualTestContext window releases rendered div view" { let cx = @test_platform.TestAppContext::new() let released = Ref(false) let (counter, vcx) = cx.add_window_view( size=@geometry.size(px(100.0), px(40.0)), fn(_, ctx) { ctx.on_release(fn(_, _) { released.val = true }).detach() { count: 1, } }, ) assert_false(released.val) assert_true(vcx.simulate_close()) assert_true(counter.is_released()) assert_true(released.val) }