Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't let hidden views contribute to the parent's rect #150

Merged
merged 1 commit into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,10 +284,10 @@

/// This removes a view from the app state.
pub fn remove_view(&mut self, view: &mut dyn View) {
view.for_each_child_mut(&mut |child| {

Check warning on line 287 in src/context.rs

View check run for this annotation

Codecov / codecov/patch

src/context.rs#L287

Added line #L287 was not covered by tests
self.remove_view(child);
false
});

Check warning on line 290 in src/context.rs

View check run for this annotation

Codecov / codecov/patch

src/context.rs#L289-L290

Added lines #L289 - L290 were not covered by tests
let id = view.id();
let view_state = self.view_state(id);
if let Some(action) = view_state.cleanup_listener.as_ref() {
Expand Down Expand Up @@ -1121,12 +1121,12 @@
// Propagate style requests to children if needed.
if view_state.request_style_recursive {
view_state.request_style_recursive = false;
view.for_each_child(&mut |child| {

Check warning on line 1124 in src/context.rs

View check run for this annotation

Codecov / codecov/patch

src/context.rs#L1124

Added line #L1124 was not covered by tests
let state = self.app_state_mut().view_state(child.id());
state.request_style_recursive = true;
state.request_style = true;
false
});

Check warning on line 1129 in src/context.rs

View check run for this annotation

Codecov / codecov/patch

src/context.rs#L1128-L1129

Added lines #L1128 - L1129 were not covered by tests
}

self.app_state
Expand Down Expand Up @@ -1326,18 +1326,16 @@
/// - 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<Rect> {

Check warning on line 1329 in src/context.rs

View check run for this annotation

Codecov / codecov/patch

src/context.rs#L1329

Added line #L1329 was not covered by tests
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;

Check warning on line 1333 in src/context.rs

View check run for this annotation

Codecov / codecov/patch

src/context.rs#L1332-L1333

Added lines #L1332 - L1333 were not covered by tests
}

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();

Check warning on line 1338 in src/context.rs

View check run for this annotation

Codecov / codecov/patch

src/context.rs#L1338

Added line #L1338 was not covered by tests
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(
Expand Down Expand Up @@ -1401,7 +1399,7 @@

self.restore();

layout_rect
Some(layout_rect)

Check warning on line 1402 in src/context.rs

View check run for this annotation

Codecov / codecov/patch

src/context.rs#L1402

Added line #L1402 was not covered by tests
}

pub(crate) fn get_resize_listener(&mut self, id: Id) -> Option<&mut ResizeListener> {
Expand Down
29 changes: 18 additions & 11 deletions src/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,11 @@

/// This method walks over children and must be implemented if the view has any children.
/// It should return children front to back and should stop if `_for_each` returns `true`.
fn for_each_child_rev_mut<'a>(
&'a mut self,
_for_each: &mut dyn FnMut(&'a mut dyn View) -> bool,
) {
}

Check warning on line 128 in src/view.rs

View check run for this annotation

Codecov / codecov/patch

src/view.rs#L124-L128

Added lines #L124 - L128 were not covered by tests

fn view_style(&self) -> Option<Style> {
None
Expand Down Expand Up @@ -180,10 +180,10 @@

/// Use this method to style the view's children.
fn style(&mut self, cx: &mut StyleCx<'_>) {
self.for_each_child_mut(&mut |child| {
cx.style_view(child);
false
});

Check warning on line 186 in src/view.rs

View check run for this annotation

Codecov / codecov/patch

src/view.rs#L183-L186

Added lines #L183 - L186 were not covered by tests
}

/// Use this method to layout the view's children.
Expand All @@ -201,17 +201,7 @@

/// Responsible for computing the layout of the view's children.
fn compute_layout(&mut self, cx: &mut LayoutCx) -> Option<Rect> {
let mut layout_rect: Option<Rect> = 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)

Check warning on line 204 in src/view.rs

View check run for this annotation

Codecov / codecov/patch

src/view.rs#L204

Added line #L204 was not covered by tests
}

/// Implement this to handle events and to pass them down to children
Expand All @@ -219,7 +209,7 @@
/// Return true to stop the event from propagating to other views
fn event(&mut self, cx: &mut EventCx, id_path: Option<&[Id]>, event: Event) -> bool {
let mut handled = false;
self.for_each_child_rev_mut(&mut |child| {

Check warning on line 212 in src/view.rs

View check run for this annotation

Codecov / codecov/patch

src/view.rs#L212

Added line #L212 was not covered by tests
handled |= cx.view_event(child, id_path, event.clone());
handled
});
Expand All @@ -236,6 +226,23 @@
}
}

/// Computes the layout of the view's children, if any.
pub fn default_compute_layout<V: View + ?Sized>(view: &mut V, cx: &mut LayoutCx) -> Option<Rect> {
let mut layout_rect: Option<Rect> = 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
}

Check warning on line 244 in src/view.rs

View check run for this annotation

Codecov / codecov/patch

src/view.rs#L230-L244

Added lines #L230 - L244 were not covered by tests

pub(crate) fn paint_bg(
cx: &mut PaintCx,
computed_style: &Style,
Expand Down Expand Up @@ -361,14 +368,14 @@
}
}

