FluentUI/src/FluTheme.cpp

98 lines
3.5 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-06 21:02:33 +08:00
#if (QT_VERSION >= QT_VERSION_CHECK(6, 5, 0))
2023-06-06 20:45:25 +08:00
#include <QStyleHints>
2023-06-06 21:02:33 +08:00
#elif ((QT_VERSION >= QT_VERSION_CHECK(6, 2, 1)))
#include <QtGui/qpa/qplatformtheme.h>
#include <QtGui/private/qguiapplication_p.h>
2023-06-08 10:17:49 +08:00
#else
#include <QPalette>
2023-06-06 21:02:33 +08:00
#endif
2023-09-13 15:11:22 +08:00
#include "Def.h"
#include "FluColors.h"
2023-06-06 21:02:33 +08:00
2023-09-13 15:11:22 +08:00
FluTheme::FluTheme(QObject *parent):QObject{parent}{
2023-04-19 23:53:00 +08:00
connect(this,&FluTheme::darkModeChanged,this,[=]{
Q_EMIT darkChanged();
});
2023-11-02 15:33:59 +08:00
connect(this,&FluTheme::darkChanged,this,[=]{refreshColors();});
connect(this,&FluTheme::themeColorChanged,this,[=]{refreshColors();});
themeColor(FluColors::getInstance()->Blue());
darkMode(FluThemeType::DarkMode::Light);
2023-04-19 18:04:14 +08:00
nativeText(false);
2023-08-31 18:06:05 +08:00
enableAnimation(true);
_systemDark = systemDark();
2023-04-20 09:15:28 +08:00
qApp->installEventFilter(this);
}
2023-11-02 15:33:59 +08:00
void FluTheme::refreshColors(){
auto isDark = dark();
primaryColor(isDark ? _themeColor->lighter() : _themeColor->dark());
backgroundColor(isDark ? QColor(0,0,0,255) : QColor(1,1,1,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));
fontSecondary(isDark ? QColor(222,222,222,255) : QColor(102,102,102,255));
fontTertiary(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,255*0.03) : QColor(0,0,0,255*0.03));
itemPressColor(isDark ? QColor(255,255,255,255*0.06) : QColor(0,0,0,255*0.06));
itemCheckColor(isDark ? QColor(255,255,255,255*0.09) : QColor(0,0,0,255*0.09));
}
2023-09-13 15:11:22 +08:00
bool FluTheme::eventFilter(QObject *obj, QEvent *event){
Q_UNUSED(obj);
2023-05-17 22:02:09 +08:00
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;
}
2023-09-27 15:18:10 +08:00
QJsonArray FluTheme::awesomeList(const QString& keyword){
QJsonArray arr;
QMetaEnum enumType = Fluent_Awesome::staticMetaObject.enumerator(Fluent_Awesome::staticMetaObject.indexOfEnumerator("Fluent_AwesomeType"));
for(int i=0; i < enumType.keyCount(); ++i){
QString name = enumType.key(i);
int icon = enumType.value(i);
if(keyword.isEmpty() || name.contains(keyword)){
QJsonObject obj;
obj.insert("name",name);
obj.insert("icon",icon);
arr.append(obj);
}
}
return arr;
}
2023-09-13 15:11:22 +08:00
bool FluTheme::systemDark(){
2023-06-06 21:02:33 +08:00
#if (QT_VERSION >= QT_VERSION_CHECK(6, 5, 0))
return (QGuiApplication::styleHints()->colorScheme() == Qt::ColorScheme::Dark);
#elif ((QT_VERSION >= QT_VERSION_CHECK(6, 2, 1)))
if (const QPlatformTheme * const theme = QGuiApplicationPrivate::platformTheme()) {
return (theme->appearance() == QPlatformTheme::Appearance::Dark);
}
return false;
2023-06-08 10:17:49 +08:00
#else
QPalette palette = qApp->palette();
QColor color = palette.color(QPalette::Window).rgb();
return !(color.red() * 0.2126 + color.green() * 0.7152 + color.blue() * 0.0722 > 255 / 2);
2023-06-06 21:02:33 +08:00
#endif
2023-03-06 14:22:13 +08:00
}
2023-04-19 23:53:00 +08:00
bool FluTheme::dark(){
2023-07-18 18:24:06 +08:00
if(_darkMode == FluThemeType::DarkMode::Dark){
2023-04-19 23:53:00 +08:00
return true;
2023-07-18 18:24:06 +08:00
}else if(_darkMode == FluThemeType::DarkMode::Light){
2023-04-19 23:53:00 +08:00
return false;
2023-07-18 18:24:06 +08:00
}else if(_darkMode == FluThemeType::DarkMode::System){
2023-05-17 22:02:09 +08:00
return _systemDark;
2023-04-19 23:53:00 +08:00
}else{
return false;
}
}