new_handler function
This commit is contained in:
+45
-1
@@ -1,4 +1,4 @@
|
||||
use crate::{key::Key, Element};
|
||||
use crate::{arc, key::Key, Element};
|
||||
|
||||
/// Enum defining commands that can be issued by an `Element`.
|
||||
#[derive(Clone)]
|
||||
@@ -89,3 +89,47 @@ pub enum Event {
|
||||
/// - &mut T: The element,
|
||||
/// - &mut EventResponse: The event response
|
||||
pub type Handler<T> = std::sync::Arc<std::sync::Mutex<dyn FnMut(&mut T, &mut EventResponse)>>;
|
||||
|
||||
/// Creates a new `Handler<T>` from a given function closure.
|
||||
///
|
||||
/// This function takes a closure and wraps it in an `Arc<Mutex<dyn FnMut>>`, allowing it to be
|
||||
/// used as a thread-safe, mutable handler that can be shared across threads. The handler function
|
||||
/// receives a mutable reference to a `T` type and a mutable reference to an `EventResponse`, allowing
|
||||
/// it to mutate both the state and response.
|
||||
///
|
||||
/// # Type Parameters
|
||||
///
|
||||
/// - `T`: The type of the data that the handler will operate on.
|
||||
/// - `F`: The type of the handler function, which must implement `FnMut(&mut T, &mut EventResponse) + 'static`.
|
||||
///
|
||||
/// # Parameters
|
||||
///
|
||||
/// - `handler_fn`: The closure or function that will serve as the handler, taking mutable references
|
||||
/// to both the `T` data and an `EventResponse`.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// Returns a `Handler<T>`, which is an `Arc<Mutex<dyn FnMut(&mut T, &mut EventResponse)>>`.
|
||||
///
|
||||
/// This type alias makes it easy to manage the handler in a multi-threaded context, allowing shared ownership
|
||||
/// with safe, mutable access to the underlying function.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```rust
|
||||
/// use osui::ui::button;
|
||||
/// rsx! {
|
||||
/// button {
|
||||
/// // Create a new handler and assign it to on_click
|
||||
/// on_click: new_handler(move |btn: &mut Button, _response| {
|
||||
/// btn.text = "Clicked".to_string();
|
||||
/// }
|
||||
/// ), "click me" }
|
||||
/// }
|
||||
/// ```
|
||||
pub fn new_handler<T, F>(handler_fn: F) -> Handler<T>
|
||||
where
|
||||
F: FnMut(&mut T, &mut EventResponse) + 'static,
|
||||
{
|
||||
arc!(handler_fn)
|
||||
}
|
||||
|
||||
+4
-9
@@ -1,14 +1,9 @@
|
||||
use crate::{self as osui, arc, rsx, rsx_elem, ui::*, Element, EventResponse};
|
||||
use crate::{self as osui, event::new_handler, rsx, ui::*, Element};
|
||||
|
||||
pub fn app() -> Box<dyn Element> {
|
||||
let mut something = String::from("!");
|
||||
rsx! {
|
||||
button { on_click: arc!(move |_btn: &mut Button, response: &mut EventResponse| {
|
||||
something += "!";
|
||||
response.update_element_by_id("my_id", rsx_elem! {
|
||||
text { id: "my_id", y: 2, "{something}" }
|
||||
});
|
||||
}), style.clicked_background: Color::Green, style.hover_background: Color::Magenta, "click me" }
|
||||
text { id: "my_id", y: 2, "!" }
|
||||
button { on_click: new_handler(move |btn: &mut Button, _response| {
|
||||
btn.text = "Clicked".to_string();
|
||||
}), "click me" }
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -5,7 +5,7 @@ use std::{
|
||||
|
||||
use crate::{
|
||||
command, create_frame, element,
|
||||
event::{Command, Event, Handler},
|
||||
event::{Command, Event, Handler, },
|
||||
execute_response,
|
||||
key::Key,
|
||||
render_to_frame,
|
||||
|
||||
Reference in New Issue
Block a user