[NUI] Split NUI Assemblies (#865)
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.Xaml / src / public / XamlBinding / StyleSheets / Style.cs
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Reflection;
5 using Tizen.NUI.XamlBinding;
6
7 namespace Tizen.NUI.StyleSheets
8 {
9     /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
10     [EditorBrowsable(EditorBrowsableState.Never)]
11     public sealed class Style
12     {
13         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
14         [EditorBrowsable(EditorBrowsableState.Never)]
15         Style()
16         {
17         }
18
19         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
20         [EditorBrowsable(EditorBrowsableState.Never)]
21         public IDictionary<string, string> Declarations { get; set; } = new Dictionary<string, string>();
22         Dictionary<KeyValuePair<string, string>, object> convertedValues = new Dictionary<KeyValuePair<string, string>, object>();
23
24         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
25         [EditorBrowsable(EditorBrowsableState.Never)]
26         public static Style Parse(CssReader reader, char stopChar = '\0')
27         {
28             Style style = new Style();
29             string propertyName = null, propertyValue = null;
30
31             int p;
32             reader.SkipWhiteSpaces();
33             bool readingName = true;
34             while ((p = reader.Peek()) > 0) {
35                 switch (unchecked((char)p)) {
36                 case ':':
37                     reader.Read();
38                     readingName = false;
39                     reader.SkipWhiteSpaces();
40                     break;
41                 case ';':
42                     reader.Read();
43                     if (!string.IsNullOrEmpty(propertyName) && !string.IsNullOrEmpty(propertyValue))
44                         style.Declarations.Add(propertyName, propertyValue);
45                     propertyName = propertyValue = null;
46                     readingName = true;
47                     reader.SkipWhiteSpaces();
48                     break;
49                 default:
50                     if ((char)p == stopChar)
51                         return style;
52
53                     if (readingName) {
54                         propertyName = reader.ReadIdent();
55                         if (propertyName == null)
56                             throw new Exception();
57                     } else 
58                         propertyValue = reader.ReadUntil(stopChar, ';', ':');
59                     break;
60                 }
61             }
62             return style;
63         }
64
65         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
66         [EditorBrowsable(EditorBrowsableState.Never)]
67         public void Apply(Element styleable, bool inheriting = false)
68         {
69             if (styleable == null)
70                 throw new ArgumentNullException(nameof(styleable));
71
72             foreach (var decl in Declarations) {
73                 var property = ((IStylable)styleable).GetProperty(decl.Key, inheriting);
74                 if (property == null)
75                     continue;
76                 if (string.Equals(decl.Value, "initial", StringComparison.OrdinalIgnoreCase))
77                     styleable.ClearValue(property, fromStyle: true);
78                 else {
79                     object value;
80                     if (!convertedValues.TryGetValue(decl, out value))
81                         convertedValues[decl] = (value = Convert(styleable, decl.Value, property));
82                     styleable.SetValue(property, value, fromStyle: true);
83                 }
84             }
85
86             foreach (var child in styleable.LogicalChildrenInternal) {
87                 var ve = child as Element;
88                 if (ve == null)
89                     continue;
90                 Apply(ve, inheriting: true);
91             }
92         }
93
94         // [MethodImpl(MethodImplOptions.AggressiveInlining)]
95         static object Convert(object target, object value, BindableProperty property)
96         {
97             Func<MemberInfo> minforetriever = () =>    property.DeclaringType.GetRuntimeProperty(property.PropertyName) as MemberInfo
98                                                     ?? property.DeclaringType.GetRuntimeMethod("Get" + property.PropertyName, new[] { typeof(BindableObject) }) as MemberInfo;
99             var serviceProvider = new StyleSheetServiceProvider(target, property);
100             // return value.ConvertTo(property.ReturnType, minforetriever, serviceProvider);
101             return null;
102         }
103
104         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
105         [EditorBrowsable(EditorBrowsableState.Never)]
106         public void UnApply(IStylable styleable)
107         {
108             throw new NotImplementedException();
109         }
110     }
111 }