Loading...
Searching...
No Matches
ThemeManager.h
Go to the documentation of this file.
1//
2// ThemeManager.h
3//
4
5#ifndef MALENA_THEMEMANAGER_H
6#define MALENA_THEMEMANAGER_H
7
8#pragma once
9
15#include <vector>
16#include <memory>
17#include <algorithm>
18
19namespace ml
20{
21 class Themeable;
22
23 // ── ThemeManager ─────────────────────────────────────────────────────────
24
57 {
58 public:
59 // ── Reading ───────────────────────────────────────────────────────────
60
67 static const Theme& get();
68
69 // ── Applying themes ───────────────────────────────────────────────────
70
85 template<typename MANIFEST>
86 static void apply(typename MANIFEST::Themes themeKey)
87 {
88 const ThemeTag& tag = Manifest::getTheme(themeKey);
89 applyTheme(static_cast<const Theme&>(tag));
90 }
91
102 template<typename T>
103 static void set(const T& theme)
104 {
105 static_assert(std::is_base_of_v<Theme, T>,
106 "ThemeManager::set() requires a type derived from ml::Theme");
107 _active = std::make_unique<T>(theme);
108 notify();
109 }
110
111 // ── Subscription (called by Themeable — not for user code) ────────────
112
119 static void subscribe(Themeable* component);
120
127 static void unsubscribe(Themeable* component);
128
129 // ── Lifecycle ─────────────────────────────────────────────────────────
130
138 static void shutdown();
139
140 private:
141 static void applyTheme(const Theme& theme);
142
150 static void notify();
151
152 inline static std::unique_ptr<Theme> _active = std::make_unique<DarkTheme>();
153 inline static std::vector<Themeable*> _subscribers = {};
154 inline static bool _destroyed = false;
155 };
156
157} // namespace ml
158
159#endif // MALENA_THEMEMANAGER_H
CRTP base that gives a manager safe deferred-operation support.
static const ThemeTag & getTheme(EnumType key)
Retrieve a theme stored via set(key, ThemeDerived{}).
Global manager for the active Theme.
static void subscribe(Themeable *component)
Subscribe a component to theme change notifications.
static void unsubscribe(Themeable *component)
Unsubscribe a component from theme change notifications.
static void set(const T &theme)
Apply a theme instance directly without a manifest.
static void shutdown()
Shut down the theme system.
static void apply(typename MANIFEST::Themes themeKey)
Apply a theme registered via Manifest::set().
static const Theme & get()
Return a const reference to the currently active theme.
Trait that makes a component react to global theme changes.
Definition Themeable.h:63
#define MALENA_API
Definition Component.h:22
Universal design token set applied across all Themeable components.
Definition Theme.h:70
Lightweight polymorphic base for all Theme structs.
Definition ThemeTag.h:27