Add ThemeManager

It can listen to system changes and update the current theme, widgets can use it to get the current theme.
This commit is contained in:
luisangelsm
2026-01-12 18:35:59 +01:00
parent 1df50ce7e6
commit cc753e1866
2 changed files with 87 additions and 0 deletions

View File

@ -0,0 +1,50 @@
#include "theme_manager.h"
#include "theme.h"
#include <QGuiApplication>
#include <QStyleHints>
ThemeManager::ThemeManager()
{
}
ThemeManager &ThemeManager::instance()
{
static ThemeManager instance;
return instance;
}
void ThemeManager::initialize()
{
auto *styleHints = qGuiApp->styleHints();
auto colorScheme = styleHints->colorScheme();
// TODO: settings are needed to decide what theme to use
auto applyColorScheme = [this](Qt::ColorScheme scheme) {
setTheme(scheme == Qt::ColorScheme::Dark ? ThemeId::Dark : ThemeId::Light);
};
applyColorScheme(colorScheme);
connect(styleHints, &QStyleHints::colorSchemeChanged, this, applyColorScheme, Qt::QueuedConnection);
}
void ThemeManager::setTheme(ThemeId themeId)
{
if (this->themeId == themeId) {
return;
}
this->themeId = themeId;
updateCurrentTheme();
emit themeChanged();
}
void ThemeManager::updateCurrentTheme()
{
// TODO
}

View File

@ -0,0 +1,37 @@
#ifndef THEME_MANAGER_H
#define THEME_MANAGER_H
#include "theme.h"
#include "theme_id.h"
#include <QtCore>
class ThemeManager : public QObject
{
Q_OBJECT
public:
static ThemeManager &instance();
ThemeManager(const ThemeManager &) = delete;
ThemeManager &operator=(const ThemeManager &) = delete;
ThemeManager(ThemeManager &&) = delete;
ThemeManager &operator=(ThemeManager &&) = delete;
void initialize();
void setTheme(ThemeId themeId);
const Theme &getCurrentTheme() const { return currentTheme; }
signals:
void themeChanged();
private:
explicit ThemeManager();
ThemeId themeId = ThemeId::Classic;
Theme currentTheme;
void updateCurrentTheme();
};
#endif // THEME_MANAGER_H