mirror of
https://github.com/FliegendeWurst/cursive.git
synced 2024-11-23 17:35:00 +00:00
Add HideableView
This commit is contained in:
parent
9569e991bf
commit
d52299a6e1
1
.gitignore
vendored
1
.gitignore
vendored
@ -5,3 +5,4 @@ tags
|
|||||||
.ctags
|
.ctags
|
||||||
*.bk
|
*.bk
|
||||||
TODO.txt
|
TODO.txt
|
||||||
|
*.rustfmt
|
||||||
|
94
src/views/hideable_view.rs
Normal file
94
src/views/hideable_view.rs
Normal file
@ -0,0 +1,94 @@
|
|||||||
|
use view::{Selector, View, ViewWrapper};
|
||||||
|
use With;
|
||||||
|
|
||||||
|
use std::any::Any;
|
||||||
|
|
||||||
|
/// Wrapper around another view that can be hidden at will.
|
||||||
|
///
|
||||||
|
/// By default, it simply forwards all calls to the inner view.
|
||||||
|
///
|
||||||
|
/// When hidden (with `HideableView::hide()`), it will appear as a zero-sized
|
||||||
|
/// invisible view, will not take focus and will not accept input.
|
||||||
|
///
|
||||||
|
/// It can be made visible again with `HideableView::unhide()`.
|
||||||
|
pub struct HideableView<V> {
|
||||||
|
view: V,
|
||||||
|
visible: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<V> HideableView<V> {
|
||||||
|
/// Creates a new HideableView around `view`.
|
||||||
|
///
|
||||||
|
/// It will be visible by default.
|
||||||
|
pub fn new(view: V) -> Self {
|
||||||
|
HideableView {
|
||||||
|
view,
|
||||||
|
visible: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sets the visibility for this view.
|
||||||
|
pub fn set_visible(&mut self, visible: bool) {
|
||||||
|
self.visible = visible;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sets the visibility for this view to `false`.
|
||||||
|
pub fn hide(&mut self) {
|
||||||
|
self.set_visible(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sets the visibility for this view to `true`.
|
||||||
|
pub fn unhide(&mut self) {
|
||||||
|
self.set_visible(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sets the visibility for this view to `false`.
|
||||||
|
///
|
||||||
|
/// Chainable variant.
|
||||||
|
pub fn hidden(self) -> Self {
|
||||||
|
self.with(Self::hide)
|
||||||
|
}
|
||||||
|
|
||||||
|
inner_getters!(self.view: V);
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<V: View> ViewWrapper for HideableView<V> {
|
||||||
|
type V = V;
|
||||||
|
|
||||||
|
fn with_view<F, R>(&self, f: F) -> Option<R>
|
||||||
|
where
|
||||||
|
F: FnOnce(&Self::V) -> R,
|
||||||
|
{
|
||||||
|
if self.visible {
|
||||||
|
Some(f(&self.view))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn with_view_mut<F, R>(&mut self, f: F) -> Option<R>
|
||||||
|
where
|
||||||
|
F: FnOnce(&mut Self::V) -> R,
|
||||||
|
{
|
||||||
|
if self.visible {
|
||||||
|
Some(f(&mut self.view))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn wrap_call_on_any<'a>(
|
||||||
|
&mut self, selector: &Selector, callback: Box<FnMut(&mut Any) + 'a>,
|
||||||
|
) {
|
||||||
|
// We always run callbacks, even when invisible.
|
||||||
|
self.view.call_on_any(selector, callback)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn into_inner(self) -> Result<Self::V, Self>
|
||||||
|
where
|
||||||
|
Self: Sized,
|
||||||
|
Self::V: Sized,
|
||||||
|
{
|
||||||
|
Ok(self.view)
|
||||||
|
}
|
||||||
|
}
|
@ -42,6 +42,7 @@ mod checkbox;
|
|||||||
mod dialog;
|
mod dialog;
|
||||||
mod dummy;
|
mod dummy;
|
||||||
mod edit_view;
|
mod edit_view;
|
||||||
|
mod hideable_view;
|
||||||
mod id_view;
|
mod id_view;
|
||||||
mod layer;
|
mod layer;
|
||||||
mod linear_layout;
|
mod linear_layout;
|
||||||
@ -69,6 +70,7 @@ pub use self::checkbox::Checkbox;
|
|||||||
pub use self::dialog::{Dialog, DialogFocus};
|
pub use self::dialog::{Dialog, DialogFocus};
|
||||||
pub use self::dummy::DummyView;
|
pub use self::dummy::DummyView;
|
||||||
pub use self::edit_view::EditView;
|
pub use self::edit_view::EditView;
|
||||||
|
pub use self::hideable_view::HideableView;
|
||||||
pub use self::id_view::{IdView, ViewRef};
|
pub use self::id_view::{IdView, ViewRef};
|
||||||
pub use self::layer::Layer;
|
pub use self::layer::Layer;
|
||||||
pub use self::linear_layout::LinearLayout;
|
pub use self::linear_layout::LinearLayout;
|
||||||
|
Loading…
Reference in New Issue
Block a user