Add EnableableView

This commit is contained in:
Alexandre Bury 2018-11-09 14:19:37 -08:00
parent 193f098ee0
commit edeb7a8c13
2 changed files with 43 additions and 0 deletions

View File

@ -0,0 +1,41 @@
use event::{Event, EventResult};
use view::{View, ViewWrapper};
use {Printer, With};
/// Wrapper around another view that can be enabled/disabled at will.
///
/// When disabled, all child views will be disabled and will stop receiving events.
pub struct EnableableView<V> {
view: V,
enabled: bool,
}
impl<V> EnableableView<V> {
/// Creates a new `EnableableView` around `view`.
///
/// It will be enabled by default.
pub fn new(view: V) -> Self {
EnableableView {
view,
enabled: true,
}
}
impl_enabled!(self.enabled);
}
impl<V: View> ViewWrapper for EnableableView<V> {
wrap_impl!(self.view: V);
fn wrap_on_event(&mut self, event: Event) -> EventResult {
if self.enabled {
self.view.on_event(event)
} else {
EventResult::Ignored
}
}
fn wrap_draw(&self, printer: &Printer) {
self.view.draw(&printer.enabled(self.enabled));
}
}

View File

@ -42,6 +42,7 @@ mod checkbox;
mod dialog;
mod dummy;
mod edit_view;
mod enableable_view;
mod hideable_view;
mod id_view;
mod layer;
@ -71,6 +72,7 @@ pub use self::checkbox::Checkbox;
pub use self::dialog::{Dialog, DialogFocus};
pub use self::dummy::DummyView;
pub use self::edit_view::EditView;
pub use self::enableable_view::EnableableView;
pub use self::hideable_view::HideableView;
pub use self::id_view::{IdView, ViewRef};
pub use self::layer::Layer;