From 673911b1c5a9496743f60420d00919ea904b5ad7 Mon Sep 17 00:00:00 2001 From: shenleban tongying Date: Tue, 5 Nov 2024 23:39:08 -0500 Subject: [PATCH] feat: auto dark reader mode that sync with system theme --- src/article_maker.cc | 16 ++++++++++++++++ src/config.hh | 10 ++++++++-- src/ui/mainwindow.cc | 13 +++++++++++++ src/ui/preferences.cc | 2 ++ 4 files changed, 39 insertions(+), 2 deletions(-) diff --git a/src/article_maker.cc b/src/article_maker.cc index 0429afa4..7a0d2f40 100644 --- a/src/article_maker.cc +++ b/src/article_maker.cc @@ -15,6 +15,7 @@ #include #include #include +#include #include "fmt/core.h" #include "fmt/compile.h" @@ -151,7 +152,22 @@ std::string ArticleMaker::makeHtmlHeader( QString const & word, QString const & result += R"()"; result += R"()"; + /// Handling Dark reader mode. + + bool darkReaderModeEnabled = false; + if ( GlobalBroadcaster::instance()->getPreference()->darkReaderMode == Config::Dark::On ) { + darkReaderModeEnabled = true; + } + +#if QT_VERSION >= QT_VERSION_CHECK( 6, 5, 0 ) + if ( GlobalBroadcaster::instance()->getPreference()->darkReaderMode == Config::Dark::Auto + && QGuiApplication::styleHints()->colorScheme() == Qt::ColorScheme::Dark ) { + darkReaderModeEnabled = true; + } +#endif + + if ( darkReaderModeEnabled ) { //only enable this darkmode on modern style. if ( cfg.displayStyle == "modern" ) { result += R"()"; diff --git a/src/config.hh b/src/config.hh index f489f0fa..71aa0d61 100644 --- a/src/config.hh +++ b/src/config.hh @@ -32,7 +32,7 @@ namespace Config { enum class Dark : std::uint8_t { Off = 0, On = 1, - // TODO: Auto = 2, + Auto = 2, }; /// Dictionaries which are temporarily disabled via the dictionary bar. @@ -428,7 +428,13 @@ struct Preferences // Appearances Dark darkMode = Dark::Off; - Dark darkReaderMode = Dark::Off; + Dark darkReaderMode = +#if defined( Q_OS_MACOS ) + Dark::Auto; +#else + Dark::Off; +#endif + QString addonStyle; QString displayStyle; // Article Display style (Which also affect interface style on windows) diff --git a/src/ui/mainwindow.cc b/src/ui/mainwindow.cc index d8bb3910..cbba62d4 100644 --- a/src/ui/mainwindow.cc +++ b/src/ui/mainwindow.cc @@ -36,6 +36,8 @@ #include #include #include +#include + #include "weburlrequestinterceptor.hh" #include "folding.hh" @@ -61,6 +63,7 @@ #include #endif +#include #include #include @@ -2321,6 +2324,7 @@ void MainWindow::editPreferences() || cfg.preferences.collapseBigArticles != p.collapseBigArticles || cfg.preferences.articleSizeLimit != p.articleSizeLimit || cfg.preferences.alwaysExpandOptionalParts != p.alwaysExpandOptionalParts // DSL format's special feature + || p.darkReaderMode == Config::Dark::Auto // We cannot know if a reload is needed, just do it regardless. ); // This line must be here because the components below require cfg's value to reconfigure @@ -2336,6 +2340,15 @@ void MainWindow::editPreferences() if ( needReload ) { view.reload(); } + +#if QT_VERSION >= QT_VERSION_CHECK( 6, 5, 0 ) + if ( cfg.preferences.darkReaderMode == Config::Dark::Auto ) { + connect( QGuiApplication::styleHints(), &QStyleHints::colorSchemeChanged, &view, &ArticleView::reload ); + } + else { + disconnect( QGuiApplication::styleHints(), &QStyleHints::colorSchemeChanged, &view, &ArticleView::reload ); + } +#endif } audioPlayerFactory.setPreferences( cfg.preferences ); diff --git a/src/ui/preferences.cc b/src/ui/preferences.cc index d9a85474..5ac6d1ea 100644 --- a/src/ui/preferences.cc +++ b/src/ui/preferences.cc @@ -189,6 +189,8 @@ Preferences::Preferences( QWidget * parent, Config::Class & cfg_ ): ui.darkMode->setCurrentIndex( i ); } + ui.darkReaderMode->addItem( tr( "Auto" ), QVariant::fromValue( Config::Dark::Auto ) ); + ui.darkReaderMode->setItemData( 0, tr( "Auto does nothing on some systems." ), Qt::ToolTipRole ); ui.darkReaderMode->addItem( tr( "On" ), QVariant::fromValue( Config::Dark::On ) ); ui.darkReaderMode->addItem( tr( "Off" ), QVariant::fromValue( Config::Dark::Off ) );