2023-05-17 22:44:38 +08:00
|
|
|
#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>
|
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"
|
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();
|
|
|
|
QGuiApplication::instance()->installEventFilter(this);
|
|
|
|
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(); });
|
|
|
|
refreshColors();
|
2023-04-19 22:27:27 +08:00
|
|
|
}
|
|
|
|
|
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));
|
|
|
|
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();
|
2023-04-19 22:27:27 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|