rgpui 1.3.0

GUI UI framework
Documentation
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
//! SVG 路径渲染组件:解析 SVG path 数据(M/L/H/V/C/Q/Z 命令),通过 PathBuilder 绘制。

use crate::*;

/// SVG 路径命令。
#[derive(Clone)]
enum SvgCommand {
    /// M 移动到。
    MoveTo(f32, f32),
    /// L 直线。
    LineTo(f32, f32),
    /// C 三次贝塞尔曲线。
    CurveTo(f32, f32, f32, f32, f32, f32),
    /// Q 二次贝塞尔曲线。
    QuadTo(f32, f32, f32, f32),
    /// Z 闭合路径。
    Close,
}

/// 解析 SVG path 数据为命令序列。
fn parse_svg_path(data: &str) -> Vec<SvgCommand> {
    let mut commands = Vec::new();
    let mut chars = data.chars().peekable();
    let mut current_cmd = ' ';

    /// 跳过空白字符与逗号。
    fn skip_ws_and_commas(chars: &mut std::iter::Peekable<std::str::Chars>) {
        while let Some(&c) = chars.peek() {
            if c.is_whitespace() || c == ',' {
                chars.next();
            } else {
                break;
            }
        }
    }

    /// 解析一个浮点数。
    fn parse_number(chars: &mut std::iter::Peekable<std::str::Chars>) -> Option<f32> {
        skip_ws_and_commas(chars);
        let mut s = String::new();
        if let Some(&c) = chars.peek() {
            if c == '-' || c == '+' {
                s.push(c);
                chars.next();
            }
        }
        let mut has_dot = false;
        while let Some(&c) = chars.peek() {
            if c.is_ascii_digit() {
                s.push(c);
                chars.next();
            } else if c == '.' && !has_dot {
                has_dot = true;
                s.push(c);
                chars.next();
            } else {
                break;
            }
        }
        if s.is_empty() || s == "-" || s == "+" {
            None
        } else {
            s.parse().ok()
        }
    }

    while chars.peek().is_some() {
        skip_ws_and_commas(&mut chars);
        if let Some(&c) = chars.peek() {
            if c.is_ascii_alphabetic() {
                current_cmd = c;
                chars.next();
            }
        }

        match current_cmd {
            'M' => {
                if let (Some(x), Some(y)) = (parse_number(&mut chars), parse_number(&mut chars)) {
                    commands.push(SvgCommand::MoveTo(x, y));
                    current_cmd = 'L';
                } else {
                    break;
                }
            }
            'L' => {
                if let (Some(x), Some(y)) = (parse_number(&mut chars), parse_number(&mut chars)) {
                    commands.push(SvgCommand::LineTo(x, y));
                } else {
                    break;
                }
            }
            'H' => {
                if let Some(x) = parse_number(&mut chars) {
                    commands.push(SvgCommand::LineTo(x, f32::NAN));
                } else {
                    break;
                }
            }
            'V' => {
                if let Some(y) = parse_number(&mut chars) {
                    commands.push(SvgCommand::LineTo(f32::NAN, y));
                } else {
                    break;
                }
            }
            'C' => {
                if let (Some(x1), Some(y1), Some(x2), Some(y2), Some(x), Some(y)) = (
                    parse_number(&mut chars),
                    parse_number(&mut chars),
                    parse_number(&mut chars),
                    parse_number(&mut chars),
                    parse_number(&mut chars),
                    parse_number(&mut chars),
                ) {
                    commands.push(SvgCommand::CurveTo(x1, y1, x2, y2, x, y));
                } else {
                    break;
                }
            }
            'Q' => {
                if let (Some(cx1), Some(cy1), Some(x), Some(y)) = (
                    parse_number(&mut chars),
                    parse_number(&mut chars),
                    parse_number(&mut chars),
                    parse_number(&mut chars),
                ) {
                    commands.push(SvgCommand::QuadTo(cx1, cy1, x, y));
                } else {
                    break;
                }
            }
            'Z' | 'z' => {
                commands.push(SvgCommand::Close);
                current_cmd = 'M';
            }
            _ => {
                chars.next();
            }
        }
    }

    commands
}

/// SVG 绘制所需的数据。
#[derive(Clone)]
struct SvgPaintData {
    /// 命令序列。
    commands: Vec<SvgCommand>,
    /// 视口范围。
    view_box: Bounds<f32>,
    /// 填充颜色。
    fill_color: Hsla,
    /// 描边颜色。
    stroke_color: Option<Hsla>,
    /// 描边宽度。
    stroke_width: f32,
}

