From ea068abeacd509b752284cee255220643132ee5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20K=C3=A5re=20Alsaker?= Date: Mon, 6 Nov 2023 09:54:28 +0100 Subject: [PATCH] Don't let hidden views contribute to the parent's rect --- src/context.rs | 12 +++++------- src/view.rs | 29 ++++++++++++++++++----------- src/views/clip.rs | 10 +--------- src/views/virtual_list.rs | 10 ++-------- 4 files changed, 26 insertions(+), 35 deletions(-) diff --git a/src/context.rs b/src/context.rs index 65ab09b9..d06f7333 100644 --- a/src/context.rs +++ b/src/context.rs @@ -1326,18 +1326,16 @@ impl<'a> LayoutCx<'a> { /// - invoking any attached context::ResizeListeners /// /// Returns the bounding rect that encompasses this view and its children - pub fn compute_view_layout(&mut self, view: &mut dyn View) -> Rect { + pub fn compute_view_layout(&mut self, view: &mut dyn View) -> Option { let id = view.id(); if self.app_state().is_hidden(id) { - return Rect::ZERO; + self.app_state_mut().view_state(id).layout_rect = Rect::ZERO; + return None; } self.save(); - let layout = self - .app_state() - .get_layout(id) - .unwrap_or(taffy::layout::Layout::new()); + let layout = self.app_state().get_layout(id).unwrap(); let origin = Point::new(layout.location.x as f64, layout.location.y as f64); let parent_viewport = self.viewport.map(|rect| { rect.with_origin(Point::new( @@ -1401,7 +1399,7 @@ impl<'a> LayoutCx<'a> { self.restore(); - layout_rect + Some(layout_rect) } pub(crate) fn get_resize_listener(&mut self, id: Id) -> Option<&mut ResizeListener> { diff --git a/src/view.rs b/src/view.rs index 951315dc..81463a68 100644 --- a/src/view.rs +++ b/src/view.rs @@ -201,17 +201,7 @@ pub trait View { /// Responsible for computing the layout of the view's children. fn compute_layout(&mut self, cx: &mut LayoutCx) -> Option { - let mut layout_rect: Option = None; - self.for_each_child_mut(&mut |child| { - let child_layout = cx.compute_view_layout(child); - if let Some(rect) = layout_rect { - layout_rect = Some(rect.union(child_layout)); - } else { - layout_rect = Some(child_layout); - } - false - }); - layout_rect + default_compute_layout(self, cx) } /// Implement this to handle events and to pass them down to children @@ -236,6 +226,23 @@ pub trait View { } } +/// Computes the layout of the view's children, if any. +pub fn default_compute_layout(view: &mut V, cx: &mut LayoutCx) -> Option { + let mut layout_rect: Option = None; + view.for_each_child_mut(&mut |child| { + let child_layout = cx.compute_view_layout(child); + if let Some(child_layout) = child_layout { + if let Some(rect) = layout_rect { + layout_rect = Some(rect.union(child_layout)); + } else { + layout_rect = Some(child_layout); + } + } + false + }); + layout_rect +} + pub(crate) fn paint_bg( cx: &mut PaintCx, computed_style: &Style, diff --git a/src/views/clip.rs b/src/views/clip.rs index 43ba40a5..3763d544 100644 --- a/src/views/clip.rs +++ b/src/views/clip.rs @@ -1,4 +1,4 @@ -use kurbo::{Rect, Size}; +use kurbo::Size; use crate::{id::Id, view::View}; @@ -38,14 +38,6 @@ impl View for Clip { "Clip".into() } - fn layout(&mut self, cx: &mut crate::context::LayoutCx) -> taffy::prelude::Node { - cx.layout_node(self.id, true, |cx| vec![cx.layout_view(&mut self.child)]) - } - - fn compute_layout(&mut self, cx: &mut crate::context::LayoutCx) -> Option { - Some(cx.compute_view_layout(&mut self.child)) - } - fn paint(&mut self, cx: &mut crate::context::PaintCx) { cx.save(); let style = cx.get_builtin_style(self.id); diff --git a/src/views/virtual_list.rs b/src/views/virtual_list.rs index a18221ac..510bb77b 100644 --- a/src/views/virtual_list.rs +++ b/src/views/virtual_list.rs @@ -8,7 +8,7 @@ use taffy::{prelude::Node, style::Dimension}; use crate::{ context::LayoutCx, id::Id, - view::{ChangeFlags, View}, + view::{self, ChangeFlags, View}, }; use super::{apply_diff, diff, Diff, DiffOpAdd, FxIndexSet, HashRun}; @@ -340,13 +340,7 @@ impl View for VirtualList { self.set_viewport.set(viewport); } - let mut layout_rect = Rect::ZERO; - for child in &mut self.children { - if let Some((child, _)) = child.as_mut() { - layout_rect = layout_rect.union(cx.compute_view_layout(child)); - } - } - Some(layout_rect) + view::default_compute_layout(self, cx) } }