Skip to content

Commit

Permalink
perf(rect)!: Rect::inner takes Margin directly instead of referen…
Browse files Browse the repository at this point in the history
…ce (#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,
 });
```
  • Loading branch information
EdJoPaTo committed May 26, 2024
1 parent 7a48c5b commit 8b447ec
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 40 deletions.
27 changes: 20 additions & 7 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ GitHub with a [breaking change] label.
This is a quick summary of the sections below:

- [Unreleased](#unreleased)
- `Rect::inner` takes `Margin` directly instead of reference
- `Stylize::bg()` now accepts `Into<Color>`
- [v0.27.0 (unreleased)](#v0270-unreleased)
- Removed deprecated `List::start_corner`
- [v0.26.0](#v0260)
- `Flex::Start` is the new default flex mode for `Layout`
Expand Down Expand Up @@ -53,17 +53,30 @@ This is a quick summary of the sections below:

## Unreleased

### `Stylize::bg()` now accepts `Into<Color>` ([#1103])
### `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,
});
```

[#1099]: https://github.com/ratatui-org/ratatui/pull/1103
### `Stylize::bg()` now accepts `Into<Color>` ([#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.
[#1103]: https://github.com/ratatui-org/ratatui/pull/1103

## v0.27.0 (unreleased)
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.

### Remove deprecated `List::start_corner` and `layout::Corner` ([#758])
### Remove deprecated `List::start_corner` and `layout::Corner` ([#757])

[#758]: https://github.com/ratatui-org/ratatui/pull/757
[#757]: https://github.com/ratatui-org/ratatui/pull/757

`List::start_corner` was deprecated in v0.25. Use `List::direction` and `ListDirection` instead.

Expand Down
22 changes: 9 additions & 13 deletions examples/demo2/tabs/about.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,16 @@ impl Widget for AboutTab {
}

fn render_crate_description(area: Rect, buf: &mut Buffer) {
let area = area.inner(
&(Margin {
vertical: 4,
horizontal: 2,
}),
);
let area = area.inner(Margin {
vertical: 4,
horizontal: 2,
});
Clear.render(area, buf); // clear out the color swatches
Block::new().style(THEME.content).render(area, buf);
let area = area.inner(
&(Margin {
vertical: 1,
horizontal: 2,
}),
);
let area = area.inner(Margin {
vertical: 1,
horizontal: 2,
});
let text = "- cooking up terminal user interfaces -
Ratatui is a Rust crate that provides widgets (e.g. Paragraph, Table) and draws them to the \
Expand Down Expand Up @@ -108,7 +104,7 @@ pub fn render_logo(selected_row: usize, area: Rect, buf: &mut Buffer) {
} else {
THEME.logo.rat_eye_alt
};
let area = area.inner(&Margin {
let area = area.inner(Margin {
vertical: 0,
horizontal: 2,
});
Expand Down
2 changes: 1 addition & 1 deletion examples/demo2/tabs/email.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl EmailTab {
impl Widget for EmailTab {
fn render(self, area: Rect, buf: &mut Buffer) {
RgbSwatch.render(area, buf);
let area = area.inner(&Margin {
let area = area.inner(Margin {
vertical: 1,
horizontal: 2,
});
Expand Down
4 changes: 2 additions & 2 deletions examples/demo2/tabs/recipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl RecipeTab {
impl Widget for RecipeTab {
fn render(self, area: Rect, buf: &mut Buffer) {
RgbSwatch.render(area, buf);
let area = area.inner(&Margin {
let area = area.inner(Margin {
vertical: 1,
horizontal: 2,
});
Expand All @@ -124,7 +124,7 @@ impl Widget for RecipeTab {
};
render_scrollbar(self.row_index, scrollbar_area, buf);

let area = area.inner(&Margin {
let area = area.inner(Margin {
horizontal: 2,
vertical: 1,
});
Expand Down
2 changes: 1 addition & 1 deletion examples/demo2/tabs/traceroute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl TracerouteTab {
impl Widget for TracerouteTab {
fn render(self, area: Rect, buf: &mut Buffer) {
RgbSwatch.render(area, buf);
let area = area.inner(&Margin {
let area = area.inner(Margin {
vertical: 1,
horizontal: 2,
});
Expand Down
4 changes: 2 additions & 2 deletions examples/demo2/tabs/weather.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ impl WeatherTab {
impl Widget for WeatherTab {
fn render(self, area: Rect, buf: &mut Buffer) {
RgbSwatch.render(area, buf);
let area = area.inner(&Margin {
let area = area.inner(Margin {
vertical: 1,
horizontal: 2,
});
Clear.render(area, buf);
Block::new().style(THEME.content).render(area, buf);

let area = area.inner(&Margin {
let area = area.inner(Margin {
horizontal: 2,
vertical: 1,
});
Expand Down
6 changes: 3 additions & 3 deletions examples/scrollbar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ fn ui(f: &mut Frame, app: &mut App) {
.begin_symbol(None)
.track_symbol(None)
.end_symbol(None),
chunks[2].inner(&Margin {
chunks[2].inner(Margin {
vertical: 1,
horizontal: 0,
}),
Expand All @@ -204,7 +204,7 @@ fn ui(f: &mut Frame, app: &mut App) {
Scrollbar::new(ScrollbarOrientation::HorizontalBottom)
.thumb_symbol("🬋")
.end_symbol(None),
chunks[3].inner(&Margin {
chunks[3].inner(Margin {
vertical: 0,
horizontal: 1,
}),
Expand All @@ -222,7 +222,7 @@ fn ui(f: &mut Frame, app: &mut App) {
Scrollbar::new(ScrollbarOrientation::HorizontalBottom)
.thumb_symbol("░")
.track_symbol(Some("─")),
chunks[4].inner(&Margin {
chunks[4].inner(Margin {
vertical: 0,
horizontal: 1,
}),
Expand Down
2 changes: 1 addition & 1 deletion examples/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ fn render_scrollbar(f: &mut Frame, app: &mut App, area: Rect) {
.orientation(ScrollbarOrientation::VerticalRight)
.begin_symbol(None)
.end_symbol(None),
area.inner(&Margin {
area.inner(Margin {
vertical: 1,
horizontal: 1,
}),
Expand Down
2 changes: 1 addition & 1 deletion src/layout/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ impl Layout {
// This is equivalent to storing the solver in `Layout` and calling `solver.reset()` here.
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 {
Direction::Horizontal => (
f64::from(inner_area.x) * FLOAT_PRECISION_MULTIPLIER,
Expand Down
5 changes: 2 additions & 3 deletions src/layout/rect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,8 @@ impl Rect {
/// 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.
#[allow(clippy::trivially_copy_pass_by_ref)] // See PR #1008
#[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_vertical = margin.vertical.saturating_mul(2);

Expand Down Expand Up @@ -406,7 +405,7 @@ mod tests {
#[test]
fn inner() {
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)
);
}
Expand Down
10 changes: 5 additions & 5 deletions src/widgets/canvas/rectangle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ mod tests {
"██████████",
]);
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);
}

Expand Down Expand Up @@ -132,8 +132,8 @@ mod tests {
"█▄▄▄▄▄▄▄▄█",
]);
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, 1)), Style::reset());
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());
assert_eq!(buffer, expected);
}

Expand Down Expand Up @@ -176,8 +176,8 @@ mod tests {
"⣇⣀⣀⣀⣀⣀⣀⣀⣀⣸",
]);
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(2, 2)), Style::reset());
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());
assert_eq!(buffer, expected);
}
}
2 changes: 1 addition & 1 deletion src/widgets/scrollbar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ use crate::{prelude::*, symbols::scrollbar::*};
/// // and the scrollbar, those are separate widgets
/// frame.render_stateful_widget(
/// scrollbar,
/// area.inner(&Margin {
/// area.inner(Margin {
/// // using an inner vertical margin of 1 unit makes the scrollbar inside the block
/// vertical: 1,
/// horizontal: 0,
Expand Down

0 comments on commit 8b447ec

Please sign in to comment.