コード例 #1
0
        public override void Configure(string propertyName, ViewNode currentViewNode, ViewNode parentViewNode, NodeConverter converter, ViewRenderService rendererService)
        {
            var currentNode = currentViewNode.Node;
            var parentNode  = parentViewNode?.Node;

            if (propertyName == PropertyNames.AddChild)
            {
                if (parentViewNode?.View != null)
                {
                    if (parentNode.IsStackView() && parentViewNode.View.NativeObject is AppKit.NSStackView stackView)
                    {
                        stackView.AddArrangedSubview(currentViewNode.View.NativeObject as NSView);
                    }
                    else
                    {
                        parentViewNode.View.AddChild(currentViewNode.View);
                    }
                }
                return;
            }
            if (propertyName == PropertyNames.Constraints)
            {
                if (!rendererService.HasConstraints(currentNode, converter))
                {
                    return;
                }

                if (currentNode is IConstraints constrainedNode && currentViewNode?.View?.NativeObject is AppKit.NSView nativeView && parentViewNode?.View?.NativeObject is AppKit.NSView parentNativeView)
                {
                    var constraints         = constrainedNode.constraints;
                    var absoluteBoundingBox = ((IAbsoluteBoundingBox)currentNode)
                                              .absoluteBoundingBox;
                    var absoluteBoundBoxParent = ((IAbsoluteBoundingBox)parentNode)
                                                 .absoluteBoundingBox;

                    if (constraints.horizontal.Contains("RIGHT") || constraints.horizontal == "SCALE")
                    {
                        var endPosition1 = absoluteBoundingBox.X + absoluteBoundingBox.Width;
                        var endPosition2 = absoluteBoundBoxParent.X + absoluteBoundBoxParent.Width;
                        var value        = Math.Max(endPosition1, endPosition2) - Math.Min(endPosition1, endPosition2);

                        var rightConstraint = nativeView.RightAnchor.ConstraintEqualToAnchor(parentNativeView.RightAnchor, -value);
                        rightConstraint.Active = true;
                    }

                    if (constraints.horizontal.Contains("LEFT"))
                    {
                        var value2 = absoluteBoundingBox.X - absoluteBoundBoxParent.X;
                        nativeView.LeftAnchor.ConstraintEqualToAnchor(parentNativeView.LeftAnchor, value2)
                        .Active = true;
                    }

                    if (constraints.vertical.Contains("BOTTOM") || constraints.horizontal == "SCALE")
                    {
                        var endPosition1 = absoluteBoundingBox.Y + absoluteBoundingBox.Height;
                        var endPosition2 = absoluteBoundBoxParent.Y + absoluteBoundBoxParent.Height;
                        var value2       = Math.Max(endPosition1, endPosition2) - Math.Min(endPosition1, endPosition2);

                        var bottomConstraint = nativeView.BottomAnchor.ConstraintEqualToAnchor(parentNativeView.BottomAnchor, -value2);
                        bottomConstraint.Active = true;
                    }

                    if (constraints.vertical.Contains("TOP"))
                    {
                        var value = absoluteBoundingBox.Y - absoluteBoundBoxParent.Y;
                        nativeView.TopAnchor.ConstraintEqualToAnchor(parentNativeView.TopAnchor, value)
                        .Active = true;
                    }

                    if (constraints.horizontal == "CENTER" || constraints.horizontal == "SCALE")
                    {
                        var delta = absoluteBoundingBox.X - absoluteBoundBoxParent.X - absoluteBoundBoxParent.Center.X;
                        nativeView.LeftAnchor.ConstraintEqualToAnchor(parentNativeView.CenterXAnchor, delta)
                        .Active = true;
                    }

                    if (constraints.vertical == "CENTER" || constraints.vertical == "SCALE")
                    {
                        var delta = absoluteBoundingBox.Y - absoluteBoundBoxParent.Y - absoluteBoundBoxParent.Center.Y;
                        //var delta = absoluteBoundBoxParent.Center.Substract(absoluteBoundingBox.Origin).Y;
                        var test = nativeView.TopAnchor.ConstraintEqualToAnchor(parentNativeView.CenterYAnchor, delta);
                        test.Active = true;
                    }
                }
                return;
            }

            if (propertyName == PropertyNames.Frame)
            {
                if (currentNode is IAbsoluteBoundingBox absoluteBounding)
                {
                    if (!rendererService.HasConstraints(currentNode, converter))
                    {
                        return;
                    }

                    var nativeView = currentViewNode?.View?.NativeObject as AppKit.NSView;
                    if (rendererService.HasWidthConstraint(currentNode, converter))
                    {
                        var widthConstraint = nativeView.WidthAnchor.ConstraintEqualToConstant(Math.Max(absoluteBounding.absoluteBoundingBox.Width, 1));
                        if (rendererService.IsFlexibleHorizontal(currentViewNode, converter))
                        {
                            widthConstraint.Priority = (float)NSLayoutPriority.DefaultLow;
                        }
                        widthConstraint.Active = true;
                    }

                    if (rendererService.HasHeightConstraint(currentNode, converter))
                    {
                        var heightConstraint = nativeView.HeightAnchor.ConstraintEqualToConstant(Math.Max(absoluteBounding.absoluteBoundingBox.Height, 1));
                        if (rendererService.IsFlexibleVertical(currentViewNode, converter))
                        {
                            heightConstraint.Priority = (float)NSLayoutPriority.DefaultLow;
                        }

                        heightConstraint.Active = true;
                    }
                }
                return;
            }
        }