Skip to content

Commit

Permalink
fix(shapes): Invalid shape positioning for 1px thickness
Browse files Browse the repository at this point in the history
  • Loading branch information
jeromelaban committed Oct 22, 2020
1 parent 0c9de5d commit f0f58c7
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/Uno.UI/UI/Xaml/Shapes/Shape.layout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,6 @@ private protected Size MeasureAbsoluteShape(Size availableSize, NativePath path)
var userSize = GetUserSizes();
var (userMinSize, userMaxSize) = GetMinMax(userSize);
var strokeThickness = StrokeThickness;
var halfStrokeThickness = GetHalfStrokeThickness();
var pathBounds = GetPathBoundingBox(path); // The BoundingBox does also contains bezier anchors even if out of geometry
var pathSize = (Size)pathBounds.Size;

Expand All @@ -242,6 +241,7 @@ private protected Size MeasureAbsoluteShape(Size availableSize, NativePath path)
{
default:
case Stretch.None:
var alignedHalfStrokeThickness = GetAlignedHalfStrokeThickness();
// If stretch is None, we have to keep the origin defined by the absolute coordinates of the path:
//
// This means that if you draw a line from 50,50 to 100,100 (so it's not starting at 0, 0),
Expand All @@ -267,8 +267,8 @@ private protected Size MeasureAbsoluteShape(Size availableSize, NativePath path)
// Note: The logic would say to include the full StrokeThickness as it will "overflow" half on booth side of the path,
// but WinUI does include only the half of it.
var pathNaturalSize = new Size(
pathBounds.X == 0 ? pathBounds.Width + strokeThickness : pathBounds.Right + halfStrokeThickness,
pathBounds.Y == 0 ? pathBounds.Height + strokeThickness : pathBounds.Bottom + halfStrokeThickness);
pathBounds.X == 0 ? pathBounds.Width + strokeThickness : pathBounds.Right + alignedHalfStrokeThickness,
pathBounds.Y == 0 ? pathBounds.Height + strokeThickness : pathBounds.Bottom + alignedHalfStrokeThickness);
size = pathNaturalSize.AtMost(userMaxSize).AtLeast(userMinSize); // The size defined on the Shape has priority over the size of the geometry itself!
break;

Expand Down Expand Up @@ -336,7 +336,7 @@ private protected Size ArrangeAbsoluteShape(Size finalSize, NativePath path)
var stretch = Stretch;
var userSize = GetUserSizes();
var strokeThickness = StrokeThickness;
var halfStrokeThickness = GetHalfStrokeThickness();
var halfStrokeThickness = strokeThickness / 2.0;
var pathBounds = GetPathBoundingBox(path); // The BoundingBox does also contains bezier anchors even if out of geometry
var pathSize = (Size)pathBounds.Size;

Expand All @@ -357,9 +357,10 @@ private protected Size ArrangeAbsoluteShape(Size finalSize, NativePath path)
{
default:
case Stretch.None:
var alignedHalfStrokeThickness = GetAlignedHalfStrokeThickness();
var pathNaturalSize = new Size(
pathBounds.X == 0 ? pathBounds.Width + strokeThickness : pathBounds.Right + halfStrokeThickness,
pathBounds.Y == 0 ? pathBounds.Height + strokeThickness : pathBounds.Bottom + halfStrokeThickness);
pathBounds.X == 0 ? pathBounds.Width + strokeThickness : pathBounds.Right + alignedHalfStrokeThickness,
pathBounds.Y == 0 ? pathBounds.Height + strokeThickness : pathBounds.Bottom + alignedHalfStrokeThickness);
var (userMinSize, userMaxSize) = GetMinMax(userSize);

var clampedSize = pathNaturalSize.AtMost(userMaxSize).AtLeast(userMinSize); // The size defined on the Shape has priority over the size of the geometry itself!
Expand Down Expand Up @@ -550,7 +551,7 @@ private protected Size ArrangeAbsoluteShape(Size finalSize, NativePath path)
/// <summary>
/// Gets the rounded/adjusted half stroke thickness that should be used for measuring absolute shapes (Path, Line, Polyline and Polygon)
/// </summary>
private double GetHalfStrokeThickness()
private double GetAlignedHalfStrokeThickness()
=> Math.Floor((ActualStrokeThickness + .5) / 2.0);

private
Expand Down

0 comments on commit f0f58c7

Please sign in to comment.