1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
use dioxus_native_core::real_dom::NodeImmutable;
use freya_dom::prelude::DioxusNode;
use freya_engine::prelude::*;
use freya_node_state::{BorderAlignment, BorderStyle, Fill, References, ShadowPosition, Style};
use torin::prelude::Area;

/// Render a `rect` element
pub fn render_rect(
    area: &Area,
    node_ref: &DioxusNode,
    canvas: &Canvas,
    font_collection: &FontCollection,
) {
    let node_style = &*node_ref.get::<Style>().unwrap();

    let mut paint = Paint::default();
    let mut path = Path::new();
    let area = area.to_f32();

    paint.set_anti_alias(true);
    paint.set_style(PaintStyle::Fill);

    let area = area.to_f32();

    match &node_style.background {
        Fill::Color(color) => {
            paint.set_color(*color);
        }
        Fill::LinearGradient(gradient) => {
            paint.set_shader(gradient.into_shader(area));
        }
    }

    let radius = node_style.corner_radius;
    let rounded_rect = RRect::new_rect_radii(
        Rect::new(area.min_x(), area.min_y(), area.max_x(), area.max_y()),
        &[
            (radius.top_left, radius.top_left).into(),
            (radius.top_right, radius.top_right).into(),
            (radius.bottom_right, radius.bottom_right).into(),
            (radius.bottom_left, radius.bottom_left).into(),
        ],
    );

    if node_style.corner_radius.smoothing > 0.0 {
        path.add_path(
            &node_style.corner_radius.smoothed_path(rounded_rect),
            (area.min_x(), area.min_y()),
            None,
        );
    } else {
        path.add_rrect(rounded_rect, None);
    }

    canvas.draw_path(&path, &paint);

    // Shadows
    for shadow in node_style.shadows.iter() {
        if shadow.fill != Fill::Color(Color::TRANSPARENT) {
            let mut shadow_paint = paint.clone();
            let mut shadow_path = Path::new();

            match &shadow.fill {
                Fill::Color(color) => {
                    shadow_paint.set_color(*color);
                }
                Fill::LinearGradient(gradient) => {
                    shadow_paint.set_shader(gradient.into_shader(area));
                }
            }

            // Shadows can be either outset or inset
            // If they are outset, we fill a copy of the path outset by spread_radius, and blur it.
            // Otherwise, we draw a stroke with the inner portion being spread_radius width, and the outer portion being blur_radius width.
            let outset: Point = match shadow.position {
                ShadowPosition::Normal => {
                    shadow_paint.set_style(PaintStyle::Fill);
                    (shadow.spread, shadow.spread).into()
                }
                ShadowPosition::Inset => {
                    shadow_paint.set_style(PaintStyle::Stroke);
                    shadow_paint.set_stroke_width(shadow.blur / 2.0 + shadow.spread);
                    (-shadow.spread / 2.0, -shadow.spread / 2.0).into()
                }
            };

            // Apply gassuan blur to the copied path.
            if shadow.blur > 0.0 {
                shadow_paint.set_mask_filter(MaskFilter::blur(
                    BlurStyle::Normal,
                    shadow.blur / 2.0,
                    false,
                ));
            }

            // Add either the RRect or smoothed path based on whether smoothing is used.
            if node_style.corner_radius.smoothing > 0.0 {
                shadow_path.add_path(
                    &node_style
                        .corner_radius
                        .smoothed_path(rounded_rect.with_outset(outset)),
                    Point::new(area.min_x(), area.min_y()) - outset,
                    None,
                );
            } else {
                shadow_path.add_rrect(rounded_rect.with_outset(outset), None);
            }

            // Offset our path by the shadow's x and y coordinates.
            shadow_path.offset((shadow.x, shadow.y));

            // Exclude the original path bounds from the shadow using a clip, then draw the shadow.
            canvas.save();
            canvas.clip_path(
                &path,
                match shadow.position {
                    ShadowPosition::Normal => ClipOp::Difference,
                    ShadowPosition::Inset => ClipOp::Intersect,
                },
                true,
            );
            canvas.draw_path(&shadow_path, &shadow_paint);
            canvas.restore();
        }
    }

    // Borders
    if node_style.border.width > 0.0 && node_style.border.style != BorderStyle::None {
        // Create a new paint and path
        let mut border_paint = paint.clone();
        let mut border_path = Path::new();

        // Setup paint params
        border_paint.set_anti_alias(true);
        border_paint.set_style(PaintStyle::Stroke);
        match &node_style.border.fill {
            Fill::Color(color) => {
                border_paint.set_color(*color);
            }
            Fill::LinearGradient(gradient) => {
                border_paint.set_shader(gradient.into_shader(area));
            }
        }
        border_paint.set_stroke_width(node_style.border.width);

        // Skia draws strokes centered on the edge of the path. This means that half of the stroke is inside the path, and half outside.
        // For Inner and Outer borders, we need to grow or shrink the stroke path by half the border width.
        let outset = Point::new(node_style.border.width / 2.0, node_style.border.width / 2.0)
            * match node_style.border.alignment {
                BorderAlignment::Center => 0.0,
                BorderAlignment::Inner => -1.0,
                BorderAlignment::Outer => 1.0,
            };

        // Add either the RRect or smoothed path based on whether smoothing is used.
        if node_style.corner_radius.smoothing > 0.0 {
            border_path.add_path(
                &node_style
                    .corner_radius
                    .smoothed_path(rounded_rect.with_outset(outset)),
                Point::new(area.min_x(), area.min_y()) - outset,
                None,
            );
        } else {
            border_path.add_rrect(rounded_rect.with_outset(outset), None);
        }

        canvas.draw_path(&border_path, &border_paint);
    }

    let references = node_ref.get::<References>().unwrap();

    if let Some(canvas_ref) = &references.canvas_ref {
        (canvas_ref.runner)(canvas, font_collection, area);
    }
}