diff options
Diffstat (limited to 'include/cru/ui/ThemeManager.h')
-rw-r--r-- | include/cru/ui/ThemeManager.h | 71 |
1 files changed, 18 insertions, 53 deletions
diff --git a/include/cru/ui/ThemeManager.h b/include/cru/ui/ThemeManager.h index 9176d85e..9917f219 100644 --- a/include/cru/ui/ThemeManager.h +++ b/include/cru/ui/ThemeManager.h @@ -1,28 +1,11 @@ #pragma once #include "Base.h" -#include "cru/common/Base.h" #include "cru/common/Event.h" -#include "cru/common/Exception.h" -#include "cru/platform/graphics/Brush.h" -#include "cru/ui/mapper/MapperRegistry.h" -#include "cru/ui/style/StyleRuleSet.h" -#include "cru/xml/XmlNode.h" +#include "cru/ui/ThemeResourceDictionary.h" -#include <any> -#include <typeindex> -#include <typeinfo> -#include <unordered_map> +#include <vector> namespace cru::ui { -class CRU_UI_API ThemeResourceKeyNotExistException : public Exception { - public: - using Exception::Exception; -}; - -class CRU_UI_API BadThemeResourceException : public Exception { - public: - using Exception::Exception; -}; class CRU_UI_API ThemeManager : public Object { public: @@ -37,33 +20,21 @@ class CRU_UI_API ThemeManager : public Object { ~ThemeManager() override; - IEvent<std::nullptr_t>* ThemeResourceChangeEvent() { - return &theme_resource_change_event_; - } - - void ReadResourcesFile(const String& file_path); + std::vector<ThemeResourceDictionary*> GetThemeResourceDictionaryList() const; - void SetThemeXml(xml::XmlElementNode* root); + void PrependThemeResourceDictionary( + std::unique_ptr<ThemeResourceDictionary> theme_resource_dictionary); 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)); + for (const auto& resource_dictionary : theme_resource_dictionary_list_) { + try { + return resource_dictionary->GetResource<T>(key); + } catch (ThemeResourceKeyNotExistException&) { + } } - - auto& cache = find_result->second.cache; - auto cache_find_result = cache.find(typeid(T)); - if (cache_find_result != cache.cend()) { - return std::any_cast<T>(cache_find_result->second); - } - - auto mapper_registry = mapper::MapperRegistry::GetInstance(); - auto mapper = mapper_registry->GetMapper<T>(); - auto resource = mapper->MapFromXml(find_result->second.xml_node); - cache[typeid(T)] = resource; - return resource; + throw ThemeResourceKeyNotExistException( + Format(u"Theme resource key {} not exist.", key)); } std::shared_ptr<platform::graphics::IBrush> GetResourceBrush( @@ -74,19 +45,13 @@ class CRU_UI_API ThemeManager : public Object { std::shared_ptr<style::StyleRuleSet> GetResourceStyleRuleSet( const String& key); - private: - struct ResourceEntry { - CRU_DEFAULT_CONSTRUCTOR_DESTRUCTOR(ResourceEntry) - CRU_DEFAULT_COPY(ResourceEntry) - CRU_DEFAULT_MOVE(ResourceEntry) - - String name; - xml::XmlElementNode* xml_node; - std::unordered_map<std::type_index, std::any> cache; - }; + IEvent<std::nullptr_t>* ThemeResourceChangeEvent() { + return &theme_resource_change_event_; + } + private: Event<std::nullptr_t> theme_resource_change_event_; - std::unique_ptr<xml::XmlElementNode> theme_resource_xml_root_; - std::unordered_map<String, ResourceEntry> theme_resource_map_; + std::vector<std::unique_ptr<ThemeResourceDictionary>> + theme_resource_dictionary_list_; }; } // namespace cru::ui |