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