smol changes

This commit is contained in:
2024-11-08 21:09:40 +01:00
parent c65bd68be5
commit 6d5abf2774
5 changed files with 105 additions and 35 deletions
+55 -3
View File
@@ -21,13 +21,58 @@ pub enum EventResponse {
/// Indicates no response.
None,
/// Issues a single command.
Command(Command),
/// Issues a list of commands.
CommandList(Vec<Command>),
// Update the current element
/// Update the current element
UpdateSelf(Box<dyn crate::Element>),
// Update a element by a id
/// Update a element by a id
UpdateElementById(String, Box<dyn crate::Element>),
/// Multiple Responses
/// # DO NOT USE MANUALLY
Mul(Vec<EventResponse>),
}
impl EventResponse {
pub fn add_command(&mut self, command: Command) {
match self {
EventResponse::CommandList(commands) => {
commands.push(command);
}
EventResponse::Mul(responses) => {
for response in responses {
match response {
EventResponse::CommandList(commands) => {
commands.push(command.clone());
return;
}
_ => {}
}
}
self.add_response(EventResponse::CommandList(vec![command.clone()]));
}
_ => {}
}
}
pub fn update_self(&mut self, elem: Box<dyn crate::Element>) {
self.add_response(Self::UpdateSelf(elem));
}
pub fn update_element_by_id(&mut self, id: &str, elem: Box<dyn crate::Element>) {
self.add_response(Self::UpdateElementById(id.to_string(), elem));
}
pub fn add_response(&mut self, response: EventResponse) {
match self {
EventResponse::Mul(responses) => {
responses.insert(0, response);
}
_ => *self = EventResponse::Mul(vec![response, self.clone()]),
}
}
pub fn execute(&self) -> Vec<EventResponse> {
return match self {
EventResponse::Mul(v) => v.clone(),
_ => vec![self.clone()],
};
}
}
/// Enum representing the possible responses from an element update.
@@ -36,3 +81,10 @@ pub enum Event {
Key(Key),
State(usize),
}
/// A handler is a function that runs on a event of a element
///
/// # Params
/// - &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)>>;