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

Remove View::children and View::children_mut #149

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
10 changes: 6 additions & 4 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,9 +284,10 @@

/// This removes a view from the app state.
pub fn remove_view(&mut self, view: &mut dyn View) {
for child in view.children_mut() {
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 @@ -1120,11 +1121,12 @@
// Propagate style requests to children if needed.
if view_state.request_style_recursive {
view_state.request_style_recursive = false;
for child in view.children() {
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
5 changes: 2 additions & 3 deletions src/inspector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use crate::event::{Event, EventListener};
use crate::id::Id;
use crate::style::{Style, StyleMapValue};
use crate::view::View;
use crate::view::{view_children, View};
use crate::views::{
dyn_container, empty, img_dynamic, scroll, stack, static_list, text, v_stack, Decorators, Label,
};
Expand Down Expand Up @@ -54,8 +54,7 @@
direct_style: computed_style,
request_style: state.request_style,
request_layout: state.request_layout,
children: view
.children()
children: view_children(view)

Check warning on line 57 in src/inspector.rs

View check run for this annotation

Codecov / codecov/patch

src/inspector.rs#L57

Added line #L57 was not covered by tests
.into_iter()
.map(|view| Rc::new(CapturedView::capture(view, app_state, clipped)))
.collect(),
Expand Down
64 changes: 24 additions & 40 deletions src/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,25 +153,6 @@
result
}

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

/// At the moment, this is used only to build the debug tree.
fn children_mut(&mut self) -> Vec<&mut dyn View> {
let mut result = Vec::new();
self.for_each_child_mut(&mut |view| {
result.push(view);
false
});
result
}

fn debug_name(&self) -> std::borrow::Cow<'static, str> {
core::any::type_name::<Self>().into()
}
Expand All @@ -191,9 +172,10 @@

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

Check warning on line 178 in src/view.rs

View check run for this annotation

Codecov / codecov/patch

src/view.rs#L175-L178

Added lines #L175 - L178 were not covered by tests
}

/// Use this method to layout the view's children.
Expand Down Expand Up @@ -371,6 +353,15 @@
}
}

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 363 in src/view.rs

View check run for this annotation

Codecov / codecov/patch

src/view.rs#L356-L363

Added lines #L356 - L363 were not covered by tests

/// Tab navigation finds the next or previous view with the `keyboard_navigatable` status in the tree.
#[allow(dead_code)]
pub(crate) fn view_tab_navigation(root_view: &dyn View, app_state: &mut AppState, backwards: bool) {
Expand Down Expand Up @@ -399,15 +390,15 @@
println!("Tab to {new_focus:?}");
}

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

Check warning on line 393 in src/view.rs

View check run for this annotation

Codecov / codecov/patch

src/view.rs#L393

Added line #L393 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_children(view)

Check warning on line 399 in src/view.rs

View check run for this annotation

Codecov / codecov/patch

src/view.rs#L399

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

Check warning on line 401 in src/view.rs

View check run for this annotation

Codecov / codecov/patch

src/view.rs#L401

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

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

Check warning on line 417 in src/view.rs

View check run for this annotation

Codecov / codecov/patch

src/view.rs#L417

Added line #L417 was not covered by tests

println!(
"children is {:?}",
Expand Down Expand Up @@ -469,7 +460,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();
let children = view_children(view);

Check warning on line 463 in src/view.rs

View check run for this annotation

Codecov / codecov/patch

src/view.rs#L463

Added line #L463 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 @@ -514,7 +505,7 @@

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

Check warning on line 508 in src/view.rs

View check run for this annotation

Codecov / codecov/patch

src/view.rs#L508

Added line #L508 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 @@ -533,15 +524,16 @@

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

Check warning on line 527 in src/view.rs

View check run for this annotation

Codecov / codecov/patch

src/view.rs#L527

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

Check warning on line 531 in src/view.rs

View check run for this annotation

Codecov / codecov/patch

src/view.rs#L530-L531

Added lines #L530 - L531 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) = last_child.children().pop() {
while let Some(new_last_child) = view_children(last_child).pop() {

Check warning on line 536 in src/view.rs

View check run for this annotation

Codecov / codecov/patch

src/view.rs#L536

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

let mut children = current_view.children();
let mut children = view_children(current_view);

Check warning on line 556 in src/view.rs

View check run for this annotation

Codecov / codecov/patch

src/view.rs#L556

Added line #L556 was not covered by tests
if let Some(last_child) = children.pop() {
views.push((last_child, [active_lines.as_slice(), &[false]].concat()));
}
Expand Down Expand Up @@ -596,14 +588,6 @@
(**self).child_mut(id)
}

fn children(&self) -> Vec<&dyn View> {
(**self).children()
}

fn children_mut(&mut self) -> Vec<&mut dyn View> {
(**self).children_mut()
}

fn debug_name(&self) -> std::borrow::Cow<'static, str> {
(**self).debug_name()
}
Expand Down