pub(crate) fn view_children(view: &dyn View) -> Vec<&dyn View> {
let mut result = Vec::new();
view.for_each_child(&mut |view| {
result.push(view);
false
});
result
}

Check warning on line 378 in src/view.rs

View check run for this annotation

Codecov / codecov/patch

src/view.rs#L371-L378

Added lines #L371 - L378 were not covered by tests

/// Tab navigation finds the next or previous view with the `keyboard_navigatable` status in the tree.
#[allow(dead_code)]
Expand Down Expand Up @@ -398,15 +405,15 @@
println!("Tab to {new_focus:?}");
}

fn view_filtered_children<'a>(view: &'a dyn View, id_path: &[Id]) -> Vec<&'a dyn View> {

Check warning on line 408 in src/view.rs

View check run for this annotation

Codecov / codecov/patch

src/view.rs#L408

Added line #L408 was not covered by tests
let id = id_path[0];
let id_path = &id_path[1..];

if id == view.id() {
if id_path.is_empty() {
view_children(view)

Check warning on line 414 in src/view.rs

View check run for this annotation

Codecov / codecov/patch

src/view.rs#L414

Added line #L414 was not covered by tests
} else if let Some(child) = view.child(id_path[0]) {
view_filtered_children(child, id_path)

Check warning on line 416 in src/view.rs

View check run for this annotation

Codecov / codecov/patch

src/view.rs#L416

Added line #L416 was not covered by tests
} else {
Vec::new()
}
Expand All @@ -422,7 +429,7 @@
println!("id is {id:?}");
println!("id path is {:?}", id_path.0);

let children = view_filtered_children(root_view, &id_path.0);

Check warning on line 432 in src/view.rs

View check run for this annotation

Codecov / codecov/patch

src/view.rs#L432

Added line #L432 was not covered by tests

println!(
"children is {:?}",
Expand Down Expand Up @@ -468,7 +475,7 @@
if id_path.len() == 1 {
println!("id is {id:?} id_path is {:?}", id_path);
let child_id = id_path[0];
let children = view_children(view);

Check warning on line 478 in src/view.rs

View check run for this annotation

Codecov / codecov/patch

src/view.rs#L478

Added line #L478 was not covered by tests
let pos = children.iter().position(|v| v.id() == child_id);
if let Some(pos) = pos {
if children.len() > 1 && pos < children.len() - 1 {
Expand Down Expand Up @@ -513,7 +520,7 @@

if id_path.len() == 1 {
let child_id = id_path[0];
let children = view_children(view);

Check warning on line 523 in src/view.rs

View check run for this annotation

Codecov / codecov/patch

src/view.rs#L523

Added line #L523 was not covered by tests
let pos = children.iter().position(|v| v.id() == child_id);
if let Some(pos) = pos {
if pos > 0 {
Expand All @@ -532,16 +539,16 @@

pub(crate) fn view_children_set_parent_id(view: &dyn View) {
let parent_id = view.id();
view.for_each_child(&mut |child| {

Check warning on line 542 in src/view.rs

View check run for this annotation

Codecov / codecov/patch

src/view.rs#L542

Added line #L542 was not covered by tests
child.id().set_parent(parent_id);
view_children_set_parent_id(child);
false
});

Check warning on line 546 in src/view.rs

View check run for this annotation

Codecov / codecov/patch

src/view.rs#L545-L546

Added lines #L545 - L546 were not covered by tests
}

fn view_nested_last_child(view: &dyn View) -> &dyn View {
let mut last_child = view;
while let Some(new_last_child) = view_children(last_child).pop() {

Check warning on line 551 in src/view.rs

View check run for this annotation

Codecov / codecov/patch

src/view.rs#L551

Added line #L551 was not covered by tests
last_child = new_last_child;
}
last_child
Expand All @@ -561,7 +568,7 @@
}
println!("{:?} {}", current_view.id(), &current_view.debug_name());

let mut children = view_children(current_view);

Check warning on line 571 in src/view.rs

View check run for this annotation

Codecov / codecov/patch

src/view.rs#L571

Added line #L571 was not covered by tests
if let Some(last_child) = children.pop() {
views.push((last_child, [active_lines.as_slice(), &[false]].concat()));
}
Expand All @@ -580,20 +587,20 @@
(**self).id()
}

fn for_each_child<'a>(&'a self, for_each: &mut dyn FnMut(&'a dyn View) -> bool) {
(**self).for_each_child(for_each)
}

Check warning on line 592 in src/view.rs

View check run for this annotation

Codecov / codecov/patch

src/view.rs#L590-L592

Added lines #L590 - L592 were not covered by tests

fn for_each_child_mut<'a>(&'a mut self, for_each: &mut dyn FnMut(&'a mut dyn View) -> bool) {
(**self).for_each_child_mut(for_each)
}

Check warning on line 596 in src/view.rs

View check run for this annotation

Codecov / codecov/patch

src/view.rs#L594-L596

Added lines #L594 - L596 were not covered by tests

fn for_each_child_rev_mut<'a>(
&'a mut self,
for_each: &mut dyn FnMut(&'a mut dyn View) -> bool,
) {
(**self).for_each_child_rev_mut(for_each)
}

Check warning on line 603 in src/view.rs

View check run for this annotation

Codecov / codecov/patch

src/view.rs#L598-L603

Added lines #L598 - L603 were not covered by tests

fn view_style(&self) -> Option<Style> {
(**self).view_style()
Expand Down
10 changes: 1 addition & 9 deletions src/views/clip.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use kurbo::{Rect, Size};
use kurbo::Size;

use crate::{id::Id, view::View};

Expand Down Expand Up @@ -27,25 +27,17 @@
for_each(&mut self.child);
}

fn for_each_child_rev_mut<'a>(
&'a mut self,
for_each: &mut dyn FnMut(&'a mut dyn View) -> bool,
) {
for_each(&mut self.child);

Check warning on line 34 in src/views/clip.rs

View check run for this annotation

Codecov / codecov/patch

src/views/clip.rs#L30-L34

Added lines #L30 - L34 were not covered by tests
}

fn debug_name(&self) -> std::borrow::Cow<'static, str> {
"Clip".into()

Check warning on line 38 in src/views/clip.rs

View check run for this annotation

Codecov / codecov/patch

src/views/clip.rs#L37-L38

Added lines #L37 - L38 were not covered by tests
}

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<Rect> {
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);
Expand Down
10 changes: 2 additions & 8 deletions src/views/virtual_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use crate::{
context::LayoutCx,
id::Id,
view::{ChangeFlags, View},
view::{self, ChangeFlags, View},
};

use super::{apply_diff, diff, Diff, DiffOpAdd, FxIndexSet, HashRun};
Expand Down Expand Up @@ -217,21 +217,21 @@
}
}

fn for_each_child_rev_mut<'a>(
&'a mut self,
for_each: &mut dyn FnMut(&'a mut dyn View) -> bool,
) {
for child in self
.children
.iter_mut()
.rev()
.filter_map(|child| child.as_mut())

Check warning on line 228 in src/views/virtual_list.rs

View check run for this annotation

Codecov / codecov/patch

src/views/virtual_list.rs#L220-L228

Added lines #L220 - L228 were not covered by tests
{
if for_each(&mut child.0) {
break;
}

Check warning on line 232 in src/views/virtual_list.rs

View check run for this annotation

Codecov / codecov/patch

src/views/virtual_list.rs#L230-L232

Added lines #L230 - L232 were not covered by tests
}
}

Check warning on line 234 in src/views/virtual_list.rs

View check run for this annotation

Codecov / codecov/patch

src/views/virtual_list.rs#L234

Added line #L234 was not covered by tests

fn debug_name(&self) -> std::borrow::Cow<'static, str> {
"VirtualList".into()
Expand Down Expand Up @@ -340,13 +340,7 @@
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)

Check warning on line 343 in src/views/virtual_list.rs

View check run for this annotation

Codecov / codecov/patch

src/views/virtual_list.rs#L343

Added line #L343 was not covered by tests
}
}

Expand Down
Loading