refactor(backend)!: return Size from Backend::size instead of Rect (#1254)

The `Backend::size` method returns a `Size` instead of a `Rect`.
There is no need for the position here as it was always 0,0.
This commit is contained in:
EdJoPaTo 2024-08-06 02:36:50 +02:00 committed by GitHub
parent ffeb8e46b9
commit bb68bc6968
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 56 additions and 40 deletions

View file

@ -63,6 +63,13 @@ This is a quick summary of the sections below:
## v0.28.0 (unreleased) ## v0.28.0 (unreleased)
### `Backend::size` returns `Size` instead of `Rect` ([#1254])
[#1254]: https://github.com/ratatui-org/ratatui/pull/1254
The `Backend::size` method returns a `Size` instead of a `Rect`.
There is no need for the position here as it was always 0,0.
### Ratatui now requires Crossterm 0.28.0 ([#1278]) ### Ratatui now requires Crossterm 0.28.0 ([#1278])
[#1278]: https://github.com/ratatui-org/ratatui/pull/1278 [#1278]: https://github.com/ratatui-org/ratatui/pull/1278

View file

@ -104,7 +104,7 @@ use std::io;
use strum::{Display, EnumString}; use strum::{Display, EnumString};
use crate::{buffer::Cell, layout::Size, prelude::Rect}; use crate::{buffer::Cell, layout::Size};
#[cfg(feature = "termion")] #[cfg(feature = "termion")]
mod termion; mod termion;
@ -275,19 +275,19 @@ pub trait Backend {
} }
} }
/// Get the size of the terminal screen in columns/rows as a [`Rect`]. /// Get the size of the terminal screen in columns/rows as a [`Size`].
/// ///
/// The returned [`Rect`] contains the width and height of the terminal screen. /// The returned [`Size`] contains the width and height of the terminal screen.
/// ///
/// # Example /// # Example
/// ///
/// ```rust,no_run /// ```rust
/// # use ratatui::{prelude::*, backend::TestBackend}; /// # use ratatui::{prelude::*, backend::TestBackend};
/// let backend = TestBackend::new(80, 25); /// let backend = TestBackend::new(80, 25);
/// assert_eq!(backend.size()?, Rect::new(0, 0, 80, 25)); /// assert_eq!(backend.size()?, Size::new(80, 25));
/// # std::io::Result::Ok(()) /// # std::io::Result::Ok(())
/// ``` /// ```
fn size(&self) -> io::Result<Rect>; fn size(&self) -> io::Result<Size>;
/// Get the size of the terminal screen in columns/rows and pixels as a [`WindowSize`]. /// Get the size of the terminal screen in columns/rows and pixels as a [`WindowSize`].
/// ///

View file

