Follow formatting NUI
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI / src / internal / XamlBinding / StyleSheets / StyleSheet.cs
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.IO;
5 using System.Reflection;
6 using System.Runtime.CompilerServices;
7 using System.Xml;
8 using Tizen.NUI.Xaml;
9 using Tizen.NUI.Binding;
10
11 namespace Tizen.NUI.StyleSheets
12 {
13     internal sealed class StyleSheet : IStyle
14     {
15         StyleSheet()
16         {
17         }
18
19         internal IDictionary<Selector, Style> Styles { get; set; } = new Dictionary<Selector, Style>();
20
21         public static StyleSheet FromAssemblyResource(Assembly assembly, string resourceId, IXmlLineInfo lineInfo = null)
22         {
23             using (var stream = assembly.GetManifestResourceStream(resourceId))
24             {
25                 if (stream == null)
26                     throw new XamlParseException($"No resource found for '{resourceId}'.", lineInfo);
27                 using (var reader = new StreamReader(stream))
28                 {
29                     return FromReader(reader);
30                 }
31             }
32         }
33
34         //used by code generated by XamlC. Has to stay public
35         [EditorBrowsable(EditorBrowsableState.Never)]
36         public static StyleSheet FromString(string stylesheet)
37         {
38             if (stylesheet == null)
39                 throw new ArgumentNullException(nameof(stylesheet));
40             using (var reader = new StringReader(stylesheet))
41                 return FromReader(reader);
42         }
43
44         public static StyleSheet FromReader(TextReader reader)
45         {
46             if (reader == null)
47                 throw new ArgumentNullException(nameof(reader));
48
49             return Parse(new CssReader(reader));
50         }
51
52         [MethodImpl(MethodImplOptions.AggressiveInlining)]
53         static StyleSheet Parse(CssReader reader)
54         {
55             var sheet = new StyleSheet();
56
57             Style style = null;
58             var selector = Selector.All;
59
60             int p;
61             bool inStyle = false;
62             reader.SkipWhiteSpaces();
63             while ((p = reader.Peek()) > 0)
64             {
65                 switch ((char)p)
66                 {
67                     case '@':
68                         throw new NotSupportedException("AT-rules not supported");
69                     case '{':
70                         reader.Read();
71                         style = Style.Parse(reader, '}');
72                         inStyle = true;
73                         break;
74                     case '}':
75                         reader.Read();
76                         if (!inStyle)
77                             throw new Exception();
78                         inStyle = false;
79                         sheet.Styles.Add(selector, style);
80                         style = null;
81                         selector = Selector.All;
82                         break;
83                     default:
84                         selector = Selector.Parse(reader, '{');
85                         break;
86                 }
87             }
88             return sheet;
89         }
90
91         Type IStyle.TargetType
92             => typeof(/*VisualElement*/BaseHandle);
93
94         void IStyle.Apply(BindableObject bindable)
95         {
96             var styleable = bindable as Element;
97             if (styleable == null)
98                 return;
99             Apply(styleable);
100         }
101
102         void Apply(Element styleable)
103         {
104             ApplyCore(styleable);
105             foreach (var child in styleable.LogicalChildrenInternal)
106                 ((IStyle)this).Apply(child);
107         }
108
109         void ApplyCore(Element styleable)
110         {
111             var visualStylable = styleable as /*VisualElement*/BaseHandle;
112             if (visualStylable == null)
113                 return;
114             foreach (var kvp in Styles)
115             {
116                 var selector = kvp.Key;
117                 var style = kvp.Value;
118                 // if (!selector.Matches(styleable))
119                 //      continue;
120                 style.Apply(visualStylable);
121             }
122         }
123
124         void IStyle.UnApply(BindableObject bindable)
125         {
126             throw new NotImplementedException();
127         }
128     }
129 }