Skip to content

Fixes #5127. Anchor button delimiters to edges; fix highlight continuity#5279

Merged
YourRobotOverlord merged 5 commits into
gui-cs:developfrom
YourRobotOverlord:fix-5127-button-edge-delimiters
May 9, 2026
Merged

Fixes #5127. Anchor button delimiters to edges; fix highlight continuity#5279
YourRobotOverlord merged 5 commits into
gui-cs:developfrom
YourRobotOverlord:fix-5127-button-edge-delimiters

Conversation

@YourRobotOverlord
Copy link
Copy Markdown
Collaborator

@YourRobotOverlord YourRobotOverlord commented May 8, 2026

Copilot Session

a98f7d36-f362-457b-8f86-885ba3e011bc

Fixes #5127.

Problem

When a Button has a fixed Width wider than its natural size:

  • The [ and ] delimiters were centered with the text instead of pinned to the view edges.
  • When focused, the highlight attribute had gaps — cells before/after the centered text used the default attribute rather than Focus.

Solution

Override OnDrawingText to take direct control of rendering:

  • Draw [ explicitly at drawRect.X (left edge) and ] at drawRect.X + Width - 1 (right edge).
  • Call Driver.FillRect(drawRect) with the normalAttr (which resolves to Focus when focused) before drawing text, ensuring continuous highlight across the entire button area including all rows.
  • Center only the interior text (between the brackets) using a dedicated _interiorTextFormatter.
  • UpdateTextFormatterText still sets TextFormatter.Text to the full decorated string so Dim.Auto sizing continues to work correctly.

Tests

Two new tests in ButtonDrawingTests:

  • FixedWidth_Anchors_Delimiters_To_Edges — verifies [ and ] appear at columns 0 and 9 for a Width=10 button, for both IsDefault=false and IsDefault=true.
  • Focused_FixedWidth_Button_Highlight_Is_Continuous — verifies all 10 cells carry Focus or HotFocus attribute with no gaps.

YourRobotOverlord and others added 3 commits May 7, 2026 20:31
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…t for full highlight

- Cache _interiorTextFormatter.GetDrawRegion result; pass to GetDelimiterRow
  instead of recomputing it, eliminating a redundant layout pass.
- Replace Driver.AddStr(new string(' ', width)) with Driver.FillRect(drawRect)
  so focus highlight covers all rows of a multi-row button, not just the text row.
- Remove redundant Region.Combine union (interior region is always a subset of drawRect).
- Add comment linking GetDecoratedText / GetInteriorText dual-path responsibility.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes Button rendering when a fixed Width is larger than the button’s natural text width, ensuring the bracket delimiters render at the view edges and that focused highlighting is continuous across the full button area.

Changes:

  • Override Button.OnDrawingText to explicitly render left/right delimiters at the content edges and pre-fill the content area with the appropriate attribute.
  • Introduce a dedicated _interiorTextFormatter to center and draw only the interior text between delimiters.
  • Add new driver-based unit tests validating delimiter placement and focused attribute continuity for a fixed-width button.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
Terminal.Gui/Views/Button.cs Customizes Button text drawing to anchor delimiters to edges and fill the button area for continuous focus highlight.
Tests/UnitTestsParallelizable/Views/ButtonDrawingTests.cs Adds tests for fixed-width delimiter placement and focused highlight continuity.

Comment thread Terminal.Gui/Views/Button.cs Outdated
Comment thread Terminal.Gui/Views/Button.cs Outdated
Comment thread Tests/UnitTestsParallelizable/Views/ButtonDrawingTests.cs
YourRobotOverlord and others added 2 commits May 8, 2026 17:46
…issing TextFormatter settings, add multi-row highlight test

- Guard every _interiorTextFormatter property assignment with a value-equality check
  so NeedsFormat is not set spuriously on unchanged values
- Mirror MultiLine, WordWrap, TabWidth, and PreserveTabs from TextFormatter
  to _interiorTextFormatter (previously unmirrored, potential behavioral regression)
- Add Focused_FixedWidth_Button_MultiRow_Highlight_Is_Continuous test verifying
  all rows carry the Focus attribute when Height > 1

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Adds Tests/Benchmarks/Views/ButtonDrawBenchmark.cs with two methods:
- DrawButton_Unchanged: steady-state draw (no property changes) — the
  guard-optimized path where NeedsFormat is never set spuriously
- DrawButton_TextChanging: text rotates each iteration — forces a real
  reformat, approximating the old unguarded behaviour

[MemoryDiagnoser] exposes the allocation difference between the two paths,
quantifying the benefit of the guards added in the preceding commit.

Run: dotnet run --project Tests/Benchmarks -c Release -- --filter "*ButtonDraw*"

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copy link
Copy Markdown
Member

@tig tig left a comment

Choose a reason for hiding this comment

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

lgtm

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.

When a button has a fixed size then the delimiters should render at edges

3 participants