[NUI] Sync dalihub and Samsung TizenFX (#338)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / internal / Xaml / MarkupExtensions / StaticResourceExtension.cs
1 using System;
2 using System.Xml;
3 using System.Reflection;
4 using System.Linq;
5 using Tizen.NUI.Binding;
6
7
8 namespace Tizen.NUI.Xaml
9 {
10     [ContentProperty("Key")]
11     internal sealed class StaticResourceExtension : IMarkupExtension
12     {
13         public string Key { get; set; }
14
15         public object ProvideValue(IServiceProvider serviceProvider)
16         {
17             if (serviceProvider == null)
18                 throw new ArgumentNullException(nameof(serviceProvider));
19             if (Key == null) {
20                 var lineInfoProvider = serviceProvider.GetService(typeof(IXmlLineInfoProvider)) as IXmlLineInfoProvider;
21                 var lineInfo = (lineInfoProvider != null) ? lineInfoProvider.XmlLineInfo : new XmlLineInfo();
22                 throw new XamlParseException("you must specify a key in {StaticResource}", lineInfo);
23             }
24             var valueProvider = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideParentValues;
25             if (valueProvider == null)
26                 throw new ArgumentException();
27             var xmlLineInfoProvider = serviceProvider.GetService(typeof(IXmlLineInfoProvider)) as IXmlLineInfoProvider;
28             var xmlLineInfo = xmlLineInfoProvider != null ? xmlLineInfoProvider.XmlLineInfo : null;
29             object resource = null;
30
31             foreach (var p in valueProvider.ParentObjects) {
32                 var irp = p as IResourcesProvider;
33                 var resDict = irp != null && irp.IsResourcesCreated ? irp.XamlResources : p as ResourceDictionary;
34                 if (resDict == null)
35                     continue;
36                 if (resDict.TryGetValue(Key, out resource))
37                     break;
38             }
39             resource = resource ?? GetApplicationLevelResource(Key, xmlLineInfo);
40
41             var bp = valueProvider.TargetProperty as BindableProperty;
42             var pi = valueProvider.TargetProperty as PropertyInfo;
43             var propertyType = bp?.ReturnType ?? pi?.PropertyType;
44             if (propertyType == null) {
45                 if (resource != null) {
46                     if (resource.GetType().GetTypeInfo().IsGenericType && (resource.GetType().GetGenericTypeDefinition() == typeof(OnPlatform<>) || resource.GetType().GetGenericTypeDefinition() == typeof(OnIdiom<>))) {
47                         // This is only there to support our backward compat story with pre 2.3.3 compiled Xaml project who was not providing TargetProperty
48                         var method = resource.GetType().GetRuntimeMethod("op_Implicit", new[] { resource.GetType() });
49                         resource = method.Invoke(null, new[] { resource });
50                     }
51                 }
52                 return resource;
53             }
54             if (propertyType.IsAssignableFrom(resource?.GetType()))
55                 return resource;
56             var implicit_op =  resource?.GetType().GetImplicitConversionOperator(fromType: resource?.GetType(), toType: propertyType)
57                             ?? propertyType.GetImplicitConversionOperator(fromType: resource?.GetType(), toType: propertyType);
58             if (implicit_op != null)
59                 return implicit_op.Invoke(resource, new [] { resource });
60
61             if (resource != null) { 
62                 //Special case for https://bugzilla.xamarin.com/show_bug.cgi?id=59818
63                 //On OnPlatform, check for an opImplicit from the targetType
64                 if (   Device.Flags != null
65                     && Device.Flags.Contains("xamlDoubleImplicitOpHack")
66                     && resource.GetType().GetTypeInfo().IsGenericType
67                     && (resource.GetType().GetGenericTypeDefinition() == typeof(OnPlatform<>))) {
68                     var tType = resource.GetType().GenericTypeArguments[0];
69                     var opImplicit = tType.GetImplicitConversionOperator(fromType: tType, toType: propertyType)
70                                     ?? propertyType.GetImplicitConversionOperator(fromType: tType, toType: propertyType);
71
72                     if (opImplicit != null) {
73                         //convert the OnPlatform<T> to T
74                         var opPlatformImplicitConversionOperator = resource?.GetType().GetImplicitConversionOperator(fromType: resource?.GetType(), toType: tType);
75                         resource = opPlatformImplicitConversionOperator?.Invoke(null, new[] { resource });
76
77                         //and convert to toType
78                         resource = opImplicit.Invoke(null, new[] { resource });
79                         return resource;
80                     }
81                 }
82             }
83             return resource;
84         }
85
86         internal object GetApplicationLevelResource(string key, IXmlLineInfo xmlLineInfo)
87         {
88             object resource = null;
89             if (Application.Current == null || !((IResourcesProvider)Application.Current).IsResourcesCreated || !Application.Current.XamlResources.TryGetValue(Key, out resource))
90                 throw new XamlParseException($"StaticResource not found for key {Key}", xmlLineInfo);
91             return resource;
92         }
93     }
94 }