cad-editor/src/ui/text_util.rs
Hakan Seven ff10f8a89a feat(ui): elide overflowing text, tooltip full layer name
iced has no built-in ellipsis, so long user content (file names, layer
names, paths, property values) wrapped or spilled past fixed-width
panels. Add an `elide` helper (char-budget truncation with a trailing
ellipsis) and apply it to the layer manager, properties panel, document
tabs, recent-files list and the save dialog.

In the layer manager a truncated name reveals its full text in a
cursor-following tooltip on hover, so the column need not be widened.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-09 19:04:18 +03:00

45 lines
1.3 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//! Shared helpers for fitting text into width-constrained widgets.
//!
//! iced has no built-in ellipsis, so long user content (file names, layer
//! names, paths, property values) would otherwise wrap and break row
//! heights or spill past fixed-width panels. `elide` trims to a character
//! budget and marks the cut with a single ellipsis.
/// Truncate `s` to at most `max` characters, replacing the overflow with a
/// trailing ellipsis. `max` includes the ellipsis character; `max == 0`
/// yields an empty string. Counts by `char` so multi-byte text is safe.
pub fn elide(s: &str, max: usize) -> String {
if s.chars().count() <= max {
return s.to_string();
}
if max == 0 {
return String::new();
}
let mut out: String = s.chars().take(max - 1).collect();
out.push('…');
out
}
#[cfg(test)]
mod tests {
use super::elide;
#[test]
fn short_unchanged() {
assert_eq!(elide("abc", 8), "abc");
assert_eq!(elide("abc", 3), "abc");
}
#[test]
fn long_truncated() {
assert_eq!(elide("abcdef", 4), "abc…");
assert_eq!(elide("abcdef", 1), "…");
assert_eq!(elide("abcdef", 0), "");
}
#[test]
fn multibyte_safe() {
// No panic on a char boundary and the budget counts chars.
assert_eq!(elide("çağrışım", 4), "çağ…");
}
}