rust_widgets 2.9.2

Pure Rust cross-platform native GUI library with hardware-adaptive rendering, 180 widgets, touch/gesture support, i18n, and SVG-pipeline-accurate output
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
// SPDX-FileCopyrightText: Copyright (c) 2026 Mike Li/Mikewolfli/Wei Li(mikewolfli@163.com)
// SPDX-License-Identifier: MIT

//! Tests for the freeform-shape geometry and the control that draws it.

// The module-level gate, so tooling that scans file text recognises this as a test
// module. `mod.rs` declares `mod tests;` under `#[cfg(test)]`, which the compiler
// sees but a text scan of this file does not.
#![cfg(test)]

use super::shape::FreeformShapeWidget;
use super::types::{BubbleTailDirection, PathSegment, ShapePath, UnitPathSegment};
use crate::core::{Color, Point, Rect};
use crate::event::EventHandler;
use crate::widget::{Widget, WidgetKind};
use std::sync::{Arc, Mutex};

#[test]
fn freeform_shape_creation_defaults() {
    let shape = FreeformShapeWidget::new(Rect::new(10, 20, 200, 150), ShapePath::Heart);
    assert_eq!(shape.path(), &ShapePath::Heart);
    assert_eq!(shape.fill_color(), Color::rgb(200, 220, 255));
    assert_eq!(shape.stroke_color(), Some(Color::rgb(80, 120, 200)));
    assert_eq!(shape.stroke_width(), 2);
    assert!(shape.base().is_visible());
    assert!(shape.base().is_enabled());
}

#[test]
fn freeform_shape_set_path() {
    let mut shape = FreeformShapeWidget::new(Rect::new(0, 0, 100, 100), ShapePath::Heart);
    let polygon =
        ShapePath::Polygon(vec![Point::new(10, 10), Point::new(90, 10), Point::new(50, 90)]);
    shape.set_path(polygon.clone());
    assert_eq!(shape.path(), &polygon);
}

#[test]
fn freeform_shape_set_fill_color() {
    let mut shape = FreeformShapeWidget::new(Rect::new(0, 0, 100, 100), ShapePath::Heart);
    shape.set_fill_color(Color::rgb(255, 0, 0));
    assert_eq!(shape.fill_color(), Color::rgb(255, 0, 0));
}

#[test]
fn freeform_shape_set_stroke_color() {
    let mut shape = FreeformShapeWidget::new(Rect::new(0, 0, 100, 100), ShapePath::Heart);
    shape.set_stroke_color(Some(Color::rgb(0, 255, 0)));
    assert_eq!(shape.stroke_color(), Some(Color::rgb(0, 255, 0)));

    shape.set_stroke_color(None);
    assert_eq!(shape.stroke_color(), None);
}

#[test]
fn freeform_shape_set_stroke_width() {
    let mut shape = FreeformShapeWidget::new(Rect::new(0, 0, 100, 100), ShapePath::Heart);
    shape.set_stroke_width(5);
    assert_eq!(shape.stroke_width(), 5);
}

#[test]
fn freeform_shape_contains_heart_center() {
    // Heart in 100x100 rect — center at (50,50) should be inside
    let shape = FreeformShapeWidget::new(Rect::new(0, 0, 100, 100), ShapePath::Heart);
    assert!(shape.contains(Point::new(50, 50)));
}

#[test]
fn freeform_shape_contains_heart_outside_rect() {
    let shape = FreeformShapeWidget::new(Rect::new(0, 0, 100, 100), ShapePath::Heart);
    // Point outside the bounding rect
    assert!(!shape.contains(Point::new(150, 150)));
}

#[test]
fn freeform_shape_contains_polygon_center() {
    let triangle =
        ShapePath::Polygon(vec![Point::new(0, 0), Point::new(100, 0), Point::new(50, 100)]);
    let shape = FreeformShapeWidget::new(Rect::new(0, 0, 100, 100), triangle);
    // Center of the bounding rect is near (50, 30) which is inside the triangle
    assert!(shape.contains(Point::new(50, 30)));
}

#[test]
fn freeform_shape_contains_rounded_rect() {
    let shape =
        FreeformShapeWidget::new(Rect::new(0, 0, 100, 100), ShapePath::RoundedRect { radius: 10 });
    // Top-left inside rounded rect (beyond corner radius)
    assert!(shape.contains(Point::new(15, 15)));
    // Outside bounding rect
    assert!(!shape.contains(Point::new(200, 200)));
}

