From 560c0ead613658a2b7444907c3d1d69e49be8c32 Mon Sep 17 00:00:00 2001 From: crupest Date: Thu, 27 Jan 2022 21:21:51 +0800 Subject: ... --- include/cru/ui/ThemeManager.hpp | 33 +++++++++++++++++++++++++-------- include/cru/ui/UiManager.hpp | 5 ----- include/cru/ui/controls/Control.hpp | 4 ++-- include/cru/ui/mapper/Mapper.hpp | 4 ++-- include/cru/ui/style/StyleRuleSet.hpp | 13 +++++++------ 5 files changed, 36 insertions(+), 23 deletions(-) (limited to 'include/cru/ui') 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 @@ -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* ThemeResourceChangeEvent() { return &theme_resource_change_event_; } - gsl::not_null> GetBrush( - StringView key); + void ReadResourcesFile(const String& file_path); - private: - void Init(); + template + 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(); + return mapper->MapFromXml(find_result->second); + } + + std::shared_ptr GetResourceBrush( + const String& key); + + std::shared_ptr GetResourceStyleRuleSet( + const String& key); private: Event theme_resource_change_event_; - std::unordered_map theme_resource_map_; - std::unordered_map> - brushes_; + std::unique_ptr theme_resource_xml_root_; + std::unordered_map theme_resource_map_; }; } // namespace cru::ui diff --git a/include/cru/ui/UiManager.hpp b/include/cru/ui/UiManager.hpp index d16adc4f..9ac7f416 100644 --- a/include/cru/ui/UiManager.hpp +++ b/include/cru/ui/UiManager.hpp @@ -18,11 +18,6 @@ struct ThemeResources { std::shared_ptr text_brush; std::shared_ptr text_selection_brush; std::shared_ptr caret_brush; - - std::shared_ptr button_style; - std::shared_ptr text_box_style; - - style::StyleRuleSet menu_item_style; }; class CRU_UI_API UiManager : public Object { diff --git a/include/cru/ui/controls/Control.hpp b/include/cru/ui/controls/Control.hpp index ed6233a9..c51643be 100644 --- a/include/cru/ui/controls/Control.hpp +++ b/include/cru/ui/controls/Control.hpp @@ -67,7 +67,7 @@ class CRU_UI_API Control : public Object { void SetCursor(std::shared_ptr cursor); public: - style::StyleRuleSet* GetStyleRuleSet(); + std::shared_ptr GetStyleRuleSet(); //*************** region: events *************** public: @@ -151,7 +151,7 @@ class CRU_UI_API Control : public Object { std::shared_ptr cursor_ = nullptr; - std::unique_ptr style_rule_set_; + std::shared_ptr style_rule_set_; std::unique_ptr style_rule_set_bind_; }; } // namespace cru::ui::controls diff --git a/include/cru/ui/mapper/Mapper.hpp b/include/cru/ui/mapper/Mapper.hpp index ceda8e0f..c52bec70 100644 --- a/include/cru/ui/mapper/Mapper.hpp +++ b/include/cru/ui/mapper/Mapper.hpp @@ -69,11 +69,11 @@ class CRU_UI_API BasicMapper : public MapperBase { T MapFromXml(xml::XmlElementNode* node) { if (!SupportMapFromXml()) { - throw new Exception(u"This mapper does not support map from xml."); + throw Exception(u"This mapper does not support map from xml."); } if (!XmlElementIsOfThisType(node)) { - throw new Exception(u"This xml element is not of mapping type."); + throw Exception(u"This xml element is not of mapping type."); } return DoMapFromXml(node); diff --git a/include/cru/ui/style/StyleRuleSet.hpp b/include/cru/ui/style/StyleRuleSet.hpp index b3c4f683..32b02d78 100644 --- a/include/cru/ui/style/StyleRuleSet.hpp +++ b/include/cru/ui/style/StyleRuleSet.hpp @@ -9,7 +9,7 @@ namespace cru::ui::style { class StyleRuleSet : public Object { public: StyleRuleSet() = default; - explicit StyleRuleSet(StyleRuleSet* parent); + explicit StyleRuleSet(std::shared_ptr parent); CRU_DELETE_COPY(StyleRuleSet) CRU_DELETE_MOVE(StyleRuleSet) @@ -17,8 +17,8 @@ class StyleRuleSet : public Object { ~StyleRuleSet() override = default; public: - StyleRuleSet* GetParent() const { return parent_; } - void SetParent(StyleRuleSet* parent); + std::shared_ptr GetParent() const { return parent_; } + void SetParent(std::shared_ptr parent); gsl::index GetSize() const { return static_cast(rules_.size()); } const std::vector& GetRules() const { return rules_; } @@ -47,7 +47,7 @@ class StyleRuleSet : public Object { private: Event change_event_; - StyleRuleSet* parent_ = nullptr; + std::shared_ptr parent_ = nullptr; EventRevokerGuard parent_change_event_guard_; std::vector rules_; @@ -55,7 +55,8 @@ class StyleRuleSet : public Object { class StyleRuleSetBind { public: - StyleRuleSetBind(controls::Control* control, StyleRuleSet* ruleset); + StyleRuleSetBind(controls::Control* control, + std::shared_ptr ruleset); CRU_DELETE_COPY(StyleRuleSetBind) CRU_DELETE_MOVE(StyleRuleSetBind) @@ -69,7 +70,7 @@ class StyleRuleSetBind { private: controls::Control* control_; - StyleRuleSet* ruleset_; + std::shared_ptr ruleset_; // child first, parent last. std::vector ruleset_chain_cache_; -- cgit v1.2.3