partial_simple/
partial_simple.rs

1extern crate native_windows_gui as nwg;
2
3use nwg::{NativeUi, PartialUi};
4use std::{rc::Rc, cell::RefCell, ops::Deref};
5
6#[derive(Default)]
7pub struct MainUi {
8    window: nwg::Window,
9    form: SubmitForm,
10}
11
12#[derive(Default)]
13pub struct SubmitForm {
14    form_data: String,
15    layout: nwg::GridLayout,
16    value: nwg::TextInput,
17    sumbit_button: nwg::Button
18}
19
20pub struct MainUiWrapper {
21    inner: Rc<MainUi>,
22    default_handler: RefCell<Vec<nwg::EventHandler>>
23}
24
25impl nwg::NativeUi<MainUiWrapper> for MainUi {
26    fn build_ui(mut data: MainUi) -> Result<MainUiWrapper, nwg::NwgError> {
27        nwg::Window::builder()
28            .size((500, 200))
29            .position((500, 300))
30            .title("My Form")
31            .build(&mut data.window)?;
32
33        // !!! Partials controls setup !!!
34        SubmitForm::build_partial(&mut data.form, Some(&data.window))?;
35
36        let ui = MainUiWrapper {
37            inner: Rc::new(data),
38            default_handler: Default::default(),
39        };
40
41        // !!! Partials Event Binding !!!
42        let mut window_handles = vec![&ui.window.handle];
43        window_handles.append(&mut ui.form.handles());
44
45        for handle in window_handles.iter() {
46            let evt_ui = Rc::downgrade(&ui.inner);
47            let handle_events = move |evt, evt_data, handle| {
48                use nwg::Event as E;
49
50                if let Some(ui) = evt_ui.upgrade() {
51
52                    // !!! Partials Event Dispatch !!!
53                    ui.form.process_event(evt, &evt_data, handle);
54
55                    match evt {
56                        E::OnButtonClick => 
57                            if &handle == &ui.form.sumbit_button {
58                                println!("SAVING!");
59                            },
60                        E::OnWindowClose => 
61                            if &handle == &ui.window {
62                                nwg::stop_thread_dispatch();
63                            },
64                        _ => {}
65                    }
66                }
67            };
68
69            ui.default_handler.borrow_mut().push(
70                nwg::full_bind_event_handler(handle, handle_events)
71            );
72        }
73
74        return Ok(ui);
75    }
76}
77
78impl Deref for MainUiWrapper {
79    type Target = MainUi;
80
81    fn deref(&self) -> &MainUi {
82        &self.inner
83    }
84}
85
86
87impl PartialUi for SubmitForm {
88
89    fn build_partial<W: Into<nwg::ControlHandle>>(data: &mut SubmitForm, parent: Option<W>) -> Result<(), nwg::NwgError> {
90        let parent = parent.unwrap().into();
91
92        nwg::TextInput::builder()
93            .text(&data.form_data)
94            .parent(&parent)
95            .build(&mut data.value)?;
96
97        nwg::Button::builder()
98            .text("Save")
99            .parent(&parent)
100            .build(&mut data.sumbit_button)?;
101
102        nwg::GridLayout::builder()
103            .child(0, 0, &data.value)
104            .child(0, 1, &data.sumbit_button)
105            .parent(&parent)
106            .build(&data.layout)?;
107
108        Ok(())
109    }
110
111    fn process_event<'a>(&self, evt: nwg::Event, _evt_data: &nwg::EventData, handle: nwg::ControlHandle) {
112        use nwg::Event as E;
113
114        match evt {
115            E::OnButtonClick => 
116                if &handle == &self.sumbit_button {
117                    println!("PARTIAL EVENT!");
118                },
119            _ => {}
120        }
121    }
122
123    fn handles(&self) -> Vec<&nwg::ControlHandle> {
124        // No top level window in this partial
125        Vec::new()
126    }
127}
128
129fn main() {
130    nwg::init().expect("Failed to init Native Windows GUI");
131    nwg::Font::set_global_family("Segoe UI").expect("Failed to set default font");
132
133    let state = MainUi {
134        form: SubmitForm {
135            form_data: "Default Value".to_string(),
136            ..Default::default()
137        },
138        ..Default::default()
139    };
140
141    let _ui = MainUi::build_ui(state).expect("Failed to build UI");
142    nwg::dispatch_thread_events();
143}
144