X-Git-Url: http://review.tizen.org/git/?p=platform%2Fcore%2Fuifw%2Fdali-toolkit.git;a=blobdiff_plain;f=dali-toolkit%2Finternal%2Fstyling%2Fstyle-manager-impl.cpp;h=0968081cb16e1ece5a6a56b7b9272a41922469e7;hp=16fde994d129edbe3d9cfceb48115b2261d9a7e2;hb=7fe4f51894c4c9f158e64b3d11d5d9c8ebdbf990;hpb=2071317d301e9d5af326a3a2efcd2996263fbead diff --git a/dali-toolkit/internal/styling/style-manager-impl.cpp b/dali-toolkit/internal/styling/style-manager-impl.cpp index 16fde99..0968081 100644 --- a/dali-toolkit/internal/styling/style-manager-impl.cpp +++ b/dali-toolkit/internal/styling/style-manager-impl.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014 Samsung Electronics Co., Ltd. + * Copyright (c) 2021 Samsung Electronics Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,56 +18,58 @@ #include "style-manager-impl.h" // EXTERNAL INCLUDES -#include -#include -#include -#include -#include -#include +#include #include +#include +#include +#include // INTERNAL INCLUDES -#include +#include +#include +#include #include +#include #include namespace { - -const char* LANDSCAPE_QUALIFIER = "landscape"; +//const char* LANDSCAPE_QUALIFIER = "landscape"; const char* PORTRAIT_QUALIFIER = "portrait"; -const char* FONT_SIZE_QUALIFIER = "font-size-"; +const char* FONT_SIZE_QUALIFIER = "fontsize"; + +const char* DEFAULT_THEME_FILE_NAME = "dali-toolkit-default-theme.json"; -const char* DEFAULT_THEME = DALI_STYLE_DIR "tizen-default-theme.json"; +const char* PACKAGE_PATH_KEY = "PACKAGE_PATH"; +const char* APPLICATION_RESOURCE_PATH_KEY = "APPLICATION_RESOURCE_PATH"; -const char* PACKAGE_PATH_KEY = "PACKAGE_PATH"; -const char* DEFAULT_PACKAGE_PATH = DALI_DATA_READ_ONLY_DIR "/toolkit/"; +const char* DEFAULT_TOOLKIT_PACKAGE_PATH = "/toolkit/"; + +#if defined(DEBUG_ENABLED) +Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_STYLE"); +#endif } // namespace namespace Dali { - namespace Toolkit { - namespace Internal { - namespace { - BaseHandle Create() { BaseHandle handle = StyleManager::Get(); - if ( !handle ) + if(!handle) { - SingletonService singletonService( SingletonService::Get() ); - if ( singletonService ) + SingletonService singletonService(SingletonService::Get()); + if(singletonService) { - Toolkit::StyleManager manager = Toolkit::StyleManager( new Internal::StyleManager() ); - singletonService.Register( typeid( manager ), manager ); + Toolkit::StyleManager manager = Toolkit::StyleManager(new Internal::StyleManager()); + singletonService.Register(typeid(manager), manager); handle = manager; } } @@ -75,7 +77,7 @@ BaseHandle Create() return handle; } -DALI_TYPE_REGISTRATION_BEGIN_CREATE( Toolkit::StyleManager, Dali::BaseHandle, Create, true ) +DALI_TYPE_REGISTRATION_BEGIN_CREATE(Toolkit::StyleManager, Dali::BaseHandle, Create, true) DALI_TYPE_REGISTRATION_END() } // namespace @@ -84,15 +86,15 @@ Toolkit::StyleManager StyleManager::Get() { Toolkit::StyleManager manager; - SingletonService singletonService( SingletonService::Get() ); - if ( singletonService ) + SingletonService singletonService(SingletonService::Get()); + if(singletonService) { // Check whether the style manager is already created - Dali::BaseHandle handle = singletonService.GetSingleton( typeid( Toolkit::StyleManager ) ); - if( handle ) + Dali::BaseHandle handle = singletonService.GetSingleton(typeid(Toolkit::StyleManager)); + if(handle) { // If so, downcast the handle of singleton - manager = Toolkit::StyleManager( dynamic_cast< StyleManager* >( handle.GetObjectPtr() ) ); + manager = Toolkit::StyleManager(dynamic_cast(handle.GetObjectPtr())); } } @@ -100,72 +102,60 @@ Toolkit::StyleManager StyleManager::Get() } StyleManager::StyleManager() -: mOrientationDegrees( 0 ), // Portrait - mDefaultFontSize( -1 ) +: mDefaultFontSize(-1), + mDefaultFontFamily(""), + mDefaultThemeFilePath(), + mFeedbackStyle(nullptr) { // Add theme builder constants - mThemeBuilderConstants[ PACKAGE_PATH_KEY ] = DEFAULT_PACKAGE_PATH; + const std::string dataReadOnlyDir = AssetManager::GetDaliDataReadOnlyPath(); + mThemeBuilderConstants[PACKAGE_PATH_KEY] = dataReadOnlyDir + DEFAULT_TOOLKIT_PACKAGE_PATH; + mThemeBuilderConstants[APPLICATION_RESOURCE_PATH_KEY] = Application::GetResourcePath(); - RequestDefaultTheme(); - - StyleMonitor styleMonitor( StyleMonitor::Get() ); - if( styleMonitor ) + mStyleMonitor = StyleMonitor::Get(); + if(mStyleMonitor) { - styleMonitor.StyleChangeSignal().Connect( this, &StyleManager::StyleMonitorChange ); - - mDefaultFontSize = styleMonitor.GetDefaultFontSize(); + mStyleMonitor.StyleChangeSignal().Connect(this, &StyleManager::StyleMonitorChange); + mDefaultFontSize = mStyleMonitor.GetDefaultFontSize(); } -} -StyleManager::~StyleManager() -{ + // Set the full path for the default style theme. + const std::string styleDirPath = AssetManager::GetDaliStylePath(); + mDefaultThemeFilePath = styleDirPath + DEFAULT_THEME_FILE_NAME; + + // Sound & haptic style + mFeedbackStyle = new FeedbackStyle(); } -void StyleManager::SetOrientationValue( int orientation ) +StyleManager::~StyleManager() { - if( orientation != mOrientationDegrees ) - { - mOrientationDegrees = orientation; - // TODO: if orientation changed, apply the new style to all controls - // dont want to really do the whole load from file again if the bundle contains both portrait & landscape - SetTheme(); - } + delete mFeedbackStyle; } -int StyleManager::GetOrientationValue() +void StyleManager::ApplyTheme(const std::string& themeFile) { - return mOrientationDegrees; + SetTheme(themeFile); } -void StyleManager::SetOrientation( Orientation orientation ) +void StyleManager::ApplyDefaultTheme() { - if( mOrientation ) - { - mOrientation.ChangedSignal().Disconnect( this, &StyleManager::OnOrientationChanged ); - } - - OnOrientationChanged( orientation ); - - if( mOrientation ) - { - mOrientation.ChangedSignal().Connect( this, &StyleManager::OnOrientationChanged ); - } + SetTheme(mDefaultThemeFilePath); } -Orientation StyleManager::GetOrientation() +const std::string& StyleManager::GetDefaultFontFamily() const { - return mOrientation; + return mDefaultFontFamily; } -void StyleManager::SetStyleConstant( const std::string& key, const Property::Value& value ) +void StyleManager::SetStyleConstant(const std::string& key, const Property::Value& value) { - mStyleBuilderConstants[ key ] = value; + mStyleBuilderConstants[key] = value; } -bool StyleManager::GetStyleConstant( const std::string& key, Property::Value& valueOut ) +bool StyleManager::GetStyleConstant(const std::string& key, Property::Value& valueOut) { - Property::Value* value = mStyleBuilderConstants.Find( key ); - if( value ) + Property::Value* value = mStyleBuilderConstants.Find(key); + if(value) { valueOut = *value; return true; @@ -174,214 +164,307 @@ bool StyleManager::GetStyleConstant( const std::string& key, Property::Value& va return false; } -void StyleManager::OnOrientationChanged( Orientation orientation ) +void StyleManager::ApplyThemeStyle(Toolkit::Control control) { - mOrientation = orientation; - // TODO: if orientation changed, apply the new style to all controls - // dont want to really do the whole load from file again if the bundle contains both portrait & landscape - SetTheme(); + if(!mThemeBuilder) + { + ApplyDefaultTheme(); + } + + if(mThemeBuilder) + { + ApplyStyle(mThemeBuilder, control); + } } -Toolkit::Builder StyleManager::CreateBuilder( const Property::Map& constants ) +void StyleManager::ApplyThemeStyleAtInit(Toolkit::Control control) { - Toolkit::Builder builder = Toolkit::Builder::New(); - builder.AddConstants( constants ); + ApplyThemeStyle(control); - return builder; + if(mFeedbackStyle) + { + mFeedbackStyle->ObjectCreated(control); + } } -bool StyleManager::LoadJSON( Toolkit::Builder builder, const std::string& jsonFilePath ) +void StyleManager::ApplyStyle(Toolkit::Control control, const std::string& jsonFileName, const std::string& styleName) { - std::string fileString; - if( LoadFile( jsonFilePath, fileString ) ) + bool builderReady = false; + + // First look in the cache + Toolkit::Builder builder = FindCachedBuilder(jsonFileName); + if(builder) { - builder.LoadFromString( fileString ); - return true; + builderReady = true; } else { - DALI_LOG_WARNING("Error loading file '%s'\n", jsonFilePath.c_str()); - return false; + // Merge theme and style constants + Property::Map constants(mThemeBuilderConstants); + constants.Merge(mStyleBuilderConstants); + + // Create it + builder = CreateBuilder(constants); + + if(LoadJSON(builder, jsonFileName)) + { + CacheBuilder(builder, jsonFileName); + builderReady = true; + } + } + + // Apply the style to the control + if(builderReady) + { + builder.ApplyStyle(styleName, control); } } -void StyleManager::CollectQualifiers( StringList& qualifiersOut ) +Toolkit::StyleManager::StyleChangedSignalType& StyleManager::StyleChangedSignal() { - // Append the relevant qualifier for orientation - int orientation = mOrientationDegrees; + return mStyleChangedSignal; +} + +Toolkit::StyleManager::StyleChangedSignalType& StyleManager::ControlStyleChangeSignal() +{ + return mControlStyleChangeSignal; +} + +void StyleManager::SetTheme(const std::string& themeFile) +{ + bool themeLoaded = false; + bool loading = false; - if( mOrientation ) + // If we haven't loaded a theme, or the stored theme file is empty, or + // the previously loaded theme is different to the requested theme, + // first reset the builder and load the default theme. + if(!mThemeBuilder || mThemeFile.empty() || mThemeFile.compare(themeFile) != 0) { - orientation = mOrientation.GetDegrees(); + loading = true; + mThemeBuilder = CreateBuilder(mThemeBuilderConstants); + themeLoaded = LoadJSON(mThemeBuilder, mDefaultThemeFilePath); // Sets themeLoaded to true if theme exists } - switch( orientation ) + if(themeFile.compare(mDefaultThemeFilePath) != 0) { - case 90: - case 270: + // The theme is different to the default: Merge it + loading = true; + themeLoaded |= LoadJSON(mThemeBuilder, themeFile); + } + + if(loading) + { + mThemeFile = themeFile; + + if(themeLoaded) { - qualifiersOut.push_back( std::string( LANDSCAPE_QUALIFIER ) ); - break; + // We've successfully loaded the theme file + if(mFeedbackStyle) + { + mFeedbackStyle->StyleChanged(mThemeFile, StyleChange::THEME_CHANGE); + } + + EmitStyleChangeSignals(StyleChange::THEME_CHANGE); } - case 180: - case 0: // fall through - default: + else { - qualifiersOut.push_back( std::string( PORTRAIT_QUALIFIER ) ); - break; + // We tried to load a theme, but it failed. Ensure the builder is reset + mThemeBuilder.Reset(); + mThemeFile.clear(); } } } -void StyleManager::BuildQualifiedStyleName( const std::string& styleName, const StringList& qualifiers, std::string& qualifiedStyleOut ) +const Property::Map StyleManager::GetConfigurations() { - qualifiedStyleOut.append( styleName ); + DALI_LOG_STREAM(gLogFilter, Debug::Concise, "GetConfigurations()\n On entry, mThemeBuilder: " << (bool(mThemeBuilder) ? "Created" : "Empty") << " mThemeFile: " << mThemeFile); - for( StringList::const_iterator it = qualifiers.begin(), itEnd = qualifiers.end(); it != itEnd; ++it ) + Property::Map result; + if(mThemeBuilder) { - const std::string& str = *it; - - qualifiedStyleOut.append( "-" ); - qualifiedStyleOut.append( str ); + result = mThemeBuilder.GetConfigurations(); } -} - -void StyleManager::ApplyStyle( Toolkit::Builder builder, Toolkit::Control control ) -{ - std::string styleName = control.GetStyleName(); - if( styleName.empty() ) + else { - // Convert control name to lower case - styleName = control.GetTypeName(); - std::transform( styleName.begin(), styleName.end(), styleName.begin(), ::tolower ); - } + DALI_LOG_STREAM(gLogFilter, Debug::Concise, "GetConfigurations() Loading default theme"); - // Apply the style after choosing the correct actual style (e.g. landscape or portrait) - StringList qualifiers; - CollectQualifiers( qualifiers ); + bool themeLoaded = false; - while( true ) - { - std::string qualifiedStyleName; - BuildQualifiedStyleName( styleName, qualifiers, qualifiedStyleName ); + mThemeBuilder = CreateBuilder(mThemeBuilderConstants); - // Break if style found or we have tried the root style name (qualifiers is empty) - if( builder.ApplyStyle( qualifiedStyleName, control ) || qualifiers.size() == 0 ) + // Load default theme because this is first try to load stylesheet. + themeLoaded = LoadJSON(mThemeBuilder, mDefaultThemeFilePath); + mThemeFile = mDefaultThemeFilePath; + + if(themeLoaded) { - break; + result = mThemeBuilder.GetConfigurations(); } - - // Remove the last qualifier in an attempt to find a style that is valid - qualifiers.pop_back(); + DALI_LOG_STREAM(gLogFilter, Debug::Concise, " themeLoaded" << (themeLoaded ? "success" : "failure")); } - if( mDefaultFontSize >= 0 ) - { - // Apply the style for logical font size - std::stringstream fontSizeQualifier; - fontSizeQualifier << styleName << "-" << FONT_SIZE_QUALIFIER << mDefaultFontSize; - builder.ApplyStyle( fontSizeQualifier.str(), control ); - } + DALI_LOG_STREAM(gLogFilter, Debug::Concise, "GetConfigurations()\n On exit, result Count: " << (result.Count() != 0)); + DALI_LOG_STREAM(gLogFilter, Debug::Verbose, " result: " << result); + + return result; } -void StyleManager::ApplyThemeStyle( Toolkit::Control control ) +bool StyleManager::LoadFile(const std::string& filename, std::string& stringOut) { - if( mThemeBuilder ) + DALI_ASSERT_DEBUG(0 != filename.length()); + + // as toolkit is platform agnostic, it cannot load files from filesystem + // ask style monitor to load the style sheet + if(mStyleMonitor) { - ApplyStyle( mThemeBuilder, control ); + return mStyleMonitor.LoadThemeFile(filename, stringOut); } + + return false; } -void StyleManager::ApplyStyle( Toolkit::Control control, const std::string& jsonFileName, const std::string& styleName ) +Toolkit::Builder StyleManager::CreateBuilder(const Property::Map& constants) { - bool builderReady = false; + Toolkit::Builder builder = Toolkit::Builder::New(); + builder.AddConstants(constants); - // First look in the cache - Toolkit::Builder builder = FindCachedBuilder( jsonFileName ); - if( builder ) + return builder; +} + +bool StyleManager::LoadJSON(Toolkit::Builder builder, const std::string& jsonFilePath) +{ + std::string fileString; + if(LoadFile(jsonFilePath, fileString)) { - builderReady = true; + builder.LoadFromString(fileString); + return true; } else { - // Merge theme and style constants - Property::Map constants( mThemeBuilderConstants ); - constants.Merge( mStyleBuilderConstants ); - - // Create it - builder = CreateBuilder( constants ); - - if( LoadJSON( builder, jsonFileName ) ) - { - CacheBuilder( builder, jsonFileName ); - builderReady = true; - } + DALI_LOG_WARNING("Error loading file '%s'\n", jsonFilePath.c_str()); + return false; } +} - // Apply the style to the control - if( builderReady ) - { - builder.ApplyStyle( styleName, control ); - } +static void CollectQualifiers(std::vector& qualifiersOut) +{ + // Append the relevant qualifier for orientation + // int orientation = 0; // Get the orientation from the system + /* + //// To Do ///// + Getting orientation from the system, and determine Qualifie LANDSCAPE or PORTRAIT + orientation 0, 180 : PORTRAIT_QUALIFIER (default) + orientation 90, 270 : LANDSCAPE_QUALIFIER + */ + + qualifiersOut.push_back(std::string(PORTRAIT_QUALIFIER)); } -bool StyleManager::LoadFile( const std::string& filename, std::string& stringOut ) +/** + * @brief Construct a qualified style name out of qualifiers + * + * A qualifed style name will be in the format: style-qualifier0-qualifier1-qualifierN + * + * @param[in] styleName The root name of the style + * @param[in] qualifiers List of qualifier names + * @param[out] qualifiedStyleOut The qualified style name + */ +static void BuildQualifiedStyleName( + const std::string& styleName, + const std::vector& qualifiers, + std::string& qualifiedStyleOut) { - DALI_ASSERT_DEBUG( 0 != filename.length()); + qualifiedStyleOut.append(styleName); - std::ifstream in( filename.c_str(), std::ios::in ); - if( in ) + for(std::vector::const_iterator it = qualifiers.begin(), + itEnd = qualifiers.end(); + it != itEnd; + ++it) { - std::stringstream buffer; - buffer << in.rdbuf(); + const std::string& str = *it; - stringOut = buffer.str(); + qualifiedStyleOut.append("-"); + qualifiedStyleOut.append(str); + } +} - in.close(); +static bool GetStyleNameForControl(Toolkit::Builder builder, Toolkit::Control control, std::string& styleName) +{ + styleName = control.GetStyleName(); - return true; + if(styleName.empty()) + { + styleName = control.GetTypeName(); } - return false; -} + // Apply the style after choosing the correct actual style (e.g. landscape or portrait) + std::vector qualifiers; + CollectQualifiers(qualifiers); -Toolkit::StyleManager::StyleChangeSignalType& StyleManager::StyleChangeSignal() -{ - return mStyleChangeSignal; -} + bool found = 0; + std::string qualifiedStyleName; + do + { + qualifiedStyleName.clear(); + BuildQualifiedStyleName(styleName, qualifiers, qualifiedStyleName); -void StyleManager::RequestThemeChange( const std::string& themeFile ) -{ - mThemeFile = themeFile; + // Break if style found or we have tried the root style name (qualifiers is empty) + if(GetImpl(builder).LookupStyleName(qualifiedStyleName)) + { + found = true; + break; + } + if(qualifiers.size() == 0) + { + break; + } + // Remove the last qualifier in an attempt to find a style that is valid + qualifiers.pop_back(); + } while(!found); - // need to do style change synchronously as app might create a UI control on the next line - SetTheme(); + if(found) + { + styleName = qualifiedStyleName; + } + return found; } -void StyleManager::RequestDefaultTheme() +void StyleManager::ApplyStyle(Toolkit::Builder builder, Toolkit::Control control) { - RequestThemeChange( DEFAULT_THEME ); -} + std::string styleName = control.GetStyleName(); + if(GetStyleNameForControl(builder, control, styleName)) + { + builder.ApplyStyle(styleName, control); + } -void StyleManager::SetTheme() -{ - mThemeBuilder = CreateBuilder( mThemeBuilderConstants ); - if ( LoadJSON( mThemeBuilder, mThemeFile ) ) + if(mDefaultFontSize >= 0) { - StyleChange change; - change.themeChange = true; - mStyleChangeSignal.Emit( Toolkit::StyleManager::Get(), change ); + // Apply the style for logical font size + std::stringstream fontSizeQualifier; + fontSizeQualifier << styleName << FONT_SIZE_QUALIFIER << mDefaultFontSize; + builder.ApplyStyle(fontSizeQualifier.str(), control); } - else +} + +const StylePtr StyleManager::GetRecordedStyle(Toolkit::Control control) +{ + if(mThemeBuilder) { - mThemeBuilder.Reset(); + std::string styleName = control.GetStyleName(); + + if(GetStyleNameForControl(mThemeBuilder, control, styleName)) + { + const StylePtr style = GetImpl(mThemeBuilder).GetStyle(styleName); + return style; + } } + return StylePtr(NULL); } -Toolkit::Builder StyleManager::FindCachedBuilder( const std::string& key ) +Toolkit::Builder StyleManager::FindCachedBuilder(const std::string& key) { - BuilderMap::iterator builderIt = mBuilderCache.find( key ); - if( builderIt != mBuilderCache.end() ) + BuilderMap::iterator builderIt = mBuilderCache.find(key); + if(builderIt != mBuilderCache.end()) { return builderIt->second; } @@ -389,19 +472,45 @@ Toolkit::Builder StyleManager::FindCachedBuilder( const std::string& key ) return Toolkit::Builder(); } -void StyleManager::CacheBuilder( Toolkit::Builder builder, const std::string& key ) +void StyleManager::CacheBuilder(Toolkit::Builder builder, const std::string& key) { - mBuilderCache[ key ] = builder; + mBuilderCache[key] = builder; } -void StyleManager::StyleMonitorChange( StyleMonitor styleMonitor, StyleChange styleChange ) +void StyleManager::StyleMonitorChange(StyleMonitor styleMonitor, StyleChange::Type styleChange) { - if( styleChange.defaultFontSizeChange ) + switch(styleChange) { - mDefaultFontSize = styleMonitor.GetDefaultFontSize(); + case StyleChange::DEFAULT_FONT_CHANGE: + { + mDefaultFontFamily = styleMonitor.GetDefaultFontFamily(); + break; + } + + case StyleChange::DEFAULT_FONT_SIZE_CHANGE: + { + mDefaultFontSize = styleMonitor.GetDefaultFontSize(); + break; + } + + case StyleChange::THEME_CHANGE: + { + SetTheme(styleMonitor.GetTheme()); + break; + } } + EmitStyleChangeSignals(styleChange); +} + +void StyleManager::EmitStyleChangeSignals(StyleChange::Type styleChange) +{ + Toolkit::StyleManager styleManager = StyleManager::Get(); + + // Update Controls first + mControlStyleChangeSignal.Emit(styleManager, styleChange); - mStyleChangeSignal.Emit( Toolkit::StyleManager::Get(), styleChange ); + // Inform application last + mStyleChangedSignal.Emit(styleManager, styleChange); } } // namespace Internal