Skip to main content

boltz_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 gpui_util::ResultExt;
10use itertools::Itertools;
11use smallvec::SmallVec;
12use std::{
13    borrow::Cow,
14    cell::{Cell, RefCell},
15    mem,
16    ops::Range,
17    rc::Rc,
18    sync::Arc,
19};
20
21impl Element for &'static str {
22    type RequestLayoutState = TextLayout;
23    type PrepaintState = ();
24
25    fn id(&self) -> Option<ElementId> {
26        None
27    }
28
29    fn source_location(&self) -> Option<&'static core::panic::Location<'static>> {
30        None
31    }
32
33    fn request_layout(
34        &mut self,
35        _id: Option<&GlobalElementId>,
36        _inspector_id: Option<&InspectorElementId>,
37        window: &mut Window,
38        cx: &mut App,
39    ) -> (LayoutId, Self::RequestLayoutState) {
40        let mut state = TextLayout::default();
41        let layout_id = state.layout(SharedString::from(*self), None, window, cx);
42        (layout_id, state)
43    }
44
45    fn prepaint(
46        &mut self,
47        _id: Option<&GlobalElementId>,
48        _inspector_id: Option<&InspectorElementId>,
49        bounds: Bounds<Pixels>,
50        text_layout: &mut Self::RequestLayoutState,
51        _window: &mut Window,
52        _cx: &mut App,
53    ) {
54        text_layout.prepaint(bounds, self)
55    }
56
57    fn paint(
58        &mut self,
59        _id: Option<&GlobalElementId>,
60        _inspector_id: Option<&InspectorElementId>,
61        _bounds: Bounds<Pixels>,
62        text_layout: &mut TextLayout,
63        _: &mut (),
64        window: &mut Window,
65        cx: &mut App,
66    ) {
67        text_layout.paint(self, window, cx)
68    }
69}
70
71impl IntoElement for &'static str {
72    type Element = Self;
73
74    fn into_element(self) -> Self::Element {
75        self
76    }
77}
78
79impl IntoElement for String {
80    type Element = SharedString;
81
82    fn into_element(self) -> Self::Element {
83        self.into()
84    }
85}
86
87impl IntoElement for Cow<'static, str> {
88    type Element = SharedString;
89
90    fn into_element(self) -> Self::Element {
91        self.into()
92    }
93}
94
95impl Element for SharedString {
96    type RequestLayoutState = TextLayout;
97    type PrepaintState = ();
98
99    fn id(&self) -> Option<ElementId> {
100        None
101    }
102
103    fn source_location(&self) -> Option<&'static core::panic::Location<'static>> {
104        None
105    }
106
107    fn request_layout(
108        &mut self,
109        _id: Option<&GlobalElementId>,
110        _inspector_id: Option<&InspectorElementId>,
111        window: &mut Window,
112        cx: &mut App,
113    ) -> (LayoutId, Self::RequestLayoutState) {
114        let mut state = TextLayout::default();
115        let layout_id = state.layout(self.clone(), None, window, cx);
116        (layout_id, state)
117    }
118
119    fn prepaint(
120        &mut self,
121        _id: Option<&GlobalElementId>,
122        _inspector_id: Option<&InspectorElementId>,
123        bounds: Bounds<Pixels>,
124        text_layout: &mut Self::RequestLayoutState,
125        _window: &mut Window,
126        _cx: &mut App,
127    ) {
128        text_layout.prepaint(bounds, self.as_ref())
129    }
130
131    fn paint(
132        &mut self,
133        _id: Option<&GlobalElementId>,
134        _inspector_id: Option<&InspectorElementId>,
135        _bounds: Bounds<Pixels>,
136        text_layout: &mut Self::RequestLayoutState,
137        _: &mut Self::PrepaintState,
138        window: &mut Window,
139        cx: &mut App,
140    ) {
141        text_layout.paint(self.as_ref(), window, cx)
142    }
143}
144
145impl IntoElement for SharedString {
146    type Element = Self;
147
148    fn into_element(self) -> Self::Element {
149        self
150    }
151}
152
153/// Renders text with runs of different styles.
154///
155/// Callers are responsible for setting the correct style for each run.
156/// For text with a uniform style, you can usually avoid calling this constructor
157/// and just pass text directly.
158pub struct StyledText {
159    text: SharedString,
160    runs: Option<Vec<TextRun>>,
161    delayed_highlights: Option<Vec<(Range<usize>, HighlightStyle)>>,
162    delayed_font_family_overrides: Option<Vec<(Range<usize>, SharedString)>>,
163    layout: TextLayout,
164}
165
166impl StyledText {
167    /// Construct a new styled text element from the given string.
168    pub fn new(text: impl Into<SharedString>) -> Self {
169        StyledText {
170            text: text.into(),
171            runs: None,
172            delayed_highlights: None,
173            delayed_font_family_overrides: None,
174            layout: TextLayout::default(),
175        }
176    }
177
178    /// Get the layout for this element. This can be used to map indices to pixels and vice versa.
179    pub fn layout(&self) -> &TextLayout {
180        &self.layout
181    }
182
183    /// Set the styling attributes for the given text, as well as
184    /// as any ranges of text that have had their style customized.
185    pub fn with_default_highlights(
186        mut self,
187        default_style: &TextStyle,
188        highlights: impl IntoIterator<Item = (Range<usize>, HighlightStyle)>,
189    ) -> Self {
190        debug_assert!(
191            self.delayed_highlights.is_none(),
192            "Can't use `with_default_highlights` and `with_highlights`"
193        );
194        let runs = Self::compute_runs(&self.text, default_style, highlights);
195        self.with_runs(runs)
196    }
197
198    /// Set the styling attributes for the given text, as well as
199    /// as any ranges of text that have had their style customized.
200    pub fn with_highlights(
201        mut self,
202        highlights: impl IntoIterator<Item = (Range<usize>, HighlightStyle)>,
203    ) -> Self {
204        debug_assert!(
205            self.runs.is_none(),
206            "Can't use `with_highlights` and `with_default_highlights`"
207        );
208        self.delayed_highlights = Some(
209            highlights
210                .into_iter()
211                .inspect(|(run, _)| {
212                    debug_assert!(self.text.is_char_boundary(run.start));
213                    debug_assert!(self.text.is_char_boundary(run.end));
214                })
215                .collect::<Vec<_>>(),
216        );
217        self
218    }
219
220    fn compute_runs(
221        text: &str,
222        default_style: &TextStyle,
223        highlights: impl IntoIterator<Item = (Range<usize>, HighlightStyle)>,
224    ) -> Vec<TextRun> {
225        let mut runs = Vec::new();
226        let mut ix = 0;
227        for (range, highlight) in highlights {
228            if ix < range.start {
229                debug_assert!(text.is_char_boundary(range.start));
230                runs.push(default_style.clone().to_run(range.start - ix));
231            }
232            debug_assert!(text.is_char_boundary(range.end));
233            runs.push(
234                default_style
235                    .clone()
236                    .highlight(highlight)
237                    .to_run(range.len()),
238            );
239            ix = range.end;
240        }
241        if ix < text.len() {
242            runs.push(default_style.to_run(text.len() - ix));
243        }
244        runs
245    }
246
247    /// Override the font family for specific byte ranges of the text.
248    ///
249    /// This is resolved lazily at layout time, so the overrides are applied
250    /// on top of the inherited text style from the parent element.
251    /// Can be combined with [`with_highlights`](Self::with_highlights).
252    ///
253    /// The overrides must be sorted by range start and non-overlapping.
254    /// Each override range must fall on character boundaries.
255    pub fn with_font_family_overrides(
256        mut self,
257        overrides: impl IntoIterator<Item = (Range<usize>, SharedString)>,
258    ) -> Self {
259        self.delayed_font_family_overrides = Some(
260            overrides
261                .into_iter()
262                .inspect(|(range, _)| {
263                    debug_assert!(self.text.is_char_boundary(range.start));
264                    debug_assert!(self.text.is_char_boundary(range.end));
265                })
266                .collect(),
267        );
268        self
269    }
270
271    fn apply_font_family_overrides(
272        runs: &mut [TextRun],
273        overrides: &[(Range<usize>, SharedString)],
274    ) {
275        let mut byte_offset = 0;
276        let mut override_idx = 0;
277        for run in runs.iter_mut() {
278            let run_end = byte_offset + run.len;
279            while override_idx < overrides.len() && overrides[override_idx].0.end <= byte_offset {
280                override_idx += 1;
281            }
282            if override_idx < overrides.len() {
283                let (ref range, ref family) = overrides[override_idx];
284                if byte_offset >= range.start && run_end <= range.end {
285                    run.font.family = family.clone();
286                }
287            }
288            byte_offset = run_end;
289        }
290    }
291
292    /// Set the text runs for this piece of text.
293    pub fn with_runs(mut self, runs: Vec<TextRun>) -> Self {
294        let mut text = &*self.text;
295        for run in &runs {
296            text = text.get(run.len..).unwrap_or_else(|| {
297                #[cfg(debug_assertions)]
298                panic!("invalid text run. Text: '{text}', run: {run:?}");
299                #[cfg(not(debug_assertions))]
300                panic!("invalid text run");
301            });
302        }
303        assert!(text.is_empty(), "invalid text run");
304        self.runs = Some(runs);
305        self
306    }
307}
308
309impl Element for StyledText {
310    type RequestLayoutState = ();
311    type PrepaintState = ();
312
313    fn id(&self) -> Option<ElementId> {
314        None
315    }
316
317    fn source_location(&self) -> Option<&'static core::panic::Location<'static>> {
318        None
319    }
320
321    fn request_layout(
322        &mut self,
323        _id: Option<&GlobalElementId>,
324        _inspector_id: Option<&InspectorElementId>,
325        window: &mut Window,
326        cx: &mut App,
327    ) -> (LayoutId, Self::RequestLayoutState) {
328        let font_family_overrides = self.delayed_font_family_overrides.take();
329        let mut runs = self.runs.take().or_else(|| {
330            self.delayed_highlights.take().map(|delayed_highlights| {
331                Self::compute_runs(&self.text, &window.text_style(), delayed_highlights)
332            })
333        });
334
335        if let Some(ref overrides) = font_family_overrides {
336            let runs =
337                runs.get_or_insert_with(|| vec![window.text_style().to_run(self.text.len())]);
338            Self::apply_font_family_overrides(runs, overrides);
339        }
340
341        let layout_id = self.layout.layout(self.text.clone(), runs, window, cx);
342        (layout_id, ())
343    }
344
345    fn prepaint(
346        &mut self,
347        _id: Option<&GlobalElementId>,
348        _inspector_id: Option<&InspectorElementId>,
349        bounds: Bounds<Pixels>,
350        _: &mut Self::RequestLayoutState,
351        _window: &mut Window,
352        _cx: &mut App,
353    ) {
354        self.layout.prepaint(bounds, &self.text)
355    }
356
357    fn paint(
358        &mut self,
359        _id: Option<&GlobalElementId>,
360        _inspector_id: Option<&InspectorElementId>,
361        _bounds: Bounds<Pixels>,
362        _: &mut Self::RequestLayoutState,
363        _: &mut Self::PrepaintState,
364        window: &mut Window,
365        cx: &mut App,
366    ) {
367        self.layout.paint(&self.text, window, cx)
368    }
369}
370
371impl IntoElement for StyledText {
372    type Element = Self;
373
374    fn into_element(self) -> Self::Element {
375        self
376    }
377}
378
379/// The Layout for TextElement. This can be used to map indices to pixels and vice versa.
380#[derive(Default, Clone)]
381pub struct TextLayout(Rc<RefCell<Option<TextLayoutInner>>>);
382
383struct TextLayoutInner {
384    len: usize,
385    lines: SmallVec<[WrappedLine; 1]>,
386    line_height: Pixels,
387    wrap_width: Option<Pixels>,
388    size: Option<Size<Pixels>>,
389    bounds: Option<Bounds<Pixels>>,
390}
391
392impl TextLayout {
393    fn layout(
394        &self,
395        text: SharedString,
396        runs: Option<Vec<TextRun>>,
397        window: &mut Window,
398        _: &mut App,
399    ) -> LayoutId {
400        let text_style = window.text_style();
401        let font_size = text_style.font_size.to_pixels(window.rem_size());
402        let line_height = window.pixel_snap(
403            text_style
404                .line_height
405                .to_pixels(font_size.into(), window.rem_size()),
406        );
407
408        let runs = if let Some(runs) = runs {
409            runs
410        } else {
411            vec![text_style.to_run(text.len())]
412        };
413        window.request_measured_layout(Default::default(), {
414            let element_state = self.clone();
415
416            move |known_dimensions, available_space, window, cx| {
417                let wrap_width = if text_style.white_space == WhiteSpace::Normal {
418                    known_dimensions.width.or(match available_space.width {
419                        crate::AvailableSpace::Definite(x) => Some(x),
420                        _ => None,
421                    })
422                } else {
423                    None
424                };
425
426                let (truncate_width, truncation_affix, truncate_from) =
427                    if let Some(text_overflow) = text_style.text_overflow.clone() {
428                        let width = known_dimensions.width.or(match available_space.width {
429                            crate::AvailableSpace::Definite(x) => match text_style.line_clamp {
430                                Some(max_lines) => Some(x * max_lines),
431                                None => Some(x),
432                            },
433                            _ => None,
434                        });
435
436                        match text_overflow {
437                            TextOverflow::Truncate(s) => (width, s, TruncateFrom::End),
438                            TextOverflow::TruncateStart(s) => (width, s, TruncateFrom::Start),
439                        }
440                    } else {
441                        (None, "".into(), TruncateFrom::End)
442                    };
443
444                // Only use cached layout if:
445                // 1. We have a cached size
446                // 2. wrap_width matches (or both are None)
447                // 3. truncate_width is None (if truncate_width is Some, we need to re-layout
448                //    because the previous layout may have been computed without truncation)
449                if let Some(text_layout) = element_state.0.borrow().as_ref()
450                    && let Some(size) = text_layout.size
451                    && (wrap_width.is_none() || wrap_width == text_layout.wrap_width)
452                    && truncate_width.is_none()
453                {
454                    return size;
455                }
456
457                let mut line_wrapper = cx.text_system().line_wrapper(text_style.font(), font_size);
458                let (text, runs) = if let Some(truncate_width) = truncate_width {
459                    line_wrapper.truncate_line(
460                        text.clone(),
461                        truncate_width,
462                        &truncation_affix,
463                        &runs,
464                        truncate_from,
465                    )
466                } else {
467                    (text.clone(), Cow::Borrowed(&*runs))
468                };
469                let len = text.len();
470
471                let Some(lines) = window
472                    .text_system()
473                    .shape_text(
474                        text,
475                        font_size,
476                        &runs,
477                        wrap_width,            // Wrap if we know the width.
478                        text_style.line_clamp, // Limit the number of lines if line_clamp is set.
479                    )
480                    .log_err()
481                else {
482                    element_state.0.borrow_mut().replace(TextLayoutInner {
483                        lines: Default::default(),
484                        len: 0,
485                        line_height,
486                        wrap_width,
487                        size: Some(Size::default()),
488                        bounds: None,
489                    });
490                    return Size::default();
491                };
492
493                let mut size: Size<Pixels> = Size::default();
494                for line in &lines {
495                    let line_size = line.size(line_height);
496                    size.height += line_size.height;
497                    size.width = size.width.max(line_size.width).ceil();
498                }
499
500                element_state.0.borrow_mut().replace(TextLayoutInner {
501                    lines,
502                    len,
503                    line_height,
504                    wrap_width,
505                    size: Some(size),
506                    bounds: None,
507                });
508
509                size
510            }
511        })
512    }
513
514    fn prepaint(&self, bounds: Bounds<Pixels>, text: &str) {
515        let mut element_state = self.0.borrow_mut();
516        let element_state = element_state
517            .as_mut()
518            .with_context(|| format!("measurement has not been performed on {text}"))
519            .unwrap();
520        element_state.bounds = Some(bounds);
521    }
522
523    fn paint(&self, text: &str, window: &mut Window, cx: &mut App) {
524        let element_state = self.0.borrow();
525        let element_state = element_state
526            .as_ref()
527            .with_context(|| format!("measurement has not been performed on {text}"))
528            .unwrap();
529        let bounds = element_state
530            .bounds
531            .with_context(|| format!("prepaint has not been performed on {text}"))
532            .unwrap();
533
534        let line_height = element_state.line_height;
535        let mut line_origin = bounds.origin;
536        let text_style = window.text_style();
537        for line in &element_state.lines {
538            line.paint_background(
539                line_origin,
540                line_height,
541                text_style.text_align,
542                Some(bounds),
543                window,
544                cx,
545            )
546            .log_err();
547            line.paint(
548                line_origin,
549                line_height,
550                text_style.text_align,
551                Some(bounds),
552                window,
553                cx,
554            )
555            .log_err();
556            line_origin.y += line.size(line_height).height;
557        }
558    }
559
560    /// Get the byte index into the input of the pixel position.
561    pub fn index_for_position(&self, mut position: Point<Pixels>) -> Result<usize, usize> {
562        let element_state = self.0.borrow();
563        let element_state = element_state
564            .as_ref()
565            .expect("measurement has not been performed");
566        let bounds = element_state
567            .bounds
568            .expect("prepaint has not been performed");
569
570        if position.y < bounds.top() {
571            return Err(0);
572        }
573
574        let line_height = element_state.line_height;
575        let mut line_origin = bounds.origin;
576        let mut line_start_ix = 0;
577        for line in &element_state.lines {
578            let line_bottom = line_origin.y + line.size(line_height).height;
579            if position.y > line_bottom {
580                line_origin.y = line_bottom;
581                line_start_ix += line.len() + 1;
582            } else {
583                let position_within_line = position - line_origin;
584                match line.index_for_position(position_within_line, line_height) {
585                    Ok(index_within_line) => return Ok(line_start_ix + index_within_line),
586                    Err(index_within_line) => return Err(line_start_ix + index_within_line),
587                }
588            }
589        }
590
591        Err(line_start_ix.saturating_sub(1))
592    }
593
594    /// Get the pixel position for the given byte index.
595    pub fn position_for_index(&self, index: usize) -> Option<Point<Pixels>> {
596        let element_state = self.0.borrow();
597        let element_state = element_state
598            .as_ref()
599            .expect("measurement has not been performed");
600        let bounds = element_state
601            .bounds
602            .expect("prepaint has not been performed");
603        let line_height = element_state.line_height;
604
605        let mut line_origin = bounds.origin;
606        let mut line_start_ix = 0;
607
608        for line in &element_state.lines {
609            let line_end_ix = line_start_ix + line.len();
610            if index < line_start_ix {
611                break;
612            } else if index > line_end_ix {
613                line_origin.y += line.size(line_height).height;
614                line_start_ix = line_end_ix + 1;
615                continue;
616            } else {
617                let ix_within_line = index - line_start_ix;
618                return Some(line_origin + line.position_for_index(ix_within_line, line_height)?);
619            }
620        }
621
622        None
623    }
624
625    /// Retrieve the layout for the line containing the given byte index.
626    pub fn line_layout_for_index(&self, index: usize) -> Option<Arc<WrappedLineLayout>> {
627        let element_state = self.0.borrow();
628        let element_state = element_state
629            .as_ref()
630            .expect("measurement has not been performed");
631        let bounds = element_state
632            .bounds
633            .expect("prepaint has not been performed");
634        let line_height = element_state.line_height;
635
636        let mut line_origin = bounds.origin;
637        let mut line_start_ix = 0;
638
639        for line in &element_state.lines {
640            let line_end_ix = line_start_ix + line.len();
641            if index < line_start_ix {
642                break;
643            } else if index > line_end_ix {
644                line_origin.y += line.size(line_height).height;
645                line_start_ix = line_end_ix + 1;
646                continue;
647            } else {
648                return Some(line.layout.clone());
649            }
650        }
651
652        None
653    }
654
655    /// The per-(wrapped-)line layouts for the whole text, in order. Used by
656    /// consumers that map source ranges to on-screen segments across lines
657    /// (e.g. selection / search-highlight bounds computation).
658    pub fn line_layouts(&self) -> SmallVec<[Arc<WrappedLineLayout>; 1]> {
659        self.0
660            .borrow()
661            .as_ref()
662            .expect("measurement has not been performed")
663            .lines
664            .iter()
665            .map(|line| line.layout.clone())
666            .collect()
667    }
668
669    /// The bounds of this layout.
670    pub fn bounds(&self) -> Bounds<Pixels> {
671        self.0.borrow().as_ref().unwrap().bounds.unwrap()
672    }
673
674    /// The line height for this layout.
675    pub fn line_height(&self) -> Pixels {
676        self.0.borrow().as_ref().unwrap().line_height
677    }
678
679    /// The UTF-8 length of the underlying text.
680    pub fn len(&self) -> usize {
681        self.0.borrow().as_ref().unwrap().len
682    }
683
684    /// The text for this layout.
685    pub fn text(&self) -> String {
686        self.0
687            .borrow()
688            .as_ref()
689            .unwrap()
690            .lines
691            .iter()
692            .map(|s| &s.text)
693            .join("\n")
694    }
695
696    /// The text for this layout (with soft-wraps as newlines)
697    pub fn wrapped_text(&self) -> String {
698        let mut accumulator = String::new();
699
700        for wrapped in self.0.borrow().as_ref().unwrap().lines.iter() {
701            let mut seen = 0;
702            for boundary in wrapped.layout.wrap_boundaries.iter() {
703                let index = wrapped.layout.unwrapped_layout.runs[boundary.run_ix].glyphs
704                    [boundary.glyph_ix]
705                    .index;
706
707                accumulator.push_str(&wrapped.text[seen..index]);
708                accumulator.push('\n');
709                seen = index;
710            }
711            accumulator.push_str(&wrapped.text[seen..]);
712            accumulator.push('\n');
713        }
714        // Remove trailing newline
715        accumulator.pop();
716        accumulator
717    }
718}
719
720/// A text element that can be interacted with.
721pub struct InteractiveText {
722    element_id: ElementId,
723    text: StyledText,
724    click_listener:
725        Option<Box<dyn Fn(&[Range<usize>], InteractiveTextClickEvent, &mut Window, &mut App)>>,
726    hover_listener: Option<Box<dyn Fn(Option<usize>, MouseMoveEvent, &mut Window, &mut App)>>,
727    tooltip_builder: Option<Rc<dyn Fn(usize, &mut Window, &mut App) -> Option<AnyView>>>,
728    tooltip_id: Option<TooltipId>,
729    clickable_ranges: Vec<Range<usize>>,
730}
731
732struct InteractiveTextClickEvent {
733    mouse_down_index: usize,
734    mouse_up_index: usize,
735}
736
737#[doc(hidden)]
738#[derive(Default)]
739pub struct InteractiveTextState {
740    mouse_down_index: Rc<Cell<Option<usize>>>,
741    hovered_index: Rc<Cell<Option<usize>>>,
742    active_tooltip: Rc<RefCell<Option<ActiveTooltip>>>,
743}
744
745/// InteractiveTest is a wrapper around StyledText that adds mouse interactions.
746impl InteractiveText {
747    /// Creates a new InteractiveText from the given text.
748    pub fn new(id: impl Into<ElementId>, text: StyledText) -> Self {
749        Self {
750            element_id: id.into(),
751            text,
752            click_listener: None,
753            hover_listener: None,
754            tooltip_builder: None,
755            tooltip_id: None,
756            clickable_ranges: Vec::new(),
757        }
758    }
759
760    /// on_click is called when the user clicks on one of the given ranges, passing the index of
761    /// the clicked range.
762    pub fn on_click(
763        mut self,
764        ranges: Vec<Range<usize>>,
765        listener: impl Fn(usize, &mut Window, &mut App) + 'static,
766    ) -> Self {
767        self.click_listener = Some(Box::new(move |ranges, event, window, cx| {
768            for (range_ix, range) in ranges.iter().enumerate() {
769                if range.contains(&event.mouse_down_index) && range.contains(&event.mouse_up_index)
770                {
771                    listener(range_ix, window, cx);
772                }
773            }
774        }));
775        self.clickable_ranges = ranges;
776        self
777    }
778
779    /// on_hover is called when the mouse moves over a character within the text, passing the
780    /// index of the hovered character, or None if the mouse leaves the text.
781    pub fn on_hover(
782        mut self,
783        listener: impl Fn(Option<usize>, MouseMoveEvent, &mut Window, &mut App) + 'static,
784    ) -> Self {
785        self.hover_listener = Some(Box::new(listener));
786        self
787    }
788
789    /// tooltip lets you specify a tooltip for a given character index in the string.
790    pub fn tooltip(
791        mut self,
792        builder: impl Fn(usize, &mut Window, &mut App) -> Option<AnyView> + 'static,
793    ) -> Self {
794        self.tooltip_builder = Some(Rc::new(builder));
795        self
796    }
797}
798
799impl Element for InteractiveText {
800    type RequestLayoutState = ();
801    type PrepaintState = Hitbox;
802
803    fn id(&self) -> Option<ElementId> {
804        Some(self.element_id.clone())
805    }
806
807    fn source_location(&self) -> Option<&'static core::panic::Location<'static>> {
808        None
809    }
810
811    fn request_layout(
812        &mut self,
813        _id: Option<&GlobalElementId>,
814        inspector_id: Option<&InspectorElementId>,
815        window: &mut Window,
816        cx: &mut App,
817    ) -> (LayoutId, Self::RequestLayoutState) {
818        self.text.request_layout(None, inspector_id, window, cx)
819    }
820
821    fn prepaint(
822        &mut self,
823        global_id: Option<&GlobalElementId>,
824        inspector_id: Option<&InspectorElementId>,
825        bounds: Bounds<Pixels>,
826        state: &mut Self::RequestLayoutState,
827        window: &mut Window,
828        cx: &mut App,
829    ) -> Hitbox {
830        window.with_optional_element_state::<InteractiveTextState, _>(
831            global_id,
832            |interactive_state, window| {
833                let mut interactive_state = interactive_state
834                    .map(|interactive_state| interactive_state.unwrap_or_default());
835
836                if let Some(interactive_state) = interactive_state.as_mut() {
837                    if self.tooltip_builder.is_some() {
838                        self.tooltip_id =
839                            set_tooltip_on_window(&interactive_state.active_tooltip, window);
840                    } else {
841                        // If there is no longer a tooltip builder, remove the active tooltip.
842                        interactive_state.active_tooltip.take();
843                    }
844                }
845
846                self.text
847                    .prepaint(None, inspector_id, bounds, state, window, cx);
848                let hitbox = window.insert_hitbox(bounds, HitboxBehavior::Normal);
849                (hitbox, interactive_state)
850            },
851        )
852    }
853
854    fn paint(
855        &mut self,
856        global_id: Option<&GlobalElementId>,
857        inspector_id: Option<&InspectorElementId>,
858        bounds: Bounds<Pixels>,
859        _: &mut Self::RequestLayoutState,
860        hitbox: &mut Hitbox,
861        window: &mut Window,
862        cx: &mut App,
863    ) {
864        let current_view = window.current_view();
865        let text_layout = self.text.layout().clone();
866        window.with_element_state::<InteractiveTextState, _>(
867            global_id.unwrap(),
868            |interactive_state, window| {
869                let mut interactive_state = interactive_state.unwrap_or_default();
870                if let Some(click_listener) = self.click_listener.take() {
871                    let mouse_position = window.mouse_position();
872                    if let Ok(ix) = text_layout.index_for_position(mouse_position)
873                        && self
874                            .clickable_ranges
875                            .iter()
876                            .any(|range| range.contains(&ix))
877                    {
878                        window.set_cursor_style(crate::CursorStyle::PointingHand, hitbox)
879                    }
880
881                    let text_layout = text_layout.clone();
882                    let mouse_down = interactive_state.mouse_down_index.clone();
883                    if let Some(mouse_down_index) = mouse_down.get() {
884                        let hitbox = hitbox.clone();
885                        let clickable_ranges = mem::take(&mut self.clickable_ranges);
886                        window.on_mouse_event(
887                            move |event: &MouseUpEvent, phase, window: &mut Window, cx| {
888                                if phase == DispatchPhase::Bubble && hitbox.is_hovered(window) {
889                                    if let Ok(mouse_up_index) =
890                                        text_layout.index_for_position(event.position)
891                                    {
892                                        click_listener(
893                                            &clickable_ranges,
894                                            InteractiveTextClickEvent {
895                                                mouse_down_index,
896                                                mouse_up_index,
897                                            },
898                                            window,
899                                            cx,
900                                        )
901                                    }
902
903                                    mouse_down.take();
904                                    window.refresh();
905                                }
906                            },
907                        );
908                    } else {
909                        let hitbox = hitbox.clone();
910                        window.on_mouse_event(move |event: &MouseDownEvent, phase, window, _| {
911                            if phase == DispatchPhase::Bubble
912                                && hitbox.is_hovered(window)
913                                && let Ok(mouse_down_index) =
914                                    text_layout.index_for_position(event.position)
915                            {
916                                mouse_down.set(Some(mouse_down_index));
917                                window.refresh();
918                            }
919                        });
920                    }
921                }
922
923                window.on_mouse_event({
924                    let mut hover_listener = self.hover_listener.take();
925                    let hitbox = hitbox.clone();
926                    let text_layout = text_layout.clone();
927                    let hovered_index = interactive_state.hovered_index.clone();
928                    move |event: &MouseMoveEvent, phase, window, cx| {
929                        if phase == DispatchPhase::Bubble && hitbox.is_hovered(window) {
930                            let current = hovered_index.get();
931                            let updated = text_layout.index_for_position(event.position).ok();
932                            if current != updated {
933                                hovered_index.set(updated);
934                                if let Some(hover_listener) = hover_listener.as_ref() {
935                                    hover_listener(updated, event.clone(), window, cx);
936                                }
937                                cx.notify(current_view);
938                            }
939                        }
940                    }
941                });
942
943                if let Some(tooltip_builder) = self.tooltip_builder.clone() {
944                    let active_tooltip = interactive_state.active_tooltip.clone();
945                    let build_tooltip = Rc::new({
946                        let tooltip_is_hoverable = false;
947                        let text_layout = text_layout.clone();
948                        move |window: &mut Window, cx: &mut App| {
949                            text_layout
950                                .index_for_position(window.mouse_position())
951                                .ok()
952                                .and_then(|position| tooltip_builder(position, window, cx))
953                                .map(|view| (view, tooltip_is_hoverable))
954                        }
955                    });
956
957                    // Use bounds instead of testing hitbox since this is called during prepaint.
958                    let check_is_hovered_during_prepaint = Rc::new({
959                        let source_bounds = hitbox.bounds;
960                        let text_layout = text_layout.clone();
961                        let pending_mouse_down = interactive_state.mouse_down_index.clone();
962                        move |window: &Window| {
963                            text_layout
964                                .index_for_position(window.mouse_position())
965                                .is_ok()
966                                && source_bounds.contains(&window.mouse_position())
967                                && pending_mouse_down.get().is_none()
968                        }
969                    });
970
971                    let check_is_hovered = Rc::new({
972                        let hitbox = hitbox.clone();
973                        let text_layout = text_layout.clone();
974                        let pending_mouse_down = interactive_state.mouse_down_index.clone();
975                        move |window: &Window| {
976                            text_layout
977                                .index_for_position(window.mouse_position())
978                                .is_ok()
979                                && hitbox.is_hovered(window)
980                                && pending_mouse_down.get().is_none()
981                        }
982                    });
983
984                    register_tooltip_mouse_handlers(
985                        &active_tooltip,
986                        self.tooltip_id,
987                        build_tooltip,
988                        check_is_hovered,
989                        check_is_hovered_during_prepaint,
990                        window,
991                    );
992                }
993
994                self.text
995                    .paint(None, inspector_id, bounds, &mut (), &mut (), window, cx);
996
997                ((), interactive_state)
998            },
999        );
1000    }
1001}
1002
1003impl IntoElement for InteractiveText {
1004    type Element = Self;
1005
1006    fn into_element(self) -> Self::Element {
1007        self
1008    }
1009}
1010
1011#[cfg(test)]
1012mod tests {
1013    #[test]
1014    fn test_into_element_for() {
1015        use crate::{ParentElement as _, SharedString, div};
1016        use std::borrow::Cow;
1017
1018        let _ = div().child("static str");
1019        let _ = div().child("String".to_string());
1020        let _ = div().child(Cow::Borrowed("Cow"));
1021        let _ = div().child(SharedString::from("SharedString"));
1022    }
1023}