diff --git a/src/event.rs b/src/event.rs index 981fc88..257f58a 100644 --- a/src/event.rs +++ b/src/event.rs @@ -1,7 +1,7 @@ use crate::key::Key; /// Enum defining commands that can be issued by an `Element`. -#[derive(Debug, Clone, PartialEq)] +#[derive(Clone)] pub enum Command { /// Renders the element with the specified state. Render(usize), @@ -14,7 +14,7 @@ pub enum Command { } /// Enum representing the possible responses from an element event. -#[derive(Debug, Clone, PartialEq)] +#[derive(Clone)] pub enum EventResponse { /// Indicates that the event is complete. Done, @@ -24,6 +24,10 @@ pub enum EventResponse { Command(Command), /// Issues a list of commands. CommandList(Vec), + // Update the current element + UpdateSelf(Box), + // Update a element by a id + UpdateElementById(String, Box), } /// Enum representing the possible responses from an element update. diff --git a/src/lib.rs b/src/lib.rs index 204c51e..3b910b8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -218,7 +218,7 @@ pub trait Element: DynClone { /// * `_height` - The height of the terminal window or parent element. fn update_data(&mut self, _width: usize, _height: usize); - fn get_id(&mut self) -> String; + fn get_id(&self) -> String; fn get_element_by_id(&mut self, id: &str) -> Option<&mut Box>; diff --git a/src/macros.rs b/src/macros.rs index 1b450da..4804712 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -119,7 +119,7 @@ macro_rules! element { } } - fn get_id(&mut self) -> String { + fn get_id(&self) -> String { self.id.to_string() } diff --git a/src/test/mod.rs b/src/test/mod.rs index cb4351a..c5d0d43 100644 --- a/src/test/mod.rs +++ b/src/test/mod.rs @@ -1,7 +1,11 @@ -use crate::{self as osui, rsx, ui::*, Element}; +use crate::{self as osui, arc, rsx, rsx_elem, ui::*, Element, EventResponse}; pub fn app() -> Box { rsx! { - button { "ok" } + button { on_click: arc!(move |btn: &mut Button| { + btn.event_response = EventResponse::UpdateElementById("my_id".to_string(), rsx_elem! { button { y: 2, id: "my_id", "updated!" } } ); + }), "click me" } + + button { id: "my_id", y: 2, "ok" } } } diff --git a/src/ui/elements.rs b/src/ui/elements.rs index f618d8d..8b77f92 100644 --- a/src/ui/elements.rs +++ b/src/ui/elements.rs @@ -37,10 +37,15 @@ element! { Button { /// A callback function executed when the button is clicked. use `arc!` to use function pub on_click: Arc>, + pub event_response: EventResponse, } defaults { on_click: Arc::new(Mutex::new(|_btn: &mut Button<'_>| {})), + event_response: command!( + Command::Render(2), + Command::Sleep(120) + ), } fn render(&self, state: usize) -> String { @@ -59,10 +64,7 @@ element! { (on_click)(&mut btn); drop(on_click); *self = btn; - return command!( - Command::Render(2), - Command::Sleep(120) - ); + return self.event_response.clone(); } } _ => {} @@ -108,7 +110,21 @@ element! { if let Some(direction) = self.keybinds.get(&k) { self.child = crate::closest_component(&self.children, self.child, direction.clone()); } else if let Some(child) = self.get_child() { - return child.event(event.clone()); + let res = child.event(event.clone()); + match res.clone() { + EventResponse::UpdateElementById(id, elem) => { + for old in &mut self.children { + if old.get_id() == id { + *old = elem.clone(); + } + } + } + EventResponse::UpdateSelf(elem) => { + *child = elem; + } + _ => {} + } + return res; } } _ => {}