Skip to content

Commit 8ed153a

Browse files
committed
style: Dynamically calculate dialog center y-offset based on terminal height
1 parent dce97cc commit 8ed153a

1 file changed

Lines changed: 12 additions & 2 deletions

File tree

src/widgets.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,18 @@ impl ListChoice {
5858

5959
pub fn render(&mut self, app: &mut App, frame: &mut Frame) {
6060
let area = frame.area();
61-
let dialog_area = center_widget(area.width / 3, area.height / 4, area);
62-
61+
62+
// Calculate height dynamically: choices count + 2 (borders)
63+
let dialog_height = (self.choices.len() as u16 + 2).min(area.height);
64+
65+
// Calculate width dynamically: longest choice + padding
66+
let max_choice_width = self.choices.iter().map(|s| s.len()).max().unwrap_or(0) as u16;
67+
let dialog_width = (max_choice_width + 12).max(area.width / 3).min(area.width);
68+
69+
// Calculate centered position, then shift it upwards dynamically based on height (roughly 2 lines for standard heights)
70+
let mut dialog_area = center_widget(dialog_width, dialog_height, area);
71+
dialog_area.y = dialog_area.y.saturating_sub((area.height / 12).max(1));
72+
6373
let block = Block::new()
6474
.title(Line::raw(self.title.clone()).centered())
6575
.borders(Borders::ALL)

0 commit comments

Comments
 (0)