Skip to content

Fix button auto-size text measurement#7

Merged
SteffenCarlsen merged 1 commit into
mainfrom
feature/button-measured-text-sizing
Jun 5, 2026
Merged

Fix button auto-size text measurement#7
SteffenCarlsen merged 1 commit into
mainfrom
feature/button-measured-text-sizing

Conversation

@SteffenCarlsen

Copy link
Copy Markdown
Owner

Summary

  • Fix Button auto-size measurement to use the active UI text measurement path instead of a character-count heuristic when a render measurement context is available.
  • Add a regression test proving wide measured text expands the auto-sized button bounds.
  • Record the reusable retained-UI text measurement lesson in Lessons.md.

Root cause

Button.RenderCore centered and drew text using measured glyph width, but Button.MeasureCore sized text-only buttons with Text.Length * fontSize * 0.6f. With proportional or otherwise wide measured text, an auto-sized button could report a much narrower desired width than the text it later rendered.

Validation

  • dotnet test tests\ModernOverlay.Tests\ModernOverlay.Tests.csproj --configuration Debug --filter "FullyQualifiedName~ButtonAutoSizeUsesMeasuredTextWidth"
  • dotnet test tests\ModernOverlay.Tests\ModernOverlay.Tests.csproj --configuration Debug --filter "FullyQualifiedName~OverlayUiButtonControlTests"
  • dotnet test tests\ModernOverlay.Tests\ModernOverlay.Tests.csproj --configuration Debug --logger trx
  • dotnet test tests\ModernOverlay.Tests\ModernOverlay.Tests.csproj --configuration Release --filter "FullyQualifiedName~ButtonAutoSizeUsesMeasuredTextWidth"
  • git diff --check

@SteffenCarlsen SteffenCarlsen marked this pull request as ready for review June 5, 2026 13:21
Copilot AI review requested due to automatic review settings June 5, 2026 13:21
@SteffenCarlsen SteffenCarlsen merged commit 7b2ef52 into main Jun 5, 2026
1 check passed

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Fixes a retained-UI layout inconsistency where Button auto-size width was computed via a character-count heuristic while rendering centered/drew text using measured glyph widths, which could cause text to overflow a “correctly rendered but incorrectly measured” button.

Changes:

  • Update Button.MeasureCore to use the retained UI root’s active text measurement path when available (falling back to the prior heuristic otherwise).
  • Add a regression test that proves measured wide text (“WWW”) expands an auto-sized button’s desired/bounds width.
  • Document the measurement/render consistency lesson in Lessons.md.

Reviewed changes

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

File Description
tests/ModernOverlay.Tests/OverlayUiButtonControlTests.cs Adds a Windows integration regression test and extends the test draw sink to simulate wide measured text.
src/ModernOverlay.UI/Controls.cs Updates Button auto-size measurement to prefer active measured text widths when a layout measurement context exists.
Lessons.md Records the retained-UI measurement/render consistency guideline for text-rendering controls.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 555 to 559
float fontSize = Root?.ThemeResources.Theme.FontSize ?? UiTheme.Default.FontSize;
float width = MathF.Min(availableSize.Width, Text.Length * fontSize * 0.6f + Padding.Horizontal);
float height = fontSize * 1.35f + Padding.Vertical;
SizeF textSize = MeasureButtonText(Text, fontSize);
float width = MathF.Min(availableSize.Width, textSize.Width + Padding.Horizontal);
float height = textSize.Height * 1.35f + Padding.Vertical;
return new SizeF(MathF.Max(MinWidth, width), MathF.Max(MinHeight, height));
Comment on lines +173 to +176
ui.Render(new DrawContext(new RecordingDrawCommandSink()));

Assert.AreEqual(140f, button.DesiredSize.Width, 0.001f);
Assert.AreEqual(140f, button.Bounds.Width, 0.001f);
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.

2 participants