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 [EditorBrowsable(EditorBrowsableState.Never)]
28 public sealed class StyleManager
31 /// StyleManager construct.
33 private StyleManager()
35 ThemeManager.ThemeChanged += OnThemeChanged;
39 /// An event for the theme changed signal which can be used to subscribe or unsubscribe the event handler provided by the user.<br />
41 [EditorBrowsable(EditorBrowsableState.Never)]
42 public event EventHandler<ThemeChangeEventArgs> ThemeChangedEvent;
45 /// StyleManager static instance.
47 [EditorBrowsable(EditorBrowsableState.Never)]
48 public static StyleManager Instance { get; } = new StyleManager();
53 [EditorBrowsable(EditorBrowsableState.Never)]
58 return ThemeManager.CurrentTheme?.Id;
62 if (value == null) return;
64 var key = value.ToUpperInvariant();
66 // Please note that it does not check "key == Theme" here,
67 // because of the font size issue of the Tizen.NUI.StyleManager.
68 // (There are applications to use NUI.Components.StyleManager.ThemeChangedEvent to fix Tizen.NUI.StyleManager font size issue.)
69 // Please do not check equality until we fix that issue.
71 if (!ThemeMap.ContainsKey(key) || ThemeMap[key] == null)
73 ThemeMap[key] = new Theme()
79 ThemeManager.CurrentTheme = ThemeMap[key];
84 /// (Theme name, Theme instance)
86 private Dictionary<string, Theme> ThemeMap { get; } = new Dictionary<string, Theme> { ["DEFAULT"] = ThemeManager.DefaultTheme };
89 /// Register style in StyleManager.
91 /// <param name="style">Style name.</param>
92 /// <param name="theme">Theme id.</param>
93 /// <param name="styleType">Style type.</param>
94 /// <param name="bDefault">Flag to decide if it is default style.</param>
95 [EditorBrowsable(EditorBrowsableState.Never)]
96 public void RegisterStyle(string style, string theme, Type styleType, bool bDefault = false)
100 throw new InvalidOperationException($"style can't be null");
103 if (Activator.CreateInstance(styleType) is StyleBase styleBase)
107 if (bDefault && theme != null)
109 ThemeMap[key].AddStyleWithoutClone(style, styleBase.GetViewStyle());
114 key = theme.ToUpperInvariant();
117 if (!ThemeMap.ContainsKey(key) || ThemeMap[key] == null)
119 ThemeMap[key] = new Theme()
125 if (ThemeMap[key].HasStyle(style))
127 throw new InvalidOperationException($"{style} already be used");
130 ThemeMap[key].AddStyleWithoutClone(style, styleBase.GetViewStyle());
137 /// <param name="style">Style name.</param>
138 /// <returns>The style corresponding to style name .</returns>
139 [EditorBrowsable(EditorBrowsableState.Never)]
140 public ViewStyle GetViewStyle(string style)
147 return ThemeManager.GetStyle(style);
151 /// Register a style for a component to theme.
153 /// <param name="targetTheme">The target theme name to register a component style. It theme should be a known one.</param>
154 /// <param name="component">The type of ComponentStyle</param>
155 /// <param name="style">The derived class of StyleBase</param>
156 [EditorBrowsable(EditorBrowsableState.Never)]
157 public void RegisterComponentStyle(string targetTheme, Type component, Type style)
159 if (targetTheme == null || component == null || style == null)
161 throw new ArgumentException("The argument targetTheme must be specified");
164 var key = targetTheme.ToUpperInvariant();
166 if (!ThemeMap.ContainsKey(key) || ThemeMap[key] == null)
168 Tizen.Log.Error("NUI", "The theme name should be a known one.");
172 if (Activator.CreateInstance(style) as StyleBase != null)
174 ThemeMap[key].AddStyleWithoutClone(component.FullName, (Activator.CreateInstance(style) as StyleBase).GetViewStyle());
179 /// Get components style in the current theme.
181 /// <param name="component">The type of component</param>
182 /// <returns>The style of the component.</returns>
183 [EditorBrowsable(EditorBrowsableState.Never)]
184 public ViewStyle GetComponentStyle(Type component)
186 return ThemeManager.GetStyle(component.FullName);
189 private void OnThemeChanged(object target, ThemeChangedEventArgs args)
191 ThemeChangedEvent?.Invoke(null, new ThemeChangeEventArgs { CurrentTheme = args.ThemeId });
195 /// ThemeChangeEventArgs is a class to record theme change event arguments which will sent to user.
197 [EditorBrowsable(EditorBrowsableState.Never)]
198 public class ThemeChangeEventArgs : EventArgs
203 [EditorBrowsable(EditorBrowsableState.Never)]
204 public string CurrentTheme;