FluentUI/src/FluTheme.cpp

98 lines
3.7 KiB
C++
Raw Normal View History

#include "FluTheme.h"
2023-03-06 14:22:13 +08:00
2023-09-13 15:11:22 +08:00
#include <QGuiApplication>
2023-06-08 10:17:49 +08:00
#include <QPalette>
2024-04-12 16:26:32 +08:00
#include <QImage>
2024-04-13 21:30:23 +08:00
#include <QThreadPool>
2023-09-13 15:11:22 +08:00
#include "Def.h"
#include "FluColors.h"
2024-04-12 16:26:32 +08:00
#include "FluTools.h"
2023-06-06 21:02:33 +08:00
2024-04-11 14:51:43 +08:00
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;
}
FluTheme::FluTheme(QObject *parent) : QObject{parent} {
_accentColor = FluColors::getInstance()->Blue();
_darkMode = FluThemeType::DarkMode::Light;
_nativeText = false;
_animationEnabled = true;
_systemDark = systemDark();
2024-04-12 16:26:32 +08:00
_desktopImagePath = "";
_blurBehindWindowEnabled = false;
2024-04-11 14:51:43 +08:00
QGuiApplication::instance()->installEventFilter(this);
2024-04-12 16:26:32 +08:00
refreshColors();
2024-04-11 14:51:43 +08:00
connect(this, &FluTheme::darkModeChanged, this, [=] {
2023-04-19 23:53:00 +08:00
Q_EMIT darkChanged();
});
2024-04-11 14:51:43 +08:00
connect(this, &FluTheme::darkChanged, this, [=] { refreshColors(); });
connect(this, &FluTheme::accentColorChanged, this, [=] { refreshColors(); });
2024-04-24 10:37:15 +08:00
connect(&_watcher, &QFileSystemWatcher::fileChanged, this, [=](const QString &path) {
2024-04-12 16:26:32 +08:00
Q_EMIT desktopImagePathChanged();
});
2024-04-24 10:37:15 +08:00
connect(this, &FluTheme::blurBehindWindowEnabledChanged, this, [=] { checkUpdateDesktopImage(); });
2024-04-13 10:50:27 +08:00
startTimer(1000);
}
2024-04-11 14:51:43 +08:00
void FluTheme::refreshColors() {
2023-11-02 15:33:59 +08:00
auto isDark = dark();
2024-03-07 13:58:23 +08:00
primaryColor(isDark ? _accentColor->lighter() : _accentColor->dark());
2024-04-11 14:51:43 +08:00
backgroundColor(isDark ? QColor(0, 0, 0, 255) : QColor(255, 255, 255, 255));
dividerColor(isDark ? QColor(80, 80, 80, 255) : QColor(210, 210, 210, 255));
windowBackgroundColor(isDark ? QColor(32, 32, 32, 255) : QColor(237, 237, 237, 255));
windowActiveBackgroundColor(isDark ? QColor(26, 26, 26, 255) : QColor(243, 243, 243, 255));
fontPrimaryColor(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));
itemNormalColor(isDark ? QColor(255, 255, 255, 0) : QColor(0, 0, 0, 0));
2024-04-24 09:57:28 +08:00
frameColor(isDark ? QColor(56, 56, 56, qRound(255 * 0.8)) : QColor(243, 243, 243, qRound(255 * 0.8)));
2024-04-17 21:39:55 +08:00
frameActiveColor(isDark ? QColor(48, 48, 48, qRound(255 * 0.8)) : QColor(255, 255, 255, qRound(255 * 0.8)));
2024-04-11 14:51:43 +08:00
itemHoverColor(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)));
2023-11-02 15:33:59 +08:00
}
2024-04-11 14:51:43 +08:00
bool FluTheme::eventFilter(QObject *, QEvent *event) {
if (event->type() == QEvent::ApplicationPaletteChange || event->type() == QEvent::ThemeChange) {
2023-05-17 22:02:09 +08:00
_systemDark = systemDark();
2023-04-19 23:53:00 +08:00
Q_EMIT darkChanged();
event->accept();
return true;
}
return false;
}
2024-04-11 14:51:43 +08:00
bool FluTheme::dark() const {
if (_darkMode == FluThemeType::DarkMode::Dark) {
2023-04-19 23:53:00 +08:00
return true;
2024-04-11 14:51:43 +08:00
} else if (_darkMode == FluThemeType::DarkMode::System) {
2023-05-17 22:02:09 +08:00
return _systemDark;
2024-04-11 14:51:43 +08:00
} else {
2023-04-19 23:53:00 +08:00
return false;
}
}
2024-04-12 16:26:32 +08:00
2024-04-24 10:37:15 +08:00
void FluTheme::checkUpdateDesktopImage() {
if (!_blurBehindWindowEnabled) {
2024-04-23 00:31:20 +08:00
return;
}
2024-04-13 21:30:23 +08:00
QThreadPool::globalInstance()->start([=]() {
_mutex.lock();
auto path = FluTools::getInstance()->getWallpaperFilePath();
2024-04-24 10:37:15 +08:00
if (_desktopImagePath != path) {
if (!_desktopImagePath.isEmpty()) {
2024-04-13 21:30:23 +08:00
_watcher.removePath(_desktopImagePath);
}
desktopImagePath(path);
_watcher.addPath(path);
2024-04-12 16:26:32 +08:00
}
2024-04-13 21:30:23 +08:00
_mutex.unlock();
});
2024-04-12 16:26:32 +08:00
}
2024-04-24 10:37:15 +08:00
void FluTheme::timerEvent(QTimerEvent *event) {
2024-04-23 00:31:20 +08:00
checkUpdateDesktopImage();
2024-04-12 16:26:32 +08:00
}