internal override IPropertyEditorModel Create(XenProperty property)
        {
            var info = GetTypeEditorInfo(property.XenType);

            if (info == null)
            {
                return(null);
            }

            var model = CreateTypeEditorModel(info);

            if (model != null)
            {
                model.UseDefaultValue = true;
                model.Property        = property;
                model.ToolboxValue    = property.Value;
                model.FullTypeName    = property.XenType.FullName;
                model.ShortTypeName   = property.XenType.ShortName;

                if (model.Property.XenType.Descriptor.HasFlag(XenPropertyDescriptors.AttachedProperty))
                {
                    model.DisplayName = property.XamlPropertyName;
                }
                else
                {
                    model.DisplayName = property.PropertyName;
                }
            }

            return(model);
        }
Example #2
0
        public void SetPossibleValues(XenProperty property)
        {
            if (property.Value == null)
            {
                return;
            }

            property.XenType.Descriptor = GetDescriptors(property.Value.GetType());

            var entry = GetItem(property.Value.GetType());

            if (entry?.Generator == null)
            {
                return;
            }

            var type = _typeFinder.Find(property.XenType.FullName);

            if (type == null)
            {
                return;
            }

            property.XenType.PossibleValues = entry.Generator.Get(type);

            if (property.XenType.PossibleValues?.Length > 0)
            {
                property.XenType.Descriptor |= XenPropertyDescriptors.Literals;
            }
        }
Example #3
0
        public void SetPossibleValues(XenReflectionProperty xRef, XenProperty xProp, Enum e)
        {
            if (xRef == null || xProp == null)
            {
                return;
            }

            xProp.XenType = CreateType(xRef);
            var item = GetItem(xProp.XenType.GetType());

            var gen    = new EnumGenerator();
            var result = gen.Get(e.GetType());

            if (item != null)
            {
                xProp.XenType.Descriptor = item.Descriptor;
            }

            xProp.XenType.PossibleValues = result;
            xProp.XenType.Descriptor    |= XenPropertyDescriptors.Literals;

            if (e.HasFlags())
            {
                xProp.XenType.Descriptor |= XenPropertyDescriptors.Flags;
            }
        }
Example #4
0
        public void SetPossibleValues(XenReflectionProperty xRef, XenProperty xProp)
        {
            if (xRef == null || xProp == null)
            {
                return;
            }

            xProp.XenType = CreateType(xRef);

            var entry = GetItem(xProp.Value?.GetType());

            if (entry?.Generator == null)
            {
                return;
            }

            var type = _typeFinder.Find(xProp.XenType.FullName);

            if (type == null)
            {
                return;
            }

            xProp.XenType.PossibleValues = entry.Generator.Get(type);

            if (xProp.XenType.PossibleValues?.Length > 0)
            {
                xProp.XenType.Descriptor |= XenPropertyDescriptors.Literals;
            }
        }
Example #5
0
        public void SetProperty(XenWidget widget, XenProperty property, object value, bool isBase64 = false, bool isAp = false, object metadata = null)
        {
            var r = XenMessage.Create <SetPropertyRequest>();

            r.Metadata           = metadata;
            r.Path               = property.Path;
            r.Value              = value;
            r.WidgetId           = widget.Id;
            r.IsBase64           = isBase64;
            r.IsAttachedProperty = isAp;

            _socket.Send(r);
        }
Example #6
0
        public void CallConstructor(string widgetId, XenProperty property, XenConstructor ctor)
        {
            if (ctor == null)
            {
                return;
            }
            var r = XenMessage.Create <CallConstructorRequest>();

            r.Constructor = ctor;
            r.Property    = property;
            r.WidgetId    = widgetId;

            _socket.Send(r);
        }
