diff options
author | crupest <crupest@outlook.com> | 2022-01-27 21:21:51 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2022-01-27 21:21:51 +0800 |
commit | 560c0ead613658a2b7444907c3d1d69e49be8c32 (patch) | |
tree | 37938098f0e80403d572accf9f7a275ec8792c00 /include/cru/ui/ThemeManager.hpp | |
parent | 56633dab8c1bf9d25a6367a651b5b054055a2130 (diff) | |
download | cru-560c0ead613658a2b7444907c3d1d69e49be8c32.tar.gz cru-560c0ead613658a2b7444907c3d1d69e49be8c32.tar.bz2 cru-560c0ead613658a2b7444907c3d1d69e49be8c32.zip |
...
Diffstat (limited to 'include/cru/ui/ThemeManager.hpp')
-rw-r--r-- | include/cru/ui/ThemeManager.hpp | 33 |
1 files changed, 25 insertions, 8 deletions
diff --git a/include/cru/ui/ThemeManager.hpp b/include/cru/ui/ThemeManager.hpp index 07b5f57b..7d52fa48 100644 --- a/include/cru/ui/ThemeManager.hpp +++ b/include/cru/ui/ThemeManager.hpp @@ -4,6 +4,9 @@ #include "cru/common/Event.hpp" #include "cru/common/Exception.hpp" #include "cru/platform/graphics/Brush.hpp" +#include "cru/ui/mapper/MapperRegistry.hpp" +#include "cru/ui/style/StyleRuleSet.hpp" +#include "cru/xml/XmlNode.hpp" #include <unordered_map> @@ -29,22 +32,36 @@ class CRU_UI_API ThemeManager : public Object { CRU_DELETE_COPY(ThemeManager) CRU_DELETE_MOVE(ThemeManager) - ~ThemeManager() override = default; + ~ThemeManager() override; IEvent<std::nullptr_t>* ThemeResourceChangeEvent() { return &theme_resource_change_event_; } - gsl::not_null<std::shared_ptr<platform::graphics::IBrush>> GetBrush( - StringView key); + void ReadResourcesFile(const String& file_path); - private: - void Init(); + template <typename T> + T GetResource(const String& key) { + auto find_result = theme_resource_map_.find(key); + if (find_result == theme_resource_map_.cend()) { + throw ThemeResourceKeyNotExistException( + Format(u"Theme resource key \"%s\" not exist.", key)); + } + + auto mapper_registry = mapper::MapperRegistry::GetInstance(); + auto mapper = mapper_registry->GetMapper<T>(); + return mapper->MapFromXml(find_result->second); + } + + std::shared_ptr<platform::graphics::IBrush> GetResourceBrush( + const String& key); + + std::shared_ptr<style::StyleRuleSet> GetResourceStyleRuleSet( + const String& key); private: Event<std::nullptr_t> theme_resource_change_event_; - std::unordered_map<String, String> theme_resource_map_; - std::unordered_map<String, std::shared_ptr<platform::graphics::IBrush>> - brushes_; + std::unique_ptr<xml::XmlElementNode> theme_resource_xml_root_; + std::unordered_map<String, xml::XmlElementNode*> theme_resource_map_; }; } // namespace cru::ui |