Skip to content

Commit

Permalink
fix build, correctly size UIScrollViews
Browse files Browse the repository at this point in the history
Summary: Closes #626

Reviewed By: emilsjolander

Differential Revision: D5824425

Pulled By: splhack

fbshipit-source-id: e1a8dda5e86e2705afa7f6630a6757491a94c6d6
  • Loading branch information
Shaddix authored and facebook-github-bot committed Sep 19, 2017
1 parent bcc36cc commit 7217471
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 23 deletions.
8 changes: 5 additions & 3 deletions csharp/Facebook.Yoga/YogaNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -595,12 +595,14 @@ public void SetBaselineFunction(BaselineFunction baselineFunction)
Native.YGNodeSetBaselineFunc(_ygNode, _managedBaseline);
}

public void CalculateLayout()
public void CalculateLayout(
float width = YogaConstants.Undefined,
float height = YogaConstants.Undefined)
{
Native.YGNodeCalculateLayout(
_ygNode,
YogaConstants.Undefined,
YogaConstants.Undefined,
width,
height,
Native.YGNodeStyleGetDirection(_ygNode));
}

Expand Down
30 changes: 20 additions & 10 deletions csharp/Facebook.YogaKit/YogaLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Drawing;
#if __IOS__
using NativeView = UIKit.UIView;
using NativeScrollView = UIKit.UIScrollView;
#endif

namespace Facebook.YogaKit
Expand Down Expand Up @@ -699,12 +700,12 @@ public float AspectRatio
{
get
{
return _node.StyleAspectRatio;
return _node.AspectRatio;
}

set
{
_node.StyleAspectRatio = value;
_node.AspectRatio = value;
}
}

Expand All @@ -716,6 +717,17 @@ public void ApplyLayout()
float width = 0;
float height = 0;
GetWidthHeightOfNativeView(view, out width, out height);
if (view is NativeScrollView)
{
if (FlexDirection == YogaFlexDirection.Column || FlexDirection == YogaFlexDirection.ColumnReverse)
{
height = float.NaN;
}
else
{
width = float.NaN;
}
}
CalculateLayoutWithSize(this, width, height);
ApplyLayoutToViewHierarchy(view);
}
Expand Down Expand Up @@ -743,9 +755,7 @@ SizeF CalculateLayoutWithSize(YogaLayout layout, float width, float height)

var node = layout._node;

node.Width = width;
node.Height = height;
node.CalculateLayout();
node.CalculateLayout(width, height);