#[test]
fn freeform_shape_contains_zero_dimensions() {
    let shape = FreeformShapeWidget::new(Rect::new(0, 0, 0, 0), ShapePath::Heart);
    assert!(!shape.contains(Point::new(0, 0)));
}

#[test]
fn freeform_shape_rounded_rect_zero_radius_equal_rect() {
    let shape =
        FreeformShapeWidget::new(Rect::new(0, 0, 50, 50), ShapePath::RoundedRect { radius: 0 });
    // Should act as a plain rect hit-test
    assert!(shape.contains(Point::new(25, 25)));
    assert!(!shape.contains(Point::new(-1, 25)));
}

#[test]
fn freeform_shape_bubble_contains_body() {
    let shape = FreeformShapeWidget::new(
        Rect::new(10, 10, 100, 80),
        ShapePath::Bubble { tail_direction: BubbleTailDirection::Right },
    );
    // Inside the body
    assert!(shape.contains(Point::new(60, 50)));
}

#[test]
fn freeform_shape_star_contains_center() {
    let shape = FreeformShapeWidget::new(
        Rect::new(0, 0, 100, 100),
        ShapePath::Star { points: 5, inner_radius: 0.4 },
    );
    // Center of star is always inside
    assert!(shape.contains(Point::new(50, 50)));
}

#[test]
fn freeform_shape_custom_path_empty() {
    let shape = FreeformShapeWidget::new(Rect::new(0, 0, 100, 100), ShapePath::Custom(vec![]));
    // Empty custom path should not contain anything
    assert!(!shape.contains(Point::new(50, 50)));
}

#[test]
fn freeform_shape_custom_path_triangle() {
    let segs = vec![
        PathSegment::MoveTo(Point::new(0, 0)),
        PathSegment::LineTo(Point::new(100, 0)),
        PathSegment::LineTo(Point::new(50, 100)),
        PathSegment::Close,
    ];
    let shape = FreeformShapeWidget::new(Rect::new(0, 0, 100, 100), ShapePath::Custom(segs));
    assert!(shape.contains(Point::new(50, 30)));
    assert!(!shape.contains(Point::new(200, 200)));
}

#[test]
fn freeform_shape_geometry_delegation() {
    let rect = Rect::new(5, 10, 300, 200);
    let shape = FreeformShapeWidget::new(rect, ShapePath::Heart);
    assert_eq!(shape.geometry(), rect);
    assert_eq!(shape.position(), Point::new(5, 10));
    assert_eq!(shape.size(), crate::core::Size::new(300, 200));
}

#[test]
fn freeform_shape_visibility() {
    let mut shape = FreeformShapeWidget::new(Rect::new(0, 0, 100, 100), ShapePath::Heart);
    assert!(shape.is_visible());
    shape.set_visible(false);
    assert!(!shape.is_visible());
    shape.set_visible(true);
    assert!(shape.is_visible());
}

#[test]
fn freeform_shape_enabled_state() {
    let mut shape = FreeformShapeWidget::new(Rect::new(0, 0, 100, 100), ShapePath::Heart);
    assert!(shape.is_enabled());
    shape.set_enabled(false);
    assert!(!shape.is_enabled());
}

#[test]
fn freeform_shape_kind() {
    let shape = FreeformShapeWidget::new(Rect::new(0, 0, 100, 100), ShapePath::Heart);
    assert_eq!(shape.kind(), WidgetKind::FreeformShape);
}

#[test]
fn freeform_shape_ids_are_unique() {
    let a = FreeformShapeWidget::new(Rect::new(0, 0, 100, 100), ShapePath::Heart);
    let b = FreeformShapeWidget::new(Rect::new(0, 0, 100, 100), ShapePath::Heart);
    assert_ne!(a.id(), b.id());
}

