diff --git a/src/cursive.rs b/src/cursive.rs index 1c8695c..3c6bbc8 100644 --- a/src/cursive.rs +++ b/src/cursive.rs @@ -286,7 +286,7 @@ impl Cursive { pub fn show_debug_console(&mut self) { self.add_layer( views::Dialog::around( - views::ScrollView::new(views::IdView::new( + views::ScrollView::new(views::NamedView::new( DEBUG_VIEW_ID, views::DebugView::new(), )) @@ -572,12 +572,12 @@ impl Cursive { V: View + Any, F: FnOnce(&mut V) -> R, { - self.call_on(&view::Selector::Id(id), callback) + self.call_on(&view::Selector::Name(id), callback) } - /// Convenient method to find a view wrapped in [`IdView`]. + /// Convenient method to find a view wrapped in [`NamedView`]. /// - /// This looks for a `IdView` with the given ID, and return + /// This looks for a `NamedView` with the given ID, and return /// a [`ViewRef`] to the wrapped view. The `ViewRef` implements /// `DerefMut`, so you can treat it just like a `&mut T`. /// @@ -618,20 +618,20 @@ impl Cursive { /// assert!(siv.find_id::>("select").is_some()); /// ``` /// - /// [`IdView`]: views/struct.IdView.html + /// [`NamedView`]: views/struct.NamedView.html /// [`ViewRef`]: views/type.ViewRef.html pub fn find_id(&mut self, id: &str) -> Option> where V: View + Any, { - self.call_on_id(id, views::IdView::::get_mut) + self.call_on_id(id, views::NamedView::::get_mut) } /// Moves the focus to the view identified by `id`. /// /// Convenient method to call `focus` with a `view::Selector::Id`. pub fn focus_id(&mut self, id: &str) -> Result<(), ()> { - self.focus(&view::Selector::Id(id)) + self.focus(&view::Selector::Name(id)) } /// Moves the focus to the view identified by `sel`. diff --git a/src/traits.rs b/src/traits.rs index 76d0590..521e874 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -11,7 +11,7 @@ #[doc(no_inline)] #[allow(deprecated)] pub use crate::view::{ - Boxable, Finder, Identifiable, Resizable, Scrollable, View, + Boxable, Finder, Identifiable, Nameable, Resizable, Scrollable, View, }; #[doc(no_inline)] diff --git a/src/view/finder.rs b/src/view/finder.rs index 8136d2b..3c9a55e 100644 --- a/src/view/finder.rs +++ b/src/view/finder.rs @@ -1,5 +1,5 @@ use crate::view::{View, ViewPath, ViewWrapper}; -use crate::views::{IdView, ViewRef}; +use crate::views::{NamedView, ViewRef}; use std::any::Any; /// Provides `call_on` to views. @@ -31,17 +31,17 @@ pub trait Finder { V: View + Any, F: FnOnce(&mut V) -> R, { - self.call_on(&Selector::Id(id), callback) + self.call_on(&Selector::Name(id), callback) } - /// Convenient method to find a view wrapped in an [`IdView`]. + /// Convenient method to find a view wrapped in an [`NamedView`]. /// - /// [`IdView`]: views/struct.IdView.html + /// [`NamedView`]: views/struct.NamedView.html fn find_id(&mut self, id: &str) -> Option> where V: View + Any, { - self.call_on_id(id, IdView::::get_mut) + self.call_on_id(id, NamedView::::get_mut) } } @@ -65,9 +65,9 @@ impl Finder for T { if v.is::() { *result_ref = v.downcast_mut::().map(|v| callback(v)); - } else if v.is::>() { + } else if v.is::>() { *result_ref = v - .downcast_mut::>() + .downcast_mut::>() .and_then(|v| v.with_view_mut(callback)); } } @@ -81,7 +81,12 @@ impl Finder for T { /// Selects a single view (if any) in the tree. pub enum Selector<'a> { /// Selects a view from its ID. + #[deprecated(note = "Id is being renamed to Name")] Id(&'a str), + + /// Selects a view from its name. + Name(&'a str), + /// Selects a view from its path. Path(&'a ViewPath), } diff --git a/src/view/mod.rs b/src/view/mod.rs index 9939266..930e574 100644 --- a/src/view/mod.rs +++ b/src/view/mod.rs @@ -93,7 +93,7 @@ mod view_path; mod view_trait; // Helper bases -mod identifiable; +mod nameable; mod resizable; #[macro_use] pub mod scroll; @@ -105,9 +105,9 @@ mod into_boxed_view; pub use self::any::AnyView; pub use self::finder::{Finder, Selector}; -pub use self::identifiable::Identifiable; pub use self::into_boxed_view::IntoBoxedView; pub use self::margins::Margins; +pub use self::nameable::Nameable; pub use self::position::{Offset, Position}; pub use self::resizable::Resizable; pub use self::scroll::ScrollStrategy; @@ -121,3 +121,6 @@ pub use self::view_wrapper::ViewWrapper; #[deprecated(note = "Boxable is being renamed to Resizable")] pub use self::resizable::Resizable as Boxable; + +#[deprecated(note = "Identifiable is being renamed to Nameable")] +pub use self::nameable::Nameable as Identifiable; diff --git a/src/view/identifiable.rs b/src/view/nameable.rs similarity index 60% rename from src/view/identifiable.rs rename to src/view/nameable.rs index 4d6610c..0ffcfe2 100644 --- a/src/view/identifiable.rs +++ b/src/view/nameable.rs @@ -1,13 +1,13 @@ use crate::view::View; -use crate::views::IdView; +use crate::views::NamedView; -/// Makes a view wrappable in an [`IdView`]. +/// Makes a view wrappable in an [`NamedView`]. /// -/// [`IdView`]: ../views/struct.IdView.html -pub trait Identifiable: View + Sized { - /// Wraps this view into an `IdView` with the given id. +/// [`NamedView`]: ../views/struct.NamedView.html +pub trait Nameable: View + Sized { + /// Wraps this view into an `NamedView` with the given id. /// - /// This is just a shortcut for `IdView::new(id, self)` + /// This is just a shortcut for `NamedView::new(id, self)` /// /// You can use the given id to find the view in the layout tree. /// @@ -17,7 +17,7 @@ pub trait Identifiable: View + Sized { /// # use cursive::Cursive; /// # use cursive::views::TextView; /// # use cursive::view::Resizable; - /// use cursive::view::Identifiable; + /// use cursive::view::Nameable; /// /// let mut siv = Cursive::dummy(); /// siv.add_layer( @@ -41,10 +41,16 @@ pub trait Identifiable: View + Sized { /// [`fixed_width`]: trait.Resizable.html#method.fixed_width /// [`ResizedView`]: ../views/struct.ResizedView.html /// - fn with_id>(self, id: S) -> IdView { - IdView::new(id, self) + fn with_name>(self, name: S) -> NamedView { + NamedView::new(name, self) + } + + /// Same as [`with_name`](Self::with_name()) + #[deprecated(note = "with_id is being renamed to with_name")] + fn with_id>(self, id: S) -> NamedView { + self.with_name(id) } } /// Any `View` implements this trait. -impl Identifiable for T {} +impl Nameable for T {} diff --git a/src/views/mod.rs b/src/views/mod.rs index e7ee5a2..fcafe2c 100644 --- a/src/views/mod.rs +++ b/src/views/mod.rs @@ -63,13 +63,13 @@ mod dummy; mod edit_view; mod enableable_view; mod hideable_view; -mod id_view; mod last_size_view; mod layer; mod linear_layout; mod list_view; mod menu_popup; mod menubar; +mod named_view; mod on_event_view; mod padded_view; mod panel; @@ -96,13 +96,13 @@ pub use self::dummy::DummyView; pub use self::edit_view::EditView; pub use self::enableable_view::EnableableView; pub use self::hideable_view::HideableView; -pub use self::id_view::{IdView, ViewRef}; pub use self::last_size_view::LastSizeView; pub use self::layer::Layer; pub use self::linear_layout::LinearLayout; pub use self::list_view::{ListChild, ListView}; pub use self::menu_popup::MenuPopup; pub use self::menubar::Menubar; +pub use self::named_view::{NamedView, ViewRef}; pub use self::on_event_view::OnEventView; pub use self::padded_view::PaddedView; pub use self::panel::Panel; @@ -118,14 +118,18 @@ pub use self::text_area::TextArea; pub use self::text_view::{TextContent, TextContentRef, TextView}; pub use self::tracked_view::TrackedView; -#[deprecated(note = "SizedView is being renamed to LastSizeView")] /// Same as [`LastSizeView`](self::LastSizeView). +#[deprecated(note = "SizedView is being renamed to LastSizeView")] pub type SizedView = LastSizeView; -#[deprecated(note = "BoxView is being renamed to ResizedView")] /// Same as [`ResizedView`](self::ResizedView). +#[deprecated(note = "BoxView is being renamed to ResizedView")] pub type BoxView = ResizedView; -#[deprecated(note = "ViewBox is being renamed to BoxedView")] /// Same as [`BoxedView`](self::BoxedView). +#[deprecated(note = "ViewBox is being renamed to BoxedView")] pub type ViewBox = BoxedView; + +/// Same as [`NamedView`](self::NamedView). +#[deprecated(note = "IdView is being renamed to NamedView")] +pub type IdView = NamedView; diff --git a/src/views/id_view.rs b/src/views/named_view.rs similarity index 85% rename from src/views/id_view.rs rename to src/views/named_view.rs index 91b82d9..53833c0 100644 --- a/src/views/id_view.rs +++ b/src/views/named_view.rs @@ -10,7 +10,7 @@ use std::rc::Rc; /// This lets other views refer to this one using a string identifier. /// /// See [`Identifiable`](crate::view::Identifiable) for an easy way to wrap any view with it. -pub struct IdView { +pub struct NamedView { view: Rc>, id: String, } @@ -22,10 +22,10 @@ pub struct IdView { /// [`RefMut`]: https://doc.rust-lang.org/std/cell/struct.RefMut.html pub type ViewRef = OwningHandle>, RefMut<'static, V>>; -impl IdView { - /// Wraps `view` in a new `IdView`. +impl NamedView { + /// Wraps `view` in a new `NamedView`. pub fn new>(id: S, view: V) -> Self { - IdView { + NamedView { view: Rc::new(RefCell::new(view)), id: id.into(), } @@ -45,7 +45,7 @@ impl IdView { } } -impl ViewWrapper for IdView { +impl ViewWrapper for NamedView { type V = T; fn with_view(&self, f: F) -> Option @@ -83,7 +83,10 @@ impl ViewWrapper for IdView { callback: AnyCb<'a>, ) { match selector { - &Selector::Id(id) if id == self.id => callback(self), + #[allow(deprecated)] + &Selector::Name(id) | &Selector::Id(id) if id == self.id => { + callback(self) + } s => { if let Ok(mut v) = self.view.try_borrow_mut() { v.deref_mut().call_on_any(s, callback); @@ -94,7 +97,8 @@ impl ViewWrapper for IdView { fn wrap_focus_view(&mut self, selector: &Selector<'_>) -> Result<(), ()> { match selector { - &Selector::Id(id) if id == self.id => Ok(()), + #[allow(deprecated)] + &Selector::Name(id) | &Selector::Id(id) if id == self.id => Ok(()), s => self .view .try_borrow_mut() diff --git a/src/views/stack_view.rs b/src/views/stack_view.rs index 8ce898a..9148421 100644 --- a/src/views/stack_view.rs +++ b/src/views/stack_view.rs @@ -318,7 +318,7 @@ impl StackView { /// assert_eq!(stack.find_layer_from_id("text"), Some(LayerPosition::FromBack(1))); /// ``` pub fn find_layer_from_id(&mut self, id: &str) -> Option { - let selector = Selector::Id(id); + let selector = Selector::Name(id); for (i, child) in self.layers.iter_mut().enumerate() { let mut found = false; diff --git a/src/views/tracked_view.rs b/src/views/tracked_view.rs index 9c02ccb..24e6a1b 100644 --- a/src/views/tracked_view.rs +++ b/src/views/tracked_view.rs @@ -1,6 +1,6 @@ use crate::vec::Vec2; use crate::view::{View, ViewWrapper}; -use crate::views::IdView; +use crate::views::NamedView; use crate::Printer; use std::cell::Cell; @@ -26,9 +26,15 @@ impl TrackedView { } } - /// Wraps itself in a `IdView` for easy retrieval. - pub fn with_id(self, id: &str) -> IdView { - IdView::new(id, self) + /// Same as [`with_name`](Self::with_name()) + #[deprecated(note = "with_id is being renamed to with_name")] + pub fn with_id(self, id: &str) -> NamedView { + self.with_name(id) + } + + /// Wraps itself in a `NamedView` for easy retrieval. + pub fn with_name(self, name: &str) -> NamedView { + NamedView::new(name, self) } inner_getters!(self.view: T);