Skip to main content

cranpose_ui/modifier/
clickable.rs

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