Add basic Xamarin.Mac support

- Does not contain "magic" found in YogaKit yet, but enough to get started
- Simple test project showing use
This commit is contained in:
Chris Hamons
2016-12-08 09:41:59 -06:00
parent 73662ebf83
commit 05ab9be814
28 changed files with 1211 additions and 1 deletions

View File

@@ -0,0 +1,83 @@
using System;
using AppKit;
using Foundation;
using CoreGraphics;
namespace Facebook.Yoga.Mac.Test
{
public static class NSViewYogaExtensions
{
public static void ApplyYogaLayout(this NSView view, YogaNode n)
{
view.Frame = new CGRect(n.LayoutX, n.LayoutY, n.LayoutWidth, n.LayoutHeight);
// This assumes your YogaNode and NSView children were inserted in same order
for (int i = 0; i < n.Count; ++i) {
YogaNode childNode = n[i];
view.Subviews[i].Frame = new CGRect (childNode.LayoutX, childNode.LayoutY, childNode.LayoutWidth, childNode.LayoutHeight);
}
}
}
public partial class ViewController : NSViewController
{
public ViewController(IntPtr handle) : base(handle)
{
}
public override void ViewDidLoad()
{
base.ViewDidLoad ();
NSImage image = NSImage.ImageNamed (NSImageName.TrashFull);
NSView root = CreateViewHierarchy (image);
var rootNode = CalculateLayout (image.Size);
root.ApplyYogaLayout (rootNode);
View.AddSubview (root);
}
static NSView CreateViewHierarchy (NSImage image)
{
var root = new NSView ();
NSImageView imageView = new NSImageView () {
Image = image
};
root.AddSubview (imageView);
NSTextView text = new NSTextView () {
Value = "Hello World",
};
root.AddSubview (text);
return root;
}
static YogaNode CalculateLayout (CGSize imageSize)
{
var rootNode = new YogaNode () {
Width = 400,
Height = 120,
FlexDirection = YogaFlexDirection.Row
};
rootNode.SetPadding (YogaEdge.All, 20);
var imageNode = new YogaNode () {
Width = (float)imageSize.Width,
};
imageNode.SetMargin (YogaEdge.End, 20);
var textNode = new YogaNode () {
AlignSelf = YogaAlign.Center,
FlexGrow = 1,
};
rootNode.Insert (0, imageNode);
rootNode.Insert (1, textNode);
rootNode.CalculateLayout ();
return rootNode;
}
}
}