UI/Qt: Fall back to luma dark mode detection for unknown color scheme

The Qt style hints' color scheme can return ::Unknown, in which case we
still need to fall back to our 'old' method of using the luma component
of the background role color.
This commit is contained in:
Jelle Raaijmakers 2024-11-25 02:08:22 +01:00 committed by Tim Flynn
parent 1870f1a0a8
commit f0bcb07ff7
Notes: github-actions[bot] 2024-11-25 06:05:45 +00:00

View file

@ -37,19 +37,17 @@ bool is_using_dark_system_theme(QWidget&);
bool is_using_dark_system_theme(QWidget& widget) bool is_using_dark_system_theme(QWidget& widget)
{ {
#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0) #if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
// We use that only for fallback mode // Use the explicitly set or system default color scheme whenever available
Q_UNUSED(widget);
// Use the new Qt API available from version 6.5.0
auto color_scheme = QGuiApplication::styleHints()->colorScheme(); auto color_scheme = QGuiApplication::styleHints()->colorScheme();
return color_scheme == Qt::ColorScheme::Dark; if (color_scheme != Qt::ColorScheme::Unknown)
#else return color_scheme == Qt::ColorScheme::Dark;
// Fallback for older Qt versions #endif
// Calculate luma based on Rec. 709 coefficients // Calculate luma based on Rec. 709 coefficients
// https://en.wikipedia.org/wiki/Rec._709#Luma_coefficients // https://en.wikipedia.org/wiki/Rec._709#Luma_coefficients
auto color = widget.palette().color(widget.backgroundRole()); auto color = widget.palette().color(widget.backgroundRole());
auto luma = 0.2126f * color.redF() + 0.7152f * color.greenF() + 0.0722f * color.blueF(); auto luma = 0.2126f * color.redF() + 0.7152f * color.greenF() + 0.0722f * color.blueF();
return luma <= 0.5f; return luma <= 0.5f;
#endif
} }
} }