#[test]
fn freeform_shape_clicked_signal_emits_on_click() {
    let mut shape = FreeformShapeWidget::new(Rect::new(0, 0, 200, 200), ShapePath::Heart);
    let count = Arc::new(Mutex::new(0u32));
    let sink = count.clone();
    shape.clicked.connect(move || {
        if let Ok(mut c) = sink.lock() {
            *c += 1;
        }
    });

    // Press inside shape
    let press =
        crate::event::Event::MousePress { pos: Point::new(100, 100), button: 1, modifiers: 0 };
    shape.handle_event(&press);

    // Release inside shape
    let release = crate::event::Event::MouseRelease { pos: Point::new(100, 100), button: 1 };
    shape.handle_event(&release);

    let got = *count.lock().unwrap();
    assert_eq!(got, 1, "clicked signal should fire once on press+release inside");
}

#[test]
fn freeform_shape_click_outside_no_signal() {
    let mut shape = FreeformShapeWidget::new(Rect::new(0, 0, 200, 200), ShapePath::Heart);
    let count = Arc::new(Mutex::new(0u32));
    let sink = count.clone();
    shape.clicked.connect(move || {
        if let Ok(mut c) = sink.lock() {
            *c += 1;
        }
    });

    // Press outside
    let press =
        crate::event::Event::MousePress { pos: Point::new(999, 999), button: 1, modifiers: 0 };
    shape.handle_event(&press);

    let release = crate::event::Event::MouseRelease { pos: Point::new(999, 999), button: 1 };
    shape.handle_event(&release);

    let got = *count.lock().unwrap();
    assert_eq!(got, 0, "click should not fire for outside press+release");
}

#[test]
fn freeform_shape_hovered_signal_emits_on_mouse_move() {
    let mut shape = FreeformShapeWidget::new(Rect::new(0, 0, 200, 200), ShapePath::Heart);
    let states = Arc::new(Mutex::new(Vec::<bool>::new()));
    let sink = states.clone();
    shape.hover_changed.connect(move |h| {
        if let Ok(mut s) = sink.lock() {
            s.push(*h);
        }
    });

    // Move into shape
    let ev = crate::event::Event::MouseMove { pos: Point::new(100, 100) };
    shape.handle_event(&ev);

    // Move out of shape
    let ev2 = crate::event::Event::MouseMove { pos: Point::new(999, 999) };
    shape.handle_event(&ev2);

    let got = states.lock().ok().map(|g| g.clone()).unwrap_or_default();
    assert_eq!(got, vec![true, false], "hover_changed should fire true then false");
}

#[test]
fn freeform_shape_pressed_signal_emits_on_press() {
    let mut shape = FreeformShapeWidget::new(Rect::new(0, 0, 200, 200), ShapePath::Heart);
    let states = Arc::new(Mutex::new(Vec::<bool>::new()));
    let sink = states.clone();
    shape.pressed_changed.connect(move |p| {
        if let Ok(mut s) = sink.lock() {
            s.push(*p);
        }
    });

    // Press inside
    let press =
        crate::event::Event::MousePress { pos: Point::new(100, 100), button: 1, modifiers: 0 };
    shape.handle_event(&press);

    // Release
    let release = crate::event::Event::MouseRelease { pos: Point::new(100, 100), button: 1 };
    shape.handle_event(&release);

    let got = states.lock().ok().map(|g| g.clone()).unwrap_or_default();
    assert_eq!(got, vec![true, false], "pressed_changed should fire true then false");
}

#[test]
fn freeform_shape_disabled_ignores_events() {
    let mut shape = FreeformShapeWidget::new(Rect::new(0, 0, 200, 200), ShapePath::Heart);
    shape.set_enabled(false);

    let count = Arc::new(Mutex::new(0u32));
    let sink = count.clone();
    shape.clicked.connect(move || {
        if let Ok(mut c) = sink.lock() {
            *c += 1;
        }
    });

    let press =
        crate::event::Event::MousePress { pos: Point::new(100, 100), button: 1, modifiers: 0 };
    shape.handle_event(&press);

    let release = crate::event::Event::MouseRelease { pos: Point::new(100, 100), button: 1 };
    shape.handle_event(&release);

    let got = *count.lock().unwrap();
    assert_eq!(got, 0, "disabled widget should not process events");
}

