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,11 +593,18 @@ 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(); VCommonSettings *settings = VAbstractApplication::VApp()->Settings();
VThemeMode themeMode = settings->GetThemeMode(); VThemeMode themeMode = settings->GetThemeMode();
if (themeMode == VThemeMode::System && VTheme::NativeDarkThemeAvailable()) if (themeMode == VThemeMode::System && VTheme::NativeDarkThemeAvailable())
@ -617,23 +624,39 @@ VTheme::VTheme(QObject *parent)
} }
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]()
{ {
if (isProcessingColorSchemeChange)
{
return; // Already processing, avoid recursion
}
isProcessingColorSchemeChange = true;
bool darkTheme = IsInDarkTheme(); bool darkTheme = IsInDarkTheme();
if (m_darkTheme != darkTheme) if (m_darkTheme != darkTheme)
{ {
m_darkTheme = darkTheme; m_darkTheme = darkTheme;
ResetThemeSettings(); 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)