ailloli_ui_widgets/text/rich_text.rs
1//! Minimal multi-span text model rendered with the first span's style.
2
3use ailloli_ui_core::TextStyle;
4use ailloli_ui_runtime::{DrawCmd, DrawText};
5use ailloli_ui_text::{TextLayoutParams, TextSystem, WrapMode};
6
7#[derive(Debug, Clone)]
8/// Owned text fragment paired with its intended style.
9///
10/// In the current renderer, only the first span's style is applied to the
11/// concatenated string.
12///
13/// # Examples
14///
15/// ```
16/// use ailloli_ui_core::{Color, FontId, TextStyle};
17/// use ailloli_ui_widgets::text::TextSpan;
18/// let span = TextSpan { text: "hello".into(), style: TextStyle::new(FontId::Ui, 14, Color::WHITE) };
19/// assert_eq!(span.text, "hello");
20/// ```
21pub struct TextSpan {
22 /// Owned UTF-8 fragment; empty spans are allowed.
23 pub text: String,
24 /// Requested span style; currently ignored for non-first spans.
25 pub style: TextStyle,
26}
27
28#[derive(Debug, Clone)]
29/// Ordered spans plus wrap and optional logical-pixel width constraints.
30///
31/// `None` means unbounded width. An empty span list draws nothing.
32///
33/// # Examples
34///
35/// ```
36/// use ailloli_ui_core::{Color, FontId, TextStyle};
37/// use ailloli_ui_widgets::text::{RichText, WrapMode};
38/// let rich = RichText::plain("hello", TextStyle::new(FontId::Ui, 14, Color::WHITE));
39/// assert_eq!(rich.spans.len(), 1);
40/// assert_eq!(rich.wrap, WrapMode::NoWrap);
41/// assert_eq!(rich.max_width, None);
42/// ```
43pub struct RichText {
44 /// Ordered owned fragments; the first also supplies the rendered style.
45 pub spans: Vec<TextSpan>,
46 /// Line-breaking policy applied to the concatenated text.
47 pub wrap: WrapMode,
48 /// Optional maximum line width in logical pixels, passed through unchanged.
49 pub max_width: Option<f32>,
50}
51
52impl RichText {
53 /// Creates one unwrapped span with no maximum width.
54 ///
55 /// # Examples
56 ///
57 /// ```
58 /// use ailloli_ui_core::{Color, FontId, TextStyle};
59 /// use ailloli_ui_widgets::text::RichText;
60 /// let rich = RichText::plain("hello", TextStyle::new(FontId::Ui, 14, Color::WHITE));
61 /// assert_eq!(rich.spans[0].text, "hello");
62 /// ```
63 pub fn plain(text: impl Into<String>, style: TextStyle) -> Self {
64 Self {
65 spans: vec![TextSpan {
66 text: text.into(),
67 style,
68 }],
69 wrap: WrapMode::NoWrap,
70 max_width: None,
71 }
72 }
73}
74
75/// Concatenates spans into one string using the **first** span's style.
76///
77/// Full Parley multi-style layout in a single run is not implemented yet.
78/// Returns `None` for an empty span list. `baseline_xy` is in logical pixels and
79/// its y coordinate is the text baseline. The merged string allocates once.
80///
81/// # Examples
82///
83/// ```
84/// use ailloli_ui_core::{Color, FontId, TextStyle};
85/// use ailloli_ui_text::TextSystem;
86/// use ailloli_ui_widgets::text::{draw_rich_text, RichText};
87/// let mut text_system = TextSystem::new();
88/// let rich = RichText::plain("hello", TextStyle::new(FontId::Ui, 14, Color::WHITE));
89/// assert!(draw_rich_text([2.0, 18.0], &rich, &mut text_system).is_some());
90/// ```
91pub fn draw_rich_text(
92 baseline_xy: [f32; 2],
93 rich: &RichText,
94 text_system: &mut TextSystem,
95) -> Option<DrawCmd> {
96 let first = rich.spans.first()?;
97 let merged: String = rich.spans.iter().map(|s| s.text.as_str()).collect();
98 let style = first.style;
99 let color = style.color;
100 let prepared = text_system.layout_cached(TextLayoutParams {
101 text: merged.as_str(),
102 style,
103 max_width: rich.max_width,
104 wrap_mode: rich.wrap,
105 });
106 Some(DrawCmd::Text(DrawText {
107 pos: baseline_xy,
108 color,
109 decoration: ailloli_ui_core::TextDecoration::None,
110 layout: prepared,
111 }))
112}