Example #7
0
        protected override void OnExecute(XenMessageContext ctx)
        {
            if (ctx.Request == null)
            {
                return;
            }

            var req = ctx.Get <GetObjectRequest>();

            if (req == null)
            {
                return;
            }

            var props = GetXenProperties(req.WidgetId, true, req.Path);

            if (!props.Any())
            {
                return;
            }

            var prop = props[0];

            if (prop.XenType.Descriptor.HasFlag(XenPropertyDescriptors.Enumerable))
            {
                if (prop.XenType.Descriptor.HasFlag(XenPropertyDescriptors.Enumerable))
                {
                    // grab index value; if null, return without creating an ObjectResponse.
                    var index = ReflectionMethods.GetIndexerValue(req.Path[0]);
                    if (index == null)
                    {
                        return;
                    }

                    var item = ReflectionMethods.GetItem(prop.Value, index.Value);
                    prop.Value = GetXenProperties(item);
                }
            }

            prop.Path = req.Path?.Union(prop.Path)?.ToArray();
            var cantCast = SetPath(prop.Value, req.Path);

            ctx.SetResponse <ObjectResponse>(res =>
            {
                res.UnknownCondition = cantCast;
                res.Property         = prop;
                res.WidgetId         = req.WidgetId;
                res.ObjectName       = XenProperty.GetLastPath(req.Path);
            });
        }
        public void Test_flags_and_staticlist()
        {
            Dr.Add(typeof(FakeEnumWithFlags), XenPropertyDescriptors.None);

            var t1 = FakeEnumWithFlags.Test1;

            var xRef = new XenReflectionProperty
            {
                TargetType = typeof(FakeEnumWithFlags),
            };

            var xProp = new XenProperty
            {
                Value = t1
            };

            Dr.SetPossibleValues(xRef, xProp, t1);

            Assert.IsTrue(xProp.XenType.Descriptor.HasFlag(XenPropertyDescriptors.Flags | XenPropertyDescriptors.Literals));
        }
Example #9
0
        private Color Convert(XenProperty property)
        {
            var json = property.Value?.ToString();

            if (string.IsNullOrWhiteSpace(json))
            {
                return(Colors.Transparent);
            }

            var xenProps = JsonConvert.DeserializeObject <XenProperty[]>(json);

            var r = GetProperty(xenProps, "R");
            var g = GetProperty(xenProps, "G");
            var b = GetProperty(xenProps, "B");
            var a = GetProperty(xenProps, "A");

            if (r == -1 && g == -1 && b == -1 && a == -1)
            {
                return(new Color(0, 0, 0, 0));
            }

            return(new Color(r, g, b, a));
        }
Example #10
0
        public XenProperty[] GetXenProperties(XenReflectionProperty[] refProps, bool includeValues = false)
        {
            if (refProps == null)
            {
                return(null);
            }
            if (SupportingTypes.Types == null)
            {
                return(null);
            }

            var result = new List <XenProperty>();

            // The properties are in an intermediate state, right here.
            // a XenReflectionProperty is not meant to be returned in a request.

            // build xen properties.
            foreach (var curRef in refProps)
            {
                if (curRef.IsTargetEnum && SupportingTypes.IsRegistered(typeof(Enum)))
                {
                    if (!SupportingTypes.IsRegistered(curRef, RegistrarMatches.TypeName | RegistrarMatches.Enum))
                    {
                        continue;
                    }
                }
                else
                {
                    if (!SupportingTypes.IsRegistered(curRef, RegistrarMatches.TypeName))
                    {
                        continue;
                    }
                }

                var xenProp = new XenProperty
                {
                    Path     = new[] { curRef.TargetName },
                    Value    = curRef.GetTargetObject(),
                    CanRead  = curRef.CanReadTarget,
                    CanWrite = curRef.CanWriteTarget
                };

                // is the current property an enumeration?
                if (curRef.IsTargetEnum && SupportingTypes.IsRegistered(typeof(Enum)))
                {
                    var value = curRef.As <Enum>();
                    if (value != null)
                    {
                        Descriptor.SetPossibleValues(curRef, xenProp, value);

                        if (xenProp.Value != null)
                        {
                            xenProp.Value = Enum.GetName(xenProp.Value.GetType(), value);
                        }
                    }
                }
                else
                {
                    Descriptor.SetPossibleValues(curRef, xenProp);

                    // non-primitive structures (not System.DateTime, but Xamarin.Forms.Point)
                    if (ReflectionMethods.IsNotPrimitiveValueType(xenProp.Value))
                    {
                        xenProp.XenType.Descriptor |= XenPropertyDescriptors.ValueType;

                        var vtProps = XenPropertyMethods
                                      .GetPrimitiveValueTypeProperties(xenProp.Value)
                                      .ToArray();

                        if (vtProps.Length != 0)
                        {
                            foreach (var vtProp in vtProps)
                            {
                                Descriptor.SetPossibleValues(vtProp);

                                if (vtProp.Value.GetType() != typeof(string))
                                {
                                    var tmp = Convert.ToString(vtProp.Value ?? string.Empty);
                                    vtProp.Value = tmp;
                                }
                            }

                            // don't return values to the toolbox that will never be used.
                            xenProp.Value = includeValues ? vtProps : null;
                        }
                    }

                    // enumerables, collections, list.
                    if (curRef.TargetType.IsKindOf(typeof(ICollection <>)))
                    {
                        xenProp.XenType.Descriptor |= XenPropertyDescriptors.Collection;
                    }

                    if (curRef.TargetType.IsKindOf(typeof(IList <>)))
                    {
                        xenProp.XenType.Descriptor |= XenPropertyDescriptors.List;
                    }

                    if (curRef.TargetType.IsKindOf(typeof(IEnumerable <>)))
                    {
                        xenProp.XenType.Descriptor |= XenPropertyDescriptors.Enumerable;
                        var collection = xenProp.Value as IEnumerable <object>;

                        if (collection != null)
                        {
                            var count = collection.Count();
                            xenProp.XenType.PossibleValues = new[] { count.ToString() };

                            if (includeValues == false)
                            {
                                // don't send the value back to the toolbox
                                xenProp.Value = null;
                            }
                        }
                    }

                    /*
                     * obj support?
                     * if (!curRef.TargetType.GetTypeInfo().IsValueType && !curRef.TargetType.GetTypeInfo().IsPrimitive)
                     * {
                     *  if (!curRef.TargetType.Namespace.StartsWith("System"))
                     *  {
                     *      var oProps = XenPropertyMethods
                     *      .GetObjectProperties(xenProp.Value)
                     *      .ToArray();
                     *
                     *      if (oProps.Length != 0)
                     *      {
                     *          foreach (var oProp in oProps)
                     *          {
                     *              Descriptor.SetPossibleValues(oProp);
                     *
                     *              if (oProp.Value != null && oProp.Value.GetType() != typeof(string))
                     *              {
                     *                  var tmp = Convert.ToString(oProp.Value ?? string.Empty);
                     *                  oProp.Value = tmp;
                     *              }
                     *          }
                     *
                     *          // don't return values to the toolbox that will never be used.
                     *          xenProp.Value = includeValues ? oProps : null;
                     *      }
                     *  }
                     * }
                     */
                }

                result.Add(xenProp);
            }

            return(result.ToArray());
        }
