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