[NUI] Update theme system
[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.ComponentModel;
19 using System.Collections.Generic;
20 using Tizen.NUI.BaseComponents;
21
22 namespace Tizen.NUI.Components
23 {
24     /// <summary>
25     /// StyleManager is a class to manager all style.
26     /// </summary>
27     [Obsolete("This will be removed soon. Please use ThemeManager instead.")]
28     [EditorBrowsable(EditorBrowsableState.Never)]
29     public sealed class StyleManager
30     {
31         private const string defaultName = "DEFAULT";
32
33         /// <summary>
34         /// StyleManager construct.
35         /// </summary>
36         private StyleManager()
37         {
38             ThemeManager.ThemeChanged += OnThemeChanged;
39         }
40
41         /// <summary>
42         /// An event for the theme changed signal which can be used to subscribe or unsubscribe the event handler provided by the user.<br />
43         /// </summary>
44         [Obsolete("This will be removed soon. Please use ThemeManager instead.")]
45         [EditorBrowsable(EditorBrowsableState.Never)]
46         public event EventHandler<ThemeChangeEventArgs> ThemeChangedEvent;
47
48         /// <summary>
49         /// StyleManager static instance.
50         /// </summary>
51         [Obsolete("This will be removed soon. Please use ThemeManager instead.")]
52         [EditorBrowsable(EditorBrowsableState.Never)]
53         public static StyleManager Instance { get; } = new StyleManager();
54
55         /// <summary>
56         /// Style theme.
57         /// </summary>
58         [Obsolete("This will be removed soon. Please use ThemeManager instead.")]
59         [EditorBrowsable(EditorBrowsableState.Never)]
60         public string Theme
61         {
62             get
63             {
64                 return ThemeManager.CurrentTheme.Id;
65             }
66             set
67             {
68                 if (value == null) return;
69
70                 var key = value.ToUpperInvariant();
71
72                 if (key.Equals(defaultName))
73                 {
74                     ThemeManager.CurrentTheme = null;
75                     return;
76                 }
77
78                 // Please note that it does not check "key == Theme" here,
79                 // because of the font size issue of the Tizen.NUI.StyleManager.
80                 // (There are applications to use NUI.Components.StyleManager.ThemeChangedEvent to fix Tizen.NUI.StyleManager font size issue.)
81                 // Please do not check equality until we fix that issue.
82
83                 if (!ThemeMap.ContainsKey(key) || ThemeMap[key] == null)
84                 {
85                     ThemeMap[key] = new Theme()
86                     {
87                         Id = value
88                     };
89                 }
90
91                 ThemeManager.CurrentTheme = ThemeMap[key];
92             }
93         }
94
95         /// <summary>
96         /// (Theme name, Theme instance)
97         /// </summary>
98         private Dictionary<string, Theme> ThemeMap { get; } = new Dictionary<string, Theme>();
99
100         /// <summary>
101         /// Register style in StyleManager.
102         /// </summary>
103         /// <param name="style">Style name.</param>
104         /// <param name="theme">Theme id.</param>
105         /// <param name="styleType">Style type.</param>
106         /// <param name="bDefault">Flag to decide if it is default style.</param>
107         [Obsolete("This will be removed soon. Please use ThemeManager instead.")]
108         [EditorBrowsable(EditorBrowsableState.Never)]
109         public void RegisterStyle(string style, string theme, Type styleType, bool bDefault = false)
110         {
111             if (style == null)
112             {
113                 throw new InvalidOperationException($"style can't be null");
114             }
115
116             if (Activator.CreateInstance(styleType) is StyleBase styleBase)
117             {
118                 if (string.IsNullOrEmpty(theme) || defaultName.Equals(theme, StringComparison.CurrentCultureIgnoreCase))
119                 {
120                     ThemeManager.BaseTheme.AddStyleWithoutClone(style, styleBase.GetViewStyle());
121                     ThemeManager.UpdateThemeForInitialize();
122                     return;
123                 }
124
125                 var key = theme.ToUpperInvariant();
126
127                 if (!ThemeMap.ContainsKey(key) || ThemeMap[key] == null)
128                 {
129                     ThemeMap[key] = new Theme()
130                     {
131                         Id = theme ?? key
132                     };
133                 }
134
135                 if (ThemeMap[key].HasStyle(style))
136                 {
137                     throw new InvalidOperationException($"{style} already be used");
138                 }
139
140                 ThemeMap[key].AddStyleWithoutClone(style, styleBase.GetViewStyle());
141
142                 if (bDefault)
143                 {
144                     ThemeManager.BaseTheme.AddStyleWithoutClone(style, styleBase.GetViewStyle());
145                     ThemeManager.UpdateThemeForInitialize();
146                 }
147             }
148         }
149
150         /// <summary>
151         /// Get style.
152         /// </summary>
153         /// <param name="style">Style name.</param>
154         /// <returns>The style corresponding to style name .</returns>
155         [Obsolete("This will be removed soon. Please use ThemeManager instead.")]
156         [EditorBrowsable(EditorBrowsableState.Never)]
157         public ViewStyle GetViewStyle(string style)
158         {
159             if (style == null)
160             {
161                 return null;
162             }
163
164             return ThemeManager.GetStyle(style);
165         }
166
167         /// <summary>
168         /// Register a style for a component to theme.
169         /// </summary>
170         /// <param name="targetTheme">The target theme name to register a component style. It theme should be a known one.</param>
171         /// <param name="component">The type of ComponentStyle</param>
172         /// <param name="style">The derived class of StyleBase</param>
173         [Obsolete("This will be removed soon. Please use ThemeManager instead.")]
174         [EditorBrowsable(EditorBrowsableState.Never)]
175         public void RegisterComponentStyle(string targetTheme, Type component, Type style)
176         {
177             if (targetTheme == null || component == null || style == null)
178             {
179                 throw new ArgumentException("The argument targetTheme must be specified");
180             }
181
182             if (Activator.CreateInstance(style) is StyleBase styleBase)
183             {
184                 if (defaultName.Equals(targetTheme, StringComparison.CurrentCultureIgnoreCase))
185                 {
186                     ThemeManager.BaseTheme.AddStyleWithoutClone(component.FullName, styleBase.GetViewStyle());
187                     ThemeManager.UpdateThemeForInitialize();
188                     return;
189                 }
190
191                 var key = targetTheme.ToUpperInvariant();
192
193                 if (!ThemeMap.ContainsKey(key) || ThemeMap[key] == null)
194                 {
195                     Tizen.Log.Error("NUI", "The theme name should be a known one.");
196                     return;
197                 }
198
199                 if (Activator.CreateInstance(style) as StyleBase != null)
200                 {
201                     ThemeMap[key].AddStyleWithoutClone(component.FullName, styleBase.GetViewStyle());
202                 }
203             }
204         }
205
206         /// <summary>
207         /// Get components style in the current theme.
208         /// </summary>
209         /// <param name="component">The type of component</param>
210         /// <returns>The style of the component.</returns>
211         [Obsolete("This will be removed soon. Please use ThemeManager instead.")]
212         [EditorBrowsable(EditorBrowsableState.Never)]
213         public ViewStyle GetComponentStyle(Type component)
214         {
215             return ThemeManager.GetStyle(component.FullName);
216         }
217
218         private void OnThemeChanged(object target, ThemeChangedEventArgs args)
219         {
220             ThemeChangedEvent?.Invoke(null, new ThemeChangeEventArgs { CurrentTheme = args.ThemeId });
221         }
222
223         /// <summary>
224         /// ThemeChangeEventArgs is a class to record theme change event arguments which will sent to user.
225         /// </summary>
226         [Obsolete("This will be removed soon. Please use ThemeManager instead.")]
227         [EditorBrowsable(EditorBrowsableState.Never)]
228         public class ThemeChangeEventArgs : EventArgs
229         {
230             /// <summary>
231             /// CurrentTheme
232             /// </summary>
233             [EditorBrowsable(EditorBrowsableState.Never)]
234             public string CurrentTheme { get; set; }
235         }
236     }
237 }