Avoid panicking if child view points to a view that was not rendered

This commit is contained in:
Antonio Scandurra 2022-10-13 15:11:57 +02:00
parent 06dfb74663
commit edb61a9c8f

View file

@ -984,7 +984,7 @@ impl ChildView {
} }
impl Element for ChildView { impl Element for ChildView {
type LayoutState = (); type LayoutState = bool;
type PaintState = (); type PaintState = ();
fn layout( fn layout(
@ -992,18 +992,27 @@ impl Element for ChildView {
constraint: SizeConstraint, constraint: SizeConstraint,
cx: &mut LayoutContext, cx: &mut LayoutContext,
) -> (Vector2F, Self::LayoutState) { ) -> (Vector2F, Self::LayoutState) {
let size = cx.layout(self.view.id(), constraint); if cx.rendered_views.contains_key(&self.view.id()) {
(size, ()) let size = cx.layout(self.view.id(), constraint);
(size, true)
} else {
log::error!("layout called on a view that was not rendered");
(Vector2F::zero(), false)
}
} }
fn paint( fn paint(
&mut self, &mut self,
bounds: RectF, bounds: RectF,
visible_bounds: RectF, visible_bounds: RectF,
_: &mut Self::LayoutState, view_is_valid: &mut Self::LayoutState,
cx: &mut PaintContext, cx: &mut PaintContext,
) -> Self::PaintState { ) {
cx.paint(self.view.id(), bounds.origin(), visible_bounds); if *view_is_valid {
cx.paint(self.view.id(), bounds.origin(), visible_bounds);
} else {
log::error!("paint called on a view that was not rendered");
}
} }
fn dispatch_event( fn dispatch_event(
@ -1011,11 +1020,16 @@ impl Element for ChildView {
event: &Event, event: &Event,
_: RectF, _: RectF,
_: RectF, _: RectF,
_: &mut Self::LayoutState, view_is_valid: &mut Self::LayoutState,
_: &mut Self::PaintState, _: &mut Self::PaintState,
cx: &mut EventContext, cx: &mut EventContext,
) -> bool { ) -> bool {
cx.dispatch_event(self.view.id(), event) if *view_is_valid {
cx.dispatch_event(self.view.id(), event)
} else {
log::error!("dispatch_event called on a view that was not rendered");
false
}
} }
fn rect_for_text_range( fn rect_for_text_range(
@ -1023,11 +1037,15 @@ impl Element for ChildView {
range_utf16: Range<usize>, range_utf16: Range<usize>,
_: RectF, _: RectF,
_: RectF, _: RectF,
_: &Self::LayoutState, view_is_valid: &Self::LayoutState,
_: &Self::PaintState, _: &Self::PaintState,
cx: &MeasurementContext, cx: &MeasurementContext,
) -> Option<RectF> { ) -> Option<RectF> {
cx.rect_for_text_range(self.view.id(), range_utf16) if *view_is_valid {
cx.rect_for_text_range(self.view.id(), range_utf16)
} else {
None
}
} }
fn debug( fn debug(