2 * Copyright (c) 2018 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/public-api/object/type-registry.h>
23 #include <dali/public-api/object/type-registry-helper.h>
24 #include <dali/integration-api/debug.h>
25 #include <dali/public-api/adaptor-framework/application.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/public-api/controls/control.h>
31 #include <dali-toolkit/public-api/controls/control-impl.h>
32 #include <dali-toolkit/public-api/styling/style-manager.h>
33 #include <dali-toolkit/internal/feedback/feedback-style.h>
38 //const char* LANDSCAPE_QUALIFIER = "landscape";
39 const char* PORTRAIT_QUALIFIER = "portrait";
40 const char* FONT_SIZE_QUALIFIER = "fontsize";
42 const char* DEFAULT_THEME_FILE_NAME = "dali-toolkit-default-theme.json";
44 const char* PACKAGE_PATH_KEY = "PACKAGE_PATH";
45 const char* APPLICATION_RESOURCE_PATH_KEY = "APPLICATION_RESOURCE_PATH";
47 const char* DEFAULT_TOOLKIT_PACKAGE_PATH = "/toolkit/";
49 #if defined(DEBUG_ENABLED)
50 Debug::Filter* gLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_STYLE");
69 BaseHandle handle = StyleManager::Get();
73 SingletonService singletonService( SingletonService::Get() );
74 if ( singletonService )
76 Toolkit::StyleManager manager = Toolkit::StyleManager( new Internal::StyleManager() );
77 singletonService.Register( typeid( manager ), manager );
85 DALI_TYPE_REGISTRATION_BEGIN_CREATE( Toolkit::StyleManager, Dali::BaseHandle, Create, true )
86 DALI_TYPE_REGISTRATION_END()
90 Toolkit::StyleManager StyleManager::Get()
92 Toolkit::StyleManager manager;
94 SingletonService singletonService( SingletonService::Get() );
95 if ( singletonService )
97 // Check whether the style manager is already created
98 Dali::BaseHandle handle = singletonService.GetSingleton( typeid( Toolkit::StyleManager ) );
101 // If so, downcast the handle of singleton
102 manager = Toolkit::StyleManager( dynamic_cast< StyleManager* >( handle.GetObjectPtr() ) );
109 StyleManager::StyleManager()
110 : mDefaultFontSize( -1 ),
111 mDefaultFontFamily(""),
112 mDefaultThemeFilePath(),
113 mFeedbackStyle( nullptr )
115 // Add theme builder constants
116 const std::string dataReadOnlyDir = AssetManager::GetDaliDataReadOnlyPath();
117 mThemeBuilderConstants[ PACKAGE_PATH_KEY ] = dataReadOnlyDir + DEFAULT_TOOLKIT_PACKAGE_PATH;
118 mThemeBuilderConstants[ APPLICATION_RESOURCE_PATH_KEY ] = Application::GetResourcePath();
120 mStyleMonitor = StyleMonitor::Get();
123 mStyleMonitor.StyleChangeSignal().Connect( this, &StyleManager::StyleMonitorChange );
124 mDefaultFontSize = mStyleMonitor.GetDefaultFontSize();
127 // Set the full path for the default style theme.
128 const std::string styleDirPath = AssetManager::GetDaliStylePath();
129 mDefaultThemeFilePath = styleDirPath + DEFAULT_THEME_FILE_NAME;
131 // Sound & haptic style
132 mFeedbackStyle = new FeedbackStyle();
135 StyleManager::~StyleManager()
137 delete mFeedbackStyle;
140 void StyleManager::ApplyTheme( const std::string& themeFile )
142 SetTheme( 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 void StyleManager::SetTheme( const std::string& themeFile )
240 bool themeLoaded = false;
241 bool loading = false;
243 // If we haven't loaded a theme, or the stored theme file is empty, or
244 // the previously loaded theme is different to the requested theme,
245 // first reset the builder and load the default theme.
246 if( ! mThemeBuilder || mThemeFile.empty() || mThemeFile.compare( themeFile ) != 0 )
249 mThemeBuilder = CreateBuilder( mThemeBuilderConstants );
250 themeLoaded = LoadJSON( mThemeBuilder, mDefaultThemeFilePath ); // Sets themeLoaded to true if theme exists
253 if( themeFile.compare(mDefaultThemeFilePath) != 0 )
255 // The theme is different to the default: Merge it
257 themeLoaded |= LoadJSON( mThemeBuilder, themeFile );
262 mThemeFile = themeFile;
266 // We've successfully loaded the theme file
269 mFeedbackStyle->StyleChanged( mThemeFile, StyleChange::THEME_CHANGE );
272 EmitStyleChangeSignals(StyleChange::THEME_CHANGE);
276 // We tried to load a theme, but it failed. Ensure the builder is reset
277 mThemeBuilder.Reset();
283 const Property::Map StyleManager::GetConfigurations()
285 DALI_LOG_STREAM( gLogFilter, Debug::Concise, "GetConfigurations()\n On entry, mThemeBuilder: " << (bool(mThemeBuilder)?"Created":"Empty") << " mThemeFile: " << mThemeFile);
287 Property::Map result;
290 result = mThemeBuilder.GetConfigurations();
294 DALI_LOG_STREAM( gLogFilter, Debug::Concise, "GetConfigurations() Loading default theme" );
296 bool themeLoaded = false;
298 mThemeBuilder = CreateBuilder( mThemeBuilderConstants );
300 // Load default theme because this is first try to load stylesheet.
301 themeLoaded = LoadJSON( mThemeBuilder, mDefaultThemeFilePath );
302 mThemeFile = mDefaultThemeFilePath;
306 result = mThemeBuilder.GetConfigurations();
308 DALI_LOG_STREAM( gLogFilter, Debug::Concise, " themeLoaded" << (themeLoaded?"success":"failure") );
311 DALI_LOG_STREAM( gLogFilter, Debug::Concise, "GetConfigurations()\n On exit, result Count: " << (result.Count() != 0) );
312 DALI_LOG_STREAM( gLogFilter, Debug::Verbose, " result: " << result );
317 bool StyleManager::LoadFile( const std::string& filename, std::string& stringOut )
319 DALI_ASSERT_DEBUG( 0 != filename.length());
321 // as toolkit is platform agnostic, it cannot load files from filesystem
322 // ask style monitor to load the style sheet
325 return mStyleMonitor.LoadThemeFile( filename, stringOut );
331 Toolkit::Builder StyleManager::CreateBuilder( const Property::Map& constants )
333 Toolkit::Builder builder = Toolkit::Builder::New();
334 builder.AddConstants( constants );
339 bool StyleManager::LoadJSON( Toolkit::Builder builder, const std::string& jsonFilePath )
341 std::string fileString;
342 if( LoadFile( jsonFilePath, fileString ) )
344 builder.LoadFromString( fileString );
349 DALI_LOG_WARNING("Error loading file '%s'\n", jsonFilePath.c_str());
354 static void CollectQualifiers( std::vector<std::string>& qualifiersOut )
356 // Append the relevant qualifier for orientation
357 // int orientation = 0; // Get the orientation from the system
360 Getting orientation from the system, and determine Qualifie LANDSCAPE or PORTRAIT
361 orientation 0, 180 : PORTRAIT_QUALIFIER (default)
362 orientation 90, 270 : LANDSCAPE_QUALIFIER
365 qualifiersOut.push_back( std::string( PORTRAIT_QUALIFIER ) );
370 * @brief Construct a qualified style name out of qualifiers
372 * A qualifed style name will be in the format: style-qualifier0-qualifier1-qualifierN
374 * @param[in] styleName The root name of the style
375 * @param[in] qualifiers List of qualifier names
376 * @param[out] qualifiedStyleOut The qualified style name
378 static void BuildQualifiedStyleName(
379 const std::string& styleName,
380 const std::vector<std::string>& qualifiers,
381 std::string& qualifiedStyleOut )
383 qualifiedStyleOut.append( styleName );
385 for( std::vector<std::string>::const_iterator it = qualifiers.begin(),
386 itEnd = qualifiers.end(); it != itEnd; ++it )
388 const std::string& str = *it;
390 qualifiedStyleOut.append( "-" );
391 qualifiedStyleOut.append( str );
395 static bool GetStyleNameForControl( Toolkit::Builder builder, Toolkit::Control control, std::string& styleName)
397 styleName = control.GetStyleName();
399 if( styleName.empty() )
401 styleName = control.GetTypeName();
404 // Apply the style after choosing the correct actual style (e.g. landscape or portrait)
405 std::vector<std::string> qualifiers;
406 CollectQualifiers( qualifiers );
409 std::string qualifiedStyleName;
412 qualifiedStyleName.clear();
413 BuildQualifiedStyleName( styleName, qualifiers, qualifiedStyleName );
415 // Break if style found or we have tried the root style name (qualifiers is empty)
416 if( GetImpl(builder).LookupStyleName( qualifiedStyleName ) )
421 if( qualifiers.size() == 0 )
425 // Remove the last qualifier in an attempt to find a style that is valid
426 qualifiers.pop_back();
431 styleName = qualifiedStyleName;
436 void StyleManager::ApplyStyle( Toolkit::Builder builder, Toolkit::Control control )
438 std::string styleName = control.GetStyleName();
439 if( GetStyleNameForControl( builder, control, styleName ) )
441 builder.ApplyStyle( styleName, control );
444 if( mDefaultFontSize >= 0 )
446 // Apply the style for logical font size
447 std::stringstream fontSizeQualifier;
448 fontSizeQualifier << styleName << FONT_SIZE_QUALIFIER << mDefaultFontSize;
449 builder.ApplyStyle( fontSizeQualifier.str(), control );
453 const StylePtr StyleManager::GetRecordedStyle( Toolkit::Control control )
457 std::string styleName = control.GetStyleName();
459 if( GetStyleNameForControl( mThemeBuilder, control, styleName ) )
461 const StylePtr style = GetImpl(mThemeBuilder).GetStyle( styleName );
465 return StylePtr(NULL);
468 Toolkit::Builder StyleManager::FindCachedBuilder( const std::string& key )
470 BuilderMap::iterator builderIt = mBuilderCache.find( key );
471 if( builderIt != mBuilderCache.end() )
473 return builderIt->second;
476 return Toolkit::Builder();
479 void StyleManager::CacheBuilder( Toolkit::Builder builder, const std::string& key )
481 mBuilderCache[ key ] = builder;
484 void StyleManager::StyleMonitorChange( StyleMonitor styleMonitor, StyleChange::Type styleChange )
486 switch ( styleChange )
488 case StyleChange::DEFAULT_FONT_CHANGE:
490 mDefaultFontFamily = styleMonitor.GetDefaultFontFamily();
494 case StyleChange::DEFAULT_FONT_SIZE_CHANGE:
496 mDefaultFontSize = styleMonitor.GetDefaultFontSize();
500 case StyleChange::THEME_CHANGE:
502 SetTheme( styleMonitor.GetTheme() );
506 EmitStyleChangeSignals( styleChange );
509 void StyleManager::EmitStyleChangeSignals( StyleChange::Type styleChange )
511 Toolkit::StyleManager styleManager = StyleManager::Get();
513 // Update Controls first
514 mControlStyleChangeSignal.Emit( styleManager, styleChange );
516 // Inform application last
517 mStyleChangedSignal.Emit( styleManager, styleChange );
521 } // namespace Internal
523 } // namespace Toolkit