Skip to content

Commit

Permalink
chore: Fix rounding logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Youssef1313 committed Sep 21, 2024
1 parent ac2f937 commit 225bbba
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,6 @@ public async Task When_Rectangle_Rounded_Measure_Height()
Assert.AreEqual(146, nativeViewRect.Top);
Assert.AreEqual(293, nativeViewRect.Bottom);
Assert.AreEqual(293, nativeViewBottomBorder.Top);

Assert.IsNotNull(rect.FrameRoundingAdjustment);
Assert.AreEqual(1, rect.FrameRoundingAdjustment.Value.Height);
Assert.AreEqual(0, rect.FrameRoundingAdjustment.Value.Width);
}
}
}
Expand Down
13 changes: 1 addition & 12 deletions src/Uno.UI/Extensions/ViewHelper.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -237,18 +237,7 @@ public static int LogicalToPhysicalPixels(double value)
return int.MinValue;
}

// The rounding is explained here : http://developer.android.com/guide/practices/screens_support.html#dips-pels
//
// "This figure is the factor by which you should multiply the dp units on order to get the actual
// pixel count for the current screen. (Then add 0.5f to round the figure up to the nearest whole number,
// when converting to an integer.)"

if (value < 0)
{
return (int)Math.Ceiling(value * Scale);
}

return (int)((value * Scale) + .5f);
return (int)Math.Round(value * Scale);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,6 @@ private IDisposable InnerCreateLayers(

if (!fullCornerRadius.IsEmpty)
{
if (viewUIElement.FrameRoundingAdjustment is { } fra)
{
drawArea.Height += fra.Height;
drawArea.Width += fra.Width;
}

// This needs to be adjusted if multiple UI threads are used in the future for multi-window
fullCornerRadius.Outer.GetRadii(_outerRadiiStore);
fullCornerRadius.Inner.GetRadii(_innerRadiiStore);
Expand Down
23 changes: 2 additions & 21 deletions src/Uno.UI/UI/Xaml/Shapes/Shape.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -204,34 +204,15 @@ protected Android.Graphics.Path GetOrCreatePath()
return _path ?? new Android.Graphics.Path();
}

protected global::Windows.Foundation.Rect TransformToLogical(global::Windows.Foundation.Rect renderingArea)
{
//Android's path rendering logic rounds values down to the nearest int, make sure we round up here instead using the ViewHelper scaling logic
var physicalRenderingArea = renderingArea.LogicalToPhysicalPixels();
if (FrameRoundingAdjustment is { } fra)
{
physicalRenderingArea.Height += fra.Height;
physicalRenderingArea.Width += fra.Width;
}

var logicalRenderingArea = physicalRenderingArea.PhysicalToLogicalPixels();
logicalRenderingArea.X = renderingArea.X;
logicalRenderingArea.Y = renderingArea.Y;

return logicalRenderingArea;
}

protected global::Windows.Foundation.Size BasicArrangeOverride(global::Windows.Foundation.Size finalSize, Action<Android.Graphics.Path> action)
{
var (shapeSize, renderingArea) = ArrangeRelativeShape(finalSize);

if (renderingArea.Width > 0 && renderingArea.Height > 0)
{
var logicalRenderingArea = TransformToLogical(renderingArea);

if (!_logicalRenderingArea.Equals(logicalRenderingArea))
if (!_logicalRenderingArea.Equals(renderingArea))
{
_logicalRenderingArea = logicalRenderingArea;
_logicalRenderingArea = renderingArea;
Android.Graphics.Path path = GetOrCreatePath();
action(path);
Render(path);
Expand Down
24 changes: 5 additions & 19 deletions src/Uno.UI/UI/Xaml/UIElement.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,18 +125,6 @@ public UIElement()
InitializePointers();
}

/// <summary>
/// The difference between the physical layout width and height taking the origin into account,
/// and the physical width and height that would've been calculated for an origin of (0,0).
/// The difference may be -1,0, or +1 pixels due to different roundings.
///
/// (Eg, consider a Grid that is 31 logical pixels high, with 3 children with alignment Stretch in successive Star-sized rows.
/// Each child will be measured with a logical height of 10.3, and logical origins of 0, 10.3, and 20.6. Assume the device scale is 1.
/// The child origins will be converted to 0, 10, and 21 respectively in integer pixel values; this will give heights of 10, 11, and 10 pixels.
/// The FrameRoundingAdjustment values will be (0,0), (0,1), and (0,0) respectively.
/// </summary>
internal Size? FrameRoundingAdjustment { get; set; }

protected override void OnDraw(Android.Graphics.Canvas canvas)
{
if (m_pLayoutClipGeometry is { } clipRectLogical)
Expand Down Expand Up @@ -213,18 +201,16 @@ internal void ArrangeVisual(Rect finalRect, Rect? clippedFrame = default)
LayoutSlotWithMarginsAndAlignments = finalRect;

var physical = finalRect.LogicalToPhysicalPixels();
FrameRoundingAdjustment = new Size(
(int)physical.Width - physical.Width,
(int)physical.Height - physical.Height);

try
{
_isInArrangeVisualLayout = true;
global::System.Diagnostics.Debug.WriteLine($"Logical rect of {this.GetDebugName()}: {finalRect}, physical: {physical}");
this.Layout(
(int)physical.Left,
(int)physical.Top,
(int)physical.Right,
(int)physical.Bottom
(int)Math.Round(physical.Left),
(int)Math.Round(physical.Top),
(int)Math.Round(physical.Right),
(int)Math.Round(physical.Bottom)
);
}
finally
Expand Down

0 comments on commit 225bbba

Please sign in to comment.