aboutsummaryrefslogtreecommitdiff
path: root/include/cru/ui/ThemeManager.hpp
blob: 7d52fa48ebd57e56c1d4d5739f57d81912401eb2 (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
58
59
60
61
62
63
64
65
66
67
#pragma once
#include "Base.hpp"
#include "cru/common/Base.hpp"
#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>

namespace cru::ui {
class CRU_UI_API ThemeResourceKeyNotExistException : public Exception {
 public:
  using Exception::Exception;
};

class CRU_UI_API BadThemeResourceException : public Exception {
 public:
  using Exception::Exception;
};

class CRU_UI_API ThemeManager : public Object {
 public:
  static ThemeManager* GetInstance();

 private:
  ThemeManager();

 public:
  CRU_DELETE_COPY(ThemeManager)
  CRU_DELETE_MOVE(ThemeManager)

  ~ThemeManager() override;

  IEvent<std::nullptr_t>* ThemeResourceChangeEvent() {
    return &theme_resource_change_event_;
  }

  void ReadResourcesFile(const String& file_path);

  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::unique_ptr<xml::XmlElementNode> theme_resource_xml_root_;
  std::unordered_map<String, xml::XmlElementNode*> theme_resource_map_;
};
}  // namespace cru::ui