feat: auto dark reader mode that sync with system theme

This commit is contained in:
shenleban tongying 2024-11-05 23:39:08 -05:00 committed by GitHub
parent b6fa66df92
commit 673911b1c5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 39 additions and 2 deletions

View file

@ -15,6 +15,7 @@
#include <QFile>
#include <QTextDocumentFragment>
#include <QUrl>
#include <QStyleHints>
#include "fmt/core.h"
#include "fmt/compile.h"
@ -151,7 +152,22 @@ std::string ArticleMaker::makeHtmlHeader( QString const & word, QString const &
result += R"(<script src="qrc:///scripts/gd-builtin.js"></script>)";
result += R"(<script src="qrc:///scripts/mark.min.js"></script>)";
/// 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"(<link href="qrc:///article-style-darkmode.css" media="all" rel="stylesheet" type="text/css">)";

View file

@ -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)

View file

@ -36,6 +36,8 @@
#include <QThreadPool>
#include <QSslConfiguration>
#include <QStyleFactory>
#include <QStyleHints>
#include "weburlrequestinterceptor.hh"
#include "folding.hh"
@ -61,6 +63,7 @@
#include <windows.h>
#endif
#include <QGuiApplication>
#include <QWebEngineSettings>
#include <QProxyStyle>
@ -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 );

View file

@ -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 ) );