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:
Orhun Parmaksız 2024-12-03 12:04:53 +03:00 committed by GitHub
parent f2451e7f1e
commit ce4856a65f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 32 additions and 0 deletions

View File

@ -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 {

View File

@ -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 {

View File

@ -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] = [