diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/ui/ThemeManager.cpp | 22 | ||||
-rw-r--r-- | src/xml/XmlNode.cpp | 7 |
2 files changed, 25 insertions, 4 deletions
diff --git a/src/ui/ThemeManager.cpp b/src/ui/ThemeManager.cpp index dc08edb3..be980e8c 100644 --- a/src/ui/ThemeManager.cpp +++ b/src/ui/ThemeManager.cpp @@ -49,11 +49,15 @@ void ThemeManager::ReadResourcesFile(const String& file_path) { io::FileStream stream(file_path, io::OpenFileFlags::Read); auto xml_string = stream.ReadAllAsString(); auto parser = xml::XmlParser(xml_string); - theme_resource_xml_root_.reset(parser.Parse()); + SetThemeXml(parser.Parse()); +} + +void ThemeManager::SetThemeXml(xml::XmlElementNode* root) { + theme_resource_xml_root_.reset(root); theme_resource_map_.clear(); if (!theme_resource_xml_root_->GetTag().CaseInsensitiveEqual(u"Theme")) { - throw Exception(u"Root tag of theme resource file must be \"Theme\"."); + throw Exception(u"Root tag of theme must be \"Theme\"."); } for (auto child : theme_resource_xml_root_->GetChildren()) { @@ -64,10 +68,20 @@ void ThemeManager::ReadResourcesFile(const String& file_path) { if (!key_attr) { throw Exception(u"\"key\" attribute is required for resource."); } - theme_resource_map_[*key_attr] = c->GetFirstChildElement(); + if (!c->GetChildElementCount()) { + throw Exception(u"Resource must have only one child element."); + } + + ResourceEntry entry; + + entry.name = *key_attr; + entry.xml_node = c->GetFirstChildElement(); + + theme_resource_map_[entry.name] = std::move(entry); } } } -} + theme_resource_change_event_.Raise(nullptr); +} } // namespace cru::ui diff --git a/src/xml/XmlNode.cpp b/src/xml/XmlNode.cpp index 79847544..00437f9b 100644 --- a/src/xml/XmlNode.cpp +++ b/src/xml/XmlNode.cpp @@ -1,4 +1,5 @@ #include "cru/xml/XmlNode.hpp" +#include <algorithm> namespace cru::xml { @@ -32,6 +33,12 @@ void XmlElementNode::AddChild(XmlNode* child) { child->parent_ = this; } +Index XmlElementNode::GetChildElementCount() const { + return std::count_if( + children_.cbegin(), children_.cend(), + [](xml::XmlNode* node) { return node->IsElementNode(); }); +} + XmlElementNode* XmlElementNode::GetFirstChildElement() const { for (auto child : children_) { if (child->GetType() == XmlNode::Type::Element) { |