Rename id to name: phase 1

This commit is contained in:
Alexandre Bury 2020-01-06 12:10:24 -08:00
parent 9aee7d374c
commit ddac91373c
9 changed files with 72 additions and 44 deletions

View File

@ -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<V>` with the given ID, and return
/// This looks for a `NamedView<V>` with the given ID, and return
/// a [`ViewRef`] to the wrapped view. The `ViewRef` implements
/// `DerefMut<Target=T>`, so you can treat it just like a `&mut T`.
///
@ -618,20 +618,20 @@ impl Cursive {
/// assert!(siv.find_id::<SelectView<u32>>("select").is_some());
/// ```
///
/// [`IdView`]: views/struct.IdView.html
/// [`NamedView`]: views/struct.NamedView.html
/// [`ViewRef`]: views/type.ViewRef.html
pub fn find_id<V>(&mut self, id: &str) -> Option<views::ViewRef<V>>
where
V: View + Any,
{
self.call_on_id(id, views::IdView::<V>::get_mut)
self.call_on_id(id, views::NamedView::<V>::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`.

View File

@ -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)]

View File

@ -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<V: View>` 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<V>(&mut self, id: &str) -> Option<ViewRef<V>>
where
V: View + Any,
{
self.call_on_id(id, IdView::<V>::get_mut)
self.call_on_id(id, NamedView::<V>::get_mut)
}
}
@ -65,9 +65,9 @@ impl<T: View> Finder for T {
if v.is::<V>() {
*result_ref =
v.downcast_mut::<V>().map(|v| callback(v));
} else if v.is::<IdView<V>>() {
} else if v.is::<NamedView<V>>() {
*result_ref = v
.downcast_mut::<IdView<V>>()
.downcast_mut::<NamedView<V>>()
.and_then(|v| v.with_view_mut(callback));
}
}
@ -81,7 +81,12 @@ impl<T: View> 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),
}

View File

@ -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;

View File

@ -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<S: Into<String>>(self, id: S) -> IdView<Self> {
IdView::new(id, self)
fn with_name<S: Into<String>>(self, name: S) -> NamedView<Self> {
NamedView::new(name, self)
}
/// Same as [`with_name`](Self::with_name())
#[deprecated(note = "with_id is being renamed to with_name")]
fn with_id<S: Into<String>>(self, id: S) -> NamedView<Self> {
self.with_name(id)
}
}
/// Any `View` implements this trait.
impl<T: View> Identifiable for T {}
impl<T: View> Nameable for T {}

View File

@ -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<T> = LastSizeView<T>;
#[deprecated(note = "BoxView is being renamed to ResizedView")]
/// Same as [`ResizedView`](self::ResizedView).
#[deprecated(note = "BoxView is being renamed to ResizedView")]
pub type BoxView<T> = ResizedView<T>;
#[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<T> = NamedView<T>;

View File

@ -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<V: View> {
pub struct NamedView<V: View> {
view: Rc<RefCell<V>>,
id: String,
}
@ -22,10 +22,10 @@ pub struct IdView<V: View> {
/// [`RefMut`]: https://doc.rust-lang.org/std/cell/struct.RefMut.html
pub type ViewRef<V> = OwningHandle<RcRef<RefCell<V>>, RefMut<'static, V>>;
impl<V: View> IdView<V> {
/// Wraps `view` in a new `IdView`.
impl<V: View> NamedView<V> {
/// Wraps `view` in a new `NamedView`.
pub fn new<S: Into<String>>(id: S, view: V) -> Self {
IdView {
NamedView {
view: Rc::new(RefCell::new(view)),
id: id.into(),
}
@ -45,7 +45,7 @@ impl<V: View> IdView<V> {
}
}
impl<T: View + 'static> ViewWrapper for IdView<T> {
impl<T: View + 'static> ViewWrapper for NamedView<T> {
type V = T;
fn with_view<F, R>(&self, f: F) -> Option<R>
@ -83,7 +83,10 @@ impl<T: View + 'static> ViewWrapper for IdView<T> {
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<T: View + 'static> ViewWrapper for IdView<T> {
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()

View File

@ -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<LayerPosition> {
let selector = Selector::Id(id);
let selector = Selector::Name(id);
for (i, child) in self.layers.iter_mut().enumerate() {
let mut found = false;

View File

@ -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<T: View> TrackedView<T> {
}
}
/// Wraps itself in a `IdView` for easy retrieval.
pub fn with_id(self, id: &str) -> IdView<Self> {
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> {
self.with_name(id)
}
/// Wraps itself in a `NamedView` for easy retrieval.
pub fn with_name(self, name: &str) -> NamedView<Self> {
NamedView::new(name, self)
}
inner_getters!(self.view: T);