Skip to main content

smoke_controls/
smoke_controls.rs

1//! Scripted smoke-test target (driven by scripts/smoke-gtk.sh with xdotool): prints one line per
2//! event / widget bounds on stdout so a shell script can click and type at known positions.
3use rungui::*;
4
5fn main() {
6    let app = App::new("smoke").expect("init");
7    let a = Window::new("smoke-a");
8    let col = VBox::new(a);
9    let btn = Button::new(col, "Press ünï");
10    let txt = TextInput::new(col);
11    let chk = CheckBox::new(col, "Check");
12    let list = ListBox::new(col);
13    list.set_items(&["one", "two", "three"]);
14    let slider = Slider::new(col, 0.0, 100.0);
15    let combo = ComboBox::new(col);
16    combo.set_items(&["x", "y"]);
17    let dlg = Button::new(col, "Dialog");
18    dlg.on_click(move || {
19        let r = message_box(
20            Some(a),
21            MessageKind::Question,
22            Buttons::YesNo,
23            "Title ü",
24            "Really?",
25        );
26        println!("DIALOG {r:?}");
27    });
28    let g = RadioGroup::new();
29    let ra = RadioButton::new(col, &g, "Ra");
30    let rb = RadioButton::new(col, &g, "Rb");
31    ra.set_checked(true);
32    ra.on_toggle(|v| println!("RA {v}"));
33    rb.on_toggle(|v| println!("RB {v}"));
34    let table = Table::new(col);
35    table.set_columns(&[
36        Column::new("Name").width(80).sortable(true),
37        Column::new("Size").align(ColumnAlign::Right),
38    ]);
39    table.set_rows(&[vec!["a", "1"], vec!["b", "2"], vec!["c", "3"]]);
40    table.on_select(|i| println!("TABLE_SEL {i:?}"));
41    table.on_column_click(|c| println!("TABLE_COL {c}"));
42    let tree = Tree::new(col);
43    let root = tree.add(None, "root");
44    let kid = tree.add(Some(root), "kid");
45    let lazy = tree.add(None, "lazy");
46    tree.set_has_children(lazy, true);
47    let _ = kid;
48    tree.on_select(|n| println!("TREE_SEL {}", n.is_some()));
49    tree.on_expand(|_, open| println!("TREE_EXPAND {open}"));
50    a.set_context_menu({
51        let p = PopupMenu::new();
52        MenuItem::new(p, "Ctx item").on_click(|| println!("CTXMENU"));
53        p
54    });
55    btn.on_click(|| println!("CLICK"));
56    txt.on_change(|t| println!("TEXT {t}"));
57    chk.on_toggle(|b| println!("TOGGLE {b}"));
58    list.on_select(|i| println!("SELECT {i:?}"));
59    slider.on_change(|v| println!("VALUE {v:.0}"));
60    combo.on_select(|i| println!("COMBO {i:?}"));
61    a.on_resize(|w, h| println!("RESIZED {w} {h}"));
62    a.on_close(|| {
63        println!("CLOSE_REQUESTED");
64        true
65    });
66
67    let b = Window::new("smoke-b");
68    let bar = MenuBar::new(b);
69    let file = Menu::new(bar, "File");
70    let item = MenuItem::new(file, "Quit");
71    item.set_accel("Ctrl+Q");
72    item.on_click(|| {
73        println!("MENU_QUIT");
74        App::quit();
75    });
76    let ck = CheckMenuItem::new(file, "Flag");
77    ck.on_toggle(|v| println!("MENUTOGGLE {v}"));
78    Label::new(b, "second window");
79
80    a.show();
81    b.show();
82    println!(
83        "HANDLE {}",
84        a.native_handle().is_some() && btn.native_handle().is_some()
85    );
86    let _t = Timer::once(600, move || {
87        for (n, w) in [
88            ("btn", btn.bounds()),
89            ("txt", txt.bounds()),
90            ("chk", chk.bounds()),
91            ("list", list.bounds()),
92            ("slider", slider.bounds()),
93            ("combo", combo.bounds()),
94            ("dlg", dlg.bounds()),
95            ("rb", rb.bounds()),
96            ("table", table.bounds()),
97            ("tree", tree.bounds()),
98        ] {
99            println!("BOUNDS {n} {} {} {} {}", w.x, w.y, w.w, w.h);
100        }
101        println!("READY");
102    });
103    // CI: RUNGUI_EXIT_AFTER_MS=n quits the loop after n ms so a startup crash shows up as a failure.
104    let _exit = std::env::var("RUNGUI_EXIT_AFTER_MS")
105        .ok()
106        .and_then(|v| v.parse::<u32>().ok())
107        .map(|ms| Timer::once(ms, App::quit));
108    app.run();
109    println!("BYE");
110}