Skip to content

Fix SegmentBackground not being drawn when StrokeThickness is set to 0 #199

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions maui/src/SegmentedControl/Views/SegmentItemView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -237,27 +237,36 @@ void OnSemanticsNodeClick(SemanticsNode node)
/// <param name="dirtyRect">The area that needs to be redrawn.</param>
void DrawRoundedRectangle(ICanvas canvas, RectF dirtyRect)
{
if (itemInfo?.SegmentTemplate != null || itemInfo == null || itemInfo.StrokeThickness == 0)
if (itemInfo?.SegmentTemplate != null || itemInfo == null)
{
return;
}

canvas.CanvasSaveState();
canvas.Antialias = true;
float strokeRadius = (float)itemInfo.StrokeThickness / 2f;
canvas.StrokeSize = (float)itemInfo.StrokeThickness;
canvas.StrokeColor = SegmentViewHelper.BrushToColorConverter(itemInfo.Stroke);
CornerRadius cornerRadius = itemInfo.SegmentCornerRadius;

// Subtracting stroke radius value to resolve the stroke thickness cropping issue.
float cornerRadiusTopLeft = (float)cornerRadius.TopLeft - strokeRadius;
float cornerRadiusTopRight = (float)cornerRadius.TopRight - strokeRadius;
float cornerRadiusBottomRight = (float)cornerRadius.BottomRight - strokeRadius;
float cornerRadiusBottomLeft = (float)cornerRadius.BottomLeft - strokeRadius;
// Calculate corner radius values, subtracting stroke radius when there's stroke thickness
float cornerRadiusTopLeft = itemInfo.StrokeThickness > 0 ? (float)cornerRadius.TopLeft - strokeRadius : (float)cornerRadius.TopLeft;
float cornerRadiusTopRight = itemInfo.StrokeThickness > 0 ? (float)cornerRadius.TopRight - strokeRadius : (float)cornerRadius.TopRight;
float cornerRadiusBottomRight = itemInfo.StrokeThickness > 0 ? (float)cornerRadius.BottomRight - strokeRadius : (float)cornerRadius.BottomRight;
float cornerRadiusBottomLeft = itemInfo.StrokeThickness > 0 ? (float)cornerRadius.BottomLeft - strokeRadius : (float)cornerRadius.BottomLeft;

// Always draw the background
bool isEnabled = SegmentViewHelper.GetItemEnabled(itemInfo, _segmentItem);
Brush background = isEnabled ? SegmentViewHelper.GetSegmentBackground(itemInfo, _segmentItem) : itemInfo.DisabledSegmentBackground;
canvas.FillColor = SegmentViewHelper.BrushToColorConverter(background);
canvas.FillRoundedRectangle(dirtyRect.Left, dirtyRect.Top, dirtyRect.Width, dirtyRect.Height, cornerRadiusTopLeft, cornerRadiusTopRight, cornerRadiusBottomRight, cornerRadiusBottomLeft);

// Only draw stroke if stroke thickness is greater than 0
if (itemInfo.StrokeThickness > 0)
{
canvas.StrokeSize = (float)itemInfo.StrokeThickness;
canvas.StrokeColor = SegmentViewHelper.BrushToColorConverter(itemInfo.Stroke);
canvas.DrawRoundedRectangle(dirtyRect.Left, dirtyRect.Top, dirtyRect.Width, dirtyRect.Height, cornerRadiusTopLeft, cornerRadiusTopRight, cornerRadiusBottomRight, cornerRadiusBottomLeft);
}

canvas.CanvasRestoreState();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,18 @@ public void SegmentBackground_SetValue_ReturnsExpectedValue(string colorHex)
Assert.Equal(segmentBackground, segmentedControl.SegmentBackground);
}

[Theory]
[InlineData("#FF0000")]
public void SegmentBackground_WithZeroStrokeThickness_ReturnsExpectedValue(string colorHex)
{
var segmentedControl = new SfSegmentedControl();
Brush segmentBackground = new SolidColorBrush(Color.FromArgb(colorHex));
segmentedControl.SegmentBackground = segmentBackground;
segmentedControl.StrokeThickness = 0;
Assert.Equal(segmentBackground, segmentedControl.SegmentBackground);
Assert.Equal(0, segmentedControl.StrokeThickness);
}

[Theory]
[InlineData("#FF0000")]
public void DisabledSegmentBackground_SetValue_ReturnsExpectedValue(string colorHex)
Expand Down Expand Up @@ -1124,6 +1136,16 @@ public void GetSegmentBackground_ReturnsItemInfoSegmentBackground_WhenSegmentIte
Assert.Equal(itemInfo.SegmentBackground, resultBackground);
}

[Fact]
public void GetSegmentBackground_ReturnsItemInfoSegmentBackground_WhenStrokeThicknessIsZero()
{
var itemInfo = new SfSegmentedControl { SegmentBackground = Brush.Red, StrokeThickness = 0 };
var segmentItem = new SfSegmentItem();
var resultBackground = SegmentViewHelper.GetSegmentBackground(itemInfo, segmentItem);
Assert.Equal(itemInfo.SegmentBackground, resultBackground);
Assert.Equal(0, itemInfo.StrokeThickness);
}

[Fact]
public void GetSegmentBackground_ReturnsTransparent_WhenBothSegmentItemAndItemInfoBackgroundsAreNull()
{
Expand Down