Exemple #1
0
        public IXamlIlAstNode Transform(XamlIlAstTransformationContext context, IXamlIlAstNode node)
        {
            if (node is XamlIlPropertyAssignmentNode pa &&
                pa.Property.Name == "Name" &&
                pa.Property.DeclaringType.FullName == "Avalonia.StyledElement")
            {
                if (context.ParentNodes().FirstOrDefault() is XamlIlManipulationGroupNode mg &&
                    mg.Children.OfType <AvaloniaNameScopeRegistrationXamlIlNode>().Any())
                {
                    return(node);
                }

                IXamlIlAstValueNode value = null;
                for (var c = 0; c < pa.Values.Count; c++)
                {
                    if (pa.Values[c].Type.GetClrType().Equals(context.Configuration.WellKnownTypes.String))
                    {
                        value = pa.Values[c];
                        if (!(value is XamlIlAstTextNode))
                        {
                            var local = new XamlIlAstCompilerLocalNode(value);
                            // Wrap original in local initialization
                            pa.Values[c] = new XamlIlAstLocalInitializationNodeEmitter(value, value, local);
                            // Use local
                            value = local;
                        }

                        break;
                    }
                }

                if (value != null)
                {
                    return new XamlIlManipulationGroupNode(pa)
                           {
                               Children =
                               {
                                   pa,
                                   new AvaloniaNameScopeRegistrationXamlIlNode(value, context.GetAvaloniaTypes())
                               }
                           }
                }
                ;
            }

            if (!context.ParentNodes().Any() &&
                node is XamlIlValueWithManipulationNode mnode)
            {
                mnode.Manipulation = new XamlIlManipulationGroupNode(mnode,
                                                                     new[]
                {
                    mnode.Manipulation,
                    new HandleRootObjectScopeNode(mnode, context.GetAvaloniaTypes())
                });
            }
            return(node);
        }
Exemple #2
0
        public IXamlIlAstNode Transform(XamlIlAstTransformationContext context, IXamlIlAstNode node)
        {
            if (node is XamlIlAstClrProperty prop)
            {
                var n     = prop.Name + "Property";
                var field =
                    prop.DeclaringType.Fields
                    .FirstOrDefault(f => f.Name == n);
                if (field != null)
                {
                    return(new XamlIlAvaloniaProperty(prop, field, context.GetAvaloniaTypes()));
                }
            }

            return(node);
        }
