From 7217471427d6c8304c05c9ebd59f42eab3ee6590 Mon Sep 17 00:00:00 2001 From: Shaddix Date: Tue, 19 Sep 2017 09:29:21 -0700 Subject: [PATCH] fix build, correctly size UIScrollViews Summary: Closes https://github.com/facebook/yoga/pull/626 Reviewed By: emilsjolander Differential Revision: D5824425 Pulled By: splhack fbshipit-source-id: e1a8dda5e86e2705afa7f6630a6757491a94c6d6 --- csharp/Facebook.Yoga/YogaNode.cs | 8 +-- csharp/Facebook.YogaKit/YogaLayout.cs | 30 +++++++---- .../Facebook.YogaKit.iOS.Tests.csproj | 9 +--- .../YogaKitNativeTest.cs | 51 +++++++++++++++++++ csharp/iOS/Facebook.YogaKit.iOS/YogaLayout.cs | 11 +++- 5 files changed, 86 insertions(+), 23 deletions(-) create mode 100644 csharp/iOS/Facebook.YogaKit.iOS.Tests/YogaKitNativeTest.cs diff --git a/csharp/Facebook.Yoga/YogaNode.cs b/csharp/Facebook.Yoga/YogaNode.cs index 250a5db1..ddbdd371 100644 --- a/csharp/Facebook.Yoga/YogaNode.cs +++ b/csharp/Facebook.Yoga/YogaNode.cs @@ -595,12 +595,14 @@ namespace Facebook.Yoga 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)); } diff --git a/csharp/Facebook.YogaKit/YogaLayout.cs b/csharp/Facebook.YogaKit/YogaLayout.cs index d59c1b17..e38de96c 100644 --- a/csharp/Facebook.YogaKit/YogaLayout.cs +++ b/csharp/Facebook.YogaKit/YogaLayout.cs @@ -4,6 +4,7 @@ using System.Collections.Generic; using System.Drawing; #if __IOS__ using NativeView = UIKit.UIView; +using NativeScrollView = UIKit.UIScrollView; #endif namespace Facebook.YogaKit @@ -699,12 +700,12 @@ namespace Facebook.YogaKit { get { - return _node.StyleAspectRatio; + return _node.AspectRatio; } set { - _node.StyleAspectRatio = value; + _node.AspectRatio = value; } } @@ -716,6 +717,17 @@ namespace Facebook.YogaKit 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); } @@ -743,9 +755,7 @@ namespace Facebook.YogaKit 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 }; } @@ -775,14 +785,14 @@ namespace Facebook.YogaKit 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; @@ -820,7 +830,7 @@ namespace Facebook.YogaKit var subviewsToInclude = new List(); foreach (var subview in view.Subviews) { - if (subview.Yoga().IsIncludeInLayout) + if (subview.Yoga().IsEnabled && subview.Yoga().IsIncludeInLayout) { subviewsToInclude.Add(subview); } @@ -856,7 +866,7 @@ namespace Facebook.YogaKit static double RoundPointValue(float value) { - float scale = NativePointScale; + float scale = NativePixelScale; return Math.Round(value * scale) / scale; } diff --git a/csharp/iOS/Facebook.YogaKit.iOS.Tests/Facebook.YogaKit.iOS.Tests.csproj b/csharp/iOS/Facebook.YogaKit.iOS.Tests/Facebook.YogaKit.iOS.Tests.csproj index 626f3488..b85e8ce5 100644 --- a/csharp/iOS/Facebook.YogaKit.iOS.Tests/Facebook.YogaKit.iOS.Tests.csproj +++ b/csharp/iOS/Facebook.YogaKit.iOS.Tests/Facebook.YogaKit.iOS.Tests.csproj @@ -22,8 +22,6 @@ true true true - true - true 17481 None x86_64 @@ -39,8 +37,6 @@ prompt 4 iPhone Developer - true - true true Entitlements.plist SdkOnly @@ -56,8 +52,6 @@ prompt 4 iPhone Developer - true - true None x86_64 HttpClientHandler @@ -76,8 +70,6 @@ true true true - true - true true Entitlements.plist SdkOnly @@ -102,6 +94,7 @@ + diff --git a/csharp/iOS/Facebook.YogaKit.iOS.Tests/YogaKitNativeTest.cs b/csharp/iOS/Facebook.YogaKit.iOS.Tests/YogaKitNativeTest.cs new file mode 100644 index 00000000..f7452d92 --- /dev/null +++ b/csharp/iOS/Facebook.YogaKit.iOS.Tests/YogaKitNativeTest.cs @@ -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); + } + + } +} diff --git a/csharp/iOS/Facebook.YogaKit.iOS/YogaLayout.cs b/csharp/iOS/Facebook.YogaKit.iOS/YogaLayout.cs index aacd3377..296949af 100644 --- a/csharp/iOS/Facebook.YogaKit.iOS/YogaLayout.cs +++ b/csharp/iOS/Facebook.YogaKit.iOS/YogaLayout.cs @@ -23,14 +23,21 @@ namespace Facebook.YogaKit 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;