/// SVG 路径渲染组件:将 path 数据按视口等比缩放绘制。
#[derive(IntoElement)]
pub struct SVGRenderer {
    /// SVG path 数据字符串。
    path_data: SharedString,
    /// 视口范围。
    view_box: Bounds<f32>,
    /// 填充颜色(默认主题前景色)。
    fill_color: Option<Hsla>,
    /// 描边颜色。
    stroke_color: Option<Hsla>,
    /// 描边宽度。
    stroke_width: f32,
    /// 用户样式。
    style: StyleRefinement,
}

impl SVGRenderer {
    /// 创建 SVG 渲染组件,默认视口 100x100、无描边。
    pub fn new() -> Self {
        Self {
            path_data: SharedString::default(),
            view_box: Bounds::new(point(0.0_f32, 0.0_f32), size(100.0_f32, 100.0_f32)),
            fill_color: None,
            stroke_color: None,
            stroke_width: 1.0,
            style: StyleRefinement::default(),
        }
    }

    /// 设置 SVG path 数据。
    pub fn path_data(mut self, data: impl Into<SharedString>) -> Self {
        self.path_data = data.into();
        self
    }

    /// 设置视口范围(x, y, 宽, 高)。
    pub fn view_box(mut self, x: f32, y: f32, w: f32, h: f32) -> Self {
        self.view_box = Bounds::new(point(x, y), size(w, h));
        self
    }

    /// 设置填充颜色。
    pub fn fill(mut self, color: Hsla) -> Self {
        self.fill_color = Some(color);
        self
    }

    /// 设置描边颜色。
    pub fn stroke(mut self, color: Hsla) -> Self {
        self.stroke_color = Some(color);
        self
    }

    /// 设置描边宽度。
    pub fn stroke_width(mut self, width: f32) -> Self {
        self.stroke_width = width;
        self
    }
}

impl Styled for SVGRenderer {
    fn style(&mut self) -> &mut StyleRefinement {
        &mut self.style
    }
}

/// 将视口坐标映射到元素内的实际像素坐标(等比缩放并居中)。
fn transform_point(
    vx: f32,
    vy: f32,
    view_box: &Bounds<f32>,
    bounds: &Bounds<Pixels>,
) -> Point<Pixels> {
    let scale_x = bounds.size.width / px(1.0) / view_box.size.width;
    let scale_y = bounds.size.height / px(1.0) / view_box.size.height;
    let scale = scale_x.min(scale_y);

    let offset_x = (bounds.size.width / px(1.0) - view_box.size.width * scale) * 0.5;
    let offset_y = (bounds.size.height / px(1.0) - view_box.size.height * scale) * 0.5;

    point(
        bounds.left() + px(offset_x + (vx - view_box.origin.x) * scale),
        bounds.top() + px(offset_y + (vy - view_box.origin.y) * scale),
    )
}

impl RenderOnce for SVGRenderer {
    fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
        let theme = cx.theme();
        let user_style = self.style;

        let fill_color = self.fill_color.unwrap_or(*theme.tokens.foreground);
        let commands = parse_svg_path(&self.path_data);

        // 纯 DOM 模式下 canvas 隐藏,直接复用原始 path 数据生成等价 SVG。
        #[cfg(feature = "dom-backend")]
        let dom_data = (
            self.path_data.clone(),
            self.view_box,
            fill_color,
            self.stroke_color,
            self.stroke_width,
        );

        let paint_data = SvgPaintData {
            commands,
            view_box: self.view_box,
            fill_color,
            stroke_color: self.stroke_color,
            stroke_width: self.stroke_width,
        };