Exemple #3
0
        public static IXamlIlAvaloniaPropertyNode CreateNode(XamlIlAstTransformationContext context,
                                                             string propertyName, IXamlIlAstTypeReference selectorTypeReference, IXamlIlLineInfo lineInfo)
        {
            XamlIlAstNamePropertyReference forgedReference;

            var parser = new PropertyParser();

            var parsedPropertyName = parser.Parse(new CharacterReader(propertyName.AsSpan()));

            if (parsedPropertyName.owner == null)
            {
                forgedReference = new XamlIlAstNamePropertyReference(lineInfo, selectorTypeReference,
                                                                     propertyName, selectorTypeReference);
            }
            else
            {
                var xmlOwner = parsedPropertyName.ns;
                if (xmlOwner != null)
                {
                    xmlOwner += ":";
                }
                xmlOwner += parsedPropertyName.owner;

                var tref = XamlIlTypeReferenceResolver.ResolveType(context, xmlOwner, false, lineInfo, true);

                var propertyFieldName = parsedPropertyName.name + "Property";
                var found             = tref.Type.GetAllFields()
                                        .FirstOrDefault(f => f.IsStatic && f.IsPublic && f.Name == propertyFieldName);
                if (found == null)
                {
                    throw new XamlIlParseException(
                              $"Unable to find {propertyFieldName} field on type {tref.Type.GetFullName()}", lineInfo);
                }
                return(new XamlIlAvaloniaPropertyFieldNode(context.GetAvaloniaTypes(), lineInfo, found));
            }

            var clrProperty =
                ((XamlIlAstClrProperty) new XamlIlPropertyReferenceResolver().Transform(context,
                                                                                        forgedReference));

            return(new XamlIlAvaloniaPropertyNode(lineInfo,
                                                  context.Configuration.TypeSystem.GetType("Avalonia.AvaloniaProperty"),
                                                  clrProperty));
        }
 public IXamlIlAstNode Transform(XamlIlAstTransformationContext context, IXamlIlAstNode node)
 {
     if (node is XamlIlAstObjectNode on)
     {
         foreach (var ch in on.Children)
         {
             if (ch is XamlIlAstXamlPropertyValueNode pn &&
                 pn.Property.GetClrProperty().Getter?.ReturnType.Equals(context.GetAvaloniaTypes().Transitions) == true)
             {
                 for (var c = 0; c < pn.Values.Count; c++)
                 {
                     pn.Values[c] = new AvaloniaXamlIlTargetTypeMetadataNode(pn.Values[c], on.Type,
                                                                             AvaloniaXamlIlTargetTypeMetadataNode.ScopeTypes.Transitions);
                 }
             }
         }
     }
     return(node);
 }
        public IXamlIlAstNode Transform(XamlIlAstTransformationContext context, IXamlIlAstNode node)
        {
            if (!(node is XamlIlAstObjectNode on &&
                  on.Type.GetClrType().FullName == "Avalonia.Styling.Setter"))
            {
                return(node);
            }

            var parent = context.ParentNodes().OfType <XamlIlAstObjectNode>()
                         .FirstOrDefault(p => p.Type.GetClrType().FullName == "Avalonia.Styling.Style");

            if (parent == null)
            {
                throw new XamlIlParseException(
                          "Avalonia.Styling.Setter is only valid inside Avalonia.Styling.Style", node);
            }
            var selectorProperty = parent.Children.OfType <XamlIlAstXamlPropertyValueNode>()
                                   .FirstOrDefault(p => p.Property.GetClrProperty().Name == "Selector");

            if (selectorProperty == null)
            {
                throw new XamlIlParseException(
                          "Can not find parent Style Selector", node);
            }
            var selector = selectorProperty.Values.FirstOrDefault() as XamlIlSelectorNode;

            if (selector?.TargetType == null)
            {
                throw new XamlIlParseException(
                          "Can not resolve parent Style Selector type", node);
            }


            var property = @on.Children.OfType <XamlIlAstXamlPropertyValueNode>()
                           .FirstOrDefault(x => x.Property.GetClrProperty().Name == "Property");

            if (property == null)
            {
                throw new XamlIlParseException("Setter without a property is not valid", node);
            }

            var propertyName = property.Values.OfType <XamlIlAstTextNode>().FirstOrDefault()?.Text;

            if (propertyName == null)
            {
                throw new XamlIlParseException("Setter.Property must be a string", node);
            }


            var avaloniaPropertyNode = XamlIlAvaloniaPropertyHelper.CreateNode(context, propertyName,
                                                                               new XamlIlAstClrTypeReference(selector, selector.TargetType, false), property.Values[0]);

            property.Values = new List <IXamlIlAstValueNode>
            {
                avaloniaPropertyNode
            };

            var valueProperty = on.Children
                                .OfType <XamlIlAstXamlPropertyValueNode>().FirstOrDefault(p => p.Property.GetClrProperty().Name == "Value");

            if (valueProperty?.Values?.Count == 1 && valueProperty.Values[0] is XamlIlAstTextNode)
            {
                var propType = avaloniaPropertyNode.AvaloniaPropertyType;
                if (!XamlIlTransformHelpers.TryGetCorrectlyTypedValue(context, valueProperty.Values[0],
                                                                      propType, out var converted))
                {
                    throw new XamlIlParseException(
                              $"Unable to convert property value to {propType.GetFqn()}",
                              valueProperty.Values[0]);
                }

                valueProperty.Property = new SetterValueProperty(valueProperty.Property,
                                                                 on.Type.GetClrType(), propType, context.GetAvaloniaTypes());
            }

            return(node);
        }