mirror of
https://github.com/ratatui/ratatui.git
synced 2025-09-28 05:21:23 +00:00
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.
This commit is contained in:
parent
f2451e7f1e
commit
ce4856a65f
@ -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 {
|
||||
|
@ -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 {
|
||||
|
@ -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] = [
|
||||
|
Loading…
x
Reference in New Issue
Block a user