@ -19,7 +19,7 @@ use crate::{
}, },
terminal::{self, Clear}, terminal::{self, Clear},
}, },
layout::{Position, Rect, Size}, layout::{Position, Size},
style::{Color, Modifier, Style}, style::{Color, Modifier, Style},
}; };
@ -245,9 +245,9 @@ where
self.writer.flush() self.writer.flush()
} }
fn size(&self) -> io::Result<Rect> { fn size(&self) -> io::Result<Size> {
let (width, height) = terminal::size()?; let (width, height) = terminal::size()?;
Ok(Rect::new(0, 0, width, height)) Ok(Size { width, height })
} }
fn window_size(&mut self) -> io::Result<WindowSize> { fn window_size(&mut self) -> io::Result<WindowSize> {

View file

@ -12,7 +12,7 @@ use std::{
use crate::{ use crate::{
backend::{Backend, ClearType, WindowSize}, backend::{Backend, ClearType, WindowSize},
buffer::Cell, buffer::Cell,
layout::{Position, Rect}, layout::{Position, Size},
style::{Color, Modifier, Style}, style::{Color, Modifier, Style},
termion::{self, color as tcolor, color::Color as _, style as tstyle}, termion::{self, color as tcolor, color::Color as _, style as tstyle},
}; };
@ -214,9 +214,9 @@ where
) )
} }
fn size(&self) -> io::Result<Rect> { fn size(&self) -> io::Result<Size> {
let terminal = termion::terminal_size()?; let terminal = termion::terminal_size()?;
Ok(Rect::new(0, 0, terminal.0, terminal.1)) Ok(Size::new(terminal.0, terminal.1))
} }
fn window_size(&mut self) -> io::Result<WindowSize> { fn window_size(&mut self) -> io::Result<WindowSize> {

View file

@ -11,7 +11,6 @@ use crate::{
backend::{Backend, WindowSize}, backend::{Backend, WindowSize},
buffer::Cell, buffer::Cell,
layout::Size, layout::Size,
prelude::Rect,
style::{Color, Modifier, Style}, style::{Color, Modifier, Style},
termwiz::{ termwiz::{
caps::Capabilities, caps::Capabilities,
@ -212,9 +211,9 @@ impl Backend for TermwizBackend {
Ok(()) Ok(())
} }
fn size(&self) -> io::Result<Rect> { fn size(&self) -> io::Result<Size> {
let (cols, rows) = self.buffered_terminal.dimensions(); let (cols, rows) = self.buffered_terminal.dimensions();
Ok(Rect::new(0, 0, u16_max(cols), u16_max(rows))) Ok(Size::new(u16_max(cols), u16_max(rows)))
} }
fn window_size(&mut self) -> io::Result<WindowSize> { fn window_size(&mut self) -> io::Result<WindowSize> {

View file

@ -230,13 +230,13 @@ impl Backend for TestBackend {
Ok(()) Ok(())
} }
fn size(&self) -> io::Result<Rect> { fn size(&self) -> io::Result<Size> {
Ok(self.buffer.area) Ok(self.buffer.area.as_size())
} }
fn window_size(&mut self) -> io::Result<WindowSize> { fn window_size(&mut self) -> io::Result<WindowSize> {
// Some arbitrary window pixel size, probably doesn't need much testing. // Some arbitrary window pixel size, probably doesn't need much testing.
static WINDOW_PIXEL_SIZE: Size = Size { const WINDOW_PIXEL_SIZE: Size = Size {
width: 640, width: 640,
height: 480, height: 480,
}; };
@ -647,7 +647,7 @@ mod tests {
#[test] #[test]
fn size() { fn size() {
let backend = TestBackend::new(10, 2); let backend = TestBackend::new(10, 2);
assert_eq!(backend.size().unwrap(), Rect::new(0, 0, 10, 2)); assert_eq!(backend.size().unwrap(), Size::new(10, 2));
} }
#[test] #[test]

View file

@ -66,8 +66,8 @@ where
viewport: Viewport, viewport: Viewport,
/// Area of the viewport /// Area of the viewport
viewport_area: Rect, viewport_area: Rect,
/// Last known size of the terminal. Used to detect if the internal buffers have to be resized. /// Last known area of the terminal. Used to detect if the internal buffers have to be resized.
last_known_size: Rect, last_known_area: Rect,
/// Last known position of the cursor. Used to find the new area when the viewport is inlined /// Last known position of the cursor. Used to find the new area when the viewport is inlined
/// and the terminal resized. /// and the terminal resized.
last_known_cursor_pos: Position, last_known_cursor_pos: Position,
@ -133,13 +133,17 @@ where
/// # std::io::Result::Ok(()) /// # std::io::Result::Ok(())
/// ``` /// ```
pub fn with_options(mut backend: B, options: TerminalOptions) -> io::Result<Self> { pub fn with_options(mut backend: B, options: TerminalOptions) -> io::Result<Self> {
let size = match options.viewport { let area = match options.viewport {
Viewport::Fullscreen | Viewport::Inline(_) => backend.size()?, Viewport::Fullscreen | Viewport::Inline(_) => {
Rect::from((Position::ORIGIN, backend.size()?))
}
Viewport::Fixed(area) => area, Viewport::Fixed(area) => area,
}; };
let (viewport_area, cursor_pos) = match options.viewport { let (viewport_area, cursor_pos) = match options.viewport {
Viewport::Fullscreen => (size, Position::ORIGIN), Viewport::Fullscreen => (area, Position::ORIGIN),
Viewport::Inline(height) => compute_inline_size(&mut backend, height, size, 0)?, Viewport::Inline(height) => {
compute_inline_size(&mut backend, height, area.as_size(), 0)?
}
Viewport::Fixed(area) => (area, area.as_position()), Viewport::Fixed(area) => (area, area.as_position()),
}; };
Ok(Self { Ok(Self {
@ -149,7 +153,7 @@ where
hidden_cursor: false, hidden_cursor: false,
viewport: options.viewport, viewport: options.viewport,
viewport_area, viewport_area,
last_known_size: size, last_known_area: area,
last_known_cursor_pos: cursor_pos, last_known_cursor_pos: cursor_pos,
frame_count: 0, frame_count: 0,
}) })
@ -193,26 +197,32 @@ where
self.backend.draw(updates.into_iter()) self.backend.draw(updates.into_iter())
} }
/// Updates the Terminal so that internal buffers match the requested size. /// Updates the Terminal so that internal buffers match the requested area.
/// ///
/// Requested size will be saved so the size can remain consistent when rendering. This leads /// Requested area will be saved to remain consistent when rendering. This leads to a full clear
/// to a full clear of the screen. /// of the screen.
pub fn resize(&mut self, size: Rect) -> io::Result<()> { pub fn resize(&mut self, area: Rect) -> io::Result<()> {
let next_area = match self.viewport { let next_area = match self.viewport {
Viewport::Fullscreen => size, Viewport::Fullscreen => area,
Viewport::Inline(height) => { Viewport::Inline(height) => {
let offset_in_previous_viewport = self let offset_in_previous_viewport = self
.last_known_cursor_pos .last_known_cursor_pos
.y .y
.saturating_sub(self.viewport_area.top()); .saturating_sub(self.viewport_area.top());
compute_inline_size(&mut self.backend, height, size, offset_in_previous_viewport)?.0 compute_inline_size(
&mut self.backend,
height,
area.as_size(),
offset_in_previous_viewport,
)?
.0
} }
Viewport::Fixed(area) => area, Viewport::Fixed(area) => area,
}; };
self.set_viewport_area(next_area); self.set_viewport_area(next_area);
self.clear()?; self.clear()?;
self.last_known_size = size; self.last_known_area = area;
Ok(()) Ok(())
} }
@ -226,9 +236,9 @@ where
pub fn autoresize(&mut self) -> io::Result<()> { pub fn autoresize(&mut self) -> io::Result<()> {
// fixed viewports do not get autoresized // fixed viewports do not get autoresized
if matches!(self.viewport, Viewport::Fullscreen | Viewport::Inline(_)) { if matches!(self.viewport, Viewport::Fullscreen | Viewport::Inline(_)) {
let size = self.size()?; let area = Rect::from((Position::ORIGIN, self.size()?));
if size != self.last_known_size { if area != self.last_known_area {
self.resize(size)?; self.resize(area)?;
} }
}; };
Ok(()) Ok(())
@ -396,7 +406,7 @@ where
let completed_frame = CompletedFrame { let completed_frame = CompletedFrame {
buffer: &self.buffers[1 - self.current], buffer: &self.buffers[1 - self.current],
area: self.last_known_size, area: self.last_known_area,
count: self.frame_count, count: self.frame_count,
}; };
@ -463,7 +473,7 @@ where
} }
/// Queries the real size of the backend. /// Queries the real size of the backend.
pub fn size(&self) -> io::Result<Rect> { pub fn size(&self) -> io::Result<Size> {
self.backend.size() self.backend.size()
} }
@ -523,7 +533,7 @@ where
self.clear()?; self.clear()?;
// Move the viewport by height, but don't move it past the bottom of the terminal // Move the viewport by height, but don't move it past the bottom of the terminal
let viewport_at_bottom = self.last_known_size.bottom() - self.viewport_area.height; let viewport_at_bottom = self.last_known_area.bottom() - self.viewport_area.height;
self.set_viewport_area(Rect { self.set_viewport_area(Rect {
y: self y: self
.viewport_area .viewport_area
@ -571,7 +581,7 @@ where
fn compute_inline_size<B: Backend>( fn compute_inline_size<B: Backend>(
backend: &mut B, backend: &mut B,
height: u16, height: u16,
size: Rect, size: Size,
offset_in_previous_viewport: u16, offset_in_previous_viewport: u16,
) -> io::Result<(Rect, Position)> { ) -> io::Result<(Rect, Position)> {
let pos: Position = backend.get_cursor()?.into(); let pos: Position = backend.get_cursor()?.into();