Skip to content
Snippets Groups Projects
Commit 5a3c0db5 authored by MrGVSV's avatar MrGVSV
Browse files

Added convenience method for sending events

parent 0af32b00
No related branches found
No related tags found
No related merge requests found
...@@ -25,12 +25,12 @@ fn TextBoxExample(context: &mut KayakContext) { ...@@ -25,12 +25,12 @@ fn TextBoxExample(context: &mut KayakContext) {
}; };
let cloned_value = value.clone(); let cloned_value = value.clone();
let on_change = OnChange::new(move |event| { let on_change = OnChange::new_handler(move |event| {
cloned_value.set(event.value); cloned_value.set(event.value);
}); });
let cloned_value2 = value2.clone(); let cloned_value2 = value2.clone();
let on_change2 = OnChange::new(move |event| { let on_change2 = OnChange::new_handler(move |event| {
cloned_value2.set(event.value); cloned_value2.set(event.value);
}); });
......
...@@ -49,9 +49,17 @@ pub struct ChangeEvent<T> { ...@@ -49,9 +49,17 @@ pub struct ChangeEvent<T> {
pub struct OnChange<T>(pub Arc<RwLock<dyn FnMut(ChangeEvent<T>) + Send + Sync + 'static>>); pub struct OnChange<T>(pub Arc<RwLock<dyn FnMut(ChangeEvent<T>) + Send + Sync + 'static>>);
impl<T> OnChange<T> { impl<T> OnChange<T> {
/// Create a new handler for a [ChangeEvent]
pub fn new<F: FnMut(ChangeEvent<T>) + Send + Sync + 'static>(f: F) -> Self { pub fn new<F: FnMut(ChangeEvent<T>) + Send + Sync + 'static>(f: F) -> Self {
Self(Arc::new(RwLock::new(f))) Self(Arc::new(RwLock::new(f)))
} }
/// Send the given event to be handled by the current handler
pub fn send(&self, event: ChangeEvent<T>) {
if let Ok(mut on_change) = self.0.write() {
on_change(event);
}
}
} }
impl<T> PartialEq for OnChange<T> { impl<T> PartialEq for OnChange<T> {
......
...@@ -47,11 +47,9 @@ pub fn TextBox(value: String, on_change: Option<OnChange<String>>) { ...@@ -47,11 +47,9 @@ pub fn TextBox(value: String, on_change: Option<OnChange<String>>) {
current_value.push(c); current_value.push(c);
} }
if let Some(on_change) = cloned_on_change.as_ref() { if let Some(on_change) = cloned_on_change.as_ref() {
if let Ok(mut on_change) = on_change.0.write() { on_change.send(ChangeEvent {
on_change(ChangeEvent { value: current_value.clone(),
value: current_value.clone(), });
});
}
} }
} }
EventType::Focus => cloned_has_focus.set(Focus(true)), EventType::Focus => cloned_has_focus.set(Focus(true)),
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment