Big renaming: part 1

This commit is contained in:
Alexandre Bury 2019-11-06 12:26:17 -08:00
parent d246d69c02
commit c86eaa5875
30 changed files with 397 additions and 398 deletions

View File

@ -32,7 +32,7 @@ type HashMap<K, V> = std::collections::HashMap<K, V, ahash::ABuildHasher>;
/// It uses a list of screen, with one screen active at a time.
pub struct Cursive {
theme: theme::Theme,
screens: Vec<views::StackView>,
screens: Vec<views::Stack>,
global_callbacks: HashMap<Event, Vec<Callback>>,
menubar: views::Menubar,
@ -153,7 +153,7 @@ impl Cursive {
backend_init().map(|backend| Cursive {
theme,
screens: vec![views::StackView::new()],
screens: vec![views::Stack::new()],
last_sizes: Vec::new(),
global_callbacks: HashMap::default(),
menubar: views::Menubar::new(),
@ -286,9 +286,9 @@ impl Cursive {
pub fn show_debug_console(&mut self) {
self.add_layer(
views::Dialog::around(
views::ScrollView::new(views::IdView::new(
views::Scroll::new(views::Named::new(
DEBUG_VIEW_ID,
views::DebugView::new(),
views::DebugConsole::new(),
))
.scroll_x(true),
)
@ -466,13 +466,13 @@ impl Cursive {
}
/// Returns a reference to the currently active screen.
pub fn screen(&self) -> &views::StackView {
pub fn screen(&self) -> &views::Stack {
let id = self.active_screen;
&self.screens[id]
}
/// Returns a mutable reference to the currently active screen.
pub fn screen_mut(&mut self) -> &mut views::StackView {
pub fn screen_mut(&mut self) -> &mut views::Stack {
let id = self.active_screen;
&mut self.screens[id]
}
@ -485,7 +485,7 @@ impl Cursive {
/// Adds a new screen, and returns its ID.
pub fn add_screen(&mut self) -> ScreenId {
let res = self.screens.len();
self.screens.push(views::StackView::new());
self.screens.push(views::Stack::new());
res
}
@ -575,9 +575,9 @@ impl Cursive {
self.call_on(&view::Selector::Id(id), callback)
}
/// Convenient method to find a view wrapped in [`IdView`].
/// Convenient method to find a view wrapped in [`Named`].
///
/// This looks for a `IdView<V>` with the given ID, and return
/// This looks for a `Named<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,13 +618,13 @@ impl Cursive {
/// assert!(siv.find_id::<SelectView<u32>>("select").is_some());
/// ```
///
/// [`IdView`]: views/struct.IdView.html
/// [`Named`]: views/struct.Named.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::Named::<V>::get_mut)
}
/// Moves the focus to the view identified by `id`.

View File

@ -101,8 +101,10 @@ mod utf8;
pub mod backend;
pub use self::cursive::{CbSink, Cursive, ScreenId};
pub use self::direction::Direction;
pub use self::printer::Printer;
pub use self::rect::Rect;
pub use self::vec::Vec2;
pub use self::view::View;
pub use self::with::With;
pub use self::xy::XY;

View File

@ -1,78 +1,78 @@
use crate::vec::Vec2;
use crate::view::{SizeConstraint, View};
use crate::views::BoxView;
use crate::views::Resized;
/// Makes a view wrappable in a [`BoxView`].
/// Makes a view wrappable in a [`Resized`].
///
/// [`BoxView`]: ../views/struct.BoxView.html
/// [`Resized`]: ../views/struct.Resized.html
pub trait Boxable: View + Sized {
/// Wraps `self` in a `BoxView` with the given size constraints.
/// Wraps `self` in a `Resized` with the given size constraints.
fn boxed(
self,
width: SizeConstraint,
height: SizeConstraint,
) -> BoxView<Self> {
BoxView::new(width, height, self)
) -> Resized<Self> {
Resized::new(width, height, self)
}
/// Wraps `self` into a fixed-size `BoxView`.
fn fixed_size<S: Into<Vec2>>(self, size: S) -> BoxView<Self> {
BoxView::with_fixed_size(size, self)
/// Wraps `self` into a fixed-size `Resized`.
fn fixed_size<S: Into<Vec2>>(self, size: S) -> Resized<Self> {
Resized::with_fixed_size(size, self)
}
/// Wraps `self` into a fixed-width `BoxView`.
fn fixed_width(self, width: usize) -> BoxView<Self> {
BoxView::with_fixed_width(width, self)
/// Wraps `self` into a fixed-width `Resized`.
fn fixed_width(self, width: usize) -> Resized<Self> {
Resized::with_fixed_width(width, self)
}
/// Wraps `self` into a fixed-width `BoxView`.
fn fixed_height(self, height: usize) -> BoxView<Self> {
BoxView::with_fixed_height(height, self)
/// Wraps `self` into a fixed-width `Resized`.
fn fixed_height(self, height: usize) -> Resized<Self> {
Resized::with_fixed_height(height, self)
}
/// Wraps `self` into a full-screen `BoxView`.
fn full_screen(self) -> BoxView<Self> {
BoxView::with_full_screen(self)
/// Wraps `self` into a full-screen `Resized`.
fn full_screen(self) -> Resized<Self> {
Resized::with_full_screen(self)
}
/// Wraps `self` into a full-width `BoxView`.
fn full_width(self) -> BoxView<Self> {
BoxView::with_full_width(self)
/// Wraps `self` into a full-width `Resized`.
fn full_width(self) -> Resized<Self> {
Resized::with_full_width(self)
}
/// Wraps `self` into a full-height `BoxView`.
fn full_height(self) -> BoxView<Self> {
BoxView::with_full_height(self)
/// Wraps `self` into a full-height `Resized`.
fn full_height(self) -> Resized<Self> {
Resized::with_full_height(self)
}
/// Wraps `self` into a limited-size `BoxView`.
fn max_size<S: Into<Vec2>>(self, size: S) -> BoxView<Self> {
BoxView::with_max_size(size, self)
/// Wraps `self` into a limited-size `Resized`.
fn max_size<S: Into<Vec2>>(self, size: S) -> Resized<Self> {
Resized::with_max_size(size, self)
}
/// Wraps `self` into a limited-width `BoxView`.
fn max_width(self, max_width: usize) -> BoxView<Self> {
BoxView::with_max_width(max_width, self)
/// Wraps `self` into a limited-width `Resized`.
fn max_width(self, max_width: usize) -> Resized<Self> {
Resized::with_max_width(max_width, self)
}
/// Wraps `self` into a limited-height `BoxView`.
fn max_height(self, max_height: usize) -> BoxView<Self> {
BoxView::with_max_height(max_height, self)
/// Wraps `self` into a limited-height `Resized`.
fn max_height(self, max_height: usize) -> Resized<Self> {
Resized::with_max_height(max_height, self)
}
/// Wraps `self` into a `BoxView` at least sized `size`.
fn min_size<S: Into<Vec2>>(self, size: S) -> BoxView<Self> {
BoxView::with_min_size(size, self)
/// Wraps `self` into a `Resized` at least sized `size`.
fn min_size<S: Into<Vec2>>(self, size: S) -> Resized<Self> {
Resized::with_min_size(size, self)
}
/// Wraps `self` in a `BoxView` at least `min_width` wide.
fn min_width(self, min_width: usize) -> BoxView<Self> {
BoxView::with_min_width(min_width, self)
/// Wraps `self` in a `Resized` at least `min_width` wide.
fn min_width(self, min_width: usize) -> Resized<Self> {
Resized::with_min_width(min_width, self)
}
/// Wraps `self` in a `BoxView` at least `min_height` tall.
fn min_height(self, min_height: usize) -> BoxView<Self> {
BoxView::with_min_height(min_height, self)
/// Wraps `self` in a `Resized` at least `min_height` tall.
fn min_height(self, min_height: usize) -> Resized<Self> {
Resized::with_min_height(min_height, self)
}
}

View File

@ -1,5 +1,5 @@
use crate::view::{View, ViewPath, ViewWrapper};
use crate::views::{IdView, ViewRef};
use crate::views::{Named, ViewRef};
use std::any::Any;
/// Provides `call_on<V: View>` to views.
@ -34,14 +34,14 @@ pub trait Finder {
self.call_on(&Selector::Id(id), callback)
}
/// Convenient method to find a view wrapped in an [`IdView`].
/// Convenient method to find a view wrapped in an [`Named`].
///
/// [`IdView`]: views/struct.IdView.html
/// [`Named`]: views/struct.Named.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, Named::<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::<Named<V>>() {
*result_ref = v
.downcast_mut::<IdView<V>>()
.downcast_mut::<Named<V>>()
.and_then(|v| v.with_view_mut(callback));
}
}

View File

@ -1,13 +1,13 @@
use crate::view::View;
use crate::views::IdView;
use crate::views::Named;
/// Makes a view wrappable in an [`IdView`].
/// Makes a view wrappable in an [`Named`].
///
/// [`IdView`]: ../views/struct.IdView.html
/// [`Named`]: ../views/struct.Named.html
pub trait Identifiable: View + Sized {
/// Wraps this view into an `IdView` with the given id.
/// Wraps this view into an `Named` with the given id.
///
/// This is just a shortcut for `IdView::new(id, self)`
/// This is just a shortcut for `Named::new(id, self)`
///
/// You can use the given id to find the view in the layout tree.
///
@ -41,8 +41,8 @@ pub trait Identifiable: View + Sized {
/// [`fixed_width`]: trait.Boxable.html#method.fixed_width
/// [`BoxView`]: ../views/struct.BoxView.html
///
fn with_id<S: Into<String>>(self, id: S) -> IdView<Self> {
IdView::new(id, self)
fn with_id<S: Into<String>>(self, id: S) -> Named<Self> {
Named::new(id, self)
}
}

View File

@ -1,13 +1,13 @@
use crate::view::View;
use crate::views::ScrollView;
use crate::views::Scroll;
/// Makes a view wrappable in a [`ScrollView`].
/// Makes a view wrappable in a [`Scroll`].
///
/// [`ScrollView`]: crate::views::ScrollView
/// [`Scroll`]: crate::views::Scroll
pub trait Scrollable: View + Sized {
/// Wraps `self` in a `ScrollView`.
fn scrollable(self) -> ScrollView<Self> {
ScrollView::new(self)
/// Wraps `self` in a `Scroll`.
fn scrollable(self) -> Scroll<Self> {
Scroll::new(self)
}
}

View File

@ -2,9 +2,9 @@ use std::cmp::min;
/// Single-dimensional constraint on a view size.
///
/// This describes a possible behaviour for a [`BoxView`].
/// This describes a possible behaviour for a [`Resized`].
///
/// [`BoxView`]: ../views/struct.BoxView.html
/// [`Resized`]: ../views/struct.Resized.html
#[derive(Debug, Clone, Copy)]
pub enum SizeConstraint {
/// No constraint imposed, the child view's response is used.
@ -22,7 +22,7 @@ pub enum SizeConstraint {
impl SizeConstraint {
/// Returns the size to be given to the child.
///
/// When `available` is offered to the `BoxView`.
/// When `available` is offered to the `Resized`.
pub fn available(self, available: usize) -> usize {
match self {
SizeConstraint::Free

View File

@ -4,14 +4,14 @@ use std::ops::{Deref, DerefMut};
/// A boxed `View`.
///
/// It derefs to the wrapped view.
pub struct ViewBox {
pub struct Boxed {
view: Box<dyn View>,
}
impl ViewBox {
/// Creates a new `ViewBox` around the given boxed view.
impl Boxed {
/// Creates a new `Boxed` around the given boxed view.
pub fn new(view: Box<dyn View>) -> Self {
ViewBox { view }
Boxed { view }
}
/// Box the given view
@ -19,7 +19,7 @@ impl ViewBox {
where
T: IntoBoxedView,
{
ViewBox::new(view.as_boxed_view())
Boxed::new(view.as_boxed_view())
}
/// Returns the inner boxed view.
@ -28,7 +28,7 @@ impl ViewBox {
}
}
impl Deref for ViewBox {
impl Deref for Boxed {
type Target = dyn View;
fn deref(&self) -> &dyn View {
@ -36,13 +36,13 @@ impl Deref for ViewBox {
}
}
impl DerefMut for ViewBox {
impl DerefMut for Boxed {
fn deref_mut(&mut self) -> &mut dyn View {
&mut *self.view
}
}
impl ViewWrapper for ViewBox {
impl ViewWrapper for Boxed {
type V = dyn View;
fn with_view<F, R>(&self, f: F) -> Option<R>

View File

@ -7,24 +7,24 @@ use crate::Printer;
use unicode_width::UnicodeWidthStr;
/// View used for debugging, showing logs.
pub struct DebugView {
pub struct DebugConsole {
// TODO: wrap log lines if needed, and save the line splits here.
}
impl DebugView {
/// Creates a new DebugView.
impl DebugConsole {
/// Creates a new DebugConsole.
pub fn new() -> Self {
DebugView {}
DebugConsole {}
}
}
impl Default for DebugView {
impl Default for DebugConsole {
fn default() -> Self {
Self::new()
}
}
impl View for DebugView {
impl View for DebugConsole {
fn draw(&self, printer: &Printer<'_, '_>) {
let logs = logger::LOGS.lock().unwrap();
// Only print the last logs, so skip what doesn't fit

View File

@ -5,7 +5,7 @@ use crate::rect::Rect;
use crate::theme::ColorStyle;
use crate::vec::Vec2;
use crate::view::{Margins, Selector, View};
use crate::views::{Button, DummyView, SizedView, TextView, ViewBox};
use crate::views::{Boxed, Button, Dummy, LastSize, Text};
use crate::Cursive;
use crate::Printer;
use crate::With;
@ -25,7 +25,7 @@ pub enum DialogFocus {
}
struct ChildButton {
button: SizedView<Button>,
button: LastSize<Button>,
offset: Cell<Vec2>,
}
@ -35,7 +35,7 @@ impl ChildButton {
F: 'static + Fn(&mut Cursive),
{
ChildButton {
button: SizedView::new(Button::new(label, cb)),
button: LastSize::new(Button::new(label, cb)),
offset: Cell::new(Vec2::zero()),
}
}
@ -46,8 +46,8 @@ impl ChildButton {
/// # Examples
///
/// ```
/// # use cursive::views::{Dialog,TextView};
/// let dialog = Dialog::around(TextView::new("Hello!"))
/// # use cursive::views::{Dialog,Text};
/// let dialog = Dialog::around(Text::new("Hello!"))
/// .button("Ok", |s| s.quit());
/// ```
pub struct Dialog {
@ -58,7 +58,7 @@ pub struct Dialog {
title_position: HAlign,
// The actual inner view.
content: SizedView<ViewBox>,
content: LastSize<Boxed>,
// Optional list of buttons under the main view.
// Include the top-left corner.
@ -87,13 +87,13 @@ impl Dialog {
///
/// You should probably call `content()` next.
pub fn new() -> Self {
Self::around(DummyView)
Self::around(Dummy)
}
/// Creates a new `Dialog` with the given content.
pub fn around<V: View + 'static>(view: V) -> Self {
Dialog {
content: SizedView::new(ViewBox::boxed(view)),
content: LastSize::new(Boxed::boxed(view)),
buttons: Vec::new(),
title: String::new(),
title_position: HAlign::Center,
@ -112,10 +112,10 @@ impl Dialog {
/// # Examples
///
/// ```
/// use cursive::views::{Dialog, TextView};
/// use cursive::views::{Dialog, Text};
///
/// let dialog = Dialog::new()
/// .content(TextView::new("Hello!"))
/// .content(Text::new("Hello!"))
/// .button("Quit", |s| s.quit());
/// ```
pub fn content<V: View + 'static>(self, view: V) -> Self {
@ -125,12 +125,12 @@ impl Dialog {
/// Gets the content of this dialog.
///
/// ```
/// use cursive::views::{Dialog, TextView};
/// let dialog = Dialog::around(TextView::new("Hello!"));
/// let text_view: &TextView = dialog
/// use cursive::views::{Dialog, Text};
/// let dialog = Dialog::around(Text::new("Hello!"));
/// let text_view: &Text = dialog
/// .get_content()
/// .as_any()
/// .downcast_ref::<TextView>()
/// .downcast_ref::<Text>()
/// .unwrap();
/// assert_eq!(text_view.get_content().source(), "Hello!");
/// ```
@ -148,7 +148,7 @@ impl Dialog {
///
/// Previous content will be dropped.
pub fn set_content<V: View + 'static>(&mut self, view: V) {
self.content = SizedView::new(ViewBox::boxed(view));
self.content = LastSize::new(Boxed::boxed(view));
self.invalidate();
}
@ -163,7 +163,7 @@ impl Dialog {
/// .button("Quit", |s| s.quit());
/// ```
pub fn text<S: Into<String>>(text: S) -> Self {
Self::around(TextView::new(text))
Self::around(Text::new(text))
}
/// Convenient method to create an infobox.

View File

@ -4,9 +4,9 @@ use crate::Printer;
/// Dummy view.
///
/// Doesn't print anything. Minimal size is (1,1).
pub struct DummyView;
pub struct Dummy;
impl View for DummyView {
impl View for Dummy {
fn draw(&self, _: &Printer<'_, '_>) {}
fn needs_relayout(&self) -> bool {

View File

@ -1,11 +1,9 @@
use crate::direction::Direction;
use crate::event::{Callback, Event, EventResult, Key, MouseEvent};
use crate::rect::Rect;
use crate::theme::{ColorStyle, Effect};
use crate::utils::lines::simple::{simple_prefix, simple_suffix};
use crate::vec::Vec2;
use crate::view::View;
use crate::{Cursive, Printer, With};
use crate::{Cursive, Direction, Printer, Vec2, View, With};
use std::cell::RefCell;
use std::rc::Rc;
use unicode_segmentation::UnicodeSegmentation;
@ -33,7 +31,7 @@ pub type OnSubmit = dyn Fn(&mut Cursive, &str);
/// ```rust
/// # use cursive::Cursive;
/// # use cursive::traits::*;
/// # use cursive::views::{Dialog, EditView, TextView};
/// # use cursive::views::{Dialog, Edit, Text};
/// let mut siv = Cursive::dummy();
///
/// // Create a dialog with an edit text and a button.
@ -44,7 +42,7 @@ pub type OnSubmit = dyn Fn(&mut Cursive, &str);
/// .title("Enter your name")
/// .padding((1, 1, 1, 0))
/// .content(
/// EditView::new()
/// Edit::new()
/// .on_submit(show_popup)
/// .with_id("name")
/// .fixed_width(20),
@ -52,7 +50,7 @@ pub type OnSubmit = dyn Fn(&mut Cursive, &str);
/// .button("Ok", |s| {
/// let name = s.call_on_id(
/// "name",
/// |view: &mut EditView| view.get_content(),
/// |view: &mut Edit| view.get_content(),
/// ).unwrap();
/// show_popup(s, &name);
/// }),
@ -64,12 +62,12 @@ pub type OnSubmit = dyn Fn(&mut Cursive, &str);
/// } else {
/// let content = format!("Hello {}!", name);
/// s.pop_layer();
/// s.add_layer(Dialog::around(TextView::new(content))
/// s.add_layer(Dialog::around(Text::new(content))
/// .button("Quit", |s| s.quit()));
/// }
/// }
/// ```
pub struct EditView {
pub struct Edit {
/// Current content.
content: Rc<String>,
@ -108,12 +106,12 @@ pub struct EditView {
style: ColorStyle,
}
new_default!(EditView);
new_default!(Edit);
impl EditView {
impl Edit {
/// Creates a new, empty edit view.
pub fn new() -> Self {
EditView {
Edit {
content: Rc::new(String::new()),
cursor: 0,
offset: 0,
@ -174,8 +172,8 @@ impl EditView {
/// # Examples
///
/// ```rust
/// # use cursive::views::EditView;
/// let edit = EditView::new().filler(" ");
/// # use cursive::views::Edit;
/// let edit = Edit::new().filler(" ");
/// ```
pub fn filler<S: Into<String>>(self, filler: S) -> Self {
self.with(|s| s.set_filler(filler))
@ -269,12 +267,12 @@ impl EditView {
/// # Examples
///
/// ```
/// use cursive::views::{TextContent, TextView, EditView};
/// use cursive::views::{TextContent, Text, Edit};
/// // Keep the length of the text in a separate view.
/// let mut content = TextContent::new("0");
/// let text_view = TextView::new_with_content(content.clone());
/// let text_view = Text::new_with_content(content.clone());
///
/// let on_edit = EditView::new()
/// let on_edit = Edit::new()
/// .on_edit(move |_s, text, _cursor| {
/// content.set_content(format!("{}", text.len()));
/// });
@ -343,9 +341,9 @@ impl EditView {
/// # Examples
///
/// ```
/// use cursive::views::{Dialog, EditView};
/// use cursive::views::{Dialog, Edit};
///
/// let edit_view = EditView::new()
/// let edit_view = Edit::new()
/// .on_submit(|s, text| {
/// s.add_layer(Dialog::info(text));
/// });
@ -522,7 +520,7 @@ fn make_small_stars(length: usize) -> &'static str {
&"****"[..length]
}
impl View for EditView {
impl View for Edit {
fn draw(&self, printer: &Printer<'_, '_>) {
assert_eq!(
printer.size.x, self.last_length,

View File

@ -1,6 +1,6 @@
use crate::event::{Event, EventResult};
use crate::view::{View, ViewWrapper};
use crate::Printer;
use crate::view::ViewWrapper;
use crate::{Printer, View};
/// Wrapper around another view that can be enabled/disabled at will.
///
@ -10,15 +10,15 @@ use crate::Printer;
///
/// ```
/// use cursive::Cursive;
/// use cursive::views::{Button, EnableableView, Checkbox, LinearLayout};
/// use cursive::views::{Button, Enableable, Checkbox, LinearLayout};
/// use cursive::traits::Identifiable;
///
/// let mut siv = Cursive::dummy();
///
/// siv.add_layer(LinearLayout::vertical()
/// .child(EnableableView::new(Checkbox::new()).with_id("my_view"))
/// .child(Enableable::new(Checkbox::new()).with_id("my_view"))
/// .child(Button::new("Toggle", |s| {
/// s.call_on_id("my_view", |v: &mut EnableableView<Checkbox>| {
/// s.call_on_id("my_view", |v: &mut Enableable<Checkbox>| {
/// // This will disable (or re-enable) the checkbox, preventing the user from
/// // interacting with it.
/// v.set_enabled(!v.is_enabled());
@ -26,17 +26,17 @@ use crate::Printer;
/// }))
/// );
/// ```
pub struct EnableableView<V> {
pub struct Enableable<V> {
view: V,
enabled: bool,
}
impl<V> EnableableView<V> {
/// Creates a new `EnableableView` around `view`.
impl<V> Enableable<V> {
/// Creates a new `Enableable` around `view`.
///
/// It will be enabled by default.
pub fn new(view: V) -> Self {
EnableableView {
Enableable {
view,
enabled: true,
}
@ -46,7 +46,7 @@ impl<V> EnableableView<V> {
inner_getters!(self.view: V);
}
impl<V: View> ViewWrapper for EnableableView<V> {
impl<V: View> ViewWrapper for Enableable<V> {
wrap_impl!(self.view: V);
fn wrap_on_event(&mut self, event: Event) -> EventResult {

View File

@ -7,22 +7,22 @@ use crate::With;
///
/// By default, it simply forwards all calls to the inner view.
///
/// When hidden (with `HideableView::hide()`), it will appear as a zero-sized
/// When hidden (with `Hideable::hide()`), it will appear as a zero-sized
/// invisible view, will not take focus and will not accept input.
///
/// It can be made visible again with `HideableView::unhide()`.
pub struct HideableView<V> {
/// It can be made visible again with `Hideable::unhide()`.
pub struct Hideable<V> {
view: V,
visible: bool,
invalidated: bool,
}
impl<V> HideableView<V> {
/// Creates a new HideableView around `view`.
impl<V> Hideable<V> {
/// Creates a new Hideable around `view`.
///
/// It will be visible by default.
pub fn new(view: V) -> Self {
HideableView {
Hideable {
view,
visible: true,
invalidated: true,
@ -64,7 +64,7 @@ impl<V> HideableView<V> {
inner_getters!(self.view: V);
}
impl<V: View> ViewWrapper for HideableView<V> {
impl<V: View> ViewWrapper for Hideable<V> {
type V = V;
fn with_view<F, R>(&self, f: F) -> Option<R>

View File

@ -3,17 +3,17 @@ use crate::view::View;
use crate::view::ViewWrapper;
/// Wrapper around a view that remembers its size.
pub struct SizedView<T> {
pub struct LastSize<T> {
/// Wrapped view.
pub view: T,
/// Cached size from the last layout() call.
pub size: Vec2,
}
impl<T> SizedView<T> {
impl<T> LastSize<T> {
/// Wraps the given view.
pub fn new(view: T) -> Self {
SizedView {
LastSize {
view,
size: Vec2::zero(),
}
@ -22,7 +22,7 @@ impl<T> SizedView<T> {
inner_getters!(self.view: T);
}
impl<T: View> ViewWrapper for SizedView<T> {
impl<T: View> ViewWrapper for LastSize<T> {
wrap_impl!(self.view: T);
fn wrap_layout(&mut self, size: Vec2) {

View File

@ -10,9 +10,9 @@ use log::debug;
use std::rc::Rc;
use unicode_width::UnicodeWidthStr;
/// Represents a child from a [`ListView`].
/// Represents a child from a [`List`].
///
/// [`ListView`]: struct.ListView.html
/// [`List`]: struct.List.html
pub enum ListChild {
/// A single row, with a label and a view.
Row(String, Box<dyn View>),
@ -37,7 +37,7 @@ impl ListChild {
}
/// Displays a list of elements.
pub struct ListView {
pub struct List {
children: Vec<ListChild>,
focus: usize,
// This callback is called when the selection is changed.
@ -45,12 +45,12 @@ pub struct ListView {
last_size: Vec2,
}
new_default!(ListView);
new_default!(List);
impl ListView {
/// Creates a new, empty `ListView`.
impl List {
/// Creates a new, empty `List`.
pub fn new() -> Self {
ListView {
List {
children: Vec::new(),
focus: 0,
on_select: None,
@ -272,7 +272,7 @@ fn try_focus(
}
}
impl View for ListView {
impl View for List {
fn draw(&self, printer: &Printer<'_, '_>) {
if self.children.is_empty() {
return;

View File

@ -7,7 +7,7 @@ use crate::rect::Rect;
use crate::vec::Vec2;
use crate::view::scroll;
use crate::view::{Position, View};
use crate::views::OnEventView;
use crate::views::OnEvent;
use crate::Cursive;
use crate::Printer;
use crate::With;
@ -202,7 +202,7 @@ impl MenuPopup {
let action_cb = action_cb.clone();
s.screen_mut().add_layer_at(
Position::parent(offset),
OnEventView::new(MenuPopup::new(Rc::clone(&tree)).on_action(
OnEvent::new(MenuPopup::new(Rc::clone(&tree)).on_action(
move |s| {
// This will happen when the subtree popup
// activates something;

View File

@ -5,7 +5,7 @@ use crate::rect::Rect;
use crate::theme::ColorStyle;
use crate::vec::Vec2;
use crate::view::{Position, View};
use crate::views::{MenuPopup, OnEventView};
use crate::views::{MenuPopup, OnEvent};
use crate::Cursive;
use crate::Printer;
use std::rc::Rc;
@ -231,11 +231,11 @@ fn show_child(s: &mut Cursive, offset: Vec2, menu: Rc<MenuTree>) {
// Also adds two key callbacks on this new view, to handle `left` and
// `right` key presses.
// (If the view itself listens for a `left` or `right` press, it will
// consume it before our OnEventView. This means sub-menus can properly
// consume it before our OnEvent. This means sub-menus can properly
// be entered.)
s.screen_mut().add_layer_at(
Position::absolute(offset),
OnEventView::new(
OnEvent::new(
MenuPopup::new(menu)
.on_dismiss(Cursive::select_menubar)
.on_action(|s| s.menubar().state = State::Inactive),

View File

@ -52,68 +52,68 @@ macro_rules! impl_enabled {
}
}
mod box_view;
mod boxed;
mod button;
mod canvas;
mod checkbox;
mod circular_focus;
mod debug_view;
mod debug_console;
mod dialog;
mod dummy;
mod edit_view;
mod enableable_view;
mod hideable_view;
mod id_view;
mod edit;
mod enableable;
mod hideable;
mod last_size;
mod layer;
mod linear_layout;
mod list_view;
mod list;
mod menu_popup;
mod menubar;
mod on_event_view;
mod padded_view;
mod named;
mod on_event;
mod padded;
mod panel;
mod progress_bar;
mod radio;
mod scroll_view;
mod select_view;
mod shadow_view;
mod sized_view;
mod slider_view;
mod stack_view;
mod resized;
mod scroll;
mod select;
mod shadow;
mod slider;
mod stack;
mod text;
mod text_area;
mod text_view;
mod tracked_view;
mod view_box;
mod tracked;
pub use self::box_view::BoxView;
pub use self::boxed::Boxed;
pub use self::button::Button;
pub use self::canvas::Canvas;
pub use self::checkbox::Checkbox;
pub use self::circular_focus::CircularFocus;
pub use self::debug_view::DebugView;
pub use self::debug_console::DebugConsole;
pub use self::dialog::{Dialog, DialogFocus};
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::dummy::Dummy;
pub use self::edit::Edit;
pub use self::enableable::Enableable;
pub use self::hideable::Hideable;
pub use self::last_size::LastSize;
pub use self::layer::Layer;
pub use self::linear_layout::LinearLayout;
pub use self::list_view::{ListChild, ListView};
pub use self::list::{List, ListChild};
pub use self::menu_popup::MenuPopup;
pub use self::menubar::Menubar;
pub use self::on_event_view::OnEventView;
pub use self::padded_view::PaddedView;
pub use self::named::{Named, ViewRef};
pub use self::on_event::OnEvent;
pub use self::padded::Padded;
pub use self::panel::Panel;
pub use self::progress_bar::ProgressBar;
pub use self::radio::{RadioButton, RadioGroup};
pub use self::scroll_view::ScrollView;
pub use self::select_view::SelectView;
pub use self::shadow_view::ShadowView;
pub use self::sized_view::SizedView;
pub use self::slider_view::SliderView;
pub use self::stack_view::{LayerPosition, StackView};
pub use self::resized::Resized;
pub use self::scroll::Scroll;
pub use self::select::Select;
pub use self::shadow::Shadow;
pub use self::slider::Slider;
pub use self::stack::{LayerPosition, Stack};
pub use self::text::{Text, TextContent, TextContentRef};
pub use self::text_area::TextArea;
pub use self::text_view::{TextContent, TextContentRef, TextView};
pub use self::tracked_view::TrackedView;
pub use self::view_box::ViewBox;
pub use self::tracked::Tracked;

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 Named<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> Named<V> {
/// Wraps `view` in a new `Named`.
pub fn new<S: Into<String>>(id: S, view: V) -> Self {
IdView {
Named {
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 Named<T> {
type V = T;
fn with_view<F, R>(&self, f: F) -> Option<R>

View File

@ -24,21 +24,21 @@ use std::rc::Rc;
/// "Simple" callbacks ([`on_event`] and [`on_pre_event`]) skip this first
/// phase and are only called with a `&mut Cursive`.
///
/// [`on_event`]: struct.OnEventView.html#method.on_event
/// [`on_pre_event`]: struct.OnEventView.html#method.on_pre_event
/// [`on_event_inner`]: struct.OnEventView.html#method.on_event_inner
/// [`on_pre_event_inner`]: struct.OnEventView.html#method.on_pre_event_inner
/// [`on_event`]: struct.OnEvent.html#method.on_event
/// [`on_pre_event`]: struct.OnEvent.html#method.on_pre_event
/// [`on_event_inner`]: struct.OnEvent.html#method.on_event_inner
/// [`on_pre_event_inner`]: struct.OnEvent.html#method.on_pre_event_inner
///
/// # Examples
///
/// ```
/// # use cursive::event;;
/// # use cursive::views::{OnEventView, TextView};
/// let view = OnEventView::new(TextView::new("This view has an event!"))
/// # use cursive::views::{OnEvent, Text};
/// let view = OnEvent::new(Text::new("This view has an event!"))
/// .on_event('q', |s| s.quit())
/// .on_event(event::Key::Esc, |s| s.quit());
/// ```
pub struct OnEventView<T: View> {
pub struct OnEvent<T: View> {
view: T,
callbacks: Vec<(EventTrigger, Action<T>)>,
}
@ -65,10 +65,10 @@ enum TriggerPhase {
AfterChild,
}
impl<T: View> OnEventView<T> {
/// Wraps the given view in a new OnEventView.
impl<T: View> OnEvent<T> {
/// Wraps the given view in a new OnEvent.
pub fn new(view: T) -> Self {
OnEventView {
OnEvent {
view,
callbacks: Vec::new(),
}
@ -82,15 +82,15 @@ impl<T: View> OnEventView<T> {
///
///
/// ```rust
/// # use cursive::views::{OnEventView, DummyView};
/// # use cursive::views::{OnEvent, Dummy};
/// # use cursive::event::{Key, EventTrigger};
/// let view = OnEventView::new(DummyView)
/// let view = OnEvent::new(Dummy)
/// .on_event('q', |s| s.quit())
/// .on_event(Key::Esc, |s| {
/// s.pop_layer();
/// })
/// .on_event(EventTrigger::mouse(), |s| {
/// s.add_layer(DummyView);
/// s.add_layer(Dummy);
/// });
/// ```
pub fn on_event<F, E>(self, trigger: E, cb: F) -> Self
@ -143,17 +143,17 @@ impl<T: View> OnEventView<T> {
///
/// Chainable variant.
///
/// [`on_event`]: OnEventView::on_event()
/// [`on_event`]: OnEvent::on_event()
///
/// # Examples
///
/// ```rust
/// # use cursive::views::{DummyView, OnEventView};
/// # use cursive::views::{Dummy, OnEvent};
/// # use cursive::event::{Event, EventTrigger, MouseEvent, EventResult};
/// let view = OnEventView::new(DummyView)
/// let view = OnEvent::new(Dummy)
/// .on_event_inner(
/// EventTrigger::mouse(),
/// |d: &mut DummyView, e: &Event| {
/// |d: &mut Dummy, e: &Event| {
/// if let &Event::Mouse { event: MouseEvent::Press(_), .. } = e {
/// // Do something on mouse press
/// Some(EventResult::with_cb(|s| {
@ -254,7 +254,7 @@ impl<T: View> OnEventView<T> {
inner_getters!(self.view: T);
}
impl<T: View> ViewWrapper for OnEventView<T> {
impl<T: View> ViewWrapper for OnEvent<T> {
wrap_impl!(self.view: T);
fn wrap_on_event(&mut self, event: Event) -> EventResult {

View File

@ -12,23 +12,23 @@ use crate::Printer;
/// # Examples
///
/// ```rust
/// # use cursive::views::{TextView, PaddedView};
/// # use cursive::views::{Text, Padded};
/// // Adds 2 columns of padding to the left and to the right.
/// let view = PaddedView::new(
/// let view = Padded::new(
/// ((2,2), (0,0)), // ((left, right), (top, bottom))
/// TextView::new("Padded text")
/// Text::new("Padded text")
/// );
/// ```
pub struct PaddedView<V> {
pub struct Padded<V> {
view: V,
margins: Margins,
}
impl<V: View> PaddedView<V> {
/// Wraps `view` in a new `PaddedView` with the given margins.
impl<V: View> Padded<V> {
/// Wraps `view` in a new `Padded` with the given margins.
pub fn new<M: Into<Margins>>(margins: M, view: V) -> Self {
let margins = margins.into();
PaddedView { view, margins }
Padded { view, margins }
}
/// Sets the margins for this view.
@ -40,7 +40,7 @@ impl<V: View> PaddedView<V> {
inner_getters!(self.view: V);
}
impl<V: View> ViewWrapper for PaddedView<V> {
impl<V: View> ViewWrapper for Padded<V> {
wrap_impl!(self.view: V);
fn wrap_required_size(&mut self, req: Vec2) -> Vec2 {

View File

@ -8,7 +8,7 @@ use crate::XY;
/// Each axis can independently be set to:
///
/// * Keep a **fixed** size
/// * Use **all** available size
/// * Use the **full** available size
/// * Use **at most** a given size
/// * Use **at least** a given size
/// * Let the wrapped view decide.
@ -16,14 +16,14 @@ use crate::XY;
/// # Examples
///
/// ```
/// use cursive::views::{BoxView, TextView};
/// use cursive::views::{Resized, TextView};
///
/// // Creates a 20x4 BoxView with a TextView content.
/// let view = BoxView::with_fixed_size((20,4), TextView::new("Hello!"));
/// // Creates a 20x4 Resized with a TextView content.
/// let view = Resized::with_fixed_size((20,4), TextView::new("Hello!"));
/// ```
///
/// See also [`Boxable`](crate::view::Boxable) for an easy way to wrap any view.
pub struct BoxView<T: View> {
pub struct Resized<T: View> {
/// Constraint on each axis
size: XY<SizeConstraint>,
@ -34,8 +34,8 @@ pub struct BoxView<T: View> {
view: T,
}
impl<T: View> BoxView<T> {
/// Creates a new `BoxView` with the given width and height requirements.
impl<T: View> Resized<T> {
/// Creates a new `Resized` with the given width and height requirements.
///
/// `None` values will use the wrapped view's preferences.
pub fn new(
@ -43,7 +43,7 @@ impl<T: View> BoxView<T> {
height: SizeConstraint,
view: T,
) -> Self {
BoxView {
Resized {
size: (width, height).into(),
invalidated: true,
view,
@ -76,102 +76,102 @@ impl<T: View> BoxView<T> {
self.invalidate();
}
/// Wraps `view` in a new `BoxView` with the given size.
/// Wraps `view` in a new `Resized` with the given size.
pub fn with_fixed_size<S: Into<Vec2>>(size: S, view: T) -> Self {
let size = size.into();
BoxView::new(
Resized::new(
SizeConstraint::Fixed(size.x),
SizeConstraint::Fixed(size.y),
view,
)
}
/// Wraps `view` in a new `BoxView` with fixed width.
/// Wraps `view` in a new `Resized` with fixed width.
pub fn with_fixed_width(width: usize, view: T) -> Self {
BoxView::new(SizeConstraint::Fixed(width), SizeConstraint::Free, view)
Resized::new(SizeConstraint::Fixed(width), SizeConstraint::Free, view)
}
/// Wraps `view` in a new `BoxView` with fixed height.
/// Wraps `view` in a new `Resized` with fixed height.
pub fn with_fixed_height(height: usize, view: T) -> Self {
BoxView::new(SizeConstraint::Free, SizeConstraint::Fixed(height), view)
Resized::new(SizeConstraint::Free, SizeConstraint::Fixed(height), view)
}
/// Wraps `view` in a `BoxView` which will take all available space.
/// Wraps `view` in a `Resized` which will take all available space.
pub fn with_full_screen(view: T) -> Self {
BoxView::new(SizeConstraint::Full, SizeConstraint::Full, view)
Resized::new(SizeConstraint::Full, SizeConstraint::Full, view)
}
/// Wraps `view` in a `BoxView` which will take all available width.
/// Wraps `view` in a `Resized` which will take all available width.
pub fn with_full_width(view: T) -> Self {
BoxView::new(SizeConstraint::Full, SizeConstraint::Free, view)
Resized::new(SizeConstraint::Full, SizeConstraint::Free, view)
}
/// Wraps `view` in a `BoxView` which will take all available height.
/// Wraps `view` in a `Resized` which will take all available height.
pub fn with_full_height(view: T) -> Self {
BoxView::new(SizeConstraint::Free, SizeConstraint::Full, view)
Resized::new(SizeConstraint::Free, SizeConstraint::Full, view)
}
/// Wraps `view` in a `BoxView` which will never be bigger than `size`.
/// Wraps `view` in a `Resized` which will never be bigger than `size`.
pub fn with_max_size<S: Into<Vec2>>(size: S, view: T) -> Self {
let size = size.into();
BoxView::new(
Resized::new(
SizeConstraint::AtMost(size.x),
SizeConstraint::AtMost(size.y),
view,
)
}
/// Wraps `view` in a `BoxView` which will enforce a maximum width.
/// Wraps `view` in a `Resized` which will enforce a maximum width.
///
/// The resulting width will never be more than `max_width`.
pub fn with_max_width(max_width: usize, view: T) -> Self {
BoxView::new(
Resized::new(
SizeConstraint::AtMost(max_width),
SizeConstraint::Free,
view,
)
}
/// Wraps `view` in a `BoxView` which will enforce a maximum height.
/// Wraps `view` in a `Resized` which will enforce a maximum height.
///
/// The resulting height will never be more than `max_height`.
pub fn with_max_height(max_height: usize, view: T) -> Self {
BoxView::new(
Resized::new(
SizeConstraint::Free,
SizeConstraint::AtMost(max_height),
view,
)
}
/// Wraps `view` in a `BoxView` which will never be smaller than `size`.
/// Wraps `view` in a `Resized` which will never be smaller than `size`.
pub fn with_min_size<S: Into<Vec2>>(size: S, view: T) -> Self {
let size = size.into();
BoxView::new(
Resized::new(
SizeConstraint::AtLeast(size.x),
SizeConstraint::AtLeast(size.y),
view,
)
}
/// Wraps `view` in a `BoxView` which will enforce a minimum width.
/// Wraps `view` in a `Resized` which will enforce a minimum width.
///
/// The resulting width will never be less than `min_width`.
pub fn with_min_width(min_width: usize, view: T) -> Self {
BoxView::new(
Resized::new(
SizeConstraint::AtLeast(min_width),
SizeConstraint::Free,
view,
)
}
/// Wraps `view` in a `BoxView` which will enforce a minimum height.
/// Wraps `view` in a `Resized` which will enforce a minimum height.
///
/// The resulting height will never be less than `min_height`.
pub fn with_min_height(min_height: usize, view: T) -> Self {
BoxView::new(
Resized::new(
SizeConstraint::Free,
SizeConstraint::AtLeast(min_height),
view,
@ -186,7 +186,7 @@ impl<T: View> BoxView<T> {
inner_getters!(self.view: T);
}
impl<T: View> ViewWrapper for BoxView<T> {
impl<T: View> ViewWrapper for Resized<T> {
wrap_impl!(self.view: T);
fn wrap_draw(&self, printer: &Printer) {

View File

@ -4,22 +4,22 @@ use crate::view::{scroll, ScrollStrategy, Selector, View};
use crate::{Printer, Rect, Vec2, With};
/// Wraps a view in a scrollable area.
pub struct ScrollView<V> {
pub struct Scroll<V> {
/// The wrapped view.
inner: V,
core: scroll::Core,
}
impl_scroller!(ScrollView<V>::core);
impl_scroller!(Scroll<V>::core);
impl<V> ScrollView<V>
impl<V> Scroll<V>
where
V: View,
{
/// Creates a new ScrollView around `view`.
/// Creates a new Scroll around `view`.
pub fn new(inner: V) -> Self {
ScrollView {
Scroll {
inner,
core: scroll::Core::new(),
}
@ -34,7 +34,7 @@ where
/// phase.
///
/// This is only the size the content _thinks_ it has, and may be larger
/// than the actual size used by this `ScrollView`.
/// than the actual size used by this `Scroll`.
pub fn inner_size(&self) -> Vec2 {
self.core.inner_size()
}
@ -169,7 +169,7 @@ where
inner_getters!(self.inner: V);
}
impl<V> View for ScrollView<V>
impl<V> View for Scroll<V>
where
V: View,
{

View File

@ -26,9 +26,9 @@ use std::rc::Rc;
///
/// ```rust
/// # use cursive::Cursive;
/// # use cursive::views::{SelectView, Dialog, TextView};
/// # use cursive::views::{Select, Dialog, Text};
/// # use cursive::align::HAlign;
/// let mut time_select = SelectView::new().h_align(HAlign::Center);
/// let mut time_select = Select::new().h_align(HAlign::Center);
/// time_select.add_item("Short", 1);
/// time_select.add_item("Medium", 5);
/// time_select.add_item("Long", 10);
@ -36,7 +36,7 @@ use std::rc::Rc;
/// time_select.set_on_submit(|s, time| {
/// s.pop_layer();
/// let text = format!("You will wait for {} minutes...", time);
/// s.add_layer(Dialog::around(TextView::new(text))
/// s.add_layer(Dialog::around(Text::new(text))
/// .button("Quit", |s| s.quit()));
/// });
///
@ -44,7 +44,7 @@ use std::rc::Rc;
/// siv.add_layer(Dialog::around(time_select)
/// .title("How long is your wait?"));
/// ```
pub struct SelectView<T = String> {
pub struct Select<T = String> {
// The core of the view: we store a list of items
// `Item` is more or less a `(String, Rc<T>)`.
items: Vec<Item<T>>,
@ -78,16 +78,16 @@ pub struct SelectView<T = String> {
last_size: Vec2,
}
impl<T: 'static> Default for SelectView<T> {
impl<T: 'static> Default for Select<T> {
fn default() -> Self {
Self::new()
}
}
impl<T: 'static> SelectView<T> {
/// Creates a new empty SelectView.
impl<T: 'static> Select<T> {
/// Creates a new empty Select.
pub fn new() -> Self {
SelectView {
Select {
items: Vec::new(),
enabled: true,
focus: Rc::new(Cell::new(0)),
@ -176,11 +176,11 @@ impl<T: 'static> SelectView<T> {
///
/// ```
/// use cursive::traits::Identifiable;
/// use cursive::views::{TextView, SelectView};
/// use cursive::views::{Text, Select};
///
/// let text_view = TextView::new("").with_id("text");
/// let text_view = Text::new("").with_id("text");
///
/// let select_view = SelectView::new()
/// let select_view = Select::new()
/// .item("One", 1)
/// .item("Two", 2)
/// .on_select(|s, item| {
@ -191,7 +191,7 @@ impl<T: 'static> SelectView<T> {
/// };
///
/// // Update the textview with the currently selected item.
/// s.call_on_id("text", |v: &mut TextView| {
/// s.call_on_id("text", |v: &mut Text| {
/// v.set_content(content);
/// }).unwrap();
/// });
@ -231,9 +231,9 @@ impl<T: 'static> SelectView<T> {
/// # Examples
///
/// ```
/// use cursive::views::{Dialog, SelectView};
/// use cursive::views::{Dialog, Select};
///
/// let select_view = SelectView::new()
/// let select_view = Select::new()
/// .item("One", 1)
/// .item("Two", 2)
/// .on_submit(|s, item| {
@ -261,9 +261,9 @@ impl<T: 'static> SelectView<T> {
///
/// ```
/// use cursive::align;
/// use cursive::views::SelectView;
/// use cursive::views::Select;
///
/// let select_view = SelectView::new()
/// let select_view = Select::new()
/// .item("One", 1)
/// .align(align::Align::top_center());
/// ```
@ -311,9 +311,9 @@ impl<T: 'static> SelectView<T> {
/// # Examples
///
/// ```
/// use cursive::views::SelectView;
/// use cursive::views::Select;
///
/// let mut select_view = SelectView::new();
/// let mut select_view = Select::new();
///
/// select_view.add_item("Item 1", 1);
/// select_view.add_item("Item 2", 2);
@ -326,8 +326,8 @@ impl<T: 'static> SelectView<T> {
///
/// ```
/// use cursive::Cursive;
/// use cursive::views::{SelectView, TextView};
/// let select = SelectView::new()
/// use cursive::views::{Select, Text};
/// let select = Select::new()
/// .item("Short", 1);
/// assert_eq!(select.get_item(0), Some(("Short", &1)));
/// ```
@ -391,9 +391,9 @@ impl<T: 'static> SelectView<T> {
/// # Examples
///
/// ```
/// use cursive::views::SelectView;
/// use cursive::views::Select;
///
/// let select_view = SelectView::new()
/// let select_view = Select::new()
/// .item("Item 1", 1)
/// .item("Item 2", 2)
/// .item("Surprise item", 42);
@ -420,10 +420,10 @@ impl<T: 'static> SelectView<T> {
/// # Examples
///
/// ```
/// use cursive::views::SelectView;
/// use cursive::views::Select;
///
/// // Create a SelectView with 100 items
/// let select_view = SelectView::new()
/// // Create a Select with 100 items
/// let select_view = Select::new()
/// .with_all((1u8..100).into_iter().map(|i| {
/// (format!("Item {}", i), i)
/// }));
@ -463,9 +463,9 @@ impl<T: 'static> SelectView<T> {
/// # Examples
///
/// ```
/// use cursive::views::SelectView;
/// use cursive::views::Select;
///
/// let select_view = SelectView::new()
/// let select_view = Select::new()
/// .item("Item 1", 1)
/// .item("Item 2", 2)
/// .item("Item 3", 3);
@ -481,9 +481,9 @@ impl<T: 'static> SelectView<T> {
/// # Examples
///
/// ```
/// use cursive::views::SelectView;
/// use cursive::views::Select;
///
/// let mut select_view = SelectView::new();
/// let mut select_view = Select::new();
/// assert!(select_view.is_empty());
///
/// select_view.add_item("Item 1", 1);
@ -551,7 +551,7 @@ impl<T: 'static> SelectView<T> {
/// You should run this callback with a `&mut Cursive`.
pub fn set_selection(&mut self, i: usize) -> Callback {
// TODO: Check if `i >= self.len()` ?
// assert!(i < self.len(), "SelectView: trying to select out-of-bound");
// assert!(i < self.len(), "Select: trying to select out-of-bound");
// Or just cap the ID?
let i = if self.is_empty() {
0
@ -582,8 +582,8 @@ impl<T: 'static> SelectView<T> {
///
/// ```rust
/// # use cursive::Cursive;
/// # use cursive::views::SelectView;
/// fn select_up(siv: &mut Cursive, view: &mut SelectView<()>) {
/// # use cursive::views::Select;
/// fn select_up(siv: &mut Cursive, view: &mut Select<()>) {
/// let cb = view.select_up(1);
/// cb(siv);
/// }
@ -777,7 +777,7 @@ impl<T: 'static> SelectView<T> {
}
}
impl SelectView<String> {
impl Select<String> {
/// Convenient method to use the label as value.
pub fn add_item_str<S: Into<String>>(&mut self, label: S) {
let label = label.into();
@ -789,9 +789,9 @@ impl SelectView<String> {
/// # Examples
///
/// ```
/// use cursive::views::SelectView;
/// use cursive::views::Select;
///
/// let select_view = SelectView::new()
/// let select_view = Select::new()
/// .item_str("Paris")
/// .item_str("New York")
/// .item_str("Tokyo");
@ -814,8 +814,8 @@ impl SelectView<String> {
/// # Examples
///
/// ```
/// # use cursive::views::SelectView;
/// let mut select_view = SelectView::new();
/// # use cursive::views::Select;
/// let mut select_view = Select::new();
/// select_view.add_all_str(vec!["a", "b", "c"]);
/// ```
pub fn add_all_str<S, I>(&mut self, iter: I)
@ -835,11 +835,11 @@ impl SelectView<String> {
/// # Examples
///
/// ```
/// use cursive::views::SelectView;
/// use cursive::views::Select;
///
/// let text = "..."; // Maybe read some config file
///
/// let select_view = SelectView::new()
/// let select_view = Select::new()
/// .with_all_str(text.lines());
/// ```
pub fn with_all_str<S, I>(self, iter: I) -> Self
@ -851,7 +851,7 @@ impl SelectView<String> {
}
}
impl<T: 'static> SelectView<T>
impl<T: 'static> Select<T>
where
T: Ord,
{
@ -866,7 +866,7 @@ where
}
}
impl<T: 'static> View for SelectView<T> {
impl<T: 'static> View for Select<T> {
fn draw(&self, printer: &Printer<'_, '_>) {
self.last_offset.set(printer.offset);
@ -987,7 +987,7 @@ mod tests {
#[test]
fn select_view_sorting() {
// We add items in no particular order, from going by their label.
let mut view = SelectView::new();
let mut view = Select::new();
view.add_item_str("Y");
view.add_item_str("Z");
view.add_item_str("X");
@ -1009,7 +1009,7 @@ mod tests {
#[test]
fn select_view_sorting_with_comparator() {
// We add items in no particular order, from going by their value.
let mut view = SelectView::new();
let mut view = Select::new();
view.add_item("Y", 2);
view.add_item("Z", 1);
view.add_item("X", 3);
@ -1036,7 +1036,7 @@ mod tests {
key: i32,
}
let mut view = SelectView::new();
let mut view = Select::new();
view.add_item("Y", MyStruct { key: 2 });
view.add_item("Z", MyStruct { key: 1 });
view.add_item("X", MyStruct { key: 3 });
@ -1058,7 +1058,7 @@ mod tests {
#[test]
fn select_view_sorting_orderable_items() {
// We add items in no particular order, from going by their value.
let mut view = SelectView::new();
let mut view = Select::new();
view.add_item("Y", 2);
view.add_item("Z", 1);
view.add_item("X", 3);

View File

@ -7,17 +7,17 @@ use crate::Printer;
/// Wrapper view that adds a shadow.
///
/// It reserves a 1 pixel border on each side.
pub struct ShadowView<T: View> {
pub struct Shadow<T: View> {
view: T,
top_padding: bool,
left_padding: bool,
// TODO: invalidate if we change the padding?
}
impl<T: View> ShadowView<T> {
impl<T: View> Shadow<T> {
/// Wraps the given view.
pub fn new(view: T) -> Self {
ShadowView {
Shadow {
view,
top_padding: true,
left_padding: true,
@ -51,7 +51,7 @@ impl<T: View> ShadowView<T> {
inner_getters!(self.view: T);
}
impl<T: View> ViewWrapper for ShadowView<T> {
impl<T: View> ViewWrapper for Shadow<T> {
wrap_impl!(self.view: T);
fn wrap_required_size(&mut self, req: Vec2) -> Vec2 {

View File

@ -14,9 +14,9 @@ use std::rc::Rc;
/// # Examples
///
/// ```
/// use cursive::views::{Dialog, SliderView};
/// use cursive::views::{Dialog, Slider};
///
/// let slider_view = SliderView::horizontal(10)
/// let slider_view = Slider::horizontal(10)
/// .on_change(|s, n| if n == 5 {
/// s.add_layer(Dialog::info("5! Pick 5!"));
/// })
@ -25,7 +25,7 @@ use std::rc::Rc;
/// n => s.add_layer(Dialog::info(format!("Why {}? Why not 5?", n))),
/// });
/// ```
pub struct SliderView {
pub struct Slider {
orientation: Orientation,
on_change: Option<Rc<dyn Fn(&mut Cursive, usize)>>,
on_enter: Option<Rc<dyn Fn(&mut Cursive, usize)>>,
@ -34,15 +34,15 @@ pub struct SliderView {
dragging: bool,
}
impl SliderView {
/// Creates a new `SliderView` in the given orientation.
impl Slider {
/// Creates a new `Slider` in the given orientation.
///
/// The view will have a fixed length of `max_value`,
/// with one tick per block.
///
/// The actual range of values for this slider is `[0, max_value - 1]`.
pub fn new(orientation: Orientation, max_value: usize) -> Self {
SliderView {
Slider {
orientation,
value: 0,
max_value,
@ -52,12 +52,12 @@ impl SliderView {
}
}
/// Creates a new vertical `SliderView`.
/// Creates a new vertical `Slider`.
pub fn vertical(max_value: usize) -> Self {
Self::new(Orientation::Vertical, max_value)
}
/// Creates a new horizontal `SliderView`.
/// Creates a new horizontal `Slider`.
pub fn horizontal(max_value: usize) -> Self {
Self::new(Orientation::Horizontal, max_value)
}
@ -140,7 +140,7 @@ impl SliderView {
}
}
impl View for SliderView {
impl View for Slider {
fn draw(&self, printer: &Printer<'_, '_>) {
match self.orientation {
Orientation::Vertical => {

View File

@ -5,7 +5,7 @@ use crate::vec::Vec2;
use crate::view::{
IntoBoxedView, Offset, Position, Selector, View, ViewWrapper,
};
use crate::views::{CircularFocus, Layer, ShadowView, ViewBox};
use crate::views::{Boxed, CircularFocus, Layer, Shadow};
use crate::Printer;
use crate::With;
use std::cell;
@ -13,7 +13,7 @@ use std::ops::Deref;
/// Simple stack of views.
/// Only the top-most view is active and can receive input.
pub struct StackView {
pub struct Stack {
// Store layers from back to front.
layers: Vec<Child>,
last_size: Vec2,
@ -31,7 +31,7 @@ enum Placement {
Fullscreen,
}
/// Identifies a layer in a `StackView`.
/// Identifies a layer in a `Stack`.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum LayerPosition {
/// Starts from the back (bottom) of the stack.
@ -64,7 +64,7 @@ impl Placement {
/// A child view can be wrapped in multiple ways.
enum ChildWrapper<T: View> {
// Some views include a shadow around.
Shadow(ShadowView<Layer<CircularFocus<T>>>),
Shadow(Shadow<Layer<CircularFocus<T>>>),
// Some include only include a background.
Backfilled(Layer<CircularFocus<T>>),
@ -77,7 +77,7 @@ impl<T: View> ChildWrapper<T> {
fn unwrap(self) -> T {
match self {
// All these into_inner() can never fail.
// (ShadowView, Layer, CircularFocus)
// (Shadow, Layer, CircularFocus)
ChildWrapper::Shadow(shadow) => shadow
.into_inner()
.ok()
@ -199,7 +199,7 @@ impl<T: View> View for ChildWrapper<T> {
}
struct Child {
view: ChildWrapper<ViewBox>,
view: ChildWrapper<Boxed>,
size: Vec2,
placement: Placement,
@ -210,24 +210,24 @@ struct Child {
virgin: bool,
}
new_default!(StackView);
new_default!(Stack);
impl StackView {
/// Creates a new empty StackView
impl Stack {
/// Creates a new empty Stack
pub fn new() -> Self {
StackView {
Stack {
layers: Vec::new(),
last_size: Vec2::zero(),
bg_dirty: cell::Cell::new(true),
}
}
/// Returns the number of layers in this `StackView`.
/// Returns the number of layers in this `Stack`.
pub fn len(&self) -> usize {
self.layers.len()
}
/// Returns `true` if there is no layer in this `StackView`.
/// Returns `true` if there is no layer in this `Stack`.
pub fn is_empty(&self) -> bool {
self.layers.is_empty()
}
@ -250,7 +250,7 @@ impl StackView {
where
T: IntoBoxedView,
{
let boxed = ViewBox::boxed(view);
let boxed = Boxed::boxed(view);
self.layers.push(Child {
view: ChildWrapper::Backfilled(Layer::new(
CircularFocus::wrap_tab(boxed),
@ -308,12 +308,12 @@ impl StackView {
/// # Examples
///
/// ```rust
/// # use cursive::views::{TextView, StackView, Dialog, LayerPosition};
/// # use cursive::views::{Text, Stack, Dialog, LayerPosition};
/// # use cursive::view::Identifiable;
/// let mut stack = StackView::new();
/// stack.add_layer(TextView::new("Back"));
/// stack.add_layer(Dialog::around(TextView::new("Middle").with_id("text")));
/// stack.add_layer(TextView::new("Front"));
/// let mut stack = Stack::new();
/// stack.add_layer(Text::new("Back"));
/// stack.add_layer(Dialog::around(Text::new("Middle").with_id("text")));
/// stack.add_layer(Text::new("Front"));
///
/// assert_eq!(stack.find_layer_from_id("text"), Some(LayerPosition::FromBack(1)));
/// ```
@ -356,11 +356,11 @@ impl StackView {
where
T: IntoBoxedView,
{
let boxed = ViewBox::boxed(view);
let boxed = Boxed::boxed(view);
self.layers.push(Child {
// Skip padding for absolute/parent-placed views
view: ChildWrapper::Shadow(
ShadowView::new(Layer::new(CircularFocus::wrap_tab(boxed)))
Shadow::new(Layer::new(CircularFocus::wrap_tab(boxed)))
.top_padding(position.y == Offset::Center)
.left_padding(position.x == Offset::Center),
),
@ -383,7 +383,7 @@ impl StackView {
where
T: IntoBoxedView,
{
let boxed = ViewBox::boxed(view);
let boxed = Boxed::boxed(view);
self.layers.push(Child {
view: ChildWrapper::Plain(CircularFocus::wrap_tab(boxed)),
size: Vec2::new(0, 0),
@ -402,7 +402,7 @@ impl StackView {
self.with(|s| s.add_layer_at(position, view))
}
/// Remove a layer from this `StackView`.
/// Remove a layer from this `Stack`.
///
/// # Panics
///
@ -419,7 +419,7 @@ impl StackView {
.pop()
.map(|child| child.view)
.map(ChildWrapper::unwrap)
.map(ViewBox::unwrap)
.map(Boxed::unwrap)
}
/// Computes the offset of the current top view.
@ -596,7 +596,7 @@ where
}
}
impl View for StackView {
impl View for Stack {
fn draw(&self, printer: &Printer<'_, '_>) {
// This function is included for compat with the view trait,
// it should behave the same as calling them seperately, but does
@ -685,12 +685,12 @@ impl View for StackView {
#[cfg(test)]
mod tests {
use super::*;
use crate::views::TextView;
use crate::views::Text;
#[test]
fn pop_add() {
// Start with a simple stack
let mut stack = StackView::new().layer(TextView::new("1"));
let mut stack = Stack::new().layer(Text::new("1"));
// And keep poping and re-pushing the view
for _ in 0..20 {
@ -700,18 +700,18 @@ mod tests {
// We want to make sure we don't add any layer of Box'ing
let layer = stack.pop_layer().unwrap();
let text: Box<TextView> = layer.as_boxed_any().downcast().unwrap();
let text: Box<Text> = layer.as_boxed_any().downcast().unwrap();
assert_eq!(text.get_content().source(), "1");
}
#[test]
fn move_layer_works() {
let mut stack = StackView::new()
.layer(TextView::new("1"))
.layer(TextView::new("2"))
.layer(TextView::new("3"))
.layer(TextView::new("4"));
let mut stack = Stack::new()
.layer(Text::new("1"))
.layer(Text::new("2"))
.layer(Text::new("3"))
.layer(Text::new("4"));
// Try moving views around, make sure we have the expected result
@ -734,19 +734,19 @@ mod tests {
// 1,2,4,3
let layer = stack.pop_layer().unwrap();
let text: Box<TextView> = layer.as_boxed_any().downcast().unwrap();
let text: Box<Text> = layer.as_boxed_any().downcast().unwrap();
assert_eq!(text.get_content().source(), "3");
let layer = stack.pop_layer().unwrap();
let text: Box<TextView> = layer.as_boxed_any().downcast().unwrap();
let text: Box<Text> = layer.as_boxed_any().downcast().unwrap();
assert_eq!(text.get_content().source(), "4");
let layer = stack.pop_layer().unwrap();
let text: Box<TextView> = layer.as_boxed_any().downcast().unwrap();
let text: Box<Text> = layer.as_boxed_any().downcast().unwrap();
assert_eq!(text.get_content().source(), "2");
let layer = stack.pop_layer().unwrap();
let text: Box<TextView> = layer.as_boxed_any().downcast().unwrap();
let text: Box<Text> = layer.as_boxed_any().downcast().unwrap();
assert_eq!(text.get_content().source(), "1");
assert!(stack.pop_layer().is_none());
@ -754,29 +754,28 @@ mod tests {
#[test]
fn get() {
let mut stack = StackView::new()
.layer(TextView::new("1"))
.layer(TextView::new("2"));
let mut stack =
Stack::new().layer(Text::new("1")).layer(Text::new("2"));
assert!(stack
.get(LayerPosition::FromFront(0))
.unwrap()
.as_any()
.is::<TextView>());
.is::<Text>());
assert!(stack
.get(LayerPosition::FromBack(0))
.unwrap()
.as_any()
.is::<TextView>());
.is::<Text>());
assert!(stack
.get_mut(LayerPosition::FromFront(0))
.unwrap()
.as_any_mut()
.is::<TextView>());
.is::<Text>());
assert!(stack
.get_mut(LayerPosition::FromBack(0))
.unwrap()
.as_any_mut()
.is::<TextView>());
.is::<Text>());
}
}

View File

@ -15,18 +15,18 @@ use crate::{Printer, Vec2, With, XY};
// Content type used internally for caching and storage
type InnerContentType = Arc<StyledString>;
/// Provides access to the content of a [`TextView`].
/// Provides access to the content of a [`Text`].
///
/// Cloning this object will still point to the same content.
///
/// [`TextView`]: struct.TextView.html
/// [`Text`]: struct.Text.html
///
/// # Examples
///
/// ```rust
/// # use cursive::views::{TextView, TextContent};
/// # use cursive::views::{Text, TextContent};
/// let mut content = TextContent::new("content");
/// let view = TextView::new_with_content(content.clone());
/// let view = Text::new_with_content(content.clone());
///
/// // Later, possibly in a different thread
/// content.set_content("new content");
@ -93,7 +93,7 @@ impl TextContent {
});
}
/// Append `content` to the end of a `TextView`.
/// Append `content` to the end of a `Text`.
pub fn append<S>(&self, content: S)
where
S: Into<StyledString>,
@ -128,7 +128,7 @@ impl TextContent {
}
}
/// Internel representation of the content for a `TextView`.
/// Internel representation of the content for a `Text`.
///
/// This is mostly just a `StyledString`.
///
@ -175,12 +175,12 @@ impl TextContentInner {
///
/// ```rust
/// # use cursive::Cursive;
/// # use cursive::views::TextView;
/// # use cursive::views::Text;
/// let mut siv = Cursive::dummy();
///
/// siv.add_layer(TextView::new("Hello world!"));
/// siv.add_layer(Text::new("Hello world!"));
/// ```
pub struct TextView {
pub struct Text {
// content: String,
content: TextContent,
rows: Vec<Row>,
@ -196,8 +196,8 @@ pub struct TextView {
width: Option<usize>,
}
impl TextView {
/// Creates a new TextView with the given content.
impl Text {
/// Creates a new Text with the given content.
pub fn new<S>(content: S) -> Self
where
S: Into<StyledString>,
@ -205,7 +205,7 @@ impl TextView {
Self::new_with_content(TextContent::new(content))
}
/// Creates a new TextView using the given `Arc<Mutex<String>>`.
/// Creates a new Text using the given `Arc<Mutex<String>>`.
///
/// If you kept a clone of the given content, you'll be able to update it
/// remotely.
@ -213,16 +213,16 @@ impl TextView {
/// # Examples
///
/// ```rust
/// # use cursive::views::{TextView, TextContent};
/// # use cursive::views::{Text, TextContent};
/// let mut content = TextContent::new("content");
/// let view = TextView::new_with_content(content.clone());
/// let view = Text::new_with_content(content.clone());
///
/// // Later, possibly in a different thread
/// content.set_content("new content");
/// assert!(view.get_content().source().contains("new"));
/// ```
pub fn new_with_content(content: TextContent) -> Self {
TextView {
Text {
content,
effect: Effect::Simple,
rows: Vec::new(),
@ -233,9 +233,9 @@ impl TextView {
}
}
/// Creates a new empty `TextView`.
/// Creates a new empty `Text`.
pub fn empty() -> Self {
TextView::new("")
Text::new("")
}
/// Sets the effect for the entire content.
@ -309,7 +309,7 @@ impl TextView {
self.content.set_content(content);
}
/// Append `content` to the end of a `TextView`.
/// Append `content` to the end of a `Text`.
pub fn append<S>(&mut self, content: S)
where
S: Into<StyledString>,
@ -359,7 +359,7 @@ impl TextView {
}
}
impl View for TextView {
impl View for Text {
fn draw(&self, printer: &Printer<'_, '_>) {
let h = self.rows.len();
// If the content is smaller than the view, align it somewhere.

View File

@ -1,40 +1,40 @@
use crate::vec::Vec2;
use crate::view::{View, ViewWrapper};
use crate::views::IdView;
use crate::views::Named;
use crate::Printer;
use std::cell::Cell;
/// Wrapper around a view that remembers its position.
pub struct TrackedView<T: View> {
pub struct Tracked<T: View> {
/// Wrapped view.
pub view: T,
/// Last position the view was located.
offset: Cell<Vec2>,
}
impl<T: View> TrackedView<T> {
impl<T: View> Tracked<T> {
/// Return the last offset at which the view was drawn.
pub fn offset(&self) -> Vec2 {
self.offset.get()
}
/// Creates a new `TrackedView` around `view`.
/// Creates a new `Tracked` around `view`.
pub fn new(view: T) -> Self {
TrackedView {
Tracked {
view,
offset: Cell::new(Vec2::zero()),
}
}
/// Wraps itself in a `IdView` for easy retrieval.
pub fn with_id(self, id: &str) -> IdView<Self> {
IdView::new(id, self)
/// Wraps itself in a `Named` for easy retrieval.
pub fn with_id(self, id: &str) -> Named<Self> {
Named::new(id, self)
}
inner_getters!(self.view: T);
}
impl<T: View> ViewWrapper for TrackedView<T> {
impl<T: View> ViewWrapper for Tracked<T> {
wrap_impl!(self.view: T);
fn wrap_draw(&self, printer: &Printer<'_, '_>) {