6f4b39728ca3ea96e0e9a13e9611257ee000a17d
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.Components / Utils / StyleManager.cs
1 /*
2  * Copyright(c) 2019 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17 using System;
18 using System.Collections.Generic;
19 using Tizen.NUI.BaseComponents;
20
21 namespace Tizen.NUI.Components
22 {
23     /// <summary>
24     /// StyleManager is a class to manager all style.
25     /// </summary>
26     /// <since_tizen> 8 </since_tizen>
27     public sealed class StyleManager
28     {
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;
35
36         /// <summary>
37         /// StyleManager construct.
38         /// </summary>
39         private StyleManager()
40         {
41         }
42
43         /// <summary>
44         /// An event for the theme changed signal which can be used to subscribe or unsubscribe the event handler provided by the user.<br />
45         /// </summary>
46         /// <since_tizen> 8 </since_tizen>
47         public event EventHandler<ThemeChangeEventArgs> ThemeChangedEvent
48         {
49             add
50             {
51                 themeChangeHander += value;
52             }
53             remove
54             {
55                 themeChangeHander -= value;
56             }
57         }
58
59         /// <summary>
60         /// StyleManager static instance.
61         /// </summary>
62         /// <since_tizen> 8 </since_tizen>
63         public static StyleManager Instance { get; internal set; } = new StyleManager();
64
65         /// <summary>
66         /// Style theme.
67         /// </summary>
68         /// <since_tizen> 8 </since_tizen>
69         public string Theme
70         {
71             get
72             {
73                 return theme;
74             }
75
76             set
77             {
78                 if (theme != value)
79                 {
80                     theme = value;
81                     themeChangeHander?.Invoke(null, new ThemeChangeEventArgs { CurrentTheme = theme });
82                 }
83             }
84         }
85
86         /// <summary>
87         /// Register style in StyleManager.
88         /// </summary>
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)
95         {
96             if (style == null)
97             {
98                 throw new InvalidOperationException($"style can't be null");
99             }
100
101             if (theme == null || bDefault == true)
102             {
103                 if (defaultStyleSet.ContainsKey(style))
104                 {
105                     throw new InvalidOperationException($"{style}] already be used");
106                 }
107                 else
108                 {
109                     defaultStyleSet.Add(style, Activator.CreateInstance(styleType) as StyleBase);
110                 }
111                 return;
112             }
113
114             if (themeStyleSet.ContainsKey(style) && themeStyleSet[style].ContainsKey(theme))
115             {
116                 throw new InvalidOperationException($"{style}] already be used");
117             }
118
119             if (!themeStyleSet.ContainsKey(style))
120             {
121                 themeStyleSet.Add(style, new Dictionary<string, StyleBase>());
122             }
123
124             themeStyleSet[style].Add(theme, Activator.CreateInstance(styleType) as StyleBase);
125         }
126
127         /// <summary>
128         /// Get style.
129         /// </summary>
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)
134         {
135             if (style == null)
136             {
137                 return null;
138             }
139
140             if (themeStyleSet.ContainsKey(style) && themeStyleSet[style].ContainsKey(Theme))
141             {
142                 return (themeStyleSet[style][Theme])?.GetViewStyle();
143             }
144             else if (defaultStyleSet.ContainsKey(style))
145             {
146                 return (defaultStyleSet[style])?.GetViewStyle();
147             }
148
149             return null;
150         }
151
152         /// <summary>
153         /// Register a style for a component to theme.
154         /// </summary>
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)
160         {
161             if (targetTheme == null)
162             {
163                 throw new ArgumentException("The argument targetTheme must be specified");
164             }
165
166             if (defaultThemeName.Equals(targetTheme))
167             {
168                 // Ensure default component styles have loaded before override custom default style
169                 LoadDefaultComponentStyle();
170             }
171
172             if (!componentStyleByTheme.ContainsKey(targetTheme))
173             {
174                 componentStyleByTheme.Add(targetTheme, new Dictionary<Type, StyleBase>());
175             }
176
177             if (componentStyleByTheme[targetTheme].ContainsKey(component))
178             {
179                 componentStyleByTheme[targetTheme][component] = Activator.CreateInstance(style) as StyleBase;
180             }
181             else
182             {
183                 componentStyleByTheme[targetTheme].Add(component, Activator.CreateInstance(style) as StyleBase);
184             }
185         }
186
187         /// <summary>
188         /// Get components style in the current theme.
189         /// </summary>
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)
194         {
195             var currentTheme = theme;
196
197             if (!componentStyleByTheme.ContainsKey(theme))
198             {
199                 currentTheme = defaultThemeName;
200             }
201
202             if (defaultThemeName.Equals(currentTheme))
203             {
204                 LoadDefaultComponentStyle();
205             }
206
207             if (!componentStyleByTheme[currentTheme].ContainsKey(component))
208             {
209                 return null;
210             }
211
212             return (componentStyleByTheme[currentTheme][component])?.GetViewStyle();
213         }
214
215         /// <summary>
216         /// ThemeChangeEventArgs is a class to record theme change event arguments which will sent to user.
217         /// </summary>
218         /// <since_tizen> 8 </since_tizen>
219         public class ThemeChangeEventArgs : EventArgs
220         {
221             /// <summary>
222             /// CurrentTheme
223             /// </summary>
224             /// <since_tizen> 8 </since_tizen>
225             public string CurrentTheme;
226         }
227
228         private void LoadDefaultComponentStyle()
229         {
230             if (componentStyleByTheme.ContainsKey(defaultThemeName))
231             {
232                 return;
233             }
234
235             componentStyleByTheme.Add(defaultThemeName, new Dictionary<Type, StyleBase>());
236
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());
249         }
250     }
251 }