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
98 template<typename MANIFEST>
99 static void apply(typename MANIFEST::Themes themeKey)
100 {
101 const ThemeTag& tag = Manifest::getTheme(themeKey);
102 applyTheme(static_cast<const Theme&>(tag));
103 }
104
115 template<typename T>
116 static void set(const T& theme)
117 {
118 static_assert(std::is_base_of_v<Theme, T>,
119 "ThemeManager::set() requires a type derived from ml::Theme");
120 _active = std::make_unique<T>(theme);
121 notify();
122 }
123
124 // ── Subscription (called by Themeable — not for user code) ────────────
125
132 static void subscribe(Themeable* component);
133
140 static void unsubscribe(Themeable* component);
141
142 // ── Lifecycle ─────────────────────────────────────────────────────────
143
151 static void shutdown();
152
153 private:
154 static void applyTheme(const Theme& theme);
155
163 static void notify();
164
165 inline static std::unique_ptr<Theme> _active = std::make_unique<DarkTheme>();
166 inline static std::vector<Themeable*> _subscribers = {};
167 inline static bool _destroyed = false;
168 };
169
170} // namespace ml
171
172#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 stored in a Manifest enum.
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