From d499bab94142cb9a0c16dcd6e9977d8cdc47491a Mon Sep 17 00:00:00 2001 From: Alexandre Bury Date: Fri, 10 Jan 2020 10:51:48 -0800 Subject: [PATCH] Remove From implementations for Margins --- CHANGELOG.md | 6 +++ examples/edit.rs | 2 +- examples/mines/main.rs | 2 +- examples/progress.rs | 2 +- examples/text_area.rs | 2 +- src/view/margins.rs | 89 ++++++++++++++++++++-------------------- src/views/dialog.rs | 63 +++++++++++++++++++++------- src/views/edit_view.rs | 2 +- src/views/padded_view.rs | 22 +++++++--- 9 files changed, 120 insertions(+), 70 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9809fa2..d06f009 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/examples/edit.rs b/examples/edit.rs index 4c8012c..2934842 100644 --- a/examples/edit.rs +++ b/examples/edit.rs @@ -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` diff --git a/examples/mines/main.rs b/examples/mines/main.rs index 5df855b..9e978c7 100644 --- a/examples/mines/main.rs +++ b/examples/mines/main.rs @@ -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)) diff --git a/examples/progress.rs b/examples/progress.rs index b6a5b3f..79778b2 100644 --- a/examples/progress.rs +++ b/examples/progress.rs @@ -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)), ); diff --git a/examples/text_area.rs b/examples/text_area.rs index 22272dd..b1a5029 100644 --- a/examples/text_area.rs +++ b/examples/text_area.rs @@ -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), ); diff --git a/src/view/margins.rs b/src/view/margins.rs index a8fbaaf..a97eba8 100644 --- a/src/view/margins.rs +++ b/src/view/margins.rs @@ -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> Add for Margins { +impl Add 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> Sub for Margins { +impl Sub 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, } } } diff --git a/src/views/dialog.rs b/src/views/dialog.rs index a8ec988..9440fe3 100644 --- a/src/views/dialog.rs +++ b/src/views/dialog.rs @@ -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>(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. diff --git a/src/views/edit_view.rs b/src/views/edit_view.rs index 63f6daa..eb07a39 100644 --- a/src/views/edit_view.rs +++ b/src/views/edit_view.rs @@ -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) diff --git a/src/views/padded_view.rs b/src/views/padded_view.rs index 0a3c9c6..26dcc22 100644 --- a/src/views/padded_view.rs +++ b/src/views/padded_view.rs @@ -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 { impl PaddedView { /// Wraps `view` in a new `PaddedView` with the given margins. - pub fn new>(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>(&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);