Skip to main content

cranpose_ui/
unhandled_keys.rs

1use std::{cell::RefCell, rc::Rc};
2
3use cranpose_core::{DisposableEffect, remember};
4use cranpose_macros::composable;
5
6use crate::KeyEvent;
7
8pub(crate) type KeyHandler = Rc<RefCell<Rc<dyn Fn(&KeyEvent) -> bool>>>;
9
10/// Hands `handler` every key event no control took: none was focused, a
11/// focused control let the key pass, and no text field is taking typing.
12///
13/// This is where an application puts the shortcuts that work anywhere in it,
14/// a media player's play and stop keys, for instance. The handler returns
15/// `true` for a key it used. While several are composed, the one composed
16/// last is asked first, and a key goes no further once one takes it.
17///
18/// ```ignore
19/// UnhandledKeyEvents(move |event| {
20///     if event.event_type != KeyEventType::KeyDown {
21///         return false;
22///     }
23///     match event.key_code {
24///         KeyCode::X => { play(); true }
25///         KeyCode::V => { stop(); true }
26///         _ => false,
27///     }
28/// });
29/// ```
30#[composable]
31pub fn UnhandledKeyEvents(handler: impl Fn(&KeyEvent) -> bool + 'static) {
32    let latest: KeyHandler = remember(|| {
33        let placeholder: Rc<dyn Fn(&KeyEvent) -> bool> = Rc::new(|_: &KeyEvent| false);
34        Rc::new(RefCell::new(placeholder))
35    })
36    .with(Rc::clone);
37    *latest.borrow_mut() = Rc::new(handler);
38    DisposableEffect((), move |scope| {
39        let id = crate::thread_state::cells(|cells| {
40            let id = cells.last_unhandled_key_handler_id.get() + 1;
41            cells.last_unhandled_key_handler_id.set(id);
42            id
43        });
44        crate::thread_state::with(|state| {
45            state.unhandled_key_handlers.borrow_mut().push((id, latest));
46        });
47        scope.on_dispose(move || {
48            crate::thread_state::try_with(|state| {
49                state
50                    .unhandled_key_handlers
51                    .borrow_mut()
52                    .retain(|(held, _)| *held != id);
53            });
54        })
55    });
56}
57
58/// Offers `event` to the [`UnhandledKeyEvents`] handlers, the one composed
59/// last first, and says whether one of them took it. The platform shells
60/// call this with each key the focused controls and text fields let pass.
61pub fn dispatch_unhandled_key_event(event: &KeyEvent) -> bool {
62    let handlers: Vec<KeyHandler> = crate::thread_state::with(|state| {
63        state
64            .unhandled_key_handlers
65            .borrow()
66            .iter()
67            .rev()
68            .map(|(_, handler)| Rc::clone(handler))
69            .collect()
70    });
71    handlers.into_iter().any(|handler| {
72        let current = Rc::clone(&handler.borrow());
73        current(event)
74    })
75}