use std::fmt::Debug;
use std::rc::Rc;
use waft_foundation::App;
use waft_rendering::{AnyRenderObject, RenderAnnotatedRegion, RenderBox, RenderHandle};
use crate::framework::{
BuildContext, IntoWidget, KeyRef, RenderObjectWidget, SingleChildRenderObjectWidget, WidgetRef,
};
#[derive(Debug)]
pub struct AnnotatedRegion<T> {
pub key: Option<KeyRef>,
pub value: Rc<T>,
pub sized: bool,
pub child: WidgetRef,
}
impl<T: PartialEq + Debug + 'static> AnnotatedRegion<T> {
pub fn new<K>(child: impl IntoWidget<K>, value: T) -> AnnotatedRegion<T> {
AnnotatedRegion {
key: None,
value: Rc::new(value),
sized: true,
child: child.into_widget(),
}
}
pub fn key(mut self, key: KeyRef) -> AnnotatedRegion<T> {
self.key = Some(key);
self
}
pub fn sized(mut self, sized: bool) -> AnnotatedRegion<T> {
self.sized = sized;
self
}
}
impl<T: PartialEq + Debug + 'static> RenderObjectWidget for AnnotatedRegion<T> {
type RenderObject = RenderAnnotatedRegion<T>;
fn key(&self) -> Option<&KeyRef> {
self.key.as_ref()
}
fn create_render_object(&self, app: &mut App, _context: BuildContext) -> AnyRenderObject {
RenderAnnotatedRegion::new(app, Rc::clone(&self.value), self.sized, None).as_object()
}
fn update_render_object(
&self,
app: &mut App,
_context: BuildContext,
render_object: RenderHandle<RenderAnnotatedRegion<T>>,
) {
render_object.set_value(app, Rc::clone(&self.value));
render_object.set_sized(app, self.sized);
}
}
impl<T: PartialEq + Debug + 'static> SingleChildRenderObjectWidget for AnnotatedRegion<T> {
fn child(&self) -> Option<&WidgetRef> {
Some(&self.child)
}
}
#[cfg(test)]
mod tests {
use std::cell::RefCell;
use std::rc::Rc;
use waft_embedder::{
ApplicationSwitcherDescription, Brightness, Offset, SystemChrome, SystemUiOverlayStyle,
};
use waft_foundation::AppCell;
use waft_rendering::{AnnotationResult, AnyContainerLayer, ErasedLayer};
use waft_test::TestPlatform;
use super::*;
use crate::test_harness::Harness;
use crate::widgets::basic::{Center, Column, SizedBox};
#[derive(Default)]
struct RecordingChrome {
styles: RefCell<Vec<SystemUiOverlayStyle>>,
}
impl RecordingChrome {
fn styles(&self) -> Vec<SystemUiOverlayStyle> {
self.styles.borrow().clone()
}
}
impl SystemChrome for RecordingChrome {
fn set_overlay_style(&self, style: &SystemUiOverlayStyle) {
self.styles.borrow_mut().push(*style);
}
fn set_application_switcher_description(
&self,
_description: &ApplicationSwitcherDescription,
) {
}
}
fn styles_of_one_frame(child: WidgetRef) -> Vec<SystemUiOverlayStyle> {
let chrome = Rc::new(RecordingChrome::default());
let platform = TestPlatform::new().with_system_chrome(chrome.clone());
let cell = AppCell::with_platform(Rc::new(platform));
let harness = Harness::mount(&mut cell.borrow_mut(), child);
harness.pump(&mut cell.borrow_mut());
let root = harness.render_root(&cell.borrow());
root.composite_frame(&mut cell.borrow_mut());
cell.checkpoint();
chrome.styles()
}
fn the_whole_view(style: SystemUiOverlayStyle) -> WidgetRef {
AnnotatedRegion::new(SizedBox::expand(), style).into_widget()
}
#[test]
fn one_region_over_the_whole_view_styles_both_of_the_system_bars() {
let style = SystemUiOverlayStyle::new()
.status_bar_icon_brightness(Brightness::Dark)
.system_navigation_bar_icon_brightness(Brightness::Dark);
assert_eq!(styles_of_one_frame(the_whole_view(style)), vec![style]);
}
#[test]
fn a_view_that_annotates_nothing_leaves_the_system_bars_alone() {
assert_eq!(
styles_of_one_frame(SizedBox::expand().into_widget()),
vec![]
);
}
#[test]
fn the_region_under_each_bar_styles_that_bar_and_not_the_other() {
let half = |style: SystemUiOverlayStyle| {
AnnotatedRegion::new(sized(300.0, 100.0), style).into_widget()
};
let upper = SystemUiOverlayStyle::new().status_bar_icon_brightness(Brightness::Dark);
let lower =
SystemUiOverlayStyle::new().system_navigation_bar_icon_brightness(Brightness::Light);
let view = Column::new()
.children(vec![half(upper), half(lower)])
.into_widget();
assert_eq!(
styles_of_one_frame(view),
vec![
SystemUiOverlayStyle::new()
.status_bar_icon_brightness(Brightness::Dark)
.system_navigation_bar_icon_brightness(Brightness::Light)
],
"the upper region speaks for the status bar and the lower for the navigation bar"
);
}
#[derive(Debug, PartialEq)]
struct Marker(u32);
fn sized(width: f64, height: f64) -> WidgetRef {
SizedBox {
width: Some(width),
height: Some(height),
..Default::default()
}
.into_widget()
}
fn root_layer(harness: &Harness, app: &App) -> AnyContainerLayer {
harness
.render_root(app)
.as_object()
.debug_layer(app)
.expect("the root has a layer")
}
fn centred_region(sized_region: bool) -> WidgetRef {
Center::new()
.child(
SizedBox {
width: Some(100.0),
height: Some(50.0),
child: Some(
AnnotatedRegion::new(SizedBox::expand(), Marker(1))
.sized(sized_region)
.into_widget(),
),
..Default::default()
}
.into_widget(),
)
.into_widget()
}
#[test]
fn a_sized_region_answers_inside_its_bounds_and_nowhere_else() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let harness = Harness::mount(&mut app, centred_region(true));
harness.pump(&mut app);
let layer = root_layer(&harness, &app).as_layer();
let inside = layer.find::<Marker>(&app, Offset::new(150.0, 100.0));
assert_eq!(inside.as_deref(), Some(&Marker(1)));
assert!(
layer
.find::<Marker>(&app, Offset::new(100.0, 75.0))
.is_some(),
"the top left corner is inside"
);
assert!(
layer
.find::<Marker>(&app, Offset::new(10.0, 10.0))
.is_none(),
"outside the region"
);
assert!(
layer
.find::<Marker>(&app, Offset::new(200.0, 100.0))
.is_none(),
"the right edge is excluded"
);
}
#[test]
fn an_unsized_region_answers_everywhere() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let harness = Harness::mount(&mut app, centred_region(false));
harness.pump(&mut app);
let layer = root_layer(&harness, &app).as_layer();
assert_eq!(
layer
.find::<Marker>(&app, Offset::new(150.0, 100.0))
.as_deref(),
Some(&Marker(1))
);
assert_eq!(
layer.find::<Marker>(&app, Offset::new(1.0, 1.0)).as_deref(),
Some(&Marker(1)),
"no size means no bounds check"
);
}
#[test]
fn a_find_of_another_type_finds_nothing() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let harness = Harness::mount(&mut app, centred_region(true));
harness.pump(&mut app);
assert!(
root_layer(&harness, &app)
.as_layer()
.find::<u32>(&app, Offset::new(150.0, 100.0))
.is_none(),
"the target type must be identical to the annotated type"
);
}
#[test]
fn nested_regions_answer_innermost_first() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let inner = AnnotatedRegion::new(SizedBox::expand(), Marker(2));
let tree = Center::new()
.child(
SizedBox {
width: Some(100.0),
height: Some(50.0),
child: Some(
AnnotatedRegion::new(
Center::new().child(
SizedBox {
width: Some(40.0),
height: Some(20.0),
child: Some(inner.into_widget()),
..Default::default()
}
.into_widget(),
),
Marker(1),
)
.into_widget(),
),
..Default::default()
}
.into_widget(),
)
.into_widget();
let harness = Harness::mount(&mut app, tree);
harness.pump(&mut app);
let layer = root_layer(&harness, &app).as_layer();
let result: AnnotationResult<Marker> =
layer.find_all_annotations(&app, Offset::new(150.0, 100.0));
assert_eq!(
result
.annotations()
.map(|marker| marker.0)
.collect::<Vec<_>>(),
vec![2, 1],
"the innermost region is the most specific"
);
assert_eq!(
result
.entries()
.iter()
.map(|entry| entry.local_position)
.collect::<Vec<_>>(),
vec![Offset::new(20.0, 10.0), Offset::new(50.0, 25.0)],
"each entry is in the coordinate space of its own region"
);
assert_eq!(
layer
.find::<Marker>(&app, Offset::new(150.0, 100.0))
.as_deref(),
Some(&Marker(2)),
"`find` stops at the first"
);
let outer_only: AnnotationResult<Marker> =
layer.find_all_annotations(&app, Offset::new(110.0, 80.0));
assert_eq!(
outer_only
.annotations()
.map(|marker| marker.0)
.collect::<Vec<_>>(),
vec![1],
"outside the inner region, only the outer one answers"
);
}
#[test]
fn a_region_reconfigures_its_render_object_in_place() {
let cell = AppCell::new();
let mut app = cell.borrow_mut();
let region = |value: u32, sized_region: bool| {
AnnotatedRegion::new(sized(10.0, 10.0), Marker(value))
.sized(sized_region)
.into_widget()
};
let harness = Harness::mount(&mut app, region(1, true));
harness.pump(&mut app);
let render_object = harness
.render_root(&app)
.child(&app)
.expect("a child")
.as_object()
.downcast::<RenderAnnotatedRegion<Marker>>(&app)
.expect("a RenderAnnotatedRegion");
assert_eq!(*render_object.value(&app), Marker(1));
assert!(render_object.sized(&app), "Dart's default");
harness.set_child(&mut app, region(2, false));
harness.pump(&mut app);
assert_eq!(
*render_object.value(&app),
Marker(2),
"the same render object is reconfigured"
);
assert!(!render_object.sized(&app));
}
}