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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//! LinkedText widget — renders AnnotatedString with link annotations auto-handled.
//!
//! Mirrors the behaviour of Jetpack Compose `BasicText` / `Text` when the
//! annotated string contains `LinkAnnotation.Url` or `LinkAnnotation.Clickable`.
#![expect(non_snake_case, reason = "names follow Compose's casing")]
use std::rc::Rc;
use cranpose_core::NodeId;
use crate::{
modifier::Modifier,
text::{AnnotatedString, LinkAnnotation, TextStyle},
widgets::ClickableText,
};
/// Displays annotated text and dispatches each link action. A URL annotation
/// calls `open_url`; a clickable annotation calls its own handler. A shared
/// [`AnnotatedString`] retains the prepared text across uses.
///
/// # Example
///
/// ```rust
/// use cranpose_ui::{text::AnnotatedString, *};
///
/// #[composable]
/// fn WebsiteLink() {
/// let text = AnnotatedString::builder()
/// .append("Visit ")
/// .with_link(
/// LinkAnnotation::Url("https://example.com".into()),
/// |builder| builder.append("the website"),
/// )
/// .to_annotated_string();
/// LinkedText(text, Modifier::empty(), TextStyle::default(), |url| {
/// println!("Open URL: {url}");
/// });
/// }
/// ```
///
/// An app can pass a callback from the platform URI service to open the URL.
pub fn LinkedText(
text: impl Into<AnnotatedString>,
modifier: Modifier,
style: TextStyle,
open_url: impl Fn(&str) + 'static,
) -> NodeId {
let text = text.into();
let text_for_links = text.clone();
let open_url: Rc<dyn Fn(&str)> = Rc::new(open_url);
let modifier = modifier.stable_semantics(link_actions(text.clone(), Rc::clone(&open_url)));
ClickableText(text, modifier, style, move |offset| {
for ann in text_for_links
.get_link_annotations(offset, offset + 1)
.into_iter()
.filter(|ann| ann.range.contains(&offset))
{
match &ann.item {
LinkAnnotation::Url(url) => open_url(url),
LinkAnnotation::Clickable { handler, .. } => handler(),
}
}
})
}
/// One action per link for a screen reader's actions menu, so a person who
/// cannot aim a tap at one word still opens every link the text holds.
fn link_actions(
text: AnnotatedString,
open_url: Rc<dyn Fn(&str)>,
) -> impl Fn(&mut cranpose_foundation::SemanticsConfiguration) {
move |config| {
for link in text.get_link_annotations(0, text.len()) {
let shown = text.text().get(link.range.clone());
let (label, action) = link_action(shown, &link.item, &open_url);
config
.custom_actions
.push(cranpose_foundation::SemanticsCustomAction::new(
label,
move || action(),
));
}
}
}
fn link_action(
shown: Option<&str>,
link: &LinkAnnotation,
open_url: &Rc<dyn Fn(&str)>,
) -> (String, Rc<dyn Fn()>) {
let shown = shown.map(str::trim).filter(|shown| !shown.is_empty());
match link {
LinkAnnotation::Url(url) => {
let label = format!("Open {}", shown.unwrap_or(url));
let open_url = Rc::clone(open_url);
let url = url.clone();
(label, Rc::new(move || open_url(&url)))
}
LinkAnnotation::Clickable { tag, handler } => {
let label = format!("Open {}", shown.unwrap_or(tag));
(label, Rc::clone(handler))
}
}
}
#[cfg(test)]
#[path = "tests/linked_text_tests.rs"]
mod tests;