#[test]
fn freeform_shape_mouse_enter_and_leave() {
    let mut shape = FreeformShapeWidget::new(Rect::new(0, 0, 200, 200), ShapePath::Heart);
    let states = Arc::new(Mutex::new(Vec::<bool>::new()));
    let sink = states.clone();
    shape.hover_changed.connect(move |h| {
        if let Ok(mut s) = sink.lock() {
            s.push(*h);
        }
    });

    let enter = crate::event::Event::MouseEnter { pos: Point::new(100, 100) };
    shape.handle_event(&enter);

    let leave = crate::event::Event::MouseLeave { pos: Point::new(100, 100) };
    shape.handle_event(&leave);

    let got = states.lock().ok().map(|g| g.clone()).unwrap_or_default();
    assert_eq!(got, vec![true, false], "MouseEnter/MouseLeave should fire hover_changed");
}

#[test]
fn freeform_shape_bubble_tail_directions() {
    for dir in &[
        BubbleTailDirection::TopLeft,
        BubbleTailDirection::TopRight,
        BubbleTailDirection::BottomLeft,
        BubbleTailDirection::BottomRight,
        BubbleTailDirection::Left,
        BubbleTailDirection::Right,
        BubbleTailDirection::Top,
        BubbleTailDirection::Bottom,
    ] {
        let shape = FreeformShapeWidget::new(
            Rect::new(0, 0, 80, 60),
            ShapePath::Bubble { tail_direction: *dir },
        );
        // Center should be inside regardless of tail direction
        assert!(shape.contains(Point::new(40, 30)), "center should be inside for {:?}", dir);
    }
}

#[test]
fn freeform_shape_polygon_less_than_3_vertices() {
    let shape = FreeformShapeWidget::new(
        Rect::new(0, 0, 100, 100),
        ShapePath::Polygon(vec![Point::new(0, 0), Point::new(100, 0)]),
    );
    // 2 vertices can't form a polygon
    assert!(!shape.contains(Point::new(50, 25)));
}

#[test]
fn freeform_shape_star_few_points() {
    let shape = FreeformShapeWidget::new(
        Rect::new(0, 0, 100, 100),
        ShapePath::Star { points: 3, inner_radius: 0.5 },
    );
    assert!(shape.contains(Point::new(50, 50)));
}

#[test]
fn freeform_shape_path_enum_variants() {
    // Verify all variants can be constructed and compared
    let paths = [
        ShapePath::Heart,
        ShapePath::Star { points: 5, inner_radius: 0.4 },
        ShapePath::Polygon(vec![Point::new(0, 0), Point::new(50, 0), Point::new(25, 50)]),
        ShapePath::RoundedRect { radius: 8 },
        ShapePath::Bubble { tail_direction: BubbleTailDirection::Top },
        ShapePath::Custom(vec![
            PathSegment::MoveTo(Point::new(0, 0)),
            PathSegment::LineTo(Point::new(100, 0)),
            PathSegment::Close,
        ]),
        ShapePath::UnitCustom(vec![
            UnitPathSegment::MoveTo(0.0, 0.0),
            UnitPathSegment::LineTo(1.0, 0.0),
            UnitPathSegment::Close,
        ]),
    ];

    // Check all paths are different (no duplicates)
    for i in 0..paths.len() {
        for j in (i + 1)..paths.len() {
            assert_ne!(paths[i], paths[j], "path variant {} should differ from {}", i, j);
        }
    }
}

/// A unit-square custom path scales with the widget instead of tracking its pixels.
#[test]
fn freeform_shape_unit_custom_path_scales_with_the_widget() {
    let square = |size: u32| {
        FreeformShapeWidget::new(
            Rect::new(0, 0, size, size),
            ShapePath::UnitCustom(vec![
                UnitPathSegment::MoveTo(0.0, 0.0),
                UnitPathSegment::LineTo(1.0, 0.0),
                UnitPathSegment::LineTo(1.0, 1.0),
                UnitPathSegment::LineTo(0.0, 1.0),
                UnitPathSegment::Close,
            ]),
        )
    };

    for size in [40, 100, 250] {
        let shape = square(size);
        assert!(
            shape.contains(Point::new(size as i32 / 2, size as i32 / 2)),
            "the unit square covers the centre at {size}px"
        );
        assert!(
            shape.contains(Point::new(size as i32 - 1, size as i32 - 1)),
            "the unit square covers the far corner at {size}px"
        );
    }
}

// ── ED-28 / ED-29 / ED-30 / ED-38 ──────────────────────────────────────────

