Add NamedView::{name, set_name} and {LinearLayout,FixedLayout}::find_child_from_name

This commit is contained in:
Alexandre Bury 2020-07-02 17:14:37 -07:00
parent 421c08c922
commit 58ef6c298b
3 changed files with 58 additions and 6 deletions

View File

@ -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<Child>,
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<usize> {
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],

View File

@ -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<usize> {
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<Vec2> {

View File

@ -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<V> {
view: Rc<RefCell<V>>,
id: String,
name: String,
}
/// Mutable reference to a view.
@ -24,10 +24,10 @@ pub type ViewRef<V> = OwningHandle<RcRef<RefCell<V>>, RefMut<'static, V>>;
impl<V> NamedView<V> {
/// Wraps `view` in a new `NamedView`.
pub fn new<S: Into<String>>(id: S, view: V) -> Self {
pub fn new<S: Into<String>>(name: S, view: V) -> Self {
NamedView {
view: Rc::new(RefCell::new(view)),
id: id.into(),
name: name.into(),
}
}
@ -43,6 +43,16 @@ impl<V> NamedView<V> {
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<S: Into<String>>(&mut self, name: S) {
self.name = name.into();
}
}
impl<T: View + 'static> ViewWrapper for NamedView<T> {
@ -83,7 +93,9 @@ impl<T: View + 'static> ViewWrapper for NamedView<T> {
) {
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<T: View + 'static> ViewWrapper for NamedView<T> {
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()