added id's

This commit is contained in:
2024-11-08 16:41:07 +01:00
parent 2a43bf75db
commit c65bd68be5
5 changed files with 35 additions and 11 deletions
+6 -2
View File
@@ -1,7 +1,7 @@
use crate::key::Key; use crate::key::Key;
/// Enum defining commands that can be issued by an `Element`. /// Enum defining commands that can be issued by an `Element`.
#[derive(Debug, Clone, PartialEq)] #[derive(Clone)]
pub enum Command { pub enum Command {
/// Renders the element with the specified state. /// Renders the element with the specified state.
Render(usize), Render(usize),
@@ -14,7 +14,7 @@ pub enum Command {
} }
/// Enum representing the possible responses from an element event. /// Enum representing the possible responses from an element event.
#[derive(Debug, Clone, PartialEq)] #[derive(Clone)]
pub enum EventResponse { pub enum EventResponse {
/// Indicates that the event is complete. /// Indicates that the event is complete.
Done, Done,
@@ -24,6 +24,10 @@ pub enum EventResponse {
Command(Command), Command(Command),
/// Issues a list of commands. /// Issues a list of commands.
CommandList(Vec<Command>), CommandList(Vec<Command>),
// Update the current element
UpdateSelf(Box<dyn crate::Element>),
// Update a element by a id
UpdateElementById(String, Box<dyn crate::Element>),
} }
/// Enum representing the possible responses from an element update. /// Enum representing the possible responses from an element update.
+1 -1
View File
@@ -218,7 +218,7 @@ pub trait Element: DynClone {
/// * `_height` - The height of the terminal window or parent element. /// * `_height` - The height of the terminal window or parent element.
fn update_data(&mut self, _width: usize, _height: usize); 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<dyn Element>>; fn get_element_by_id(&mut self, id: &str) -> Option<&mut Box<dyn Element>>;
+1 -1
View File
@@ -119,7 +119,7 @@ macro_rules! element {
} }
} }
fn get_id(&mut self) -> String { fn get_id(&self) -> String {
self.id.to_string() self.id.to_string()
} }
+6 -2
View File
@@ -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<dyn Element> { pub fn app() -> Box<dyn Element> {
rsx! { 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" }
} }
} }
+21 -5
View File
@@ -37,10 +37,15 @@ element! {
Button { Button {
/// A callback function executed when the button is clicked. use `arc!` to use function /// A callback function executed when the button is clicked. use `arc!` to use function
pub on_click: Arc<Mutex<dyn FnMut(&mut Button)>>, pub on_click: Arc<Mutex<dyn FnMut(&mut Button)>>,
pub event_response: EventResponse,
} }
defaults { defaults {
on_click: Arc::new(Mutex::new(|_btn: &mut Button<'_>| {})), on_click: Arc::new(Mutex::new(|_btn: &mut Button<'_>| {})),
event_response: command!(
Command::Render(2),
Command::Sleep(120)
),
} }
fn render(&self, state: usize) -> String { fn render(&self, state: usize) -> String {
@@ -59,10 +64,7 @@ element! {
(on_click)(&mut btn); (on_click)(&mut btn);
drop(on_click); drop(on_click);
*self = btn; *self = btn;
return command!( return self.event_response.clone();
Command::Render(2),
Command::Sleep(120)
);
} }
} }
_ => {} _ => {}
@@ -108,7 +110,21 @@ element! {
if let Some(direction) = self.keybinds.get(&k) { if let Some(direction) = self.keybinds.get(&k) {
self.child = crate::closest_component(&self.children, self.child, direction.clone()); self.child = crate::closest_component(&self.children, self.child, direction.clone());
} else if let Some(child) = self.get_child() { } 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;
} }
} }
_ => {} _ => {}