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/public-api/controls/control.h>
29 #include <dali-toolkit/public-api/controls/control-impl.h>
30 #include <dali-toolkit/public-api/styling/style-manager.h>
31 #include <dali-toolkit/internal/feedback/feedback-style.h>
36 const char* LANDSCAPE_QUALIFIER = "landscape";
37 const char* PORTRAIT_QUALIFIER = "portrait";
38 const char* FONT_SIZE_QUALIFIER = "fontsize";
40 const char* DEFAULT_THEME = DALI_STYLE_DIR "dali-toolkit-default-theme.json";
42 const char* PACKAGE_PATH_KEY = "PACKAGE_PATH";
43 const char* APPLICATION_RESOURCE_PATH_KEY = "APPLICATION_RESOURCE_PATH";
45 const char* DEFAULT_PACKAGE_PATH = DALI_DATA_READ_ONLY_DIR "/toolkit/";
63 BaseHandle handle = StyleManager::Get();
67 SingletonService singletonService( SingletonService::Get() );
68 if ( singletonService )
70 Toolkit::StyleManager manager = Toolkit::StyleManager( new Internal::StyleManager() );
71 singletonService.Register( typeid( manager ), manager );
79 DALI_TYPE_REGISTRATION_BEGIN_CREATE( Toolkit::StyleManager, Dali::BaseHandle, Create, true )
80 DALI_TYPE_REGISTRATION_END()
84 Toolkit::StyleManager StyleManager::Get()
86 Toolkit::StyleManager manager;
88 SingletonService singletonService( SingletonService::Get() );
89 if ( singletonService )
91 // Check whether the style manager is already created
92 Dali::BaseHandle handle = singletonService.GetSingleton( typeid( Toolkit::StyleManager ) );
95 // If so, downcast the handle of singleton
96 manager = Toolkit::StyleManager( dynamic_cast< StyleManager* >( handle.GetObjectPtr() ) );
103 StyleManager::StyleManager()
104 : mDefaultFontSize( -1 ),
105 mDefaultFontFamily(""),
106 mFeedbackStyle( NULL )
108 // Add theme builder constants
109 mThemeBuilderConstants[ PACKAGE_PATH_KEY ] = DEFAULT_PACKAGE_PATH;
110 mThemeBuilderConstants[ APPLICATION_RESOURCE_PATH_KEY ] = Application::GetResourcePath();
112 mStyleMonitor = StyleMonitor::Get();
115 mStyleMonitor.StyleChangeSignal().Connect( this, &StyleManager::StyleMonitorChange );
116 mDefaultFontSize = mStyleMonitor.GetDefaultFontSize();
119 // Sound & haptic style
120 mFeedbackStyle = new FeedbackStyle();
123 StyleManager::~StyleManager()
125 delete mFeedbackStyle;
128 void StyleManager::ApplyTheme( const std::string& themeFile )
130 SetTheme( themeFile );
133 void StyleManager::ApplyDefaultTheme()
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 mThemeBuilder = CreateBuilder( mThemeBuilderConstants );
233 // Always load the default theme first, then merge in the custom theme if present
234 themeLoaded = LoadJSON( mThemeBuilder, DEFAULT_THEME );
236 if( ! themeFile.empty() )
238 mThemeFile = themeFile;
239 themeLoaded = LoadJSON( mThemeBuilder, mThemeFile );
246 mFeedbackStyle->StyleChanged( mThemeFile, StyleChange::THEME_CHANGE );
249 EmitStyleChangeSignals(StyleChange::THEME_CHANGE);
253 mThemeBuilder.Reset();
257 bool StyleManager::LoadFile( const std::string& filename, std::string& stringOut )
259 DALI_ASSERT_DEBUG( 0 != filename.length());
261 // as toolkit is platform agnostic, it cannot load files from filesystem
262 // ask style monitor to load the style sheet
265 return mStyleMonitor.LoadThemeFile( filename, stringOut );
271 Toolkit::Builder StyleManager::CreateBuilder( const Property::Map& constants )
273 Toolkit::Builder builder = Toolkit::Builder::New();
274 builder.AddConstants( constants );
279 bool StyleManager::LoadJSON( Toolkit::Builder builder, const std::string& jsonFilePath )
281 std::string fileString;
282 if( LoadFile( jsonFilePath, fileString ) )
284 builder.LoadFromString( fileString );
289 DALI_LOG_WARNING("Error loading file '%s'\n", jsonFilePath.c_str());
294 void StyleManager::CollectQualifiers( StringList& qualifiersOut )
296 // Append the relevant qualifier for orientation
297 int orientation = 0; // Get the orientation from the system
298 switch( orientation )
303 qualifiersOut.push_back( std::string( LANDSCAPE_QUALIFIER ) );
307 case 0: // fall through
310 qualifiersOut.push_back( std::string( PORTRAIT_QUALIFIER ) );
316 void StyleManager::BuildQualifiedStyleName( const std::string& styleName, const StringList& qualifiers, std::string& qualifiedStyleOut )
318 qualifiedStyleOut.append( styleName );
320 for( StringList::const_iterator it = qualifiers.begin(), itEnd = qualifiers.end(); it != itEnd; ++it )
322 const std::string& str = *it;
324 qualifiedStyleOut.append( "-" );
325 qualifiedStyleOut.append( str );
329 void StyleManager::ApplyStyle( Toolkit::Builder builder, Toolkit::Control control )
331 std::string styleName = control.GetStyleName();
333 if( styleName.empty() )
335 styleName = control.GetTypeName();
338 // Apply the style after choosing the correct actual style (e.g. landscape or portrait)
339 StringList qualifiers;
340 CollectQualifiers( qualifiers );
344 std::string qualifiedStyleName;
345 BuildQualifiedStyleName( styleName, qualifiers, qualifiedStyleName );
347 // Break if style found or we have tried the root style name (qualifiers is empty)
348 if( builder.ApplyStyle( qualifiedStyleName, control ) || qualifiers.size() == 0 )
353 // Remove the last qualifier in an attempt to find a style that is valid
354 qualifiers.pop_back();
357 if( mDefaultFontSize >= 0 )
359 // Apply the style for logical font size
360 std::stringstream fontSizeQualifier;
361 fontSizeQualifier << styleName << FONT_SIZE_QUALIFIER << mDefaultFontSize;
362 builder.ApplyStyle( fontSizeQualifier.str(), control );
366 Toolkit::Builder StyleManager::FindCachedBuilder( const std::string& key )
368 BuilderMap::iterator builderIt = mBuilderCache.find( key );
369 if( builderIt != mBuilderCache.end() )
371 return builderIt->second;
374 return Toolkit::Builder();
377 void StyleManager::CacheBuilder( Toolkit::Builder builder, const std::string& key )
379 mBuilderCache[ key ] = builder;
382 void StyleManager::StyleMonitorChange( StyleMonitor styleMonitor, StyleChange::Type styleChange )
384 switch ( styleChange )
386 case StyleChange::DEFAULT_FONT_CHANGE:
388 mDefaultFontFamily = styleMonitor.GetDefaultFontFamily();
392 case StyleChange::DEFAULT_FONT_SIZE_CHANGE:
394 mDefaultFontSize = styleMonitor.GetDefaultFontSize();
398 case StyleChange::THEME_CHANGE:
400 SetTheme( styleMonitor.GetTheme() );
404 EmitStyleChangeSignals( styleChange );
407 void StyleManager::EmitStyleChangeSignals( StyleChange::Type styleChange )
409 Toolkit::StyleManager styleManager = StyleManager::Get();
411 // Update Controls first
412 mControlStyleChangeSignal.Emit( styleManager, styleChange );
414 // Inform application last
415 mStyleChangedSignal.Emit( styleManager, styleChange );
419 } // namespace Internal
421 } // namespace Toolkit