2 * Copyright (c) 2021 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 #include "style-manager-impl.h"
21 #include <dali/devel-api/common/singleton-service.h>
22 #include <dali/integration-api/debug.h>
23 #include <dali/public-api/adaptor-framework/application.h>
24 #include <dali/public-api/object/type-registry-helper.h>
25 #include <dali/public-api/object/type-registry.h>
28 #include <dali-toolkit/devel-api/asset-manager/asset-manager.h>
29 #include <dali-toolkit/internal/builder/builder-impl.h>
30 #include <dali-toolkit/internal/feedback/feedback-style.h>
31 #include <dali-toolkit/public-api/controls/control-impl.h>
32 #include <dali-toolkit/public-api/controls/control.h>
33 #include <dali-toolkit/public-api/styling/style-manager.h>
37 //const char* LANDSCAPE_QUALIFIER = "landscape";
38 const char* PORTRAIT_QUALIFIER = "portrait";
39 const char* FONT_SIZE_QUALIFIER = "fontsize";
41 const char* DEFAULT_THEME_FILE_NAME = "dali-toolkit-default-theme.json";
43 const char* PACKAGE_PATH_KEY = "PACKAGE_PATH";
44 const char* APPLICATION_RESOURCE_PATH_KEY = "APPLICATION_RESOURCE_PATH";
46 const char* DEFAULT_TOOLKIT_PACKAGE_PATH = "/toolkit/";
48 static constexpr int32_t COUNT_BROKEN_IMAGE_MAX = 3;
50 #if defined(DEBUG_ENABLED)
51 Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_STYLE");
66 BaseHandle handle = StyleManager::Get();
70 SingletonService singletonService(SingletonService::Get());
73 Toolkit::StyleManager manager = Toolkit::StyleManager(new Internal::StyleManager());
74 singletonService.Register(typeid(manager), manager);
82 DALI_TYPE_REGISTRATION_BEGIN_CREATE(Toolkit::StyleManager, Dali::BaseHandle, Create, true)
83 DALI_TYPE_REGISTRATION_END()
87 Toolkit::StyleManager StyleManager::Get()
89 Toolkit::StyleManager manager;
91 SingletonService singletonService(SingletonService::Get());
94 // Check whether the style manager is already created
95 Dali::BaseHandle handle = singletonService.GetSingleton(typeid(Toolkit::StyleManager));
98 // If so, downcast the handle of singleton
99 manager = Toolkit::StyleManager(dynamic_cast<StyleManager*>(handle.GetObjectPtr()));
106 StyleManager::StyleManager()
107 : mDefaultFontSize(-1),
108 mDefaultFontFamily(""),
109 mDefaultThemeFilePath(),
110 mFeedbackStyle(nullptr)
112 // Add theme builder constants
113 const std::string dataReadOnlyDir = AssetManager::GetDaliDataReadOnlyPath();
114 mThemeBuilderConstants[PACKAGE_PATH_KEY] = dataReadOnlyDir + DEFAULT_TOOLKIT_PACKAGE_PATH;
115 mThemeBuilderConstants[APPLICATION_RESOURCE_PATH_KEY] = Application::GetResourcePath();
117 mStyleMonitor = StyleMonitor::Get();
120 mStyleMonitor.StyleChangeSignal().Connect(this, &StyleManager::StyleMonitorChange);
121 mDefaultFontSize = mStyleMonitor.GetDefaultFontSize();
124 // Set the full path for the default style theme.
125 const std::string styleDirPath = AssetManager::GetDaliStylePath();
126 mDefaultThemeFilePath = styleDirPath + DEFAULT_THEME_FILE_NAME;
128 // Sound & haptic style
129 mFeedbackStyle = new FeedbackStyle();
131 // Initialize BrokenImages
132 mBrokenImageUrls.assign(COUNT_BROKEN_IMAGE_MAX, "");
135 StyleManager::~StyleManager()
137 delete mFeedbackStyle;
140 void StyleManager::ApplyTheme(const std::string& themeFile)
145 void StyleManager::ApplyDefaultTheme()
147 SetTheme(mDefaultThemeFilePath);
150 const std::string& StyleManager::GetDefaultFontFamily() const
152 return mDefaultFontFamily;
155 void StyleManager::SetStyleConstant(const std::string& key, const Property::Value& value)
157 mStyleBuilderConstants[key] = value;
160 bool StyleManager::GetStyleConstant(const std::string& key, Property::Value& valueOut)
162 Property::Value* value = mStyleBuilderConstants.Find(key);
172 void StyleManager::ApplyThemeStyle(Toolkit::Control control)
181 ApplyStyle(mThemeBuilder, control);
185 void StyleManager::ApplyThemeStyleAtInit(Toolkit::Control control)
187 ApplyThemeStyle(control);
191 mFeedbackStyle->ObjectCreated(control);
195 void StyleManager::ApplyStyle(Toolkit::Control control, const std::string& jsonFileName, const std::string& styleName)
197 bool builderReady = false;
199 // First look in the cache
200 Toolkit::Builder builder = FindCachedBuilder(jsonFileName);
207 // Merge theme and style constants
208 Property::Map constants(mThemeBuilderConstants);
209 constants.Merge(mStyleBuilderConstants);
212 builder = CreateBuilder(constants);
214 if(LoadJSON(builder, jsonFileName))
216 CacheBuilder(builder, jsonFileName);
221 // Apply the style to the control
224 builder.ApplyStyle(styleName, control);
228 Toolkit::StyleManager::StyleChangedSignalType& StyleManager::StyleChangedSignal()
230 return mStyleChangedSignal;
233 Toolkit::StyleManager::StyleChangedSignalType& StyleManager::ControlStyleChangeSignal()
235 return mControlStyleChangeSignal;
238 Toolkit::DevelStyleManager::BrokenImageChangedSignalType& StyleManager::BrokenImageChangedSignal()
240 return mBrokenImageChangedSignal;
243 void StyleManager::SetTheme(const std::string& themeFile)
245 bool themeLoaded = false;
246 bool loading = false;
248 // If we haven't loaded a theme, or the stored theme file is empty, or
249 // the previously loaded theme is different to the requested theme,
250 // first reset the builder and load the default theme.
251 if(!mThemeBuilder || mThemeFile.empty() || mThemeFile.compare(themeFile) != 0)
254 mThemeBuilder = CreateBuilder(mThemeBuilderConstants);
255 themeLoaded = LoadJSON(mThemeBuilder, mDefaultThemeFilePath); // Sets themeLoaded to true if theme exists
258 if(themeFile.compare(mDefaultThemeFilePath) != 0)
260 // The theme is different to the default: Merge it
262 themeLoaded |= LoadJSON(mThemeBuilder, themeFile);
267 mThemeFile = themeFile;
271 // We've successfully loaded the theme file
274 mFeedbackStyle->StyleChanged(mThemeFile, StyleChange::THEME_CHANGE);
277 EmitStyleChangeSignals(StyleChange::THEME_CHANGE);
281 // We tried to load a theme, but it failed. Ensure the builder is reset
282 mThemeBuilder.Reset();
288 const Property::Map StyleManager::GetConfigurations()
290 DALI_LOG_STREAM(gLogFilter, Debug::Concise, "GetConfigurations()\n On entry, mThemeBuilder: " << (bool(mThemeBuilder) ? "Created" : "Empty") << " mThemeFile: " << mThemeFile);
292 Property::Map result;
295 result = mThemeBuilder.GetConfigurations();
299 DALI_LOG_STREAM(gLogFilter, Debug::Concise, "GetConfigurations() Loading default theme");
301 bool themeLoaded = false;
303 mThemeBuilder = CreateBuilder(mThemeBuilderConstants);
305 // Load default theme because this is first try to load stylesheet.
306 themeLoaded = LoadJSON(mThemeBuilder, mDefaultThemeFilePath);
307 mThemeFile = mDefaultThemeFilePath;
311 result = mThemeBuilder.GetConfigurations();
313 DALI_LOG_STREAM(gLogFilter, Debug::Concise, " themeLoaded" << (themeLoaded ? "success" : "failure"));
316 DALI_LOG_STREAM(gLogFilter, Debug::Concise, "GetConfigurations()\n On exit, result Count: " << (result.Count() != 0));
317 DALI_LOG_STREAM(gLogFilter, Debug::Verbose, " result: " << result);
322 void StyleManager::SetBrokenImageUrl(DevelStyleManager::BrokenImageType brokenImageType, const std::string& brokenImageUrl)
324 int brokenType = static_cast<int>(brokenImageType);
325 mBrokenImageUrls[brokenType] = brokenImageUrl;
326 Toolkit::StyleManager styleManager = StyleManager::Get();
327 mBrokenImageChangedSignal.Emit(styleManager);
330 std::string StyleManager::GetBrokenImageUrl(DevelStyleManager::BrokenImageType brokenImageType)
332 int brokenType = static_cast<int>(brokenImageType);
333 return mBrokenImageUrls[brokenType];
336 std::vector<std::string> StyleManager::GetBrokenImageUrlList()
338 // create a list for brokenImage
339 std::vector<std::string> brokenImageUrlList;
340 for(int i = 0; i < COUNT_BROKEN_IMAGE_MAX; i++)
342 if(!mBrokenImageUrls[i].empty())
344 brokenImageUrlList.push_back(mBrokenImageUrls[i]);
347 return brokenImageUrlList;
350 bool StyleManager::LoadFile(const std::string& filename, std::string& stringOut)
352 DALI_ASSERT_DEBUG(0 != filename.length());
354 // as toolkit is platform agnostic, it cannot load files from filesystem
355 // ask style monitor to load the style sheet
358 return mStyleMonitor.LoadThemeFile(filename, stringOut);
364 Toolkit::Builder StyleManager::CreateBuilder(const Property::Map& constants)
366 Toolkit::Builder builder = Toolkit::Builder::New();
367 builder.AddConstants(constants);
372 bool StyleManager::LoadJSON(Toolkit::Builder builder, const std::string& jsonFilePath)
374 std::string fileString;
375 if(LoadFile(jsonFilePath, fileString))
377 builder.LoadFromString(fileString);
382 DALI_LOG_WARNING("Error loading file '%s'\n", jsonFilePath.c_str());
387 static void CollectQualifiers(std::vector<std::string>& qualifiersOut)
389 // Append the relevant qualifier for orientation
390 // int orientation = 0; // Get the orientation from the system
393 Getting orientation from the system, and determine Qualifie LANDSCAPE or PORTRAIT
394 orientation 0, 180 : PORTRAIT_QUALIFIER (default)
395 orientation 90, 270 : LANDSCAPE_QUALIFIER
398 qualifiersOut.push_back(std::string(PORTRAIT_QUALIFIER));
402 * @brief Construct a qualified style name out of qualifiers
404 * A qualifed style name will be in the format: style-qualifier0-qualifier1-qualifierN
406 * @param[in] styleName The root name of the style
407 * @param[in] qualifiers List of qualifier names
408 * @param[out] qualifiedStyleOut The qualified style name
410 static void BuildQualifiedStyleName(
411 const std::string& styleName,
412 const std::vector<std::string>& qualifiers,
413 std::string& qualifiedStyleOut)
415 qualifiedStyleOut.append(styleName);
417 for(std::vector<std::string>::const_iterator it = qualifiers.begin(),
418 itEnd = qualifiers.end();
422 const std::string& str = *it;
424 qualifiedStyleOut.append("-");
425 qualifiedStyleOut.append(str);
429 static bool GetStyleNameForControl(Toolkit::Builder builder, Toolkit::Control control, std::string& styleName)
431 styleName = control.GetStyleName();
433 if(styleName.empty())
435 styleName = control.GetTypeName();
438 // Apply the style after choosing the correct actual style (e.g. landscape or portrait)
439 std::vector<std::string> qualifiers;
440 CollectQualifiers(qualifiers);
443 std::string qualifiedStyleName;
446 qualifiedStyleName.clear();
447 BuildQualifiedStyleName(styleName, qualifiers, qualifiedStyleName);
449 // Break if style found or we have tried the root style name (qualifiers is empty)
450 if(GetImpl(builder).LookupStyleName(qualifiedStyleName))
455 if(qualifiers.size() == 0)
459 // Remove the last qualifier in an attempt to find a style that is valid
460 qualifiers.pop_back();
465 styleName = qualifiedStyleName;
470 void StyleManager::ApplyStyle(Toolkit::Builder builder, Toolkit::Control control)
472 std::string styleName = control.GetStyleName();
473 if(GetStyleNameForControl(builder, control, styleName))
475 builder.ApplyStyle(styleName, control);
478 if(mDefaultFontSize >= 0)
480 // Apply the style for logical font size
481 std::stringstream fontSizeQualifier;
482 fontSizeQualifier << styleName << FONT_SIZE_QUALIFIER << mDefaultFontSize;
483 builder.ApplyStyle(fontSizeQualifier.str(), control);
487 const StylePtr StyleManager::GetRecordedStyle(Toolkit::Control control)
491 std::string styleName = control.GetStyleName();
493 if(GetStyleNameForControl(mThemeBuilder, control, styleName))
495 const StylePtr style = GetImpl(mThemeBuilder).GetStyle(styleName);
499 return StylePtr(NULL);
502 Toolkit::Builder StyleManager::FindCachedBuilder(const std::string& key)
504 BuilderMap::iterator builderIt = mBuilderCache.find(key);
505 if(builderIt != mBuilderCache.end())
507 return builderIt->second;
510 return Toolkit::Builder();
513 void StyleManager::CacheBuilder(Toolkit::Builder builder, const std::string& key)
515 mBuilderCache[key] = builder;
518 void StyleManager::StyleMonitorChange(StyleMonitor styleMonitor, StyleChange::Type styleChange)
522 case StyleChange::DEFAULT_FONT_CHANGE:
524 mDefaultFontFamily = styleMonitor.GetDefaultFontFamily();
528 case StyleChange::DEFAULT_FONT_SIZE_CHANGE:
530 mDefaultFontSize = styleMonitor.GetDefaultFontSize();
534 case StyleChange::THEME_CHANGE:
536 SetTheme(styleMonitor.GetTheme());
540 EmitStyleChangeSignals(styleChange);
543 void StyleManager::EmitStyleChangeSignals(StyleChange::Type styleChange)
545 Toolkit::StyleManager styleManager = StyleManager::Get();
547 // Update Controls first
548 mControlStyleChangeSignal.Emit(styleManager, styleChange);
550 // Inform application last
551 mStyleChangedSignal.Emit(styleManager, styleChange);
554 } // namespace Internal
556 } // namespace Toolkit