graphix-package-gui 0.8.0

A dataflow language for UIs and network programming, GUI package
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
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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
use super::{expect_call, expect_call_with_args, InteractionHarness};
use anyhow::Result;
use iced_core::{Point, Size};
use netidx::publisher::Value;

/// Standard widget imports for interaction tests.
const IMPORTS: &str = "\
use gui;\n\
use gui::text;\n\
use gui::button;\n\
use gui::checkbox;\n\
use gui::toggler;\n\
use gui::text_input;\n\
use gui::slider;\n\
use gui::radio;\n\
use gui::pick_list;\n\
use gui::text_editor;\n\
use gui::vertical_slider;\n\
use gui::mouse_area;\n\
use gui::keyboard_area;\n\
use gui::scrollable;\n\
use gui::combo_box;\n\
use gui::column";

async fn harness(widget_expr: &str) -> Result<InteractionHarness> {
    let code = format!("{IMPORTS};\nlet result = {widget_expr}");
    InteractionHarness::new(&code).await
}

/// Click near the origin — widgets use Shrink sizing and are laid out
/// at (0,0), so clicking at the viewport center misses them.
const WIDGET_HIT: Point = Point::new(10.0, 10.0);

// ── Button ──────────────────────────────────────────────────────────

#[tokio::test(flavor = "current_thread")]
async fn button_click_produces_call() -> Result<()> {
    let mut h = harness("button(#on_press: |_| null, &text(&\"Click me\"))").await?;
    let msgs = h.click(WIDGET_HIT);
    expect_call(&msgs);
    Ok(())
}

// Note: the graphix `button` function always provides a default
// `on_press: |_| null`, so there is no way to create a button without
// an on_press via the graphix function.

// ── Checkbox ────────────────────────────────────────────────────────

// Note: checkbox/toggler/slider/radio interactions produce Call messages
// via on_toggle/on_change/on_select callbacks. Without a callback, the
// widget is display-only. These tests verify both the no-callback case
// (no panic) and the callback case (produces Call).

#[tokio::test(flavor = "current_thread")]
async fn checkbox_click_no_panic() -> Result<()> {
    let code = format!(
        "{IMPORTS};\n\
         let checked = &false;\n\
         let result = checkbox(#label: &\"Toggle me\", checked)"
    );
    let mut h = InteractionHarness::new(&code).await?;
    let _ = h.view();
    let _ = h.click(WIDGET_HIT);
    Ok(())
}

#[tokio::test(flavor = "current_thread")]
async fn checkbox_toggle_produces_call() -> Result<()> {
    let code = format!(
        "{IMPORTS};\n\
         let result = checkbox(#label: &\"Toggle me\", #on_toggle: |v| null, &false)"
    );
    let mut h = InteractionHarness::new(&code).await?;
    let msgs = h.click(WIDGET_HIT);
    expect_call_with_args(&msgs, |args| {
        matches!(args.iter().next(), Some(Value::Bool(_)))
    });
    Ok(())
}

// ── Toggler ─────────────────────────────────────────────────────────

#[tokio::test(flavor = "current_thread")]
async fn toggler_click_no_panic() -> Result<()> {
    let code = format!(
        "{IMPORTS};\n\
         let toggled = &false;\n\
         let result = toggler(#label: &\"Dark mode\", toggled)"
    );
    let mut h = InteractionHarness::new(&code).await?;
    let _ = h.view();
    let _ = h.click(WIDGET_HIT);
    Ok(())
}

#[tokio::test(flavor = "current_thread")]
async fn toggler_toggle_produces_call() -> Result<()> {
    let code = format!(
        "{IMPORTS};\n\
         let result = toggler(#label: &\"Dark mode\", #on_toggle: |v| null, &false)"
    );
    let mut h = InteractionHarness::new(&code).await?;
    let msgs = h.click(WIDGET_HIT);
    expect_call_with_args(&msgs, |args| {
        matches!(args.iter().next(), Some(Value::Bool(_)))
    });
    Ok(())
}

// ── Slider ──────────────────────────────────────────────────────────

#[tokio::test(flavor = "current_thread")]
async fn slider_click_no_panic() -> Result<()> {
    let code = format!(
        "{IMPORTS};\n\
         let val = &50.0;\n\
         let result = slider(#min: &0.0, #max: &100.0, val)"
    );
    let mut h = InteractionHarness::with_viewport(&code, Size::new(200.0, 22.0)).await?;
    let _ = h.click(Point::new(150.0, 10.0));
    Ok(())
}

