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.ComponentModel;
19 using System.Collections.Generic;
20 using Tizen.NUI.BaseComponents;
22 namespace Tizen.NUI.Components
25 /// StyleManager is a class to manager all style.
27 [EditorBrowsable(EditorBrowsableState.Never)]
28 public sealed class StyleManager
30 internal const float PointSizeNormal = 12;
31 internal const float PointSizeTitle = 16;
33 private const string defaultThemeName = "DEFAULT"; //"default";
34 private const string wearableThemeName = "WEARABLE"; //"wearable";
36 private string currentThemeName = defaultThemeName;
37 private Dictionary<string, Dictionary<string, StyleBase>> themeStyleSet = new Dictionary<string, Dictionary<string, StyleBase>>();
38 private Dictionary<string, StyleBase> defaultStyleSet = new Dictionary<string, StyleBase>();
41 /// (Theme name, Theme instance)
43 private Dictionary<string, Dictionary<Type, StyleBase>> componentStyleByTheme = new Dictionary<string, Dictionary<Type, StyleBase>>();
46 /// (Theme name, Theme instance)
48 private Dictionary<string, Theme> themeMap = new Dictionary<string, Theme>();
50 private EventHandler<ThemeChangeEventArgs> themeChangeHandler;
52 private Theme currentTheme;
55 /// StyleManager construct.
57 private StyleManager()
59 SetInitialThemeByDeviceProfile();
63 /// An event for the theme changed signal which can be used to subscribe or unsubscribe the event handler provided by the user.<br />
65 [EditorBrowsable(EditorBrowsableState.Never)]
66 public event EventHandler<ThemeChangeEventArgs> ThemeChangedEvent
70 themeChangeHandler += value;
74 themeChangeHandler -= value;
79 /// StyleManager static instance.
81 [EditorBrowsable(EditorBrowsableState.Never)]
82 public static StyleManager Instance { get; } = new StyleManager();
87 [EditorBrowsable(EditorBrowsableState.Never)]
92 return currentThemeName;
97 if (value != null && currentThemeName != value)
99 currentThemeName = value.ToUpperInvariant();
100 themeChangeHandler?.Invoke(null, new ThemeChangeEventArgs { CurrentTheme = currentThemeName });
108 /// Register style in StyleManager.
110 /// <param name="style">Style name.</param>
111 /// <param name="theme">Theme.</param>
112 /// <param name="styleType">Style type.</param>
113 /// <param name="bDefault">Flag to decide if it is default style.</param>
114 [EditorBrowsable(EditorBrowsableState.Never)]
115 public void RegisterStyle(string style, string theme, Type styleType, bool bDefault = false)
119 throw new InvalidOperationException($"style can't be null");
122 if (theme == null || bDefault == true)
124 if (defaultStyleSet.ContainsKey(style))
126 throw new InvalidOperationException($"{style}] already be used");
130 defaultStyleSet.Add(style, Activator.CreateInstance(styleType) as StyleBase);
135 theme = theme.ToUpperInvariant();
137 if (themeStyleSet.ContainsKey(style) && themeStyleSet[style].ContainsKey(theme))
139 throw new InvalidOperationException($"{style}] already be used");
142 if (!themeStyleSet.ContainsKey(style))
144 themeStyleSet.Add(style, new Dictionary<string, StyleBase>());
147 themeStyleSet[style].Add(theme, Activator.CreateInstance(styleType) as StyleBase);
153 /// <param name="style">Style name.</param>
154 /// <returns>The style corresponding to style name .</returns>
155 [EditorBrowsable(EditorBrowsableState.Never)]
156 public ViewStyle GetViewStyle(string style)
163 if (themeStyleSet.ContainsKey(style) && themeStyleSet[style].ContainsKey(Theme))
165 return (themeStyleSet[style][Theme])?.GetViewStyle();
167 else if (defaultStyleSet.ContainsKey(style))
169 return (defaultStyleSet[style])?.GetViewStyle();
176 /// Register a style for a component to theme.
178 /// <param name="targetTheme">The target theme name to register a component style. It theme should be a known one.</param>
179 /// <param name="component">The type of ComponentStyle</param>
180 /// <param name="style">The derived class of StyleBase</param>
181 [EditorBrowsable(EditorBrowsableState.Never)]
182 public void RegisterComponentStyle(string targetTheme, Type component, Type style)
184 if (targetTheme == null || component == null || style == null)
186 throw new ArgumentException("The argument targetTheme must be specified");
189 if (!themeMap.ContainsKey(targetTheme))
191 Tizen.Log.Error("NUI", "The theme name should be a known one.");
195 if (!componentStyleByTheme.ContainsKey(targetTheme))
197 componentStyleByTheme.Add(targetTheme, new Dictionary<Type, StyleBase>());
200 if (componentStyleByTheme[targetTheme].ContainsKey(component))
202 componentStyleByTheme[targetTheme][component] = Activator.CreateInstance(style) as StyleBase;
206 componentStyleByTheme[targetTheme].Add(component, Activator.CreateInstance(style) as StyleBase);
211 /// Get components style in the current theme.
213 /// <param name="component">The type of component</param>
214 /// <returns>The style of the component.</returns>
215 [EditorBrowsable(EditorBrowsableState.Never)]
216 public ViewStyle GetComponentStyle(Type component)
218 if (componentStyleByTheme.ContainsKey(currentThemeName) && componentStyleByTheme[currentThemeName].ContainsKey(component))
220 return componentStyleByTheme[currentThemeName][component].GetViewStyle();
223 return currentTheme.GetComponentStyle(component);
227 /// ThemeChangeEventArgs is a class to record theme change event arguments which will sent to user.
229 [EditorBrowsable(EditorBrowsableState.Never)]
230 public class ThemeChangeEventArgs : EventArgs
235 [EditorBrowsable(EditorBrowsableState.Never)]
236 public string CurrentTheme;
239 internal static string GetFrameworkResourcePath(string resourceFileName)
241 return "/usr/share/dotnet.tizen/framework/res/" + resourceFileName;
244 private void SetInitialThemeByDeviceProfile()
246 Theme wearableTheme = WearableTheme.Instance;
247 Theme defaultTheme = DefaultTheme.Instance;
248 themeMap.Add(wearableThemeName, wearableTheme);
249 themeMap.Add(defaultThemeName, defaultTheme);
251 currentThemeName = defaultThemeName;
252 currentTheme = defaultTheme;
254 string currentProfile;
258 System.Information.TryGetValue<string>("tizen.org/feature/profile", out currentProfile);
259 Tizen.Log.Info("NUI", "Profile for initial theme found : " + currentProfile);
263 Tizen.Log.Error("NUI", "Unknown device profile\n");
267 if (string.Equals(currentProfile, wearableThemeName))
269 currentThemeName = wearableThemeName;
270 currentTheme = wearableTheme;
274 private void UpdateTheme()
276 if (themeMap.ContainsKey(currentThemeName))
278 currentTheme = themeMap[currentThemeName];
282 currentTheme = DefaultTheme.Instance;