aboutsummaryrefslogtreecommitdiff
path: root/include/cru/ui/ThemeManager.h
blob: d4e6a096e8eef2c496d974b4adb8cf99c1c2f4b0 (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
#pragma once
#include "Base.h"
#include "cru/base/Event.h"
#include "cru/ui/ThemeResourceDictionary.h"

#include <vector>

namespace cru::ui {

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

 private:
  ThemeManager();

 public:
  CRU_DELETE_COPY(ThemeManager)
  CRU_DELETE_MOVE(ThemeManager)

  ~ThemeManager() override;

  std::vector<ThemeResourceDictionary*> GetThemeResourceDictionaryList() const;

  void PrependThemeResourceDictionary(
      std::unique_ptr<ThemeResourceDictionary> theme_resource_dictionary);

  template <typename T>
  T GetResource(const String& key) {
    for (const auto& resource_dictionary : theme_resource_dictionary_list_) {
      try {
        return resource_dictionary->GetResource<T>(key);
      } catch (ThemeResourceKeyNotExistException&) {
      }
    }
    throw ThemeResourceKeyNotExistException(
        Format(u"Theme resource key {} not exist.", key));
  }

  String GetResourceString(const String& key);

  std::shared_ptr<platform::graphics::IBrush> GetResourceBrush(
      const String& key);

  std::shared_ptr<platform::graphics::IFont> GetResourceFont(const String& key);

  std::shared_ptr<style::StyleRuleSet> GetResourceStyleRuleSet(
      const String& key);

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

 private:
  Event<std::nullptr_t> theme_resource_change_event_;
  std::vector<std::unique_ptr<ThemeResourceDictionary>>
      theme_resource_dictionary_list_;
};
}  // namespace cru::ui