mirror of
https://github.com/ratatui/ratatui.git
synced 2025-09-26 20:40:44 +00:00
feat: add conversion from Size to Rect (#2028)
`Rect::from(size)` returns a new Rect at the origin (0, 0) with the specified `Size`
This commit is contained in:
parent
0afb1a99af
commit
c845fec765
@ -646,6 +646,18 @@ impl From<(Position, Size)> for Rect {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Size> 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);
|
||||
|
@ -164,9 +164,7 @@ where
|
||||
/// ```
|
||||
pub fn with_options(mut backend: B, options: TerminalOptions) -> Result<Self, B::Error> {
|
||||
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)?;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user