diff --git a/cursive-core/src/views/fixed_layout.rs b/cursive-core/src/views/fixed_layout.rs index 7283db3..6b54dc4 100644 --- a/cursive-core/src/views/fixed_layout.rs +++ b/cursive-core/src/views/fixed_layout.rs @@ -24,7 +24,7 @@ use crate::{ /// .child(Rect::from_size((0,2), (1,1)), TextView::new(r"\")) /// .child(Rect::from_size((14,2), (1,1)), TextView::new("/")) /// .child(Rect::from_size((3,1), (11,1)), Button::new("Clickme", |s| s.quit())); -/// ```` +/// ``` pub struct FixedLayout { children: Vec, focus: usize, @@ -125,6 +125,24 @@ impl FixedLayout { Some(self.children.remove(i).view) } + /// Looks for the child containing a view with the given name. + /// + /// Returns `Some(i)` if `self.get_child(i)` has the given name, or + /// contains a view with the given name. + /// + /// Returns `None` if the given name was not found. + pub fn find_child_from_name(&mut self, name: &str) -> Option { + let selector = Selector::Name(name); + for (i, c) in self.children.iter_mut().enumerate() { + let mut found = false; + c.view.call_on_any(&selector, &mut |_| found = true); + if found { + return Some(i); + } + } + None + } + fn iter_mut<'a>( source: Direction, children: &'a mut [Child], diff --git a/cursive-core/src/views/linear_layout.rs b/cursive-core/src/views/linear_layout.rs index 8d4a7df..95da5a7 100644 --- a/cursive-core/src/views/linear_layout.rs +++ b/cursive-core/src/views/linear_layout.rs @@ -292,6 +292,24 @@ impl LinearLayout { } } + /// Looks for the child containing a view with the given name. + /// + /// Returns `Some(i)` if `self.get_child(i)` has the given name, or + /// contains a view with the given name. + /// + /// Returns `None` if the given name was not found. + pub fn find_child_from_name(&mut self, name: &str) -> Option { + let selector = Selector::Name(name); + for (i, c) in self.children.iter_mut().enumerate() { + let mut found = false; + c.view.call_on_any(&selector, &mut |_| found = true); + if found { + return Some(i); + } + } + None + } + // If the cache can be used, return the cached size. // Otherwise, return None. fn get_cache(&self, req: Vec2) -> Option { diff --git a/cursive-core/src/views/named_view.rs b/cursive-core/src/views/named_view.rs index a7c2ca7..ccc3e8b 100644 --- a/cursive-core/src/views/named_view.rs +++ b/cursive-core/src/views/named_view.rs @@ -12,7 +12,7 @@ use std::rc::Rc; /// See [`Identifiable`](crate::view::Identifiable) for an easy way to wrap any view with it. pub struct NamedView { view: Rc>, - id: String, + name: String, } /// Mutable reference to a view. @@ -24,10 +24,10 @@ pub type ViewRef = OwningHandle>, RefMut<'static, V>>; impl NamedView { /// Wraps `view` in a new `NamedView`. - pub fn new>(id: S, view: V) -> Self { + pub fn new>(name: S, view: V) -> Self { NamedView { view: Rc::new(RefCell::new(view)), - id: id.into(), + name: name.into(), } } @@ -43,6 +43,16 @@ impl NamedView { OwningHandle::new_mut(cell_ref) } + + /// Returns the name attached to this view. + pub fn name(&self) -> &str { + &self.name + } + + /// Changes the name attached to this view. + pub fn set_name>(&mut self, name: S) { + self.name = name.into(); + } } impl ViewWrapper for NamedView { @@ -83,7 +93,9 @@ impl ViewWrapper for NamedView { ) { match selector { #[allow(deprecated)] - &Selector::Name(id) | &Selector::Id(id) if id == self.id => { + &Selector::Name(name) | &Selector::Id(name) + if name == self.name => + { callback(self) } s => { @@ -97,7 +109,11 @@ impl ViewWrapper for NamedView { fn wrap_focus_view(&mut self, selector: &Selector<'_>) -> Result<(), ()> { match selector { #[allow(deprecated)] - &Selector::Name(id) | &Selector::Id(id) if id == self.id => Ok(()), + &Selector::Name(name) | &Selector::Id(name) + if name == self.name => + { + Ok(()) + } s => self .view .try_borrow_mut()