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
153pub 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 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 pub fn layout(&self) -> &TextLayout {
180 &self.layout
181 }
182
183 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 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 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 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#[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 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, text_style.line_clamp, )
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 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 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 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 pub fn bounds(&self) -> Bounds<Pixels> {
657 self.0.borrow().as_ref().unwrap().bounds.unwrap()
658 }
659
660 pub fn line_height(&self) -> Pixels {
662 self.0.borrow().as_ref().unwrap().line_height
663 }
664
665 pub fn len(&self) -> usize {
667 self.0.borrow().as_ref().unwrap().len
668 }
669
670 pub fn text(&self) -> String {
672 self.0
673 .borrow()
674 .as_ref()
675 .unwrap()
676 .lines
677 .iter()
678 .map(|s| &s.text)
679 .join("\n")
680 }
681
682 pub fn wrapped_text(&self) -> String {
684 let mut accumulator = String::new();
685
686 for wrapped in self.0.borrow().as_ref().unwrap().lines.iter() {
687 let mut seen = 0;
688 for boundary in wrapped.layout.wrap_boundaries.iter() {
689 let index = wrapped.layout.unwrapped_layout.runs[boundary.run_ix].glyphs
690 [boundary.glyph_ix]
691 .index;
692
693 accumulator.push_str(&wrapped.text[seen..index]);
694 accumulator.push('\n');
695 seen = index;
696 }
697 accumulator.push_str(&wrapped.text[seen..]);
698 accumulator.push('\n');
699 }
700 accumulator.pop();
702 accumulator
703 }
704}
705
706pub struct InteractiveText {
708 element_id: ElementId,
709 text: StyledText,
710 click_listener:
711 Option<Box<dyn Fn(&[Range<usize>], InteractiveTextClickEvent, &mut Window, &mut App)>>,
712 hover_listener: Option<Box<dyn Fn(Option<usize>, MouseMoveEvent, &mut Window, &mut App)>>,
713 tooltip_builder: Option<Rc<dyn Fn(usize, &mut Window, &mut App) -> Option<AnyView>>>,
714 tooltip_id: Option<TooltipId>,
715 clickable_ranges: Vec<Range<usize>>,
716}
717
718struct InteractiveTextClickEvent {
719 mouse_down_index: usize,
720 mouse_up_index: usize,
721}
722
723#[doc(hidden)]
724#[derive(Default)]
725pub struct InteractiveTextState {
726 mouse_down_index: Rc<Cell<Option<usize>>>,
727 hovered_index: Rc<Cell<Option<usize>>>,
728 active_tooltip: Rc<RefCell<Option<ActiveTooltip>>>,
729}
730
731impl InteractiveText {
733 pub fn new(id: impl Into<ElementId>, text: StyledText) -> Self {
735 Self {
736 element_id: id.into(),
737 text,
738 click_listener: None,
739 hover_listener: None,
740 tooltip_builder: None,
741 tooltip_id: None,
742 clickable_ranges: Vec::new(),
743 }
744 }
745
746 pub fn on_click(
749 mut self,
750 ranges: Vec<Range<usize>>,
751 listener: impl Fn(usize, &mut Window, &mut App) + 'static,
752 ) -> Self {
753 self.click_listener = Some(Box::new(move |ranges, event, window, cx| {
754 for (range_ix, range) in ranges.iter().enumerate() {
755 if range.contains(&event.mouse_down_index) && range.contains(&event.mouse_up_index)
756 {
757 listener(range_ix, window, cx);
758 }
759 }
760 }));
761 self.clickable_ranges = ranges;
762 self
763 }
764
765 pub fn on_hover(
768 mut self,
769 listener: impl Fn(Option<usize>, MouseMoveEvent, &mut Window, &mut App) + 'static,
770 ) -> Self {
771 self.hover_listener = Some(Box::new(listener));
772 self
773 }
774
775 pub fn tooltip(
777 mut self,
778 builder: impl Fn(usize, &mut Window, &mut App) -> Option<AnyView> + 'static,
779 ) -> Self {
780 self.tooltip_builder = Some(Rc::new(builder));
781 self
782 }
783}
784
785impl Element for InteractiveText {
786 type RequestLayoutState = ();
787 type PrepaintState = Hitbox;
788
789 fn id(&self) -> Option<ElementId> {
790 Some(self.element_id.clone())
791 }
792
793 fn source_location(&self) -> Option<&'static core::panic::Location<'static>> {
794 None
795 }
796
797 fn request_layout(
798 &mut self,
799 _id: Option<&GlobalElementId>,
800 inspector_id: Option<&InspectorElementId>,
801 window: &mut Window,
802 cx: &mut App,
803 ) -> (LayoutId, Self::RequestLayoutState) {
804 self.text.request_layout(None, inspector_id, window, cx)
805 }
806
807 fn prepaint(
808 &mut self,
809 global_id: Option<&GlobalElementId>,
810 inspector_id: Option<&InspectorElementId>,
811 bounds: Bounds<Pixels>,
812 state: &mut Self::RequestLayoutState,
813 window: &mut Window,
814 cx: &mut App,
815 ) -> Hitbox {
816 window.with_optional_element_state::<InteractiveTextState, _>(
817 global_id,
818 |interactive_state, window| {
819 let mut interactive_state = interactive_state
820 .map(|interactive_state| interactive_state.unwrap_or_default());
821
822 if let Some(interactive_state) = interactive_state.as_mut() {
823 if self.tooltip_builder.is_some() {
824 self.tooltip_id =
825 set_tooltip_on_window(&interactive_state.active_tooltip, window);
826 } else {
827 interactive_state.active_tooltip.take();
829 }
830 }
831
832 self.text
833 .prepaint(None, inspector_id, bounds, state, window, cx);
834 let hitbox = window.insert_hitbox(bounds, HitboxBehavior::Normal);
835 (hitbox, interactive_state)
836 },
837 )
838 }
839
840 fn paint(
841 &mut self,
842 global_id: Option<&GlobalElementId>,
843 inspector_id: Option<&InspectorElementId>,
844 bounds: Bounds<Pixels>,
845 _: &mut Self::RequestLayoutState,
846 hitbox: &mut Hitbox,
847 window: &mut Window,
848 cx: &mut App,
849 ) {
850 let current_view = window.current_view();
851 let text_layout = self.text.layout().clone();
852 window.with_element_state::<InteractiveTextState, _>(
853 global_id.unwrap(),
854 |interactive_state, window| {
855 let mut interactive_state = interactive_state.unwrap_or_default();
856 if let Some(click_listener) = self.click_listener.take() {
857 let mouse_position = window.mouse_position();
858 if let Ok(ix) = text_layout.index_for_position(mouse_position)
859 && self
860 .clickable_ranges
861 .iter()
862 .any(|range| range.contains(&ix))
863 {
864 window.set_cursor_style(crate::CursorStyle::PointingHand, hitbox)
865 }
866
867 let text_layout = text_layout.clone();
868 let mouse_down = interactive_state.mouse_down_index.clone();
869 if let Some(mouse_down_index) = mouse_down.get() {
870 let hitbox = hitbox.clone();
871 let clickable_ranges = mem::take(&mut self.clickable_ranges);
872 window.on_mouse_event(
873 move |event: &MouseUpEvent, phase, window: &mut Window, cx| {
874 if phase == DispatchPhase::Bubble && hitbox.is_hovered(window) {
875 if let Ok(mouse_up_index) =
876 text_layout.index_for_position(event.position)
877 {
878 click_listener(
879 &clickable_ranges,
880 InteractiveTextClickEvent {
881 mouse_down_index,
882 mouse_up_index,
883 },
884 window,
885 cx,
886 )
887 }
888
889 mouse_down.take();
890 window.refresh();
891 }
892 },
893 );
894 } else {
895 let hitbox = hitbox.clone();
896 window.on_mouse_event(move |event: &MouseDownEvent, phase, window, _| {
897 if phase == DispatchPhase::Bubble
898 && hitbox.is_hovered(window)
899 && let Ok(mouse_down_index) =
900 text_layout.index_for_position(event.position)
901 {
902 mouse_down.set(Some(mouse_down_index));
903 window.refresh();
904 }
905 });
906 }
907 }
908
909 window.on_mouse_event({
910 let mut hover_listener = self.hover_listener.take();
911 let hitbox = hitbox.clone();
912 let text_layout = text_layout.clone();
913 let hovered_index = interactive_state.hovered_index.clone();
914 move |event: &MouseMoveEvent, phase, window, cx| {
915 if phase == DispatchPhase::Bubble && hitbox.is_hovered(window) {
916 let current = hovered_index.get();
917 let updated = text_layout.index_for_position(event.position).ok();
918 if current != updated {
919 hovered_index.set(updated);
920 if let Some(hover_listener) = hover_listener.as_ref() {
921 hover_listener(updated, event.clone(), window, cx);
922 }
923 cx.notify(current_view);
924 }
925 }
926 }
927 });
928
929 if let Some(tooltip_builder) = self.tooltip_builder.clone() {
930 let active_tooltip = interactive_state.active_tooltip.clone();
931 let build_tooltip = Rc::new({
932 let tooltip_is_hoverable = false;
933 let text_layout = text_layout.clone();
934 move |window: &mut Window, cx: &mut App| {
935 text_layout
936 .index_for_position(window.mouse_position())
937 .ok()
938 .and_then(|position| tooltip_builder(position, window, cx))
939 .map(|view| (view, tooltip_is_hoverable))
940 }
941 });
942
943 let check_is_hovered_during_prepaint = Rc::new({
945 let source_bounds = hitbox.bounds;
946 let text_layout = text_layout.clone();
947 let pending_mouse_down = interactive_state.mouse_down_index.clone();
948 move |window: &Window| {
949 text_layout
950 .index_for_position(window.mouse_position())
951 .is_ok()
952 && source_bounds.contains(&window.mouse_position())
953 && pending_mouse_down.get().is_none()
954 }
955 });
956
957 let check_is_hovered = Rc::new({
958 let hitbox = hitbox.clone();
959 let text_layout = text_layout.clone();
960 let pending_mouse_down = interactive_state.mouse_down_index.clone();
961 move |window: &Window| {
962 text_layout
963 .index_for_position(window.mouse_position())
964 .is_ok()
965 && hitbox.is_hovered(window)
966 && pending_mouse_down.get().is_none()
967 }
968 });
969
970 register_tooltip_mouse_handlers(
971 &active_tooltip,
972 self.tooltip_id,
973 build_tooltip,
974 check_is_hovered,
975 check_is_hovered_during_prepaint,
976 window,
977 );
978 }
979
980 self.text
981 .paint(None, inspector_id, bounds, &mut (), &mut (), window, cx);
982
983 ((), interactive_state)
984 },
985 );
986 }
987}
988
989impl IntoElement for InteractiveText {
990 type Element = Self;
991
992 fn into_element(self) -> Self::Element {
993 self
994 }
995}
996
997#[cfg(test)]
998mod tests {
999 #[test]
1000 fn test_into_element_for() {
1001 use crate::{ParentElement as _, SharedString, div};
1002 use std::borrow::Cow;
1003
1004 let _ = div().child("static str");
1005 let _ = div().child("String".to_string());
1006 let _ = div().child(Cow::Borrowed("Cow"));
1007 let _ = div().child(SharedString::from("SharedString"));
1008 }
1009}