allow event bubbling

This commit is contained in:
🪞👃🪞 2024-06-20 19:32:24 +03:00
parent f77c84a99c
commit 225c686db9
10 changed files with 90 additions and 100 deletions

View file

@ -88,25 +88,28 @@ pub fn run (device: impl Device + Send + Sync + 'static) -> Result<(), Box<dyn E
}
pub trait Device: Send + Sync {
fn handle (&mut self, _event: &AppEvent) -> Usually<()> {
Ok(())
// Returns Ok(true) if the device handled the event.
// This is the mechanism which allows nesting of components;.
fn handle (&mut self, _event: &AppEvent) -> Usually<bool> {
Ok(false)
}
// Returns space used by component.
// This is insufficient but for the most basic dynamic layout algorithms.
fn render (&self, _buffer: &mut Buffer, _area: Rect) -> Usually<Rect> {
Ok(Rect { x: 0, y: 0, width: 0, height: 0 })
}
fn process (&mut self, _client: Client, _scope: ProcessScope) {}
}
pub struct DynamicDevice<T> {
pub state: Arc<Mutex<T>>,
pub render: Mutex<Box<dyn FnMut(&T, &mut Buffer, Rect)->Usually<Rect> + Send>>,
pub handle: Arc<Mutex<Box<dyn FnMut(&mut T, &AppEvent)->Usually<()> + Send>>>,
pub handle: Arc<Mutex<Box<dyn FnMut(&mut T, &AppEvent)->Usually<bool> + Send>>>,
pub process: Arc<Mutex<Box<dyn FnMut(&mut T, &Client, &ProcessScope)->Control + Send>>>,
client: Option<DynamicAsyncClient>
}
impl<T: Send + Sync> Device for DynamicDevice<T> {
fn handle (&mut self, event: &AppEvent) -> Usually<()> {
fn handle (&mut self, event: &AppEvent) -> Usually<bool> {
self.handle.lock().unwrap()(&mut *self.state.lock().unwrap(), event)
}
fn render (&self, buf: &mut Buffer, area: Rect) -> Usually<Rect> {
@ -120,9 +123,9 @@ type DynamicProcessHandler = ClosureProcessHandler<BoxedProcessHandler>;
impl<T: Send + Sync + 'static> DynamicDevice<T> {
fn new <'a, R, H, P> (render: R, handle: H, process: P, state: T) -> Self where
R: FnMut(&T, &mut Buffer, Rect)->Usually<Rect> + Send + 'static,
H: FnMut(&mut T, &AppEvent) -> Result<(), Box<dyn Error>> + Send + 'static,
P: FnMut(&mut T, &Client, &ProcessScope)->Control + Send + 'static,
R: FnMut(&T, &mut Buffer, Rect) -> Usually<Rect> + Send + 'static,
H: FnMut(&mut T, &AppEvent) -> Usually<bool> + Send + 'static,
P: FnMut(&mut T, &Client, &ProcessScope) -> Control + Send + 'static,
{
Self {
state: Arc::new(Mutex::new(state)),
@ -142,7 +145,7 @@ impl<T: Send + Sync + 'static> DynamicDevice<T> {
move|event|{
let mut state = state.lock().unwrap();
let mut handle = handle.lock().unwrap();
handle(&mut state, &event).unwrap()
handle(&mut state, &event).unwrap();
}
}) as Box<dyn Fn(AppEvent) + Send + Sync>), ClosureProcessHandler::new(Box::new({
let state = self.state.clone();