use std::time::Duration;
use gpui_kit::component::tooltip::Tooltip;
use gpui_kit::prelude::FluentBuilder as _;
use gpui_kit::{
AnyElement, AnyView, App, ElementId, Entity, InteractiveElement as _, IntoElement,
ParentElement as _, Pixels, Refineable as _, RenderOnce, SharedString,
StatefulInteractiveElement as _, StyleRefinement, Styled, Window, canvas, div, px,
};
pub const TOOLTIP_DELAY: Duration = Duration::from_millis(600);
const TOOLTIP_WIDTH: f32 = 480.;
const TOLERANCE: f32 = 1.;
const UNMEASURED: f32 = 0.;
pub fn is_cut(text_width: f32, available: f32) -> bool {
if available <= UNMEASURED || text_width <= UNMEASURED {
return false;
}
text_width > available + TOLERANCE
}
pub fn text(id: impl Into<ElementId>, text: impl Into<SharedString>) -> Shortened {
Shortened {
id: id.into(),
text: text.into(),
full: None,
child: None,
wrap: false,
style: StyleRefinement::default(),
}
}
pub fn styled(
id: impl Into<ElementId>,
label: impl Into<SharedString>,
child: impl IntoElement,
) -> Shortened {
Shortened {
child: Some(child.into_any_element()),
..text(id, label)
}
}
#[derive(IntoElement)]
pub struct Shortened {
id: ElementId,
text: SharedString,
full: Option<SharedString>,
child: Option<AnyElement>,
wrap: bool,
style: StyleRefinement,
}
impl Shortened {
pub fn wrap(mut self, wrap: bool) -> Self {
self.wrap = wrap;
self
}
pub fn full(mut self, full: impl Into<SharedString>) -> Self {
self.full = Some(full.into());
self
}
}
impl Styled for Shortened {
fn style(&mut self) -> &mut StyleRefinement {
&mut self.style
}
}
impl RenderOnce for Shortened {
fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
let cut = window.use_keyed_state(self.id.clone(), cx, |_, _| false);
let full = self.full.unwrap_or_else(|| self.text.clone());
let offers = !self.wrap && (*cut.read(cx) || full != self.text);
let wrap = self.wrap;
let width = shaped_width(window, &self.text);
let shown_text = self
.child
.unwrap_or_else(|| self.text.clone().into_any_element());
let mut shown = div()
.id(self.id)
.relative()
.min_w_0()
.map(|shown| {
if wrap {
shown.whitespace_normal()
} else {
shown.overflow_hidden().whitespace_nowrap().text_ellipsis()
}
})
.child(shown_text)
.when(!wrap, |shown| shown.child(measure(width, cut)))
.when(offers, |shown| {
shown
.tooltip(move |window, cx| wrapped(full.clone(), window, cx))
.tooltip_show_delay(TOOLTIP_DELAY)
});
shown.style().refine(&self.style);
shown
}
}
fn measure(width: Pixels, cut: Entity<bool>) -> impl IntoElement {
canvas(
move |bounds, _, cx| {
let measured = is_cut(f32::from(width), f32::from(bounds.size.width));
cut.update(cx, |cut, cx| {
if *cut != measured {
*cut = measured;
cx.notify();
}
});
},
|_, (), _, _| {},
)
.absolute()
.size_full()
}
fn shaped_width(window: &Window, text: &SharedString) -> Pixels {
let style = window.text_style();
let font_size = style.font_size.to_pixels(window.rem_size());
let line: SharedString = first_line(text).to_owned().into();
let run = style.to_run(line.len());
window
.text_system()
.shape_line(line, font_size, &[run], None)
.width
}
pub fn first_line(text: &str) -> &str {
text.split('\n').next().unwrap_or_default()
}
fn wrapped(full: SharedString, window: &mut Window, cx: &mut App) -> AnyView {
Tooltip::element(move |_, _| {
div()
.max_w(px(TOOLTIP_WIDTH))
.whitespace_normal()
.child(full.clone())
})
.build(window, cx)
}
#[cfg(test)]
mod tests {
use super::{first_line, is_cut};
#[test]
fn only_text_wider_than_its_row_is_cut() {
assert!(is_cut(200., 120.));
assert!(!is_cut(100., 120.));
assert!(!is_cut(120., 120.), "text that just fits is not cut");
}
#[test]
fn rounding_between_shaping_and_layout_is_not_a_cut() {
assert!(!is_cut(120.5, 120.));
assert!(is_cut(122., 120.));
}
#[test]
fn an_unmeasured_row_offers_no_tooltip() {
assert!(!is_cut(200., 0.), "before the first layout");
assert!(!is_cut(0., 120.), "empty text");
assert!(!is_cut(200., -5.), "a row with no room at all");
}
#[test]
fn a_row_measures_the_line_it_can_show() {
assert_eq!(first_line("one line"), "one line");
assert_eq!(first_line("first\nsecond"), "first");
assert_eq!(first_line(""), "");
}
}