#[tokio::test(flavor = "current_thread")]
async fn slider_drag_no_panic() -> Result<()> {
    let code = format!(
        "{IMPORTS};\n\
         let val = &50.0;\n\
         let result = slider(#min: &0.0, #max: &100.0, val)"
    );
    let mut h = InteractionHarness::with_viewport(&code, Size::new(200.0, 22.0)).await?;
    let from = Point::new(100.0, 10.0);
    let _ = h.drag_horizontal(from, 180.0, 5);
    Ok(())
}

#[tokio::test(flavor = "current_thread")]
async fn slider_on_change_produces_call() -> Result<()> {
    let code = format!(
        "{IMPORTS};\n\
         let changed = false;\n\
         let result = slider(#min: &0.0, #max: &100.0, \
             #on_change: |v| changed <- v ~ true, &50.0)"
    );
    let mut h = InteractionHarness::with_viewport(&code, Size::new(200.0, 22.0)).await?;
    let initial = h.watch("test::changed").await?;
    assert_eq!(initial, Value::Bool(false));
    let msgs = h.click(Point::new(150.0, 10.0));
    h.dispatch_calls(&msgs).await?;
    assert_eq!(h.get_watched("test::changed"), Some(&Value::Bool(true)));
    Ok(())
}

#[tokio::test(flavor = "current_thread")]
async fn slider_on_release_produces_call() -> Result<()> {
    let code = format!(
        "{IMPORTS};\n\
         let released = false;\n\
         let result = slider(#min: &0.0, #max: &100.0, \
             #on_release: |click| released <- click ~ true, &50.0)"
    );
    let mut h = InteractionHarness::with_viewport(&code, Size::new(200.0, 22.0)).await?;
    let initial = h.watch("test::released").await?;
    assert_eq!(initial, Value::Bool(false));
    let msgs = h.click(Point::new(150.0, 10.0));
    h.dispatch_calls(&msgs).await?;
    assert_eq!(h.get_watched("test::released"), Some(&Value::Bool(true)));
    Ok(())
}

// ── VerticalSlider ──────────────────────────────────────────────────

#[tokio::test(flavor = "current_thread")]
async fn vertical_slider_click_no_panic() -> Result<()> {
    let code = format!(
        "{IMPORTS};\n\
         let val = &50.0;\n\
         let result = vertical_slider(#min: &0.0, #max: &100.0, val)"
    );
    let mut h = InteractionHarness::with_viewport(&code, Size::new(22.0, 200.0)).await?;
    let _ = h.click(Point::new(10.0, 50.0));
    Ok(())
}

#[tokio::test(flavor = "current_thread")]
async fn vertical_slider_on_change_produces_call() -> Result<()> {
    let code = format!(
        "{IMPORTS};\n\
         let changed = false;\n\
         let result = vertical_slider(\
             #min: &0.0, #max: &100.0, #on_change: |v| changed <- v ~ true, &50.0)"
    );
    let mut h = InteractionHarness::with_viewport(&code, Size::new(22.0, 200.0)).await?;
    let initial = h.watch("test::changed").await?;
    assert_eq!(initial, Value::Bool(false));
    let msgs = h.click(Point::new(10.0, 50.0));
    h.dispatch_calls(&msgs).await?;
    assert_eq!(h.get_watched("test::changed"), Some(&Value::Bool(true)));
    Ok(())
}

#[tokio::test(flavor = "current_thread")]
async fn vertical_slider_on_release_produces_call() -> Result<()> {
    let code = format!(
        "{IMPORTS};\n\
         let released = false;\n\
         let result = vertical_slider(\
             #min: &0.0, #max: &100.0, \
             #on_release: |click| released <- click ~ true, &50.0)"
    );
    let mut h = InteractionHarness::with_viewport(&code, Size::new(22.0, 200.0)).await?;
    let initial = h.watch("test::released").await?;
    assert_eq!(initial, Value::Bool(false));
    let msgs = h.click(Point::new(10.0, 50.0));
    h.dispatch_calls(&msgs).await?;
    assert_eq!(h.get_watched("test::released"), Some(&Value::Bool(true)));
    Ok(())
}

// ── TextInput ───────────────────────────────────────────────────────

