aboutsummaryrefslogtreecommitdiff
path: root/src/ui/ThemeResourceDictionary.cpp
blob: d9483f6c518dfe1c891099d155706575ae561519 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include "cru/ui/ThemeResourceDictionary.h"
#include "cru/common/io/FileStream.h"
#include "cru/common/log/Logger.h"
#include "cru/xml/XmlNode.h"
#include "cru/xml/XmlParser.h"

namespace cru::ui {

std::unique_ptr<ThemeResourceDictionary> ThemeResourceDictionary::FromFile(
    const String& file_path) {
  io::FileStream stream(file_path, io::OpenFileFlags::Read);
  auto xml_string = stream.ReadAllAsString();
  auto parser = xml::XmlParser(xml_string);
  return std::make_unique<ThemeResourceDictionary>(parser.Parse(), false);
}

ThemeResourceDictionary::ThemeResourceDictionary(xml::XmlElementNode* xml_root,
                                                 bool clone) {
  Expects(xml_root);
  xml_root_.reset(clone ? xml_root->Clone()->AsElement() : xml_root);
  UpdateResourceMap(xml_root_.get());
}

ThemeResourceDictionary::~ThemeResourceDictionary() = default;

void ThemeResourceDictionary::UpdateResourceMap(xml::XmlElementNode* xml_root) {
  if (!xml_root->GetTag().CaseInsensitiveEqual(u"Theme")) {
    throw Exception(u"Root tag of theme must be 'Theme'.");
  }

  for (auto child : 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() != 1) {
          throw Exception(u"Resource must have only one child element.");
        }

        ResourceEntry entry;

        entry.name = *key_attr;
        entry.xml_node = c->GetFirstChildElement();

        resource_map_[entry.name] = std::move(entry);
      } else {
        CRU_LOG_DEBUG(u"Ignore unknown element {} of theme.", c->GetTag());
      }
    } else {
      CRU_LOG_WARN(u"Ignore text node of theme.");
    }
  }
}
}  // namespace cru::ui