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

Add View::for_each_child_rev_mut and handle events front to back #148

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
29 changes: 26 additions & 3 deletions src/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,22 @@
pub trait View {
fn id(&self) -> Id;

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

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

/// 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 @@ -229,7 +237,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_mut(&mut |child| {
self.for_each_child_rev_mut(&mut |child| {

Check warning on line 240 in src/view.rs

View check run for this annotation

Codecov / codecov/patch

src/view.rs#L240

Added line #L240 was not covered by tests
handled |= cx.view_event(child, id_path, event.clone());
handled
});
Expand Down Expand Up @@ -580,6 +588,21 @@
(**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 593 in src/view.rs

View check run for this annotation

Codecov / codecov/patch

src/view.rs#L591-L593

Added lines #L591 - L593 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 597 in src/view.rs

View check run for this annotation

Codecov / codecov/patch

src/view.rs#L595-L597

Added lines #L595 - L597 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 604 in src/view.rs

View check run for this annotation

Codecov / codecov/patch

src/view.rs#L599-L604

Added lines #L599 - L604 were not covered by tests

fn view_style(&self) -> Option<Style> {
(**self).view_style()
}
Expand Down
7 changes: 7 additions & 0 deletions src/views/clip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@
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 35 in src/views/clip.rs

View check run for this annotation

Codecov / codecov/patch

src/views/clip.rs#L30-L35

Added lines #L30 - L35 were not covered by tests

fn debug_name(&self) -> std::borrow::Cow<'static, str> {
"Clip".into()
}
Expand Down
7 changes: 7 additions & 0 deletions src/views/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@
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 38 in src/views/container.rs

View check run for this annotation

Codecov / codecov/patch

src/views/container.rs#L33-L38

Added lines #L33 - L38 were not covered by tests

fn debug_name(&self) -> std::borrow::Cow<'static, str> {
"Container".into()
}
Expand Down
43 changes: 8 additions & 35 deletions src/views/container_box.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
use kurbo::Rect;

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

/// A wrapper around any type that implements View. See [`container_box`]
pub struct ContainerBox {
Expand Down Expand Up @@ -65,36 +60,14 @@
for_each(&mut self.child);
}

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

fn update(
&mut self,
_cx: &mut crate::context::UpdateCx,
_state: Box<dyn std::any::Any>,
) -> crate::view::ChangeFlags {
ChangeFlags::empty()
}

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 event(
&mut self,
cx: &mut crate::context::EventCx,
id_path: Option<&[Id]>,
event: crate::event::Event,
) -> bool {
cx.view_event(&mut self.child, id_path, event)
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 67 in src/views/container_box.rs

View check run for this annotation

Codecov / codecov/patch

src/views/container_box.rs#L63-L67

Added lines #L63 - L67 were not covered by tests
}

fn paint(&mut self, cx: &mut crate::context::PaintCx) {
cx.paint_view(&mut self.child);
fn debug_name(&self) -> std::borrow::Cow<'static, str> {
"ContainerBox".into()

Check warning on line 71 in src/views/container_box.rs

View check run for this annotation

Codecov / codecov/patch

src/views/container_box.rs#L70-L71

Added lines #L70 - L71 were not covered by tests
}
}
7 changes: 7 additions & 0 deletions src/views/drag_resize_window_area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,11 @@
fn for_each_child_mut<'a>(&'a mut self, for_each: &mut dyn FnMut(&'a mut dyn View) -> bool) {
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 60 in src/views/drag_resize_window_area.rs

View check run for this annotation

Codecov / codecov/patch

src/views/drag_resize_window_area.rs#L55-L60

Added lines #L55 - L60 were not covered by tests
}
7 changes: 7 additions & 0 deletions src/views/drag_window_area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,11 @@
fn for_each_child_mut<'a>(&'a mut self, for_each: &mut dyn FnMut(&'a mut dyn View) -> bool) {
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 49 in src/views/drag_window_area.rs

View check run for this annotation

Codecov / codecov/patch

src/views/drag_window_area.rs#L44-L49

Added lines #L44 - L49 were not covered by tests
}
7 changes: 7 additions & 0 deletions src/views/dyn_container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,13 @@
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 108 in src/views/dyn_container.rs

View check run for this annotation

Codecov / codecov/patch

src/views/dyn_container.rs#L103-L108