#[tokio::test(flavor = "current_thread")]
async fn text_input_click_and_type_no_panic() -> Result<()> {
    let code = format!(
        "{IMPORTS};\n\
         let val = &\"\";\n\
         let result = text_input(#placeholder: &\"Type here\", val)"
    );
    let mut h = InteractionHarness::new(&code).await?;
    h.click(WIDGET_HIT);
    let _ = h.type_text("abc");
    Ok(())
}

#[tokio::test(flavor = "current_thread")]
async fn text_input_submit_produces_call() -> Result<()> {
    let code = format!(
        "{IMPORTS};\n\
         let val = &\"\";\n\
         let result = text_input(\
             #placeholder: &\"Search\", \
             #on_submit: |_| null, \
             val)"
    );
    let mut h = InteractionHarness::new(&code).await?;
    h.click(WIDGET_HIT);
    h.type_text("query");
    let msgs = h.press_key(iced_core::keyboard::key::Named::Enter);
    expect_call(&msgs);
    Ok(())
}

#[tokio::test(flavor = "current_thread")]
async fn text_input_on_input_produces_call() -> Result<()> {
    let code = format!(
        "{IMPORTS};\n\
         let result = text_input(\
             #placeholder: &\"Type here\", \
             #on_input: |s| null, \
             &\"\")"
    );
    let mut h = InteractionHarness::new(&code).await?;
    h.click(WIDGET_HIT);
    let msgs = h.type_text("a");
    expect_call_with_args(&msgs, |args| {
        matches!(args.iter().next(), Some(Value::String(_)))
    });
    Ok(())
}

// ── Radio ───────────────────────────────────────────────────────────

#[tokio::test(flavor = "current_thread")]
async fn radio_click_no_panic() -> Result<()> {
    let code = format!(
        "{IMPORTS};\n\
         let sel = &\"none\";\n\
         let result = radio(#label: &\"Option A\", #selected: sel, &\"option_a\")"
    );
    let mut h = InteractionHarness::new(&code).await?;
    let _ = h.view();
    let _ = h.click(WIDGET_HIT);
    Ok(())
}

#[tokio::test(flavor = "current_thread")]
async fn radio_on_select_produces_call() -> Result<()> {
    let code = format!(
        "{IMPORTS};\n\
         let result = radio(\
             #label: &\"Option A\", \
             #selected: &\"none\", \
             #on_select: |v| null, \
             &\"option_a\")"
    );
    let mut h = InteractionHarness::new(&code).await?;
    let msgs = h.click(WIDGET_HIT);
    expect_call_with_args(&msgs, |args| {
        matches!(args.iter().next(), Some(Value::String(_)))
    });
    Ok(())
}

// ── PickList ────────────────────────────────────────────────────────

#[tokio::test(flavor = "current_thread")]
async fn pick_list_basic() -> Result<()> {
    let mut h = harness(
        "pick_list(\
            #selected: &\"Red\",\
            #placeholder: &\"Choose...\",\
            &[\"Red\", \"Green\", \"Blue\"])",
    )
    .await?;
    let _ = h.view();
    let _ = h.click(WIDGET_HIT);
    Ok(())
}

#[tokio::test(flavor = "current_thread")]
async fn pick_list_on_select_produces_call() -> Result<()> {
    let code = format!(
        "{IMPORTS};\n\
         let result = pick_list(\
             #selected: &\"Red\", \
             #on_select: |s| null, \
             #placeholder: &\"Choose...\", \
             &[\"Red\", \"Green\", \"Blue\"])"
    );
    // Pick list uses an overlay for the dropdown menu. Headless
    // UserInterface may not route overlay clicks correctly, so we
    // verify the widget compiles and accepts clicks without panic.
    // A full on_select test requires overlay interaction support.
    let mut h = InteractionHarness::with_viewport(&code, Size::new(300.0, 200.0)).await?;
    let _ = h.view();
    let _ = h.click(WIDGET_HIT);
    // TODO: investigate overlay interaction to verify Call message
    Ok(())
}

// ── MouseArea ───────────────────────────────────────────────────────

#[tokio::test(flavor = "current_thread")]
async fn mouse_area_press_produces_call() -> Result<()> {
    let code = format!(
        "{IMPORTS};\n\
         let pressed = false;\n\
         let result = mouse_area(\
             #on_press: |click| pressed <- click ~ true, \
             &text(&\"Click zone\"))"
    );
    let mut h = InteractionHarness::new(&code).await?;
    let initial = h.watch("test::pressed").await?;
    assert_eq!(initial, Value::Bool(false));
    let msgs = h.click(WIDGET_HIT);
    h.dispatch_calls(&msgs).await?;
    assert_eq!(h.get_watched("test::pressed"), Some(&Value::Bool(true)));
    Ok(())
}

