basic_barebone/
basic_barebone.rs

1/**
2    A very simple application that show your name in a message box.
3
4    This demo shows how to use NWG without the NativeUi trait boilerplate.
5    Note that this way of doing things is alot less extensible and cannot make use of native windows derive.
6
7    See `basic` for the NativeUi version and `basic_d` for the derive version
8*/
9extern crate native_windows_gui as nwg;
10use std::rc::Rc;
11
12fn main() {
13    nwg::init().expect("Failed to init Native Windows GUI");
14    nwg::Font::set_global_family("Segoe UI").expect("Failed to set default font");
15
16    let mut window = Default::default();
17    let mut name_edit = Default::default();
18    let mut hello_button = Default::default();
19    let layout = Default::default();
20
21    nwg::Window::builder()
22        .size((300, 115))
23        .position((300, 300))
24        .title("Basic example")
25        .build(&mut window)
26        .unwrap();
27
28    nwg::TextInput::builder()
29        .text("Heisenberg")
30        .focus(true)
31        .parent(&window)
32        .build(&mut name_edit)
33        .unwrap();
34
35    nwg::Button::builder()
36        .text("Say my name")
37        .parent(&window)
38        .build(&mut hello_button)
39        .unwrap();
40
41    nwg::GridLayout::builder()
42        .parent(&window)
43        .spacing(1)
44        .child(0, 0, &name_edit)
45        .child_item(nwg::GridLayoutItem::new(&hello_button, 0, 1, 1, 2))
46        .build(&layout)
47        .unwrap();
48
49    let window = Rc::new(window);
50    let events_window = window.clone();
51
52    let handler = nwg::full_bind_event_handler(&window.handle, move |evt, _evt_data, handle| {
53        use nwg::Event as E;
54
55        match evt {
56            E::OnWindowClose => 
57                if &handle == &events_window as &nwg::Window {
58                    nwg::modal_info_message(&events_window.handle, "Goodbye", &format!("Goodbye {}", name_edit.text()));
59                    nwg::stop_thread_dispatch();
60                },
61            E::OnButtonClick => 
62                if &handle == &hello_button {
63                    nwg::modal_info_message(&events_window.handle, "Hello", &format!("Hello {}", name_edit.text()));
64                },
65            _ => {}
66        }
67    });
68
69    nwg::dispatch_thread_events();
70    nwg::unbind_event_handler(&handler);
71}