centered_rect

Function centered_rect 

Source
pub fn centered_rect(
    r: Rect,
    width: u16,
    height: u16,
    horizontal_offset: i16,
    vertical_offset: i16,
) -> Rect
Expand description

Create a centered Rect to place the dialog in.

frame.area() will typically be used for the r (Rect) parameter.

To offset horizontally or vertically, pass in negative values to go left or up, and positive values to go right or down. Use 0 for these parameters for no offset.

Based on https://ratatui.rs/how-to/layout/center-a-rect/.

Examples found in repository?
examples/basic.rs (line 136)
108fn render(frame: &mut Frame, app: &mut App) {
109    // Layout the rectangles of the UI.
110    let horizontal = Layout::horizontal([Constraint::Fill(1), Constraint::Length(SIDEBAR_SIZE)]);
111
112    let [left, right] = horizontal.areas(frame.area());
113
114    // Left - main content.
115    let main_block = Block::default()
116        .title_top(Line::from(" tui-dialog example ").bold().centered())
117        .borders(Borders::ALL)
118        .border_set(border::THICK)
119        .padding(Padding::horizontal(1));
120
121    // Right - Controls menu
122    let controls_block = Block::default()
123        .title_top(Line::from(" Controls").centered().bold())
124        .borders(Borders::ALL)
125        .border_set(border::THICK);
126
127    let controls_content: Vec<Line<'_>> = vec![
128        vec!["d".blue(), " Show dialog".into()].into(),
129        vec!["q".blue(), " Quit".into()].into(),
130    ];
131
132    let controls = Paragraph::new(controls_content).block(controls_block);
133    frame.render_widget(controls, right);
134    frame.render_widget(Paragraph::new(app.text.clone()).block(main_block), left);
135
136    let dialog_area = centered_rect(frame.area(), 45, 5, -(SIDEBAR_SIZE as i16), 0);
137    frame.render_widget(app.dialog.title_top("Enter text:"), dialog_area);
138}