diff --git a/README.md b/README.md index c4f656e8..5bc310e4 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ Over the last 2 years my go to GUI tool for this was [fork](https://git-fork.com * [x] support staging * [x] show added files on working dir changes * [x] support committing -* [ ] popup centered +* [x] popup centered * [ ] crash: adding a pic to index crashes * [ ] crash: renamed files cannot be added to index * [ ] allow selecting/diff index items diff --git a/assets/main.jpg b/assets/main.jpg index 87eb96ae..03201a86 100644 Binary files a/assets/main.jpg and b/assets/main.jpg differ diff --git a/src/app.rs b/src/app.rs index 9a741dd0..ce202c3c 100644 --- a/src/app.rs +++ b/src/app.rs @@ -186,7 +186,7 @@ impl App { .block(Block::default().title("Commit").borders(Borders::ALL)) .alignment(Alignment::Left), ) - .render(f, Rect::new(20, 0, 100, 10)); + .render(f, git_utils::centered_rect(60, 20, f.size())); } } diff --git a/src/git_utils.rs b/src/git_utils.rs index c33e92fc..4e013356 100644 --- a/src/git_utils.rs +++ b/src/git_utils.rs @@ -1,5 +1,6 @@ use git2::{DiffFormat, DiffOptions, Repository}; use std::path::Path; +use tui::layout::{Constraint, Direction, Layout, Rect}; /// #[derive(Copy, Clone, PartialEq)] @@ -103,3 +104,31 @@ pub fn commit(msg: &String) { ) .unwrap(); } + +/// use layouts to create a rects that +/// centers inside `r` and sizes `percent_x`/`percent_x` of `r` +pub fn centered_rect(percent_x: u16, percent_y: u16, r: Rect) -> Rect { + let popup_layout = Layout::default() + .direction(Direction::Vertical) + .constraints( + [ + Constraint::Percentage((100 - percent_y) / 2), + Constraint::Percentage(percent_y), + Constraint::Percentage((100 - percent_y) / 2), + ] + .as_ref(), + ) + .split(r); + + Layout::default() + .direction(Direction::Horizontal) + .constraints( + [ + Constraint::Percentage((100 - percent_x) / 2), + Constraint::Percentage(percent_x), + Constraint::Percentage((100 - percent_x) / 2), + ] + .as_ref(), + ) + .split(popup_layout[1])[1] +}