Skip to main content

cranpose_ui/
interaction.rs

1#![expect(non_snake_case)]
2
3use std::{
4    cell::RefCell,
5    collections::HashSet,
6    hash::{Hash, Hasher},
7    rc::Rc,
8};
9
10use cranpose_core::{
11    MutableState, OwnedMutableState, RuntimeHandle, State, remember, with_current_composer,
12};
13use cranpose_foundation::{
14    DelegatableNode, InvalidationKind, ModifierNode, ModifierNodeContext, ModifierNodeElement,
15    NodeCapabilities, NodeState, PointerInputNode,
16};
17
18use crate::{
19    composable,
20    modifier::{Modifier, Point, PointerEvent, PointerEventKind, inspector_metadata},
21};
22
23#[derive(Clone, Copy)]
24pub struct MutableInteractionSource {
25    inner: MutableState<Rc<MutableInteractionSourceInner>>,
26}
27
28struct MutableInteractionSourceInner {
29    next_press_id: RefCell<u64>,
30    active_presses: RefCell<HashSet<u64>>,
31    pressed: OwnedMutableState<bool>,
32    last_interaction: OwnedMutableState<Option<Interaction>>,
33}
34
35#[derive(Clone, Copy, Debug, PartialEq)]
36pub enum Interaction {
37    Press(PressInteraction),
38}
39
40#[derive(Clone, Copy, Debug, PartialEq)]
41pub enum PressInteraction {
42    Press(PressInteractionPress),
43    Release(PressInteractionRelease),
44    Cancel(PressInteractionCancel),
45}
46
47#[derive(Clone, Copy, Debug, PartialEq)]
48pub struct PressInteractionPress {
49    id: u64,
50    pub press_position: Point,
51}
52
53#[derive(Clone, Copy, Debug, PartialEq)]
54pub struct PressInteractionRelease {
55    pub press: PressInteractionPress,
56}
57
58#[derive(Clone, Copy, Debug, PartialEq)]
59pub struct PressInteractionCancel {
60    pub press: PressInteractionPress,
61}
62
63impl MutableInteractionSource {
64    pub fn new() -> Self {
65        let runtime = with_current_composer(cranpose_core::Composer::runtime_handle);
66        Self::with_runtime(runtime)
67    }
68
69    pub fn with_runtime(runtime: RuntimeHandle) -> Self {
70        Self {
71            inner: MutableState::with_runtime(
72                Rc::new(MutableInteractionSourceInner {
73                    next_press_id: RefCell::new(1),
74                    active_presses: RefCell::new(HashSet::new()),
75                    pressed: OwnedMutableState::with_runtime(false, runtime.clone()),
76                    last_interaction: OwnedMutableState::with_runtime(None, runtime.clone()),
77                }),
78                runtime,
79            ),
80        }
81    }
82
83    fn inner(&self) -> Rc<MutableInteractionSourceInner> {
84        self.inner.get_non_reactive()
85    }
86
87    pub fn id(&self) -> u64 {
88        let mut hasher = std::collections::hash_map::DefaultHasher::new();
89        self.inner.runtime_state_id().hash(&mut hasher);
90        hasher.finish()
91    }
92
93    pub fn press(&self, press_position: Point) -> PressInteractionPress {
94        let inner = self.inner();
95        let id = {
96            let mut next_press_id = inner.next_press_id.borrow_mut();
97            let id = *next_press_id;
98            *next_press_id = id.saturating_add(1);
99            id
100        };
101        let press = PressInteractionPress { id, press_position };
102        self.emit(Interaction::Press(PressInteraction::Press(press)));
103        press
104    }
105
106    pub fn release(&self, press: PressInteractionPress) {
107        self.emit(Interaction::Press(PressInteraction::Release(
108            PressInteractionRelease { press },
109        )));
110    }
111
112    pub fn cancel(&self, press: PressInteractionPress) {
113        self.emit(Interaction::Press(PressInteraction::Cancel(
114            PressInteractionCancel { press },
115        )));
116    }
117
118    pub fn emit(&self, interaction: Interaction) {
119        let inner = self.inner();
120        inner.last_interaction.set(Some(interaction));
121        let is_pressed = {
122            let mut active_presses = inner.active_presses.borrow_mut();
123            match interaction {
124                Interaction::Press(PressInteraction::Press(press)) => {
125                    active_presses.insert(press.id);
126                }
127                Interaction::Press(PressInteraction::Release(release)) => {
128                    active_presses.remove(&release.press.id);
129                }
130                Interaction::Press(PressInteraction::Cancel(cancel)) => {
131                    active_presses.remove(&cancel.press.id);
132                }
133            }
134            !active_presses.is_empty()
135        };
136
137        if inner.pressed.get_non_reactive() != is_pressed {
138            inner.pressed.set(is_pressed);
139        }
140    }
141
142    /// Returns whether the interaction source is currently pressed as a
143    /// reactive [`State`].
144    ///
145    /// Mirrors Jetpack Compose: `InteractionSource.collectIsPressedAsState()`.
146    ///
147    /// The value flips to `true` when a `PressInteraction::Press` is emitted
148    /// and back to `false` once every active press has seen a matching
149    /// `PressInteraction::Release` or `PressInteraction::Cancel`. Reading the
150    /// returned state inside a composable subscribes the enclosing recompose
151    /// scope, so the composable recomposes whenever the pressed state changes.
152    pub fn collectIsPressedAsState(&self) -> State<bool> {
153        self.inner().pressed.as_state()
154    }
155
156    pub fn collectLastInteractionAsState(&self) -> State<Option<Interaction>> {
157        self.inner().last_interaction.as_state()
158    }
159}
160
161impl PressInteractionPress {
162    pub fn id(&self) -> u64 {
163        self.id
164    }
165}
166
167impl std::fmt::Debug for MutableInteractionSource {
168    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
169        f.debug_struct("MutableInteractionSource")
170            .field("id", &self.id())
171            .finish()
172    }
173}
174
175impl PartialEq for MutableInteractionSource {
176    fn eq(&self, other: &Self) -> bool {
177        self.inner == other.inner
178    }
179}
180
181impl Eq for MutableInteractionSource {}
182
183impl Default for MutableInteractionSource {
184    fn default() -> Self {
185        Self::new()
186    }
187}
188
189#[composable]
190pub fn rememberMutableInteractionSource() -> MutableInteractionSource {
191    let runtime = with_current_composer(|composer| composer.runtime_handle());
192    remember(move || MutableInteractionSource::with_runtime(runtime)).with(|source| *source)
193}
194
195/// Free-function form of
196/// [`MutableInteractionSource::collectIsPressedAsState`].
197///
198/// Mirrors Jetpack Compose: `InteractionSource.collectIsPressedAsState()`.
199///
200/// Returns a reactive [`State`] derived from the press interactions emitted
201/// by `interaction_source`: `true` between `PressInteraction::Press` and the
202/// matching `PressInteraction::Release`/`PressInteraction::Cancel`.
203pub fn collect_is_pressed_as_state(interaction_source: &MutableInteractionSource) -> State<bool> {
204    interaction_source.collectIsPressedAsState()
205}
206
207impl Modifier {
208    pub fn press_interaction_source(self, interaction_source: MutableInteractionSource) -> Self {
209        let source_id = interaction_source.id();
210        let modifier = Self::with_element(PressInteractionElement::new(interaction_source))
211            .with_inspector_metadata(inspector_metadata("pressInteractionSource", move |info| {
212                info.add_property("sourceId", source_id.to_string());
213            }));
214        self.then(modifier)
215    }
216}
217
218#[derive(Clone)]
219struct PressInteractionElement {
220    interaction_source: MutableInteractionSource,
221}
222
223impl PressInteractionElement {
224    fn new(interaction_source: MutableInteractionSource) -> Self {
225        Self { interaction_source }
226    }
227}
228
229impl std::fmt::Debug for PressInteractionElement {
230    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
231        f.debug_struct("PressInteractionElement")
232            .field("source_id", &self.interaction_source.id())
233            .finish()
234    }
235}
236
237impl PartialEq for PressInteractionElement {
238    fn eq(&self, other: &Self) -> bool {
239        self.interaction_source == other.interaction_source
240    }
241}
242
243impl Eq for PressInteractionElement {}
244
245impl Hash for PressInteractionElement {
246    fn hash<H: Hasher>(&self, state: &mut H) {
247        "pressInteractionSource".hash(state);
248        self.interaction_source.id().hash(state);
249    }
250}
251
252impl ModifierNodeElement for PressInteractionElement {
253    type Node = PressInteractionNode;
254
255    fn create(&self) -> Self::Node {
256        PressInteractionNode::new(self.interaction_source)
257    }
258
259    fn update(&self, node: &mut Self::Node) {
260        node.update(self.interaction_source);
261    }
262
263    fn capabilities(&self) -> NodeCapabilities {
264        NodeCapabilities::POINTER_INPUT
265    }
266}
267
268struct PressInteractionNode {
269    interaction_source: MutableInteractionSource,
270    active_press: Rc<RefCell<Option<PressInteractionPress>>>,
271    cached_handler: Rc<dyn Fn(PointerEvent)>,
272    state: NodeState,
273}
274
275impl PressInteractionNode {
276    fn new(interaction_source: MutableInteractionSource) -> Self {
277        let active_press = Rc::new(RefCell::new(None));
278        let cached_handler = Self::create_handler(interaction_source, active_press.clone());
279        Self {
280            interaction_source,
281            active_press,
282            cached_handler,
283            state: NodeState::new(),
284        }
285    }
286
287    fn update(&mut self, interaction_source: MutableInteractionSource) {
288        if self.interaction_source == interaction_source {
289            return;
290        }
291        if let Some(press) = self.active_press.borrow_mut().take() {
292            self.interaction_source.cancel(press);
293        }
294        self.interaction_source = interaction_source;
295        self.cached_handler =
296            Self::create_handler(self.interaction_source, self.active_press.clone());
297    }
298
299    fn create_handler(
300        interaction_source: MutableInteractionSource,
301        active_press: Rc<RefCell<Option<PressInteractionPress>>>,
302    ) -> Rc<dyn Fn(PointerEvent)> {
303        Rc::new(move |event: PointerEvent| {
304            if event.id != 0 {
305                return;
306            }
307
308            if event.is_consumed() {
309                if let Some(press) = active_press.borrow_mut().take() {
310                    interaction_source.cancel(press);
311                }
312                return;
313            }
314
315            match event.kind {
316                PointerEventKind::Down => {
317                    if active_press.borrow().is_none() {
318                        let press = interaction_source.press(event.position);
319                        *active_press.borrow_mut() = Some(press);
320                    }
321                }
322                PointerEventKind::Up => {
323                    if let Some(press) = active_press.borrow_mut().take() {
324                        interaction_source.release(press);
325                    }
326                }
327                PointerEventKind::Cancel => {
328                    if let Some(press) = active_press.borrow_mut().take() {
329                        interaction_source.cancel(press);
330                    }
331                }
332                PointerEventKind::Move
333                | PointerEventKind::Scroll
334                | PointerEventKind::Zoom
335                | PointerEventKind::RotaryScrollPre
336                | PointerEventKind::RotaryScroll
337                | PointerEventKind::Enter
338                | PointerEventKind::Exit => {}
339            }
340        })
341    }
342}
343
344impl std::fmt::Debug for PressInteractionNode {
345    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
346        f.debug_struct("PressInteractionNode")
347            .field("source_id", &self.interaction_source.id())
348            .finish()
349    }
350}
351
352impl DelegatableNode for PressInteractionNode {
353    fn node_state(&self) -> &NodeState {
354        &self.state
355    }
356}
357
358impl ModifierNode for PressInteractionNode {
359    fn on_attach(&mut self, context: &mut dyn ModifierNodeContext) {
360        context.invalidate(InvalidationKind::PointerInput);
361    }
362
363    fn as_pointer_input_node(&self) -> Option<&dyn PointerInputNode> {
364        Some(self)
365    }
366
367    fn as_pointer_input_node_mut(&mut self) -> Option<&mut dyn PointerInputNode> {
368        Some(self)
369    }
370
371    fn on_detach(&mut self) {
372        if let Some(press) = self.active_press.borrow_mut().take() {
373            self.interaction_source.cancel(press);
374        }
375    }
376}
377
378impl PointerInputNode for PressInteractionNode {
379    fn pointer_input_handler(&self) -> Option<Rc<dyn Fn(PointerEvent)>> {
380        Some(self.cached_handler.clone())
381    }
382}
383
384#[cfg(test)]
385#[path = "tests/interaction_tests.rs"]
386mod tests;