telar_renderer_core/style/
shape.rs1use crate::{BorderRadius, Color};
2
3use super::paint::{FillRule, Paint, Shadow, Stroke};
4
5pub trait ShapeStyle: Sized {
6 fn fill_mut(&mut self) -> &mut Option<Paint>;
7 fn stroke_mut(&mut self) -> &mut Option<Stroke>;
8 fn shadow_mut(&mut self) -> &mut Option<Shadow>;
9
10 fn with_fill(mut self, fill: impl Into<Paint>) -> Self {
11 *self.fill_mut() = Some(fill.into());
12 self
13 }
14 fn with_stroke(mut self, stroke: Stroke) -> Self {
15 *self.stroke_mut() = Some(stroke);
16 self
17 }
18 fn with_shadow(mut self, shadow: Shadow) -> Self {
19 *self.shadow_mut() = Some(shadow);
20 self
21 }
22}
23
24#[derive(Debug, Clone, Copy, PartialEq, Default)]
25pub struct RectStyle {
26 pub fill: Option<Paint>,
27 pub stroke: Option<Stroke>,
28 pub shadow: Option<Shadow>,
29 pub radius: BorderRadius,
30}
31
32impl RectStyle {
33 pub fn filled(color: Color, radius: f32) -> Self {
34 Self {
35 fill: Some(Paint::Solid(color)),
36 radius: BorderRadius::all(radius),
37 ..Self::default()
38 }
39 }
40
41 pub fn with_radius(mut self, radius: BorderRadius) -> Self {
42 self.radius = radius;
43 self
44 }
45}
46
47impl ShapeStyle for RectStyle {
48 fn fill_mut(&mut self) -> &mut Option<Paint> {
49 &mut self.fill
50 }
51 fn stroke_mut(&mut self) -> &mut Option<Stroke> {
52 &mut self.stroke
53 }
54 fn shadow_mut(&mut self) -> &mut Option<Shadow> {
55 &mut self.shadow
56 }
57}
58
59#[derive(Debug, Clone, Copy, PartialEq, Default)]
60pub struct PathStyle {
61 pub fill: Option<Paint>,
62 pub stroke: Option<Stroke>,
63 pub shadow: Option<Shadow>,
64 pub fill_rule: FillRule,
65}
66
67impl PathStyle {
68 pub fn with_fill_rule(mut self, rule: FillRule) -> Self {
69 self.fill_rule = rule;
70 self
71 }
72}
73
74impl ShapeStyle for PathStyle {
75 fn fill_mut(&mut self) -> &mut Option<Paint> {
76 &mut self.fill
77 }
78 fn stroke_mut(&mut self) -> &mut Option<Stroke> {
79 &mut self.stroke
80 }
81 fn shadow_mut(&mut self) -> &mut Option<Shadow> {
82 &mut self.shadow
83 }
84}