mirror of
https://github.com/zealdocs/zeal.git
synced 2026-08-30 09:00:52 +08:00
fix(ui): correctly apply color scheme without reload
Only works with Qt 6.5 and above.
This commit is contained in:
@@ -17,6 +17,8 @@
|
||||
#include <QWebEngineSettings>
|
||||
|
||||
#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
|
||||
#include <QStyle>
|
||||
#include <QStyleFactory>
|
||||
#include <QStyleHints>
|
||||
#else
|
||||
#include <QPalette>
|
||||
@@ -50,6 +52,16 @@ Settings::Settings(QObject *parent)
|
||||
qRegisterMetaType<ExternalLinkPolicy>("ExternalLinkPolicy");
|
||||
#endif
|
||||
|
||||
#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
|
||||
// When the OS color scheme changes, reapply the color scheme.
|
||||
connect(qApp->styleHints(), &QStyleHints::colorSchemeChanged,
|
||||
this, [this]() {
|
||||
if (contentAppearance == ContentAppearance::Automatic) {
|
||||
applyColorScheme();
|
||||
}
|
||||
});
|
||||
#endif
|
||||
|
||||
load();
|
||||
}
|
||||
|
||||
@@ -71,7 +83,37 @@ bool Settings::isDarkModeEnabled() const
|
||||
return false;
|
||||
}
|
||||
|
||||
Zeal::Core::Settings::ColorScheme Settings::colorScheme()
|
||||
#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
|
||||
void Settings::applyColorScheme()
|
||||
{
|
||||
Qt::ColorScheme scheme = Qt::ColorScheme::Unknown;
|
||||
switch (contentAppearance) {
|
||||
case ContentAppearance::Light:
|
||||
scheme = Qt::ColorScheme::Light;
|
||||
break;
|
||||
case ContentAppearance::Dark:
|
||||
scheme = Qt::ColorScheme::Dark;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
qApp->styleHints()->setColorScheme(scheme);
|
||||
// setColorScheme() alone doesn't reliably update existing widgets:
|
||||
// - Widgets with stylesheets (QStyleSheetStyle) only update on polish().
|
||||
// - Direct palette consumers (QTreeView, QLineEdit) only update when the
|
||||
// application palette changes.
|
||||
// Re-instantiating the style triggers the full unpolish-polish cycle for
|
||||
// QStyleSheetStyle widgets and also updates the application palette via
|
||||
// the new style's standardPalette().
|
||||
QStyle *newStyle = QStyleFactory::create(qApp->style()->name());
|
||||
if (newStyle != nullptr) {
|
||||
qApp->setStyle(newStyle);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
Settings::ColorScheme Settings::colorScheme()
|
||||
{
|
||||
#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
|
||||
return QApplication::styleHints()->colorScheme();
|
||||
@@ -127,6 +169,10 @@ void Settings::load()
|
||||
}
|
||||
#endif
|
||||
|
||||
#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
|
||||
applyColorScheme();
|
||||
#endif
|
||||
|
||||
// Fonts
|
||||
QWebEngineSettings *webSettings = QWebEngineProfile::defaultProfile()->settings();
|
||||
serifFontFamily = settings->value(QStringLiteral("serif_font_family"),
|
||||
@@ -296,6 +342,10 @@ void Settings::save()
|
||||
|
||||
settings->sync();
|
||||
|
||||
#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
|
||||
applyColorScheme();
|
||||
#endif
|
||||
|
||||
emit updated();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user