Kylin/Fluent/Theme.cpp

182 lines
6.0 KiB
C++
Raw Normal View History

2024-08-21 23:26:43 +08:00
#include "Theme.h"
2024-08-31 04:13:21 +08:00
#include "Colors.h"
#include <QGuiApplication>
#include <QPalette>
#include <QThreadPool>
static bool systemDark() {
QPalette palette = QGuiApplication::palette();
QColor color = palette.color(QPalette::Window).rgb();
return color.red() * 0.2126 + color.green() * 0.7152 + color.blue() * 0.0722 <= 255.0f / 2;
}
Theme *Theme::instance() {
static Theme *self = nullptr;
if (self == nullptr) {
self = new Theme();
}
return self;
}
Theme *Theme::create(QQmlEngine *, QJSEngine *) {
auto ret = instance();
QJSEngine::setObjectOwnership(ret, QJSEngine::CppOwnership);
return ret;
}
2024-08-21 23:26:43 +08:00
Theme::Theme(QObject *parent) : QObject{parent} {
2024-08-31 04:13:21 +08:00
m_accentColor = Colors::instance()->Blue();
m_darkMode = ThemeType::DarkMode::Light;
m_nativeText = false;
m_animationEnabled = true;
m_systemDark = systemDark();
m_desktopImagePath = "";
m_blurBehindWindowEnabled = false;
QGuiApplication::instance()->installEventFilter(this);
refreshColors();
connect(this, &Theme::darkModeChanged, this, [=] { Q_EMIT darkChanged(); });
connect(this, &Theme::darkChanged, this, [=] { refreshColors(); });
connect(this, &Theme::accentColorChanged, this, [=] { refreshColors(); });
connect(&m_watcher, &QFileSystemWatcher::fileChanged, this,
[=](const QString &path) { Q_EMIT desktopImagePathChanged(); });
connect(this, &Theme::blurBehindWindowEnabledChanged, this, [=] { checkUpdateDesktopImage(); });
startTimer(1000);
}
bool Theme::dark() const {
if (m_darkMode == ThemeType::DarkMode::Dark) {
return true;
} else if (m_darkMode == ThemeType::DarkMode::System) {
return m_systemDark;
} else {
return false;
}
2024-08-21 23:26:43 +08:00
}
QColor Theme::fontPrimaryColor() const {
return m_fontPrimaryColor;
}
void Theme::setFontPrimaryColor(const QColor &color) {
if (m_fontPrimaryColor != color) {
m_fontPrimaryColor = color;
emit fontPrimaryColorChanged();
}
}
QColor Theme::itemNormalColor() const {
return m_itemNormalColor;
}
void Theme::setItemNormalColor(const QColor &color) {
if (m_itemNormalColor != color) {
m_itemNormalColor = color;
emit itemNormalColorChanged();
}
}
2024-08-23 00:04:16 +08:00
QColor Theme::itemHoverColor() const {
return m_itemHoverColor;
}
void Theme::setItemHoverColor(const QColor &color) {
if (m_itemHoverColor != color) {
m_itemHoverColor = color;
emit itemHoverColorChanged();
}
}
QColor Theme::windowBackgroundColor() const {
return m_windowBackgroundColor;
}
void Theme::setWindowBackgroundColor(const QColor &color) {
if (m_windowBackgroundColor != color) {
m_windowBackgroundColor = color;
emit windowBackgroundColorChanged();
}
}
QColor Theme::windowActiveBackgroundColor() const {
return m_windowActiveBackgroundColor;
}
void Theme::setWindowActiveBackgroundColor(const QColor &color) {
if (m_windowActiveBackgroundColor != color) {
m_windowActiveBackgroundColor = color;
emit windowActiveBackgroundColorChanged();
}
}
QString Theme::desktopImagePath() const {
return m_desktopImagePath;
}
void Theme::setDesktopImagePath(const QString &path) {
if (m_desktopImagePath != path) {
m_desktopImagePath = path;
emit desktopImagePathChanged();
}
}
bool Theme::blurBehindWindowEnabled() const {
return m_blurBehindWindowEnabled;
}
void Theme::setBlurBehindWindowEnabled(bool enabled) {
if (m_blurBehindWindowEnabled != enabled) {
m_blurBehindWindowEnabled = enabled;
emit blurBehindWindowEnabledChanged();
}
}
2024-08-31 04:13:21 +08:00
void Theme::refreshColors() {
auto isDark = dark();
primaryColor(isDark ? m_accentColor->lighter() : m_accentColor->dark());
backgroundColor(isDark ? QColor(0, 0, 0, 255) : QColor(255, 255, 255, 255));
dividerColor(isDark ? QColor(80, 80, 80, 255) : QColor(210, 210, 210, 255));
setWindowBackgroundColor(isDark ? QColor(32, 32, 32, 255) : QColor(237, 237, 237, 255));
setWindowActiveBackgroundColor(isDark ? QColor(26, 26, 26, 255) : QColor(243, 243, 243, 255));
setFontPrimaryColor(isDark ? QColor(248, 248, 248, 255) : QColor(7, 7, 7, 255));
fontSecondaryColor(isDark ? QColor(222, 222, 222, 255) : QColor(102, 102, 102, 255));
fontTertiaryColor(isDark ? QColor(200, 200, 200, 255) : QColor(153, 153, 153, 255));
setItemNormalColor(isDark ? QColor(255, 255, 255, 0) : QColor(0, 0, 0, 0));
frameColor(isDark ? QColor(56, 56, 56, qRound(255 * 0.8)) : QColor(243, 243, 243, qRound(255 * 0.8)));
frameActiveColor(isDark ? QColor(48, 48, 48, qRound(255 * 0.8)) : QColor(255, 255, 255, qRound(255 * 0.8)));
setItemHoverColor(isDark ? QColor(255, 255, 255, qRound(255 * 0.06)) : QColor(0, 0, 0, qRound(255 * 0.03)));
itemPressColor(isDark ? QColor(255, 255, 255, qRound(255 * 0.09)) : QColor(0, 0, 0, qRound(255 * 0.06)));
itemCheckColor(isDark ? QColor(255, 255, 255, qRound(255 * 0.12)) : QColor(0, 0, 0, qRound(255 * 0.09)));
}
void Theme::checkUpdateDesktopImage() {
if (!m_blurBehindWindowEnabled) {
return;
}
QThreadPool::globalInstance()->start([=]() {
m_mutex.lock();
auto path = Utilities::instance()->getWallpaperFilePath();
if (m_desktopImagePath != path) {
if (!m_desktopImagePath.isEmpty()) {
m_watcher.removePath(m_desktopImagePath);
}
setDesktopImagePath(path);
m_watcher.addPath(path);
}
m_mutex.unlock();
});
}
bool Theme::eventFilter(QObject *, QEvent *event) {
if (event->type() == QEvent::ApplicationPaletteChange || event->type() == QEvent::ThemeChange) {
m_systemDark = systemDark();
Q_EMIT darkChanged();
event->accept();
return true;
}
return false;
}
void Theme::timerEvent(QTimerEvent *event) {
checkUpdateDesktopImage();
}