mirror of
https://github.com/FliegendeWurst/cursive.git
synced 2024-11-23 17:35:00 +00:00
Big renaming: part 1
This commit is contained in:
parent
d246d69c02
commit
c86eaa5875
@ -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.
|
/// It uses a list of screen, with one screen active at a time.
|
||||||
pub struct Cursive {
|
pub struct Cursive {
|
||||||
theme: theme::Theme,
|
theme: theme::Theme,
|
||||||
screens: Vec<views::StackView>,
|
screens: Vec<views::Stack>,
|
||||||
global_callbacks: HashMap<Event, Vec<Callback>>,
|
global_callbacks: HashMap<Event, Vec<Callback>>,
|
||||||
menubar: views::Menubar,
|
menubar: views::Menubar,
|
||||||
|
|
||||||
@ -153,7 +153,7 @@ impl Cursive {
|
|||||||
|
|
||||||
backend_init().map(|backend| Cursive {
|
backend_init().map(|backend| Cursive {
|
||||||
theme,
|
theme,
|
||||||
screens: vec![views::StackView::new()],
|
screens: vec![views::Stack::new()],
|
||||||
last_sizes: Vec::new(),
|
last_sizes: Vec::new(),
|
||||||
global_callbacks: HashMap::default(),
|
global_callbacks: HashMap::default(),
|
||||||
menubar: views::Menubar::new(),
|
menubar: views::Menubar::new(),
|
||||||
@ -286,9 +286,9 @@ impl Cursive {
|
|||||||
pub fn show_debug_console(&mut self) {
|
pub fn show_debug_console(&mut self) {
|
||||||
self.add_layer(
|
self.add_layer(
|
||||||
views::Dialog::around(
|
views::Dialog::around(
|
||||||
views::ScrollView::new(views::IdView::new(
|
views::Scroll::new(views::Named::new(
|
||||||
DEBUG_VIEW_ID,
|
DEBUG_VIEW_ID,
|
||||||
views::DebugView::new(),
|
views::DebugConsole::new(),
|
||||||
))
|
))
|
||||||
.scroll_x(true),
|
.scroll_x(true),
|
||||||
)
|
)
|
||||||
@ -466,13 +466,13 @@ impl Cursive {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a reference to the currently active screen.
|
/// 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;
|
let id = self.active_screen;
|
||||||
&self.screens[id]
|
&self.screens[id]
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns a mutable reference to the currently active screen.
|
/// 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;
|
let id = self.active_screen;
|
||||||
&mut self.screens[id]
|
&mut self.screens[id]
|
||||||
}
|
}
|
||||||
@ -485,7 +485,7 @@ impl Cursive {
|
|||||||
/// Adds a new screen, and returns its ID.
|
/// Adds a new screen, and returns its ID.
|
||||||
pub fn add_screen(&mut self) -> ScreenId {
|
pub fn add_screen(&mut self) -> ScreenId {
|
||||||
let res = self.screens.len();
|
let res = self.screens.len();
|
||||||
self.screens.push(views::StackView::new());
|
self.screens.push(views::Stack::new());
|
||||||
res
|
res
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -575,9 +575,9 @@ impl Cursive {
|
|||||||
self.call_on(&view::Selector::Id(id), callback)
|
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
|
/// a [`ViewRef`] to the wrapped view. The `ViewRef` implements
|
||||||
/// `DerefMut<Target=T>`, so you can treat it just like a `&mut T`.
|
/// `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());
|
/// assert!(siv.find_id::<SelectView<u32>>("select").is_some());
|
||||||
/// ```
|
/// ```
|
||||||
///
|
///
|
||||||
/// [`IdView`]: views/struct.IdView.html
|
/// [`Named`]: views/struct.Named.html
|
||||||
/// [`ViewRef`]: views/type.ViewRef.html
|
/// [`ViewRef`]: views/type.ViewRef.html
|
||||||
pub fn find_id<V>(&mut self, id: &str) -> Option<views::ViewRef<V>>
|
pub fn find_id<V>(&mut self, id: &str) -> Option<views::ViewRef<V>>
|
||||||
where
|
where
|
||||||
V: View + Any,
|
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`.
|
/// Moves the focus to the view identified by `id`.
|
||||||
|
@ -101,8 +101,10 @@ mod utf8;
|
|||||||
pub mod backend;
|
pub mod backend;
|
||||||
|
|
||||||
pub use self::cursive::{CbSink, Cursive, ScreenId};
|
pub use self::cursive::{CbSink, Cursive, ScreenId};
|
||||||
|
pub use self::direction::Direction;
|
||||||
pub use self::printer::Printer;
|
pub use self::printer::Printer;
|
||||||
pub use self::rect::Rect;
|
pub use self::rect::Rect;
|
||||||
pub use self::vec::Vec2;
|
pub use self::vec::Vec2;
|
||||||
|
pub use self::view::View;
|
||||||
pub use self::with::With;
|
pub use self::with::With;
|
||||||
pub use self::xy::XY;
|
pub use self::xy::XY;
|
||||||
|
@ -1,78 +1,78 @@
|
|||||||
use crate::vec::Vec2;
|
use crate::vec::Vec2;
|
||||||
use crate::view::{SizeConstraint, View};
|
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 {
|
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(
|
fn boxed(
|
||||||
self,
|
self,
|
||||||
width: SizeConstraint,
|
width: SizeConstraint,
|
||||||
height: SizeConstraint,
|
height: SizeConstraint,
|
||||||
) -> BoxView<Self> {
|
) -> Resized<Self> {
|
||||||
BoxView::new(width, height, self)
|
Resized::new(width, height, self)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Wraps `self` into a fixed-size `BoxView`.
|
/// Wraps `self` into a fixed-size `Resized`.
|
||||||
fn fixed_size<S: Into<Vec2>>(self, size: S) -> BoxView<Self> {
|
fn fixed_size<S: Into<Vec2>>(self, size: S) -> Resized<Self> {
|
||||||
BoxView::with_fixed_size(size, self)
|
Resized::with_fixed_size(size, self)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Wraps `self` into a fixed-width `BoxView`.
|
/// Wraps `self` into a fixed-width `Resized`.
|
||||||
fn fixed_width(self, width: usize) -> BoxView<Self> {
|
fn fixed_width(self, width: usize) -> Resized<Self> {
|
||||||
BoxView::with_fixed_width(width, self)
|
Resized::with_fixed_width(width, self)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Wraps `self` into a fixed-width `BoxView`.
|
/// Wraps `self` into a fixed-width `Resized`.
|
||||||
fn fixed_height(self, height: usize) -> BoxView<Self> {
|
fn fixed_height(self, height: usize) -> Resized<Self> {
|
||||||
BoxView::with_fixed_height(height, self)
|
Resized::with_fixed_height(height, self)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Wraps `self` into a full-screen `BoxView`.
|
/// Wraps `self` into a full-screen `Resized`.
|
||||||
fn full_screen(self) -> BoxView<Self> {
|
fn full_screen(self) -> Resized<Self> {
|
||||||
BoxView::with_full_screen(self)
|
Resized::with_full_screen(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Wraps `self` into a full-width `BoxView`.
|
/// Wraps `self` into a full-width `Resized`.
|
||||||
fn full_width(self) -> BoxView<Self> {
|
fn full_width(self) -> Resized<Self> {
|
||||||
BoxView::with_full_width(self)
|
Resized::with_full_width(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Wraps `self` into a full-height `BoxView`.
|
/// Wraps `self` into a full-height `Resized`.
|
||||||
fn full_height(self) -> BoxView<Self> {
|
fn full_height(self) -> Resized<Self> {
|
||||||
BoxView::with_full_height(self)
|
Resized::with_full_height(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Wraps `self` into a limited-size `BoxView`.
|
/// Wraps `self` into a limited-size `Resized`.
|
||||||
fn max_size<S: Into<Vec2>>(self, size: S) -> BoxView<Self> {
|
fn max_size<S: Into<Vec2>>(self, size: S) -> Resized<Self> {
|
||||||
BoxView::with_max_size(size, self)
|
Resized::with_max_size(size, self)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Wraps `self` into a limited-width `BoxView`.
|
/// Wraps `self` into a limited-width `Resized`.
|
||||||
fn max_width(self, max_width: usize) -> BoxView<Self> {
|
fn max_width(self, max_width: usize) -> Resized<Self> {
|
||||||
BoxView::with_max_width(max_width, self)
|
Resized::with_max_width(max_width, self)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Wraps `self` into a limited-height `BoxView`.
|
/// Wraps `self` into a limited-height `Resized`.
|
||||||
fn max_height(self, max_height: usize) -> BoxView<Self> {
|
fn max_height(self, max_height: usize) -> Resized<Self> {
|
||||||
BoxView::with_max_height(max_height, self)
|
Resized::with_max_height(max_height, self)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Wraps `self` into a `BoxView` at least sized `size`.
|
/// Wraps `self` into a `Resized` at least sized `size`.
|
||||||
fn min_size<S: Into<Vec2>>(self, size: S) -> BoxView<Self> {
|
fn min_size<S: Into<Vec2>>(self, size: S) -> Resized<Self> {
|
||||||
BoxView::with_min_size(size, self)
|
Resized::with_min_size(size, self)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Wraps `self` in a `BoxView` at least `min_width` wide.
|
/// Wraps `self` in a `Resized` at least `min_width` wide.
|
||||||
fn min_width(self, min_width: usize) -> BoxView<Self> {
|
fn min_width(self, min_width: usize) -> Resized<Self> {
|
||||||
BoxView::with_min_width(min_width, self)
|
Resized::with_min_width(min_width, self)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Wraps `self` in a `BoxView` at least `min_height` tall.
|
/// Wraps `self` in a `Resized` at least `min_height` tall.
|
||||||
fn min_height(self, min_height: usize) -> BoxView<Self> {
|
fn min_height(self, min_height: usize) -> Resized<Self> {
|
||||||
BoxView::with_min_height(min_height, self)
|
Resized::with_min_height(min_height, self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
use crate::view::{View, ViewPath, ViewWrapper};
|
use crate::view::{View, ViewPath, ViewWrapper};
|
||||||
use crate::views::{IdView, ViewRef};
|
use crate::views::{Named, ViewRef};
|
||||||
use std::any::Any;
|
use std::any::Any;
|
||||||
|
|
||||||
/// Provides `call_on<V: View>` to views.
|
/// Provides `call_on<V: View>` to views.
|
||||||
@ -34,14 +34,14 @@ pub trait Finder {
|
|||||||
self.call_on(&Selector::Id(id), callback)
|
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>>
|
fn find_id<V>(&mut self, id: &str) -> Option<ViewRef<V>>
|
||||||
where
|
where
|
||||||
V: View + Any,
|
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>() {
|
if v.is::<V>() {
|
||||||
*result_ref =
|
*result_ref =
|
||||||
v.downcast_mut::<V>().map(|v| callback(v));
|
v.downcast_mut::<V>().map(|v| callback(v));
|
||||||
} else if v.is::<IdView<V>>() {
|
} else if v.is::<Named<V>>() {
|
||||||
*result_ref = v
|
*result_ref = v
|
||||||
.downcast_mut::<IdView<V>>()
|
.downcast_mut::<Named<V>>()
|
||||||
.and_then(|v| v.with_view_mut(callback));
|
.and_then(|v| v.with_view_mut(callback));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,13 +1,13 @@
|
|||||||
use crate::view::View;
|
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 {
|
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.
|
/// 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
|
/// [`fixed_width`]: trait.Boxable.html#method.fixed_width
|
||||||
/// [`BoxView`]: ../views/struct.BoxView.html
|
/// [`BoxView`]: ../views/struct.BoxView.html
|
||||||
///
|
///
|
||||||
fn with_id<S: Into<String>>(self, id: S) -> IdView<Self> {
|
fn with_id<S: Into<String>>(self, id: S) -> Named<Self> {
|
||||||
IdView::new(id, self)
|
Named::new(id, self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,13 +1,13 @@
|
|||||||
use crate::view::View;
|
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 {
|
pub trait Scrollable: View + Sized {
|
||||||
/// Wraps `self` in a `ScrollView`.
|
/// Wraps `self` in a `Scroll`.
|
||||||
fn scrollable(self) -> ScrollView<Self> {
|
fn scrollable(self) -> Scroll<Self> {
|
||||||
ScrollView::new(self)
|
Scroll::new(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,9 +2,9 @@ use std::cmp::min;
|
|||||||
|
|
||||||
/// Single-dimensional constraint on a view size.
|
/// 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)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
pub enum SizeConstraint {
|
pub enum SizeConstraint {
|
||||||
/// No constraint imposed, the child view's response is used.
|
/// No constraint imposed, the child view's response is used.
|
||||||
@ -22,7 +22,7 @@ pub enum SizeConstraint {
|
|||||||
impl SizeConstraint {
|
impl SizeConstraint {
|
||||||
/// Returns the size to be given to the child.
|
/// 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 {
|
pub fn available(self, available: usize) -> usize {
|
||||||
match self {
|
match self {
|
||||||
SizeConstraint::Free
|
SizeConstraint::Free
|
||||||
|
@ -4,14 +4,14 @@ use std::ops::{Deref, DerefMut};
|
|||||||
/// A boxed `View`.
|
/// A boxed `View`.
|
||||||
///
|
///
|
||||||
/// It derefs to the wrapped view.
|
/// It derefs to the wrapped view.
|
||||||
pub struct ViewBox {
|
pub struct Boxed {
|
||||||
view: Box<dyn View>,
|
view: Box<dyn View>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ViewBox {
|
impl Boxed {
|
||||||
/// Creates a new `ViewBox` around the given boxed view.
|
/// Creates a new `Boxed` around the given boxed view.
|
||||||
pub fn new(view: Box<dyn View>) -> Self {
|
pub fn new(view: Box<dyn View>) -> Self {
|
||||||
ViewBox { view }
|
Boxed { view }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Box the given view
|
/// Box the given view
|
||||||
@ -19,7 +19,7 @@ impl ViewBox {
|
|||||||
where
|
where
|
||||||
T: IntoBoxedView,
|
T: IntoBoxedView,
|
||||||
{
|
{
|
||||||
ViewBox::new(view.as_boxed_view())
|
Boxed::new(view.as_boxed_view())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the inner 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;
|
type Target = dyn View;
|
||||||
|
|
||||||
fn deref(&self) -> &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 {
|
fn deref_mut(&mut self) -> &mut dyn View {
|
||||||
&mut *self.view
|
&mut *self.view
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ViewWrapper for ViewBox {
|
impl ViewWrapper for Boxed {
|
||||||
type V = dyn View;
|
type V = dyn View;
|
||||||
|
|
||||||
fn with_view<F, R>(&self, f: F) -> Option<R>
|
fn with_view<F, R>(&self, f: F) -> Option<R>
|
@ -7,24 +7,24 @@ use crate::Printer;
|
|||||||
use unicode_width::UnicodeWidthStr;
|
use unicode_width::UnicodeWidthStr;
|
||||||
|
|
||||||
/// View used for debugging, showing logs.
|
/// View used for debugging, showing logs.
|
||||||
pub struct DebugView {
|
pub struct DebugConsole {
|
||||||
// TODO: wrap log lines if needed, and save the line splits here.
|
// TODO: wrap log lines if needed, and save the line splits here.
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DebugView {
|
impl DebugConsole {
|
||||||
/// Creates a new DebugView.
|
/// Creates a new DebugConsole.
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
DebugView {}
|
DebugConsole {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for DebugView {
|
impl Default for DebugConsole {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self::new()
|
Self::new()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl View for DebugView {
|
impl View for DebugConsole {
|
||||||
fn draw(&self, printer: &Printer<'_, '_>) {
|
fn draw(&self, printer: &Printer<'_, '_>) {
|
||||||
let logs = logger::LOGS.lock().unwrap();
|
let logs = logger::LOGS.lock().unwrap();
|
||||||
// Only print the last logs, so skip what doesn't fit
|
// Only print the last logs, so skip what doesn't fit
|
@ -5,7 +5,7 @@ use crate::rect::Rect;
|
|||||||
use crate::theme::ColorStyle;
|
use crate::theme::ColorStyle;
|
||||||
use crate::vec::Vec2;
|
use crate::vec::Vec2;
|
||||||
use crate::view::{Margins, Selector, View};
|
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::Cursive;
|
||||||
use crate::Printer;
|
use crate::Printer;
|
||||||
use crate::With;
|
use crate::With;
|
||||||
@ -25,7 +25,7 @@ pub enum DialogFocus {
|
|||||||
}
|
}
|
||||||
|
|
||||||
struct ChildButton {
|
struct ChildButton {
|
||||||
button: SizedView<Button>,
|
button: LastSize<Button>,
|
||||||
offset: Cell<Vec2>,
|
offset: Cell<Vec2>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -35,7 +35,7 @@ impl ChildButton {
|
|||||||
F: 'static + Fn(&mut Cursive),
|
F: 'static + Fn(&mut Cursive),
|
||||||
{
|
{
|
||||||
ChildButton {
|
ChildButton {
|
||||||
button: SizedView::new(Button::new(label, cb)),
|
button: LastSize::new(Button::new(label, cb)),
|
||||||
offset: Cell::new(Vec2::zero()),
|
offset: Cell::new(Vec2::zero()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -46,8 +46,8 @@ impl ChildButton {
|
|||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// # use cursive::views::{Dialog,TextView};
|
/// # use cursive::views::{Dialog,Text};
|
||||||
/// let dialog = Dialog::around(TextView::new("Hello!"))
|
/// let dialog = Dialog::around(Text::new("Hello!"))
|
||||||
/// .button("Ok", |s| s.quit());
|
/// .button("Ok", |s| s.quit());
|
||||||
/// ```
|
/// ```
|
||||||
pub struct Dialog {
|
pub struct Dialog {
|
||||||
@ -58,7 +58,7 @@ pub struct Dialog {
|
|||||||
title_position: HAlign,
|
title_position: HAlign,
|
||||||
|
|
||||||
// The actual inner view.
|
// The actual inner view.
|
||||||
content: SizedView<ViewBox>,
|
content: LastSize<Boxed>,
|
||||||
|
|
||||||
// Optional list of buttons under the main view.
|
// Optional list of buttons under the main view.
|
||||||
// Include the top-left corner.
|
// Include the top-left corner.
|
||||||
@ -87,13 +87,13 @@ impl Dialog {
|
|||||||
///
|
///
|
||||||
/// You should probably call `content()` next.
|
/// You should probably call `content()` next.
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self::around(DummyView)
|
Self::around(Dummy)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates a new `Dialog` with the given content.
|
/// Creates a new `Dialog` with the given content.
|
||||||
pub fn around<V: View + 'static>(view: V) -> Self {
|
pub fn around<V: View + 'static>(view: V) -> Self {
|
||||||
Dialog {
|
Dialog {
|
||||||
content: SizedView::new(ViewBox::boxed(view)),
|
content: LastSize::new(Boxed::boxed(view)),
|
||||||
buttons: Vec::new(),
|
buttons: Vec::new(),
|
||||||
title: String::new(),
|
title: String::new(),
|
||||||
title_position: HAlign::Center,
|
title_position: HAlign::Center,
|
||||||
@ -112,10 +112,10 @@ impl Dialog {
|
|||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// use cursive::views::{Dialog, TextView};
|
/// use cursive::views::{Dialog, Text};
|
||||||
///
|
///
|
||||||
/// let dialog = Dialog::new()
|
/// let dialog = Dialog::new()
|
||||||
/// .content(TextView::new("Hello!"))
|
/// .content(Text::new("Hello!"))
|
||||||
/// .button("Quit", |s| s.quit());
|
/// .button("Quit", |s| s.quit());
|
||||||
/// ```
|
/// ```
|
||||||
pub fn content<V: View + 'static>(self, view: V) -> Self {
|
pub fn content<V: View + 'static>(self, view: V) -> Self {
|
||||||
@ -125,12 +125,12 @@ impl Dialog {
|
|||||||
/// Gets the content of this dialog.
|
/// Gets the content of this dialog.
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// use cursive::views::{Dialog, TextView};
|
/// use cursive::views::{Dialog, Text};
|
||||||
/// let dialog = Dialog::around(TextView::new("Hello!"));
|
/// let dialog = Dialog::around(Text::new("Hello!"));
|
||||||
/// let text_view: &TextView = dialog
|
/// let text_view: &Text = dialog
|
||||||
/// .get_content()
|
/// .get_content()
|
||||||
/// .as_any()
|
/// .as_any()
|
||||||
/// .downcast_ref::<TextView>()
|
/// .downcast_ref::<Text>()
|
||||||
/// .unwrap();
|
/// .unwrap();
|
||||||
/// assert_eq!(text_view.get_content().source(), "Hello!");
|
/// assert_eq!(text_view.get_content().source(), "Hello!");
|
||||||
/// ```
|
/// ```
|
||||||
@ -148,7 +148,7 @@ impl Dialog {
|
|||||||
///
|
///
|
||||||
/// Previous content will be dropped.
|
/// Previous content will be dropped.
|
||||||
pub fn set_content<V: View + 'static>(&mut self, view: V) {
|
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();
|
self.invalidate();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -163,7 +163,7 @@ impl Dialog {
|
|||||||
/// .button("Quit", |s| s.quit());
|
/// .button("Quit", |s| s.quit());
|
||||||
/// ```
|
/// ```
|
||||||
pub fn text<S: Into<String>>(text: S) -> Self {
|
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.
|
/// Convenient method to create an infobox.
|
||||||
|
@ -4,9 +4,9 @@ use crate::Printer;
|
|||||||
/// Dummy view.
|
/// Dummy view.
|
||||||
///
|
///
|
||||||
/// Doesn't print anything. Minimal size is (1,1).
|
/// 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 draw(&self, _: &Printer<'_, '_>) {}
|
||||||
|
|
||||||
fn needs_relayout(&self) -> bool {
|
fn needs_relayout(&self) -> bool {
|
||||||
|
@ -1,11 +1,9 @@
|
|||||||
use crate::direction::Direction;
|
|
||||||
use crate::event::{Callback, Event, EventResult, Key, MouseEvent};
|
use crate::event::{Callback, Event, EventResult, Key, MouseEvent};
|
||||||
use crate::rect::Rect;
|
use crate::rect::Rect;
|
||||||
use crate::theme::{ColorStyle, Effect};
|
use crate::theme::{ColorStyle, Effect};
|
||||||
use crate::utils::lines::simple::{simple_prefix, simple_suffix};
|
use crate::utils::lines::simple::{simple_prefix, simple_suffix};
|
||||||
use crate::vec::Vec2;
|
use crate::{Cursive, Direction, Printer, Vec2, View, With};
|
||||||
use crate::view::View;
|
|
||||||
use crate::{Cursive, Printer, With};
|
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use unicode_segmentation::UnicodeSegmentation;
|
use unicode_segmentation::UnicodeSegmentation;
|
||||||
@ -33,7 +31,7 @@ pub type OnSubmit = dyn Fn(&mut Cursive, &str);
|
|||||||
/// ```rust
|
/// ```rust
|
||||||
/// # use cursive::Cursive;
|
/// # use cursive::Cursive;
|
||||||
/// # use cursive::traits::*;
|
/// # use cursive::traits::*;
|
||||||
/// # use cursive::views::{Dialog, EditView, TextView};
|
/// # use cursive::views::{Dialog, Edit, Text};
|
||||||
/// let mut siv = Cursive::dummy();
|
/// let mut siv = Cursive::dummy();
|
||||||
///
|
///
|
||||||
/// // Create a dialog with an edit text and a button.
|
/// // 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")
|
/// .title("Enter your name")
|
||||||
/// .padding((1, 1, 1, 0))
|
/// .padding((1, 1, 1, 0))
|
||||||
/// .content(
|
/// .content(
|
||||||
/// EditView::new()
|
/// Edit::new()
|
||||||
/// .on_submit(show_popup)
|
/// .on_submit(show_popup)
|
||||||
/// .with_id("name")
|
/// .with_id("name")
|
||||||
/// .fixed_width(20),
|
/// .fixed_width(20),
|
||||||
@ -52,7 +50,7 @@ pub type OnSubmit = dyn Fn(&mut Cursive, &str);
|
|||||||
/// .button("Ok", |s| {
|
/// .button("Ok", |s| {
|
||||||
/// let name = s.call_on_id(
|
/// let name = s.call_on_id(
|
||||||
/// "name",
|
/// "name",
|
||||||
/// |view: &mut EditView| view.get_content(),
|
/// |view: &mut Edit| view.get_content(),
|
||||||
/// ).unwrap();
|
/// ).unwrap();
|
||||||
/// show_popup(s, &name);
|
/// show_popup(s, &name);
|
||||||
/// }),
|
/// }),
|
||||||
@ -64,12 +62,12 @@ pub type OnSubmit = dyn Fn(&mut Cursive, &str);
|
|||||||
/// } else {
|
/// } else {
|
||||||
/// let content = format!("Hello {}!", name);
|
/// let content = format!("Hello {}!", name);
|
||||||
/// s.pop_layer();
|
/// s.pop_layer();
|
||||||
/// s.add_layer(Dialog::around(TextView::new(content))
|
/// s.add_layer(Dialog::around(Text::new(content))
|
||||||
/// .button("Quit", |s| s.quit()));
|
/// .button("Quit", |s| s.quit()));
|
||||||
/// }
|
/// }
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
pub struct EditView {
|
pub struct Edit {
|
||||||
/// Current content.
|
/// Current content.
|
||||||
content: Rc<String>,
|
content: Rc<String>,
|
||||||
|
|
||||||
@ -108,12 +106,12 @@ pub struct EditView {
|
|||||||
style: ColorStyle,
|
style: ColorStyle,
|
||||||
}
|
}
|
||||||
|
|
||||||
new_default!(EditView);
|
new_default!(Edit);
|
||||||
|
|
||||||
impl EditView {
|
impl Edit {
|
||||||
/// Creates a new, empty edit view.
|
/// Creates a new, empty edit view.
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
EditView {
|
Edit {
|
||||||
content: Rc::new(String::new()),
|
content: Rc::new(String::new()),
|
||||||
cursor: 0,
|
cursor: 0,
|
||||||
offset: 0,
|
offset: 0,
|
||||||
@ -174,8 +172,8 @@ impl EditView {
|
|||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// # use cursive::views::EditView;
|
/// # use cursive::views::Edit;
|
||||||
/// let edit = EditView::new().filler(" ");
|
/// let edit = Edit::new().filler(" ");
|
||||||
/// ```
|
/// ```
|
||||||
pub fn filler<S: Into<String>>(self, filler: S) -> Self {
|
pub fn filler<S: Into<String>>(self, filler: S) -> Self {
|
||||||
self.with(|s| s.set_filler(filler))
|
self.with(|s| s.set_filler(filler))
|
||||||
@ -269,12 +267,12 @@ impl EditView {
|
|||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// use cursive::views::{TextContent, TextView, EditView};
|
/// use cursive::views::{TextContent, Text, Edit};
|
||||||
/// // Keep the length of the text in a separate view.
|
/// // Keep the length of the text in a separate view.
|
||||||
/// let mut content = TextContent::new("0");
|
/// 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| {
|
/// .on_edit(move |_s, text, _cursor| {
|
||||||
/// content.set_content(format!("{}", text.len()));
|
/// content.set_content(format!("{}", text.len()));
|
||||||
/// });
|
/// });
|
||||||
@ -343,9 +341,9 @@ impl EditView {
|
|||||||
/// # Examples
|
/// # 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| {
|
/// .on_submit(|s, text| {
|
||||||
/// s.add_layer(Dialog::info(text));
|
/// s.add_layer(Dialog::info(text));
|
||||||
/// });
|
/// });
|
||||||
@ -522,7 +520,7 @@ fn make_small_stars(length: usize) -> &'static str {
|
|||||||
&"****"[..length]
|
&"****"[..length]
|
||||||
}
|
}
|
||||||
|
|
||||||
impl View for EditView {
|
impl View for Edit {
|
||||||
fn draw(&self, printer: &Printer<'_, '_>) {
|
fn draw(&self, printer: &Printer<'_, '_>) {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
printer.size.x, self.last_length,
|
printer.size.x, self.last_length,
|
@ -1,6 +1,6 @@
|
|||||||
use crate::event::{Event, EventResult};
|
use crate::event::{Event, EventResult};
|
||||||
use crate::view::{View, ViewWrapper};
|
use crate::view::ViewWrapper;
|
||||||
use crate::Printer;
|
use crate::{Printer, View};
|
||||||
|
|
||||||
/// Wrapper around another view that can be enabled/disabled at will.
|
/// Wrapper around another view that can be enabled/disabled at will.
|
||||||
///
|
///
|
||||||
@ -10,15 +10,15 @@ use crate::Printer;
|
|||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// use cursive::Cursive;
|
/// use cursive::Cursive;
|
||||||
/// use cursive::views::{Button, EnableableView, Checkbox, LinearLayout};
|
/// use cursive::views::{Button, Enableable, Checkbox, LinearLayout};
|
||||||
/// use cursive::traits::Identifiable;
|
/// use cursive::traits::Identifiable;
|
||||||
///
|
///
|
||||||
/// let mut siv = Cursive::dummy();
|
/// let mut siv = Cursive::dummy();
|
||||||
///
|
///
|
||||||
/// siv.add_layer(LinearLayout::vertical()
|
/// 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| {
|
/// .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
|
/// // This will disable (or re-enable) the checkbox, preventing the user from
|
||||||
/// // interacting with it.
|
/// // interacting with it.
|
||||||
/// v.set_enabled(!v.is_enabled());
|
/// v.set_enabled(!v.is_enabled());
|
||||||
@ -26,17 +26,17 @@ use crate::Printer;
|
|||||||
/// }))
|
/// }))
|
||||||
/// );
|
/// );
|
||||||
/// ```
|
/// ```
|
||||||
pub struct EnableableView<V> {
|
pub struct Enableable<V> {
|
||||||
view: V,
|
view: V,
|
||||||
enabled: bool,
|
enabled: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<V> EnableableView<V> {
|
impl<V> Enableable<V> {
|
||||||
/// Creates a new `EnableableView` around `view`.
|
/// Creates a new `Enableable` around `view`.
|
||||||
///
|
///
|
||||||
/// It will be enabled by default.
|
/// It will be enabled by default.
|
||||||
pub fn new(view: V) -> Self {
|
pub fn new(view: V) -> Self {
|
||||||
EnableableView {
|
Enableable {
|
||||||
view,
|
view,
|
||||||
enabled: true,
|
enabled: true,
|
||||||
}
|
}
|
||||||
@ -46,7 +46,7 @@ impl<V> EnableableView<V> {
|
|||||||
inner_getters!(self.view: 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);
|
wrap_impl!(self.view: V);
|
||||||
|
|
||||||
fn wrap_on_event(&mut self, event: Event) -> EventResult {
|
fn wrap_on_event(&mut self, event: Event) -> EventResult {
|
@ -7,22 +7,22 @@ use crate::With;
|
|||||||
///
|
///
|
||||||
/// By default, it simply forwards all calls to the inner view.
|
/// 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.
|
/// invisible view, will not take focus and will not accept input.
|
||||||
///
|
///
|
||||||
/// It can be made visible again with `HideableView::unhide()`.
|
/// It can be made visible again with `Hideable::unhide()`.
|
||||||
pub struct HideableView<V> {
|
pub struct Hideable<V> {
|
||||||
view: V,
|
view: V,
|
||||||
visible: bool,
|
visible: bool,
|
||||||
invalidated: bool,
|
invalidated: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<V> HideableView<V> {
|
impl<V> Hideable<V> {
|
||||||
/// Creates a new HideableView around `view`.
|
/// Creates a new Hideable around `view`.
|
||||||
///
|
///
|
||||||
/// It will be visible by default.
|
/// It will be visible by default.
|
||||||
pub fn new(view: V) -> Self {
|
pub fn new(view: V) -> Self {
|
||||||
HideableView {
|
Hideable {
|
||||||
view,
|
view,
|
||||||
visible: true,
|
visible: true,
|
||||||
invalidated: true,
|
invalidated: true,
|
||||||
@ -64,7 +64,7 @@ impl<V> HideableView<V> {
|
|||||||
inner_getters!(self.view: V);
|
inner_getters!(self.view: V);
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<V: View> ViewWrapper for HideableView<V> {
|
impl<V: View> ViewWrapper for Hideable<V> {
|
||||||
type V = V;
|
type V = V;
|
||||||
|
|
||||||
fn with_view<F, R>(&self, f: F) -> Option<R>
|
fn with_view<F, R>(&self, f: F) -> Option<R>
|
@ -3,17 +3,17 @@ use crate::view::View;
|
|||||||
use crate::view::ViewWrapper;
|
use crate::view::ViewWrapper;
|
||||||
|
|
||||||
/// Wrapper around a view that remembers its size.
|
/// Wrapper around a view that remembers its size.
|
||||||
pub struct SizedView<T> {
|
pub struct LastSize<T> {
|
||||||
/// Wrapped view.
|
/// Wrapped view.
|
||||||
pub view: T,
|
pub view: T,
|
||||||
/// Cached size from the last layout() call.
|
/// Cached size from the last layout() call.
|
||||||
pub size: Vec2,
|
pub size: Vec2,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> SizedView<T> {
|
impl<T> LastSize<T> {
|
||||||
/// Wraps the given view.
|
/// Wraps the given view.
|
||||||
pub fn new(view: T) -> Self {
|
pub fn new(view: T) -> Self {
|
||||||
SizedView {
|
LastSize {
|
||||||
view,
|
view,
|
||||||
size: Vec2::zero(),
|
size: Vec2::zero(),
|
||||||
}
|
}
|
||||||
@ -22,7 +22,7 @@ impl<T> SizedView<T> {
|
|||||||
inner_getters!(self.view: 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);
|
wrap_impl!(self.view: T);
|
||||||
|
|
||||||
fn wrap_layout(&mut self, size: Vec2) {
|
fn wrap_layout(&mut self, size: Vec2) {
|
@ -10,9 +10,9 @@ use log::debug;
|
|||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use unicode_width::UnicodeWidthStr;
|
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 {
|
pub enum ListChild {
|
||||||
/// A single row, with a label and a view.
|
/// A single row, with a label and a view.
|
||||||
Row(String, Box<dyn View>),
|
Row(String, Box<dyn View>),
|
||||||
@ -37,7 +37,7 @@ impl ListChild {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Displays a list of elements.
|
/// Displays a list of elements.
|
||||||
pub struct ListView {
|
pub struct List {
|
||||||
children: Vec<ListChild>,
|
children: Vec<ListChild>,
|
||||||
focus: usize,
|
focus: usize,
|
||||||
// This callback is called when the selection is changed.
|
// This callback is called when the selection is changed.
|
||||||
@ -45,12 +45,12 @@ pub struct ListView {
|
|||||||
last_size: Vec2,
|
last_size: Vec2,
|
||||||
}
|
}
|
||||||
|
|
||||||
new_default!(ListView);
|
new_default!(List);
|
||||||
|
|
||||||
impl ListView {
|
impl List {
|
||||||
/// Creates a new, empty `ListView`.
|
/// Creates a new, empty `List`.
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
ListView {
|
List {
|
||||||
children: Vec::new(),
|
children: Vec::new(),
|
||||||
focus: 0,
|
focus: 0,
|
||||||
on_select: None,
|
on_select: None,
|
||||||
@ -272,7 +272,7 @@ fn try_focus(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl View for ListView {
|
impl View for List {
|
||||||
fn draw(&self, printer: &Printer<'_, '_>) {
|
fn draw(&self, printer: &Printer<'_, '_>) {
|
||||||
if self.children.is_empty() {
|
if self.children.is_empty() {
|
||||||
return;
|
return;
|
@ -7,7 +7,7 @@ use crate::rect::Rect;
|
|||||||
use crate::vec::Vec2;
|
use crate::vec::Vec2;
|
||||||
use crate::view::scroll;
|
use crate::view::scroll;
|
||||||
use crate::view::{Position, View};
|
use crate::view::{Position, View};
|
||||||
use crate::views::OnEventView;
|
use crate::views::OnEvent;
|
||||||
use crate::Cursive;
|
use crate::Cursive;
|
||||||
use crate::Printer;
|
use crate::Printer;
|
||||||
use crate::With;
|
use crate::With;
|
||||||
@ -202,7 +202,7 @@ impl MenuPopup {
|
|||||||
let action_cb = action_cb.clone();
|
let action_cb = action_cb.clone();
|
||||||
s.screen_mut().add_layer_at(
|
s.screen_mut().add_layer_at(
|
||||||
Position::parent(offset),
|
Position::parent(offset),
|
||||||
OnEventView::new(MenuPopup::new(Rc::clone(&tree)).on_action(
|
OnEvent::new(MenuPopup::new(Rc::clone(&tree)).on_action(
|
||||||
move |s| {
|
move |s| {
|
||||||
// This will happen when the subtree popup
|
// This will happen when the subtree popup
|
||||||
// activates something;
|
// activates something;
|
||||||
|
@ -5,7 +5,7 @@ use crate::rect::Rect;
|
|||||||
use crate::theme::ColorStyle;
|
use crate::theme::ColorStyle;
|
||||||
use crate::vec::Vec2;
|
use crate::vec::Vec2;
|
||||||
use crate::view::{Position, View};
|
use crate::view::{Position, View};
|
||||||
use crate::views::{MenuPopup, OnEventView};
|
use crate::views::{MenuPopup, OnEvent};
|
||||||
use crate::Cursive;
|
use crate::Cursive;
|
||||||
use crate::Printer;
|
use crate::Printer;
|
||||||
use std::rc::Rc;
|
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
|
// Also adds two key callbacks on this new view, to handle `left` and
|
||||||
// `right` key presses.
|
// `right` key presses.
|
||||||
// (If the view itself listens for a `left` or `right` press, it will
|
// (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.)
|
// be entered.)
|
||||||
s.screen_mut().add_layer_at(
|
s.screen_mut().add_layer_at(
|
||||||
Position::absolute(offset),
|
Position::absolute(offset),
|
||||||
OnEventView::new(
|
OnEvent::new(
|
||||||
MenuPopup::new(menu)
|
MenuPopup::new(menu)
|
||||||
.on_dismiss(Cursive::select_menubar)
|
.on_dismiss(Cursive::select_menubar)
|
||||||
.on_action(|s| s.menubar().state = State::Inactive),
|
.on_action(|s| s.menubar().state = State::Inactive),
|
||||||
|
@ -52,68 +52,68 @@ macro_rules! impl_enabled {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mod box_view;
|
mod boxed;
|
||||||
mod button;
|
mod button;
|
||||||
mod canvas;
|
mod canvas;
|
||||||
mod checkbox;
|
mod checkbox;
|
||||||
mod circular_focus;
|
mod circular_focus;
|
||||||
mod debug_view;
|
mod debug_console;
|
||||||
mod dialog;
|
mod dialog;
|
||||||
mod dummy;
|
mod dummy;
|
||||||
mod edit_view;
|
mod edit;
|
||||||
mod enableable_view;
|
mod enableable;
|
||||||
mod hideable_view;
|
mod hideable;
|
||||||
mod id_view;
|
mod last_size;
|
||||||
mod layer;
|
mod layer;
|
||||||
mod linear_layout;
|
mod linear_layout;
|
||||||
mod list_view;
|
mod list;
|
||||||
mod menu_popup;
|
mod menu_popup;
|
||||||
mod menubar;
|
mod menubar;
|
||||||
mod on_event_view;
|
mod named;
|
||||||
mod padded_view;
|
mod on_event;
|
||||||
|
mod padded;
|
||||||
mod panel;
|
mod panel;
|
||||||
mod progress_bar;
|
mod progress_bar;
|
||||||
mod radio;
|
mod radio;
|
||||||
mod scroll_view;
|
mod resized;
|
||||||
mod select_view;
|
mod scroll;
|
||||||
mod shadow_view;
|
mod select;
|
||||||
mod sized_view;
|
mod shadow;
|
||||||
mod slider_view;
|
mod slider;
|
||||||
mod stack_view;
|
mod stack;
|
||||||
|
mod text;
|
||||||
mod text_area;
|
mod text_area;
|
||||||
mod text_view;
|
mod tracked;
|
||||||
mod tracked_view;
|
|
||||||
mod view_box;
|
|
||||||
|
|
||||||
pub use self::box_view::BoxView;
|
pub use self::boxed::Boxed;
|
||||||
pub use self::button::Button;
|
pub use self::button::Button;
|
||||||
pub use self::canvas::Canvas;
|
pub use self::canvas::Canvas;
|
||||||
pub use self::checkbox::Checkbox;
|
pub use self::checkbox::Checkbox;
|
||||||
pub use self::circular_focus::CircularFocus;
|
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::dialog::{Dialog, DialogFocus};
|
||||||
pub use self::dummy::DummyView;
|
pub use self::dummy::Dummy;
|
||||||
pub use self::edit_view::EditView;
|
pub use self::edit::Edit;
|
||||||
pub use self::enableable_view::EnableableView;
|
pub use self::enableable::Enableable;
|
||||||
pub use self::hideable_view::HideableView;
|
pub use self::hideable::Hideable;
|
||||||
pub use self::id_view::{IdView, ViewRef};
|
pub use self::last_size::LastSize;
|
||||||
pub use self::layer::Layer;
|
pub use self::layer::Layer;
|
||||||
pub use self::linear_layout::LinearLayout;
|
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::menu_popup::MenuPopup;
|
||||||
pub use self::menubar::Menubar;
|
pub use self::menubar::Menubar;
|
||||||
pub use self::on_event_view::OnEventView;
|
pub use self::named::{Named, ViewRef};
|
||||||
pub use self::padded_view::PaddedView;
|
pub use self::on_event::OnEvent;
|
||||||
|
pub use self::padded::Padded;
|
||||||
pub use self::panel::Panel;
|
pub use self::panel::Panel;
|
||||||
pub use self::progress_bar::ProgressBar;
|
pub use self::progress_bar::ProgressBar;
|
||||||
pub use self::radio::{RadioButton, RadioGroup};
|
pub use self::radio::{RadioButton, RadioGroup};
|
||||||
pub use self::scroll_view::ScrollView;
|
pub use self::resized::Resized;
|
||||||
pub use self::select_view::SelectView;
|
pub use self::scroll::Scroll;
|
||||||
pub use self::shadow_view::ShadowView;
|
pub use self::select::Select;
|
||||||
pub use self::sized_view::SizedView;
|
pub use self::shadow::Shadow;
|
||||||
pub use self::slider_view::SliderView;
|
pub use self::slider::Slider;
|
||||||
pub use self::stack_view::{LayerPosition, StackView};
|
pub use self::stack::{LayerPosition, Stack};
|
||||||
|
pub use self::text::{Text, TextContent, TextContentRef};
|
||||||
pub use self::text_area::TextArea;
|
pub use self::text_area::TextArea;
|
||||||
pub use self::text_view::{TextContent, TextContentRef, TextView};
|
pub use self::tracked::Tracked;
|
||||||
pub use self::tracked_view::TrackedView;
|
|
||||||
pub use self::view_box::ViewBox;
|
|
||||||
|
@ -10,7 +10,7 @@ use std::rc::Rc;
|
|||||||
/// This lets other views refer to this one using a string identifier.
|
/// 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.
|
/// 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>>,
|
view: Rc<RefCell<V>>,
|
||||||
id: String,
|
id: String,
|
||||||
}
|
}
|
||||||
@ -22,10 +22,10 @@ pub struct IdView<V: View> {
|
|||||||
/// [`RefMut`]: https://doc.rust-lang.org/std/cell/struct.RefMut.html
|
/// [`RefMut`]: https://doc.rust-lang.org/std/cell/struct.RefMut.html
|
||||||
pub type ViewRef<V> = OwningHandle<RcRef<RefCell<V>>, RefMut<'static, V>>;
|
pub type ViewRef<V> = OwningHandle<RcRef<RefCell<V>>, RefMut<'static, V>>;
|
||||||
|
|
||||||
impl<V: View> IdView<V> {
|
impl<V: View> Named<V> {
|
||||||
/// Wraps `view` in a new `IdView`.
|
/// Wraps `view` in a new `Named`.
|
||||||
pub fn new<S: Into<String>>(id: S, view: V) -> Self {
|
pub fn new<S: Into<String>>(id: S, view: V) -> Self {
|
||||||
IdView {
|
Named {
|
||||||
view: Rc::new(RefCell::new(view)),
|
view: Rc::new(RefCell::new(view)),
|
||||||
id: id.into(),
|
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;
|
type V = T;
|
||||||
|
|
||||||
fn with_view<F, R>(&self, f: F) -> Option<R>
|
fn with_view<F, R>(&self, f: F) -> Option<R>
|
@ -24,21 +24,21 @@ use std::rc::Rc;
|
|||||||
/// "Simple" callbacks ([`on_event`] and [`on_pre_event`]) skip this first
|
/// "Simple" callbacks ([`on_event`] and [`on_pre_event`]) skip this first
|
||||||
/// phase and are only called with a `&mut Cursive`.
|
/// phase and are only called with a `&mut Cursive`.
|
||||||
///
|
///
|
||||||
/// [`on_event`]: struct.OnEventView.html#method.on_event
|
/// [`on_event`]: struct.OnEvent.html#method.on_event
|
||||||
/// [`on_pre_event`]: struct.OnEventView.html#method.on_pre_event
|
/// [`on_pre_event`]: struct.OnEvent.html#method.on_pre_event
|
||||||
/// [`on_event_inner`]: struct.OnEventView.html#method.on_event_inner
|
/// [`on_event_inner`]: struct.OnEvent.html#method.on_event_inner
|
||||||
/// [`on_pre_event_inner`]: struct.OnEventView.html#method.on_pre_event_inner
|
/// [`on_pre_event_inner`]: struct.OnEvent.html#method.on_pre_event_inner
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// # use cursive::event;;
|
/// # use cursive::event;;
|
||||||
/// # use cursive::views::{OnEventView, TextView};
|
/// # use cursive::views::{OnEvent, Text};
|
||||||
/// let view = OnEventView::new(TextView::new("This view has an event!"))
|
/// let view = OnEvent::new(Text::new("This view has an event!"))
|
||||||
/// .on_event('q', |s| s.quit())
|
/// .on_event('q', |s| s.quit())
|
||||||
/// .on_event(event::Key::Esc, |s| s.quit());
|
/// .on_event(event::Key::Esc, |s| s.quit());
|
||||||
/// ```
|
/// ```
|
||||||
pub struct OnEventView<T: View> {
|
pub struct OnEvent<T: View> {
|
||||||
view: T,
|
view: T,
|
||||||
callbacks: Vec<(EventTrigger, Action<T>)>,
|
callbacks: Vec<(EventTrigger, Action<T>)>,
|
||||||
}
|
}
|
||||||
@ -65,10 +65,10 @@ enum TriggerPhase {
|
|||||||
AfterChild,
|
AfterChild,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: View> OnEventView<T> {
|
impl<T: View> OnEvent<T> {
|
||||||
/// Wraps the given view in a new OnEventView.
|
/// Wraps the given view in a new OnEvent.
|
||||||
pub fn new(view: T) -> Self {
|
pub fn new(view: T) -> Self {
|
||||||
OnEventView {
|
OnEvent {
|
||||||
view,
|
view,
|
||||||
callbacks: Vec::new(),
|
callbacks: Vec::new(),
|
||||||
}
|
}
|
||||||
@ -82,15 +82,15 @@ impl<T: View> OnEventView<T> {
|
|||||||
///
|
///
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// # use cursive::views::{OnEventView, DummyView};
|
/// # use cursive::views::{OnEvent, Dummy};
|
||||||
/// # use cursive::event::{Key, EventTrigger};
|
/// # use cursive::event::{Key, EventTrigger};
|
||||||
/// let view = OnEventView::new(DummyView)
|
/// let view = OnEvent::new(Dummy)
|
||||||
/// .on_event('q', |s| s.quit())
|
/// .on_event('q', |s| s.quit())
|
||||||
/// .on_event(Key::Esc, |s| {
|
/// .on_event(Key::Esc, |s| {
|
||||||
/// s.pop_layer();
|
/// s.pop_layer();
|
||||||
/// })
|
/// })
|
||||||
/// .on_event(EventTrigger::mouse(), |s| {
|
/// .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
|
pub fn on_event<F, E>(self, trigger: E, cb: F) -> Self
|
||||||
@ -143,17 +143,17 @@ impl<T: View> OnEventView<T> {
|
|||||||
///
|
///
|
||||||
/// Chainable variant.
|
/// Chainable variant.
|
||||||
///
|
///
|
||||||
/// [`on_event`]: OnEventView::on_event()
|
/// [`on_event`]: OnEvent::on_event()
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// # use cursive::views::{DummyView, OnEventView};
|
/// # use cursive::views::{Dummy, OnEvent};
|
||||||
/// # use cursive::event::{Event, EventTrigger, MouseEvent, EventResult};
|
/// # use cursive::event::{Event, EventTrigger, MouseEvent, EventResult};
|
||||||
/// let view = OnEventView::new(DummyView)
|
/// let view = OnEvent::new(Dummy)
|
||||||
/// .on_event_inner(
|
/// .on_event_inner(
|
||||||
/// EventTrigger::mouse(),
|
/// EventTrigger::mouse(),
|
||||||
/// |d: &mut DummyView, e: &Event| {
|
/// |d: &mut Dummy, e: &Event| {
|
||||||
/// if let &Event::Mouse { event: MouseEvent::Press(_), .. } = e {
|
/// if let &Event::Mouse { event: MouseEvent::Press(_), .. } = e {
|
||||||
/// // Do something on mouse press
|
/// // Do something on mouse press
|
||||||
/// Some(EventResult::with_cb(|s| {
|
/// Some(EventResult::with_cb(|s| {
|
||||||
@ -254,7 +254,7 @@ impl<T: View> OnEventView<T> {
|
|||||||
inner_getters!(self.view: 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);
|
wrap_impl!(self.view: T);
|
||||||
|
|
||||||
fn wrap_on_event(&mut self, event: Event) -> EventResult {
|
fn wrap_on_event(&mut self, event: Event) -> EventResult {
|
@ -12,23 +12,23 @@ use crate::Printer;
|
|||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// # use cursive::views::{TextView, PaddedView};
|
/// # use cursive::views::{Text, Padded};
|
||||||
/// // Adds 2 columns of padding to the left and to the right.
|
/// // 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))
|
/// ((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,
|
view: V,
|
||||||
margins: Margins,
|
margins: Margins,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<V: View> PaddedView<V> {
|
impl<V: View> Padded<V> {
|
||||||
/// Wraps `view` in a new `PaddedView` with the given margins.
|
/// Wraps `view` in a new `Padded` with the given margins.
|
||||||
pub fn new<M: Into<Margins>>(margins: M, view: V) -> Self {
|
pub fn new<M: Into<Margins>>(margins: M, view: V) -> Self {
|
||||||
let margins = margins.into();
|
let margins = margins.into();
|
||||||
PaddedView { view, margins }
|
Padded { view, margins }
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets the margins for this view.
|
/// Sets the margins for this view.
|
||||||
@ -40,7 +40,7 @@ impl<V: View> PaddedView<V> {
|
|||||||
inner_getters!(self.view: 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);
|
wrap_impl!(self.view: V);
|
||||||
|
|
||||||
fn wrap_required_size(&mut self, req: Vec2) -> Vec2 {
|
fn wrap_required_size(&mut self, req: Vec2) -> Vec2 {
|
@ -8,7 +8,7 @@ use crate::XY;
|
|||||||
/// Each axis can independently be set to:
|
/// Each axis can independently be set to:
|
||||||
///
|
///
|
||||||
/// * Keep a **fixed** size
|
/// * Keep a **fixed** size
|
||||||
/// * Use **all** available size
|
/// * Use the **full** available size
|
||||||
/// * Use **at most** a given size
|
/// * Use **at most** a given size
|
||||||
/// * Use **at least** a given size
|
/// * Use **at least** a given size
|
||||||
/// * Let the wrapped view decide.
|
/// * Let the wrapped view decide.
|
||||||
@ -16,14 +16,14 @@ use crate::XY;
|
|||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// use cursive::views::{BoxView, TextView};
|
/// use cursive::views::{Resized, TextView};
|
||||||
///
|
///
|
||||||
/// // Creates a 20x4 BoxView with a TextView content.
|
/// // Creates a 20x4 Resized with a TextView content.
|
||||||
/// let view = BoxView::with_fixed_size((20,4), TextView::new("Hello!"));
|
/// 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.
|
/// 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
|
/// Constraint on each axis
|
||||||
size: XY<SizeConstraint>,
|
size: XY<SizeConstraint>,
|
||||||
|
|
||||||
@ -34,8 +34,8 @@ pub struct BoxView<T: View> {
|
|||||||
view: T,
|
view: T,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: View> BoxView<T> {
|
impl<T: View> Resized<T> {
|
||||||
/// Creates a new `BoxView` with the given width and height requirements.
|
/// Creates a new `Resized` with the given width and height requirements.
|
||||||
///
|
///
|
||||||
/// `None` values will use the wrapped view's preferences.
|
/// `None` values will use the wrapped view's preferences.
|
||||||
pub fn new(
|
pub fn new(
|
||||||
@ -43,7 +43,7 @@ impl<T: View> BoxView<T> {
|
|||||||
height: SizeConstraint,
|
height: SizeConstraint,
|
||||||
view: T,
|
view: T,
|
||||||
) -> Self {
|
) -> Self {
|
||||||
BoxView {
|
Resized {
|
||||||
size: (width, height).into(),
|
size: (width, height).into(),
|
||||||
invalidated: true,
|
invalidated: true,
|
||||||
view,
|
view,
|
||||||
@ -76,102 +76,102 @@ impl<T: View> BoxView<T> {
|
|||||||
self.invalidate();
|
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 {
|
pub fn with_fixed_size<S: Into<Vec2>>(size: S, view: T) -> Self {
|
||||||
let size = size.into();
|
let size = size.into();
|
||||||
|
|
||||||
BoxView::new(
|
Resized::new(
|
||||||
SizeConstraint::Fixed(size.x),
|
SizeConstraint::Fixed(size.x),
|
||||||
SizeConstraint::Fixed(size.y),
|
SizeConstraint::Fixed(size.y),
|
||||||
view,
|
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 {
|
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 {
|
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 {
|
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 {
|
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 {
|
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 {
|
pub fn with_max_size<S: Into<Vec2>>(size: S, view: T) -> Self {
|
||||||
let size = size.into();
|
let size = size.into();
|
||||||
|
|
||||||
BoxView::new(
|
Resized::new(
|
||||||
SizeConstraint::AtMost(size.x),
|
SizeConstraint::AtMost(size.x),
|
||||||
SizeConstraint::AtMost(size.y),
|
SizeConstraint::AtMost(size.y),
|
||||||
view,
|
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`.
|
/// The resulting width will never be more than `max_width`.
|
||||||
pub fn with_max_width(max_width: usize, view: T) -> Self {
|
pub fn with_max_width(max_width: usize, view: T) -> Self {
|
||||||
BoxView::new(
|
Resized::new(
|
||||||
SizeConstraint::AtMost(max_width),
|
SizeConstraint::AtMost(max_width),
|
||||||
SizeConstraint::Free,
|
SizeConstraint::Free,
|
||||||
view,
|
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`.
|
/// The resulting height will never be more than `max_height`.
|
||||||
pub fn with_max_height(max_height: usize, view: T) -> Self {
|
pub fn with_max_height(max_height: usize, view: T) -> Self {
|
||||||
BoxView::new(
|
Resized::new(
|
||||||
SizeConstraint::Free,
|
SizeConstraint::Free,
|
||||||
SizeConstraint::AtMost(max_height),
|
SizeConstraint::AtMost(max_height),
|
||||||
view,
|
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 {
|
pub fn with_min_size<S: Into<Vec2>>(size: S, view: T) -> Self {
|
||||||
let size = size.into();
|
let size = size.into();
|
||||||
|
|
||||||
BoxView::new(
|
Resized::new(
|
||||||
SizeConstraint::AtLeast(size.x),
|
SizeConstraint::AtLeast(size.x),
|
||||||
SizeConstraint::AtLeast(size.y),
|
SizeConstraint::AtLeast(size.y),
|
||||||
view,
|
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`.
|
/// The resulting width will never be less than `min_width`.
|
||||||
pub fn with_min_width(min_width: usize, view: T) -> Self {
|
pub fn with_min_width(min_width: usize, view: T) -> Self {
|
||||||
BoxView::new(
|
Resized::new(
|
||||||
SizeConstraint::AtLeast(min_width),
|
SizeConstraint::AtLeast(min_width),
|
||||||
SizeConstraint::Free,
|
SizeConstraint::Free,
|
||||||
view,
|
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`.
|
/// The resulting height will never be less than `min_height`.
|
||||||
pub fn with_min_height(min_height: usize, view: T) -> Self {
|
pub fn with_min_height(min_height: usize, view: T) -> Self {
|
||||||
BoxView::new(
|
Resized::new(
|
||||||
SizeConstraint::Free,
|
SizeConstraint::Free,
|
||||||
SizeConstraint::AtLeast(min_height),
|
SizeConstraint::AtLeast(min_height),
|
||||||
view,
|
view,
|
||||||
@ -186,7 +186,7 @@ impl<T: View> BoxView<T> {
|
|||||||
inner_getters!(self.view: 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);
|
wrap_impl!(self.view: T);
|
||||||
|
|
||||||
fn wrap_draw(&self, printer: &Printer) {
|
fn wrap_draw(&self, printer: &Printer) {
|
@ -4,22 +4,22 @@ use crate::view::{scroll, ScrollStrategy, Selector, View};
|
|||||||
use crate::{Printer, Rect, Vec2, With};
|
use crate::{Printer, Rect, Vec2, With};
|
||||||
|
|
||||||
/// Wraps a view in a scrollable area.
|
/// Wraps a view in a scrollable area.
|
||||||
pub struct ScrollView<V> {
|
pub struct Scroll<V> {
|
||||||
/// The wrapped view.
|
/// The wrapped view.
|
||||||
inner: V,
|
inner: V,
|
||||||
|
|
||||||
core: scroll::Core,
|
core: scroll::Core,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl_scroller!(ScrollView<V>::core);
|
impl_scroller!(Scroll<V>::core);
|
||||||
|
|
||||||
impl<V> ScrollView<V>
|
impl<V> Scroll<V>
|
||||||
where
|
where
|
||||||
V: View,
|
V: View,
|
||||||
{
|
{
|
||||||
/// Creates a new ScrollView around `view`.
|
/// Creates a new Scroll around `view`.
|
||||||
pub fn new(inner: V) -> Self {
|
pub fn new(inner: V) -> Self {
|
||||||
ScrollView {
|
Scroll {
|
||||||
inner,
|
inner,
|
||||||
core: scroll::Core::new(),
|
core: scroll::Core::new(),
|
||||||
}
|
}
|
||||||
@ -34,7 +34,7 @@ where
|
|||||||
/// phase.
|
/// phase.
|
||||||
///
|
///
|
||||||
/// This is only the size the content _thinks_ it has, and may be larger
|
/// 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 {
|
pub fn inner_size(&self) -> Vec2 {
|
||||||
self.core.inner_size()
|
self.core.inner_size()
|
||||||
}
|
}
|
||||||
@ -169,7 +169,7 @@ where
|
|||||||
inner_getters!(self.inner: V);
|
inner_getters!(self.inner: V);
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<V> View for ScrollView<V>
|
impl<V> View for Scroll<V>
|
||||||
where
|
where
|
||||||
V: View,
|
V: View,
|
||||||
{
|
{
|
@ -26,9 +26,9 @@ use std::rc::Rc;
|
|||||||
///
|
///
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// # use cursive::Cursive;
|
/// # use cursive::Cursive;
|
||||||
/// # use cursive::views::{SelectView, Dialog, TextView};
|
/// # use cursive::views::{Select, Dialog, Text};
|
||||||
/// # use cursive::align::HAlign;
|
/// # 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("Short", 1);
|
||||||
/// time_select.add_item("Medium", 5);
|
/// time_select.add_item("Medium", 5);
|
||||||
/// time_select.add_item("Long", 10);
|
/// time_select.add_item("Long", 10);
|
||||||
@ -36,7 +36,7 @@ use std::rc::Rc;
|
|||||||
/// time_select.set_on_submit(|s, time| {
|
/// time_select.set_on_submit(|s, time| {
|
||||||
/// s.pop_layer();
|
/// s.pop_layer();
|
||||||
/// let text = format!("You will wait for {} minutes...", time);
|
/// 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()));
|
/// .button("Quit", |s| s.quit()));
|
||||||
/// });
|
/// });
|
||||||
///
|
///
|
||||||
@ -44,7 +44,7 @@ use std::rc::Rc;
|
|||||||
/// siv.add_layer(Dialog::around(time_select)
|
/// siv.add_layer(Dialog::around(time_select)
|
||||||
/// .title("How long is your wait?"));
|
/// .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
|
// The core of the view: we store a list of items
|
||||||
// `Item` is more or less a `(String, Rc<T>)`.
|
// `Item` is more or less a `(String, Rc<T>)`.
|
||||||
items: Vec<Item<T>>,
|
items: Vec<Item<T>>,
|
||||||
@ -78,16 +78,16 @@ pub struct SelectView<T = String> {
|
|||||||
last_size: Vec2,
|
last_size: Vec2,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: 'static> Default for SelectView<T> {
|
impl<T: 'static> Default for Select<T> {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self::new()
|
Self::new()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: 'static> SelectView<T> {
|
impl<T: 'static> Select<T> {
|
||||||
/// Creates a new empty SelectView.
|
/// Creates a new empty Select.
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
SelectView {
|
Select {
|
||||||
items: Vec::new(),
|
items: Vec::new(),
|
||||||
enabled: true,
|
enabled: true,
|
||||||
focus: Rc::new(Cell::new(0)),
|
focus: Rc::new(Cell::new(0)),
|
||||||
@ -176,11 +176,11 @@ impl<T: 'static> SelectView<T> {
|
|||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// use cursive::traits::Identifiable;
|
/// 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("One", 1)
|
||||||
/// .item("Two", 2)
|
/// .item("Two", 2)
|
||||||
/// .on_select(|s, item| {
|
/// .on_select(|s, item| {
|
||||||
@ -191,7 +191,7 @@ impl<T: 'static> SelectView<T> {
|
|||||||
/// };
|
/// };
|
||||||
///
|
///
|
||||||
/// // Update the textview with the currently selected item.
|
/// // 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);
|
/// v.set_content(content);
|
||||||
/// }).unwrap();
|
/// }).unwrap();
|
||||||
/// });
|
/// });
|
||||||
@ -231,9 +231,9 @@ impl<T: 'static> SelectView<T> {
|
|||||||
/// # Examples
|
/// # 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("One", 1)
|
||||||
/// .item("Two", 2)
|
/// .item("Two", 2)
|
||||||
/// .on_submit(|s, item| {
|
/// .on_submit(|s, item| {
|
||||||
@ -261,9 +261,9 @@ impl<T: 'static> SelectView<T> {
|
|||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// use cursive::align;
|
/// use cursive::align;
|
||||||
/// use cursive::views::SelectView;
|
/// use cursive::views::Select;
|
||||||
///
|
///
|
||||||
/// let select_view = SelectView::new()
|
/// let select_view = Select::new()
|
||||||
/// .item("One", 1)
|
/// .item("One", 1)
|
||||||
/// .align(align::Align::top_center());
|
/// .align(align::Align::top_center());
|
||||||
/// ```
|
/// ```
|
||||||
@ -311,9 +311,9 @@ impl<T: 'static> SelectView<T> {
|
|||||||
/// # Examples
|
/// # 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 1", 1);
|
||||||
/// select_view.add_item("Item 2", 2);
|
/// select_view.add_item("Item 2", 2);
|
||||||
@ -326,8 +326,8 @@ impl<T: 'static> SelectView<T> {
|
|||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// use cursive::Cursive;
|
/// use cursive::Cursive;
|
||||||
/// use cursive::views::{SelectView, TextView};
|
/// use cursive::views::{Select, Text};
|
||||||
/// let select = SelectView::new()
|
/// let select = Select::new()
|
||||||
/// .item("Short", 1);
|
/// .item("Short", 1);
|
||||||
/// assert_eq!(select.get_item(0), Some(("Short", &1)));
|
/// assert_eq!(select.get_item(0), Some(("Short", &1)));
|
||||||
/// ```
|
/// ```
|
||||||
@ -391,9 +391,9 @@ impl<T: 'static> SelectView<T> {
|
|||||||
/// # Examples
|
/// # 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 1", 1)
|
||||||
/// .item("Item 2", 2)
|
/// .item("Item 2", 2)
|
||||||
/// .item("Surprise item", 42);
|
/// .item("Surprise item", 42);
|
||||||
@ -420,10 +420,10 @@ impl<T: 'static> SelectView<T> {
|
|||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// use cursive::views::SelectView;
|
/// use cursive::views::Select;
|
||||||
///
|
///
|
||||||
/// // Create a SelectView with 100 items
|
/// // Create a Select with 100 items
|
||||||
/// let select_view = SelectView::new()
|
/// let select_view = Select::new()
|
||||||
/// .with_all((1u8..100).into_iter().map(|i| {
|
/// .with_all((1u8..100).into_iter().map(|i| {
|
||||||
/// (format!("Item {}", i), i)
|
/// (format!("Item {}", i), i)
|
||||||
/// }));
|
/// }));
|
||||||
@ -463,9 +463,9 @@ impl<T: 'static> SelectView<T> {
|
|||||||
/// # Examples
|
/// # 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 1", 1)
|
||||||
/// .item("Item 2", 2)
|
/// .item("Item 2", 2)
|
||||||
/// .item("Item 3", 3);
|
/// .item("Item 3", 3);
|
||||||
@ -481,9 +481,9 @@ impl<T: 'static> SelectView<T> {
|
|||||||
/// # Examples
|
/// # 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());
|
/// assert!(select_view.is_empty());
|
||||||
///
|
///
|
||||||
/// select_view.add_item("Item 1", 1);
|
/// 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`.
|
/// You should run this callback with a `&mut Cursive`.
|
||||||
pub fn set_selection(&mut self, i: usize) -> Callback {
|
pub fn set_selection(&mut self, i: usize) -> Callback {
|
||||||
// TODO: Check if `i >= self.len()` ?
|
// 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?
|
// Or just cap the ID?
|
||||||
let i = if self.is_empty() {
|
let i = if self.is_empty() {
|
||||||
0
|
0
|
||||||
@ -582,8 +582,8 @@ impl<T: 'static> SelectView<T> {
|
|||||||
///
|
///
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// # use cursive::Cursive;
|
/// # use cursive::Cursive;
|
||||||
/// # use cursive::views::SelectView;
|
/// # use cursive::views::Select;
|
||||||
/// fn select_up(siv: &mut Cursive, view: &mut SelectView<()>) {
|
/// fn select_up(siv: &mut Cursive, view: &mut Select<()>) {
|
||||||
/// let cb = view.select_up(1);
|
/// let cb = view.select_up(1);
|
||||||
/// cb(siv);
|
/// 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.
|
/// Convenient method to use the label as value.
|
||||||
pub fn add_item_str<S: Into<String>>(&mut self, label: S) {
|
pub fn add_item_str<S: Into<String>>(&mut self, label: S) {
|
||||||
let label = label.into();
|
let label = label.into();
|
||||||
@ -789,9 +789,9 @@ impl SelectView<String> {
|
|||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// use cursive::views::SelectView;
|
/// use cursive::views::Select;
|
||||||
///
|
///
|
||||||
/// let select_view = SelectView::new()
|
/// let select_view = Select::new()
|
||||||
/// .item_str("Paris")
|
/// .item_str("Paris")
|
||||||
/// .item_str("New York")
|
/// .item_str("New York")
|
||||||
/// .item_str("Tokyo");
|
/// .item_str("Tokyo");
|
||||||
@ -814,8 +814,8 @@ impl SelectView<String> {
|
|||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// # use cursive::views::SelectView;
|
/// # use cursive::views::Select;
|
||||||
/// let mut select_view = SelectView::new();
|
/// let mut select_view = Select::new();
|
||||||
/// select_view.add_all_str(vec!["a", "b", "c"]);
|
/// select_view.add_all_str(vec!["a", "b", "c"]);
|
||||||
/// ```
|
/// ```
|
||||||
pub fn add_all_str<S, I>(&mut self, iter: I)
|
pub fn add_all_str<S, I>(&mut self, iter: I)
|
||||||
@ -835,11 +835,11 @@ impl SelectView<String> {
|
|||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// use cursive::views::SelectView;
|
/// use cursive::views::Select;
|
||||||
///
|
///
|
||||||
/// let text = "..."; // Maybe read some config file
|
/// let text = "..."; // Maybe read some config file
|
||||||
///
|
///
|
||||||
/// let select_view = SelectView::new()
|
/// let select_view = Select::new()
|
||||||
/// .with_all_str(text.lines());
|
/// .with_all_str(text.lines());
|
||||||
/// ```
|
/// ```
|
||||||
pub fn with_all_str<S, I>(self, iter: I) -> Self
|
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
|
where
|
||||||
T: Ord,
|
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<'_, '_>) {
|
fn draw(&self, printer: &Printer<'_, '_>) {
|
||||||
self.last_offset.set(printer.offset);
|
self.last_offset.set(printer.offset);
|
||||||
|
|
||||||
@ -987,7 +987,7 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn select_view_sorting() {
|
fn select_view_sorting() {
|
||||||
// We add items in no particular order, from going by their label.
|
// 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("Y");
|
||||||
view.add_item_str("Z");
|
view.add_item_str("Z");
|
||||||
view.add_item_str("X");
|
view.add_item_str("X");
|
||||||
@ -1009,7 +1009,7 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn select_view_sorting_with_comparator() {
|
fn select_view_sorting_with_comparator() {
|
||||||
// We add items in no particular order, from going by their value.
|
// 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("Y", 2);
|
||||||
view.add_item("Z", 1);
|
view.add_item("Z", 1);
|
||||||
view.add_item("X", 3);
|
view.add_item("X", 3);
|
||||||
@ -1036,7 +1036,7 @@ mod tests {
|
|||||||
key: i32,
|
key: i32,
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut view = SelectView::new();
|
let mut view = Select::new();
|
||||||
view.add_item("Y", MyStruct { key: 2 });
|
view.add_item("Y", MyStruct { key: 2 });
|
||||||
view.add_item("Z", MyStruct { key: 1 });
|
view.add_item("Z", MyStruct { key: 1 });
|
||||||
view.add_item("X", MyStruct { key: 3 });
|
view.add_item("X", MyStruct { key: 3 });
|
||||||
@ -1058,7 +1058,7 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn select_view_sorting_orderable_items() {
|
fn select_view_sorting_orderable_items() {
|
||||||
// We add items in no particular order, from going by their value.
|
// 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("Y", 2);
|
||||||
view.add_item("Z", 1);
|
view.add_item("Z", 1);
|
||||||
view.add_item("X", 3);
|
view.add_item("X", 3);
|
@ -7,17 +7,17 @@ use crate::Printer;
|
|||||||
/// Wrapper view that adds a shadow.
|
/// Wrapper view that adds a shadow.
|
||||||
///
|
///
|
||||||
/// It reserves a 1 pixel border on each side.
|
/// It reserves a 1 pixel border on each side.
|
||||||
pub struct ShadowView<T: View> {
|
pub struct Shadow<T: View> {
|
||||||
view: T,
|
view: T,
|
||||||
top_padding: bool,
|
top_padding: bool,
|
||||||
left_padding: bool,
|
left_padding: bool,
|
||||||
// TODO: invalidate if we change the padding?
|
// TODO: invalidate if we change the padding?
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: View> ShadowView<T> {
|
impl<T: View> Shadow<T> {
|
||||||
/// Wraps the given view.
|
/// Wraps the given view.
|
||||||
pub fn new(view: T) -> Self {
|
pub fn new(view: T) -> Self {
|
||||||
ShadowView {
|
Shadow {
|
||||||
view,
|
view,
|
||||||
top_padding: true,
|
top_padding: true,
|
||||||
left_padding: true,
|
left_padding: true,
|
||||||
@ -51,7 +51,7 @@ impl<T: View> ShadowView<T> {
|
|||||||
inner_getters!(self.view: 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);
|
wrap_impl!(self.view: T);
|
||||||
|
|
||||||
fn wrap_required_size(&mut self, req: Vec2) -> Vec2 {
|
fn wrap_required_size(&mut self, req: Vec2) -> Vec2 {
|
@ -14,9 +14,9 @@ use std::rc::Rc;
|
|||||||
/// # Examples
|
/// # 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 {
|
/// .on_change(|s, n| if n == 5 {
|
||||||
/// s.add_layer(Dialog::info("5! Pick 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))),
|
/// n => s.add_layer(Dialog::info(format!("Why {}? Why not 5?", n))),
|
||||||
/// });
|
/// });
|
||||||
/// ```
|
/// ```
|
||||||
pub struct SliderView {
|
pub struct Slider {
|
||||||
orientation: Orientation,
|
orientation: Orientation,
|
||||||
on_change: Option<Rc<dyn Fn(&mut Cursive, usize)>>,
|
on_change: Option<Rc<dyn Fn(&mut Cursive, usize)>>,
|
||||||
on_enter: 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,
|
dragging: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SliderView {
|
impl Slider {
|
||||||
/// Creates a new `SliderView` in the given orientation.
|
/// Creates a new `Slider` in the given orientation.
|
||||||
///
|
///
|
||||||
/// The view will have a fixed length of `max_value`,
|
/// The view will have a fixed length of `max_value`,
|
||||||
/// with one tick per block.
|
/// with one tick per block.
|
||||||
///
|
///
|
||||||
/// The actual range of values for this slider is `[0, max_value - 1]`.
|
/// The actual range of values for this slider is `[0, max_value - 1]`.
|
||||||
pub fn new(orientation: Orientation, max_value: usize) -> Self {
|
pub fn new(orientation: Orientation, max_value: usize) -> Self {
|
||||||
SliderView {
|
Slider {
|
||||||
orientation,
|
orientation,
|
||||||
value: 0,
|
value: 0,
|
||||||
max_value,
|
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 {
|
pub fn vertical(max_value: usize) -> Self {
|
||||||
Self::new(Orientation::Vertical, max_value)
|
Self::new(Orientation::Vertical, max_value)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates a new horizontal `SliderView`.
|
/// Creates a new horizontal `Slider`.
|
||||||
pub fn horizontal(max_value: usize) -> Self {
|
pub fn horizontal(max_value: usize) -> Self {
|
||||||
Self::new(Orientation::Horizontal, max_value)
|
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<'_, '_>) {
|
fn draw(&self, printer: &Printer<'_, '_>) {
|
||||||
match self.orientation {
|
match self.orientation {
|
||||||
Orientation::Vertical => {
|
Orientation::Vertical => {
|
@ -5,7 +5,7 @@ use crate::vec::Vec2;
|
|||||||
use crate::view::{
|
use crate::view::{
|
||||||
IntoBoxedView, Offset, Position, Selector, View, ViewWrapper,
|
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::Printer;
|
||||||
use crate::With;
|
use crate::With;
|
||||||
use std::cell;
|
use std::cell;
|
||||||
@ -13,7 +13,7 @@ use std::ops::Deref;
|
|||||||
|
|
||||||
/// Simple stack of views.
|
/// Simple stack of views.
|
||||||
/// Only the top-most view is active and can receive input.
|
/// Only the top-most view is active and can receive input.
|
||||||
pub struct StackView {
|
pub struct Stack {
|
||||||
// Store layers from back to front.
|
// Store layers from back to front.
|
||||||
layers: Vec<Child>,
|
layers: Vec<Child>,
|
||||||
last_size: Vec2,
|
last_size: Vec2,
|
||||||
@ -31,7 +31,7 @@ enum Placement {
|
|||||||
Fullscreen,
|
Fullscreen,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Identifies a layer in a `StackView`.
|
/// Identifies a layer in a `Stack`.
|
||||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||||
pub enum LayerPosition {
|
pub enum LayerPosition {
|
||||||
/// Starts from the back (bottom) of the stack.
|
/// Starts from the back (bottom) of the stack.
|
||||||
@ -64,7 +64,7 @@ impl Placement {
|
|||||||
/// A child view can be wrapped in multiple ways.
|
/// A child view can be wrapped in multiple ways.
|
||||||
enum ChildWrapper<T: View> {
|
enum ChildWrapper<T: View> {
|
||||||
// Some views include a shadow around.
|
// Some views include a shadow around.
|
||||||
Shadow(ShadowView<Layer<CircularFocus<T>>>),
|
Shadow(Shadow<Layer<CircularFocus<T>>>),
|
||||||
|
|
||||||
// Some include only include a background.
|
// Some include only include a background.
|
||||||
Backfilled(Layer<CircularFocus<T>>),
|
Backfilled(Layer<CircularFocus<T>>),
|
||||||
@ -77,7 +77,7 @@ impl<T: View> ChildWrapper<T> {
|
|||||||
fn unwrap(self) -> T {
|
fn unwrap(self) -> T {
|
||||||
match self {
|
match self {
|
||||||
// All these into_inner() can never fail.
|
// All these into_inner() can never fail.
|
||||||
// (ShadowView, Layer, CircularFocus)
|
// (Shadow, Layer, CircularFocus)
|
||||||
ChildWrapper::Shadow(shadow) => shadow
|
ChildWrapper::Shadow(shadow) => shadow
|
||||||
.into_inner()
|
.into_inner()
|
||||||
.ok()
|
.ok()
|
||||||
@ -199,7 +199,7 @@ impl<T: View> View for ChildWrapper<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
struct Child {
|
struct Child {
|
||||||
view: ChildWrapper<ViewBox>,
|
view: ChildWrapper<Boxed>,
|
||||||
size: Vec2,
|
size: Vec2,
|
||||||
placement: Placement,
|
placement: Placement,
|
||||||
|
|
||||||
@ -210,24 +210,24 @@ struct Child {
|
|||||||
virgin: bool,
|
virgin: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
new_default!(StackView);
|
new_default!(Stack);
|
||||||
|
|
||||||
impl StackView {
|
impl Stack {
|
||||||
/// Creates a new empty StackView
|
/// Creates a new empty Stack
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
StackView {
|
Stack {
|
||||||
layers: Vec::new(),
|
layers: Vec::new(),
|
||||||
last_size: Vec2::zero(),
|
last_size: Vec2::zero(),
|
||||||
bg_dirty: cell::Cell::new(true),
|
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 {
|
pub fn len(&self) -> usize {
|
||||||
self.layers.len()
|
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 {
|
pub fn is_empty(&self) -> bool {
|
||||||
self.layers.is_empty()
|
self.layers.is_empty()
|
||||||
}
|
}
|
||||||
@ -250,7 +250,7 @@ impl StackView {
|
|||||||
where
|
where
|
||||||
T: IntoBoxedView,
|
T: IntoBoxedView,
|
||||||
{
|
{
|
||||||
let boxed = ViewBox::boxed(view);
|
let boxed = Boxed::boxed(view);
|
||||||
self.layers.push(Child {
|
self.layers.push(Child {
|
||||||
view: ChildWrapper::Backfilled(Layer::new(
|
view: ChildWrapper::Backfilled(Layer::new(
|
||||||
CircularFocus::wrap_tab(boxed),
|
CircularFocus::wrap_tab(boxed),
|
||||||
@ -308,12 +308,12 @@ impl StackView {
|
|||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// # use cursive::views::{TextView, StackView, Dialog, LayerPosition};
|
/// # use cursive::views::{Text, Stack, Dialog, LayerPosition};
|
||||||
/// # use cursive::view::Identifiable;
|
/// # use cursive::view::Identifiable;
|
||||||
/// let mut stack = StackView::new();
|
/// let mut stack = Stack::new();
|
||||||
/// stack.add_layer(TextView::new("Back"));
|
/// stack.add_layer(Text::new("Back"));
|
||||||
/// stack.add_layer(Dialog::around(TextView::new("Middle").with_id("text")));
|
/// stack.add_layer(Dialog::around(Text::new("Middle").with_id("text")));
|
||||||
/// stack.add_layer(TextView::new("Front"));
|
/// stack.add_layer(Text::new("Front"));
|
||||||
///
|
///
|
||||||
/// assert_eq!(stack.find_layer_from_id("text"), Some(LayerPosition::FromBack(1)));
|
/// assert_eq!(stack.find_layer_from_id("text"), Some(LayerPosition::FromBack(1)));
|
||||||
/// ```
|
/// ```
|
||||||
@ -356,11 +356,11 @@ impl StackView {
|
|||||||
where
|
where
|
||||||
T: IntoBoxedView,
|
T: IntoBoxedView,
|
||||||
{
|
{
|
||||||
let boxed = ViewBox::boxed(view);
|
let boxed = Boxed::boxed(view);
|
||||||
self.layers.push(Child {
|
self.layers.push(Child {
|
||||||
// Skip padding for absolute/parent-placed views
|
// Skip padding for absolute/parent-placed views
|
||||||
view: ChildWrapper::Shadow(
|
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)
|
.top_padding(position.y == Offset::Center)
|
||||||
.left_padding(position.x == Offset::Center),
|
.left_padding(position.x == Offset::Center),
|
||||||
),
|
),
|
||||||
@ -383,7 +383,7 @@ impl StackView {
|
|||||||
where
|
where
|
||||||
T: IntoBoxedView,
|
T: IntoBoxedView,
|
||||||
{
|
{
|
||||||
let boxed = ViewBox::boxed(view);
|
let boxed = Boxed::boxed(view);
|
||||||
self.layers.push(Child {
|
self.layers.push(Child {
|
||||||
view: ChildWrapper::Plain(CircularFocus::wrap_tab(boxed)),
|
view: ChildWrapper::Plain(CircularFocus::wrap_tab(boxed)),
|
||||||
size: Vec2::new(0, 0),
|
size: Vec2::new(0, 0),
|
||||||
@ -402,7 +402,7 @@ impl StackView {
|
|||||||
self.with(|s| s.add_layer_at(position, view))
|
self.with(|s| s.add_layer_at(position, view))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Remove a layer from this `StackView`.
|
/// Remove a layer from this `Stack`.
|
||||||
///
|
///
|
||||||
/// # Panics
|
/// # Panics
|
||||||
///
|
///
|
||||||
@ -419,7 +419,7 @@ impl StackView {
|
|||||||
.pop()
|
.pop()
|
||||||
.map(|child| child.view)
|
.map(|child| child.view)
|
||||||
.map(ChildWrapper::unwrap)
|
.map(ChildWrapper::unwrap)
|
||||||
.map(ViewBox::unwrap)
|
.map(Boxed::unwrap)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Computes the offset of the current top view.
|
/// 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<'_, '_>) {
|
fn draw(&self, printer: &Printer<'_, '_>) {
|
||||||
// This function is included for compat with the view trait,
|
// This function is included for compat with the view trait,
|
||||||
// it should behave the same as calling them seperately, but does
|
// it should behave the same as calling them seperately, but does
|
||||||
@ -685,12 +685,12 @@ impl View for StackView {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::views::TextView;
|
use crate::views::Text;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn pop_add() {
|
fn pop_add() {
|
||||||
// Start with a simple stack
|
// 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
|
// And keep poping and re-pushing the view
|
||||||
for _ in 0..20 {
|
for _ in 0..20 {
|
||||||
@ -700,18 +700,18 @@ mod tests {
|
|||||||
|
|
||||||
// We want to make sure we don't add any layer of Box'ing
|
// We want to make sure we don't add any layer of Box'ing
|
||||||
let layer = stack.pop_layer().unwrap();
|
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_eq!(text.get_content().source(), "1");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn move_layer_works() {
|
fn move_layer_works() {
|
||||||
let mut stack = StackView::new()
|
let mut stack = Stack::new()
|
||||||
.layer(TextView::new("1"))
|
.layer(Text::new("1"))
|
||||||
.layer(TextView::new("2"))
|
.layer(Text::new("2"))
|
||||||
.layer(TextView::new("3"))
|
.layer(Text::new("3"))
|
||||||
.layer(TextView::new("4"));
|
.layer(Text::new("4"));
|
||||||
|
|
||||||
// Try moving views around, make sure we have the expected result
|
// Try moving views around, make sure we have the expected result
|
||||||
|
|
||||||
@ -734,19 +734,19 @@ mod tests {
|
|||||||
// 1,2,4,3
|
// 1,2,4,3
|
||||||
|
|
||||||
let layer = stack.pop_layer().unwrap();
|
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");
|
assert_eq!(text.get_content().source(), "3");
|
||||||
|
|
||||||
let layer = stack.pop_layer().unwrap();
|
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");
|
assert_eq!(text.get_content().source(), "4");
|
||||||
|
|
||||||
let layer = stack.pop_layer().unwrap();
|
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");
|
assert_eq!(text.get_content().source(), "2");
|
||||||
|
|
||||||
let layer = stack.pop_layer().unwrap();
|
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_eq!(text.get_content().source(), "1");
|
||||||
|
|
||||||
assert!(stack.pop_layer().is_none());
|
assert!(stack.pop_layer().is_none());
|
||||||
@ -754,29 +754,28 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn get() {
|
fn get() {
|
||||||
let mut stack = StackView::new()
|
let mut stack =
|
||||||
.layer(TextView::new("1"))
|
Stack::new().layer(Text::new("1")).layer(Text::new("2"));
|
||||||
.layer(TextView::new("2"));
|
|
||||||
|
|
||||||
assert!(stack
|
assert!(stack
|
||||||
.get(LayerPosition::FromFront(0))
|
.get(LayerPosition::FromFront(0))
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.as_any()
|
.as_any()
|
||||||
.is::<TextView>());
|
.is::<Text>());
|
||||||
assert!(stack
|
assert!(stack
|
||||||
.get(LayerPosition::FromBack(0))
|
.get(LayerPosition::FromBack(0))
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.as_any()
|
.as_any()
|
||||||
.is::<TextView>());
|
.is::<Text>());
|
||||||
assert!(stack
|
assert!(stack
|
||||||
.get_mut(LayerPosition::FromFront(0))
|
.get_mut(LayerPosition::FromFront(0))
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.as_any_mut()
|
.as_any_mut()
|
||||||
.is::<TextView>());
|
.is::<Text>());
|
||||||
assert!(stack
|
assert!(stack
|
||||||
.get_mut(LayerPosition::FromBack(0))
|
.get_mut(LayerPosition::FromBack(0))
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.as_any_mut()
|
.as_any_mut()
|
||||||
.is::<TextView>());
|
.is::<Text>());
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -15,18 +15,18 @@ use crate::{Printer, Vec2, With, XY};
|
|||||||
// Content type used internally for caching and storage
|
// Content type used internally for caching and storage
|
||||||
type InnerContentType = Arc<StyledString>;
|
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.
|
/// Cloning this object will still point to the same content.
|
||||||
///
|
///
|
||||||
/// [`TextView`]: struct.TextView.html
|
/// [`Text`]: struct.Text.html
|
||||||
///
|
///
|
||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// # use cursive::views::{TextView, TextContent};
|
/// # use cursive::views::{Text, TextContent};
|
||||||
/// let mut content = TextContent::new("content");
|
/// 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
|
/// // Later, possibly in a different thread
|
||||||
/// content.set_content("new content");
|
/// 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)
|
pub fn append<S>(&self, content: S)
|
||||||
where
|
where
|
||||||
S: Into<StyledString>,
|
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`.
|
/// This is mostly just a `StyledString`.
|
||||||
///
|
///
|
||||||
@ -175,12 +175,12 @@ impl TextContentInner {
|
|||||||
///
|
///
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// # use cursive::Cursive;
|
/// # use cursive::Cursive;
|
||||||
/// # use cursive::views::TextView;
|
/// # use cursive::views::Text;
|
||||||
/// let mut siv = Cursive::dummy();
|
/// 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: String,
|
||||||
content: TextContent,
|
content: TextContent,
|
||||||
rows: Vec<Row>,
|
rows: Vec<Row>,
|
||||||
@ -196,8 +196,8 @@ pub struct TextView {
|
|||||||
width: Option<usize>,
|
width: Option<usize>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TextView {
|
impl Text {
|
||||||
/// Creates a new TextView with the given content.
|
/// Creates a new Text with the given content.
|
||||||
pub fn new<S>(content: S) -> Self
|
pub fn new<S>(content: S) -> Self
|
||||||
where
|
where
|
||||||
S: Into<StyledString>,
|
S: Into<StyledString>,
|
||||||
@ -205,7 +205,7 @@ impl TextView {
|
|||||||
Self::new_with_content(TextContent::new(content))
|
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
|
/// If you kept a clone of the given content, you'll be able to update it
|
||||||
/// remotely.
|
/// remotely.
|
||||||
@ -213,16 +213,16 @@ impl TextView {
|
|||||||
/// # Examples
|
/// # Examples
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```rust
|
||||||
/// # use cursive::views::{TextView, TextContent};
|
/// # use cursive::views::{Text, TextContent};
|
||||||
/// let mut content = TextContent::new("content");
|
/// 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
|
/// // Later, possibly in a different thread
|
||||||
/// content.set_content("new content");
|
/// content.set_content("new content");
|
||||||
/// assert!(view.get_content().source().contains("new"));
|
/// assert!(view.get_content().source().contains("new"));
|
||||||
/// ```
|
/// ```
|
||||||
pub fn new_with_content(content: TextContent) -> Self {
|
pub fn new_with_content(content: TextContent) -> Self {
|
||||||
TextView {
|
Text {
|
||||||
content,
|
content,
|
||||||
effect: Effect::Simple,
|
effect: Effect::Simple,
|
||||||
rows: Vec::new(),
|
rows: Vec::new(),
|
||||||
@ -233,9 +233,9 @@ impl TextView {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates a new empty `TextView`.
|
/// Creates a new empty `Text`.
|
||||||
pub fn empty() -> Self {
|
pub fn empty() -> Self {
|
||||||
TextView::new("")
|
Text::new("")
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets the effect for the entire content.
|
/// Sets the effect for the entire content.
|
||||||
@ -309,7 +309,7 @@ impl TextView {
|
|||||||
self.content.set_content(content);
|
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)
|
pub fn append<S>(&mut self, content: S)
|
||||||
where
|
where
|
||||||
S: Into<StyledString>,
|
S: Into<StyledString>,
|
||||||
@ -359,7 +359,7 @@ impl TextView {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl View for TextView {
|
impl View for Text {
|
||||||
fn draw(&self, printer: &Printer<'_, '_>) {
|
fn draw(&self, printer: &Printer<'_, '_>) {
|
||||||
let h = self.rows.len();
|
let h = self.rows.len();
|
||||||
// If the content is smaller than the view, align it somewhere.
|
// If the content is smaller than the view, align it somewhere.
|
@ -1,40 +1,40 @@
|
|||||||
use crate::vec::Vec2;
|
use crate::vec::Vec2;
|
||||||
use crate::view::{View, ViewWrapper};
|
use crate::view::{View, ViewWrapper};
|
||||||
use crate::views::IdView;
|
use crate::views::Named;
|
||||||
use crate::Printer;
|
use crate::Printer;
|
||||||
use std::cell::Cell;
|
use std::cell::Cell;
|
||||||
|
|
||||||
/// Wrapper around a view that remembers its position.
|
/// Wrapper around a view that remembers its position.
|
||||||
pub struct TrackedView<T: View> {
|
pub struct Tracked<T: View> {
|
||||||
/// Wrapped view.
|
/// Wrapped view.
|
||||||
pub view: T,
|
pub view: T,
|
||||||
/// Last position the view was located.
|
/// Last position the view was located.
|
||||||
offset: Cell<Vec2>,
|
offset: Cell<Vec2>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: View> TrackedView<T> {
|
impl<T: View> Tracked<T> {
|
||||||
/// Return the last offset at which the view was drawn.
|
/// Return the last offset at which the view was drawn.
|
||||||
pub fn offset(&self) -> Vec2 {
|
pub fn offset(&self) -> Vec2 {
|
||||||
self.offset.get()
|
self.offset.get()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates a new `TrackedView` around `view`.
|
/// Creates a new `Tracked` around `view`.
|
||||||
pub fn new(view: T) -> Self {
|
pub fn new(view: T) -> Self {
|
||||||
TrackedView {
|
Tracked {
|
||||||
view,
|
view,
|
||||||
offset: Cell::new(Vec2::zero()),
|
offset: Cell::new(Vec2::zero()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Wraps itself in a `IdView` for easy retrieval.
|
/// Wraps itself in a `Named` for easy retrieval.
|
||||||
pub fn with_id(self, id: &str) -> IdView<Self> {
|
pub fn with_id(self, id: &str) -> Named<Self> {
|
||||||
IdView::new(id, self)
|
Named::new(id, self)
|
||||||
}
|
}
|
||||||
|
|
||||||
inner_getters!(self.view: T);
|
inner_getters!(self.view: T);
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: View> ViewWrapper for TrackedView<T> {
|
impl<T: View> ViewWrapper for Tracked<T> {
|
||||||
wrap_impl!(self.view: T);
|
wrap_impl!(self.view: T);
|
||||||
|
|
||||||
fn wrap_draw(&self, printer: &Printer<'_, '_>) {
|
fn wrap_draw(&self, printer: &Printer<'_, '_>) {
|
Loading…
Reference in New Issue
Block a user