Skip to main content

open_gpui/elements/
text.rs

1use crate::{
2    ActiveTooltip, AnyView, App, Bounds, DispatchPhase, Element, ElementId, GlobalElementId,
3    HighlightStyle, Hitbox, HitboxBehavior, InspectorElementId, IntoElement, LayoutId,
4    MouseDownEvent, MouseMoveEvent, MouseUpEvent, Pixels, Point, SharedString, Size, TextOverflow,
5    TextRun, TextStyle, TooltipId, TruncateFrom, WhiteSpace, Window, WrappedLine,
6    WrappedLineLayout, register_tooltip_mouse_handlers, set_tooltip_on_window,
7};
8use anyhow::Context as _;
9use itertools::Itertools;
10use open_gpui_core_util::ResultExt;
11use smallvec::SmallVec;
12use std::{
13    borrow::Cow,
14    cell::{Cell, RefCell},
15    mem,
16    ops::{Deref, DerefMut, Range},
17    rc::Rc,
18    sync::Arc,
19};
20
21/// An [`Element`] that renders text.
22///
23/// In general, [`Text`] objects should be created via the [`text`] macro:
24/// ```rust
25/// # use open_gpui::*;
26/// # fn render() -> impl IntoElement {
27/// div().child(text!("hello"))
28/// # }
29/// ```
30/// ## IDs and Accessibility
31///
32/// [`Text`] elements have an ID. This ID is primarily used to produce nodes in
33/// the accessibility tree, which allows the text to be visible to screen
34/// readers and other assistive technologies.
35///
36/// This ID is stable across frames. If the same text, with the same ID, is
37/// present in two consecutive frames, no updates are reported to the screen
38/// reader. If the text changes, but the ID stays the same, then the screen
39/// reader will be notified that a text node's content has changed. **However**,
40/// if the ID changes, then the screen reader will be notified that a node has
41/// been removed, and a new node has been added.
42///
43/// When using the [`text`] macro, each invocation of the macro will get a
44/// unique ID, derived from its position in the source code (filename, line, and
45/// column). For example:
46/// ```rust
47/// # use open_gpui::*;
48/// let x = text!("hello");
49/// let y = text!("hello");
50/// // not equal, because different `text!` invocations produced them
51/// assert_ne!(x.id(), y.id());
52///
53/// fn make_text(s: &str) -> Text { text!(s) }
54/// let x = make_text("hello");
55/// let y = make_text("hello");
56/// // equal, because the same `text!` invocation produced them
57/// assert_eq!(x.id(), y.id());
58/// ```
59/// When the contents of an invocation of [`text`] do not change, this
60/// distinction is less relevant (with the caveat that you still need to take
61/// care to ensure that duplicate IDs do not appear).
62///
63/// However, when a [`text`] invocation's argument *does* change, you should
64/// consider whether this change should be reported as a node "updating its
65/// contents", or an old node being destroyed and a new node being created.
66#[derive(Debug, Clone)]
67pub struct Text {
68    id: Option<ElementId>,
69    text: SharedString,
70}
71
72impl Text {
73    /// Create a new [`Text`] element with a specific ID.
74    ///
75    /// If you want a unique ID to be assigned automatically, use the [`text`]
76    /// macro. The docs for [`Text`] have more detail about choosing IDs.
77    #[inline]
78    pub const fn new(id: ElementId, text: SharedString) -> Self {
79        Self { id: Some(id), text }
80    }
81
82    /// Create a new [`Text`] element that is inaccessible to screen readers.
83    ///
84    /// In order for text to be accessible to screen readers, it must have an ID
85    /// provided. If you want text to be accessible, either use [`text`] to have
86    /// an ID automatically assigned, or use [`Text::new`] to manually assign an
87    /// ID.
88    ///
89    /// This function is intended for use inside custom UI components, where
90    /// accessible properties may be set on parent containers.
91    #[inline]
92    pub const fn new_inaccessible(text: SharedString) -> Self {
93        Self { id: None, text }
94    }
95
96    /// The ID of this [`Text`] element.
97    #[inline]
98    pub const fn id(&self) -> Option<&ElementId> {
99        self.id.as_ref()
100    }
101
102    /// Produce a new [`Text`] with the given `id`.
103    pub fn with_id(mut self, id: impl Into<ElementId>) -> Self {
104        self.id = Some(id.into());
105        self
106    }
107
108    /// The text that this [`Text`] element will display.
109    #[inline]
110    pub const fn text(&self) -> &SharedString {
111        &self.text
112    }
113}
114
115impl Deref for Text {
116    type Target = SharedString;
117    fn deref(&self) -> &Self::Target {
118        &self.text
119    }
120}
121
122impl DerefMut for Text {
123    fn deref_mut(&mut self) -> &mut Self::Target {
124        &mut self.text
125    }
126}
127
128/// Trivial hash function for the location information produced by the [`text`]
129/// macro. Not covered by semver guarantees. Performance is not particularly
130/// significant because it's only used on small strings in const contexts.
131#[doc(hidden)]
132pub const fn __hash_text_macro_location_unstable_do_not_use(s: &'static str) -> u64 {
133    const BASIS: u64 = 0xcbf29ce484222325;
134    const PRIME: u64 = 0x100000001b3;
135
136    let bytes = s.as_bytes();
137    let mut hash = BASIS;
138    let mut i = 0;
139    while i < bytes.len() {
140        hash ^= bytes[i] as u64;
141        hash = hash.wrapping_mul(PRIME);
142        i += 1;
143    }
144    hash
145}
146
147/// Create a new [`Text`] element.
148///
149/// ```rust
150/// # use open_gpui::*;
151/// let a = text!("hello");
152/// let b = text!(id = "farewell-message", "hello");
153///
154/// ```
155///
156/// Text created with this macro is *accessible*. The macro generates an ID
157/// based on the source location. See the docs for [`Text`] for a more in-depth
158/// explanation of the significance of the ID of a [`Text`] element.
159#[macro_export]
160macro_rules! text {
161    (id = $id:expr, $text:expr) => {{ $crate::Text::new($id.into(), $text.into()) }};
162    ($text:expr) => {{
163        const ID: &'static str = concat!(file!(), "/", line!(), ":", column!());
164        const HASH: u64 = $crate::__hash_text_macro_location_unstable_do_not_use(ID);
165        $crate::Text::new($crate::ElementId::Integer(HASH), $text.into())
166    }};
167}
168
169impl IntoElement for Text {
170    type Element = Self;
171    #[inline]
172    fn into_element(self) -> Self::Element {
173        self
174    }
175}
176
177impl Element for Text {
178    type RequestLayoutState = TextLayout;
179    type PrepaintState = ();
180
181    fn id(&self) -> Option<ElementId> {
182        self.id.clone()
183    }
184
185    fn source_location(&self) -> Option<&'static std::panic::Location<'static>> {
186        None
187    }
188
189    fn a11y_role(&self) -> Option<accesskit::Role> {
190        if self.id.is_some() {
191            Some(accesskit::Role::Label)
192        } else {
193            None
194        }
195    }
196
197    fn write_a11y_info(&self, node: &mut accesskit::Node) {
198        node.set_value(self.text.to_string());
199    }
200
201    fn request_layout(
202        &mut self,
203        id: Option<&GlobalElementId>,
204        inspector_id: Option<&InspectorElementId>,
205        window: &mut Window,
206        cx: &mut App,
207    ) -> (LayoutId, Self::RequestLayoutState) {
208        <SharedString as Element>::request_layout(&mut self.text, id, inspector_id, window, cx)
209    }
210
211    fn prepaint(
212        &mut self,
213        id: Option<&GlobalElementId>,
214        inspector_id: Option<&InspectorElementId>,
215        bounds: Bounds<Pixels>,
216        request_layout: &mut Self::RequestLayoutState,
217        window: &mut Window,
218        cx: &mut App,
219    ) -> Self::PrepaintState {
220        <SharedString as Element>::prepaint(
221            &mut self.text,
222            id,
223            inspector_id,
224            bounds,
225            request_layout,
226            window,
227            cx,
228        )
229    }
230
231    fn paint(
232        &mut self,
233        id: Option<&GlobalElementId>,
234        inspector_id: Option<&InspectorElementId>,
235        bounds: Bounds<Pixels>,
236        request_layout: &mut Self::RequestLayoutState,
237        prepaint: &mut Self::PrepaintState,
238        window: &mut Window,
239        cx: &mut App,
240    ) {
241        <SharedString as Element>::paint(
242            &mut self.text,
243            id,
244            inspector_id,
245            bounds,
246            request_layout,
247            prepaint,
248            window,
249            cx,
250        );
251    }
252}
253
254impl Element for &'static str {
255    type RequestLayoutState = TextLayout;
256    type PrepaintState = ();
257
258    fn id(&self) -> Option<ElementId> {
259        None
260    }
261
262    fn source_location(&self) -> Option<&'static core::panic::Location<'static>> {
263        None
264    }
265
266    fn request_layout(
267        &mut self,
268        _id: Option<&GlobalElementId>,
269        _inspector_id: Option<&InspectorElementId>,
270        window: &mut Window,
271        cx: &mut App,
272    ) -> (LayoutId, Self::RequestLayoutState) {
273        let mut state = TextLayout::default();
274        let layout_id = state.layout(SharedString::from(*self), None, window, cx);
275        (layout_id, state)
276    }
277
278    fn prepaint(
279        &mut self,
280        _id: Option<&GlobalElementId>,
281        _inspector_id: Option<&InspectorElementId>,
282        bounds: Bounds<Pixels>,
283        text_layout: &mut Self::RequestLayoutState,
284        _window: &mut Window,
285        _cx: &mut App,
286    ) {
287        text_layout.prepaint(bounds, self)
288    }
289
290    fn paint(
291        &mut self,
292        _id: Option<&GlobalElementId>,
293        _inspector_id: Option<&InspectorElementId>,
294        _bounds: Bounds<Pixels>,
295        text_layout: &mut TextLayout,
296        _: &mut (),
297        window: &mut Window,
298        cx: &mut App,
299    ) {
300        text_layout.paint(self, window, cx)
301    }
302}
303
304impl IntoElement for &'static str {
305    type Element = Self;
306
307    fn into_element(self) -> Self::Element {
308        self
309    }
310}
311
312impl IntoElement for String {
313    type Element = SharedString;
314
315    fn into_element(self) -> Self::Element {
316        self.into()
317    }
318}
319
320impl IntoElement for Cow<'static, str> {
321    type Element = SharedString;
322
323    fn into_element(self) -> Self::Element {
324        self.into()
325    }
326}
327
328impl Element for SharedString {
329    type RequestLayoutState = TextLayout;
330    type PrepaintState = ();
331
332    fn id(&self) -> Option<ElementId> {
333        None
334    }
335
336    fn source_location(&self) -> Option<&'static core::panic::Location<'static>> {
337        None
338    }
339
340    fn request_layout(
341        &mut self,
342        _id: Option<&GlobalElementId>,
343        _inspector_id: Option<&InspectorElementId>,
344        window: &mut Window,
345        cx: &mut App,
346    ) -> (LayoutId, Self::RequestLayoutState) {
347        let mut state = TextLayout::default();
348        let layout_id = state.layout(self.clone(), None, window, cx);
349        (layout_id, state)
350    }
351
352    fn prepaint(
353        &mut self,
354        _id: Option<&GlobalElementId>,
355        _inspector_id: Option<&InspectorElementId>,
356        bounds: Bounds<Pixels>,
357        text_layout: &mut Self::RequestLayoutState,
358        _window: &mut Window,
359        _cx: &mut App,
360    ) {
361        text_layout.prepaint(bounds, self.as_ref())
362    }
363
364    fn paint(
365        &mut self,
366        _id: Option<&GlobalElementId>,
367        _inspector_id: Option<&InspectorElementId>,
368        _bounds: Bounds<Pixels>,
369        text_layout: &mut Self::RequestLayoutState,
370        _: &mut Self::PrepaintState,
371        window: &mut Window,
372        cx: &mut App,
373    ) {
374        text_layout.paint(self.as_ref(), window, cx)
375    }
376}
377
378impl IntoElement for SharedString {
379    type Element = Self;
380
381    fn into_element(self) -> Self::Element {
382        self
383    }
384}
385
386/// Renders text with runs of different styles.
387///
388/// Callers are responsible for setting the correct style for each run.
389/// For text with a uniform style, you can usually avoid calling this constructor
390/// and just pass text directly.
391pub struct StyledText {
392    text: SharedString,
393    runs: Option<Vec<TextRun>>,
394    delayed_highlights: Option<Vec<(Range<usize>, HighlightStyle)>>,
395    delayed_font_family_overrides: Option<Vec<(Range<usize>, SharedString)>>,
396    layout: TextLayout,
397}
398
399impl StyledText {
400    /// Construct a new styled text element from the given string.
401    pub fn new(text: impl Into<SharedString>) -> Self {
402        StyledText {
403            text: text.into(),
404            runs: None,
405            delayed_highlights: None,
406            delayed_font_family_overrides: None,
407            layout: TextLayout::default(),
408        }
409    }
410
411    /// Get the layout for this element. This can be used to map indices to pixels and vice versa.
412    pub fn layout(&self) -> &TextLayout {
413        &self.layout
414    }
415
416    /// Set the styling attributes for the given text, as well as
417    /// as any ranges of text that have had their style customized.
418    pub fn with_default_highlights(
419        mut self,
420        default_style: &TextStyle,
421        highlights: impl IntoIterator<Item = (Range<usize>, HighlightStyle)>,
422    ) -> Self {
423        debug_assert!(
424            self.delayed_highlights.is_none(),
425            "Can't use `with_default_highlights` and `with_highlights`"
426        );
427        let runs = Self::compute_runs(&self.text, default_style, highlights);
428        self.with_runs(runs)
429    }
430
431    /// Set the styling attributes for the given text, as well as
432    /// as any ranges of text that have had their style customized.
433    pub fn with_highlights(
434        mut self,
435        highlights: impl IntoIterator<Item = (Range<usize>, HighlightStyle)>,
436    ) -> Self {
437        debug_assert!(
438            self.runs.is_none(),
439            "Can't use `with_highlights` and `with_default_highlights`"
440        );
441        self.delayed_highlights = Some(
442            highlights
443                .into_iter()
444                .inspect(|(run, _)| {
445                    debug_assert!(self.text.is_char_boundary(run.start));
446                    debug_assert!(self.text.is_char_boundary(run.end));
447                })
448                .collect::<Vec<_>>(),
449        );
450        self
451    }
452
453    fn compute_runs(
454        text: &str,
455        default_style: &TextStyle,
456        highlights: impl IntoIterator<Item = (Range<usize>, HighlightStyle)>,
457    ) -> Vec<TextRun> {
458        let mut runs = Vec::new();
459        let mut ix = 0;
460        for (range, highlight) in highlights {
461            if ix < range.start {
462                debug_assert!(text.is_char_boundary(range.start));
463                runs.push(default_style.clone().to_run(range.start - ix));
464            }
465            debug_assert!(text.is_char_boundary(range.end));
466            runs.push(
467                default_style
468                    .clone()
469                    .highlight(highlight)
470                    .to_run(range.len()),
471            );
472            ix = range.end;
473        }
474        if ix < text.len() {
475            runs.push(default_style.to_run(text.len() - ix));
476        }
477        runs
478    }
479
480    /// Override the font family for specific byte ranges of the text.
481    ///
482    /// This is resolved lazily at layout time, so the overrides are applied
483    /// on top of the inherited text style from the parent element.
484    /// Can be combined with [`with_highlights`](Self::with_highlights).
485    ///
486    /// The overrides must be sorted by range start and non-overlapping.
487    /// Each override range must fall on character boundaries.
488    pub fn with_font_family_overrides(
489        mut self,
490        overrides: impl IntoIterator<Item = (Range<usize>, SharedString)>,
491    ) -> Self {
492        self.delayed_font_family_overrides = Some(
493            overrides
494                .into_iter()
495                .inspect(|(range, _)| {
496                    debug_assert!(self.text.is_char_boundary(range.start));
497                    debug_assert!(self.text.is_char_boundary(range.end));
498                })
499                .collect(),
500        );
501        self
502    }
503
504    fn apply_font_family_overrides(
505        runs: &mut [TextRun],
506        overrides: &[(Range<usize>, SharedString)],
507    ) {
508        let mut byte_offset = 0;
509        let mut override_idx = 0;
510        for run in runs.iter_mut() {
511            let run_end = byte_offset + run.len;
512            while override_idx < overrides.len() && overrides[override_idx].0.end <= byte_offset {
513                override_idx += 1;
514            }
515            if override_idx < overrides.len() {
516                let (ref range, ref family) = overrides[override_idx];
517                if byte_offset >= range.start && run_end <= range.end {
518                    run.font.family = family.clone();
519                }
520            }
521            byte_offset = run_end;
522        }
523    }
524
525    /// Set the text runs for this piece of text.
526    pub fn with_runs(mut self, runs: Vec<TextRun>) -> Self {
527        let mut text = &*self.text;
528        for run in &runs {
529            text = text.get(run.len..).unwrap_or_else(|| {
530                #[cfg(debug_assertions)]
531                panic!("invalid text run. Text: '{text}', run: {run:?}");
532                #[cfg(not(debug_assertions))]
533                panic!("invalid text run");
534            });
535        }
536        assert!(text.is_empty(), "invalid text run");
537        self.runs = Some(runs);
538        self
539    }
540}
541
542impl Element for StyledText {
543    type RequestLayoutState = ();
544    type PrepaintState = ();
545
546    fn id(&self) -> Option<ElementId> {
547        None
548    }
549
550    fn source_location(&self) -> Option<&'static core::panic::Location<'static>> {
551        None
552    }
553
554    fn request_layout(
555        &mut self,
556        _id: Option<&GlobalElementId>,
557        _inspector_id: Option<&InspectorElementId>,
558        window: &mut Window,
559        cx: &mut App,
560    ) -> (LayoutId, Self::RequestLayoutState) {
561        let font_family_overrides = self.delayed_font_family_overrides.take();
562        let mut runs = self.runs.take().or_else(|| {
563            self.delayed_highlights.take().map(|delayed_highlights| {
564                Self::compute_runs(&self.text, &window.text_style(), delayed_highlights)
565            })
566        });
567
568        if let Some(ref overrides) = font_family_overrides {
569            let runs =
570                runs.get_or_insert_with(|| vec![window.text_style().to_run(self.text.len())]);
571            Self::apply_font_family_overrides(runs, overrides);
572        }
573
574        let layout_id = self.layout.layout(self.text.clone(), runs, window, cx);
575        (layout_id, ())
576    }
577
578    fn prepaint(
579        &mut self,
580        _id: Option<&GlobalElementId>,
581        _inspector_id: Option<&InspectorElementId>,
582        bounds: Bounds<Pixels>,
583        _: &mut Self::RequestLayoutState,
584        _window: &mut Window,
585        _cx: &mut App,
586    ) {
587        self.layout.prepaint(bounds, &self.text)
588    }
589
590    fn paint(
591        &mut self,
592        _id: Option<&GlobalElementId>,
593        _inspector_id: Option<&InspectorElementId>,
594        _bounds: Bounds<Pixels>,
595        _: &mut Self::RequestLayoutState,
596        _: &mut Self::PrepaintState,
597        window: &mut Window,
598        cx: &mut App,
599    ) {
600        self.layout.paint(&self.text, window, cx)
601    }
602}
603
604impl IntoElement for StyledText {
605    type Element = Self;
606
607    fn into_element(self) -> Self::Element {
608        self
609    }
610}
611
612/// The Layout for TextElement. This can be used to map indices to pixels and vice versa.
613#[derive(Default, Clone)]
614pub struct TextLayout(Rc<RefCell<Option<TextLayoutInner>>>);
615
616struct TextLayoutInner {
617    len: usize,
618    lines: SmallVec<[WrappedLine; 1]>,
619    line_height: Pixels,
620    wrap_width: Option<Pixels>,
621    size: Option<Size<Pixels>>,
622    bounds: Option<Bounds<Pixels>>,
623}
624
625impl TextLayout {
626    fn layout(
627        &self,
628        text: SharedString,
629        runs: Option<Vec<TextRun>>,
630        window: &mut Window,
631        _: &mut App,
632    ) -> LayoutId {
633        let text_style = window.text_style();
634        let font_size = text_style.font_size.to_pixels(window.rem_size());
635        let line_height = window.pixel_snap(
636            text_style
637                .line_height
638                .to_pixels(font_size.into(), window.rem_size()),
639        );
640
641        let runs = if let Some(runs) = runs {
642            runs
643        } else {
644            vec![text_style.to_run(text.len())]
645        };
646        window.request_measured_layout(Default::default(), {
647            let element_state = self.clone();
648
649            move |known_dimensions, available_space, window, cx| {
650                let wrap_width = if text_style.white_space == WhiteSpace::Normal {
651                    known_dimensions.width.or(match available_space.width {
652                        crate::AvailableSpace::Definite(x) => Some(x),
653                        _ => None,
654                    })
655                } else {
656                    None
657                };
658
659                let (truncate_width, truncation_affix, truncate_from) =
660                    if let Some(text_overflow) = text_style.text_overflow.clone() {
661                        let width = known_dimensions.width.or(match available_space.width {
662                            crate::AvailableSpace::Definite(x) => match text_style.line_clamp {
663                                Some(max_lines) => Some(x * max_lines),
664                                None => Some(x),
665                            },
666                            _ => None,
667                        });
668
669                        match text_overflow {
670                            TextOverflow::Truncate(s) => (width, s, TruncateFrom::End),
671                            TextOverflow::TruncateStart(s) => (width, s, TruncateFrom::Start),
672                        }
673                    } else {
674                        (None, "".into(), TruncateFrom::End)
675                    };
676
677                // Only use cached layout if:
678                // 1. We have a cached size
679                // 2. wrap_width matches (or both are None)
680                // 3. truncate_width is None (if truncate_width is Some, we need to re-layout
681                //    because the previous layout may have been computed without truncation)
682                if let Some(text_layout) = element_state.0.borrow().as_ref()
683                    && let Some(size) = text_layout.size
684                    && (wrap_width.is_none() || wrap_width == text_layout.wrap_width)
685                    && truncate_width.is_none()
686                {
687                    return size;
688                }
689
690                let mut line_wrapper = cx.text_system().line_wrapper(text_style.font(), font_size);
691                let (text, runs) = if truncate_width.is_some() {
692                    if let Some(max_lines) = text_style.line_clamp
693                        && let Some(wrap_width) = wrap_width
694                    {
695                        line_wrapper.truncate_wrapped_line(
696                            text.clone(),
697                            wrap_width,
698                            max_lines,
699                            &truncation_affix,
700                            &runs,
701                            truncate_from,
702                        )
703                    } else {
704                        line_wrapper.truncate_line(
705                            text.clone(),
706                            truncate_width.unwrap_or(Pixels::MAX),
707                            &truncation_affix,
708                            &runs,
709                            truncate_from,
710                        )
711                    }
712                } else {
713                    (text.clone(), Cow::Borrowed(&*runs))
714                };
715                let len = text.len();
716
717                let Some(lines) = window
718                    .text_system()
719                    .shape_text(
720                        text,
721                        font_size,
722                        &runs,
723                        wrap_width,            // Wrap if we know the width.
724                        text_style.line_clamp, // Limit the number of lines if line_clamp is set.
725                    )
726                    .log_err()
727                else {
728                    element_state.0.borrow_mut().replace(TextLayoutInner {
729                        lines: Default::default(),
730                        len: 0,
731                        line_height,
732                        wrap_width,
733                        size: Some(Size::default()),
734                        bounds: None,
735                    });
736                    return Size::default();
737                };
738
739                let mut size: Size<Pixels> = Size::default();
740                for line in &lines {
741                    let line_size = line.size(line_height);
742                    size.height += line_size.height;
743                    size.width = size.width.max(line_size.width).ceil();
744                }
745
746                element_state.0.borrow_mut().replace(TextLayoutInner {
747                    lines,
748                    len,
749                    line_height,
750                    wrap_width,
751                    size: Some(size),
752                    bounds: None,
753                });
754
755                size
756            }
757        })
758    }
759
760    fn prepaint(&self, bounds: Bounds<Pixels>, text: &str) {
761        let mut element_state = self.0.borrow_mut();
762        let element_state = element_state
763            .as_mut()
764            .with_context(|| format!("measurement has not been performed on {text}"))
765            .unwrap();
766        element_state.bounds = Some(bounds);
767    }
768
769    fn paint(&self, text: &str, window: &mut Window, cx: &mut App) {
770        let element_state = self.0.borrow();
771        let element_state = element_state
772            .as_ref()
773            .with_context(|| format!("measurement has not been performed on {text}"))
774            .unwrap();
775        let bounds = element_state
776            .bounds
777            .with_context(|| format!("prepaint has not been performed on {text}"))
778            .unwrap();
779
780        let line_height = element_state.line_height;
781        let mut line_origin = bounds.origin;
782        let text_style = window.text_style();
783        for line in &element_state.lines {
784            line.paint_background(
785                line_origin,
786                line_height,
787                text_style.text_align,
788                Some(bounds),
789                window,
790                cx,
791            )
792            .log_err();
793            line.paint(
794                line_origin,
795                line_height,
796                text_style.text_align,
797                Some(bounds),
798                window,
799                cx,
800            )
801            .log_err();
802            line_origin.y += line.size(line_height).height;
803        }
804    }
805
806    /// Get the byte index into the input of the pixel position.
807    pub fn index_for_position(&self, mut position: Point<Pixels>) -> Result<usize, usize> {
808        let element_state = self.0.borrow();
809        let element_state = element_state
810            .as_ref()
811            .expect("measurement has not been performed");
812        let bounds = element_state
813            .bounds
814            .expect("prepaint has not been performed");
815
816        if position.y < bounds.top() {
817            return Err(0);
818        }
819
820        let line_height = element_state.line_height;
821        let mut line_origin = bounds.origin;
822        let mut line_start_ix = 0;
823        for line in &element_state.lines {
824            let line_bottom = line_origin.y + line.size(line_height).height;
825            if position.y > line_bottom {
826                line_origin.y = line_bottom;
827                line_start_ix += line.len() + 1;
828            } else {
829                let position_within_line = position - line_origin;
830                match line.index_for_position(position_within_line, line_height) {
831                    Ok(index_within_line) => return Ok(line_start_ix + index_within_line),
832                    Err(index_within_line) => return Err(line_start_ix + index_within_line),
833                }
834            }
835        }
836
837        Err(line_start_ix.saturating_sub(1))
838    }
839
840    /// Get the pixel position for the given byte index.
841    pub fn position_for_index(&self, index: usize) -> Option<Point<Pixels>> {
842        let element_state = self.0.borrow();
843        let element_state = element_state
844            .as_ref()
845            .expect("measurement has not been performed");
846        let bounds = element_state
847            .bounds
848            .expect("prepaint has not been performed");
849        let line_height = element_state.line_height;
850
851        let mut line_origin = bounds.origin;
852        let mut line_start_ix = 0;
853
854        for line in &element_state.lines {
855            let line_end_ix = line_start_ix + line.len();
856            if index < line_start_ix {
857                break;
858            } else if index > line_end_ix {
859                line_origin.y += line.size(line_height).height;
860                line_start_ix = line_end_ix + 1;
861                continue;
862            } else {
863                let ix_within_line = index - line_start_ix;
864                return Some(line_origin + line.position_for_index(ix_within_line, line_height)?);
865            }
866        }
867
868        None
869    }
870
871    /// Retrieve the layout for the line containing the given byte index.
872    pub fn line_layout_for_index(&self, index: usize) -> Option<Arc<WrappedLineLayout>> {
873        let element_state = self.0.borrow();
874        let element_state = element_state
875            .as_ref()
876            .expect("measurement has not been performed");
877        let bounds = element_state
878            .bounds
879            .expect("prepaint has not been performed");
880        let line_height = element_state.line_height;
881
882        let mut line_origin = bounds.origin;
883        let mut line_start_ix = 0;
884
885        for line in &element_state.lines {
886            let line_end_ix = line_start_ix + line.len();
887            if index < line_start_ix {
888                break;
889            } else if index > line_end_ix {
890                line_origin.y += line.size(line_height).height;
891                line_start_ix = line_end_ix + 1;
892                continue;
893            } else {
894                return Some(line.layout.clone());
895            }
896        }
897
898        None
899    }
900
901    /// The bounds of this layout.
902    pub fn bounds(&self) -> Bounds<Pixels> {
903        self.0.borrow().as_ref().unwrap().bounds.unwrap()
904    }
905
906    /// The line height for this layout.
907    pub fn line_height(&self) -> Pixels {
908        self.0.borrow().as_ref().unwrap().line_height
909    }
910
911    /// The UTF-8 length of the underlying text.
912    pub fn len(&self) -> usize {
913        self.0.borrow().as_ref().unwrap().len
914    }
915
916    /// The text for this layout.
917    pub fn text(&self) -> String {
918        self.0
919            .borrow()
920            .as_ref()
921            .unwrap()
922            .lines
923            .iter()
924            .map(|s| &s.text)
925            .join("\n")
926    }
927
928    /// The text for this layout (with soft-wraps as newlines)
929    pub fn wrapped_text(&self) -> String {
930        let mut accumulator = String::new();
931
932        for wrapped in self.0.borrow().as_ref().unwrap().lines.iter() {
933            let mut seen = 0;
934            for boundary in wrapped.layout.wrap_boundaries.iter() {
935                let index = wrapped.layout.unwrapped_layout.runs[boundary.run_ix].glyphs
936                    [boundary.glyph_ix]
937                    .index;
938
939                accumulator.push_str(&wrapped.text[seen..index]);
940                accumulator.push('\n');
941                seen = index;
942            }
943            accumulator.push_str(&wrapped.text[seen..]);
944            accumulator.push('\n');
945        }
946        // Remove trailing newline
947        accumulator.pop();
948        accumulator
949    }
950}
951
952/// A text element that can be interacted with.
953pub struct InteractiveText {
954    element_id: ElementId,
955    text: StyledText,
956    click_listener:
957        Option<Box<dyn Fn(&[Range<usize>], InteractiveTextClickEvent, &mut Window, &mut App)>>,
958    hover_listener: Option<Box<dyn Fn(Option<usize>, MouseMoveEvent, &mut Window, &mut App)>>,
959    tooltip_builder: Option<Rc<dyn Fn(usize, &mut Window, &mut App) -> Option<AnyView>>>,
960    tooltip_id: Option<TooltipId>,
961    clickable_ranges: Vec<Range<usize>>,
962}
963
964struct InteractiveTextClickEvent {
965    mouse_down_index: usize,
966    mouse_up_index: usize,
967}
968
969#[doc(hidden)]
970#[derive(Default)]
971pub struct InteractiveTextState {
972    mouse_down_index: Rc<Cell<Option<usize>>>,
973    hovered_index: Rc<Cell<Option<usize>>>,
974    active_tooltip: Rc<RefCell<Option<ActiveTooltip>>>,
975}
976
977/// InteractiveTest is a wrapper around StyledText that adds mouse interactions.
978impl InteractiveText {
979    /// Creates a new InteractiveText from the given text.
980    pub fn new(id: impl Into<ElementId>, text: StyledText) -> Self {
981        Self {
982            element_id: id.into(),
983            text,
984            click_listener: None,
985            hover_listener: None,
986            tooltip_builder: None,
987            tooltip_id: None,
988            clickable_ranges: Vec::new(),
989        }
990    }
991
992    /// on_click is called when the user clicks on one of the given ranges, passing the index of
993    /// the clicked range.
994    pub fn on_click(
995        mut self,
996        ranges: Vec<Range<usize>>,
997        listener: impl Fn(usize, &mut Window, &mut App) + 'static,
998    ) -> Self {
999        self.click_listener = Some(Box::new(move |ranges, event, window, cx| {
1000            for (range_ix, range) in ranges.iter().enumerate() {
1001                if range.contains(&event.mouse_down_index) && range.contains(&event.mouse_up_index)
1002                {
1003                    listener(range_ix, window, cx);
1004                }
1005            }
1006        }));
1007        self.clickable_ranges = ranges;
1008        self
1009    }
1010
1011    /// on_hover is called when the mouse moves over a character within the text, passing the
1012    /// index of the hovered character, or None if the mouse leaves the text.
1013    pub fn on_hover(
1014        mut self,
1015        listener: impl Fn(Option<usize>, MouseMoveEvent, &mut Window, &mut App) + 'static,
1016    ) -> Self {
1017        self.hover_listener = Some(Box::new(listener));
1018        self
1019    }
1020
1021    /// tooltip lets you specify a tooltip for a given character index in the string.
1022    pub fn tooltip(
1023        mut self,
1024        builder: impl Fn(usize, &mut Window, &mut App) -> Option<AnyView> + 'static,
1025    ) -> Self {
1026        self.tooltip_builder = Some(Rc::new(builder));
1027        self
1028    }
1029}
1030
1031impl Element for InteractiveText {
1032    type RequestLayoutState = ();
1033    type PrepaintState = Hitbox;
1034
1035    fn id(&self) -> Option<ElementId> {
1036        Some(self.element_id.clone())
1037    }
1038
1039    fn source_location(&self) -> Option<&'static core::panic::Location<'static>> {
1040        None
1041    }
1042
1043    fn a11y_role(&self) -> Option<accesskit::Role> {
1044        Some(accesskit::Role::Label)
1045    }
1046
1047    fn write_a11y_info(&self, node: &mut accesskit::Node) {
1048        node.set_value(self.text.text.to_string());
1049    }
1050
1051    fn request_layout(
1052        &mut self,
1053        _id: Option<&GlobalElementId>,
1054        inspector_id: Option<&InspectorElementId>,
1055        window: &mut Window,
1056        cx: &mut App,
1057    ) -> (LayoutId, Self::RequestLayoutState) {
1058        self.text.request_layout(None, inspector_id, window, cx)
1059    }
1060
1061    fn prepaint(
1062        &mut self,
1063        global_id: Option<&GlobalElementId>,
1064        inspector_id: Option<&InspectorElementId>,
1065        bounds: Bounds<Pixels>,
1066        state: &mut Self::RequestLayoutState,
1067        window: &mut Window,
1068        cx: &mut App,
1069    ) -> Hitbox {
1070        window.with_optional_element_state::<InteractiveTextState, _>(
1071            global_id,
1072            |interactive_state, window| {
1073                let mut interactive_state = interactive_state
1074                    .map(|interactive_state| interactive_state.unwrap_or_default());
1075
1076                if let Some(interactive_state) = interactive_state.as_mut() {
1077                    if self.tooltip_builder.is_some() {
1078                        self.tooltip_id =
1079                            set_tooltip_on_window(&interactive_state.active_tooltip, window);
1080                    } else {
1081                        // If there is no longer a tooltip builder, remove the active tooltip.
1082                        interactive_state.active_tooltip.take();
1083                    }
1084                }
1085
1086                self.text
1087                    .prepaint(None, inspector_id, bounds, state, window, cx);
1088                let hitbox = window.insert_hitbox(bounds, HitboxBehavior::Normal);
1089                (hitbox, interactive_state)
1090            },
1091        )
1092    }
1093
1094    fn paint(
1095        &mut self,
1096        global_id: Option<&GlobalElementId>,
1097        inspector_id: Option<&InspectorElementId>,
1098        bounds: Bounds<Pixels>,
1099        _: &mut Self::RequestLayoutState,
1100        hitbox: &mut Hitbox,
1101        window: &mut Window,
1102        cx: &mut App,
1103    ) {
1104        let current_view = window.current_view();
1105        let text_layout = self.text.layout().clone();
1106        window.with_element_state::<InteractiveTextState, _>(
1107            global_id.unwrap(),
1108            |interactive_state, window| {
1109                let mut interactive_state = interactive_state.unwrap_or_default();
1110                if let Some(click_listener) = self.click_listener.take() {
1111                    let mouse_position = window.mouse_position();
1112                    if let Ok(ix) = text_layout.index_for_position(mouse_position)
1113                        && self
1114                            .clickable_ranges
1115                            .iter()
1116                            .any(|range| range.contains(&ix))
1117                    {
1118                        window.set_cursor_style(crate::CursorStyle::PointingHand, hitbox)
1119                    }
1120
1121                    let text_layout = text_layout.clone();
1122                    let mouse_down = interactive_state.mouse_down_index.clone();
1123                    if let Some(mouse_down_index) = mouse_down.get() {
1124                        let hitbox = hitbox.clone();
1125                        let clickable_ranges = mem::take(&mut self.clickable_ranges);
1126                        window.on_mouse_event(
1127                            move |event: &MouseUpEvent, phase, window: &mut Window, cx| {
1128                                if phase == DispatchPhase::Bubble && hitbox.is_hovered(window) {
1129                                    if let Ok(mouse_up_index) =
1130                                        text_layout.index_for_position(event.position)
1131                                    {
1132                                        click_listener(
1133                                            &clickable_ranges,
1134                                            InteractiveTextClickEvent {
1135                                                mouse_down_index,
1136                                                mouse_up_index,
1137                                            },
1138                                            window,
1139                                            cx,
1140                                        )
1141                                    }
1142
1143                                    mouse_down.take();
1144                                    window.refresh();
1145                                }
1146                            },
1147                        );
1148                    } else {
1149                        let hitbox = hitbox.clone();
1150                        window.on_mouse_event(move |event: &MouseDownEvent, phase, window, _| {
1151                            if phase == DispatchPhase::Bubble
1152                                && hitbox.is_hovered(window)
1153                                && let Ok(mouse_down_index) =
1154                                    text_layout.index_for_position(event.position)
1155                            {
1156                                mouse_down.set(Some(mouse_down_index));
1157                                window.refresh();
1158                            }
1159                        });
1160                    }
1161                }
1162
1163                window.on_mouse_event({
1164                    let mut hover_listener = self.hover_listener.take();
1165                    let hitbox = hitbox.clone();
1166                    let text_layout = text_layout.clone();
1167                    let hovered_index = interactive_state.hovered_index.clone();
1168                    move |event: &MouseMoveEvent, phase, window, cx| {
1169                        if phase == DispatchPhase::Bubble && hitbox.is_hovered(window) {
1170                            let current = hovered_index.get();
1171                            let updated = text_layout.index_for_position(event.position).ok();
1172                            if current != updated {
1173                                hovered_index.set(updated);
1174                                if let Some(hover_listener) = hover_listener.as_ref() {
1175                                    hover_listener(updated, event.clone(), window, cx);
1176                                }
1177                                cx.notify(current_view);
1178                            }
1179                        }
1180                    }
1181                });
1182
1183                if let Some(tooltip_builder) = self.tooltip_builder.clone() {
1184                    let active_tooltip = interactive_state.active_tooltip.clone();
1185                    let build_tooltip = Rc::new({
1186                        let tooltip_is_hoverable = false;
1187                        let text_layout = text_layout.clone();
1188                        move |window: &mut Window, cx: &mut App| {
1189                            text_layout
1190                                .index_for_position(window.mouse_position())
1191                                .ok()
1192                                .and_then(|position| tooltip_builder(position, window, cx))
1193                                .map(|view| (view, tooltip_is_hoverable))
1194                        }
1195                    });
1196
1197                    // Use bounds instead of testing hitbox since this is called during prepaint.
1198                    let check_is_hovered_during_prepaint = Rc::new({
1199                        let source_bounds = hitbox.bounds;
1200                        let text_layout = text_layout.clone();
1201                        let pending_mouse_down = interactive_state.mouse_down_index.clone();
1202                        move |window: &Window| {
1203                            text_layout
1204                                .index_for_position(window.mouse_position())
1205                                .is_ok()
1206                                && source_bounds.contains(&window.mouse_position())
1207                                && pending_mouse_down.get().is_none()
1208                        }
1209                    });
1210
1211                    let check_is_hovered = Rc::new({
1212                        let hitbox = hitbox.clone();
1213                        let text_layout = text_layout.clone();
1214                        let pending_mouse_down = interactive_state.mouse_down_index.clone();
1215                        move |window: &Window| {
1216                            text_layout
1217                                .index_for_position(window.mouse_position())
1218                                .is_ok()
1219                                && hitbox.is_hovered(window)
1220                                && pending_mouse_down.get().is_none()
1221                        }
1222                    });
1223
1224                    register_tooltip_mouse_handlers(
1225                        &active_tooltip,
1226                        self.tooltip_id,
1227                        build_tooltip,
1228                        check_is_hovered,
1229                        check_is_hovered_during_prepaint,
1230                        None,
1231                        window,
1232                    );
1233                }
1234
1235                self.text
1236                    .paint(None, inspector_id, bounds, &mut (), &mut (), window, cx);
1237
1238                ((), interactive_state)
1239            },
1240        );
1241    }
1242}
1243
1244impl IntoElement for InteractiveText {
1245    type Element = Self;
1246
1247    fn into_element(self) -> Self::Element {
1248        self
1249    }
1250}
1251
1252#[cfg(test)]
1253mod tests {
1254    use super::*;
1255
1256    #[test]
1257    fn test_into_element_for() {
1258        use crate::{ParentElement as _, SharedString, div};
1259        use std::borrow::Cow;
1260
1261        let _ = div().child("static str");
1262        let _ = div().child("String".to_string());
1263        let _ = div().child(Cow::Borrowed("Cow"));
1264        let _ = div().child(SharedString::from("SharedString"));
1265    }
1266
1267    #[test]
1268    fn text_macro_id() {
1269        // one call to `text!` = one id
1270        fn make_text_stable_id(happy: bool) -> Text {
1271            text!(if happy { "happy" } else { "sad" })
1272        }
1273
1274        // two calls to `text!` = two ids
1275        fn make_text_unstable_id(happy: bool) -> Text {
1276            if happy { text!("happy") } else { text!("sad") }
1277        }
1278
1279        assert_eq!(make_text_stable_id(false).id, make_text_stable_id(true).id);
1280        assert_ne!(
1281            make_text_unstable_id(false).id,
1282            make_text_unstable_id(true).id
1283        );
1284    }
1285}