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/adaptor-framework/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/internal/builder/builder-impl.h>
29 #include <dali-toolkit/public-api/controls/control.h>
30 #include <dali-toolkit/public-api/controls/control-impl.h>
31 #include <dali-toolkit/public-api/styling/style-manager.h>
32 #include <dali-toolkit/internal/feedback/feedback-style.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 = DALI_STYLE_DIR "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_PACKAGE_PATH = DALI_DATA_READ_ONLY_DIR "/toolkit/";
48 #if defined(DEBUG_ENABLED)
49 Debug::Filter* gLogFilter = Debug::Filter::New( Debug::NoLogging, false, "LOG_STYLE");
68 BaseHandle handle = StyleManager::Get();
72 SingletonService singletonService( SingletonService::Get() );
73 if ( singletonService )
75 Toolkit::StyleManager manager = Toolkit::StyleManager( new Internal::StyleManager() );
76 singletonService.Register( typeid( manager ), manager );
84 DALI_TYPE_REGISTRATION_BEGIN_CREATE( Toolkit::StyleManager, Dali::BaseHandle, Create, true )
85 DALI_TYPE_REGISTRATION_END()
89 Toolkit::StyleManager StyleManager::Get()
91 Toolkit::StyleManager manager;
93 SingletonService singletonService( SingletonService::Get() );
94 if ( singletonService )
96 // Check whether the style manager is already created
97 Dali::BaseHandle handle = singletonService.GetSingleton( typeid( Toolkit::StyleManager ) );
100 // If so, downcast the handle of singleton
101 manager = Toolkit::StyleManager( dynamic_cast< StyleManager* >( handle.GetObjectPtr() ) );
108 StyleManager::StyleManager()
109 : mDefaultFontSize( -1 ),
110 mDefaultFontFamily(""),
111 mFeedbackStyle( NULL )
113 // Add theme builder constants
114 mThemeBuilderConstants[ PACKAGE_PATH_KEY ] = DEFAULT_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 // Sound & haptic style
125 mFeedbackStyle = new FeedbackStyle();
128 StyleManager::~StyleManager()
130 delete mFeedbackStyle;
133 void StyleManager::ApplyTheme( const std::string& themeFile )
135 SetTheme( themeFile );
138 void StyleManager::ApplyDefaultTheme()
140 SetTheme( DEFAULT_THEME );
143 const std::string& StyleManager::GetDefaultFontFamily() const
145 return mDefaultFontFamily;
148 void StyleManager::SetStyleConstant( const std::string& key, const Property::Value& value )
150 mStyleBuilderConstants[ key ] = value;
153 bool StyleManager::GetStyleConstant( const std::string& key, Property::Value& valueOut )
155 Property::Value* value = mStyleBuilderConstants.Find( key );
165 void StyleManager::ApplyThemeStyle( Toolkit::Control control )
174 ApplyStyle( mThemeBuilder, control );
178 void StyleManager::ApplyThemeStyleAtInit( Toolkit::Control control )
180 ApplyThemeStyle( control );
184 mFeedbackStyle->ObjectCreated( control );
188 void StyleManager::ApplyStyle( Toolkit::Control control, const std::string& jsonFileName, const std::string& styleName )
190 bool builderReady = false;
192 // First look in the cache
193 Toolkit::Builder builder = FindCachedBuilder( jsonFileName );
200 // Merge theme and style constants
201 Property::Map constants( mThemeBuilderConstants );
202 constants.Merge( mStyleBuilderConstants );
205 builder = CreateBuilder( constants );
207 if( LoadJSON( builder, jsonFileName ) )
209 CacheBuilder( builder, jsonFileName );
214 // Apply the style to the control
217 builder.ApplyStyle( styleName, control );
221 Toolkit::StyleManager::StyleChangedSignalType& StyleManager::StyleChangedSignal()
223 return mStyleChangedSignal;
226 Toolkit::StyleManager::StyleChangedSignalType& StyleManager::ControlStyleChangeSignal()
228 return mControlStyleChangeSignal;
231 void StyleManager::SetTheme( const std::string& themeFile )
233 bool themeLoaded = false;
234 bool loading = false;
236 // If we haven't loaded a theme, or the stored theme file is empty, or
237 // the previously loaded theme is different to the requested theme,
238 // first reset the builder and load the default theme.
239 if( ! mThemeBuilder || mThemeFile.empty() || mThemeFile.compare( themeFile ) != 0 )
242 mThemeBuilder = CreateBuilder( mThemeBuilderConstants );
243 themeLoaded = LoadJSON( mThemeBuilder, DEFAULT_THEME ); // Sets themeLoaded to true if theme exists
246 if( themeFile.compare(DEFAULT_THEME) != 0 )
248 // The theme is different to the default: Merge it
250 themeLoaded |= LoadJSON( mThemeBuilder, themeFile );
255 mThemeFile = themeFile;
259 // We've successfully loaded the theme file
262 mFeedbackStyle->StyleChanged( mThemeFile, StyleChange::THEME_CHANGE );
265 EmitStyleChangeSignals(StyleChange::THEME_CHANGE);
269 // We tried to load a theme, but it failed. Ensure the builder is reset
270 mThemeBuilder.Reset();
276 const Property::Map StyleManager::GetConfigurations()
278 DALI_LOG_STREAM( gLogFilter, Debug::Concise, "GetConfigurations()\n On entry, mThemeBuilder: " << (bool(mThemeBuilder)?"Created":"Empty") << " mThemeFile: " << mThemeFile);
280 Property::Map result;
283 result = mThemeBuilder.GetConfigurations();
287 DALI_LOG_STREAM( gLogFilter, Debug::Concise, "GetConfigurations() Loading default theme" );
289 bool themeLoaded = false;
291 mThemeBuilder = CreateBuilder( mThemeBuilderConstants );
293 // Load default theme because this is first try to load stylesheet.
294 themeLoaded = LoadJSON( mThemeBuilder, DEFAULT_THEME );
295 mThemeFile = DEFAULT_THEME;
299 result = mThemeBuilder.GetConfigurations();
301 DALI_LOG_STREAM( gLogFilter, Debug::Concise, " themeLoaded" << (themeLoaded?"success":"failure") );
304 DALI_LOG_STREAM( gLogFilter, Debug::Concise, "GetConfigurations()\n On exit, result Count: " << (result.Count() != 0) );
305 DALI_LOG_STREAM( gLogFilter, Debug::Verbose, " result: " << result );
310 bool StyleManager::LoadFile( const std::string& filename, std::string& stringOut )
312 DALI_ASSERT_DEBUG( 0 != filename.length());
314 // as toolkit is platform agnostic, it cannot load files from filesystem
315 // ask style monitor to load the style sheet
318 return mStyleMonitor.LoadThemeFile( filename, stringOut );
324 Toolkit::Builder StyleManager::CreateBuilder( const Property::Map& constants )
326 Toolkit::Builder builder = Toolkit::Builder::New();
327 builder.AddConstants( constants );
332 bool StyleManager::LoadJSON( Toolkit::Builder builder, const std::string& jsonFilePath )
334 std::string fileString;
335 if( LoadFile( jsonFilePath, fileString ) )
337 builder.LoadFromString( fileString );
342 DALI_LOG_WARNING("Error loading file '%s'\n", jsonFilePath.c_str());
347 static void CollectQualifiers( std::vector<std::string>& qualifiersOut )
349 // Append the relevant qualifier for orientation
350 // int orientation = 0; // Get the orientation from the system
353 Getting orientation from the system, and determine Qualifie LANDSCAPE or PORTRAIT
354 orientation 0, 180 : PORTRAIT_QUALIFIER (default)
355 orientation 90, 270 : LANDSCAPE_QUALIFIER
358 qualifiersOut.push_back( std::string( PORTRAIT_QUALIFIER ) );
363 * @brief Construct a qualified style name out of qualifiers
365 * A qualifed style name will be in the format: style-qualifier0-qualifier1-qualifierN
367 * @param[in] styleName The root name of the style
368 * @param[in] qualifiers List of qualifier names
369 * @param[out] qualifiedStyleOut The qualified style name
371 static void BuildQualifiedStyleName(
372 const std::string& styleName,
373 const std::vector<std::string>& qualifiers,
374 std::string& qualifiedStyleOut )
376 qualifiedStyleOut.append( styleName );
378 for( std::vector<std::string>::const_iterator it = qualifiers.begin(),
379 itEnd = qualifiers.end(); it != itEnd; ++it )
381 const std::string& str = *it;
383 qualifiedStyleOut.append( "-" );
384 qualifiedStyleOut.append( str );
388 static bool GetStyleNameForControl( Toolkit::Builder builder, Toolkit::Control control, std::string& styleName)
390 styleName = control.GetStyleName();
392 if( styleName.empty() )
394 styleName = control.GetTypeName();
397 // Apply the style after choosing the correct actual style (e.g. landscape or portrait)
398 std::vector<std::string> qualifiers;
399 CollectQualifiers( qualifiers );
402 std::string qualifiedStyleName;
405 qualifiedStyleName.clear();
406 BuildQualifiedStyleName( styleName, qualifiers, qualifiedStyleName );
408 // Break if style found or we have tried the root style name (qualifiers is empty)
409 if( GetImpl(builder).LookupStyleName( qualifiedStyleName ) )
414 if( qualifiers.size() == 0 )
418 // Remove the last qualifier in an attempt to find a style that is valid
419 qualifiers.pop_back();
424 styleName = qualifiedStyleName;
429 void StyleManager::ApplyStyle( Toolkit::Builder builder, Toolkit::Control control )
431 std::string styleName = control.GetStyleName();
432 if( GetStyleNameForControl( builder, control, styleName ) )
434 builder.ApplyStyle( styleName, control );
437 if( mDefaultFontSize >= 0 )
439 // Apply the style for logical font size
440 std::stringstream fontSizeQualifier;
441 fontSizeQualifier << styleName << FONT_SIZE_QUALIFIER << mDefaultFontSize;
442 builder.ApplyStyle( fontSizeQualifier.str(), control );
446 const StylePtr StyleManager::GetRecordedStyle( Toolkit::Control control )
450 std::string styleName = control.GetStyleName();
452 if( GetStyleNameForControl( mThemeBuilder, control, styleName ) )
454 const StylePtr style = GetImpl(mThemeBuilder).GetStyle( styleName );
458 return StylePtr(NULL);
461 Toolkit::Builder StyleManager::FindCachedBuilder( const std::string& key )
463 BuilderMap::iterator builderIt = mBuilderCache.find( key );
464 if( builderIt != mBuilderCache.end() )
466 return builderIt->second;
469 return Toolkit::Builder();
472 void StyleManager::CacheBuilder( Toolkit::Builder builder, const std::string& key )
474 mBuilderCache[ key ] = builder;
477 void StyleManager::StyleMonitorChange( StyleMonitor styleMonitor, StyleChange::Type styleChange )
479 switch ( styleChange )
481 case StyleChange::DEFAULT_FONT_CHANGE:
483 mDefaultFontFamily = styleMonitor.GetDefaultFontFamily();
487 case StyleChange::DEFAULT_FONT_SIZE_CHANGE:
489 mDefaultFontSize = styleMonitor.GetDefaultFontSize();
493 case StyleChange::THEME_CHANGE:
495 SetTheme( styleMonitor.GetTheme() );
499 EmitStyleChangeSignals( styleChange );
502 void StyleManager::EmitStyleChangeSignals( StyleChange::Type styleChange )
504 Toolkit::StyleManager styleManager = StyleManager::Get();
506 // Update Controls first
507 mControlStyleChangeSignal.Emit( styleManager, styleChange );
509 // Inform application last
510 mStyleChangedSignal.Emit( styleManager, styleChange );
514 } // namespace Internal
516 } // namespace Toolkit