Add fyne.TextTruncateMiddle truncation mode - #6388
Conversation
Adds a new TextTruncation value that shortens overflowing text by inserting an ellipsis in the middle while preserving the beginning and end. Useful for cases like file paths where both ends carry information. The truncated string is pre-computed during line layout and stored on rowBoundary.displayText, which the renderer prefers over the [begin:end] slice when present. MinSize matches the ellipsis mode since the minimum visible content is the same. Fixes fyne-io#6353
andydotxyz
left a comment
There was a problem hiding this comment.
Thanks for this, just some notes on naming / docs
| indent float32 | ||
| // displayText overrides the [begin:end] slice when rendering. It is used | ||
| // to substitute a pre-computed truncated string such as the prefix+…+suffix | ||
| // produced by [fyne.TextTruncateMiddle]. |
There was a problem hiding this comment.
Please don't put "where it is used" documentation into a field - documents work for purpose but the code itself expresses usage (and will vary over time)
| // measured. The third return value is true when the entire input fits without | ||
| // truncation. The prefix is the larger half when the total is odd, so a single | ||
| // character input is always kept as-is. | ||
| func middleTruncate(runes []rune, maxWidth float32, measurer func([]rune) fyne.Size) (int, int, bool) { |
There was a problem hiding this comment.
I think "truncateMiddle" is a more natural name and fits with the "truncateLimit" below.
There was a problem hiding this comment.
The last sentence of the document is confusing, why does the odd length relate to a single character? I don't think this is needed but if it is perhaps "when un-even the prefix will be larger than suffix"?
- Rename middleTruncate helper (and its test) to truncateMiddle for consistency with truncateLimit. - Drop the how-it-is-used sentence on the displayText field so the documentation only describes behaviour. - Drop the confusing odd-length caveat from truncateMiddle's doc.
|
|
||
| truncSize := fyne.NewSize(natural.Width/2, natural.Height) | ||
| label.Resize(truncSize) | ||
| label.Truncation = fyne.TextTruncateMiddle |
There was a problem hiding this comment.
I think if you move the Truncation assignment before the Resize() call you can save the Refresh().
Description
Adds
fyne.TextTruncateMiddlealongside the existingOff/Clip/Ellipsismodes. When the text overflows, it shortens with an ellipsis in the middle while preserving the beginning and end — handy for file paths, URLs, etc. where both ends carry information.Closes #6353
How
TextTruncateMiddleconstant intext.go.middleTruncatehelper inwidget/richtext.go— binary search for the largest balanced prefix+ellipsis+suffix that fits the available width.displayTextfield onrowBoundaryso the renderer can substitute a pre-computed string when the slice-based path doesn't fit the use case. MinSize matches the ellipsis mode.Tests
TestText_middleTruncate— helper edge cases (empty, fits whole, only ellipsis fits, balanced split).TestText_lineBounds_truncateMiddle— short text untouched, long text gets the ellipsis with original head/tail preserved.TestLabel_TruncateMiddle— end-to-end on a real Label, asserts the rendered canvas text.