Skip to content

Add fyne.TextTruncateMiddle truncation mode#6388

Open
mohsenm4 wants to merge 1 commit into
fyne-io:developfrom
mohsenm4:feature/6353-text-truncate-middle
Open

Add fyne.TextTruncateMiddle truncation mode#6388
mohsenm4 wants to merge 1 commit into
fyne-io:developfrom
mohsenm4:feature/6353-text-truncate-middle

Conversation

@mohsenm4

Copy link
Copy Markdown
Contributor

Description

Adds fyne.TextTruncateMiddle alongside the existing Off/Clip/Ellipsis modes. 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.

label.Truncation = fyne.TextTruncateMiddle
// "/home/user/projects/fyne/widget/label.go"  →  "/home/user/…/widget/label.go"

Closes #6353

How

  • New TextTruncateMiddle constant in text.go.
  • middleTruncate helper in widget/richtext.go — binary search for the largest balanced prefix+ellipsis+suffix that fits the available width.
  • New displayText field on rowBoundary so the renderer can substitute a pre-computed string when the slice-based path doesn't fit the use case. MinSize matches the ellipsis mode.
  • Added a fourth option in the fyne_demo truncation radio so it's easy to try live.

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.

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
@coveralls

Copy link
Copy Markdown

Coverage Status

coverage: 59.915% (+0.05%) from 59.869% — mohsenm4:feature/6353-text-truncate-middle into fyne-io:develop

@andydotxyz andydotxyz added the after-release For when an ongoing release is finished. label Jun 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

after-release For when an ongoing release is finished.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support for fyne.TextTruncateMiddle

3 participants