Пример #1
0
 public XamlIlAvaloniaPropertyFieldNode(AvaloniaXamlIlWellKnownTypes types,
                                        IXamlLineInfo lineInfo, IXamlField field) : base(lineInfo)
 {
     _field = field;
     AvaloniaPropertyType = XamlIlAvaloniaPropertyHelper.GetAvaloniaPropertyType(field,
                                                                                 types, lineInfo);
 }
Пример #2
0
        public XamlIlAvaloniaProperty(XamlAstClrProperty original, IXamlField field,
                                      AvaloniaXamlIlWellKnownTypes types)
            : base(original, original.Name, original.DeclaringType, original.Getter, original.Setters)
        {
            var assignBinding = original.CustomAttributes.Any(ca => ca.Type.Equals(types.AssignBindingAttribute));

            AvaloniaProperty = field;
            CustomAttributes = original.CustomAttributes;
            if (!assignBinding)
            {
                Setters.Insert(0, new BindingSetter(types, original.DeclaringType, field));
            }

            // Styled and attached properties can be set with a BindingPriority when they're
            // assigned in a ControlTemplate.
            if (field.FieldType.GenericTypeDefinition == types.StyledPropertyT ||
                field.FieldType.GenericTypeDefinition == types.AvaloniaAttachedPropertyT)
            {
                var propertyType = field.FieldType.GenericArguments[0];
                Setters.Insert(0, new SetValueWithPrioritySetter(types, original.DeclaringType, field, propertyType));
                if (!assignBinding)
                {
                    Setters.Insert(1, new BindingWithPrioritySetter(types, original.DeclaringType, field));
                }
            }

            Setters.Insert(0, new UnsetValueSetter(types, original.DeclaringType, field));
        }
        public AvaloniaXamlIlGridLengthAstNode(IXamlLineInfo lineInfo, AvaloniaXamlIlWellKnownTypes types, GridLength gridLength) : base(lineInfo)
        {
            _types      = types;
            _gridLength = gridLength;

            Type = new XamlAstClrTypeReference(lineInfo, types.GridLength, false);
        }
Пример #4
0
 public AvaloniaPropertyCustomSetter(AvaloniaXamlIlWellKnownTypes types,
                                     IXamlIlType declaringType,
                                     IXamlIlField avaloniaProperty)
 {
     Types            = types;
     AvaloniaProperty = avaloniaProperty;
     TargetType       = declaringType;
 }
 public AvaloniaXamlIlFontFamilyAstNode(AvaloniaXamlIlWellKnownTypes types,
                                        string text,
                                        IXamlLineInfo lineInfo) : base(lineInfo)
 {
     _types = types;
     _text  = text;
     Type   = new XamlAstClrTypeReference(lineInfo, types.FontFamily, false);
 }
Пример #6
0
        public AvaloniaXamlIlAvaloniaListConstantAstNode(IXamlLineInfo lineInfo, AvaloniaXamlIlWellKnownTypes types, IXamlType listType, IXamlType elementType, IReadOnlyList <IXamlAstValueNode> values) : base(lineInfo)
        {
            _constructor           = listType.GetConstructor();
            _listAddMethod         = listType.GetMethod(new FindMethodMethodSignature("Add", types.XamlIlTypes.Void, elementType));
            _listSetCapacityMethod = listType.GetMethod(new FindMethodMethodSignature("set_Capacity", types.XamlIlTypes.Void, types.Int));

            _elementType = elementType;
            _values      = values;

            Type = new XamlAstClrTypeReference(lineInfo, listType, false);
        }
Пример #7
0
        public XamlIlAvaloniaProperty(XamlIlAstClrProperty original, IXamlIlField field,
                                      AvaloniaXamlIlWellKnownTypes types)
            : base(original, original.Name, original.DeclaringType, original.Getter, original.Setters)
        {
            AvaloniaProperty = field;
            CustomAttributes = original.CustomAttributes;
            if (!original.CustomAttributes.Any(ca => ca.Type.Equals(types.AssignBindingAttribute)))
            {
                Setters.Insert(0, new BindingSetter(types, original.DeclaringType, field));
            }

            Setters.Insert(0, new UnsetValueSetter(types, original.DeclaringType, field));
        }
