Add OnLayoutView

This commit is contained in:
Alexandre Bury 2020-06-29 16:42:14 -07:00
parent bd6386fd74
commit 81e64da72a
3 changed files with 63 additions and 0 deletions

View File

@ -6,6 +6,8 @@
- Add `Dialog::into_content`. - Add `Dialog::into_content`.
- Add `Callback::from_fn_once` and `once1!` macro to wrap a `FnOnce` in a `FnMut`. - Add `Callback::from_fn_once` and `once1!` macro to wrap a `FnOnce` in a `FnMut`.
- Add `FixedLayoutView` with manual placement of child views.
- Add `OnLayoutView` to override `View::Layout`
### Bugfixes ### Bugfixes

View File

@ -79,6 +79,7 @@ mod menu_popup;
mod menubar; mod menubar;
mod named_view; mod named_view;
mod on_event_view; mod on_event_view;
mod on_layout_view;
mod padded_view; mod padded_view;
mod panel; mod panel;
mod progress_bar; mod progress_bar;
@ -114,6 +115,7 @@ pub use self::menu_popup::MenuPopup;
pub use self::menubar::Menubar; pub use self::menubar::Menubar;
pub use self::named_view::{NamedView, ViewRef}; pub use self::named_view::{NamedView, ViewRef};
pub use self::on_event_view::OnEventView; pub use self::on_event_view::OnEventView;
pub use self::on_layout_view::OnLayoutView;
pub use self::padded_view::PaddedView; pub use self::padded_view::PaddedView;
pub use self::panel::Panel; pub use self::panel::Panel;
pub use self::progress_bar::ProgressBar; pub use self::progress_bar::ProgressBar;

View File

@ -0,0 +1,59 @@
use crate::{view::ViewWrapper, Vec2, View};
/// View wrapper overriding the `View::layout` method.
pub struct OnLayoutView<V> {
view: V,
on_layout: Box<dyn FnMut(&mut V, Vec2)>,
}
impl<V> OnLayoutView<V> {
/// Wraps a view in an `OnLayoutView`.
///
/// Will run the given closure for layout _instead_ of the one from `view`.
///
/// ```rust
/// use cursive_core::{View, views::{TextView, OnLayoutView}};
///
/// let view = TextView::new("foo");
///
/// // Here we just run the innver view's layout.
/// OnLayoutView::new(view, |v, s| v.layout(s));
/// ```
pub fn new<F>(view: V, on_layout: F) -> Self
where
F: FnMut(&mut V, Vec2) + 'static,
{
let on_layout = Box::new(on_layout);
OnLayoutView { view, on_layout }
}
/// Wraps a view in an `OnLayoutView`.
///
/// This is a shortcut for `Self::new(view, V::layout)`
///
/// You can change it later with `set_on_layout`.
pub fn wrap(view: V) -> Self
where
V: View,
{
Self::new(view, V::layout)
}
/// Replaces the callback to run.
pub fn set_on_layout<F>(&mut self, on_layout: F)
where
F: FnMut(&mut V, Vec2) + 'static,
{
self.on_layout = Box::new(on_layout);
}
inner_getters!(self.view: V);
}
impl<V: View> ViewWrapper for OnLayoutView<V> {
wrap_impl!(self.view: V);
fn wrap_layout(&mut self, size: Vec2) {
(self.on_layout)(&mut self.view, size);
}
}