2019-02-28 23:54:12 +00:00
|
|
|
use crate::event::{Event, EventResult};
|
2020-06-11 17:22:03 +00:00
|
|
|
use crate::rect::Rect;
|
2019-02-28 23:54:12 +00:00
|
|
|
use crate::view::{Margins, View, ViewWrapper};
|
|
|
|
use crate::Printer;
|
2020-01-06 23:51:38 +00:00
|
|
|
use crate::Vec2;
|
2018-11-19 04:05:31 +00:00
|
|
|
|
|
|
|
/// 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
|
2020-04-15 19:20:50 +00:00
|
|
|
/// # use cursive_core::views::{TextView, PaddedView};
|
2018-11-19 04:05:31 +00:00
|
|
|
/// // Adds 2 columns of padding to the left and to the right.
|
2020-01-10 18:51:48 +00:00
|
|
|
/// let view = PaddedView::lrtb(
|
|
|
|
/// 2,2,0,0, // Left, Right, Top, Bottom
|
2018-11-19 04:05:31 +00:00
|
|
|
/// TextView::new("Padded text")
|
|
|
|
/// );
|
|
|
|
/// ```
|
|
|
|
pub struct PaddedView<V> {
|
|
|
|
view: V,
|
|
|
|
margins: Margins,
|
|
|
|
}
|
|
|
|
|
2020-07-03 06:58:54 +00:00
|
|
|
impl<V> PaddedView<V> {
|
2018-11-19 04:05:31 +00:00
|
|
|
/// Wraps `view` in a new `PaddedView` with the given margins.
|
2020-01-10 18:51:48 +00:00
|
|
|
pub fn new(margins: Margins, view: V) -> Self {
|
2018-11-19 04:05:31 +00:00
|
|
|
PaddedView { view, margins }
|
|
|
|
}
|
|
|
|
|
2020-01-10 18:51:48 +00:00
|
|
|
/// Wraps `view` in a new `PaddedView` with the given margins.
|
|
|
|
pub fn lrtb(
|
|
|
|
left: usize,
|
|
|
|
right: usize,
|
|
|
|
top: usize,
|
|
|
|
bottom: usize,
|
|
|
|
view: V,
|
|
|
|
) -> Self {
|
|
|
|
Self::new(Margins::lrtb(left, right, top, bottom), view)
|
|
|
|
}
|
|
|
|
|
2018-11-19 04:05:31 +00:00
|
|
|
/// Sets the margins for this view.
|
2020-01-10 18:51:48 +00:00
|
|
|
pub fn set_margins(&mut self, margins: Margins) {
|
2020-06-11 17:22:03 +00:00
|
|
|
// TODO: invalidate? wrap_needs_relayout?
|
2020-01-10 18:51:48 +00:00
|
|
|
self.margins = margins;
|
2018-11-19 04:05:31 +00:00
|
|
|
}
|
2019-10-26 02:53:43 +00:00
|
|
|
|
|
|
|
inner_getters!(self.view: V);
|
2018-11-19 04:05:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2019-02-28 23:55:02 +00:00
|
|
|
fn wrap_draw(&self, printer: &Printer<'_, '_>) {
|
2018-11-19 04:05:31 +00:00
|
|
|
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);
|
|
|
|
}
|
2020-06-11 17:22:03 +00:00
|
|
|
|
|
|
|
fn wrap_important_area(&self, view_size: Vec2) -> Rect {
|
|
|
|
let inner_size = view_size.saturating_sub(self.margins.combined());
|
|
|
|
self.view.important_area(inner_size) + self.margins.top_left()
|
|
|
|
}
|
2018-11-19 04:05:31 +00:00
|
|
|
}
|