Пример #8
0
 protected AvaloniaPropertyCustomSetter(
     AvaloniaXamlIlWellKnownTypes types,
     IXamlType declaringType,
     IXamlField avaloniaProperty,
     bool allowNull)
 {
     Types            = types;
     AvaloniaProperty = avaloniaProperty;
     TargetType       = declaringType;
     BinderParameters = new PropertySetterBinderParameters
     {
         AllowXNull       = allowNull,
         AllowRuntimeNull = allowNull
     };
 }
Пример #9
0
        public static IXamlType GetAvaloniaPropertyType(IXamlField field,
                                                        AvaloniaXamlIlWellKnownTypes types, IXamlLineInfo lineInfo)
        {
            var avaloniaPropertyType = field.FieldType;

            while (avaloniaPropertyType != null)
            {
                if (avaloniaPropertyType.GenericTypeDefinition?.Equals(types.AvaloniaPropertyT) == true)
                {
                    return(avaloniaPropertyType.GenericArguments[0]);
                }

                avaloniaPropertyType = avaloniaPropertyType.BaseType;
            }

            throw new XamlX.XamlParseException(
                      $"{field.Name}'s type {field.FieldType} doesn't inherit from  AvaloniaProperty<T>, make sure to use typed properties",
                      lineInfo);
        }
        private static bool ConvertDefinitionList(
            IXamlAstValueNode node,
            string text,
            AvaloniaXamlIlWellKnownTypes types,
            IXamlType listType,
            IXamlType elementType,
            string errorDisplayName,
            out IXamlAstValueNode result)
        {
            try
            {
                var lengths = GridLength.ParseLengths(text);

                var definitionTypeRef = new XamlAstClrTypeReference(node, elementType, false);

                var definitionConstructorGridLength = elementType.GetConstructor(new List <IXamlType> {
                    types.GridLength
                });

                IXamlAstValueNode CreateDefinitionNode(GridLength length)
                {
                    var lengthNode = new AvaloniaXamlIlGridLengthAstNode(node, types, length);

                    return(new XamlAstNewClrObjectNode(node, definitionTypeRef,
                                                       definitionConstructorGridLength, new List <IXamlAstValueNode> {
                        lengthNode
                    }));
                }

                var definitionNodes =
                    new List <IXamlAstValueNode>(lengths.Select(CreateDefinitionNode));

                result = new AvaloniaXamlIlAvaloniaListConstantAstNode(node, types, listType, elementType, definitionNodes);

                return(true);
            }
            catch
            {
                throw new XamlX.XamlLoadException($"Unable to parse \"{text}\" as a {errorDisplayName}", node);
            }
        }
Пример #11
0
        public XamlIlAvaloniaPropertyFieldNode(AvaloniaXamlIlWellKnownTypes types,
                                               IXamlIlLineInfo lineInfo, IXamlIlField field) : base(lineInfo)
        {
            _field = field;
            var avaloniaPropertyType = field.FieldType;

            while (avaloniaPropertyType != null)
            {
                if (avaloniaPropertyType.GenericTypeDefinition?.Equals(types.AvaloniaPropertyT) == true)
                {
                    AvaloniaPropertyType = avaloniaPropertyType.GenericArguments[0];
                    return;
                }

                avaloniaPropertyType = avaloniaPropertyType.BaseType;
            }

            throw new XamlIlParseException(
                      $"{field.Name}'s type {field.FieldType} doesn't inherit from AvaloniaProperty<T>, make sure to use typed properties",
                      lineInfo);
        }
        public AvaloniaXamlIlVectorLikeConstantAstNode(IXamlLineInfo lineInfo, AvaloniaXamlIlWellKnownTypes types, IXamlType type, IXamlConstructor constructor, double[] values) : base(lineInfo)
        {
            var parameters = constructor.Parameters;

            if (parameters.Count != values.Length)
            {
                throw new XamlTypeSystemException($"Constructor that takes {values.Length} parameters is expected, got {parameters.Count} instead.");
            }

            var elementType = types.XamlIlTypes.Double;

            foreach (var parameter in parameters)
            {
                if (parameter != elementType)
                {
                    throw new XamlTypeSystemException($"Expected parameter of type {elementType}, got {parameter} instead.");
                }
            }

            _constructor = constructor;
            _values      = values;

            Type = new XamlAstClrTypeReference(lineInfo, type, false);
        }