Added lines #L103 - L108 were not covered by tests

fn debug_name(&self) -> std::borrow::Cow<'static, str> {
"DynamicContainer".into()
}
Expand Down
16 changes: 16 additions & 0 deletions src/views/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,22 @@
}
}

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 109 in src/views/list.rs

View check run for this annotation

Codecov / codecov/patch

src/views/list.rs#L101-L109

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

Check warning on line 113 in src/views/list.rs

View check run for this annotation

Codecov / codecov/patch

src/views/list.rs#L111-L113

Added lines #L111 - L113 were not covered by tests
}
}

Check warning on line 115 in src/views/list.rs

View check run for this annotation

Codecov / codecov/patch

src/views/list.rs#L115

Added line #L115 was not covered by tests

fn debug_name(&self) -> std::borrow::Cow<'static, str> {
"List".into()
}
Expand Down
7 changes: 7 additions & 0 deletions src/views/scroll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,13 @@
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 576 in src/views/scroll.rs

View check run for this annotation

Codecov / codecov/patch

src/views/scroll.rs#L571-L576

Added lines #L571 - L576 were not covered by tests

fn debug_name(&self) -> std::borrow::Cow<'static, str> {
"Scroll".into()
}
Expand Down
11 changes: 11 additions & 0 deletions src/views/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,17 @@
}
}

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() {
if for_each(child) {
break;
}

Check warning on line 79 in src/views/stack.rs

View check run for this annotation

Codecov / codecov/patch

src/views/stack.rs#L72-L79

Added lines #L72 - L79 were not covered by tests
}
}

Check warning on line 81 in src/views/stack.rs

View check run for this annotation

Codecov / codecov/patch

src/views/stack.rs#L81

Added line #L81 was not covered by tests

fn debug_name(&self) -> std::borrow::Cow<'static, str> {
match self.direction {
Some(FlexDirection::Column) => "Vertical Stack".into(),
Expand Down
11 changes: 11 additions & 0 deletions src/views/static_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,15 @@
}
}
}

fn for_each_child_rev_mut<'a>(
&'a mut self,
for_each: &mut dyn FnMut(&'a mut dyn View) -> bool,
) {
for child in &mut self.children.iter_mut().rev() {
if for_each(child) {
break;
}

Check warning on line 58 in src/views/static_list.rs

View check run for this annotation

Codecov / codecov/patch

src/views/static_list.rs#L51-L58

Added lines #L51 - L58 were not covered by tests
}
}

Check warning on line 60 in src/views/static_list.rs

View check run for this annotation

Codecov / codecov/patch

src/views/static_list.rs#L60

Added line #L60 was not covered by tests
}
43 changes: 16 additions & 27 deletions src/views/tab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,22 @@
}
}

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 123 in src/views/tab.rs

View check run for this annotation

Codecov / codecov/patch

src/views/tab.rs#L115-L123

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

Check warning on line 127 in src/views/tab.rs

View check run for this annotation

Codecov / codecov/patch

src/views/tab.rs#L125-L127

Added lines #L125 - L127 were not covered by tests
}
}

Check warning on line 129 in src/views/tab.rs

View check run for this annotation

Codecov / codecov/patch

src/views/tab.rs#L129

Added line #L129 was not covered by tests

fn debug_name(&self) -> std::borrow::Cow<'static, str> {
format!("Tab: {}", self.active).into()
}
Expand Down Expand Up @@ -172,31 +188,4 @@
nodes
})
}
/*
fn compute_layout(&mut self, cx: &mut crate::context::LayoutCx) -> Option<Rect> {
if let Some(Some((child, _))) = self.children.get_mut(self.active) {
Some(cx.compute_view_layout(child))
} else {
None
}
}

fn event(
&mut self,
cx: &mut EventCx,
id_path: Option<&[Id]>,
event: crate::event::Event,
) -> bool {
if let Some(Some((child, _))) = self.children.get_mut(self.active) {
cx.view_event(child, id_path, event)
} else {
false
}
}

fn paint(&mut self, cx: &mut crate::context::PaintCx) {
if let Some(Some((child, _))) = self.children.get_mut(self.active) {
cx.paint_view(child);
}
}*/
}
16 changes: 16 additions & 0 deletions src/views/virtual_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,22 @@
}
}

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