use std::mem;
use crate::core::{
AccessCtx, ChildrenIds, LayoutCtx, MeasureCtx, NewWidget, PaintCtx, PropertiesRef, RegisterCtx,
Widget, WidgetMut, WidgetPod,
};
use crate::imaging::Painter;
use crate::kurbo::{Axis, Point, Size};
use crate::layout::{LenReq, Length};
pub struct ResizeObserver {
child: WidgetPod<dyn Widget>,
last_size: Option<Size>,
}
impl ResizeObserver {
pub fn new(child: NewWidget<impl Widget + ?Sized>) -> Self {
Self {
child: child.erased().to_pod(),
last_size: None,
}
}
}
impl ResizeObserver {
pub fn set_child(this: &mut WidgetMut<'_, Self>, child: NewWidget<impl Widget + ?Sized>) {
let old_child = mem::replace(&mut this.widget.child, child.erased().to_pod());
this.ctx.remove_child(old_child);
}
pub fn force_resend(this: &mut WidgetMut<'_, Self>) {
this.widget.last_size = None;
this.ctx.request_layout();
}
pub fn child_mut<'t>(this: &'t mut WidgetMut<'_, Self>) -> WidgetMut<'t, dyn Widget> {
this.ctx.get_mut(&mut this.widget.child)
}
}
#[derive(Debug)]
pub struct LayoutChanged;
impl Widget for ResizeObserver {
type Action = LayoutChanged;
fn accepts_pointer_interaction(&self) -> bool {
false
}
fn register_children(&mut self, ctx: &mut RegisterCtx<'_>) {
ctx.register_child(&mut self.child);
}
fn measure(
&mut self,
ctx: &mut MeasureCtx<'_>,
_props: &PropertiesRef<'_>,
axis: Axis,
_len_req: LenReq,
cross_length: Option<Length>,
) -> Length {
ctx.redirect_measurement(&mut self.child, axis, cross_length)
}
fn layout(&mut self, ctx: &mut LayoutCtx<'_>, _props: &PropertiesRef<'_>, size: Size) {
ctx.run_layout(&mut self.child, size);
ctx.place_child(&mut self.child, Point::ORIGIN);
ctx.derive_baselines(&self.child);
if self.last_size.is_none_or(|it| it != size) {
self.last_size = Some(size);
ctx.submit_action::<Self::Action>(LayoutChanged);
}
}
fn paint(
&mut self,
_ctx: &mut PaintCtx<'_>,
_props: &PropertiesRef<'_>,
_painter: &mut Painter<'_>,
) {
}
fn accessibility_role(&self) -> accesskit::Role {
accesskit::Role::GenericContainer
}
fn accessibility(
&mut self,
_ctx: &mut AccessCtx<'_>,
_props: &PropertiesRef<'_>,
_node: &mut accesskit::Node,
) {
}
fn children_ids(&self) -> ChildrenIds {
ChildrenIds::from_slice(&[self.child.id()])
}
}
#[cfg(test)]
mod tests {
use masonry_testing::TestHarness;
use crate::core::{NewWidget, Widget, WidgetTag, WindowEvent};
use crate::dpi::PhysicalSize;
use crate::kurbo::Size;
use crate::layout::AsUnit;
use crate::properties::Dimensions;
use crate::theme::default_property_set;
use crate::widgets::{Flex, LayoutChanged, ResizeObserver, SizedBox};
#[test]
fn detects_inner_resizing() {
let tag = WidgetTag::named("inner_box");
let inner_box =
NewWidget::new(SizedBox::empty().width(100.px()).height(100.px())).with_tag(tag);
let observer = ResizeObserver::new(inner_box)
.prepare()
.with_props(Dimensions::MAX);
let observer_id = observer.id();
let flex = Flex::column().with_fixed(observer).prepare();
let mut harness = TestHarness::create(default_property_set(), flex);
let (LayoutChanged, action_id) = harness.pop_action::<LayoutChanged>().unwrap();
assert_eq!(action_id, observer_id);
assert_eq!(
harness
.get_widget_with_id(observer_id)
.ctx()
.border_box()
.size(),
Size {
width: 100.,
height: 100.,
}
);
assert!(harness.pop_action::<LayoutChanged>().is_none());
harness.edit_widget(tag, |mut it| SizedBox::set_height(&mut it, 200.px()));
let (LayoutChanged, action_id) = harness.pop_action::<LayoutChanged>().unwrap();
assert_eq!(action_id, observer_id);
assert_eq!(
harness
.get_widget_with_id(observer_id)
.ctx()
.border_box()
.size(),
Size {
width: 100.,
height: 200.,
}
);
assert!(harness.pop_action::<LayoutChanged>().is_none());
harness.edit_widget(tag, |mut it| SizedBox::set_height(&mut it, 200.px()));
assert!(harness.pop_action::<LayoutChanged>().is_none());
}
#[test]
fn detects_window_resizing() {
let inner_box = SizedBox::empty().prepare().with_props(Dimensions::STRETCH);
let observer = ResizeObserver::new(inner_box)
.prepare()
.with_props(Dimensions::MAX);
let observer_id = observer.id();
let mut harness =
TestHarness::create_with_size(default_property_set(), observer, (200, 200));
let (LayoutChanged, action_id) = harness.pop_action::<LayoutChanged>().unwrap();
assert_eq!(action_id, observer_id);
assert_eq!(
harness
.get_widget_with_id(observer_id)
.ctx()
.border_box()
.size(),
Size {
width: 200.,
height: 200.,
}
);
assert!(harness.pop_action::<LayoutChanged>().is_none());
harness.process_window_event(WindowEvent::Resize(PhysicalSize::new(100, 150)));
let (LayoutChanged, action_id) = harness.pop_action::<LayoutChanged>().unwrap();
assert_eq!(action_id, observer_id);
assert_eq!(
harness
.get_widget_with_id(observer_id)
.ctx()
.border_box()
.size(),
Size {
width: 100.,
height: 150.,
}
);
assert!(harness.pop_action::<LayoutChanged>().is_none());
harness.process_window_event(WindowEvent::Resize(PhysicalSize::new(100, 150)));
assert!(harness.pop_action::<LayoutChanged>().is_none());
}
}