Skip to content

Commit

Permalink
perf(rect)!: Rect::inner takes Margin directly instead of reference
Browse files Browse the repository at this point in the history
Also, adds `Rect::ZERO` and `Rect::inner` is now `const`.

BREAKING CHANGE: `Margin` needs to be passed without reference now.
  • Loading branch information
EdJoPaTo committed Apr 10, 2024
1 parent b7778e5 commit 52418f9
Show file tree
Hide file tree
Showing 12 changed files with 53 additions and 33 deletions.
16 changes: 16 additions & 0 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,22 @@ This is a quick summary of the sections below:
- MSRV is now 1.63.0
- `List` no longer ignores empty strings

## 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,
});
```

## [v0.26.0](https://github.com/ratatui-org/ratatui/releases/tag/v0.26.0)

### `Flex::Start` is the new default flex mode for `Layout` ([#881])
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
14 changes: 11 additions & 3 deletions src/layout/rect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ impl fmt::Display for Rect {
}

impl Rect {
/// A zero sized Rect at position 0,0
pub const ZERO: Self = Self {
x: 0,
y: 0,
width: 0,
height: 0,
};

/// Creates a new `Rect`, with width and height limited to keep the area under max `u16`. If
/// clipped, aspect ratio will be preserved.
pub fn new(x: u16, y: u16, width: u16, height: u16) -> Self {
Expand Down Expand Up @@ -112,12 +120,12 @@ impl Rect {
///
/// If the margin is larger than the `Rect`, the returned `Rect` will have no area.
#[must_use = "method returns the modified value"]
pub 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);

if self.width < doubled_margin_horizontal || self.height < doubled_margin_vertical {
Self::default()
Self::ZERO

Check warning on line 128 in src/layout/rect.rs

View check run for this annotation

Codecov / codecov/patch

src/layout/rect.rs#L128

Added line #L128 was not covered by tests
} else {
Self {
x: self.x.saturating_add(margin.horizontal),
Expand Down Expand Up @@ -385,7 +393,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_buffer_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_buffer_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_buffer_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 @@ -58,7 +58,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 52418f9

Please sign in to comment.