1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//! ClickableText widget for handling clicks on annotated text.
//!
//! Mirrors Jetpack Compose's `ClickableText` from:
//! `compose/foundation/foundation/src/commonMain/kotlin/androidx/compose/foundation/text/ClickableText.kt`
#![expect(non_snake_case)]
use std::rc::Rc;
use cranpose_core::NodeId;
use crate::{
modifier::Modifier,
text::{AnnotatedString, TextOverflow, TextStyle},
widgets::BasicText,
};
#[doc(hidden)]
pub trait IntoSharedAnnotatedString {
fn into_shared(self) -> Rc<AnnotatedString>;
}
impl IntoSharedAnnotatedString for AnnotatedString {
fn into_shared(self) -> Rc<AnnotatedString> {
Rc::new(self)
}
}
impl IntoSharedAnnotatedString for Rc<AnnotatedString> {
fn into_shared(self) -> Rc<AnnotatedString> {
self
}
}
/// Displays annotated text and calls `on_click` with the byte offset under the pointer.
/// Use [`LinkedText`](crate::LinkedText) for URL and custom-action annotations.
///
/// # Example
///
/// ```rust
/// use cranpose_ui::{text::AnnotatedString, *};
///
/// #[composable]
/// fn OffsetLabel() {
/// ClickableText(
/// AnnotatedString::from("Click a character"),
/// Modifier::empty(),
/// TextStyle::default(),
/// |offset| println!("Byte offset: {offset}"),
/// );
/// }
/// ```
pub fn ClickableText<T>(
text: T,
modifier: Modifier,
style: TextStyle,
on_click: impl Fn(usize) + 'static,
) -> NodeId
where
T: IntoSharedAnnotatedString,
{
let text = text.into_shared();
let text_for_click = text.clone();
let style_for_click = style.clone();
let on_click: Rc<dyn Fn(usize)> = Rc::new(on_click);
let clickable_modifier = modifier.clickable(move |point| {
let offset = crate::text::get_offset_for_position(
&text_for_click,
&style_for_click,
point.x,
point.y,
);
on_click(offset);
});
BasicText(
text,
clickable_modifier,
style,
TextOverflow::Clip,
true,
usize::MAX,
1,
)
}
#[cfg(test)]
#[path = "tests/clickable_text_tests.rs"]
mod tests;