mirror of
https://github.com/FliegendeWurst/cursive.git
synced 2024-11-12 20:23:35 +00:00
Add PaddedView
This commit is contained in:
parent
8e016a7f0a
commit
7a58970163
@ -51,6 +51,7 @@ mod list_view;
|
|||||||
mod menu_popup;
|
mod menu_popup;
|
||||||
mod menubar;
|
mod menubar;
|
||||||
mod on_event_view;
|
mod on_event_view;
|
||||||
|
mod padded_view;
|
||||||
mod panel;
|
mod panel;
|
||||||
mod progress_bar;
|
mod progress_bar;
|
||||||
mod radio;
|
mod radio;
|
||||||
@ -81,6 +82,7 @@ pub use self::list_view::{ListChild, ListView};
|
|||||||
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::on_event_view::OnEventView;
|
||||||
|
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;
|
||||||
pub use self::radio::{RadioButton, RadioGroup};
|
pub use self::radio::{RadioButton, RadioGroup};
|
||||||
|
65
src/views/padded_view.rs
Normal file
65
src/views/padded_view.rs
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
use event::{Event, EventResult};
|
||||||
|
use vec::Vec2;
|
||||||
|
use view::{Margins, View, ViewWrapper};
|
||||||
|
use Printer;
|
||||||
|
|
||||||
|
/// Adds padding to another view.
|
||||||
|
///
|
||||||
|
/// This view wraps another view and adds some padding.
|
||||||
|
///
|
||||||
|
/// The wrapped view will see a reduced space available.
|
||||||
|
///
|
||||||
|
/// # Examples
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// # use cursive::views::{TextView, PaddedView};
|
||||||
|
/// // Adds 2 columns of padding to the left and to the right.
|
||||||
|
/// let view = PaddedView::new(
|
||||||
|
/// ((2,2), (0,0)), // ((left, right), (top, bottom))
|
||||||
|
/// TextView::new("Padded text")
|
||||||
|
/// );
|
||||||
|
/// ```
|
||||||
|
pub struct PaddedView<V> {
|
||||||
|
view: V,
|
||||||
|
margins: Margins,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<V: View> PaddedView<V> {
|
||||||
|
/// Wraps `view` in a new `PaddedView` with the given margins.
|
||||||
|
pub fn new<M: Into<Margins>>(margins: M, view: V) -> Self {
|
||||||
|
let margins = margins.into();
|
||||||
|
PaddedView { view, margins }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Sets the margins for this view.
|
||||||
|
pub fn set_margins<M: Into<Margins>>(&mut self, margins: M) {
|
||||||
|
// TODO: invalidate?
|
||||||
|
self.margins = margins.into();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<V: View> ViewWrapper for PaddedView<V> {
|
||||||
|
wrap_impl!(self.view: V);
|
||||||
|
|
||||||
|
fn wrap_required_size(&mut self, req: Vec2) -> Vec2 {
|
||||||
|
let margins = self.margins.combined();
|
||||||
|
self.view.required_size(req.saturating_sub(margins)) + margins
|
||||||
|
}
|
||||||
|
|
||||||
|
fn wrap_layout(&mut self, size: Vec2) {
|
||||||
|
let margins = self.margins.combined();
|
||||||
|
self.view.layout(size.saturating_sub(margins));
|
||||||
|
}
|
||||||
|
|
||||||
|
fn wrap_on_event(&mut self, event: Event) -> EventResult {
|
||||||
|
let padding = self.margins.top_left();
|
||||||
|
self.view.on_event(event.relativized(padding))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn wrap_draw(&self, printer: &Printer) {
|
||||||
|
let top_left = self.margins.top_left();
|
||||||
|
let bot_right = self.margins.bot_right();
|
||||||
|
let printer = &printer.offset(top_left).shrinked(bot_right);
|
||||||
|
self.view.draw(printer);
|
||||||
|
}
|
||||||
|
}
|
@ -11,6 +11,7 @@ pub struct ShadowView<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?
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: View> ShadowView<T> {
|
impl<T: View> ShadowView<T> {
|
||||||
|
Loading…
Reference in New Issue
Block a user