FluentUI/src/FluTheme.cpp

108 lines
4.0 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>
2023-09-13 15:11:22 +08:00
#include "Def.h"
2024-04-11 19:18:37 +08:00
#include "FluentIconDef.h"
2023-09-13 15:11:22 +08:00
#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();
updateDesktopImage();
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-12 16:26:32 +08:00
connect(&_watcher, &QFileSystemWatcher::fileChanged, this, [=](const QString &path){
Q_EMIT desktopImagePathChanged();
});
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-12 16:26:32 +08:00
frameColor(isDark ? QColor(255, 255, 255, qRound(255 * 0.12)) : QColor(0, 0, 0, qRound(255 * 0.09)));
frameActiveColor(isDark ? QColor(32, 32, 32, qRound(255 * 0.8)) : QColor(255, 255, 255, qRound(255 * 0.6)));
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
[[maybe_unused]] QJsonArray FluTheme::awesomeList(const QString &keyword) {
2023-09-27 15:18:10 +08:00
QJsonArray arr;
2024-04-11 19:18:37 +08:00
QMetaEnum enumType = Fluent_Icons::staticMetaObject.enumerator(Fluent_Icons::staticMetaObject.indexOfEnumerator("Fluent_IconType"));
2024-04-11 14:51:43 +08:00
for (int i = 0; i <= enumType.keyCount() - 1; ++i) {
2023-09-27 15:18:10 +08:00
QString name = enumType.key(i);
int icon = enumType.value(i);
2024-04-11 14:51:43 +08:00
if (keyword.isEmpty() || name.contains(keyword)) {
2023-09-27 15:18:10 +08:00
QJsonObject obj;
2024-04-11 14:51:43 +08:00
obj.insert("name", name);
obj.insert("icon", icon);
2023-09-27 15:18:10 +08:00
arr.append(obj);
}
}
return arr;
}
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
void FluTheme::updateDesktopImage(){
auto path = FluTools::getInstance()->getWallpaperFilePath();
if(_desktopImagePath != path){
if(!_desktopImagePath.isEmpty()){
_watcher.removePath(_desktopImagePath);
}
desktopImagePath(path);
_watcher.addPath(path);
}
}
void FluTheme::timerEvent(QTimerEvent *event)
{
updateDesktopImage();
}