Skip to main content

cranpose_ui/modifier/
clickable.rs

1use std::rc::Rc;
2
3use super::{Modifier, Point, SemanticsConfiguration, inspector_metadata};
4use crate::modifier_nodes::ClickableElement;
5
6impl Modifier {
7    /// Make the component clickable.
8    ///
9    /// Example: `Modifier::empty().clickable(|pt| println!("Clicked at {:?}", pt))`
10    pub fn clickable(self, handler: impl Fn(Point) + 'static) -> Self {
11        let handler = Rc::new(handler);
12        let modifier = Self::with_element(ClickableElement::with_handler(handler))
13            .with_inspector_metadata(inspector_metadata("clickable", |info| {
14                info.add_property("onClick", "provided");
15            }))
16            .then(
17                Modifier::empty().semantics(|config: &mut SemanticsConfiguration| {
18                    config.is_clickable = true;
19                }),
20            );
21        self.then(modifier)
22    }
23
24    /// Make the component react synchronously to primary-pointer press while
25    /// retaining ordinary click confirmation on release.
26    pub fn clickable_on_press(
27        self,
28        on_press: impl Fn(Point) + 'static,
29        on_click: impl Fn(Point) + 'static,
30    ) -> Self {
31        let modifier = Self::with_element(ClickableElement::with_handlers(
32            Rc::new(on_press),
33            Rc::new(on_click),
34        ))
35        .with_inspector_metadata(inspector_metadata("clickableOnPress", |info| {
36            info.add_property("onPress", "provided");
37            info.add_property("onClick", "provided");
38        }))
39        .then(
40            Modifier::empty().semantics(|config: &mut SemanticsConfiguration| {
41                config.is_clickable = true;
42            }),
43        );
44        self.then(modifier)
45    }
46}