perf(rect)!: Rect::inner takes Margin directly instead of reference (#1008)

BREAKING CHANGE: Margin needs to be passed without reference now.

```diff
-let area = area.inner(&Margin {
+let area = area.inner(Margin {
     vertical: 0,
     horizontal: 2,
 });
```
This commit is contained in:
EdJoPaTo 2024-05-26 19:44:46 +02:00 committed by GitHub
parent 7a48c5b11b
commit 8b447ec4d6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 48 additions and 40 deletions

View file

@ -11,8 +11,8 @@ GitHub with a [breaking change] label.
This is a quick summary of the sections below: This is a quick summary of the sections below:
- [Unreleased](#unreleased) - [Unreleased](#unreleased)
- `Rect::inner` takes `Margin` directly instead of reference
- `Stylize::bg()` now accepts `Into<Color>` - `Stylize::bg()` now accepts `Into<Color>`
- [v0.27.0 (unreleased)](#v0270-unreleased)
- Removed deprecated `List::start_corner` - Removed deprecated `List::start_corner`
- [v0.26.0](#v0260) - [v0.26.0](#v0260)
- `Flex::Start` is the new default flex mode for `Layout` - `Flex::Start` is the new default flex mode for `Layout`
@ -53,17 +53,30 @@ This is a quick summary of the sections below:
## Unreleased ## Unreleased
### `Rect::inner` takes `Margin` directly instead of reference ([#1008])
[#1008]: https://github.com/ratatui-org/ratatui/pull/1008
`Margin` needs to be passed without reference now.
```diff
-let area = area.inner(&Margin {
+let area = area.inner(Margin {
vertical: 0,
horizontal: 2,
});
```
### `Stylize::bg()` now accepts `Into<Color>` ([#1103]) ### `Stylize::bg()` now accepts `Into<Color>` ([#1103])
[#1099]: https://github.com/ratatui-org/ratatui/pull/1103 [#1103]: https://github.com/ratatui-org/ratatui/pull/1103
Previously, `Stylize::bg()` accepted `Color` but now accepts `Into<Color>`. This allows more flexible types from calling scopes, though it can break some type inference in the calling scope. Previously, `Stylize::bg()` accepted `Color` but now accepts `Into<Color>`. This allows more
flexible types from calling scopes, though it can break some type inference in the calling scope.
## v0.27.0 (unreleased) ### Remove deprecated `List::start_corner` and `layout::Corner` ([#757])
### Remove deprecated `List::start_corner` and `layout::Corner` ([#758]) [#757]: https://github.com/ratatui-org/ratatui/pull/757
[#758]: https://github.com/ratatui-org/ratatui/pull/757
`List::start_corner` was deprecated in v0.25. Use `List::direction` and `ListDirection` instead. `List::start_corner` was deprecated in v0.25. Use `List::direction` and `ListDirection` instead.

View file

@ -64,20 +64,16 @@ impl Widget for AboutTab {
} }
fn render_crate_description(area: Rect, buf: &mut Buffer) { fn render_crate_description(area: Rect, buf: &mut Buffer) {
let area = area.inner( let area = area.inner(Margin {
&(Margin { vertical: 4,
vertical: 4, horizontal: 2,
horizontal: 2, });
}),
);
Clear.render(area, buf); // clear out the color swatches Clear.render(area, buf); // clear out the color swatches
Block::new().style(THEME.content).render(area, buf); Block::new().style(THEME.content).render(area, buf);
let area = area.inner( let area = area.inner(Margin {
&(Margin { vertical: 1,
vertical: 1, horizontal: 2,
horizontal: 2, });
}),
);
let text = "- cooking up terminal user interfaces - let text = "- cooking up terminal user interfaces -
Ratatui is a Rust crate that provides widgets (e.g. Paragraph, Table) and draws them to the \ Ratatui is a Rust crate that provides widgets (e.g. Paragraph, Table) and draws them to the \
@ -108,7 +104,7 @@ pub fn render_logo(selected_row: usize, area: Rect, buf: &mut Buffer) {
} else { } else {
THEME.logo.rat_eye_alt THEME.logo.rat_eye_alt
}; };
let area = area.inner(&Margin { let area = area.inner(Margin {
vertical: 0, vertical: 0,
horizontal: 2, horizontal: 2,
}); });

View file

@ -59,7 +59,7 @@ impl EmailTab {
impl Widget for EmailTab { impl Widget for EmailTab {
fn render(self, area: Rect, buf: &mut Buffer) { fn render(self, area: Rect, buf: &mut Buffer) {
RgbSwatch.render(area, buf); RgbSwatch.render(area, buf);
let area = area.inner(&Margin { let area = area.inner(Margin {
vertical: 1, vertical: 1,
horizontal: 2, horizontal: 2,
}); });

View file

@ -105,7 +105,7 @@ impl RecipeTab {
impl Widget for RecipeTab { impl Widget for RecipeTab {
fn render(self, area: Rect, buf: &mut Buffer) { fn render(self, area: Rect, buf: &mut Buffer) {
RgbSwatch.render(area, buf); RgbSwatch.render(area, buf);
let area = area.inner(&Margin { let area = area.inner(Margin {
vertical: 1, vertical: 1,
horizontal: 2, horizontal: 2,
}); });
@ -124,7 +124,7 @@ impl Widget for RecipeTab {
}; };
render_scrollbar(self.row_index, scrollbar_area, buf); render_scrollbar(self.row_index, scrollbar_area, buf);
let area = area.inner(&Margin { let area = area.inner(Margin {
horizontal: 2, horizontal: 2,
vertical: 1, vertical: 1,
}); });

View file

@ -26,7 +26,7 @@ impl TracerouteTab {
impl Widget for TracerouteTab { impl Widget for TracerouteTab {
fn render(self, area: Rect, buf: &mut Buffer) { fn render(self, area: Rect, buf: &mut Buffer) {
RgbSwatch.render(area, buf); RgbSwatch.render(area, buf);
let area = area.inner(&Margin { let area = area.inner(Margin {
vertical: 1, vertical: 1,
horizontal: 2, horizontal: 2,
}); });

View file

@ -28,14 +28,14 @@ impl WeatherTab {
impl Widget for WeatherTab { impl Widget for WeatherTab {
fn render(self, area: Rect, buf: &mut Buffer) { fn render(self, area: Rect, buf: &mut Buffer) {
RgbSwatch.render(area, buf); RgbSwatch.render(area, buf);
let area = area.inner(&Margin { let area = area.inner(Margin {
vertical: 1, vertical: 1,
horizontal: 2, horizontal: 2,
}); });
Clear.render(area, buf); Clear.render(area, buf);
Block::new().style(THEME.content).render(area, buf); Block::new().style(THEME.content).render(area, buf);
let area = area.inner(&Margin { let area = area.inner(Margin {
horizontal: 2, horizontal: 2,
vertical: 1, vertical: 1,
}); });

View file

@ -186,7 +186,7 @@ fn ui(f: &mut Frame, app: &mut App) {
.begin_symbol(None) .begin_symbol(None)
.track_symbol(None) .track_symbol(None)
.end_symbol(None), .end_symbol(None),
chunks[2].inner(&Margin { chunks[2].inner(Margin {
vertical: 1, vertical: 1,
horizontal: 0, horizontal: 0,
}), }),
@ -204,7 +204,7 @@ fn ui(f: &mut Frame, app: &mut App) {
Scrollbar::new(ScrollbarOrientation::HorizontalBottom) Scrollbar::new(ScrollbarOrientation::HorizontalBottom)
.thumb_symbol("🬋") .thumb_symbol("🬋")
.end_symbol(None), .end_symbol(None),
chunks[3].inner(&Margin { chunks[3].inner(Margin {
vertical: 0, vertical: 0,
horizontal: 1, horizontal: 1,
}), }),
@ -222,7 +222,7 @@ fn ui(f: &mut Frame, app: &mut App) {
Scrollbar::new(ScrollbarOrientation::HorizontalBottom) Scrollbar::new(ScrollbarOrientation::HorizontalBottom)
.thumb_symbol("") .thumb_symbol("")
.track_symbol(Some("")), .track_symbol(Some("")),
chunks[4].inner(&Margin { chunks[4].inner(Margin {
vertical: 0, vertical: 0,
horizontal: 1, horizontal: 1,
}), }),

View file

@ -318,7 +318,7 @@ fn render_scrollbar(f: &mut Frame, app: &mut App, area: Rect) {
.orientation(ScrollbarOrientation::VerticalRight) .orientation(ScrollbarOrientation::VerticalRight)
.begin_symbol(None) .begin_symbol(None)
.end_symbol(None), .end_symbol(None),
area.inner(&Margin { area.inner(Margin {
vertical: 1, vertical: 1,
horizontal: 1, horizontal: 1,
}), }),

View file

@ -608,7 +608,7 @@ impl Layout {
// This is equivalent to storing the solver in `Layout` and calling `solver.reset()` here. // This is equivalent to storing the solver in `Layout` and calling `solver.reset()` here.
let mut solver = Solver::new(); let mut solver = Solver::new();
let inner_area = area.inner(&self.margin); let inner_area = area.inner(self.margin);
let (area_start, area_end) = match self.direction { let (area_start, area_end) = match self.direction {
Direction::Horizontal => ( Direction::Horizontal => (
f64::from(inner_area.x) * FLOAT_PRECISION_MULTIPLIER, f64::from(inner_area.x) * FLOAT_PRECISION_MULTIPLIER,

View file

@ -119,9 +119,8 @@ impl Rect {
/// Returns a new `Rect` inside the current one, with the given margin on each side. /// Returns a new `Rect` inside the current one, with the given margin on each side.
/// ///
/// If the margin is larger than the `Rect`, the returned `Rect` will have no area. /// If the margin is larger than the `Rect`, the returned `Rect` will have no area.
#[allow(clippy::trivially_copy_pass_by_ref)] // See PR #1008
#[must_use = "method returns the modified value"] #[must_use = "method returns the modified value"]
pub const fn inner(self, margin: &Margin) -> Self { pub const fn inner(self, margin: Margin) -> Self {
let doubled_margin_horizontal = margin.horizontal.saturating_mul(2); let doubled_margin_horizontal = margin.horizontal.saturating_mul(2);
let doubled_margin_vertical = margin.vertical.saturating_mul(2); let doubled_margin_vertical = margin.vertical.saturating_mul(2);
@ -406,7 +405,7 @@ mod tests {
#[test] #[test]
fn inner() { fn inner() {
assert_eq!( assert_eq!(
Rect::new(1, 2, 3, 4).inner(&Margin::new(1, 2)), Rect::new(1, 2, 3, 4).inner(Margin::new(1, 2)),
Rect::new(2, 4, 1, 0) Rect::new(2, 4, 1, 0)
); );
} }

View file

@ -98,7 +98,7 @@ mod tests {
"██████████", "██████████",
]); ]);
expected.set_style(buffer.area, Style::new().red()); expected.set_style(buffer.area, Style::new().red());
expected.set_style(buffer.area.inner(&Margin::new(1, 1)), Style::reset()); expected.set_style(buffer.area.inner(Margin::new(1, 1)), Style::reset());
assert_eq!(buffer, expected); assert_eq!(buffer, expected);
} }
@ -132,8 +132,8 @@ mod tests {
"█▄▄▄▄▄▄▄▄█", "█▄▄▄▄▄▄▄▄█",
]); ]);
expected.set_style(buffer.area, Style::new().red().on_red()); expected.set_style(buffer.area, Style::new().red().on_red());
expected.set_style(buffer.area.inner(&Margin::new(1, 0)), Style::reset().red()); expected.set_style(buffer.area.inner(Margin::new(1, 0)), Style::reset().red());
expected.set_style(buffer.area.inner(&Margin::new(1, 1)), Style::reset()); expected.set_style(buffer.area.inner(Margin::new(1, 1)), Style::reset());
assert_eq!(buffer, expected); assert_eq!(buffer, expected);
} }
@ -176,8 +176,8 @@ mod tests {
"⣇⣀⣀⣀⣀⣀⣀⣀⣀⣸", "⣇⣀⣀⣀⣀⣀⣀⣀⣀⣸",
]); ]);
expected.set_style(buffer.area, Style::new().red()); expected.set_style(buffer.area, Style::new().red());
expected.set_style(buffer.area.inner(&Margin::new(1, 1)), Style::new().green()); expected.set_style(buffer.area.inner(Margin::new(1, 1)), Style::new().green());
expected.set_style(buffer.area.inner(&Margin::new(2, 2)), Style::reset()); expected.set_style(buffer.area.inner(Margin::new(2, 2)), Style::reset());
assert_eq!(buffer, expected); assert_eq!(buffer, expected);
} }
} }

View file

@ -63,7 +63,7 @@ use crate::{prelude::*, symbols::scrollbar::*};
/// // and the scrollbar, those are separate widgets /// // and the scrollbar, those are separate widgets
/// frame.render_stateful_widget( /// frame.render_stateful_widget(
/// scrollbar, /// scrollbar,
/// area.inner(&Margin { /// area.inner(Margin {
/// // using an inner vertical margin of 1 unit makes the scrollbar inside the block /// // using an inner vertical margin of 1 unit makes the scrollbar inside the block
/// vertical: 1, /// vertical: 1,
/// horizontal: 0, /// horizontal: 0,