use crate::{
App, AppContext as _, Bounds, Context, DevicePixels, DynamicTexture, Element, ElementId,
Entity, GlobalElementId, InspectorElementId, IntoElement, LayoutId, ParentElement as _, Render,
Size, StyleRefinement, Styled as _, TestAppContext, Window, WindowHandle, div, px, size,
};
use std::{cell::Cell, rc::Rc};
struct AnyBackend;
impl crate::DynamicTexturePlatform for AnyBackend {
fn create_texture(
&self,
_: crate::DynamicTextureId,
_: Size<DevicePixels>,
_: &[u8],
) -> anyhow::Result<()> {
Ok(())
}
fn update_texture(
&self,
_: crate::DynamicTextureId,
_: Bounds<DevicePixels>,
_: &[u8],
) -> anyhow::Result<()> {
Ok(())
}
fn remove_texture(&self, _: crate::DynamicTextureId) {}
}
struct Stream {
texture: Option<DynamicTexture>,
paints: Rc<Cell<usize>>,
created: Rc<Cell<Option<DynamicTexture>>>,
}
impl Render for Stream {
fn render(&mut self, window: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
self.paints.set(self.paints.get() + 1);
if self.texture.is_none() {
let texture = window
.create_dynamic_texture(size(DevicePixels(4), DevicePixels(4)), &[0u8; 64], 16)
.expect("the backend is installed");
self.created.set(Some(texture));
self.texture = Some(texture);
}
TexturePaint {
texture: self.texture.expect("created above"),
}
}
}
struct TexturePaint {
texture: DynamicTexture,
}
impl Element for TexturePaint {
type RequestLayoutState = ();
type PrepaintState = ();
fn id(&self) -> Option<ElementId> {
None
}
fn source_location(&self) -> Option<&'static core::panic::Location<'static>> {
None
}
fn request_layout(
&mut self,
_: Option<&GlobalElementId>,
_: Option<&InspectorElementId>,
window: &mut Window,
cx: &mut App,
) -> (LayoutId, ()) {
(window.request_layout(crate::Style::default(), None, cx), ())
}
fn prepaint(
&mut self,
_: Option<&GlobalElementId>,
_: Option<&InspectorElementId>,
_: Bounds<crate::Pixels>,
_: &mut (),
_: &mut Window,
_: &mut App,
) {
}
fn paint(
&mut self,
_: Option<&GlobalElementId>,
_: Option<&InspectorElementId>,
_: Bounds<crate::Pixels>,
_: &mut (),
_: &mut (),
window: &mut Window,
_: &mut App,
) {
let _ = window.paint_dynamic_texture(
Bounds {
origin: crate::point(px(0.), px(0.)),
size: size(px(64.), px(64.)),
},
self.texture,
);
}
}
struct Shell {
stream: Entity<Stream>,
}
impl Render for Shell {
fn render(&mut self, _: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
div()
.size(px(300.))
.child(self.stream.clone().cached(StyleRefinement::default()))
}
}
fn window(
cx: &mut TestAppContext,
) -> (
WindowHandle<Shell>,
Rc<Cell<usize>>,
Rc<Cell<Option<DynamicTexture>>>,
) {
let paints = Rc::new(Cell::new(0));
let created: Rc<Cell<Option<DynamicTexture>>> = Rc::new(Cell::new(None));
crate::set_dynamic_texture_platform(std::sync::Arc::new(AnyBackend));
let window = cx.add_window(|_, cx| {
let paints = paints.clone();
let created = created.clone();
Shell {
stream: cx.new(|_| Stream {
texture: None,
paints,
created,
}),
}
});
(window, paints, created)
}
#[test]
fn a_write_renders_a_cached_view_showing_the_texture_again() {
let mut cx = TestAppContext::single();
let (window, paints, created) = window(&mut cx);
cx.update_window(window.into(), |_, window, cx| {
window.draw(cx).clear(cx);
})
.unwrap();
assert_eq!(paints.get(), 1, "the first draw paints the view");
let texture = created.get().expect("the first draw created the texture");
cx.update_window(window.into(), |_, window, _| {
window
.update_dynamic_texture(
texture,
Bounds {
origin: crate::point(DevicePixels(0), DevicePixels(0)),
size: size(DevicePixels(2), DevicePixels(2)),
},
&[0u8; 16],
8,
)
.expect("the update succeeds");
})
.unwrap();
cx.update_window(window.into(), |_, window, cx| {
window.draw(cx).clear(cx);
})
.unwrap();
assert_eq!(
paints.get(),
2,
"the cached view must be rendered again after an update"
);
cx.update_window(window.into(), |_, window, _| {
window
.refresh_dynamic_texture(texture)
.expect("the texture is live");
})
.unwrap();
cx.update_window(window.into(), |_, window, cx| {
window.draw(cx).clear(cx);
})
.unwrap();
assert_eq!(
paints.get(),
3,
"the cached view must be rendered again after a GPU write"
);
}