diff --git a/src/libs/core/settings.cpp b/src/libs/core/settings.cpp index 1306d134..12ea876c 100644 --- a/src/libs/core/settings.cpp +++ b/src/libs/core/settings.cpp @@ -17,6 +17,8 @@ #include #if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0) +#include +#include #include #else #include @@ -50,6 +52,16 @@ Settings::Settings(QObject *parent) qRegisterMetaType("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(); } diff --git a/src/libs/core/settings.h b/src/libs/core/settings.h index 0bce7058..97126bb7 100644 --- a/src/libs/core/settings.h +++ b/src/libs/core/settings.h @@ -132,6 +132,9 @@ signals: private: void migrate(QSettings *settings) const; +#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0) + void applyColorScheme(); +#endif static QSettings *qsettings(QObject *parent = nullptr); }; diff --git a/src/libs/ui/searchsidebar.cpp b/src/libs/ui/searchsidebar.cpp index 48d03baa..7c74c5a8 100644 --- a/src/libs/ui/searchsidebar.cpp +++ b/src/libs/ui/searchsidebar.cpp @@ -44,6 +44,11 @@ SearchSidebar::SearchSidebar(const SearchSidebar *other, QWidget *parent) m_treeView->setFrameShape(QFrame::NoFrame); m_treeView->setHeaderHidden(true); m_treeView->setUniformRowHeights(true); + + // A stylesheet (even an empty one) wraps the widget in QStyleSheetStyle, + // which properly re-polishes on dynamic color scheme changes. + m_treeView->setStyleSheet(QStringLiteral("QTreeView {}")); + #ifdef Q_OS_MACOS m_treeView->setAttribute(Qt::WA_MacShowFocusRect, false); #endif @@ -84,6 +89,7 @@ SearchSidebar::SearchSidebar(const SearchSidebar *other, QWidget *parent) m_pageTocView->setFrameShape(QFrame::NoFrame); m_pageTocView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); m_pageTocView->setItemDelegate(new SearchItemDelegate(m_pageTocView)); + m_pageTocView->setStyleSheet(QStringLiteral("QListView {}")); m_pageTocView->setUniformItemSizes(true); m_pageTocView->setVisible(false); #ifdef Q_OS_MACOS diff --git a/src/libs/ui/widgets/searchedit.cpp b/src/libs/ui/widgets/searchedit.cpp index 6a140ea8..b0c87732 100644 --- a/src/libs/ui/widgets/searchedit.cpp +++ b/src/libs/ui/widgets/searchedit.cpp @@ -21,6 +21,9 @@ SearchEdit::SearchEdit(QWidget *parent) setClearButtonEnabled(true); setPlaceholderText(tr("Search")); + // Force QStyleSheetStyle wrapping so dynamic color scheme changes apply. + setStyleSheet(QStringLiteral("QLineEdit {}")); + m_completionLabel = new QLabel(this); m_completionLabel->setFont(font()); m_completionLabel->setObjectName(QStringLiteral("completer")); diff --git a/src/libs/ui/widgets/toolbarframe.cpp b/src/libs/ui/widgets/toolbarframe.cpp index 9decdf66..2bc4b548 100644 --- a/src/libs/ui/widgets/toolbarframe.cpp +++ b/src/libs/ui/widgets/toolbarframe.cpp @@ -12,6 +12,9 @@ ToolBarFrame::ToolBarFrame(QWidget *parent) { setMaximumHeight(40); setMinimumHeight(40); + + // Force QStyleSheetStyle wrapping so dynamic color scheme changes apply. + setStyleSheet(QStringLiteral("ToolBarFrame {}")); } void ToolBarFrame::paintEvent(QPaintEvent *event)