@@ -2,8 +2,8 @@ use crate::{
ActiveTooltip, AnyView, App, Bounds, DispatchPhase, Element, ElementId, GlobalElementId,
HighlightStyle, Hitbox, HitboxBehavior, InspectorElementId, IntoElement, LayoutId,
MouseDownEvent, MouseMoveEvent, MouseUpEvent, Pixels, Point, SharedString, Size, TextOverflow,
- TextRun, TextStyle, TooltipId, WhiteSpace, Window, WrappedLine, WrappedLineLayout,
- register_tooltip_mouse_handlers, set_tooltip_on_window,
+ TextRun, TextStyle, TooltipId, TruncateFrom, WhiteSpace, Window, WrappedLine,
+ WrappedLineLayout, register_tooltip_mouse_handlers, set_tooltip_on_window,
};
use anyhow::Context as _;
use itertools::Itertools;
@@ -354,7 +354,7 @@ impl TextLayout {
None
};
- let (truncate_width, truncation_suffix) =
+ let (truncate_width, truncation_affix, truncate_from) =
if let Some(text_overflow) = text_style.text_overflow.clone() {
let width = known_dimensions.width.or(match available_space.width {
crate::AvailableSpace::Definite(x) => match text_style.line_clamp {
@@ -365,10 +365,11 @@ impl TextLayout {
});
match text_overflow {
- TextOverflow::Truncate(s) => (width, s),
+ TextOverflow::Truncate(s) => (width, s, TruncateFrom::End),
+ TextOverflow::TruncateStart(s) => (width, s, TruncateFrom::Start),
}
} else {
- (None, "".into())
+ (None, "".into(), TruncateFrom::End)
};
if let Some(text_layout) = element_state.0.borrow().as_ref()
@@ -383,8 +384,9 @@ impl TextLayout {
line_wrapper.truncate_line(
text.clone(),
truncate_width,
- &truncation_suffix,
+ &truncation_affix,
&runs,
+ truncate_from,
)
} else {
(text.clone(), Cow::Borrowed(&*runs))
@@ -2,6 +2,15 @@ use crate::{FontId, FontRun, Pixels, PlatformTextSystem, SharedString, TextRun,
use collections::HashMap;
use std::{borrow::Cow, iter, sync::Arc};
+/// Determines whether to truncate text from the start or end.
+#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
+pub enum TruncateFrom {
+ /// Truncate text from the start.
+ Start,
+ /// Truncate text from the end.
+ End,
+}
+
/// The GPUI line wrapper, used to wrap lines of text to a given width.
pub struct LineWrapper {
platform_text_system: Arc<dyn PlatformTextSystem>,
@@ -129,29 +138,50 @@ impl LineWrapper {
}
/// Determines if a line should be truncated based on its width.
+ ///
+ /// Returns the truncation index in `line`.
pub fn should_truncate_line(
&mut self,
line: &str,
truncate_width: Pixels,
- truncation_suffix: &str,
+ truncation_affix: &str,
+ truncate_from: TruncateFrom,
) -> Option<usize> {
let mut width = px(0.);
- let suffix_width = truncation_suffix
+ let suffix_width = truncation_affix
.chars()
.map(|c| self.width_for_char(c))
.fold(px(0.0), |a, x| a + x);
let mut truncate_ix = 0;
- for (ix, c) in line.char_indices() {
- if width + suffix_width < truncate_width {
- truncate_ix = ix;
+ match truncate_from {
+ TruncateFrom::Start => {
+ for (ix, c) in line.char_indices().rev() {
+ if width + suffix_width < truncate_width {
+ truncate_ix = ix;
+ }
+
+ let char_width = self.width_for_char(c);
+ width += char_width;
+
+ if width.floor() > truncate_width {
+ return Some(truncate_ix);
+ }
+ }
}
+ TruncateFrom::End => {
+ for (ix, c) in line.char_indices() {
+ if width + suffix_width < truncate_width {
+ truncate_ix = ix;
+ }
- let char_width = self.width_for_char(c);
- width += char_width;
+ let char_width = self.width_for_char(c);
+ width += char_width;
- if width.floor() > truncate_width {
- return Some(truncate_ix);
+ if width.floor() > truncate_width {
+ return Some(truncate_ix);
+ }
+ }
}
}
@@ -163,16 +193,23 @@ impl LineWrapper {
&mut self,
line: SharedString,
truncate_width: Pixels,
- truncation_suffix: &str,
+ truncation_affix: &str,
runs: &'a [TextRun],
+ truncate_from: TruncateFrom,
) -> (SharedString, Cow<'a, [TextRun]>) {
if let Some(truncate_ix) =
- self.should_truncate_line(&line, truncate_width, truncation_suffix)
+ self.should_truncate_line(&line, truncate_width, truncation_affix, truncate_from)
{
- let result =
- SharedString::from(format!("{}{}", &line[..truncate_ix], truncation_suffix));
+ let result = match truncate_from {
+ TruncateFrom::Start => {
+ SharedString::from(format!("{truncation_affix}{}", &line[truncate_ix + 1..]))
+ }
+ TruncateFrom::End => {
+ SharedString::from(format!("{}{truncation_affix}", &line[..truncate_ix]))
+ }
+ };
let mut runs = runs.to_vec();
- update_runs_after_truncation(&result, truncation_suffix, &mut runs);
+ update_runs_after_truncation(&result, truncation_affix, &mut runs, truncate_from);
(result, Cow::Owned(runs))
} else {
(line, Cow::Borrowed(runs))
@@ -245,15 +282,35 @@ impl LineWrapper {
}
}
-fn update_runs_after_truncation(result: &str, ellipsis: &str, runs: &mut Vec<TextRun>) {
+fn update_runs_after_truncation(
+ result: &str,
+ ellipsis: &str,
+ runs: &mut Vec<TextRun>,
+ truncate_from: TruncateFrom,
+) {
let mut truncate_at = result.len() - ellipsis.len();
- for (run_index, run) in runs.iter_mut().enumerate() {
- if run.len <= truncate_at {
- truncate_at -= run.len;
- } else {
- run.len = truncate_at + ellipsis.len();
- runs.truncate(run_index + 1);
- break;
+ match truncate_from {
+ TruncateFrom::Start => {
+ for (run_index, run) in runs.iter_mut().enumerate().rev() {
+ if run.len <= truncate_at {
+ truncate_at -= run.len;
+ } else {
+ run.len = truncate_at + ellipsis.len();
+ runs.splice(..run_index, std::iter::empty());
+ break;
+ }
+ }
+ }
+ TruncateFrom::End => {
+ for (run_index, run) in runs.iter_mut().enumerate() {
+ if run.len <= truncate_at {
+ truncate_at -= run.len;
+ } else {
+ run.len = truncate_at + ellipsis.len();
+ runs.truncate(run_index + 1);
+ break;
+ }
+ }
}
}
}
@@ -503,7 +560,7 @@ mod tests {
}
#[test]
- fn test_truncate_line() {
+ fn test_truncate_line_end() {
let mut wrapper = build_wrapper();
fn perform_test(
@@ -514,8 +571,13 @@ mod tests {
) {
let dummy_run_lens = vec![text.len()];
let dummy_runs = generate_test_runs(&dummy_run_lens);
- let (result, dummy_runs) =
- wrapper.truncate_line(text.into(), px(220.), ellipsis, &dummy_runs);
+ let (result, dummy_runs) = wrapper.truncate_line(
+ text.into(),
+ px(220.),
+ ellipsis,
+ &dummy_runs,
+ TruncateFrom::End,
+ );
assert_eq!(result, expected);
assert_eq!(dummy_runs.first().unwrap().len, result.len());
}
@@ -541,7 +603,50 @@ mod tests {
}
#[test]
- fn test_truncate_multiple_runs() {
+ fn test_truncate_line_start() {
+ let mut wrapper = build_wrapper();
+
+ fn perform_test(
+ wrapper: &mut LineWrapper,
+ text: &'static str,
+ expected: &'static str,
+ ellipsis: &str,
+ ) {
+ let dummy_run_lens = vec![text.len()];
+ let dummy_runs = generate_test_runs(&dummy_run_lens);
+ let (result, dummy_runs) = wrapper.truncate_line(
+ text.into(),
+ px(220.),
+ ellipsis,
+ &dummy_runs,
+ TruncateFrom::Start,
+ );
+ assert_eq!(result, expected);
+ assert_eq!(dummy_runs.first().unwrap().len, result.len());
+ }
+
+ perform_test(
+ &mut wrapper,
+ "aaaa bbbb cccc ddddd eeee fff gg",
+ "cccc ddddd eeee fff gg",
+ "",
+ );
+ perform_test(
+ &mut wrapper,
+ "aaaa bbbb cccc ddddd eeee fff gg",
+ "…ccc ddddd eeee fff gg",
+ "…",
+ );
+ perform_test(
+ &mut wrapper,
+ "aaaa bbbb cccc ddddd eeee fff gg",
+ "......dddd eeee fff gg",
+ "......",
+ );
+ }
+
+ #[test]
+ fn test_truncate_multiple_runs_end() {
let mut wrapper = build_wrapper();
fn perform_test(
@@ -554,7 +659,7 @@ mod tests {
) {
let dummy_runs = generate_test_runs(run_lens);
let (result, dummy_runs) =
- wrapper.truncate_line(text.into(), line_width, "…", &dummy_runs);
+ wrapper.truncate_line(text.into(), line_width, "…", &dummy_runs, TruncateFrom::End);
assert_eq!(result, expected);
for (run, result_len) in dummy_runs.iter().zip(result_run_len) {
assert_eq!(run.len, *result_len);
@@ -600,10 +705,75 @@ mod tests {
}
#[test]
- fn test_update_run_after_truncation() {
+ fn test_truncate_multiple_runs_start() {
+ let mut wrapper = build_wrapper();
+
+ #[track_caller]
+ fn perform_test(
+ wrapper: &mut LineWrapper,
+ text: &'static str,
+ expected: &str,
+ run_lens: &[usize],
+ result_run_len: &[usize],
+ line_width: Pixels,
+ ) {
+ let dummy_runs = generate_test_runs(run_lens);
+ let (result, dummy_runs) = wrapper.truncate_line(
+ text.into(),
+ line_width,
+ "…",
+ &dummy_runs,
+ TruncateFrom::Start,
+ );
+ assert_eq!(result, expected);
+ for (run, result_len) in dummy_runs.iter().zip(result_run_len) {
+ assert_eq!(run.len, *result_len);
+ }
+ }
+ // Case 0: Normal
+ // Text: abcdefghijkl
+ // Runs: Run0 { len: 12, ... }
+ //
+ // Truncate res: …ijkl (truncate_at = 9)
+ // Run res: Run0 { string: …ijkl, len: 7, ... }
+ perform_test(&mut wrapper, "abcdefghijkl", "…ijkl", &[12], &[7], px(50.));
+ // Case 1: Drop some runs
+ // Text: abcdefghijkl
+ // Runs: Run0 { len: 4, ... }, Run1 { len: 4, ... }, Run2 { len: 4, ... }
+ //
+ // Truncate res: …ghijkl (truncate_at = 7)
+ // Runs res: Run0 { string: …gh, len: 5, ... }, Run1 { string: ijkl, len:
+ // 4, ... }
+ perform_test(
+ &mut wrapper,
+ "abcdefghijkl",
+ "…ghijkl",
+ &[4, 4, 4],
+ &[5, 4],
+ px(70.),
+ );
+ // Case 2: Truncate at start of some run
+ // Text: abcdefghijkl
+ // Runs: Run0 { len: 4, ... }, Run1 { len: 4, ... }, Run2 { len: 4, ... }
+ //
+ // Truncate res: abcdefgh… (truncate_at = 3)
+ // Runs res: Run0 { string: …, len: 3, ... }, Run1 { string: efgh, len:
+ // 4, ... }, Run2 { string: ijkl, len: 4, ... }
+ perform_test(
+ &mut wrapper,
+ "abcdefghijkl",
+ "…efghijkl",
+ &[4, 4, 4],
+ &[3, 4, 4],
+ px(90.),
+ );
+ }
+
+ #[test]
+ fn test_update_run_after_truncation_end() {
fn perform_test(result: &str, run_lens: &[usize], result_run_lens: &[usize]) {
let mut dummy_runs = generate_test_runs(run_lens);
- update_runs_after_truncation(result, "…", &mut dummy_runs);
+ update_runs_after_truncation(result, "…", &mut dummy_runs, TruncateFrom::End);
for (run, result_len) in dummy_runs.iter().zip(result_run_lens) {
assert_eq!(run.len, *result_len);
}