return new SizeF { Width = node.LayoutWidth, Height = node.LayoutHeight };
}
Expand Down Expand Up @@ -775,14 +785,14 @@ static float SanitizeMeasurement(float constrainedSize, float measuredSize, Yoga
float result;
if (measureMode == YogaMeasureMode.Exactly)
{
result = (float)constrainedSize;
result = constrainedSize;
}
else if (measureMode == YogaMeasureMode.AtMost)
{
result = (float)Math.Min(constrainedSize, measuredSize);
result = Math.Min(constrainedSize, measuredSize);
}
else {
result = (float)measuredSize;
result = measuredSize;
}

return result;
Expand Down Expand Up @@ -820,7 +830,7 @@ static void AttachNodesFromViewHierachy(NativeView view)
var subviewsToInclude = new List<NativeView>();
foreach (var subview in view.Subviews)
{
if (subview.Yoga().IsIncludeInLayout)
if (subview.Yoga().IsEnabled && subview.Yoga().IsIncludeInLayout)
{
subviewsToInclude.Add(subview);
}
Expand Down Expand Up @@ -856,7 +866,7 @@ static void RemoveAllChildren(YogaNode node)

static double RoundPointValue(float value)
{
float scale = NativePointScale;
float scale = NativePixelScale;

return Math.Round(value * scale) / scale;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
<MtouchDebug>true</MtouchDebug>
<MtouchFastDev>true</MtouchFastDev>
<MtouchProfiling>true</MtouchProfiling>
<MtouchUseSGen>true</MtouchUseSGen>
<MtouchUseRefCounting>true</MtouchUseRefCounting>
<IOSDebuggerPort>17481</IOSDebuggerPort>
<MtouchLink>None</MtouchLink>
<MtouchArch>x86_64</MtouchArch>
Expand All @@ -39,8 +37,6 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<CodesignKey>iPhone Developer</CodesignKey>
<MtouchUseSGen>true</MtouchUseSGen>
<MtouchUseRefCounting>true</MtouchUseRefCounting>
<MtouchFloat32>true</MtouchFloat32>
<CodesignEntitlements>Entitlements.plist</CodesignEntitlements>
<MtouchLink>SdkOnly</MtouchLink>
Expand All @@ -56,8 +52,6 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<CodesignKey>iPhone Developer</CodesignKey>
<MtouchUseSGen>true</MtouchUseSGen>
<MtouchUseRefCounting>true</MtouchUseRefCounting>
<MtouchLink>None</MtouchLink>
<MtouchArch>x86_64</MtouchArch>
<MtouchHttpClientHandler>HttpClientHandler</MtouchHttpClientHandler>
Expand All @@ -76,8 +70,6 @@
<MtouchDebug>true</MtouchDebug>
<MtouchFastDev>true</MtouchFastDev>
<MtouchProfiling>true</MtouchProfiling>
<MtouchUseSGen>true</MtouchUseSGen>
<MtouchUseRefCounting>true</MtouchUseRefCounting>
<MtouchFloat32>true</MtouchFloat32>
<CodesignEntitlements>Entitlements.plist</CodesignEntitlements>
<MtouchLink>SdkOnly</MtouchLink>
Expand All @@ -102,6 +94,7 @@
<ItemGroup>
<Compile Include="Main.cs" />
<Compile Include="UnitTestAppDelegate.cs" />
<Compile Include="YogaKitNativeTest.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Facebook.YogaKit.iOS\Facebook.YogaKit.iOS.csproj">
Expand Down
51 changes: 51 additions & 0 deletions csharp/iOS/Facebook.YogaKit.iOS.Tests/YogaKitNativeTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using System.Drawing;
using Facebook.Yoga;
using NUnit.Framework;
using System;
using UIKit;
using CoreGraphics;

namespace Facebook.YogaKit.iOS.Tests
{
[TestFixture]
public class YogaKitNativeTest
{
[Test]
public void ScrollViewVertical()
{
var view = new UIScrollView() {
Frame = new CGRect(0, 0, 100, 100),
};

view.Yoga().Overflow = YogaOverflow.Scroll;
var subview = new UIView();
subview.Yoga().Height = 1000;
subview.Yoga().IsEnabled = true;

view.AddSubview(subview);
view.Yoga().IsEnabled = true;
view.Yoga().ApplyLayout();
Assert.True(view.ContentSize.Height == 1000);
}

[Test]
public void NormalViewVertical()
{
var view = new UIView() {
Frame = new CGRect(0,0, 100, 100),
};


var subview = new UIView();
subview.Yoga().Height = 1000;
subview.Yoga().Width = 2;
subview.Yoga().IsEnabled = true;

view.AddSubview(subview);
view.Yoga().IsEnabled = true;
view.Yoga().ApplyLayout();
Assert.True(view.Bounds.Height == 100);
}

}
}
11 changes: 9 additions & 2 deletions csharp/iOS/Facebook.YogaKit.iOS/YogaLayout.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,21 @@ static void GetWidthHeightOfNativeView(UIView view, out float width, out float h
height = (float)view.Bounds.Height;
}

static float NativePixelScale => (float)UIScreen.MainScreen.Scale;
static float NativePixelScale => (float)UIScreen.MainScreen.Scale;


static void ApplyLayoutToNativeView(UIView view, YogaNode node)
{
var topLeft = new CGPoint(node.LayoutX, node.LayoutY);
var bottomRight = new CGPoint(topLeft.X + node.LayoutWidth, topLeft.Y + node.LayoutHeight);
view.Frame = new CGRect(RoundPointValue((float)topLeft.X), RoundPointValue((float)topLeft.Y), RoundPointValue((float)bottomRight.X) - RoundPointValue((float)topLeft.X), RoundPointValue((float)bottomRight.Y) - RoundPointValue((float)topLeft.Y));
if (view is UIScrollView scrollView)
{
scrollView.ContentSize = new CGSize(RoundPointValue((float)bottomRight.X) - RoundPointValue((float)topLeft.X), RoundPointValue((float)bottomRight.Y) - RoundPointValue((float)topLeft.Y));
}
else
{
view.Frame = new CGRect(RoundPointValue((float)topLeft.X), RoundPointValue((float)topLeft.Y), RoundPointValue((float)bottomRight.X) - RoundPointValue((float)topLeft.X), RoundPointValue((float)bottomRight.Y) - RoundPointValue((float)topLeft.Y));
}
}

bool _disposed;
Expand Down

0 comments on commit 7217471

Please sign in to comment.