Follow formatting NUI
[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             {
27                 switch (unchecked((char)p))
28                 {
29                     case ':':
30                         reader.Read();
31                         readingName = false;
32                         reader.SkipWhiteSpaces();
33                         break;
34                     case ';':
35                         reader.Read();
36                         if (!string.IsNullOrEmpty(propertyName) && !string.IsNullOrEmpty(propertyValue))
37                             style.Declarations.Add(propertyName, propertyValue);
38                         propertyName = propertyValue = null;
39                         readingName = true;
40                         reader.SkipWhiteSpaces();
41                         break;
42                     default:
43                         if ((char)p == stopChar)
44                             return style;
45
46                         if (readingName)
47                         {
48                             propertyName = reader.ReadIdent();
49                             if (propertyName == null)
50                                 throw new Exception();
51                         }
52                         else
53                             propertyValue = reader.ReadUntil(stopChar, ';', ':');
54                         break;
55                 }
56             }
57             return style;
58         }
59
60         public void Apply(/*VisualElement*/BaseHandle styleable, bool inheriting = false)
61         {
62             if (styleable == null)
63                 throw new ArgumentNullException(nameof(styleable));
64
65             foreach (var decl in Declarations)
66             {
67                 var property = ((IStylable)styleable).GetProperty(decl.Key, inheriting);
68                 if (property == null)
69                     continue;
70                 if (string.Equals(decl.Value, "initial", StringComparison.OrdinalIgnoreCase))
71                     styleable.ClearValue(property, fromStyle: true);
72                 else
73                 {
74                     object value;
75                     if (!convertedValues.TryGetValue(decl, out value))
76                         convertedValues[decl] = (value = Convert(styleable, decl.Value, property));
77                     styleable.SetValue(property, value, fromStyle: true);
78                 }
79             }
80
81             foreach (var child in styleable.LogicalChildrenInternal)
82             {
83                 var ve = child as /*VisualElement*/BaseHandle;
84                 if (ve == null)
85                     continue;
86                 Apply(ve, inheriting: true);
87             }
88         }
89
90         // [MethodImpl(MethodImplOptions.AggressiveInlining)]
91         static object Convert(object target, object value, BindableProperty property)
92         {
93             Func<MemberInfo> minforetriever = () => property.DeclaringType.GetRuntimeProperty(property.PropertyName) as MemberInfo
94                                                     ?? property.DeclaringType.GetRuntimeMethod("Get" + property.PropertyName, new[] { typeof(BindableObject) }) as MemberInfo;
95             var serviceProvider = new StyleSheetServiceProvider(target, property);
96             // return value.ConvertTo(property.ReturnType, minforetriever, serviceProvider);
97             return null;
98         }
99
100         public void UnApply(IStylable styleable)
101         {
102             throw new NotImplementedException();
103         }
104     }
105 }