Пример #13
0
 public UnsetValueSetter(AvaloniaXamlIlWellKnownTypes types, IXamlIlType declaringType, IXamlIlField avaloniaProperty)
     : base(types, declaringType, avaloniaProperty)
 {
     Parameters = new[] { types.UnsetValueType };
 }
Пример #14
0
 public BindingSetter(AvaloniaXamlIlWellKnownTypes types,
                      IXamlIlType declaringType,
                      IXamlIlField avaloniaProperty) : base(types, declaringType, avaloniaProperty)
 {
     Parameters = new[] { types.IBinding };
 }
Пример #15
0
 public BindingWithPrioritySetter(AvaloniaXamlIlWellKnownTypes types,
                                  IXamlType declaringType,
                                  IXamlField avaloniaProperty) : base(types, declaringType, avaloniaProperty, false)
 {
     Parameters = new[] { types.BindingPriority, types.IBinding };
 }
Пример #16
0
 public SetValueWithPrioritySetter(AvaloniaXamlIlWellKnownTypes types, IXamlType declaringType, IXamlField avaloniaProperty,
                                   IXamlType propertyType)
     : base(types, declaringType, avaloniaProperty, propertyType.AcceptsNull())
 {
     Parameters = new[] { types.BindingPriority, propertyType };
 }
        public static bool TryConvert(AstTransformationContext context, IXamlAstValueNode node, string text, IXamlType type, AvaloniaXamlIlWellKnownTypes types, out IXamlAstValueNode result)
        {
            if (type.FullName == "System.TimeSpan")
            {
                var tsText = text.Trim();

                if (!TimeSpan.TryParse(tsText, CultureInfo.InvariantCulture, out var timeSpan))
                {
                    // // shorthand seconds format (ie. "0.25")
                    if (!tsText.Contains(":") && double.TryParse(tsText,
                                                                 NumberStyles.Float | NumberStyles.AllowThousands,
                                                                 CultureInfo.InvariantCulture, out var seconds))
                    {
                        timeSpan = TimeSpan.FromSeconds(seconds);
                    }
                    else
                    {
                        throw new XamlX.XamlLoadException($"Unable to parse {text} as a time span", node);
                    }
                }

                result = new XamlStaticOrTargetedReturnMethodCallNode(node,
                                                                      type.FindMethod("FromTicks", type, false, types.Long),
                                                                      new[] { new XamlConstantNode(node, types.Long, timeSpan.Ticks) });
                return(true);
            }

            if (type.Equals(types.FontFamily))
            {
                result = new AvaloniaXamlIlFontFamilyAstNode(types, text, node);
                return(true);
            }

            if (type.Equals(types.Thickness))
            {
                try
                {
                    var thickness = Thickness.Parse(text);

                    result = new AvaloniaXamlIlVectorLikeConstantAstNode(node, types, types.Thickness, types.ThicknessFullConstructor,
                                                                         new[] { thickness.Left, thickness.Top, thickness.Right, thickness.Bottom });

                    return(true);
                }
                catch
                {
                    throw new XamlX.XamlLoadException($"Unable to parse \"{text}\" as a thickness", node);
                }
            }

            if (type.Equals(types.Point))
            {
                try
                {
                    var point = Point.Parse(text);

                    result = new AvaloniaXamlIlVectorLikeConstantAstNode(node, types, types.Point, types.PointFullConstructor,
                                                                         new[] { point.X, point.Y });

                    return(true);
                }
                catch
                {
                    throw new XamlX.XamlLoadException($"Unable to parse \"{text}\" as a point", node);
                }
            }

            if (type.Equals(types.Vector))
            {
                try
                {
                    var vector = Vector.Parse(text);

                    result = new AvaloniaXamlIlVectorLikeConstantAstNode(node, types, types.Vector, types.VectorFullConstructor,
                                                                         new[] { vector.X, vector.Y });

                    return(true);
                }
                catch
                {
                    throw new XamlX.XamlLoadException($"Unable to parse \"{text}\" as a vector", node);
                }
            }

            if (type.Equals(types.Size))
            {
                try
                {
                    var size = Size.Parse(text);

                    result = new AvaloniaXamlIlVectorLikeConstantAstNode(node, types, types.Size, types.SizeFullConstructor,
                                                                         new[] { size.Width, size.Height });

                    return(true);
                }
                catch
                {
                    throw new XamlX.XamlLoadException($"Unable to parse \"{text}\" as a size", node);
                }
            }

            if (type.Equals(types.Matrix))
            {
                try
                {
                    var matrix = Matrix.Parse(text);

                    result = new AvaloniaXamlIlVectorLikeConstantAstNode(node, types, types.Matrix, types.MatrixFullConstructor,
                                                                         new[] { matrix.M11, matrix.M12, matrix.M21, matrix.M22, matrix.M31, matrix.M32 });

                    return(true);
                }
                catch
                {
                    throw new XamlX.XamlLoadException($"Unable to parse \"{text}\" as a matrix", node);
                }
            }

            if (type.Equals(types.CornerRadius))
            {
                try
                {
                    var cornerRadius = CornerRadius.Parse(text);

                    result = new AvaloniaXamlIlVectorLikeConstantAstNode(node, types, types.CornerRadius, types.CornerRadiusFullConstructor,
                                                                         new[] { cornerRadius.TopLeft, cornerRadius.TopRight, cornerRadius.BottomRight, cornerRadius.BottomLeft });

                    return(true);
                }
                catch
                {
                    throw new XamlX.XamlLoadException($"Unable to parse \"{text}\" as a corner radius", node);
                }
            }

            if (type.Equals(types.Color))
            {
                if (!Color.TryParse(text, out Color color))
                {
                    throw new XamlX.XamlLoadException($"Unable to parse \"{text}\" as a color", node);
                }

                result = new XamlStaticOrTargetedReturnMethodCallNode(node,
                                                                      type.GetMethod(
                                                                          new FindMethodMethodSignature("FromUInt32", type, types.UInt)
                {
                    IsStatic = true
                }),
                                                                      new[] { new XamlConstantNode(node, types.UInt, color.ToUint32()) });

                return(true);
            }

            if (type.Equals(types.GridLength))
            {
                try
                {
                    var gridLength = GridLength.Parse(text);

                    result = new AvaloniaXamlIlGridLengthAstNode(node, types, gridLength);

                    return(true);
                }
                catch
                {
                    throw new XamlX.XamlLoadException($"Unable to parse \"{text}\" as a grid length", node);
                }
            }

            if (type.Equals(types.Cursor))
            {
                if (TypeSystemHelpers.TryGetEnumValueNode(types.StandardCursorType, text, node, out var enumConstantNode))
                {
                    var cursorTypeRef = new XamlAstClrTypeReference(node, types.Cursor, false);

                    result = new XamlAstNewClrObjectNode(node, cursorTypeRef, types.CursorTypeConstructor, new List <IXamlAstValueNode> {
                        enumConstantNode
                    });

                    return(true);
                }
            }

            if (type.Equals(types.ColumnDefinitions))
            {
                return(ConvertDefinitionList(node, text, types, types.ColumnDefinitions, types.ColumnDefinition, "column definitions", out result));
            }

            if (type.Equals(types.RowDefinitions))
            {
                return(ConvertDefinitionList(node, text, types, types.RowDefinitions, types.RowDefinition, "row definitions", out result));
            }

            if (type.Equals(types.Classes))
            {
                var classes    = text.Split(' ');
                var classNodes = classes.Select(c => new XamlAstTextNode(node, c, types.XamlIlTypes.String)).ToArray();

                result = new AvaloniaXamlIlAvaloniaListConstantAstNode(node, types, types.Classes, types.XamlIlTypes.String, classNodes);
                return(true);
            }

            result = null;
            return(false);
        }