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§
Sourcefn build_partial<W: Into<ControlHandle>>(
data: &mut Self,
parent: Option<W>,
) -> Result<(), NwgError>
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 structparent: 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§
Sourcefn process_event(
&self,
_evt: Event,
_evt_data: &EventData,
_handle: ControlHandle,
)
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 dataevt: The event raisedevt_data: The data of the event raisedhandle: Handle of the control that raised the event
Examples found in repository?
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 // !!! Partials Event Dispatch !!!
52 ui.form.process_event(evt, &evt_data, handle);
53
54 match evt {
55 E::OnButtonClick => {
56 if &handle == &ui.form.sumbit_button {
57 println!("SAVING!");
58 }
59 }
60 E::OnWindowClose => {
61 if &handle == &ui.window {
62 nwg::stop_thread_dispatch();
63 }
64 }
65 _ => {}
66 }
67 }
68 };
69
70 ui.default_handler
71 .borrow_mut()
72 .push(nwg::full_bind_event_handler(handle, handle_events));
73 }
74
75 return Ok(ui);
76 }More examples
141 fn build_ui(mut data: PartialDemo) -> Result<Rc<PartialDemoUi>, nwg::NwgError> {
142 use nwg::Event as E;
143
144 // Controls
145 nwg::Window::builder()
146 .size((500, 400))
147 .position((300, 300))
148 .title("Many UI")
149 .build(&mut data.window)?;
150
151 nwg::ListBox::builder()
152 .collection(vec!["People", "Animals", "Food"])
153 .focus(true)
154 .parent(&data.window)
155 .build(&mut data.menu)?;
156
157 nwg::Frame::builder()
158 .parent(&data.window)
159 .build(&mut data.frame1)?;
160
161 nwg::Frame::builder()
162 .flags(nwg::FrameFlags::BORDER)
163 .parent(&data.window)
164 .build(&mut data.frame2)?;
165
166 nwg::Frame::builder()
167 .flags(nwg::FrameFlags::BORDER)
168 .parent(&data.window)
169 .build(&mut data.frame3)?;
170
171 // Partials
172 PeopleUi::build_partial(&mut data.people_ui, Some(&data.frame1))?;
173 AnimalUi::build_partial(&mut data.animal_ui, Some(&data.frame2))?;
174 FoodUi::build_partial(&mut data.food_ui, Some(&data.frame3))?;
175
176 // Wrap-up
177 let ui = Rc::new(PartialDemoUi {
178 inner: data,
179 default_handler: Default::default(),
180 });
181
182 // Events
183 let mut window_handles = vec![&ui.window.handle];
184 window_handles.append(&mut ui.people_ui.handles());
185 window_handles.append(&mut ui.animal_ui.handles());
186 window_handles.append(&mut ui.food_ui.handles());
187
188 for handle in window_handles.iter() {
189 let evt_ui = ui.clone();
190 let handle_events = move |evt, evt_data, handle| {
191 evt_ui.people_ui.process_event(evt, &evt_data, handle);
192 evt_ui.animal_ui.process_event(evt, &evt_data, handle);
193 evt_ui.food_ui.process_event(evt, &evt_data, handle);
194
195 match evt {
196 E::OnListBoxSelect => {
197 if &handle == &evt_ui.menu {
198 PartialDemo::change_interface(&evt_ui.inner);
199 }
200 }
201 E::OnWindowClose => {
202 if &handle == &evt_ui.window {
203 PartialDemo::exit(&evt_ui.inner);
204 }
205 }
206 E::OnButtonClick => {
207 if &handle == &evt_ui.people_ui.save_btn
208 || &handle == &evt_ui.animal_ui.save_btn
209 || &handle == &evt_ui.food_ui.save_btn
210 {
211 PartialDemo::save(&evt_ui.inner);
212 }
213 }
214 _ => {}
215 }
216 };
217
218 ui.default_handler
219 .borrow_mut()
220 .push(nwg::full_bind_event_handler(handle, handle_events));
221 }
222
223 // Layout
224 use nwg::stretch::{geometry::Size, style::Dimension as D};
225
226 nwg::FlexboxLayout::builder()
227 .parent(&ui.window)
228 .child(&ui.menu)
229 .child_size(Size {
230 width: D::Percent(0.3),
231 height: D::Auto,
232 })
233 .child(&ui.frame1)
234 .child_size(Size {
235 width: D::Percent(1.0),
236 height: D::Auto,
237 })
238 .build(&ui.layout)?;
239
240 return Ok(ui);
241 }Sourcefn handles<'a>(&'a self) -> Vec<&'a ControlHandle>
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?
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 // !!! Partials Event Dispatch !!!
52 ui.form.process_event(evt, &evt_data, handle);
53
54 match evt {
55 E::OnButtonClick => {
56 if &handle == &ui.form.sumbit_button {
57 println!("SAVING!");
58 }
59 }
60 E::OnWindowClose => {
61 if &handle == &ui.window {
62 nwg::stop_thread_dispatch();
63 }
64 }
65 _ => {}
66 }
67 }
68 };
69
70 ui.default_handler
71 .borrow_mut()
72 .push(nwg::full_bind_event_handler(handle, handle_events));
73 }
74
75 return Ok(ui);
76 }More examples
141 fn build_ui(mut data: PartialDemo) -> Result<Rc<PartialDemoUi>, nwg::NwgError> {
142 use nwg::Event as E;
143
144 // Controls
145 nwg::Window::builder()
146 .size((500, 400))
147 .position((300, 300))
148 .title("Many UI")
149 .build(&mut data.window)?;
150
151 nwg::ListBox::builder()
152 .collection(vec!["People", "Animals", "Food"])
153 .focus(true)
154 .parent(&data.window)
155 .build(&mut data.menu)?;
156
157 nwg::Frame::builder()
158 .parent(&data.window)
159 .build(&mut data.frame1)?;
160
161 nwg::Frame::builder()
162 .flags(nwg::FrameFlags::BORDER)
163 .parent(&data.window)
164 .build(&mut data.frame2)?;
165
166 nwg::Frame::builder()
167 .flags(nwg::FrameFlags::BORDER)
168 .parent(&data.window)
169 .build(&mut data.frame3)?;
170
171 // Partials
172 PeopleUi::build_partial(&mut data.people_ui, Some(&data.frame1))?;
173 AnimalUi::build_partial(&mut data.animal_ui, Some(&data.frame2))?;
174 FoodUi::build_partial(&mut data.food_ui, Some(&data.frame3))?;
175
176 // Wrap-up
177 let ui = Rc::new(PartialDemoUi {
178 inner: data,
179 default_handler: Default::default(),
180 });
181
182 // Events
183 let mut window_handles = vec![&ui.window.handle];
184 window_handles.append(&mut ui.people_ui.handles());
185 window_handles.append(&mut ui.animal_ui.handles());
186 window_handles.append(&mut ui.food_ui.handles());
187
188 for handle in window_handles.iter() {
189 let evt_ui = ui.clone();
190 let handle_events = move |evt, evt_data, handle| {
191 evt_ui.people_ui.process_event(evt, &evt_data, handle);
192 evt_ui.animal_ui.process_event(evt, &evt_data, handle);
193 evt_ui.food_ui.process_event(evt, &evt_data, handle);
194
195 match evt {
196 E::OnListBoxSelect => {
197 if &handle == &evt_ui.menu {
198 PartialDemo::change_interface(&evt_ui.inner);
199 }
200 }
201 E::OnWindowClose => {
202 if &handle == &evt_ui.window {
203 PartialDemo::exit(&evt_ui.inner);
204 }
205 }
206 E::OnButtonClick => {
207 if &handle == &evt_ui.people_ui.save_btn
208 || &handle == &evt_ui.animal_ui.save_btn
209 || &handle == &evt_ui.food_ui.save_btn
210 {
211 PartialDemo::save(&evt_ui.inner);
212 }
213 }
214 _ => {}
215 }
216 };
217
218 ui.default_handler
219 .borrow_mut()
220 .push(nwg::full_bind_event_handler(handle, handle_events));
221 }
222
223 // Layout
224 use nwg::stretch::{geometry::Size, style::Dimension as D};
225
226 nwg::FlexboxLayout::builder()
227 .parent(&ui.window)
228 .child(&ui.menu)
229 .child_size(Size {
230 width: D::Percent(0.3),
231 height: D::Auto,
232 })
233 .child(&ui.frame1)
234 .child_size(Size {
235 width: D::Percent(1.0),
236 height: D::Auto,
237 })
238 .build(&ui.layout)?;
239
240 return Ok(ui);
241 }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.