aboutsummaryrefslogtreecommitdiff
path: root/include/cru/ui
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2022-01-27 21:21:51 +0800
committercrupest <crupest@outlook.com>2022-01-27 21:21:51 +0800
commit560c0ead613658a2b7444907c3d1d69e49be8c32 (patch)
tree37938098f0e80403d572accf9f7a275ec8792c00 /include/cru/ui
parent56633dab8c1bf9d25a6367a651b5b054055a2130 (diff)
downloadcru-560c0ead613658a2b7444907c3d1d69e49be8c32.tar.gz
cru-560c0ead613658a2b7444907c3d1d69e49be8c32.tar.bz2
cru-560c0ead613658a2b7444907c3d1d69e49be8c32.zip
...
Diffstat (limited to 'include/cru/ui')
-rw-r--r--include/cru/ui/ThemeManager.hpp33
-rw-r--r--include/cru/ui/UiManager.hpp5
-rw-r--r--include/cru/ui/controls/Control.hpp4
-rw-r--r--include/cru/ui/mapper/Mapper.hpp4
-rw-r--r--include/cru/ui/style/StyleRuleSet.hpp13
5 files changed, 36 insertions, 23 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
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<platform::graphics::IBrush> text_brush;
std::shared_ptr<platform::graphics::IBrush> text_selection_brush;
std::shared_ptr<platform::graphics::IBrush> caret_brush;
-
- std::shared_ptr<style::StyleRuleSet> button_style;
- std::shared_ptr<style::StyleRuleSet> 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<platform::gui::ICursor> cursor);
public:
- style::StyleRuleSet* GetStyleRuleSet();
+ std::shared_ptr<style::StyleRuleSet> GetStyleRuleSet();
//*************** region: events ***************
public:
@@ -151,7 +151,7 @@ class CRU_UI_API Control : public Object {
std::shared_ptr<platform::gui::ICursor> cursor_ = nullptr;
- std::unique_ptr<style::StyleRuleSet> style_rule_set_;
+ std::shared_ptr<style::StyleRuleSet> style_rule_set_;
std::unique_ptr<style::StyleRuleSetBind> 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<StyleRuleSet> 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<StyleRuleSet> GetParent() const { return parent_; }
+ void SetParent(std::shared_ptr<StyleRuleSet> parent);
gsl::index GetSize() const { return static_cast<gsl::index>(rules_.size()); }
const std::vector<StyleRule>& GetRules() const { return rules_; }
@@ -47,7 +47,7 @@ class StyleRuleSet : public Object {
private:
Event<std::nullptr_t> change_event_;
- StyleRuleSet* parent_ = nullptr;
+ std::shared_ptr<StyleRuleSet> parent_ = nullptr;
EventRevokerGuard parent_change_event_guard_;
std::vector<StyleRule> 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<StyleRuleSet> ruleset);
CRU_DELETE_COPY(StyleRuleSetBind)
CRU_DELETE_MOVE(StyleRuleSetBind)
@@ -69,7 +70,7 @@ class StyleRuleSetBind {
private:
controls::Control* control_;
- StyleRuleSet* ruleset_;
+ std::shared_ptr<StyleRuleSet> ruleset_;
// child first, parent last.
std::vector<StyleRuleSet*> ruleset_chain_cache_;