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 private static readonly string defaultThemeName = "default";
30 private string theme = defaultThemeName;
31 private Dictionary<string, Dictionary<string, StyleBase>> themeStyleSet = new Dictionary<string, Dictionary<string, StyleBase>>();
32 private Dictionary<string, StyleBase> defaultStyleSet = new Dictionary<string, StyleBase>();
33 private Dictionary<string, Dictionary<Type, StyleBase>> componentStyleByTheme = new Dictionary<string, Dictionary<Type, StyleBase>>();
34 private EventHandler<ThemeChangeEventArgs> themeChangeHander;
37 /// StyleManager construct.
39 private StyleManager()
44 /// An event for the theme changed signal which can be used to subscribe or unsubscribe the event handler provided by the user.<br />
46 /// <since_tizen> 8 </since_tizen>
47 public event EventHandler<ThemeChangeEventArgs> ThemeChangedEvent
51 themeChangeHander += value;
55 themeChangeHander -= value;
60 /// StyleManager static instance.
62 /// <since_tizen> 8 </since_tizen>
63 public static StyleManager Instance { get; internal set; } = new StyleManager();
68 /// <since_tizen> 8 </since_tizen>
81 themeChangeHander?.Invoke(null, new ThemeChangeEventArgs { CurrentTheme = theme });
87 /// Register style in StyleManager.
89 /// <param name="style">Style name.</param>
90 /// <param name="theme">Theme.</param>
91 /// <param name="styleType">Style type.</param>
92 /// <param name="bDefault">Flag to decide if it is default style.</param>
93 /// <since_tizen> 8 </since_tizen>
94 public void RegisterStyle(string style, string theme, Type styleType, bool bDefault = false)
98 throw new InvalidOperationException($"style can't be null");
101 if (theme == null || bDefault == true)
103 if (defaultStyleSet.ContainsKey(style))
105 throw new InvalidOperationException($"{style}] already be used");
109 defaultStyleSet.Add(style, Activator.CreateInstance(styleType) as StyleBase);
114 if (themeStyleSet.ContainsKey(style) && themeStyleSet[style].ContainsKey(theme))
116 throw new InvalidOperationException($"{style}] already be used");
119 if (!themeStyleSet.ContainsKey(style))
121 themeStyleSet.Add(style, new Dictionary<string, StyleBase>());
124 themeStyleSet[style].Add(theme, Activator.CreateInstance(styleType) as StyleBase);
130 /// <param name="style">Style name.</param>
131 /// <returns>The style corresponding to style name .</returns>
132 /// <since_tizen> 8 </since_tizen>
133 public ViewStyle GetViewStyle(string style)
140 if (themeStyleSet.ContainsKey(style) && themeStyleSet[style].ContainsKey(Theme))
142 return (themeStyleSet[style][Theme])?.GetViewStyle();
144 else if (defaultStyleSet.ContainsKey(style))
146 return (defaultStyleSet[style])?.GetViewStyle();
153 /// Register a style for a component to theme.
155 /// <param name="targetTheme">Theme</param>
156 /// <param name="component">The type of ComponentStyle</param>
157 /// <param name="style">The type of style</param>
158 /// <since_tizen> 8 </since_tizen>
159 public void RegisterComponentStyle(string targetTheme, Type component, Type style)
161 if (targetTheme == null)
163 throw new ArgumentException("The argument targetTheme must be specified");
166 if (defaultThemeName.Equals(targetTheme))
168 // Ensure default component styles have loaded before override custom default style
169 LoadDefaultComponentStyle();
172 if (!componentStyleByTheme.ContainsKey(targetTheme))
174 componentStyleByTheme.Add(targetTheme, new Dictionary<Type, StyleBase>());
177 if (componentStyleByTheme[targetTheme].ContainsKey(component))
179 componentStyleByTheme[targetTheme][component] = Activator.CreateInstance(style) as StyleBase;
183 componentStyleByTheme[targetTheme].Add(component, Activator.CreateInstance(style) as StyleBase);
188 /// Get components style in the current theme.
190 /// <param name="component">The type of component</param>
191 /// <returns>The style of the component.</returns>
192 /// <since_tizen> 8 </since_tizen>
193 public ViewStyle GetComponentStyle(Type component)
195 var currentTheme = theme;
197 if (!componentStyleByTheme.ContainsKey(theme))
199 currentTheme = defaultThemeName;
202 if (defaultThemeName.Equals(currentTheme))
204 LoadDefaultComponentStyle();
207 if (!componentStyleByTheme[currentTheme].ContainsKey(component))
212 return (componentStyleByTheme[currentTheme][component])?.GetViewStyle();
216 /// ThemeChangeEventArgs is a class to record theme change event arguments which will sent to user.
218 /// <since_tizen> 8 </since_tizen>
219 public class ThemeChangeEventArgs : EventArgs
224 /// <since_tizen> 8 </since_tizen>
225 public string CurrentTheme;
228 private void LoadDefaultComponentStyle()
230 if (componentStyleByTheme.ContainsKey(defaultThemeName))
235 componentStyleByTheme.Add(defaultThemeName, new Dictionary<Type, StyleBase>());
237 var defaultComponentsStyle = componentStyleByTheme[defaultThemeName];
238 defaultComponentsStyle.Add(typeof(Tizen.NUI.Components.Button), new DefaultButtonStyle());
239 defaultComponentsStyle.Add(typeof(Tizen.NUI.Components.CheckBox), new DefaultCheckBoxStyle());
240 defaultComponentsStyle.Add(typeof(Tizen.NUI.Components.RadioButton), new DefaultRadioButtonStyle());
241 defaultComponentsStyle.Add(typeof(Tizen.NUI.Components.Switch), new DefaultSwitchStyle());
242 defaultComponentsStyle.Add(typeof(Tizen.NUI.Components.Progress), new DefaultProgressStyle());
243 defaultComponentsStyle.Add(typeof(Tizen.NUI.Components.Slider), new DefaultSliderStyle());
244 defaultComponentsStyle.Add(typeof(Tizen.NUI.Components.Toast), new DefaultToastStyle());
245 defaultComponentsStyle.Add(typeof(Tizen.NUI.Components.Popup), new DefaultPopupStyle());
246 defaultComponentsStyle.Add(typeof(Tizen.NUI.Components.DropDown), new DefaultDropDownStyle());
247 defaultComponentsStyle.Add(typeof(Tizen.NUI.Components.DropDown.DropDownDataItem), new DefaultDropDownItemStyle());
248 defaultComponentsStyle.Add(typeof(Tizen.NUI.Components.Tab), new DefaultTabStyle());