2 * Copyright (c) 2023 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.
19 #include "toolkit-style-monitor.h"
22 #include <dali-toolkit/public-api/dali-toolkit-common.h>
23 #include <dali/public-api/common/vector-wrapper.h>
24 #include <dali/public-api/object/base-object.h>
25 #include <dali/public-api/signals/dali-signal.h>
29 const char* DEFAULT_THEME =
33 " \"brokenImageUrl\":\"{DALI_IMAGE_DIR}broken.png\"\n"
39 " \"fontStyle\":{\"weight\":\"normal\"},\n"
47 NamedTheme(const std::string& name, const std::string& theme)
56 typedef std::vector<NamedTheme> NamedThemes;
60 std::string gFontFamily("LucidaSans");
61 std::string gFontStyle("Regular");
64 constexpr std::string_view THROW_EXCEPTION_STYLE_FILE_NAME = "throwException";
65 class DummyException : public std::exception
72 const char* what() const throw()
74 return "DummyException";
86 * Stub for the StyleMonitor
88 class StyleMonitor : public BaseObject
90 public: // Creation & Destruction
91 static Dali::StyleMonitor Get();
95 public: // Style Information
96 std::string GetDefaultFontFamily() const;
97 std::string GetDefaultFontStyle() const;
98 float GetDefaultFontSize() const;
99 const std::string& GetTheme() const;
100 void SetTheme(std::string theme);
101 bool LoadThemeFile(const std::string& filename, std::string& output);
104 Dali::StyleMonitor::StyleChangeSignalType& StyleChangeSignal();
106 void EmitStyleChangeSignal(StyleChange::Type styleChange)
108 mStyleChangeSignal.Emit(Dali::StyleMonitor(this), styleChange);
112 Dali::StyleMonitor::StyleChangeSignalType mStyleChangeSignal;
113 static Dali::StyleMonitor mToolkitStyleMonitor;
115 std::string mTheme; ///<< Current theme name
118 Dali::StyleMonitor StyleMonitor::mToolkitStyleMonitor;
120 Dali::StyleMonitor StyleMonitor::Get()
122 if(!mToolkitStyleMonitor)
124 mToolkitStyleMonitor = Dali::StyleMonitor(new Dali::Internal::Adaptor::StyleMonitor());
126 return mToolkitStyleMonitor;
129 StyleMonitor::StyleMonitor()
134 StyleMonitor::~StyleMonitor()
138 std::string StyleMonitor::GetDefaultFontFamily() const
143 std::string StyleMonitor::GetDefaultFontStyle() const
148 float StyleMonitor::GetDefaultFontSize() const
153 const std::string& StyleMonitor::GetTheme() const
158 void StyleMonitor::SetTheme(std::string path)
161 EmitStyleChangeSignal(StyleChange::THEME_CHANGE);
164 bool StyleMonitor::LoadThemeFile(const std::string& filename, std::string& output)
166 // Throw something exceptions during load file
167 if(filename == THROW_EXCEPTION_STYLE_FILE_NAME)
169 throw DummyException();
171 for(NamedThemes::iterator iter = gThemes.begin(); iter != gThemes.end(); ++iter)
173 NamedTheme& theme = *iter;
174 if(theme.name == filename)
176 output = theme.theme;
187 output = DEFAULT_THEME;
192 Dali::StyleMonitor::StyleChangeSignalType& StyleMonitor::StyleChangeSignal()
194 return mStyleChangeSignal;
197 } // namespace Adaptor
198 } // namespace Internal
200 ////////////////////////////////////////////////////////////////////////////////////////////////////
202 Internal::Adaptor::StyleMonitor& GetImplementation(Dali::StyleMonitor& monitor)
204 BaseObject& object = monitor.GetBaseObject();
205 return static_cast<Internal::Adaptor::StyleMonitor&>(object);
207 const Internal::Adaptor::StyleMonitor& GetImplementation(const Dali::StyleMonitor& monitor)
209 const BaseObject& object = monitor.GetBaseObject();
210 return static_cast<const Internal::Adaptor::StyleMonitor&>(object);
213 StyleMonitor::StyleMonitor()
217 StyleMonitor::StyleMonitor(const StyleMonitor& monitor)
218 : BaseHandle(monitor)
222 StyleMonitor StyleMonitor::StyleMonitor::Get()
224 return Internal::Adaptor::StyleMonitor::Get();
227 StyleMonitor::~StyleMonitor()
231 std::string StyleMonitor::GetDefaultFontFamily() const
233 return GetImplementation(*this).GetDefaultFontFamily();
236 std::string StyleMonitor::GetDefaultFontStyle() const
238 return GetImplementation(*this).GetDefaultFontStyle();
241 int StyleMonitor::GetDefaultFontSize() const
243 return GetImplementation(*this).GetDefaultFontSize();
246 const std::string& StyleMonitor::GetTheme() const
248 return GetImplementation(*this).GetTheme();
251 void StyleMonitor::SetTheme(const std::string& themeFilePath)
253 GetImplementation(*this).SetTheme(themeFilePath);
256 StyleMonitor::StyleChangeSignalType& StyleMonitor::StyleChangeSignal()
258 return GetImplementation(*this).StyleChangeSignal();
261 bool StyleMonitor::LoadThemeFile(const std::string& filename, std::string& output)
263 return GetImplementation(*this).LoadThemeFile(filename, output);
266 StyleMonitor& StyleMonitor::operator=(const StyleMonitor& monitor)
270 BaseHandle::operator=(monitor);
275 StyleMonitor::StyleMonitor(Internal::Adaptor::StyleMonitor* internal)
276 : BaseHandle(internal)
284 namespace StyleMonitor
286 void SetThemeFileOutput(const std::string& name, const std::string& output)
288 for(NamedThemes::iterator iter = gThemes.begin(); iter != gThemes.end(); ++iter)
290 NamedTheme& theme = *iter;
291 if(theme.name == name)
293 theme.theme = output;
298 gThemes.push_back(NamedTheme(name, output));
301 void SetDefaultFontFamily(const std::string& family)
303 gFontFamily = family;
306 void SetDefaultFontStyle(const std::string& style)
311 void SetDefaultFontSize(float size)
316 } // namespace StyleMonitor