2 * Copyright(c) 2020 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;
20 using System.Reflection;
22 using Tizen.NUI.BaseComponents;
23 using Tizen.NUI.Binding;
30 /// Basically, the Theme is a dictionary of <seealso cref="ViewStyle"/>s that can decorate NUI <seealso cref="View"/>s.
31 /// Each ViewStyle item is identified by a string key that can be matched the <seealso cref="View.StyleName"/>.
34 /// The main purpose of providing Theme is to separate style details from the structure.
35 /// Managing style separately makes it easier to customize the look of application by user context.
36 /// Also since a Theme can be created from xaml file, it can be treated as a resource.
37 /// This enables sharing styles with other applications.
40 [EditorBrowsable(EditorBrowsableState.Never)]
41 public class Theme : BindableObject
43 private readonly Dictionary<string, ViewStyle> map;
44 private IEnumerable<KeyValuePair<string, string>> changedResources = null;
45 private string baseTheme;
46 ResourceDictionary resources;
48 /// <summary>Create an empty theme.</summary>
49 [EditorBrowsable(EditorBrowsableState.Never)]
52 map = new Dictionary<string, ViewStyle>();
55 /// <summary>Create a new theme from the xaml file.</summary>
56 /// <param name="xamlFile">An absolute path to the xaml file.</param>
57 /// <exception cref="ArgumentNullException">Thrown when the given xamlFile is null or empty string.</exception>
58 /// <exception cref="global::System.IO.IOException">Thrown when there are file IO problems.</exception>
59 /// <exception cref="Exception">Thrown when the content of the xaml file is not valid xaml form.</exception>
60 [EditorBrowsable(EditorBrowsableState.Never)]
61 public Theme(string xamlFile) : this()
63 if (string.IsNullOrEmpty(xamlFile))
65 throw new ArgumentNullException(nameof(xamlFile), "The xaml file path cannot be null or empty string");
70 using (var reader = XmlReader.Create(xamlFile))
72 XamlLoader.Load(this, reader);
75 catch (System.IO.IOException)
77 Tizen.Log.Error("NUI", $"Could not load \"{xamlFile}\".\n");
82 Tizen.Log.Error("NUI", $"Could not parse \"{xamlFile}\".\n");
83 Tizen.Log.Error("NUI", "Make sure the all used assemblies (e.g. Tizen.NUI.Components) are included in the application project.\n");
84 Tizen.Log.Error("NUI", "Make sure the type and namespace are correct.\n");
90 /// The string key to identify the Theme.
92 [EditorBrowsable(EditorBrowsableState.Never)]
93 public string Id { get; set; }
96 /// For Xaml use only.
97 /// The bulit-in theme id that will be used as base of this.
98 /// View styles with same key are merged.
100 internal string BasedOn
107 if (string.IsNullOrEmpty(baseTheme)) return;
109 var baseThemeInstance = ThemeManager.GetBuiltinTheme(baseTheme);
111 if (baseThemeInstance != null)
113 foreach (var item in baseThemeInstance)
115 var baseStyle = item.Value?.Clone();
116 if (map.ContainsKey(item.Key))
118 baseStyle.Merge(map[item.Key]);
120 map[item.Key] = baseStyle;
127 [EditorBrowsable(EditorBrowsableState.Never)]
128 public bool IsResourcesCreated => resources != null;
131 [EditorBrowsable(EditorBrowsableState.Never)]
132 internal ResourceDictionary Resources
136 if (resources != null)
138 resources = new ResourceDictionary();
139 ((IResourceDictionary)resources).ValuesChanged += OnThemeResourcesChanged;
144 if (resources == value)
147 if (resources != null)
149 ((IResourceDictionary)resources).ValuesChanged -= OnThemeResourcesChanged;
152 if (resources != null)
154 // This callback will be removed when Resource.Source is assigned.
155 ((IResourceDictionary)resources).ValuesChanged += OnThemeResourcesChanged;
161 /// For Xaml use only.
162 /// Note that it is not a normal indexer.
163 /// Setter will merge the new value with existing item.
165 internal ViewStyle this[string styleName]
167 get => map[styleName];
172 map.Remove(styleName);
176 if (map.TryGetValue(styleName, out ViewStyle style) && style != null && style.GetType() == value.GetType())
182 map[styleName] = value;
187 internal int Count => map.Count;
190 /// Get an enumerator of the theme.
192 [EditorBrowsable(EditorBrowsableState.Never)]
193 public IEnumerator<KeyValuePair<string, ViewStyle>> GetEnumerator() => map.GetEnumerator();
196 /// Removes all styles in the theme.
198 [EditorBrowsable(EditorBrowsableState.Never)]
199 public void Clear() => map.Clear();
202 /// Determines whether the theme contains the specified style name.
204 /// <exception cref="ArgumentNullException">The given style name is null.</exception>
205 [EditorBrowsable(EditorBrowsableState.Never)]
206 public bool HasStyle(string styleName) => map.ContainsKey(styleName);
209 /// Removes the style with the specified style name.
211 /// <exception cref="ArgumentNullException">The given style name is null.</exception>
212 [EditorBrowsable(EditorBrowsableState.Never)]
213 public bool RemoveStyle(string styleName) => map.Remove(styleName);
216 /// Gets a style of given style name.
218 /// <param name="styleName">The string key to find a ViewStyle.</param>
219 /// <returns>Founded style instance.</returns>
220 [EditorBrowsable(EditorBrowsableState.Never)]
221 public ViewStyle GetStyle(string styleName) => map.ContainsKey(styleName) ? map[styleName] : null;
224 /// Gets a style of given view type.
226 /// <param name="viewType">The type of View.</param>
227 /// <returns>Founded style instance.</returns>
228 /// <exception cref="ArgumentNullException">Thrown when the given viewType is null.</exception>
229 [EditorBrowsable(EditorBrowsableState.Never)]
230 public ViewStyle GetStyle(Type viewType)
232 var currentType = viewType ?? throw new ArgumentNullException(nameof(viewType));
233 ViewStyle resultStyle = null;
237 if (currentType.Equals(typeof(View))) break;
238 resultStyle = GetStyle(currentType.FullName);
239 currentType = currentType.BaseType;
241 while (resultStyle == null && currentType != null);
247 /// Adds the specified style name and value to the theme.
248 /// This replace existing value if the theme already has a style with given name.
250 /// <param name="styleName">The style name to add.</param>
251 /// <param name="value">The style instance to add.</param>
252 [EditorBrowsable(EditorBrowsableState.Never)]
253 public void AddStyle(string styleName, ViewStyle value) => map[styleName] = value?.Clone();
257 [EditorBrowsable(EditorBrowsableState.Never)]
258 public object Clone()
260 var result = new Theme()
263 Resources = Resources
266 foreach (var item in this)
268 result.AddStyle(item.Key, item.Value);
273 /// <summary>Merge other Theme into this.</summary>
274 /// <param name="xamlFile">An absolute path to the xaml file of the theme.</param>
275 /// <exception cref="ArgumentException">Thrown when the given xamlFile is null or empty string.</exception>
276 /// <exception cref="global::System.IO.IOException">Thrown when there are file IO problems.</exception>
277 /// <exception cref="XamlParseException">Thrown when the content of the xaml file is not valid xaml form.</exception>
278 [EditorBrowsable(EditorBrowsableState.Never)]
279 public void Merge(string xamlFile)
281 MergeWithoutClone(new Theme(xamlFile));
284 /// <summary>Merge other Theme into this.</summary>
285 /// <param name="theme">The Theme.</param>
286 [EditorBrowsable(EditorBrowsableState.Never)]
287 public void Merge(Theme theme)
290 throw new ArgumentNullException(nameof(theme));
292 if (Id == null) Id = theme.Id;
294 foreach (var item in theme)
296 if (item.Value == null)
298 map[item.Key] = null;
300 else if (map.ContainsKey(item.Key) && !item.Value.SolidNull)
302 map[item.Key].Merge(item.Value);
306 map[item.Key] = item.Value.Clone();
311 internal void MergeWithoutClone(Theme theme)
314 throw new ArgumentNullException(nameof(theme));
316 if (Id == null) Id = theme.Id;
318 foreach (var item in theme)
320 if (item.Value == null)
322 map[item.Key] = null;
324 else if (map.ContainsKey(item.Key) && !item.Value.SolidNull)
326 map[item.Key].Merge(item.Value);
330 map[item.Key] = item.Value;
334 if (theme.resources != null)
336 foreach (var res in theme.resources)
338 Resources[res.Key] = res.Value;
344 /// Internal use only.
346 internal void AddStyleWithoutClone(string styleName, ViewStyle value) => map[styleName] = value;
348 internal void SetChangedResources(IEnumerable<KeyValuePair<string, string>> changedResources)
350 this.changedResources = changedResources;
353 internal void OnThemeResourcesChanged(object sender, ResourcesChangedEventArgs e) => OnThemeResourcesChanged();
355 internal void OnThemeResourcesChanged()
357 if (changedResources != null)
359 // To avoid loop in infinite, remove OnThemeResourcesChanged callback.
360 ((IResourceDictionary)resources).ValuesChanged -= OnThemeResourcesChanged;
361 foreach (var changedResource in changedResources)
363 if (resources.TryGetValue(changedResource.Key, out object resourceValue))
365 string changedValue = changedResource.Value;
367 // check NUIResourcePath
368 string[] changedValues = changedValue.Split('/');
369 if (changedValues[0] == "NUIResourcePath")
371 changedValue = changedValues[1];
374 Type toType = resourceValue.GetType();
375 resources[changedResource.Key] = changedValue.ConvertTo(toType, () => toType.GetTypeInfo(), null);