Example #11
0
        protected void SetPropertyValue(string rWidgetId, string[] rPath, object rValue, bool rIsBase64, bool rIsAttachedProperty)
        {
            var ignoreSet      = false;
            var targetPropName = XenProperty.GetLastPath(rPath);
            var pair           = Surface[rWidgetId];

            AttachedPropertyInfo  targetAttachedProp = null;
            XenReflectionProperty targetProp;

            if (rIsAttachedProperty)
            {
                var apAncestors = ElementHelper.GetParents(pair.XenWidget);
                var apInfos     = new List <AttachedPropertyInfo>();

                foreach (var ancestor in apAncestors)
                {
                    var aView = Surface[ancestor.Id];
                    if (aView == null)
                    {
                        continue;
                    }
                    apInfos.AddRange(ElementHelper.GetAttachedProperties(aView.VisualElement));
                }

                if (apInfos.Count == 0)
                {
                    return;
                }
                targetAttachedProp = apInfos.LastOrDefault(a => a.PropertyName == targetPropName);
                if (targetAttachedProp == null)
                {
                    return;
                }

                object apParent      = null;
                object apGrandParent = null;

                if (apAncestors.ElementAtOrDefault(0) != null)
                {
                    apParent = Surface[apAncestors[0].Id].VisualElement;
                }

                if (apAncestors.ElementAtOrDefault(1) != null)
                {
                    apGrandParent = Surface[apAncestors[1].Id].VisualElement;
                }

                targetProp = targetAttachedProp.Convert(apParent, apGrandParent);
            }
            else
            {
                targetProp = pair?
                             .VisualElement?
                             .GetXenRefProperties(rPath)
                             .FirstOrDefault();

                if (targetProp == null)
                {
                    return;
                }
            }

            if (targetPropName != null && targetPropName.Contains("Color"))
            {
                rValue = Color.FromHex(rValue.ToString());
            }

            // todo: make into a serializer
            if (targetPropName != null && targetPropName.Contains("Source"))
            {
                if (!rIsBase64)
                {
                    return;
                }
                var bytes = Convert.FromBase64String(rValue.ToString());

                var src = new XenImageSource(ImageSource.FromStream(() => new MemoryStream(bytes)))
                {
                    FileName = _req.Metadata?.ToString()
                };

                rValue = src;
            }

            // get enumeration value
            if (targetProp.IsTargetEnum)
            {
                var eval = ReflectionMethods.CreateEnum(targetProp.TargetType, rValue);

                if (eval == null)
                {
                    ignoreSet = true;
                }
                else
                {
                    rValue = eval;
                }
            }

            if (!ignoreSet)
            {
                DesignThread.Invoke(() =>
                {
                    // if target property is part of a structure and we're modifying its properties
                    // 1) make a copy of the current value
                    // 2) change the property value
                    // 3) reassign it
                    if (ReflectionMethods.IsValueType(targetProp.ParentObject))
                    {
                        var childProp = targetProp
                                        .ParentObject
                                        .GetXenRefProperties()
                                        .FirstOrDefault(p => p.TargetName == targetPropName);

                        childProp?.SetTargetObject(rValue);
                        var copy = childProp?.ParentObject;

                        var parentInfo = targetProp
                                         .GrandParentObject?
                                         .GetType()
                                         .GetProperty(targetProp.ParentName);

                        parentInfo?.SetValue(targetProp.GrandParentObject, copy);

                        // supporting View -> Struct -> Struct -> Target (valuetype or ref)
                        if (ReflectionMethods.IsValueType(targetProp.GrandParentObject))
                        {
                            if (rPath.Length == 3)
                            {
                                targetProp
                                .GrandParentObject?
                                .GetType()
                                .GetProperty(targetProp.ParentName)
                                .SetValue(targetProp.GrandParentObject, targetProp.ParentObject);

                                pair.VisualElement
                                .GetType()
                                .GetProperty(rPath[0])
                                .SetValue(pair.VisualElement, targetProp.GrandParentObject);
                            }
                        }
                    }
                    // this is assinging a field
                    else if (targetProp.IsTargetStruct && rValue is string)
                    {
                        var fieldName = rValue.ToString() ?? string.Empty;
                        var fieldInfo = targetProp.TargetType.GetStaticFields().FirstOrDefault(f => f.Name == fieldName);

                        if (fieldInfo == null)
                        {
                            if (rIsAttachedProperty)
                            {
                                SetAttachedProperty(rValue, targetAttachedProp, pair);
                            }
                            else
                            {
                                targetProp.SetTargetObject(rValue);
                            }
                        }
                        else
                        {
                            var copy = fieldInfo.GetValue(null);

                            if (rIsAttachedProperty)
                            {
                                SetAttachedProperty(copy, targetAttachedProp, pair);
                            }
                            else
                            {
                                targetProp.SetTargetObject(copy);
                            }
                        }
                    }
                    else
                    {
                        // one last attempt
                        if (rIsAttachedProperty)
                        {
                            SetAttachedProperty(rValue, targetAttachedProp, pair);
                        }
                        else
                        {
                            targetProp.SetTargetObject(rValue);
                        }
                    }
                });
            }
        }