        let mut svg = canvas(
            move |_, _, _| paint_data,
            move |bounds, data, window, _cx| {
                if data.commands.is_empty() {
                    return;
                }

                let mut current_x = 0.0_f32;
                let mut current_y = 0.0_f32;

                if let Some(stroke_color) = data.stroke_color {
                    let mut builder = PathBuilder::stroke(px(data.stroke_width));
                    let mut started = false;

                    for cmd in &data.commands {
                        match cmd {
                            SvgCommand::MoveTo(x, y) => {
                                let pt = transform_point(*x, *y, &data.view_box, &bounds);
                                if started {
                                    if let Ok(path) = builder.build() {
                                        window.paint_path(path, stroke_color);
                                    }
                                    builder = PathBuilder::stroke(px(data.stroke_width));
                                }
                                builder.move_to(pt);
                                current_x = *x;
                                current_y = *y;
                                started = true;
                            }
                            SvgCommand::LineTo(x, y) => {
                                let fx = if x.is_nan() { current_x } else { *x };
                                let fy = if y.is_nan() { current_y } else { *y };
                                let pt = transform_point(fx, fy, &data.view_box, &bounds);
                                builder.line_to(pt);
                                current_x = fx;
                                current_y = fy;
                            }
                            SvgCommand::CurveTo(x1, y1, x2, y2, x, y) => {
                                let _cp1 = transform_point(*x1, *y1, &data.view_box, &bounds);
                                let cp2 = transform_point(*x2, *y2, &data.view_box, &bounds);
                                let end = transform_point(*x, *y, &data.view_box, &bounds);
                                builder.curve_to(cp2, end);
                                current_x = *x;
                                current_y = *y;
                            }
                            SvgCommand::QuadTo(cx1, cy1, x, y) => {
                                let cp = transform_point(*cx1, *cy1, &data.view_box, &bounds);
                                let end = transform_point(*x, *y, &data.view_box, &bounds);
                                builder.curve_to(cp, end);
                                current_x = *x;
                                current_y = *y;
                            }
                            SvgCommand::Close => {
                                builder.close();
                            }
                        }
                    }

                    if started {
                        if let Ok(path) = builder.build() {
                            window.paint_path(path, stroke_color);
                        }
                    }
                }

                {
                    let mut builder = PathBuilder::fill();
                    let mut started = false;
                    current_x = 0.0;
                    current_y = 0.0;

                    for cmd in &data.commands {
                        match cmd {
                            SvgCommand::MoveTo(x, y) => {
                                if started {
                                    builder.close();
                                    if let Ok(path) = builder.build() {
                                        window.paint_path(path, data.fill_color);
                                    }
                                    builder = PathBuilder::fill();
                                }
                                let pt = transform_point(*x, *y, &data.view_box, &bounds);
                                builder.move_to(pt);
                                current_x = *x;
                                current_y = *y;
                                started = true;
                            }
                            SvgCommand::LineTo(x, y) => {
                                let fx = if x.is_nan() { current_x } else { *x };
                                let fy = if y.is_nan() { current_y } else { *y };
                                let pt = transform_point(fx, fy, &data.view_box, &bounds);
                                builder.line_to(pt);
                                current_x = fx;
                                current_y = fy;
                            }
                            SvgCommand::CurveTo(x1, y1, x2, y2, x, y) => {
                                let _cp1 = transform_point(*x1, *y1, &data.view_box, &bounds);
                                let cp2 = transform_point(*x2, *y2, &data.view_box, &bounds);
                                let end = transform_point(*x, *y, &data.view_box, &bounds);
                                builder.curve_to(cp2, end);
                                current_x = *x;
                                current_y = *y;
                            }
                            SvgCommand::QuadTo(cx1, cy1, x, y) => {
                                let cp = transform_point(*cx1, *cy1, &data.view_box, &bounds);
                                let end = transform_point(*x, *y, &data.view_box, &bounds);
                                builder.curve_to(cp, end);
                                current_x = *x;
                                current_y = *y;
                            }
                            SvgCommand::Close => {
                                builder.close();
                            }
                        }
                    }

                    if started {
                        builder.close();
                        if let Ok(path) = builder.build() {
                            window.paint_path(path, data.fill_color);
                        }
                    }
                }
            },
        )
        .size_full();

        // 纯 DOM 模式下用 data URI 的 `<img>` 呈现 SVG 路径(viewBox 等比缩放)。
        #[cfg(feature = "dom-backend")]
        {
            let (dom_path, dom_view_box, dom_fill, dom_stroke, dom_stroke_width) = dom_data;
            svg = svg.with_dom(move |bounds, _window, _cx| {
                use crate::components::dom_svg::css_color;
                let mut markup = format!(
                    "<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"{} {} {} {}\">",
                    dom_view_box.origin.x,
                    dom_view_box.origin.y,
                    dom_view_box.size.width,
                    dom_view_box.size.height
                );
                markup.push_str(&format!("<path d=\"{}\"", dom_path));
                markup.push_str(&format!(" fill=\"{}\"", css_color(dom_fill)));
                if let Some(stroke) = dom_stroke {
                    markup.push_str(&format!(
                        " stroke=\"{}\" stroke-width=\"{}\"",
                        css_color(stroke),
                        dom_stroke_width
                    ));
                }
                markup.push_str("/></svg>");
                crate::components::dom_svg::svg_img_node(bounds, markup)
            });
        }

        let mut root = div().size_full().child(svg);
        root.style().refine(&user_style);
        root
    }
}