Skip to content

Commit 2e20c9a

Browse files
authored
Merge pull request #79 from rgwood/reilly.wood/use-rect-centered
Use ratatui 0.30's Rect::centered() for popup positioning
2 parents cb1a608 + 83d4972 commit 2e20c9a

File tree

1 file changed

+4
-36
lines changed

1 file changed

+4
-36
lines changed

src/components/home.rs

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1197,7 +1197,7 @@ impl Component for Home {
11971197
}
11981198

11991199
if self.mode == Mode::Help {
1200-
let popup = centered_rect_abs(50, 18, f.area());
1200+
let popup = f.area().centered(Constraint::Length(50), Constraint::Length(18));
12011201

12021202
let primary = |s| Span::styled(s, Style::default().fg(theme.primary));
12031203
let help_lines = vec![
@@ -1232,7 +1232,7 @@ impl Component for Home {
12321232
}
12331233

12341234
if self.mode == Mode::Error {
1235-
let popup = centered_rect_abs(50, 12, f.area());
1235+
let popup = f.area().centered(Constraint::Length(50), Constraint::Length(12));
12361236
let error_lines = self.error_message.split('\n').map(Line::from).collect_vec();
12371237
let paragraph = Paragraph::new(error_lines)
12381238
.block(
@@ -1288,7 +1288,7 @@ impl Component for Home {
12881288
let title_prefix = if self.mode == Mode::ActionMenu { "Actions" } else { "Signals" };
12891289
let title = format!("{} for {}", title_prefix, selected_item.unit.name);
12901290
let height = self.menu_items.items.len() as u16 + 2;
1291-
let popup = centered_rect_abs(popup_width, height, f.area());
1291+
let popup = f.area().centered(Constraint::Length(popup_width), Constraint::Length(height));
12921292

12931293
let items: Vec<ListItem> = self
12941294
.menu_items
@@ -1316,7 +1316,7 @@ impl Component for Home {
13161316

13171317
if self.mode == Mode::Processing {
13181318
let height = self.menu_items.items.len() as u16 + 2;
1319-
let popup = centered_rect_abs(popup_width, height, f.area());
1319+
let popup = f.area().centered(Constraint::Length(popup_width), Constraint::Length(height));
13201320

13211321
static SPINNER_CHARS: &[char] = &['⣷', '⣯', '⣟', '⡿', '⢿', '⣻', '⣽', '⣾'];
13221322

@@ -1339,38 +1339,6 @@ impl Component for Home {
13391339
}
13401340
}
13411341

1342-
/// helper function to create a centered rect using up certain percentage of the available rect `r`
1343-
fn _centered_rect(percent_x: u16, percent_y: u16, r: Rect) -> Rect {
1344-
let popup_layout = Layout::new(
1345-
Direction::Vertical,
1346-
[
1347-
Constraint::Percentage((100 - percent_y) / 2),
1348-
Constraint::Percentage(percent_y),
1349-
Constraint::Percentage((100 - percent_y) / 2),
1350-
],
1351-
)
1352-
.split(r);
1353-
1354-
Layout::new(
1355-
Direction::Horizontal,
1356-
[
1357-
Constraint::Percentage((100 - percent_x) / 2),
1358-
Constraint::Percentage(percent_x),
1359-
Constraint::Percentage((100 - percent_x) / 2),
1360-
],
1361-
)
1362-
.split(popup_layout[1])[1]
1363-
}
1364-
1365-
fn centered_rect_abs(width: u16, height: u16, r: Rect) -> Rect {
1366-
let offset_x = (r.width.saturating_sub(width)) / 2;
1367-
let offset_y = (r.height.saturating_sub(height)) / 2;
1368-
let width = width.min(r.width);
1369-
let height = height.min(r.height);
1370-
1371-
Rect::new(offset_x, offset_y, width, height)
1372-
}
1373-
13741342
/// Parse a journalctl timestamp and return a formatted date string.
13751343
///
13761344
/// systemd v255 changed the timestamp format from `-0700` to `-07:00` (RFC 3339).

0 commit comments

Comments
 (0)