From ce4856a65f3c76db714a45338ba3be9b638c6c35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Orhun=20Parmaks=C4=B1z?= Date: Tue, 3 Dec 2024 12:04:53 +0300 Subject: [PATCH] feat(widgets): add the missing constructor to canvas types (#1538) Allows constructing `Rectangle`, `Points` and `Circle` using the `new` method instead of initializing with the public fields directly. --- ratatui-widgets/src/canvas/circle.rs | 12 ++++++++++++ ratatui-widgets/src/canvas/points.rs | 7 +++++++ ratatui-widgets/src/canvas/rectangle.rs | 13 +++++++++++++ 3 files changed, 32 insertions(+) diff --git a/ratatui-widgets/src/canvas/circle.rs b/ratatui-widgets/src/canvas/circle.rs index 76766196..8c62c411 100644 --- a/ratatui-widgets/src/canvas/circle.rs +++ b/ratatui-widgets/src/canvas/circle.rs @@ -15,6 +15,18 @@ pub struct Circle { pub color: Color, } +impl Circle { + /// Create a new circle with the given center, radius, and color + pub const fn new(x: f64, y: f64, radius: f64, color: Color) -> Self { + Self { + x, + y, + radius, + color, + } + } +} + impl Shape for Circle { fn draw(&self, painter: &mut Painter<'_, '_>) { for angle in 0..360 { diff --git a/ratatui-widgets/src/canvas/points.rs b/ratatui-widgets/src/canvas/points.rs index 8c3051cc..0a85fa4a 100644 --- a/ratatui-widgets/src/canvas/points.rs +++ b/ratatui-widgets/src/canvas/points.rs @@ -11,6 +11,13 @@ pub struct Points<'a> { pub color: Color, } +impl<'a> Points<'a> { + /// Create a new Points shape with the given coordinates and color + pub const fn new(coords: &'a [(f64, f64)], color: Color) -> Self { + Self { coords, color } + } +} + impl Shape for Points<'_> { fn draw(&self, painter: &mut Painter) { for (x, y) in self.coords { diff --git a/ratatui-widgets/src/canvas/rectangle.rs b/ratatui-widgets/src/canvas/rectangle.rs index 9ee35c87..4fb9af30 100644 --- a/ratatui-widgets/src/canvas/rectangle.rs +++ b/ratatui-widgets/src/canvas/rectangle.rs @@ -24,6 +24,19 @@ pub struct Rectangle { pub color: Color, } +impl Rectangle { + /// Create a new rectangle with the given position, size, and color + pub const fn new(x: f64, y: f64, width: f64, height: f64, color: Color) -> Self { + Self { + x, + y, + width, + height, + color, + } + } +} + impl Shape for Rectangle { fn draw(&self, painter: &mut Painter) { let lines: [Line; 4] = [