Skip to content

Commit 132fd89

Browse files
fix the segmented control crop issue
1 parent e640128 commit 132fd89

File tree

3 files changed

+73
-10
lines changed

3 files changed

+73
-10
lines changed

maui/src/SegmentedControl/Helper/SegmentViewHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ internal static double GetActualSegmentWidth(ISegmentItemInfo segmentInfo, doubl
6363
// Calculated width based on widthRequest and maxWidth.
6464
if (widthRequest < 0)
6565
{
66-
return totalWidth > maxWidth ? maxWidth - keyFocusedViewPadding : totalWidth;
66+
return totalWidth + keyFocusedViewPadding > maxWidth ? maxWidth - keyFocusedViewPadding : totalWidth;
6767
}
6868

6969
// Left and right of the keyboard layout in order to avoid the control crop issue while given the heightrequest.

maui/src/SegmentedControl/Views/SegmentItemView.cs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,6 @@ void DrawRoundedRectangle(ICanvas canvas, RectF dirtyRect)
246246
canvas.Antialias = true;
247247
float strokeRadius = (float)itemInfo.StrokeThickness / 2f;
248248
CornerRadius cornerRadius = itemInfo.SegmentCornerRadius;
249-
250249
// Calculate corner radius values, subtracting stroke radius when there's stroke thickness
251250
float cornerRadiusTopLeft = itemInfo.StrokeThickness > 0 ? (float)cornerRadius.TopLeft - strokeRadius : (float)cornerRadius.TopLeft;
252251
float cornerRadiusTopRight = itemInfo.StrokeThickness > 0 ? (float)cornerRadius.TopRight - strokeRadius : (float)cornerRadius.TopRight;
@@ -258,14 +257,6 @@ void DrawRoundedRectangle(ICanvas canvas, RectF dirtyRect)
258257
Brush background = isEnabled ? SegmentViewHelper.GetSegmentBackground(itemInfo, _segmentItem) : itemInfo.DisabledSegmentBackground;
259258
canvas.FillColor = SegmentViewHelper.BrushToColorConverter(background);
260259
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-
}
268-
269260
canvas.CanvasRestoreState();
270261
}
271262

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

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -895,6 +895,78 @@ public void GetActualSegmentWidth_ReturnsExpectedWidth(double widthRequest, doub
895895
Assert.Equal(expectedResult, resultWidth);
896896
}
897897

898+
[Theory]
899+
[InlineData(100.0, 50.0, 200.0, LayoutAlignment.Fill, 94.0)]
900+
[InlineData(-100.0, 50.0, 200.0, LayoutAlignment.Fill, 194.0)]
901+
[InlineData(100.0, 50.0, 100.0, LayoutAlignment.Start, 94.0)]
902+
[InlineData(-100.0, 50.0, 200.0, LayoutAlignment.Start, 50.0)]
903+
904+
public void GetActualSegmentWidth_ReturnsExpectedWidth_WithStrokethicknessZero(double widthRequest, double minWidth, double maxWidth, LayoutAlignment alignment, double expectedResult)
905+
{
906+
var segmentInfo = new SfSegmentedControl
907+
{
908+
_items = [new SfSegmentItem { }, new SfSegmentItem { }],
909+
VisibleSegmentsCount = 2,
910+
StrokeThickness = 0
911+
};
912+
var resultWidth = SegmentViewHelper.GetActualSegmentWidth(segmentInfo, widthRequest, minWidth, maxWidth, alignment);
913+
Assert.Equal(expectedResult, resultWidth);
914+
}
915+
916+
[Theory]
917+
[InlineData(100.0, 50.0, 200.0, LayoutAlignment.Fill, 94.0)]
918+
[InlineData(-100.0, 50.0, 200.0, LayoutAlignment.Fill, 194.0)]
919+
[InlineData(100.0, 50.0, 100.0, LayoutAlignment.Start, 94.0)]
920+
[InlineData(-100.0, 50.0, 200.0, LayoutAlignment.Start, 50.0)]
921+
922+
public void GetActualSegmentWidth_DifferentCount_ReturnsExpectedWidth_WithStrokethicknessZero(double widthRequest, double minWidth, double maxWidth, LayoutAlignment alignment, double expectedResult)
923+
{
924+
var segmentInfo = new SfSegmentedControl
925+
{
926+
_items = [new SfSegmentItem { }, new SfSegmentItem { }],
927+
VisibleSegmentsCount = 1,
928+
StrokeThickness = 0
929+
};
930+
var resultWidth = SegmentViewHelper.GetActualSegmentWidth(segmentInfo, widthRequest, minWidth, maxWidth, alignment);
931+
Assert.Equal(expectedResult, resultWidth);
932+
}
933+
934+
[Theory]
935+
[InlineData(100.0, 50.0, 200.0, LayoutAlignment.Fill, 94.0)]
936+
[InlineData(-100.0, 50.0, 200.0, LayoutAlignment.Fill, 194.0)]
937+
[InlineData(100.0, 50.0, 100.0, LayoutAlignment.Start, 94.0)]
938+
[InlineData(-100.0, 50.0, 200.0, LayoutAlignment.Start, 194.0)]
939+
940+
public void GetActualSegmentWidth_NegativeCount_ReturnsExpectedWidth_WithStrokethicknessZero(double widthRequest, double minWidth, double maxWidth, LayoutAlignment alignment, double expectedResult)
941+
{
942+
var segmentInfo = new SfSegmentedControl
943+
{
944+
_items = [new SfSegmentItem { }, new SfSegmentItem { }],
945+
VisibleSegmentsCount = -1,
946+
StrokeThickness = 0
947+
};
948+
var resultWidth = SegmentViewHelper.GetActualSegmentWidth(segmentInfo, widthRequest, minWidth, maxWidth, alignment);
949+
Assert.Equal(expectedResult, resultWidth);
950+
}
951+
952+
[Theory]
953+
[InlineData(100.0, 50.0, 200.0, LayoutAlignment.Fill, 94.0)]
954+
[InlineData(-100.0, 50.0, 200.0, LayoutAlignment.Fill, 194.0)]
955+
[InlineData(100.0, 50.0, 100.0, LayoutAlignment.Start, 94.0)]
956+
[InlineData(-100.0, 50.0, 200.0, LayoutAlignment.Start, 194.0)]
957+
958+
public void GetActualSegmentWidth_NegativeCount_ReturnsExpectedWidth_WithStrokethickness(double widthRequest, double minWidth, double maxWidth, LayoutAlignment alignment, double expectedResult)
959+
{
960+
var segmentInfo = new SfSegmentedControl
961+
{
962+
_items = [new SfSegmentItem { }, new SfSegmentItem { }],
963+
VisibleSegmentsCount = -1,
964+
StrokeThickness = 1
965+
};
966+
var resultWidth = SegmentViewHelper.GetActualSegmentWidth(segmentInfo, widthRequest, minWidth, maxWidth, alignment);
967+
Assert.Equal(expectedResult, resultWidth);
968+
}
969+
898970
[Theory]
899971
[InlineData(LayoutAlignment.Fill)]
900972
[InlineData(LayoutAlignment.Start)]

0 commit comments

Comments
 (0)