#[tokio::test(flavor = "current_thread")]
async fn mouse_area_release_produces_call() -> Result<()> {
    let code = format!(
        "{IMPORTS};\n\
         let released = false;\n\
         let result = mouse_area(\
             #on_release: |click| released <- click ~ true, \
             &text(&\"Click zone\"))"
    );
    let mut h = InteractionHarness::new(&code).await?;
    let initial = h.watch("test::released").await?;
    assert_eq!(initial, Value::Bool(false));
    let msgs = h.click(WIDGET_HIT);
    h.dispatch_calls(&msgs).await?;
    assert_eq!(h.get_watched("test::released"), Some(&Value::Bool(true)));
    Ok(())
}

// ── TextEditor ──────────────────────────────────────────────────────

#[tokio::test(flavor = "current_thread")]
async fn text_editor_click_and_type_no_panic() -> Result<()> {
    let code = format!(
        "{IMPORTS};\n\
         let val = &\"\";\n\
         let result = text_editor(#placeholder: &\"Edit...\", val)"
    );
    let mut h = InteractionHarness::with_viewport(&code, Size::new(300.0, 100.0)).await?;
    h.click(WIDGET_HIT);
    let _ = h.type_text("hello");
    Ok(())
}

#[tokio::test(flavor = "current_thread")]
async fn text_editor_on_edit_produces_callback() -> Result<()> {
    let code = format!(
        "{IMPORTS};\n\
         let result = text_editor(#placeholder: &\"Edit...\", #on_edit: |s| null, &\"\")"
    );
    let mut h = InteractionHarness::with_viewport(&code, Size::new(300.0, 100.0)).await?;
    h.click(WIDGET_HIT);
    let msgs = h.type_text("a");
    let results = h.process_editor_actions(&msgs);
    assert!(
        results.iter().any(|(_, v)| matches!(v, Value::String(_))),
        "text_editor on_edit should produce a String value callback"
    );
    Ok(())
}

// ── ComboBox ────────────────────────────────────────────────────────

#[tokio::test(flavor = "current_thread")]
async fn combo_box_on_select_produces_call() -> Result<()> {
    let code = format!(
        "{IMPORTS};\n\
         let result = combo_box(\
             #selected: &\"Alpha\", \
             #on_select: |s| null, \
             #placeholder: &\"Pick one\", \
             &[\"Alpha\", \"Beta\", \"Gamma\"])"
    );
    // ComboBox uses an overlay for suggestions, similar to PickList.
    // Verify it compiles and accepts focus without panic.
    let mut h = InteractionHarness::with_viewport(&code, Size::new(300.0, 200.0)).await?;
    let _ = h.view();
    let _ = h.click(WIDGET_HIT);
    // TODO: investigate overlay interaction to verify Call message
    Ok(())
}

// ── Scrollable ──────────────────────────────────────────────────────

#[tokio::test(flavor = "current_thread")]
async fn scrollable_on_scroll_produces_call() -> Result<()> {
    // Build a scrollable with enough content to overflow and trigger scrolling
    let code = format!(
        "{IMPORTS};\n\
         let result = scrollable(\
             #on_scroll: |pos| null, \
             #height: &`Fixed(50.0), \
             &column(#spacing: &10.0, &[\
                 text(&\"Line 1\"), text(&\"Line 2\"), text(&\"Line 3\"), \
                 text(&\"Line 4\"), text(&\"Line 5\"), text(&\"Line 6\"), \
                 text(&\"Line 7\"), text(&\"Line 8\"), text(&\"Line 9\"), \
                 text(&\"Line 10\")\
             ]))"
    );
    let mut h = InteractionHarness::with_viewport(&code, Size::new(300.0, 50.0)).await?;
    // Move cursor into bounds, then scroll
    h.move_cursor(Point::new(10.0, 10.0));
    let msgs = h.scroll(0.0, 3.0);
    expect_call(&msgs);
    Ok(())
}

// ── MouseArea (additional callbacks) ────────────────────────────────

