Remove From implementations for Margins

This commit is contained in:
Alexandre Bury 2020-01-10 10:51:48 -08:00
parent 29f85a4398
commit d499bab941
9 changed files with 120 additions and 70 deletions

View File

@ -8,6 +8,8 @@
`View::call_on_any` no longer need to box their closures.
- Remove `BoxView::squishable`.
- Update crossterm to 0.14.
- Removed `From` implementations for `Margins`. Use `Margins::lrtb` and the like instead.
- Or `Dialog::padding_lrtb`.
- Renamed multiple types (old names are still re-exported, but deprecated):
- `BoxView` -> `ResizedView`
- `ViewBox` -> `BoxedView`
@ -34,6 +36,10 @@
- `LinearLayout` can now directly add boxed views without re-boxing.
- Add inner getters to `EnableableView`.
- Add `PaddedView::get_inner(_mut)`.
- Add a bunch of constructors for `Margins`.
- Add `Dialog::padding_lrtb`
- Add `Dialog::set_padding*`
- Add `PaddedView::lrtb`
### Improvements

View File

@ -12,7 +12,7 @@ fn main() {
Dialog::new()
.title("Enter your name")
// Padding is (left, right, top, bottom)
.padding((1, 1, 1, 0))
.padding_lrtb(1, 1, 1, 0)
.content(
EditView::new()
// Call `show_popup` when the user presses `Enter`

View File

@ -14,7 +14,7 @@ fn main() {
siv.add_layer(
Dialog::new()
.title("Minesweeper")
.padding((2, 2, 1, 1))
.padding_lrtb(2, 2, 1, 1)
.content(
LinearLayout::vertical()
.child(Button::new_raw(" New game ", show_options))

View File

@ -20,7 +20,7 @@ fn main() {
siv.add_layer(
Dialog::new()
.title("Progress bar example")
.padding((0, 0, 1, 1))
.padding_lrtb(0, 0, 1, 1)
.content(Button::new("Start", phase_1)),
);

View File

@ -11,7 +11,7 @@ fn main() {
siv.add_layer(
Dialog::new()
.title("Describe your issue")
.padding((1, 1, 1, 0))
.padding_lrtb(1, 1, 1, 0)
.content(TextArea::new().with_name("text"))
.button("Ok", Cursive::quit),
);

View File

@ -16,7 +16,15 @@ pub struct Margins {
impl Margins {
/// Creates a new Margins.
#[deprecated(
note = "`Margins::new()` is ambiguous. Use `Margins::lrtb()` instead."
)]
pub fn new(left: usize, right: usize, top: usize, bottom: usize) -> Self {
Self::lrtb(left, right, top, bottom)
}
/// Creates a new `Margins` object from the Left, Right, Top, Bottom fields.
pub fn lrtb(left: usize, right: usize, top: usize, bottom: usize) -> Self {
Margins {
left,
right,
@ -25,6 +33,30 @@ impl Margins {
}
}
/// Creates a new `Margins` object from the Left, Top, Right, Bottom fields.
pub fn ltrb(left_top: Vec2, right_bottom: Vec2) -> Self {
Self::lrtb(left_top.x, right_bottom.x, left_top.y, right_bottom.y)
}
/// Creates a new `Margins` object from the Top, Right, Bottom, Left fields.
pub fn trbl(top: usize, right: usize, bottom: usize, left: usize) -> Self {
Self::lrtb(left, right, top, bottom)
}
/// Creates a new `Margins` object from the Left and Right fields.
///
/// Top and Bottom will be 0.
pub fn lr(left: usize, right: usize) -> Self {
Self::lrtb(left, right, 0, 0)
}
/// Creates a new `Margins` object from the Top and Bottom fields.
///
/// Left and Right will be 0.
pub fn tb(top: usize, bottom: usize) -> Self {
Self::lrtb(0, 0, top, bottom)
}
/// Returns left + right.
pub fn horizontal(&self) -> usize {
self.left + self.right
@ -51,61 +83,28 @@ impl Margins {
}
}
impl From<(usize, usize, usize, usize)> for Margins {
fn from(
(left, right, top, bottom): (usize, usize, usize, usize),
) -> Margins {
Margins::new(left, right, top, bottom)
}
}
impl From<(i32, i32, i32, i32)> for Margins {
fn from((left, right, top, bottom): (i32, i32, i32, i32)) -> Margins {
(left as usize, right as usize, top as usize, bottom as usize).into()
}
}
impl From<((i32, i32), (i32, i32))> for Margins {
fn from(
((left, right), (top, bottom)): ((i32, i32), (i32, i32)),
) -> Margins {
(left, right, top, bottom).into()
}
}
impl From<((usize, usize), (usize, usize))> for Margins {
fn from(
((left, right), (top, bottom)): ((usize, usize), (usize, usize)),
) -> Margins {
(left, right, top, bottom).into()
}
}
impl<T: Into<Margins>> Add<T> for Margins {
impl Add<Margins> for Margins {
type Output = Margins;
fn add(self, other: T) -> Margins {
let ov = other.into();
fn add(self, other: Margins) -> Margins {
Margins {
left: self.left + ov.left,
right: self.right + ov.right,
top: self.top + ov.top,
bottom: self.bottom + ov.bottom,
left: self.left + other.left,
right: self.right + other.right,
top: self.top + other.top,
bottom: self.bottom + other.bottom,
}
}
}
impl<T: Into<Margins>> Sub<T> for Margins {
impl Sub<Margins> for Margins {
type Output = Margins;
fn sub(self, other: T) -> Margins {
let ov = other.into();
fn sub(self, other: Margins) -> Margins {
Margins {
left: self.left - ov.left,
right: self.right - ov.right,
top: self.top - ov.top,
bottom: self.bottom - ov.bottom,
left: self.left - other.left,
right: self.right - other.right,
top: self.top - other.top,
bottom: self.bottom - other.bottom,
}
}
}

View File

@ -96,8 +96,8 @@ impl Dialog {
title: String::new(),
title_position: HAlign::Center,
focus: DialogFocus::Content,
padding: Margins::new(1, 1, 0, 0),
borders: Margins::new(1, 1, 1, 1),
padding: Margins::lr(1, 1),
borders: Margins::lrtb(1, 1, 1, 1),
align: Align::top_right(),
invalidated: true,
}
@ -296,38 +296,73 @@ impl Dialog {
/// # Examples
/// ```
/// use cursive::views::Dialog;
/// use cursive::view::Margins;
///
/// let dialog = Dialog::info("Hello!")
/// .padding(((1, 1), (0, 0))); // ((Left, Right), (Top, Bottom))
/// .padding(Margins::lrtb(1, 1, 0, 0)); // (Left, Right, Top, Bottom)
/// ```
pub fn padding<T: Into<Margins>>(mut self, padding: T) -> Self {
self.padding = padding.into();
pub fn padding(self, padding: Margins) -> Self {
self.with(|s| s.set_padding(padding))
}
self
/// Sets the padding in the dialog.
///
/// Takes Left, Right, Top, Bottom fields.
pub fn padding_lrtb(
self,
left: usize,
right: usize,
top: usize,
bottom: usize,
) -> Self {
self.padding(Margins::lrtb(left, right, top, bottom))
}
/// Sets the padding in the dialog (around content and buttons).
///
/// Chainable variant.
pub fn set_padding(&mut self, padding: Margins) {
self.padding = padding;
}
/// Sets the top padding in the dialog (under the title).
pub fn padding_top(mut self, padding: usize) -> Self {
pub fn padding_top(self, padding: usize) -> Self {
self.with(|s| s.set_padding_top(padding))
}
/// Sets the top padding in the dialog (under the title).
pub fn set_padding_top(&mut self, padding: usize) {
self.padding.top = padding;
self
}
/// Sets the bottom padding in the dialog (under buttons).
pub fn padding_bottom(mut self, padding: usize) -> Self {
pub fn padding_bottom(self, padding: usize) -> Self {
self.with(|s| s.set_padding_bottom(padding))
}
/// Sets the bottom padding in the dialog (under buttons).
pub fn set_padding_bottom(&mut self, padding: usize) {
self.padding.bottom = padding;
self
}
/// Sets the left padding in the dialog.
pub fn padding_left(mut self, padding: usize) -> Self {
pub fn padding_left(self, padding: usize) -> Self {
self.with(|s| s.set_padding_left(padding))
}
/// Sets the left padding in the dialog.
pub fn set_padding_left(&mut self, padding: usize) {
self.padding.left = padding;
self
}
/// Sets the right padding in the dialog.
pub fn padding_right(mut self, padding: usize) -> Self {
pub fn padding_right(self, padding: usize) -> Self {
self.with(|s| s.set_padding_right(padding))
}
/// Sets the right padding in the dialog.
pub fn set_padding_right(&mut self, padding: usize) {
self.padding.right = padding;
self
}
/// Returns an iterator on this buttons for this dialog.

View File

@ -42,7 +42,7 @@ pub type OnSubmit = dyn Fn(&mut Cursive, &str);
/// siv.add_layer(
/// Dialog::new()
/// .title("Enter your name")
/// .padding((1, 1, 1, 0))
/// .padding_lrtb(1, 1, 1, 0)
/// .content(
/// EditView::new()
/// .on_submit(show_popup)

View File

@ -14,8 +14,8 @@ use crate::Vec2;
/// ```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))
/// let view = PaddedView::lrtb(
/// 2,2,0,0, // Left, Right, Top, Bottom
/// TextView::new("Padded text")
/// );
/// ```
@ -26,15 +26,25 @@ pub struct PaddedView<V> {
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();
pub fn new(margins: Margins, view: V) -> Self {
PaddedView { view, margins }
}
/// 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)
}
/// Sets the margins for this view.
pub fn set_margins<M: Into<Margins>>(&mut self, margins: M) {
pub fn set_margins(&mut self, margins: Margins) {
// TODO: invalidate?
self.margins = margins.into();
self.margins = margins;
}
inner_getters!(self.view: V);