From bb68bc6968219dacea8f3c272bb99d142d2f3253 Mon Sep 17 00:00:00 2001 From: EdJoPaTo Date: Tue, 6 Aug 2024 02:36:50 +0200 Subject: [PATCH] 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. --- BREAKING-CHANGES.md | 7 ++++++ src/backend.rs | 12 +++++----- src/backend/crossterm.rs | 6 ++--- src/backend/termion.rs | 6 ++--- src/backend/termwiz.rs | 5 ++-- src/backend/test.rs | 8 +++---- src/terminal/terminal.rs | 52 ++++++++++++++++++++++++---------------- 7 files changed, 56 insertions(+), 40 deletions(-) diff --git a/BREAKING-CHANGES.md b/BREAKING-CHANGES.md index 59cd624a..7762a7d8 100644 --- a/BREAKING-CHANGES.md +++ b/BREAKING-CHANGES.md @@ -63,6 +63,13 @@ This is a quick summary of the sections below: ## 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]) [#1278]: https://github.com/ratatui-org/ratatui/pull/1278 diff --git a/src/backend.rs b/src/backend.rs index 60841352..b6342705 100644 --- a/src/backend.rs +++ b/src/backend.rs @@ -104,7 +104,7 @@ use std::io; use strum::{Display, EnumString}; -use crate::{buffer::Cell, layout::Size, prelude::Rect}; +use crate::{buffer::Cell, layout::Size}; #[cfg(feature = "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 /// - /// ```rust,no_run + /// ```rust /// # use ratatui::{prelude::*, backend::TestBackend}; /// 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(()) /// ``` - fn size(&self) -> io::Result; + fn size(&self) -> io::Result; /// Get the size of the terminal screen in columns/rows and pixels as a [`WindowSize`]. /// diff --git a/src/backend/crossterm.rs b/src/backend/crossterm.rs index 19e28975..b27c8728 100644 --- a/src/backend/crossterm.rs +++ b/src/backend/crossterm.rs @@ -19,7 +19,7 @@ use crate::{ }, terminal::{self, Clear}, }, - layout::{Position, Rect, Size}, + layout::{Position, Size}, style::{Color, Modifier, Style}, }; @@ -245,9 +245,9 @@ where self.writer.flush() } - fn size(&self) -> io::Result { + fn size(&self) -> io::Result { let (width, height) = terminal::size()?; - Ok(Rect::new(0, 0, width, height)) + Ok(Size { width, height }) } fn window_size(&mut self) -> io::Result { diff --git a/src/backend/termion.rs b/src/backend/termion.rs index 836da2d4..d1e55da6 100644 --- a/src/backend/termion.rs +++ b/src/backend/termion.rs @@ -12,7 +12,7 @@ use std::{ use crate::{ backend::{Backend, ClearType, WindowSize}, buffer::Cell, - layout::{Position, Rect}, + layout::{Position, Size}, style::{Color, Modifier, Style}, termion::{self, color as tcolor, color::Color as _, style as tstyle}, }; @@ -214,9 +214,9 @@ where ) } - fn size(&self) -> io::Result { + fn size(&self) -> io::Result { 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 { diff --git a/src/backend/termwiz.rs b/src/backend/termwiz.rs index 1e01cf16..c237c473 100644 --- a/src/backend/termwiz.rs +++ b/src/backend/termwiz.rs @@ -11,7 +11,6 @@ use crate::{ backend::{Backend, WindowSize}, buffer::Cell, layout::Size, - prelude::Rect, style::{Color, Modifier, Style}, termwiz::{ caps::Capabilities, @@ -212,9 +211,9 @@ impl Backend for TermwizBackend { Ok(()) } - fn size(&self) -> io::Result { + fn size(&self) -> io::Result { 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 { diff --git a/src/backend/test.rs b/src/backend/test.rs index 78846c89..33416b7b 100644 --- a/src/backend/test.rs +++ b/src/backend/test.rs @@ -230,13 +230,13 @@ impl Backend for TestBackend { Ok(()) } - fn size(&self) -> io::Result { - Ok(self.buffer.area) + fn size(&self) -> io::Result { + Ok(self.buffer.area.as_size()) } fn window_size(&mut self) -> io::Result { // 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, height: 480, }; @@ -647,7 +647,7 @@ mod tests { #[test] fn size() { 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] diff --git a/src/terminal/terminal.rs b/src/terminal/terminal.rs index b33cfd5a..dc25627f 100644 --- a/src/terminal/terminal.rs +++ b/src/terminal/terminal.rs @@ -66,8 +66,8 @@ where viewport: Viewport, /// Area of the viewport viewport_area: Rect, - /// Last known size of the terminal. Used to detect if the internal buffers have to be resized. - last_known_size: Rect, + /// Last known area of the terminal. Used to detect if the internal buffers have to be resized. + last_known_area: Rect, /// Last known position of the cursor. Used to find the new area when the viewport is inlined /// and the terminal resized. last_known_cursor_pos: Position, @@ -133,13 +133,17 @@ where /// # std::io::Result::Ok(()) /// ``` pub fn with_options(mut backend: B, options: TerminalOptions) -> io::Result { - let size = match options.viewport { - Viewport::Fullscreen | Viewport::Inline(_) => backend.size()?, + let area = match options.viewport { + Viewport::Fullscreen | Viewport::Inline(_) => { + Rect::from((Position::ORIGIN, backend.size()?)) + } Viewport::Fixed(area) => area, }; let (viewport_area, cursor_pos) = match options.viewport { - Viewport::Fullscreen => (size, Position::ORIGIN), - Viewport::Inline(height) => compute_inline_size(&mut backend, height, size, 0)?, + Viewport::Fullscreen => (area, Position::ORIGIN), + Viewport::Inline(height) => { + compute_inline_size(&mut backend, height, area.as_size(), 0)? + } Viewport::Fixed(area) => (area, area.as_position()), }; Ok(Self { @@ -149,7 +153,7 @@ where hidden_cursor: false, viewport: options.viewport, viewport_area, - last_known_size: size, + last_known_area: area, last_known_cursor_pos: cursor_pos, frame_count: 0, }) @@ -193,26 +197,32 @@ where 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 - /// to a full clear of the screen. - pub fn resize(&mut self, size: Rect) -> io::Result<()> { + /// Requested area will be saved to remain consistent when rendering. This leads to a full clear + /// of the screen. + pub fn resize(&mut self, area: Rect) -> io::Result<()> { let next_area = match self.viewport { - Viewport::Fullscreen => size, + Viewport::Fullscreen => area, Viewport::Inline(height) => { let offset_in_previous_viewport = self .last_known_cursor_pos .y .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, }; self.set_viewport_area(next_area); self.clear()?; - self.last_known_size = size; + self.last_known_area = area; Ok(()) } @@ -226,9 +236,9 @@ where pub fn autoresize(&mut self) -> io::Result<()> { // fixed viewports do not get autoresized if matches!(self.viewport, Viewport::Fullscreen | Viewport::Inline(_)) { - let size = self.size()?; - if size != self.last_known_size { - self.resize(size)?; + let area = Rect::from((Position::ORIGIN, self.size()?)); + if area != self.last_known_area { + self.resize(area)?; } }; Ok(()) @@ -396,7 +406,7 @@ where let completed_frame = CompletedFrame { buffer: &self.buffers[1 - self.current], - area: self.last_known_size, + area: self.last_known_area, count: self.frame_count, }; @@ -463,7 +473,7 @@ where } /// Queries the real size of the backend. - pub fn size(&self) -> io::Result { + pub fn size(&self) -> io::Result { self.backend.size() } @@ -523,7 +533,7 @@ where self.clear()?; // 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 { y: self .viewport_area @@ -571,7 +581,7 @@ where fn compute_inline_size( backend: &mut B, height: u16, - size: Rect, + size: Size, offset_in_previous_viewport: u16, ) -> io::Result<(Rect, Position)> { let pos: Position = backend.get_cursor()?.into();