How to: Collapse borders in a layout
A common layout for applications is to split up the screen into panes, with borders around each pane. Often this leads to making UIs that look disconnected. E.g., the following layout:
Created by the following code:
fn ui(frame: &mut Frame) {
// create a layout that splits the screen into 2 equal columns and the right column
// into 2 equal rows
let layout = Layout::default()
.direction(Direction::Horizontal)
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)])
.split(frame.size());
let sub_layout = Layout::default()
.direction(Direction::Vertical)
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)])
.split(layout[1]);
frame.render_widget(
Block::new().borders(Borders::ALL).title("Left Block"),
layout[0],
);
frame.render_widget(
Block::new().borders(Borders::ALL).title("Top Right Block"),
sub_layout[0],
);
frame.render_widget(
Block::new()
.borders(Borders::ALL)
.title("Bottom Right Block"),
sub_layout[1],
);
}
We can do better though, by collapsing borders. E.g.:
The first thing we need to do is work out which borders to collapse. Because in the layout above we want to connect the bottom right block to the middle vertical border, we’re going to need this to be rendered by the top left and bottom left blocks rather than the right block.
We need to use the symbols module to achieve this so we add this to the imports:
use ratatui::{prelude::*, symbols, widgets::*};
Our first change is to the left block where we remove the right border:
frame.render_widget(
Block::new()
// don't render the right border because it will be rendered by the right block
.border_set(symbols::border::PLAIN)
.borders(Borders::TOP | Borders::LEFT | Borders::BOTTOM)
.title("Left Block"),
layout[0],
);
Next, we see that the top left corner of the top right block joins with the top right corner of the
left block, so we need to replace that with a T shape. We also see omit the bottom border as that
will be rendered by the bottom right block. We use a custom symbols::border::Set
to achieve
this.
// top right block must render the top left border to join with the left block
let top_right_border_set = symbols::border::Set {
top_left: symbols::line::NORMAL.horizontal_down,
..symbols::border::PLAIN
};
frame.render_widget(
Block::new()
.border_set(top_right_border_set)
// don't render the bottom border because it will be rendered by the bottom block
.borders(Borders::TOP | Borders::LEFT | Borders::RIGHT)
.title("Top Right Block"),
sub_layout[0],
);
In the bottom right block, we see that the top right corner joins the left block’s right border and so we need to rend this with a horizontal T shape pointing to the right. We need to do the same for the top right corner and the bottom left corner.
// bottom right block must render:
// - top left border to join with the left block and top right block
// - top right border to join with the top right block
// - bottom left border to join with the left block
let collapsed_top_and_left_border_set = symbols::border::Set {
top_left: symbols::line::NORMAL.vertical_right,
top_right: symbols::line::NORMAL.vertical_left,
bottom_left: symbols::line::NORMAL.horizontal_up,
..symbols::border::PLAIN
};
frame.render_widget(
Block::new()
.border_set(collapsed_top_and_left_border_set)
.borders(Borders::ALL)
.title("Bottom Right Block"),
sub_layout[1],
);
If we left it here, then we’d be mostly fine, but in small areas we’d notice that the 50/50 split no longer looks right. This is due to the fact that by default we round up when splitting an odd number of rows or columns in 2 (e.g. 5 rows => 2.5/2.5 => 3/2). This is fine normally, but when we collapse borders between blocks, the first block has one extra row (or columns) already as it does not have the collapsed block. We can easily work around this issue by allocating a small amount of extra space to the last layout item (e.g. by using 49/51 or 33/33/34).
let layout = Layout::default()
.direction(Direction::Horizontal)
// use a 49/51 split instead of 50/50 to ensure that any extra space is on the right
// side of the screen. This is important because the right side of the screen is
// where the borders are collapsed.
.constraints([Constraint::Percentage(49), Constraint::Percentage(51)])
.split(frame.size());
let sub_layout = Layout::default()
.direction(Direction::Vertical)
// use a 49/51 split to ensure that any extra space is on the bottom
.constraints([Constraint::Percentage(49), Constraint::Percentage(51)])
.split(layout[1]);
If this sounds too complex, we’re looking for some help to make this easier in https://github.com/ratatui-org/ratatui/issues/605.
The full code for this example is available at https://github.com/ratatui-org/ratatui-book/code/how-to-collapse-borders
Full ui() function
fn ui(frame: &mut Frame) {
// create a layout that splits the screen into 2 equal columns and the right column
// into 2 equal rows
let layout = Layout::default()
.direction(Direction::Horizontal)
// use a 49/51 split instead of 50/50 to ensure that any extra space is on the right
// side of the screen. This is important because the right side of the screen is
// where the borders are collapsed.
.constraints([Constraint::Percentage(49), Constraint::Percentage(51)])
.split(frame.size());
let sub_layout = Layout::default()
.direction(Direction::Vertical)
// use a 49/51 split to ensure that any extra space is on the bottom
.constraints([Constraint::Percentage(49), Constraint::Percentage(51)])
.split(layout[1]);
frame.render_widget(
Block::new()
// don't render the right border because it will be rendered by the right block
.border_set(symbols::border::PLAIN)
.borders(Borders::TOP | Borders::LEFT | Borders::BOTTOM)
.title("Left Block"),
layout[0],
);
// top right block must render the top left border to join with the left block
let top_right_border_set = symbols::border::Set {
top_left: symbols::line::NORMAL.horizontal_down,
..symbols::border::PLAIN
};
frame.render_widget(
Block::new()
.border_set(top_right_border_set)
// don't render the bottom border because it will be rendered by the bottom block
.borders(Borders::TOP | Borders::LEFT | Borders::RIGHT)
.title("Top Right Block"),
sub_layout[0],
);
// bottom right block must render:
// - top left border to join with the left block and top right block
// - top right border to join with the top right block
// - bottom left border to join with the left block
let collapsed_top_and_left_border_set = symbols::border::Set {
top_left: symbols::line::NORMAL.vertical_right,
top_right: symbols::line::NORMAL.vertical_left,
bottom_left: symbols::line::NORMAL.horizontal_up,
..symbols::border::PLAIN
};
frame.render_widget(
Block::new()
.border_set(collapsed_top_and_left_border_set)
.borders(Borders::ALL)
.title("Bottom Right Block"),
sub_layout[1],
);
}