Improve frame size

This commit is contained in:
Pierre HUBERT 2022-10-01 19:30:08 +02:00
parent 003296a782
commit 14c5820ed2
2 changed files with 22 additions and 3 deletions

View File

@ -1,7 +1,7 @@
use std::io; use std::io;
use std::time::{Duration, Instant}; 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;
use crossterm::event::{Event, KeyCode}; use crossterm::event::{Event, KeyCode};
use tui::backend::Backend; use tui::backend::Backend;
@ -80,7 +80,7 @@ pub fn select_play_mode<B: Backend>(
} }
fn ui<B: Backend>(f: &mut Frame<B>, model: &mut SelectPlayModeScreen) { fn ui<B: Backend>(f: &mut Frame<B>, 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 // Create a List from all list items and highlight the currently selected one
let items = AVAILABLE_PLAY_MODES let items = AVAILABLE_PLAY_MODES

View File

@ -1,7 +1,7 @@
use tui::layout::{Constraint, Direction, Layout, Rect}; use tui::layout::{Constraint, Direction, Layout, Rect};
/// helper function to create a centered rect using up certain percentage of the available rect `r` /// 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() let popup_layout = Layout::default()
.direction(Direction::Vertical) .direction(Direction::Vertical)
.constraints( .constraints(
@ -26,3 +26,22 @@ pub fn centered_rect(percent_x: u16, percent_y: u16, r: Rect) -> Rect {
) )
.split(popup_layout[1])[1] .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,
}
}