Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(frame): introduce .area() as its more correct than .size() #1293

Merged
merged 2 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ impl App {

fn draw(&self, terminal: &mut Terminal) -> Result<()> {
terminal.draw(|frame| {
let area = frame.size();
let area = frame.area();
frame.render_widget(
Line::from("ratatui async example").centered().cyan().bold(),
area,
Expand Down
2 changes: 1 addition & 1 deletion examples/barchart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ fn run_app<B: Backend>(
fn ui(frame: &mut Frame, app: &App) {
let vertical = Layout::vertical([Constraint::Ratio(1, 3), Constraint::Ratio(2, 3)]);
let horizontal = Layout::horizontal([Constraint::Percentage(50), Constraint::Percentage(50)]);
let [top, bottom] = vertical.areas(frame.size());
let [top, bottom] = vertical.areas(frame.area());
let [left, right] = horizontal.areas(bottom);

let barchart = BarChart::default()
Expand Down
2 changes: 1 addition & 1 deletion examples/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ fn handle_events() -> Result<ControlFlow<()>> {
}

fn ui(frame: &mut Frame) {
let (title_area, layout) = calculate_layout(frame.size());
let (title_area, layout) = calculate_layout(frame.area());

render_title(frame, title_area);

Expand Down
2 changes: 1 addition & 1 deletion examples/calendar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ fn main() -> Result<(), Box<dyn Error>> {
}

fn draw(frame: &mut Frame) {
let app_area = frame.size();
let app_area = frame.area();

let calarea = Rect {
x: app_area.x + 1,
Expand Down
2 changes: 1 addition & 1 deletion examples/canvas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ impl App {
let horizontal =
Layout::horizontal([Constraint::Percentage(50), Constraint::Percentage(50)]);
let vertical = Layout::vertical([Constraint::Percentage(50), Constraint::Percentage(50)]);
let [map, right] = horizontal.areas(frame.size());
let [map, right] = horizontal.areas(frame.area());
let [pong, boxes] = vertical.areas(right);

frame.render_widget(self.map_canvas(), map);
Expand Down
4 changes: 1 addition & 3 deletions examples/chart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,7 @@ fn run_app<B: Backend>(
}

fn ui(frame: &mut Frame, app: &App) {
let area = frame.size();

let [top, bottom] = Layout::vertical([Constraint::Fill(1); 2]).areas(area);
let [top, bottom] = Layout::vertical([Constraint::Fill(1); 2]).areas(frame.area());
let [animated_chart, bar_chart] =
Layout::horizontal([Constraint::Fill(1), Constraint::Length(29)]).areas(top);
let [line_chart, scatter] = Layout::horizontal([Constraint::Fill(1); 2]).areas(bottom);
Expand Down
2 changes: 1 addition & 1 deletion examples/colors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ fn ui(frame: &mut Frame) {
Constraint::Length(17),
Constraint::Length(2),
])
.split(frame.size());
.split(frame.area());

render_named_colors(frame, layout[0]);
render_indexed_colors(frame, layout[1]);
Expand Down
2 changes: 1 addition & 1 deletion examples/colors_rgb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ impl App {
/// This is the main event loop for the app.
pub fn run(mut self, mut terminal: Terminal<impl Backend>) -> Result<()> {
while self.is_running() {
terminal.draw(|frame| frame.render_widget(&mut self, frame.size()))?;
terminal.draw(|frame| frame.render_widget(&mut self, frame.area()))?;
self.handle_events()?;
}
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion examples/constraint-explorer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ impl App {
}

fn draw(&self, terminal: &mut Terminal<impl Backend>) -> io::Result<()> {
terminal.draw(|frame| frame.render_widget(self, frame.size()))?;
terminal.draw(|frame| frame.render_widget(self, frame.area()))?;
Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion examples/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ impl App {
}

fn draw(self, terminal: &mut Terminal<impl Backend>) -> io::Result<()> {
terminal.draw(|frame| frame.render_widget(self, frame.size()))?;
terminal.draw(|frame| frame.render_widget(self, frame.area()))?;
Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion examples/custom_widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ fn ui(frame: &mut Frame, states: [State; 3]) {
Constraint::Length(1),
Constraint::Min(0), // ignore remaining space
]);
let [title, buttons, help, _] = vertical.areas(frame.size());
let [title, buttons, help, _] = vertical.areas(frame.area());

frame.render_widget(
Paragraph::new("Custom Widget Example (mouse enabled)"),
Expand Down
2 changes: 1 addition & 1 deletion examples/demo/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use ratatui::{
use crate::app::App;

pub fn draw(f: &mut Frame, app: &mut App) {
let chunks = Layout::vertical([Constraint::Length(3), Constraint::Min(0)]).split(f.size());
let chunks = Layout::vertical([Constraint::Length(3), Constraint::Min(0)]).split(f.area());
let tabs = app
.tabs
.titles
Expand Down
2 changes: 1 addition & 1 deletion examples/demo2/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl App {
fn draw(&self, terminal: &mut Terminal<impl Backend>) -> Result<()> {
terminal
.draw(|frame| {
frame.render_widget(self, frame.size());
frame.render_widget(self, frame.area());
if self.mode == Mode::Destroy {
destroy::destroy(frame);
}
Expand Down
2 changes: 1 addition & 1 deletion examples/demo2/destroy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub fn destroy(frame: &mut Frame<'_>) {
return;
}

let area = frame.size();
let area = frame.area();
let buf = frame.buffer_mut();

drip(frame_count, area, buf);
Expand Down
6 changes: 3 additions & 3 deletions examples/docsrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ fn main() -> io::Result<()> {
fn hello_world(frame: &mut Frame) {
frame.render_widget(
Paragraph::new("Hello World!").block(Block::bordered().title("Greeting")),
frame.size(),
frame.area(),
);
}

Expand All @@ -79,7 +79,7 @@ fn layout(frame: &mut Frame) {
Constraint::Length(1),
]);
let horizontal = Layout::horizontal([Constraint::Ratio(1, 2); 2]);
let [title_bar, main_area, status_bar] = vertical.areas(frame.size());
let [title_bar, main_area, status_bar] = vertical.areas(frame.area());
let [left, right] = horizontal.areas(main_area);

frame.render_widget(
Expand All @@ -102,7 +102,7 @@ fn styling(frame: &mut Frame) {
Constraint::Length(1),
Constraint::Min(0),
])
.split(frame.size());
.split(frame.area());

let span1 = Span::raw("Hello ");
let span2 = Span::styled(
Expand Down
2 changes: 1 addition & 1 deletion examples/flex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ impl App {
}

fn draw(self, terminal: &mut Terminal<impl Backend>) -> io::Result<()> {
terminal.draw(|frame| frame.render_widget(self, frame.size()))?;
terminal.draw(|frame| frame.render_widget(self, frame.area()))?;
Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion examples/gauge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl App {
}

fn draw(&self, terminal: &mut Terminal<impl Backend>) -> Result<()> {
terminal.draw(|f| f.render_widget(self, f.size()))?;
terminal.draw(|f| f.render_widget(self, f.area()))?;
Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion examples/hello_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ fn run(terminal: &mut Terminal<CrosstermBackend<Stdout>>) -> Result<()> {
/// draws a greeting.
fn render_app(frame: &mut Frame) {
let greeting = Paragraph::new("Hello World! (press 'q' to quit)");
frame.render_widget(greeting, frame.size());
frame.render_widget(greeting, frame.area());
}

/// Check if the user has pressed 'q'. This is where you would handle events. This example just
Expand Down
2 changes: 1 addition & 1 deletion examples/hyperlink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl App {

fn run(self, terminal: &mut Terminal<CrosstermBackend<Stdout>>) -> io::Result<()> {
loop {
terminal.draw(|frame| frame.render_widget(&self.hyperlink, frame.size()))?;
terminal.draw(|frame| frame.render_widget(&self.hyperlink, frame.area()))?;
if let Event::Key(key) = event::read()? {
if matches!(key.code, KeyCode::Char('q') | KeyCode::Esc) {
break;
Expand Down
2 changes: 1 addition & 1 deletion examples/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ fn run_app<B: Backend>(
}

fn ui(f: &mut Frame, downloads: &Downloads) {
let area = f.size();
let area = f.area();

let block = Block::new().title(block::Title::from("Progress").alignment(Alignment::Center));
f.render_widget(block, area);
Expand Down
2 changes: 1 addition & 1 deletion examples/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ fn ui(frame: &mut Frame) {
Length(50), // examples
Min(0), // fills remaining space
]);
let [text_area, examples_area, _] = vertical.areas(frame.size());
let [text_area, examples_area, _] = vertical.areas(frame.area());

// title
frame.render_widget(
Expand Down
2 changes: 1 addition & 1 deletion examples/line_gauge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ impl App {
}

fn draw(&self, terminal: &mut Terminal<impl Backend>) -> Result<()> {
terminal.draw(|f| f.render_widget(self, f.size()))?;
terminal.draw(|f| f.render_widget(self, f.area()))?;
Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion examples/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ impl TodoItem {
impl App {
fn run(&mut self, mut terminal: Terminal<impl Backend>) -> io::Result<()> {
while !self.should_exit {
terminal.draw(|f| f.render_widget(&mut *self, f.size()))?;
terminal.draw(|f| f.render_widget(&mut *self, f.area()))?;
if let Event::Key(key) = event::read()? {
self.handle_key(key);
};
Expand Down
2 changes: 1 addition & 1 deletion examples/minimal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
enable_raw_mode()?;
execute!(terminal.backend_mut(), EnterAlternateScreen)?;
loop {
terminal.draw(|frame| frame.render_widget(Text::raw("Hello World!"), frame.size()))?;
terminal.draw(|frame| frame.render_widget(Text::raw("Hello World!"), frame.area()))?;
if let Event::Key(key) = event::read()? {
if key.kind == KeyEventKind::Press && key.code == KeyCode::Char('q') {
break;
Expand Down
2 changes: 1 addition & 1 deletion examples/modifiers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ fn run_app<B: Backend>(terminal: &mut Terminal<B>) -> io::Result<()> {

fn ui(frame: &mut Frame) {
let vertical = Layout::vertical([Constraint::Length(1), Constraint::Min(0)]);
let [text_area, main_area] = vertical.areas(frame.size());
let [text_area, main_area] = vertical.areas(frame.area());
frame.render_widget(
Paragraph::new("Note: not all terminals support all modifiers")
.style(Style::default().fg(Color::Red).add_modifier(Modifier::BOLD)),
Expand Down
2 changes: 1 addition & 1 deletion examples/panic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,5 +148,5 @@ fn ui(f: &mut Frame, app: &App) {
.block(Block::bordered().title("Panic Handler Demo"))
.centered();

f.render_widget(paragraph, f.size());
f.render_widget(paragraph, f.area());
}
2 changes: 1 addition & 1 deletion examples/paragraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl App {

/// Draw the app to the terminal.
fn draw(&mut self, terminal: &mut Tui) -> io::Result<()> {
terminal.draw(|frame| frame.render_widget(self, frame.size()))?;
terminal.draw(|frame| frame.render_widget(self, frame.area()))?;
Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion examples/popup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> io::Result<(
}

fn ui(f: &mut Frame, app: &App) {
let area = f.size();
let area = f.area();

let vertical = Layout::vertical([Constraint::Percentage(20), Constraint::Percentage(80)]);
let [instructions, content] = vertical.areas(area);
Expand Down
4 changes: 1 addition & 3 deletions examples/ratatui-logo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,7 @@ fn logo() -> String {

fn main() -> io::Result<()> {
let mut terminal = init()?;
terminal.draw(|frame| {
frame.render_widget(Paragraph::new(logo()), frame.size());
})?;
terminal.draw(|frame| frame.render_widget(Paragraph::new(logo()), frame.area()))?;
sleep(Duration::from_secs(5));
restore()?;
println!();
Expand Down
6 changes: 3 additions & 3 deletions examples/scrollbar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,11 @@ fn run_app<B: Backend>(

#[allow(clippy::too_many_lines, clippy::cast_possible_truncation)]
fn ui(f: &mut Frame, app: &mut App) {
let size = f.size();
let area = f.area();

// Words made "loooong" to demonstrate line breaking.
let s = "Veeeeeeeeeeeeeeeery loooooooooooooooooong striiiiiiiiiiiiiiiiiiiiiiiiiing. ";
let mut long_line = s.repeat(usize::from(size.width) / s.len() + 4);
let mut long_line = s.repeat(usize::from(area.width) / s.len() + 4);
long_line.push('\n');

let chunks = Layout::vertical([
Expand All @@ -133,7 +133,7 @@ fn ui(f: &mut Frame, app: &mut App) {
Constraint::Percentage(25),
Constraint::Percentage(25),
])
.split(size);
.split(area);

let text = vec![
Line::from("This is a line "),
Expand Down
2 changes: 1 addition & 1 deletion examples/sparkline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ fn ui(f: &mut Frame, app: &App) {
Constraint::Length(3),
Constraint::Min(0),
])
.split(f.size());
.split(f.area());
let sparkline = Sparkline::default()
.block(
Block::new()
Expand Down
2 changes: 1 addition & 1 deletion examples/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> io::Result<(
}

fn ui(f: &mut Frame, app: &mut App) {
let rects = Layout::vertical([Constraint::Min(5), Constraint::Length(3)]).split(f.size());
let rects = Layout::vertical([Constraint::Min(5), Constraint::Length(3)]).split(f.area());

app.set_colors();

Expand Down
2 changes: 1 addition & 1 deletion examples/tabs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl App {
}

fn draw(&self, terminal: &mut Terminal<impl Backend>) -> Result<()> {
terminal.draw(|frame| frame.render_widget(self, frame.size()))?;
terminal.draw(|frame| frame.render_widget(self, frame.area()))?;
Ok(())
}

Expand Down
3 changes: 1 addition & 2 deletions examples/tracing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,10 @@ fn handle_events(events: &mut Vec<Event>) -> Result<()> {
fn ui(frame: &mut ratatui::Frame, events: &[Event]) {
// To view this event, run the example with `RUST_LOG=tracing=debug cargo run --example tracing`
trace!(frame_count = frame.count(), event_count = events.len());
let area = frame.size();
let events = events.iter().map(|e| format!("{e:?}")).collect::<Vec<_>>();
let paragraph = Paragraph::new(events.join("\n"))
.block(Block::bordered().title("Tracing example. Press 'q' to quit."));
frame.render_widget(paragraph, area);
frame.render_widget(paragraph, frame.area());
}

/// Initialize the tracing subscriber to log to a file
Expand Down
2 changes: 1 addition & 1 deletion examples/user_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ fn ui(f: &mut Frame, app: &App) {
Constraint::Length(3),
Constraint::Min(1),
]);
let [help_area, input_area, messages_area] = vertical.areas(f.size());
let [help_area, input_area, messages_area] = vertical.areas(f.area());

let (msg, style) = match app.input_mode {
InputMode::Normal => (
Expand Down
16 changes: 14 additions & 2 deletions src/terminal/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,25 @@ pub struct CompletedFrame<'a> {
}

impl Frame<'_> {
/// The size of the current frame
/// The area of the current frame
///
/// This is guaranteed not to change during rendering, so may be called multiple times.
///
/// If your app listens for a resize event from the backend, it should ignore the values from
/// the event for any calculations that are used to render the current frame and use this value
/// instead as this is the size of the buffer that is used to render the current frame.
/// instead as this is the area of the buffer that is used to render the current frame.
pub const fn area(&self) -> Rect {
self.viewport_area
}

/// The area of the current frame
///
/// This is guaranteed not to change during rendering, so may be called multiple times.
///
/// If your app listens for a resize event from the backend, it should ignore the values from
/// the event for any calculations that are used to render the current frame and use this value
/// instead as this is the area of the buffer that is used to render the current frame.
#[deprecated = "use .area() as its the more correct name"]
pub const fn size(&self) -> Rect {
self.viewport_area
}
Expand Down
Loading