added id's
This commit is contained in:
+6
-2
@@ -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<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.
|
||||
|
||||
+1
-1
@@ -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<dyn Element>>;
|
||||
|
||||
|
||||
+1
-1
@@ -119,7 +119,7 @@ macro_rules! element {
|
||||
}
|
||||
}
|
||||
|
||||
fn get_id(&mut self) -> String {
|
||||
fn get_id(&self) -> String {
|
||||
self.id.to_string()
|
||||
}
|
||||
|
||||
|
||||
+6
-2
@@ -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> {
|
||||
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
@@ -37,10 +37,15 @@ element! {
|
||||
Button {
|
||||
/// A callback function executed when the button is clicked. use `arc!` to use function
|
||||
pub on_click: Arc<Mutex<dyn FnMut(&mut Button)>>,
|
||||
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;
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
|
||||
Reference in New Issue
Block a user