2 * Copyright(c) 2022 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 using System.Collections.Generic;
19 using System.ComponentModel;
21 using System.Reflection;
22 using System.Runtime.CompilerServices;
25 using Tizen.NUI.Binding;
27 namespace Tizen.NUI.StyleSheets
29 internal sealed class StyleSheet : IStyle
35 internal IDictionary<Selector, Style> Styles { get; set; } = new Dictionary<Selector, Style>();
37 public static StyleSheet FromAssemblyResource(Assembly assembly, string resourceId, IXmlLineInfo lineInfo = null)
39 using (var stream = assembly.GetManifestResourceStream(resourceId)) {
41 throw new XamlParseException($"No resource found for '{resourceId}'.", lineInfo);
42 using (var reader = new StreamReader(stream)) {
43 return FromReader(reader);
48 //used by code generated by XamlC. Has to stay public
49 [EditorBrowsable(EditorBrowsableState.Never)]
50 public static StyleSheet FromString(string stylesheet)
52 if (stylesheet == null)
53 throw new ArgumentNullException(nameof(stylesheet));
54 using (var reader = new StringReader(stylesheet))
55 return FromReader(reader);
58 public static StyleSheet FromReader(TextReader reader)
61 throw new ArgumentNullException(nameof(reader));
63 return Parse(new CssReader(reader));
66 [MethodImpl(MethodImplOptions.AggressiveInlining)]
67 static StyleSheet Parse(CssReader reader)
69 var sheet = new StyleSheet();
72 var selector = Selector.All;
76 reader.SkipWhiteSpaces();
77 while ((p = reader.Peek()) > 0) {
80 throw new NotSupportedException("AT-rules not supported");
83 style = Style.Parse(reader, '}');
89 throw new Exception();
91 sheet.Styles.Add(selector, style);
93 selector = Selector.All;
96 selector = Selector.Parse(reader, '{');
103 Type IStyle.TargetType
106 void IStyle.Apply(BindableObject bindable)
108 var styleable = bindable as Element;
109 if (styleable == null)
114 void Apply(Element styleable)
116 ApplyCore(styleable);
117 foreach (var child in styleable.LogicalChildrenInternal)
118 ((IStyle)this).Apply(child);
121 void ApplyCore(Element styleable)
123 var visualStylable = styleable as Element;
124 if (visualStylable == null)
126 foreach (var kvp in Styles) {
127 var selector = kvp.Key;
128 var style = kvp.Value;
129 // if (!selector.Matches(styleable))
131 style.Apply(visualStylable);
135 void IStyle.UnApply(BindableObject bindable)
137 throw new NotImplementedException();