From 14c5820ed209696b295e52141d2bd3813400baf8 Mon Sep 17 00:00:00 2001 From: Pierre Hubert Date: Sat, 1 Oct 2022 19:30:08 +0200 Subject: [PATCH] Improve frame size --- .../src/ui_screens/select_play_mode.rs | 4 ++-- rust/cli_player/src/ui_screens/utils.rs | 21 ++++++++++++++++++- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/rust/cli_player/src/ui_screens/select_play_mode.rs b/rust/cli_player/src/ui_screens/select_play_mode.rs index 5459927..adda484 100644 --- a/rust/cli_player/src/ui_screens/select_play_mode.rs +++ b/rust/cli_player/src/ui_screens/select_play_mode.rs @@ -1,7 +1,7 @@ use std::io; use std::time::{Duration, Instant}; -use crate::ui_screens::utils::centered_rect; +use crate::ui_screens::utils::centered_rect_size; use crossterm::event; use crossterm::event::{Event, KeyCode}; use tui::backend::Backend; @@ -80,7 +80,7 @@ pub fn select_play_mode( } fn ui(f: &mut Frame, model: &mut SelectPlayModeScreen) { - let area = centered_rect(60, 20, f.size()); + let area = centered_rect_size(50, 5, f.size()); // Create a List from all list items and highlight the currently selected one let items = AVAILABLE_PLAY_MODES diff --git a/rust/cli_player/src/ui_screens/utils.rs b/rust/cli_player/src/ui_screens/utils.rs index be95528..5eef9b8 100644 --- a/rust/cli_player/src/ui_screens/utils.rs +++ b/rust/cli_player/src/ui_screens/utils.rs @@ -1,7 +1,7 @@ use tui::layout::{Constraint, Direction, Layout, Rect}; /// helper function to create a centered rect using up certain percentage of the available rect `r` -pub fn centered_rect(percent_x: u16, percent_y: u16, r: Rect) -> Rect { +pub fn centered_rect_percentage(percent_x: u16, percent_y: u16, r: Rect) -> Rect { let popup_layout = Layout::default() .direction(Direction::Vertical) .constraints( @@ -26,3 +26,22 @@ pub fn centered_rect(percent_x: u16, percent_y: u16, r: Rect) -> Rect { ) .split(popup_layout[1])[1] } + +/// helper function to create a centered rect using up certain container size +pub fn centered_rect_size(width: u16, height: u16, parent: Rect) -> Rect { + if parent.width < width || parent.height < height { + return Rect { + x: 0, + y: 0, + width: parent.width, + height: parent.height, + }; + } + + Rect { + x: (parent.width - width) / 2, + y: (parent.height - height) / 2, + width, + height, + } +}