diff --git a/ratatui-core/src/layout/rect.rs b/ratatui-core/src/layout/rect.rs index 69f05ea5..9dbbeefe 100644 --- a/ratatui-core/src/layout/rect.rs +++ b/ratatui-core/src/layout/rect.rs @@ -646,6 +646,18 @@ impl From<(Position, Size)> for Rect { } } +impl From for Rect { + /// Creates a new `Rect` with the given size at [`Position::ORIGIN`] (0, 0). + fn from(size: Size) -> Self { + Self { + x: 0, + y: 0, + width: size.width, + height: size.height, + } + } +} + #[cfg(test)] mod tests { use alloc::string::ToString; @@ -956,6 +968,23 @@ mod tests { ); } + #[test] + fn from_size() { + let size = Size { + width: 3, + height: 4, + }; + assert_eq!( + Rect::from(size), + Rect { + x: 0, + y: 0, + width: 3, + height: 4 + } + ); + } + #[test] fn centered_horizontally() { let rect = Rect::new(0, 0, 5, 5); diff --git a/ratatui-core/src/terminal/terminal.rs b/ratatui-core/src/terminal/terminal.rs index 8b91aa43..3c8e130d 100644 --- a/ratatui-core/src/terminal/terminal.rs +++ b/ratatui-core/src/terminal/terminal.rs @@ -164,9 +164,7 @@ where /// ``` pub fn with_options(mut backend: B, options: TerminalOptions) -> Result { let area = match options.viewport { - Viewport::Fullscreen | Viewport::Inline(_) => { - Rect::from((Position::ORIGIN, backend.size()?)) - } + Viewport::Fullscreen | Viewport::Inline(_) => backend.size()?.into(), Viewport::Fixed(area) => area, }; let (viewport_area, cursor_pos) = match options.viewport { @@ -265,7 +263,7 @@ where pub fn autoresize(&mut self) -> Result<(), B::Error> { // fixed viewports do not get autoresized if matches!(self.viewport, Viewport::Fullscreen | Viewport::Inline(_)) { - let area = Rect::from((Position::ORIGIN, self.size()?)); + let area = self.size()?.into(); if area != self.last_known_area { self.resize(area)?; }