Skip to main content

cranpose_ui/modifier/
clickable.rs

1use std::rc::Rc;
2
3use super::{Modifier, Point, SemanticsConfiguration, inspector_metadata};
4use crate::{Indication, indication::current_indication, modifier_nodes::ClickableElement};
5
6impl Modifier {
7    /// Make the component clickable.
8    ///
9    /// The component shows the [`Indication`](crate::Indication) of
10    /// [`local_indication`](crate::local_indication) where the modifier is made:
11    /// while a pointer rests on it and while one presses it. A press that travels
12    /// past the drag threshold stops showing at once and does not click.
13    ///
14    /// Example: `Modifier::empty().clickable(|pt| println!("Clicked at {:?}", pt))`
15    pub fn clickable(self, handler: impl Fn(Point) + 'static) -> Self {
16        self.clickable_with_indication(current_indication(), handler)
17    }
18
19    /// Make the component clickable with `indication` in place of the one of
20    /// [`local_indication`](crate::local_indication). Compose's
21    /// `Modifier.clickable(interactionSource, indication, onClick)`.
22    /// [`Indication::NONE`] is Compose's `indication = null`, for a control
23    /// that draws its own hover and press.
24    pub fn clickable_with_indication(
25        self,
26        indication: Indication,
27        handler: impl Fn(Point) + 'static,
28    ) -> Self {
29        let element = ClickableElement::with_handler(Rc::new(handler)).indication(indication);
30        let modifier = Self::with_element(element).with_inspector_metadata(inspector_metadata(
31            "clickable",
32            |info| {
33                info.add_property("onClick", "provided");
34            },
35        ));
36        self.then(pressable(modifier))
37    }
38
39    /// Make the component react synchronously to primary-pointer press while
40    /// retaining ordinary click confirmation on release. It shows its
41    /// indication as [`clickable`](Self::clickable) does.
42    pub fn clickable_on_press(
43        self,
44        on_press: impl Fn(Point) + 'static,
45        on_click: impl Fn(Point) + 'static,
46    ) -> Self {
47        let element = ClickableElement::with_handlers(Rc::new(on_press), Rc::new(on_click))
48            .indication(current_indication());
49        let modifier = Self::with_element(element).with_inspector_metadata(inspector_metadata(
50            "clickableOnPress",
51            |info| {
52                info.add_property("onPress", "provided");
53                info.add_property("onClick", "provided");
54            },
55        ));
56        self.then(pressable(modifier))
57    }
58}
59
60/// What every control a pointer presses also gets: a reader's click, and a
61/// focus target with the keyboard ring, so Tab reaches it and Enter presses
62/// it.
63fn pressable(modifier: Modifier) -> Modifier {
64    modifier
65        .then(
66            Modifier::empty().stable_semantics(|config: &mut SemanticsConfiguration| {
67                config.is_clickable = true;
68            }),
69        )
70        .focusable()
71}