Skip to content

Commit fa3de97

Browse files
Merge pull request #199 from syncfusion/copilot/fix-191
Fix SegmentBackground not being drawn when StrokeThickness is set to 0
2 parents 2ec60b5 + 81c02e3 commit fa3de97

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed

maui/src/SegmentedControl/Views/SegmentItemView.cs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -237,27 +237,36 @@ void OnSemanticsNodeClick(SemanticsNode node)
237237
/// <param name="dirtyRect">The area that needs to be redrawn.</param>
238238
void DrawRoundedRectangle(ICanvas canvas, RectF dirtyRect)
239239
{
240-
if (itemInfo?.SegmentTemplate != null || itemInfo == null || itemInfo.StrokeThickness == 0)
240+
if (itemInfo?.SegmentTemplate != null || itemInfo == null)
241241
{
242242
return;
243243
}
244244

245245
canvas.CanvasSaveState();
246246
canvas.Antialias = true;
247247
float strokeRadius = (float)itemInfo.StrokeThickness / 2f;
248-
canvas.StrokeSize = (float)itemInfo.StrokeThickness;
249-
canvas.StrokeColor = SegmentViewHelper.BrushToColorConverter(itemInfo.Stroke);
250248
CornerRadius cornerRadius = itemInfo.SegmentCornerRadius;
251249

252-
// Subtracting stroke radius value to resolve the stroke thickness cropping issue.
253-
float cornerRadiusTopLeft = (float)cornerRadius.TopLeft - strokeRadius;
254-
float cornerRadiusTopRight = (float)cornerRadius.TopRight - strokeRadius;
255-
float cornerRadiusBottomRight = (float)cornerRadius.BottomRight - strokeRadius;
256-
float cornerRadiusBottomLeft = (float)cornerRadius.BottomLeft - strokeRadius;
250+
// Calculate corner radius values, subtracting stroke radius when there's stroke thickness
251+
float cornerRadiusTopLeft = itemInfo.StrokeThickness > 0 ? (float)cornerRadius.TopLeft - strokeRadius : (float)cornerRadius.TopLeft;
252+
float cornerRadiusTopRight = itemInfo.StrokeThickness > 0 ? (float)cornerRadius.TopRight - strokeRadius : (float)cornerRadius.TopRight;
253+
float cornerRadiusBottomRight = itemInfo.StrokeThickness > 0 ? (float)cornerRadius.BottomRight - strokeRadius : (float)cornerRadius.BottomRight;
254+
float cornerRadiusBottomLeft = itemInfo.StrokeThickness > 0 ? (float)cornerRadius.BottomLeft - strokeRadius : (float)cornerRadius.BottomLeft;
255+
256+
// Always draw the background
257257
bool isEnabled = SegmentViewHelper.GetItemEnabled(itemInfo, _segmentItem);
258258
Brush background = isEnabled ? SegmentViewHelper.GetSegmentBackground(itemInfo, _segmentItem) : itemInfo.DisabledSegmentBackground;
259259
canvas.FillColor = SegmentViewHelper.BrushToColorConverter(background);
260260
canvas.FillRoundedRectangle(dirtyRect.Left, dirtyRect.Top, dirtyRect.Width, dirtyRect.Height, cornerRadiusTopLeft, cornerRadiusTopRight, cornerRadiusBottomRight, cornerRadiusBottomLeft);
261+
262+
// Only draw stroke if stroke thickness is greater than 0
263+
if (itemInfo.StrokeThickness > 0)
264+
{
265+
canvas.StrokeSize = (float)itemInfo.StrokeThickness;
266+
canvas.StrokeColor = SegmentViewHelper.BrushToColorConverter(itemInfo.Stroke);
267+
canvas.DrawRoundedRectangle(dirtyRect.Left, dirtyRect.Top, dirtyRect.Width, dirtyRect.Height, cornerRadiusTopLeft, cornerRadiusTopRight, cornerRadiusBottomRight, cornerRadiusBottomLeft);
268+
}
269+
261270
canvas.CanvasRestoreState();
262271
}
263272

maui/tests/Syncfusion.Maui.Toolkit.UnitTest/Buttons/SfSegmentedControlUnitTests.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,18 @@ public void SegmentBackground_SetValue_ReturnsExpectedValue(string colorHex)
219219
Assert.Equal(segmentBackground, segmentedControl.SegmentBackground);
220220
}
221221

222+
[Theory]
223+
[InlineData("#FF0000")]
224+
public void SegmentBackground_WithZeroStrokeThickness_ReturnsExpectedValue(string colorHex)
225+
{
226+
var segmentedControl = new SfSegmentedControl();
227+
Brush segmentBackground = new SolidColorBrush(Color.FromArgb(colorHex));
228+
segmentedControl.SegmentBackground = segmentBackground;
229+
segmentedControl.StrokeThickness = 0;
230+
Assert.Equal(segmentBackground, segmentedControl.SegmentBackground);
231+
Assert.Equal(0, segmentedControl.StrokeThickness);
232+
}
233+
222234
[Theory]
223235
[InlineData("#FF0000")]
224236
public void DisabledSegmentBackground_SetValue_ReturnsExpectedValue(string colorHex)
@@ -1124,6 +1136,16 @@ public void GetSegmentBackground_ReturnsItemInfoSegmentBackground_WhenSegmentIte
11241136
Assert.Equal(itemInfo.SegmentBackground, resultBackground);
11251137
}
11261138

1139+
[Fact]
1140+
public void GetSegmentBackground_ReturnsItemInfoSegmentBackground_WhenStrokeThicknessIsZero()
1141+
{
1142+
var itemInfo = new SfSegmentedControl { SegmentBackground = Brush.Red, StrokeThickness = 0 };
1143+
var segmentItem = new SfSegmentItem();
1144+
var resultBackground = SegmentViewHelper.GetSegmentBackground(itemInfo, segmentItem);
1145+
Assert.Equal(itemInfo.SegmentBackground, resultBackground);
1146+
Assert.Equal(0, itemInfo.StrokeThickness);
1147+
}
1148+
11271149
[Fact]
11281150
public void GetSegmentBackground_ReturnsTransparent_WhenBothSegmentItemAndItemInfoBackgroundsAreNull()
11291151
{

0 commit comments

Comments
 (0)