PartialUi

Trait PartialUi 

Source
pub trait PartialUi {
    // Required method
    fn build_partial<W: Into<ControlHandle>>(
        data: &mut Self,
        parent: Option<W>,
    ) -> Result<(), NwgError>;

    // Provided methods
    fn process_event(
        &self,
        _evt: Event,
        _evt_data: &EventData,
        _handle: ControlHandle,
    ) { ... }
    fn handles<'a>(&'a self) -> Vec<&'a ControlHandle> { ... }
}
Expand description

A structure that implements this trait is considered a GUI structure. The structure will hold GUI components and possibly user data.

A structure that implements PartialUi must be part of another UI structure and cannot be used as it is. It will most likely be used as the struct member of another struct that implements NativeUi.

The goal of NativeUi and PartialUi is to provide a common way to define NWG applications. Native-windows-derive can automatically implement this trait.

For an example on how to implement this trait, see the Small application layout section in the NWG documentation.

Required Methods§

Source

fn build_partial<W: Into<ControlHandle>>( data: &mut Self, parent: Option<W>, ) -> Result<(), NwgError>

Should initialize the GUI components. Similar to NativeUi::build_ui except it doesn’t handle event binding.

Parameters:

  • data: A reference to the struct data from the parent struct
  • parent: An optional reference to the parent UI control. If this is defined, the ui controls of the partial should be children of this value.

Provided Methods§

Source

fn process_event( &self, _evt: Event, _evt_data: &EventData, _handle: ControlHandle, )

Should process the events of the partial. This method will probably be called from an event handler bound in the parent GUI structure.

Parameters:

  • base: A reference to the parent struct data
  • evt: The event raised
  • evt_data: The data of the event raised
  • handle: Handle of the control that raised the event
Examples found in repository?
examples/partial_simple.rs (line 53)
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    }
More examples
Hide additional examples
examples/partials.rs (line 182)
132        fn build_ui(mut data: PartialDemo) -> Result<Rc<PartialDemoUi>, nwg::NwgError> {
133            use nwg::Event as E;
134            
135            // Controls
136            nwg::Window::builder()
137                .size((500, 400))
138                .position((300, 300))
139                .title("Many UI")
140                .build(&mut data.window)?;
141
142            nwg::ListBox::builder()
143                .collection(vec!["People", "Animals", "Food"])
144                .focus(true)
145                .parent(&data.window)
146                .build(&mut data.menu)?;
147
148            nwg::Frame::builder()
149                .parent(&data.window)
150                .build(&mut data.frame1)?;
151
152            nwg::Frame::builder()
153                .flags(nwg::FrameFlags::BORDER)
154                .parent(&data.window)
155                .build(&mut data.frame2)?;
156
157            nwg::Frame::builder()
158                .flags(nwg::FrameFlags::BORDER)
159                .parent(&data.window)
160                .build(&mut data.frame3)?;
161
162            // Partials
163            PeopleUi::build_partial(&mut data.people_ui, Some(&data.frame1))?;
164            AnimalUi::build_partial(&mut data.animal_ui, Some(&data.frame2))?;
165            FoodUi::build_partial(&mut data.food_ui, Some(&data.frame3))?;
166
167            // Wrap-up
168            let ui = Rc::new(PartialDemoUi {
169                inner: data,
170                default_handler: Default::default()
171            });
172
173            // Events
174            let mut window_handles = vec![&ui.window.handle];
175            window_handles.append(&mut ui.people_ui.handles());
176            window_handles.append(&mut ui.animal_ui.handles());
177            window_handles.append(&mut ui.food_ui.handles());
178
179            for handle in window_handles.iter() {
180                let evt_ui = ui.clone();
181                let handle_events = move |evt, evt_data, handle| {
182                    evt_ui.people_ui.process_event(evt, &evt_data, handle);
183                    evt_ui.animal_ui.process_event(evt, &evt_data, handle);
184                    evt_ui.food_ui.process_event(evt, &evt_data, handle);
185                    
186                    match evt {
187                        E::OnListBoxSelect => 
188                            if &handle == &evt_ui.menu {
189                                PartialDemo::change_interface(&evt_ui.inner);
190                            },
191                        E::OnWindowClose => 
192                            if &handle == &evt_ui.window {
193                                PartialDemo::exit(&evt_ui.inner);
194                            },
195                        E::OnButtonClick => 
196                            if &handle == &evt_ui.people_ui.save_btn || &handle == &evt_ui.animal_ui.save_btn ||&handle == &evt_ui.food_ui.save_btn  {
197                                PartialDemo::save(&evt_ui.inner);
198                            },
199                        _ => {}
200                    }
201                };
202
203                ui.default_handler.borrow_mut().push(
204                    nwg::full_bind_event_handler(handle, handle_events)
205                );
206            }
207
208            // Layout
209            use nwg::stretch::{geometry::Size, style::Dimension as D};
210
211           nwg::FlexboxLayout::builder()
212                .parent(&ui.window)
213                .child(&ui.menu)
214                    .child_size(Size { width: D::Percent(0.3), height: D::Auto })
215                .child(&ui.frame1)
216                    .child_size(Size { width: D::Percent(1.0), height: D::Auto })
217                .build(&ui.layout)?;
218            
219            return Ok(ui);
220        }
Source

