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 [Obsolete("This will be removed soon. Please use ThemeManager instead.")]
28 [EditorBrowsable(EditorBrowsableState.Never)]
29 public sealed class StyleManager
31 private const string defaultName = "DEFAULT";
34 /// StyleManager construct.
36 private StyleManager()
38 ThemeManager.ThemeChanged += OnThemeChanged;
42 /// An event for the theme changed signal which can be used to subscribe or unsubscribe the event handler provided by the user.<br />
44 [Obsolete("This will be removed soon. Please use ThemeManager instead.")]
45 [EditorBrowsable(EditorBrowsableState.Never)]
46 public event EventHandler<ThemeChangeEventArgs> ThemeChangedEvent;
49 /// StyleManager static instance.
51 [Obsolete("This will be removed soon. Please use ThemeManager instead.")]
52 [EditorBrowsable(EditorBrowsableState.Never)]
53 public static StyleManager Instance { get; } = new StyleManager();
58 [Obsolete("This will be removed soon. Please use ThemeManager instead.")]
59 [EditorBrowsable(EditorBrowsableState.Never)]
64 return ThemeManager.CurrentTheme.Id;
68 if (value == null) return;
70 var key = value.ToUpperInvariant();
72 if (key.Equals(defaultName))
74 ThemeManager.CurrentTheme = null;
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.
83 if (!ThemeMap.ContainsKey(key) || ThemeMap[key] == null)
85 ThemeMap[key] = new Theme()
91 ThemeManager.CurrentTheme = ThemeMap[key];
96 /// (Theme name, Theme instance)
98 private Dictionary<string, Theme> ThemeMap { get; } = new Dictionary<string, Theme>();
101 /// Register style in StyleManager.
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)
113 throw new InvalidOperationException($"style can't be null");
116 if (Activator.CreateInstance(styleType) is StyleBase styleBase)
118 if (string.IsNullOrEmpty(theme) || defaultName.Equals(theme, StringComparison.CurrentCultureIgnoreCase))
120 ThemeManager.BaseTheme.AddStyleWithoutClone(style, styleBase.GetViewStyle());
121 ThemeManager.UpdateThemeForInitialize();
125 var key = theme.ToUpperInvariant();
127 if (!ThemeMap.ContainsKey(key) || ThemeMap[key] == null)
129 ThemeMap[key] = new Theme()
135 if (ThemeMap[key].HasStyle(style))
137 throw new InvalidOperationException($"{style} already be used");
140 ThemeMap[key].AddStyleWithoutClone(style, styleBase.GetViewStyle());
144 ThemeManager.BaseTheme.AddStyleWithoutClone(style, styleBase.GetViewStyle());
145 ThemeManager.UpdateThemeForInitialize();
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)
164 return ThemeManager.GetStyle(style);
168 /// Register a style for a component to theme.
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)
177 if (targetTheme == null || component == null || style == null)
179 throw new ArgumentException("The argument targetTheme must be specified");
182 if (Activator.CreateInstance(style) is StyleBase styleBase)
184 if (defaultName.Equals(targetTheme, StringComparison.CurrentCultureIgnoreCase))
186 ThemeManager.BaseTheme.AddStyleWithoutClone(component.FullName, styleBase.GetViewStyle());
187 ThemeManager.UpdateThemeForInitialize();
191 var key = targetTheme.ToUpperInvariant();
193 if (!ThemeMap.ContainsKey(key) || ThemeMap[key] == null)
195 Tizen.Log.Error("NUI", "The theme name should be a known one.");
199 if (Activator.CreateInstance(style) as StyleBase != null)
201 ThemeMap[key].AddStyleWithoutClone(component.FullName, styleBase.GetViewStyle());
207 /// Get components style in the current theme.
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)
215 return ThemeManager.GetStyle(component.FullName);
218 private void OnThemeChanged(object target, ThemeChangedEventArgs args)
220 ThemeChangedEvent?.Invoke(null, new ThemeChangeEventArgs { CurrentTheme = args.ThemeId });
224 /// ThemeChangeEventArgs is a class to record theme change event arguments which will sent to user.
226 [Obsolete("This will be removed soon. Please use ThemeManager instead.")]
227 [EditorBrowsable(EditorBrowsableState.Never)]
228 public class ThemeChangeEventArgs : EventArgs
233 [EditorBrowsable(EditorBrowsableState.Never)]
234 public string CurrentTheme { get; set; }