Allow to choose color scheme on Windows

PR #21615.
This commit is contained in:
Vladimir Golovnev 2024-10-19 13:37:51 +03:00 committed by GitHub
parent 4805afc1a2
commit a47e1cdb48
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 163 additions and 4 deletions

View file

@ -80,6 +80,9 @@ void UIThemeManager::initInstance()
UIThemeManager::UIThemeManager()
: m_useCustomTheme {Preferences::instance()->useCustomUITheme()}
#ifdef QBT_HAS_COLORSCHEME_OPTION
, m_colorSchemeSetting {u"Appearance/ColorScheme"_s}
#endif
#if (defined(Q_OS_UNIX) && !defined(Q_OS_MACOS))
, m_useSystemIcons {Preferences::instance()->useSystemIcons()}
#endif
@ -90,6 +93,10 @@ UIThemeManager::UIThemeManager()
LogMsg(tr("Set app style failed. Unknown style: \"%1\"").arg(styleName), Log::WARNING);
#endif
#ifdef QBT_HAS_COLORSCHEME_OPTION
applyColorScheme();
#endif
// NOTE: Qt::QueuedConnection can be omitted as soon as support for Qt 6.5 is dropped
connect(QApplication::styleHints(), &QStyleHints::colorSchemeChanged, this, &UIThemeManager::onColorSchemeChanged, Qt::QueuedConnection);
@ -125,6 +132,38 @@ UIThemeManager *UIThemeManager::instance()
return m_instance;
}
#ifdef QBT_HAS_COLORSCHEME_OPTION
ColorScheme UIThemeManager::colorScheme() const
{
return m_colorSchemeSetting.get(ColorScheme::System);
}
void UIThemeManager::setColorScheme(const ColorScheme value)
{
if (value == colorScheme())
return;
m_colorSchemeSetting = value;
}
void UIThemeManager::applyColorScheme() const
{
switch (colorScheme())
{
case ColorScheme::System:
default:
qApp->styleHints()->unsetColorScheme();
break;
case ColorScheme::Light:
qApp->styleHints()->setColorScheme(Qt::ColorScheme::Light);
break;
case ColorScheme::Dark:
qApp->styleHints()->setColorScheme(Qt::ColorScheme::Dark);
break;
}
}
#endif
void UIThemeManager::applyStyleSheet() const
{
qApp->setStyleSheet(QString::fromUtf8(m_themeSource->readStyleSheet()));