Add automatic dark color mode switching function.

This commit is contained in:
Mentalflow 2023-04-16 01:18:17 +08:00
parent 6db8c4d4ac
commit f2db544be0
No known key found for this signature in database
GPG Key ID: 5AE68D4401A2EE71

View File

@ -1,6 +1,8 @@
#include "FluTheme.h" #include "FluTheme.h"
#include "FluColors.h" #include "FluColors.h"
#include <QPalette>
#include <QGuiApplication>
FluTheme* FluTheme::m_instance = nullptr; FluTheme* FluTheme::m_instance = nullptr;
@ -19,5 +21,13 @@ FluTheme::FluTheme(QObject *parent)
textSize(13); textSize(13);
nativeText(true); nativeText(true);
frameless(true); frameless(true);
dark(false); std::function<bool()> isDark = [](){
QPalette palette = (qobject_cast<QGuiApplication *>(QCoreApplication::instance()))->palette();
QColor color = palette.color(QPalette::Window).rgb();
return !(color.red() * 0.2126 + color.green() * 0.7152 + color.blue() * 0.0722 > 255 / 2);
};
dark(isDark());
connect(qobject_cast<QGuiApplication *>(QCoreApplication::instance()), &QGuiApplication::paletteChanged, this, [=] (const QPalette &) {
dark(isDark());
});
} }