fn handles<'a>(&'a self) -> Vec<&'a ControlHandle>

Should return the handles of the top level parent controls (such as Windows). Those handle should be used to bind the default events handler.

Examples found in repository?
examples/partial_simple.rs (line 43)
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    }
More examples
Hide additional examples
examples/partials.rs (line 175)
132        fn build_ui(mut data: PartialDemo) -> Result<Rc<PartialDemoUi>, nwg::NwgError> {
133            use nwg::Event as E;
134            
135            // Controls
136            nwg::Window::builder()
137                .size((500, 400))
138                .position((300, 300))
139                .title("Many UI")
140                .build(&mut data.window)?;
141
142            nwg::ListBox::builder()
143                .collection(vec!["People", "Animals", "Food"])
144                .focus(true)
145                .parent(&data.window)
146                .build(&mut data.menu)?;
147
148            nwg::Frame::builder()
149                .parent(&data.window)
150                .build(&mut data.frame1)?;
151
152            nwg::Frame::builder()
153                .flags(nwg::FrameFlags::BORDER)
154                .parent(&data.window)
155                .build(&mut data.frame2)?;
156
157            nwg::Frame::builder()
158                .flags(nwg::FrameFlags::BORDER)
159                .parent(&data.window)
160                .build(&mut data.frame3)?;
161
162            // Partials
163            PeopleUi::build_partial(&mut data.people_ui, Some(&data.frame1))?;
164            AnimalUi::build_partial(&mut data.animal_ui, Some(&data.frame2))?;
165            FoodUi::build_partial(&mut data.food_ui, Some(&data.frame3))?;
166
167            // Wrap-up
168            let ui = Rc::new(PartialDemoUi {
169                inner: data,
170                default_handler: Default::default()
171            });
172
173            // Events
174            let mut window_handles = vec![&ui.window.handle];
175            window_handles.append(&mut ui.people_ui.handles());
176            window_handles.append(&mut ui.animal_ui.handles());
177            window_handles.append(&mut ui.food_ui.handles());
178
179            for handle in window_handles.iter() {
180                let evt_ui = ui.clone();
181                let handle_events = move |evt, evt_data, handle| {
182                    evt_ui.people_ui.process_event(evt, &evt_data, handle);
183                    evt_ui.animal_ui.process_event(evt, &evt_data, handle);
184                    evt_ui.food_ui.process_event(evt, &evt_data, handle);
185                    
186                    match evt {
187                        E::OnListBoxSelect => 
188                            if &handle == &evt_ui.menu {
189                                PartialDemo::change_interface(&evt_ui.inner);
190                            },
191                        E::OnWindowClose => 
192                            if &handle == &evt_ui.window {
193                                PartialDemo::exit(&evt_ui.inner);
194                            },
195                        E::OnButtonClick => 
196                            if &handle == &evt_ui.people_ui.save_btn || &handle == &evt_ui.animal_ui.save_btn ||&handle == &evt_ui.food_ui.save_btn  {
197                                PartialDemo::save(&evt_ui.inner);
198                            },
199                        _ => {}
200                    }
201                };
202
203                ui.default_handler.borrow_mut().push(
204                    nwg::full_bind_event_handler(handle, handle_events)
205                );
206            }
207
208            // Layout
209            use nwg::stretch::{geometry::Size, style::Dimension as D};
210
211           nwg::FlexboxLayout::builder()
212                .parent(&ui.window)
213                .child(&ui.menu)
214                    .child_size(Size { width: D::Percent(0.3), height: D::Auto })
215                .child(&ui.frame1)
216                    .child_size(Size { width: D::Percent(1.0), height: D::Auto })
217                .build(&ui.layout)?;
218            
219            return Ok(ui);
220        }

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§