aboutsummaryrefslogtreecommitdiff
path: root/src/ui/ThemeManager.cpp
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2022-02-23 21:28:53 +0800
committercrupest <crupest@outlook.com>2022-02-23 21:28:53 +0800
commit603f46e195823530bafda97f0dda1a332cc39dc8 (patch)
tree3378c68e09a4225b172b182a805b0a906233ee83 /src/ui/ThemeManager.cpp
parent81b3bfede96dd606597f609985c68994ec0e9ad4 (diff)
downloadcru-603f46e195823530bafda97f0dda1a332cc39dc8.tar.gz
cru-603f46e195823530bafda97f0dda1a332cc39dc8.tar.bz2
cru-603f46e195823530bafda97f0dda1a332cc39dc8.zip
...
Diffstat (limited to 'src/ui/ThemeManager.cpp')
-rw-r--r--src/ui/ThemeManager.cpp62
1 files changed, 21 insertions, 41 deletions
diff --git a/src/ui/ThemeManager.cpp b/src/ui/ThemeManager.cpp
index 4649f70f..1adbddaf 100644
--- a/src/ui/ThemeManager.cpp
+++ b/src/ui/ThemeManager.cpp
@@ -7,6 +7,7 @@
#include "cru/platform/graphics/Brush.h"
#include "cru/platform/graphics/Factory.h"
#include "cru/platform/gui/UiApplication.h"
+#include "cru/ui/ThemeResourceDictionary.h"
#include "cru/ui/style/StyleRuleSet.h"
#include "cru/xml/XmlParser.h"
@@ -24,11 +25,30 @@ ThemeManager::ThemeManager() {
throw Exception(u"Default resources file not found.");
}
- ReadResourcesFile(String::FromStdPath(resourses_file));
+ PrependThemeResourceDictionary(
+ ThemeResourceDictionary::FromFile(String::FromStdPath(resourses_file)));
}
ThemeManager::~ThemeManager() {}
+std::vector<ThemeResourceDictionary*>
+ThemeManager::GetThemeResourceDictionaryList() const {
+ std::vector<ThemeResourceDictionary*> result;
+ for (const auto& theme_resource_dictionary :
+ theme_resource_dictionary_list_) {
+ result.push_back(theme_resource_dictionary.get());
+ }
+ return result;
+}
+
+void ThemeManager::PrependThemeResourceDictionary(
+ std::unique_ptr<ThemeResourceDictionary> theme_resource_dictionary) {
+ theme_resource_dictionary_list_.insert(
+ theme_resource_dictionary_list_.begin(),
+ std::move(theme_resource_dictionary));
+ theme_resource_change_event_.Raise(nullptr);
+}
+
std::shared_ptr<platform::graphics::IBrush> ThemeManager::GetResourceBrush(
const String& key) {
return GetResource<std::shared_ptr<platform::graphics::IBrush>>(key);
@@ -43,44 +63,4 @@ std::shared_ptr<style::StyleRuleSet> ThemeManager::GetResourceStyleRuleSet(
const String& key) {
return GetResource<std::shared_ptr<style::StyleRuleSet>>(key);
}
-
-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);
- 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 must be \"Theme\".");
- }
-
- for (auto child : theme_resource_xml_root_->GetChildren()) {
- if (child->IsElementNode()) {
- auto c = child->AsElement();
- if (c->GetTag().CaseInsensitiveEqual(u"Resource")) {
- auto key_attr = c->GetOptionalAttributeCaseInsensitive(u"key");
- if (!key_attr) {
- throw Exception(u"\"key\" attribute is required for resource.");
- }
- 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