2 * Copyright (c) 2016 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/";
64 BaseHandle handle = StyleManager::Get();
68 SingletonService singletonService( SingletonService::Get() );
69 if ( singletonService )
71 Toolkit::StyleManager manager = Toolkit::StyleManager( new Internal::StyleManager() );
72 singletonService.Register( typeid( manager ), manager );
80 DALI_TYPE_REGISTRATION_BEGIN_CREATE( Toolkit::StyleManager, Dali::BaseHandle, Create, true )
81 DALI_TYPE_REGISTRATION_END()
85 Toolkit::StyleManager StyleManager::Get()
87 Toolkit::StyleManager manager;
89 SingletonService singletonService( SingletonService::Get() );
90 if ( singletonService )
92 // Check whether the style manager is already created
93 Dali::BaseHandle handle = singletonService.GetSingleton( typeid( Toolkit::StyleManager ) );
96 // If so, downcast the handle of singleton
97 manager = Toolkit::StyleManager( dynamic_cast< StyleManager* >( handle.GetObjectPtr() ) );
104 StyleManager::StyleManager()
105 : mDefaultFontSize( -1 ),
106 mDefaultFontFamily(""),
107 mFeedbackStyle( NULL )
109 // Add theme builder constants
110 mThemeBuilderConstants[ PACKAGE_PATH_KEY ] = DEFAULT_PACKAGE_PATH;
111 mThemeBuilderConstants[ APPLICATION_RESOURCE_PATH_KEY ] = Application::GetResourcePath();
113 mStyleMonitor = StyleMonitor::Get();
116 mStyleMonitor.StyleChangeSignal().Connect( this, &StyleManager::StyleMonitorChange );
117 mDefaultFontSize = mStyleMonitor.GetDefaultFontSize();
120 // Sound & haptic style
121 mFeedbackStyle = new FeedbackStyle();
124 StyleManager::~StyleManager()
126 delete mFeedbackStyle;
129 void StyleManager::ApplyTheme( const std::string& themeFile )
131 SetTheme( themeFile );
134 void StyleManager::ApplyDefaultTheme()
136 SetTheme( DEFAULT_THEME );
139 const std::string& StyleManager::GetDefaultFontFamily() const
141 return mDefaultFontFamily;
144 void StyleManager::SetStyleConstant( const std::string& key, const Property::Value& value )
146 mStyleBuilderConstants[ key ] = value;
149 bool StyleManager::GetStyleConstant( const std::string& key, Property::Value& valueOut )
151 Property::Value* value = mStyleBuilderConstants.Find( key );
161 void StyleManager::ApplyThemeStyle( Toolkit::Control control )
170 ApplyStyle( mThemeBuilder, control );
174 void StyleManager::ApplyThemeStyleAtInit( Toolkit::Control control )
176 ApplyThemeStyle( control );
180 mFeedbackStyle->ObjectCreated( control );
184 void StyleManager::ApplyStyle( Toolkit::Control control, const std::string& jsonFileName, const std::string& styleName )
186 bool builderReady = false;
188 // First look in the cache
189 Toolkit::Builder builder = FindCachedBuilder( jsonFileName );
196 // Merge theme and style constants
197 Property::Map constants( mThemeBuilderConstants );
198 constants.Merge( mStyleBuilderConstants );
201 builder = CreateBuilder( constants );
203 if( LoadJSON( builder, jsonFileName ) )
205 CacheBuilder( builder, jsonFileName );
210 // Apply the style to the control
213 builder.ApplyStyle( styleName, control );
217 Toolkit::StyleManager::StyleChangedSignalType& StyleManager::StyleChangedSignal()
219 return mStyleChangedSignal;
222 Toolkit::StyleManager::StyleChangedSignalType& StyleManager::ControlStyleChangeSignal()
224 return mControlStyleChangeSignal;
227 void StyleManager::SetTheme( const std::string& themeFile )
229 bool themeLoaded = false;
231 if( mThemeFile.compare(DEFAULT_THEME) == 0 && mThemeBuilder )
233 // We have already loaded the default theme into mThemeBuilder
237 // Reload the default theme
238 mThemeBuilder = CreateBuilder( mThemeBuilderConstants );
239 themeLoaded = LoadJSON( mThemeBuilder, DEFAULT_THEME );
242 if( themeFile.compare(DEFAULT_THEME) != 0 )
244 // The theme is different to the default: Merge it
245 themeLoaded = LoadJSON( mThemeBuilder, themeFile );
246 mThemeFile = themeFile;
253 mFeedbackStyle->StyleChanged( mThemeFile, StyleChange::THEME_CHANGE );
256 EmitStyleChangeSignals(StyleChange::THEME_CHANGE);
260 mThemeBuilder.Reset();
264 const Property::Map StyleManager::GetConfigurations()
266 Property::Map result;
269 result = mThemeBuilder.GetConfigurations();
273 bool themeLoaded = false;
275 mThemeBuilder = CreateBuilder( mThemeBuilderConstants );
277 // Load default theme because this is first try to load stylesheet.
278 themeLoaded = LoadJSON( mThemeBuilder, DEFAULT_THEME );
279 mThemeFile = DEFAULT_THEME;
283 result = mThemeBuilder.GetConfigurations();
290 bool StyleManager::LoadFile( const std::string& filename, std::string& stringOut )
292 DALI_ASSERT_DEBUG( 0 != filename.length());
294 // as toolkit is platform agnostic, it cannot load files from filesystem
295 // ask style monitor to load the style sheet
298 return mStyleMonitor.LoadThemeFile( filename, stringOut );
304 Toolkit::Builder StyleManager::CreateBuilder( const Property::Map& constants )
306 Toolkit::Builder builder = Toolkit::Builder::New();
307 builder.AddConstants( constants );
312 bool StyleManager::LoadJSON( Toolkit::Builder builder, const std::string& jsonFilePath )
314 std::string fileString;
315 if( LoadFile( jsonFilePath, fileString ) )
317 builder.LoadFromString( fileString );
322 DALI_LOG_WARNING("Error loading file '%s'\n", jsonFilePath.c_str());
327 static void CollectQualifiers( std::vector<std::string>& qualifiersOut )
329 // Append the relevant qualifier for orientation
330 // int orientation = 0; // Get the orientation from the system
333 Getting orientation from the system, and determine Qualifie LANDSCAPE or PORTRAIT
334 orientation 0, 180 : PORTRAIT_QUALIFIER (default)
335 orientation 90, 270 : LANDSCAPE_QUALIFIER
338 qualifiersOut.push_back( std::string( PORTRAIT_QUALIFIER ) );
343 * @brief Construct a qualified style name out of qualifiers
345 * A qualifed style name will be in the format: style-qualifier0-qualifier1-qualifierN
347 * @param[in] styleName The root name of the style
348 * @param[in] qualifiers List of qualifier names
349 * @param[out] qualifiedStyleOut The qualified style name
351 static void BuildQualifiedStyleName(
352 const std::string& styleName,
353 const std::vector<std::string>& qualifiers,
354 std::string& qualifiedStyleOut )
356 qualifiedStyleOut.append( styleName );
358 for( std::vector<std::string>::const_iterator it = qualifiers.begin(),
359 itEnd = qualifiers.end(); it != itEnd; ++it )
361 const std::string& str = *it;
363 qualifiedStyleOut.append( "-" );
364 qualifiedStyleOut.append( str );
368 static bool GetStyleNameForControl( Toolkit::Builder builder, Toolkit::Control control, std::string& styleName)
370 styleName = control.GetStyleName();
372 if( styleName.empty() )
374 styleName = control.GetTypeName();
377 // Apply the style after choosing the correct actual style (e.g. landscape or portrait)
378 std::vector<std::string> qualifiers;
379 CollectQualifiers( qualifiers );
382 std::string qualifiedStyleName;
385 qualifiedStyleName.clear();
386 BuildQualifiedStyleName( styleName, qualifiers, qualifiedStyleName );
388 // Break if style found or we have tried the root style name (qualifiers is empty)
389 if( GetImpl(builder).LookupStyleName( qualifiedStyleName ) )
394 if( qualifiers.size() == 0 )
398 // Remove the last qualifier in an attempt to find a style that is valid
399 qualifiers.pop_back();
404 styleName = qualifiedStyleName;
409 void StyleManager::ApplyStyle( Toolkit::Builder builder, Toolkit::Control control )
411 std::string styleName = control.GetStyleName();
412 if( GetStyleNameForControl( builder, control, styleName ) )
414 builder.ApplyStyle( styleName, control );
417 if( mDefaultFontSize >= 0 )
419 // Apply the style for logical font size
420 std::stringstream fontSizeQualifier;
421 fontSizeQualifier << styleName << FONT_SIZE_QUALIFIER << mDefaultFontSize;
422 builder.ApplyStyle( fontSizeQualifier.str(), control );
426 const StylePtr StyleManager::GetRecordedStyle( Toolkit::Control control )
430 std::string styleName = control.GetStyleName();
432 if( GetStyleNameForControl( mThemeBuilder, control, styleName ) )
434 const StylePtr style = GetImpl(mThemeBuilder).GetStyle( styleName );
438 return StylePtr(NULL);
441 Toolkit::Builder StyleManager::FindCachedBuilder( const std::string& key )
443 BuilderMap::iterator builderIt = mBuilderCache.find( key );
444 if( builderIt != mBuilderCache.end() )
446 return builderIt->second;
449 return Toolkit::Builder();
452 void StyleManager::CacheBuilder( Toolkit::Builder builder, const std::string& key )
454 mBuilderCache[ key ] = builder;
457 void StyleManager::StyleMonitorChange( StyleMonitor styleMonitor, StyleChange::Type styleChange )
459 switch ( styleChange )
461 case StyleChange::DEFAULT_FONT_CHANGE:
463 mDefaultFontFamily = styleMonitor.GetDefaultFontFamily();
467 case StyleChange::DEFAULT_FONT_SIZE_CHANGE:
469 mDefaultFontSize = styleMonitor.GetDefaultFontSize();
473 case StyleChange::THEME_CHANGE:
475 SetTheme( styleMonitor.GetTheme() );
479 EmitStyleChangeSignals( styleChange );
482 void StyleManager::EmitStyleChangeSignals( StyleChange::Type styleChange )
484 Toolkit::StyleManager styleManager = StyleManager::Get();
486 // Update Controls first
487 mControlStyleChangeSignal.Emit( styleManager, styleChange );
489 // Inform application last
490 mStyleChangedSignal.Emit( styleManager, styleChange );
494 } // namespace Internal
496 } // namespace Toolkit