mirror of
https://github.com/xiaoyifang/goldendict-ng.git
synced 2024-11-23 20:14:05 +00:00
feat: auto dark reader mode that sync with system theme
This commit is contained in:
parent
b6fa66df92
commit
673911b1c5
|
@ -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">)";
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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 );
|
||||
|
|
|
@ -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 ) );
|
||||
|
||||
|
|
Loading…
Reference in a new issue