pub fn unbind_event_handler(handler: &EventHandler)Expand description
Free all associated callbacks with the event handler.
This function will panic if the handler was already freed.
Examples found in repository?
examples/message_bank.rs (line 64)
61 fn exit(&self) {
62 let handlers = self.handlers.borrow();
63 for handler in handlers.iter() {
64 nwg::unbind_event_handler(&handler);
65 }
66
67 nwg::stop_thread_dispatch();
68 }
69}
70
71//
72// ALL of this stuff is handled by native-windows-derive
73//
74mod message_bank_ui {
75 use super::*;
76 use native_windows_gui2 as nwg;
77 use std::cell::RefCell;
78 use std::ops::Deref;
79 use std::rc::Rc;
80
81 pub struct MessageBankUi {
82 inner: Rc<MessageBank>,
83 default_handler: RefCell<Vec<nwg::EventHandler>>,
84 }
85
86 impl nwg::NativeUi<MessageBankUi> for MessageBank {
87 fn build_ui(mut data: MessageBank) -> Result<MessageBankUi, nwg::NwgError> {
88 use nwg::Event as E;
89
90 // Controls
91 nwg::Window::builder()
92 .flags(nwg::WindowFlags::MAIN_WINDOW | nwg::WindowFlags::VISIBLE)
93 .size((400, 300))
94 .position((800, 300))
95 .title("My message bank")
96 .build(&mut data.window)?;
97
98 nwg::TextInput::builder()
99 .text("Hello World!")
100 .focus(true)
101 .parent(&data.window)
102 .build(&mut data.message_content)?;
103
104 nwg::Button::builder()
105 .text("Save")
106 .parent(&data.window)
107 .build(&mut data.add_message_btn)?;
108
109 nwg::TextInput::builder()
110 .text("Title")
111 .parent(&data.window)
112 .build(&mut data.message_title)?;
113
114 // Wrap-up
115 let ui = MessageBankUi {
116 inner: Rc::new(data),
117 default_handler: Default::default(),
118 };
119
120 // Events
121 let window_handles = [&ui.window.handle];
122
123 for handle in window_handles.iter() {
124 let evt_ui = Rc::downgrade(&ui.inner);
125 let handle_events = move |evt, _evt_data, handle| {
126 if let Some(evt_ui) = evt_ui.upgrade() {
127 match evt {
128 E::OnButtonClick => {
129 if &handle == &evt_ui.add_message_btn {
130 MessageBank::add_message(&evt_ui);
131 }
132 }
133 E::OnWindowClose => {
134 if &handle == &evt_ui.window {
135 MessageBank::exit(&evt_ui);
136 }
137 }
138 _ => {}
139 }
140 }
141 };
142
143 ui.default_handler
144 .borrow_mut()
145 .push(nwg::full_bind_event_handler(handle, handle_events));
146 }
147
148 // Layout
149 nwg::GridLayout::builder()
150 .parent(&ui.window)
151 .max_row(Some(6))
152 .child(0, 0, &ui.add_message_btn)
153 .child_item(nwg::GridLayoutItem::new(&ui.message_title, 1, 0, 2, 1))
154 .child_item(nwg::GridLayoutItem::new(&ui.message_content, 3, 0, 3, 1))
155 .build(&ui.layout)?;
156
157 return Ok(ui);
158 }
159 }
160
161 impl Drop for MessageBankUi {
162 /// To make sure that everything is freed without issues, the default handler must be unbound.
163 fn drop(&mut self) {
164 let mut handlers = self.default_handler.borrow_mut();
165 for handler in handlers.drain(0..) {
166 nwg::unbind_event_handler(&handler);
167 }
168 }More examples
Additional examples can be found in: