2 * Copyright(c) 2019 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 Tizen.NUI.BaseComponents;
21 namespace Tizen.NUI.Components
24 /// StyleManager is a class to manager all style.
26 /// <since_tizen> 8 </since_tizen>
27 public sealed class StyleManager
29 internal const float PointSizeNormal = 12;
30 internal const float PointSizeTitle = 16;
32 private const string defaultThemeName = "default";
33 private const string wearableThemeName = "wearable";
35 private string currentThemeName = defaultThemeName;
36 private Dictionary<string, Dictionary<string, StyleBase>> themeStyleSet = new Dictionary<string, Dictionary<string, StyleBase>>();
37 private Dictionary<string, StyleBase> defaultStyleSet = new Dictionary<string, StyleBase>();
40 /// (Theme name, Theme instance)
42 private Dictionary<string, Dictionary<Type, StyleBase>> componentStyleByTheme = new Dictionary<string, Dictionary<Type, StyleBase>>();
45 /// (Theme name, Theme instance)
47 private Dictionary<string, Theme> themeMap = new Dictionary<string, Theme>();
49 private EventHandler<ThemeChangeEventArgs> themeChangeHander;
51 private Theme currentTheme;
54 /// StyleManager construct.
56 private StyleManager()
58 SetInitialThemeByDeviceProfile();
62 /// An event for the theme changed signal which can be used to subscribe or unsubscribe the event handler provided by the user.<br />
64 /// <since_tizen> 8 </since_tizen>
65 public event EventHandler<ThemeChangeEventArgs> ThemeChangedEvent
69 themeChangeHander += value;
73 themeChangeHander -= value;
78 /// StyleManager static instance.
80 /// <since_tizen> 8 </since_tizen>
81 public static StyleManager Instance { get; } = new StyleManager();
86 /// <since_tizen> 8 </since_tizen>
91 return currentThemeName;
96 if (currentThemeName != value)
98 currentThemeName = value.ToLower();
99 themeChangeHander?.Invoke(null, new ThemeChangeEventArgs { CurrentTheme = currentThemeName });
107 /// Register style in StyleManager.
109 /// <param name="style">Style name.</param>
110 /// <param name="theme">Theme.</param>
111 /// <param name="styleType">Style type.</param>
112 /// <param name="bDefault">Flag to decide if it is default style.</param>
113 /// <since_tizen> 8 </since_tizen>
114 public void RegisterStyle(string style, string theme, Type styleType, bool bDefault = false)
118 throw new InvalidOperationException($"style can't be null");
121 if (theme == null || bDefault == true)
123 if (defaultStyleSet.ContainsKey(style))
125 throw new InvalidOperationException($"{style}] already be used");
129 defaultStyleSet.Add(style, Activator.CreateInstance(styleType) as StyleBase);
134 if (themeStyleSet.ContainsKey(style) && themeStyleSet[style].ContainsKey(theme))
136 throw new InvalidOperationException($"{style}] already be used");
139 if (!themeStyleSet.ContainsKey(style))
141 themeStyleSet.Add(style, new Dictionary<string, StyleBase>());
144 themeStyleSet[style].Add(theme, Activator.CreateInstance(styleType) as StyleBase);
150 /// <param name="style">Style name.</param>
151 /// <returns>The style corresponding to style name .</returns>
152 /// <since_tizen> 8 </since_tizen>
153 public ViewStyle GetViewStyle(string style)
160 if (themeStyleSet.ContainsKey(style) && themeStyleSet[style].ContainsKey(Theme))
162 return (themeStyleSet[style][Theme])?.GetViewStyle();
164 else if (defaultStyleSet.ContainsKey(style))
166 return (defaultStyleSet[style])?.GetViewStyle();
173 /// Register a style for a component to theme.
175 /// <param name="targetTheme">The target theme name to register a component style. It theme should be a known one.</param>
176 /// <param name="component">The type of ComponentStyle</param>
177 /// <param name="style">The derived class of StyleBase</param>
178 /// <since_tizen> 8 </since_tizen>
179 public void RegisterComponentStyle(string targetTheme, Type component, Type style)
181 if (targetTheme == null || component == null || style == null)
183 throw new ArgumentException("The argument targetTheme must be specified");
186 if (!themeMap.ContainsKey(targetTheme))
188 Tizen.Log.Error("NUI", "The theme name should be a known one.");
192 if (!componentStyleByTheme.ContainsKey(targetTheme))
194 componentStyleByTheme.Add(targetTheme, new Dictionary<Type, StyleBase>());
197 if (componentStyleByTheme[targetTheme].ContainsKey(component))
199 componentStyleByTheme[targetTheme][component] = Activator.CreateInstance(style) as StyleBase;
203 componentStyleByTheme[targetTheme].Add(component, Activator.CreateInstance(style) as StyleBase);
208 /// Get components style in the current theme.
210 /// <param name="component">The type of component</param>
211 /// <returns>The style of the component.</returns>
212 /// <since_tizen> 8 </since_tizen>
213 public ViewStyle GetComponentStyle(Type component)
215 if (componentStyleByTheme.ContainsKey(currentThemeName) && componentStyleByTheme[currentThemeName].ContainsKey(component))
217 return componentStyleByTheme[currentThemeName][component].GetViewStyle();
220 return currentTheme.GetComponentStyle(component);
224 /// ThemeChangeEventArgs is a class to record theme change event arguments which will sent to user.
226 /// <since_tizen> 8 </since_tizen>
227 public class ThemeChangeEventArgs : EventArgs
232 /// <since_tizen> 8 </since_tizen>
233 public string CurrentTheme;
236 internal static string GetFrameworkResourcePath(string resourceFileName)
238 return "/usr/share/dotnet.tizen/framework/res/" + resourceFileName;
241 private void SetInitialThemeByDeviceProfile()
243 themeMap.Add(wearableThemeName, WearableTheme.Instance);
244 themeMap.Add(defaultThemeName, DefaultTheme.Instance);
246 currentThemeName = defaultThemeName;
247 currentTheme = DefaultTheme.Instance;
249 string currentProfile;
253 System.Information.TryGetValue<string>("tizen.org/feature/profile", out currentProfile);
254 Tizen.Log.Info("NUI", "Profile for initial theme found : " + currentProfile);
258 Tizen.Log.Error("NUI", "Unknown device profile\n");
262 if (string.Equals(currentProfile, wearableThemeName))
264 currentThemeName = wearableThemeName;
265 currentTheme = WearableTheme.Instance;
269 private void UpdateTheme()
271 if (themeMap.ContainsKey(currentThemeName))
273 currentTheme = themeMap[currentThemeName];
277 currentTheme = DefaultTheme.Instance;