From e0ce761d8e07bf15b830e2fbdcf6c97be59bbb53 Mon Sep 17 00:00:00 2001 From: Leo dev Date: Sat, 9 Nov 2024 21:25:18 +0100 Subject: [PATCH] new_handler function --- src/event.rs | 46 +++++++++++++++++++++++++++++++++++++++++++++- src/test/mod.rs | 13 ++++--------- src/ui/elements.rs | 2 +- 3 files changed, 50 insertions(+), 11 deletions(-) diff --git a/src/event.rs b/src/event.rs index f08d84f..cc78c4e 100644 --- a/src/event.rs +++ b/src/event.rs @@ -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 = std::sync::Arc>; + +/// Creates a new `Handler` from a given function closure. +/// +/// This function takes a closure and wraps it in an `Arc>`, 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`, which is an `Arc>`. +/// +/// 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(handler_fn: F) -> Handler +where + F: FnMut(&mut T, &mut EventResponse) + 'static, +{ + arc!(handler_fn) +} diff --git a/src/test/mod.rs b/src/test/mod.rs index 08b89f9..e162b4a 100644 --- a/src/test/mod.rs +++ b/src/test/mod.rs @@ -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 { - 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" } } } diff --git a/src/ui/elements.rs b/src/ui/elements.rs index 3f79904..758f306 100644 --- a/src/ui/elements.rs +++ b/src/ui/elements.rs @@ -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,