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

@andydotxyz andydotxyz left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this, just some notes on naming / docs

Comment thread widget/richtext.go
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].

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Comment thread widget/richtext.go
// 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) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think "truncateMiddle" is a more natural name and fits with the "truncateLimit" below.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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"?

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support for fyne.TextTruncateMiddle

3 participants