Example #12
0
 internal abstract IPropertyEditorModel Create(XenProperty property);
        protected override void OnExecute(XenMessageContext ctx)
        {
            if (ctx.Request == null)
            {
                return;
            }

            var req = ctx.Get <GetAttachedPropertiesRequest>();

            if (req == null)
            {
                return;
            }

            var target = Surface[req.WidgetId];

            var all     = new List <XenProperty>();
            var parents = ElementHelper.GetParents(target.XenWidget);

            foreach (var parent in parents)
            {
                var view = Surface[parent.Id].VisualElement;

                var aps = ElementHelper.GetAttachedProperties(view).ToArray();
                if (aps == null || aps.Length == 0)
                {
                    continue;
                }

                foreach (var ap in aps)
                {
                    // skip dupe attached props
                    var processed = all.Any(a => a.XamlPropertyName == ap.XamlPropertyName);
                    if (processed)
                    {
                        continue;
                    }

                    var xt = Descriptor.CreateType(ap.GetMethod);
                    xt.Descriptor |= XenPropertyDescriptors.AttachedProperty;
                    var apval = ap.GetMethod.Invoke(null, new object[] { target.VisualElement });

                    var xp = new XenProperty
                    {
                        XamlPropertyName = ap.XamlPropertyName,
                        CanWrite         = true,
                        CanRead          = true,
                        XenType          = xt,
                        Value            = apval,
                        Path             = new[] { ap.Field.Name }
                    };

                    all.Add(xp);
                }
            }

            target.XenWidget.AttachedProperties = all.ToArray();

            ctx.SetResponse <GetAttachedPropertiesResponse>(r =>
            {
                r.Widget = target.XenWidget;
            });
        }