Function bind_event_handler

Source
pub fn bind_event_handler<F>(
    handle: &ControlHandle,
    parent_handle: &ControlHandle,
    f: F,
) -> EventHandler
where F: Fn(Event, EventData, ControlHandle) + 'static,
Expand description

Hook the window subclass with the default event dispatcher. The hook is applied to the control and its parent. All common controls send their events to their parent.

Arguments: - handle: Handle to the main control to hook - parent_handle: Parent to the main control. - f: User event callback

Returns a EventHandler that can be passed to unbind_event_handler to remove the callbacks.

Examples found in repository?
examples/message_bank.rs (lines 46-55)
26    fn add_message(&self) {
27        let title = self.message_title.text();
28        let content = self.message_content.text();
29
30        let mut new_button = Default::default();
31        nwg::Button::builder()
32            .text(&title)
33            .parent(&self.window)
34            .build(&mut new_button)
35            .expect("Failed to build button");
36
37        let mut buttons = self.buttons.borrow_mut();
38        let mut handlers = self.handlers.borrow_mut();
39
40        let blen = buttons.len() as u32;
41        let (x, y) = (blen % 6, blen / 6);
42        self.layout.add_child(x, y+1, &new_button);
43
44        // You can share controls handle with events handlers
45        let new_button_handle = new_button.handle;
46        let handler = nwg::bind_event_handler(&new_button.handle, &self.window.handle, move |evt, _evt_data, handle| {
47            match evt {
48                nwg::Event::OnButtonClick => {
49                    if handle == new_button_handle {
50                        nwg::simple_message(&title, &content);
51                    }
52                },
53                _ => {}
54            }
55        });
56
57        buttons.push(new_button);
58        handlers.push(handler);
59    }