Fix recursion issue on MacOS.

This commit is contained in:
Roman Telezhynskyi 2023-08-11 16:16:22 +03:00
parent 19059ccaff
commit 92af5107d7

View File

@ -593,47 +593,70 @@ auto VTheme::GetResourceName(const QString &root, const QString &iconName) -> QS
VTheme::VTheme(QObject *parent) VTheme::VTheme(QObject *parent)
: QObject(parent) : QObject(parent)
{ {
bool isProcessingColorSchemeChange = false;
#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0) #if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
QStyleHints *hints = QGuiApplication::styleHints(); auto colorSchemeChangedSlot = [this, &isProcessingColorSchemeChange]()
connect(hints, &QStyleHints::colorSchemeChanged, this, {
[this]() if (isProcessingColorSchemeChange)
{
return; // Already processing, avoid recursion
}
isProcessingColorSchemeChange = true;
VCommonSettings *settings = VAbstractApplication::VApp()->Settings();
VThemeMode themeMode = settings->GetThemeMode();
if (themeMode == VThemeMode::System && VTheme::NativeDarkThemeAvailable())
{
if (QGuiApplication::styleHints()->colorScheme() == Qt::ColorScheme::Dark)
{ {
VCommonSettings *settings = VAbstractApplication::VApp()->Settings(); settings->SetThemeMode(VThemeMode::Light);
VThemeMode themeMode = settings->GetThemeMode(); }
if (themeMode == VThemeMode::System && VTheme::NativeDarkThemeAvailable()) else
{ {
if (QGuiApplication::styleHints()->colorScheme() == Qt::ColorScheme::Dark) settings->SetThemeMode(VThemeMode::Dark);
{ }
settings->SetThemeMode(VThemeMode::Light);
}
else
{
settings->SetThemeMode(VThemeMode::Dark);
}
ResetThemeSettings(); ResetThemeSettings();
QCoreApplication::processEvents(); QCoreApplication::processEvents();
settings->SetThemeMode(themeMode); settings->SetThemeMode(themeMode);
} }
ResetThemeSettings(); ResetThemeSettings();
});
isProcessingColorSchemeChange = false;
};
QStyleHints *hints = QGuiApplication::styleHints();
connect(hints, &QStyleHints::colorSchemeChanged, this, colorSchemeChangedSlot);
#else #else
if (VTheme::NativeDarkThemeAvailable()) if (VTheme::NativeDarkThemeAvailable())
{ {
m_darkTheme = IsInDarkTheme(); m_darkTheme = IsInDarkTheme();
m_themeTimer = new QTimer(this); m_themeTimer = new QTimer(this);
m_themeTimer->setTimerType(Qt::VeryCoarseTimer); m_themeTimer->setTimerType(Qt::VeryCoarseTimer);
connect(m_themeTimer, &QTimer::timeout, this,
[this]() auto colorSchemeTimeoutCheck = [this, &isProcessingColorSchemeChange]()
{ {
bool darkTheme = IsInDarkTheme(); if (isProcessingColorSchemeChange)
if (m_darkTheme != darkTheme) {
{ return; // Already processing, avoid recursion
m_darkTheme = darkTheme; }
ResetThemeSettings();
} isProcessingColorSchemeChange = true;
});
bool darkTheme = IsInDarkTheme();
if (m_darkTheme != darkTheme)
{
m_darkTheme = darkTheme;
ResetThemeSettings();
}
isProcessingColorSchemeChange = false;
};
connect(m_themeTimer, &QTimer::timeout, this, colorSchemeTimeoutCheck);
m_themeTimer->start(V_SECONDS(5)); m_themeTimer->start(V_SECONDS(5));
} }
#endif // QT_VERSION >= QT_VERSION_CHECK(6, 5, 0) #endif // QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)