#[test]
fn dragging_the_body_moves_the_shape_and_is_undoable() {
    let mut shape = FreeformShapeWidget::new(Rect::new(0, 0, 100, 100), ShapePath::Heart);
    let moves = Arc::new(Mutex::new(Vec::new()));
    let sink = Arc::clone(&moves);
    shape.moved.connect(move |position| sink.lock().unwrap().push(*position));

    shape.handle_event(&crate::event::Event::MousePress {
        pos: Point::new(50, 50),
        button: 1,
        modifiers: 0,
    });
    shape.handle_event(&crate::event::Event::MouseMove { pos: Point::new(70, 80) });
    assert_eq!(shape.geometry().x, 20);
    assert_eq!(shape.geometry().y, 30);
    shape.handle_event(&crate::event::Event::MouseRelease { pos: Point::new(70, 80), button: 1 });

    assert!(!moves.lock().unwrap().is_empty(), "a move must be reported");
    assert!(shape.can_undo());
    assert!(shape.undo());
    assert_eq!(shape.geometry(), Rect::new(0, 0, 100, 100));
    assert!(shape.redo());
    assert_eq!(shape.geometry(), Rect::new(20, 30, 100, 100));
}

#[test]
fn a_polygon_exposes_vertex_handles_and_supports_add_remove() {
    let mut shape = FreeformShapeWidget::new(
        Rect::new(0, 0, 100, 100),
        ShapePath::Polygon(vec![Point::new(10, 10), Point::new(90, 10), Point::new(50, 90)]),
    );
    shape.set_editable(true);
    let handles = shape.handle_rects();
    assert_eq!(handles.len(), 3, "a polygon has one handle per vertex");
    assert!(matches!(handles[0].0, super::shape::ShapeHandle::Vertex(0)));

    assert!(shape.add_vertex(1, Point::new(50, 5)));
    assert_eq!(shape.handle_rects().len(), 4);
    assert!(shape.remove_vertex(1));
    assert_eq!(shape.handle_rects().len(), 3);
    assert!(!shape.remove_vertex(0), "a polygon never drops below three vertices");
}

#[test]
fn dragging_a_vertex_updates_the_polygon() {
    let mut shape = FreeformShapeWidget::new(
        Rect::new(0, 0, 100, 100),
        ShapePath::Polygon(vec![Point::new(10, 10), Point::new(90, 10), Point::new(50, 90)]),
    );
    shape.set_editable(true);
    let changed = Arc::new(Mutex::new(Vec::new()));
    let sink = Arc::clone(&changed);
    shape.vertex_changed.connect(move |event| sink.lock().unwrap().push(*event));

    let handle = shape.handle_rects()[0].1;
    let start = Point::new(handle.x + 4, handle.y + 4);
    shape.handle_event(&crate::event::Event::MousePress { pos: start, button: 1, modifiers: 0 });
    shape.handle_event(&crate::event::Event::MouseMove {
        pos: Point::new(start.x + 20, start.y + 20),
    });
    shape.handle_event(&crate::event::Event::MouseRelease {
        pos: Point::new(start.x + 20, start.y + 20),
        button: 1,
    });

    let events = changed.lock().unwrap();
    assert!(events.iter().any(|(index, _)| *index == 0));
    match shape.path() {
        ShapePath::Polygon(vertices) => assert_eq!(vertices[0], Point::new(30, 30)),
        _ => panic!("the path is still a polygon"),
    }
}

#[test]
fn mouse_enter_does_not_claim_the_transparent_corner() {
    let mut shape = FreeformShapeWidget::new(
        Rect::new(0, 0, 100, 100),
        ShapePath::Star { points: 5, inner_radius: 0.4 },
    );
    let states = Arc::new(Mutex::new(Vec::new()));
    let sink = Arc::clone(&states);
    shape.hover_changed.connect(move |hovered| sink.lock().unwrap().push(*hovered));

    // The top-left corner of the bounding box is outside a five-point star.
    shape.handle_event(&crate::event::Event::MouseEnter { pos: Point::new(0, 0) });
    assert!(states.lock().unwrap().is_empty(), "no hover may be reported off-outline");
    shape.handle_event(&crate::event::Event::MouseEnter { pos: Point::new(50, 50) });
    assert_eq!(*states.lock().unwrap(), vec![true]);
}