94d9796cb62b7af914a50fe7edc213c9e1ffb9b4
[platform/core/csapi/tizenfx.git] / src / Tizen.NUI.CommonUI / 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 System.ComponentModel;
20
21 namespace Tizen.NUI.CommonUI
22 {
23     /// <summary>
24     /// StyleManager is a class to manager all style.
25     /// </summary>
26     /// <since_tizen> 6 </since_tizen>
27     /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
28     [EditorBrowsable(EditorBrowsableState.Never)]
29     public sealed class StyleManager
30     {
31         private string theme = "default";
32         private Dictionary<string, Dictionary<string, Type>> themeStyleSet = new Dictionary<string, Dictionary<string, Type>>();
33         private Dictionary<string, Type> defaultStyleSet = new Dictionary<string, Type>();
34         private EventHandler<ThemeChangeEventArgs> themeChangeHander;
35
36         /// <summary>
37         /// StyleManager construct.
38         /// </summary>
39         /// <since_tizen> 6 </since_tizen>
40         private StyleManager()
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> 6 </since_tizen>
47         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
48         [EditorBrowsable(EditorBrowsableState.Never)]
49         public event EventHandler<ThemeChangeEventArgs> ThemeChangedEvent
50         {
51             add
52             {
53                 themeChangeHander += value;
54             }
55             remove
56             {
57                 themeChangeHander -= value;
58             }
59         }
60
61         /// <summary>
62         /// StyleManager static instance.
63         /// </summary>
64         /// <since_tizen> 6 </since_tizen>
65         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
66         [EditorBrowsable(EditorBrowsableState.Never)]
67         public static StyleManager Instance { get; } = new StyleManager();
68
69         /// <summary>
70         /// Style theme.
71         /// </summary>
72         /// <since_tizen> 6 </since_tizen>
73         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
74         [EditorBrowsable(EditorBrowsableState.Never)]
75         public string Theme
76         {
77             get
78             {
79                 return theme;
80             }
81
82             set
83             {
84                 if(theme != value)
85                 {
86                     theme = value;
87                     themeChangeHander?.Invoke(null, new ThemeChangeEventArgs { CurrentTheme = theme });
88                 }
89             }
90         }
91
92         /// <summary>
93         /// Register style in StyleManager.
94         /// </summary>
95         /// <param name="style">Style name.</param>
96         /// <param name="theme">Theme.</param>
97         /// <param name="styleType">Style type.</param>
98         /// <param name="bDefault">Flag to decide if it is default style.</param>
99         /// <since_tizen> 6 </since_tizen>
100         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
101         [EditorBrowsable(EditorBrowsableState.Never)]
102         public void RegisterStyle(string style, string theme, Type styleType, bool bDefault = false)
103         {
104             if(style == null)
105             {
106                 throw new InvalidOperationException($"style can't be null");
107             }
108
109             if(theme == null || bDefault == true)
110             {
111                 if(defaultStyleSet.ContainsKey(style))
112                 {
113                     throw new InvalidOperationException($"{style}] already be used");
114                 }
115                 else
116                 {
117                     defaultStyleSet.Add(style, styleType);
118                 }
119                 return;
120             }
121
122             if(themeStyleSet.ContainsKey(style) && themeStyleSet[style].ContainsKey(theme))
123             {
124                 throw new InvalidOperationException($"{style}] already be used");
125             }
126             if(!themeStyleSet.ContainsKey(style))
127             {
128                 themeStyleSet.Add(style, new Dictionary<string, Type>());
129             }
130             themeStyleSet[style].Add(theme, styleType);
131         }
132
133         /// <summary>
134         /// Get attributes by style.
135         /// </summary>
136         /// <param name="style">Style name.</param>
137         /// <since_tizen> 6 </since_tizen>
138         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
139         [EditorBrowsable(EditorBrowsableState.Never)]
140         public Attributes GetAttributes(string style)
141         {
142             if(style == null)
143             {
144                 return null;
145             }
146             object obj = null;
147
148             if(themeStyleSet.ContainsKey(style) && themeStyleSet[style].ContainsKey(Theme))
149             {
150                 obj = Activator.CreateInstance(themeStyleSet[style][Theme]);
151             }
152             else if(defaultStyleSet.ContainsKey(style))
153             {
154                 obj = Activator.CreateInstance(defaultStyleSet[style]);
155             }
156
157             return (obj as StyleBase)?.GetAttributes();
158         }
159
160         /// <summary>
161         /// ThemeChangeEventArgs is a class to record theme change event arguments which will sent to user.
162         /// </summary>
163         /// <since_tizen> 6 </since_tizen>
164         /// This will be public opened in tizen_5.5 after ACR done. Before ACR, need to be hidden as inhouse API.
165         [EditorBrowsable(EditorBrowsableState.Never)]
166         public class ThemeChangeEventArgs : EventArgs
167         {
168             public string CurrentTheme;
169         }
170     }
171 }