// mouse_area has many callback slots, so a bare `expect_call` can't
// tell which one fired (e.g. on_move would match the assertion even
// when testing on_enter). Every callback test flips a graphix-side
// variable only that specific handler could reach, then verifies the
// variable's post-dispatch value — the same pattern slider uses.

#[tokio::test(flavor = "current_thread")]
async fn mouse_area_on_enter_produces_call() -> Result<()> {
    let code = format!(
        "{IMPORTS};\n\
         let entered = false;\n\
         let result = mouse_area(\
             #on_enter: |click| entered <- click ~ true, \
             &text(&\"Zone\"))"
    );
    let mut h = InteractionHarness::new(&code).await?;
    let initial = h.watch("test::entered").await?;
    assert_eq!(initial, Value::Bool(false));
    let msgs = h.move_cursor(WIDGET_HIT);
    h.dispatch_calls(&msgs).await?;
    assert_eq!(h.get_watched("test::entered"), Some(&Value::Bool(true)));
    Ok(())
}

#[tokio::test(flavor = "current_thread")]
async fn mouse_area_on_exit_produces_call() -> Result<()> {
    let code = format!(
        "{IMPORTS};\n\
         let exited = false;\n\
         let result = mouse_area(\
             #on_exit: |click| exited <- click ~ true, \
             &text(&\"Zone\"))"
    );
    let mut h = InteractionHarness::new(&code).await?;
    let initial = h.watch("test::exited").await?;
    assert_eq!(initial, Value::Bool(false));
    // Enter first, then exit
    h.move_cursor(WIDGET_HIT);
    let msgs = h.move_cursor(Point::new(999.0, 999.0));
    h.dispatch_calls(&msgs).await?;
    assert_eq!(h.get_watched("test::exited"), Some(&Value::Bool(true)));
    Ok(())
}

#[tokio::test(flavor = "current_thread")]
async fn mouse_area_on_move_produces_call() -> Result<()> {
    let code = format!(
        "{IMPORTS};\n\
         let moved = false;\n\
         let result = mouse_area(\
             #on_move: |pos| moved <- pos ~ true, \
             &text(&\"Zone\"))"
    );
    let mut h = InteractionHarness::new(&code).await?;
    let initial = h.watch("test::moved").await?;
    assert_eq!(initial, Value::Bool(false));
    // Enter first so iced's on_enter arm is consumed — only subsequent
    // cursor motion inside the bounds reaches the on_move arm.
    h.move_cursor(Point::new(5.0, 5.0));
    let msgs = h.move_cursor(WIDGET_HIT);
    h.dispatch_calls(&msgs).await?;
    assert_eq!(h.get_watched("test::moved"), Some(&Value::Bool(true)));
    Ok(())
}

// ── KeyboardArea ────────────────────────────────────────────────────

#[tokio::test(flavor = "current_thread")]
async fn keyboard_area_on_key_press_produces_call() -> Result<()> {
    let code = format!(
        "{IMPORTS};\n\
         let pressed = false;\n\
         let result = keyboard_area(\
             #on_key_press: |ev| pressed <- ev ~ true, \
             &text(&\"Type here\"))"
    );
    let mut h = InteractionHarness::new(&code).await?;
    let initial = h.watch("test::pressed").await?;
    assert_eq!(initial, Value::Bool(false));
    // Click to focus the keyboard_area
    h.click(WIDGET_HIT);
    let msgs = h.press_key(iced_core::keyboard::key::Named::Space);
    h.dispatch_calls(&msgs).await?;
    assert_eq!(h.get_watched("test::pressed"), Some(&Value::Bool(true)));
    Ok(())
}

#[tokio::test(flavor = "current_thread")]
async fn keyboard_area_on_key_release_produces_call() -> Result<()> {
    let code = format!(
        "{IMPORTS};\n\
         let released = false;\n\
         let result = keyboard_area(\
             #on_key_release: |ev| released <- ev ~ true, \
             &text(&\"Type here\"))"
    );
    let mut h = InteractionHarness::new(&code).await?;
    let initial = h.watch("test::released").await?;
    assert_eq!(initial, Value::Bool(false));
    // Click to focus the keyboard_area
    h.click(WIDGET_HIT);
    let msgs = h.release_key(iced_core::keyboard::key::Named::Space);
    h.dispatch_calls(&msgs).await?;
    assert_eq!(h.get_watched("test::released"), Some(&Value::Bool(true)));
    Ok(())
}