refactor: prepare config code to implement auto dark (reader) mode
Some checks are pending
SonarCloud / Build and analyze (push) Waiting to run

This commit is contained in:
shenleban tongying 2024-11-05 22:27:19 -05:00 committed by GitHub
parent 9315dda365
commit b6fa66df92
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 80 additions and 35 deletions

View file

@ -151,7 +151,7 @@ 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>)";
if ( GlobalBroadcaster::instance()->getPreference()->darkReaderMode ) {
if ( GlobalBroadcaster::instance()->getPreference()->darkReaderMode == Config::Dark::On ) {
//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

@ -213,8 +213,6 @@ Preferences::Preferences():
selectWordBySingleClick( false ),
autoScrollToTargetArticle( true ),
escKeyHidesMainWindow( false ),
darkMode( false ),
darkReaderMode( false ),
alwaysOnTop( false ),
searchInDock( false ),
// on macOS, register hotkeys will override system shortcuts, disabled for now to avoid troubles.
@ -947,11 +945,12 @@ Class load()
}
if ( !preferences.namedItem( "darkMode" ).isNull() ) {
c.preferences.darkMode = ( preferences.namedItem( "darkMode" ).toElement().text() == "1" );
c.preferences.darkMode = static_cast< Dark >( preferences.namedItem( "darkMode" ).toElement().text().toInt() );
}
if ( !preferences.namedItem( "darkReaderMode" ).isNull() ) {
c.preferences.darkReaderMode = ( preferences.namedItem( "darkReaderMode" ).toElement().text() == "1" );
c.preferences.darkReaderMode =
static_cast< Dark >( preferences.namedItem( "darkReaderMode" ).toElement().text().toInt() );
}
if ( !preferences.namedItem( "zoomFactor" ).isNull() ) {
@ -1882,11 +1881,11 @@ void save( Class const & c )
preferences.appendChild( opt );
opt = dd.createElement( "darkMode" );
opt.appendChild( dd.createTextNode( c.preferences.darkMode ? "1" : "0" ) );
opt.appendChild( dd.createTextNode( QString::number( static_cast< int >( c.preferences.darkMode ) ) ) );
preferences.appendChild( opt );
opt = dd.createElement( "darkReaderMode" );
opt.appendChild( dd.createTextNode( c.preferences.darkReaderMode ? "1" : "0" ) );
opt.appendChild( dd.createTextNode( QString::number( static_cast< int >( c.preferences.darkReaderMode ) ) ) );
preferences.appendChild( opt );
opt = dd.createElement( "zoomFactor" );

View file

@ -28,6 +28,13 @@ enum GroupId : unsigned {
/// GoldenDict's configuration
namespace Config {
// Tri states enum for Dark and Dark reader mode
enum class Dark : std::uint8_t {
Off = 0,
On = 1,
// TODO: Auto = 2,
};
/// Dictionaries which are temporarily disabled via the dictionary bar.
typedef QSet< QString > MutedDictionaries;
@ -420,8 +427,8 @@ struct Preferences
// Appearances
bool darkMode;
bool darkReaderMode;
Dark darkMode = Dark::Off;
Dark darkReaderMode = Dark::Off;
QString addonStyle;
QString displayStyle; // Article Display style (Which also affect interface style on windows)

View file

@ -1229,7 +1229,7 @@ void ArticleView::syncBackgroundColorWithCfgDarkReader() const
{
// Only works Qt6.6.3+ https://bugreports.qt.io/browse/QTBUG-112013
#if QT_VERSION >= QT_VERSION_CHECK( 6, 6, 3 )
if ( cfg.preferences.darkReaderMode ) {
if ( cfg.preferences.darkReaderMode == Config::Dark::On ) {
webview->page()->setBackgroundColor( QColor( 39, 40, 40 ) );
}
else {

View file

@ -1318,7 +1318,7 @@ QPrinter & MainWindow::getPrinter()
void MainWindow::updateAppearances( QString const & addonStyle,
QString const & displayStyle,
bool const & darkMode
Config::Dark darkMode
#if !defined( Q_OS_WIN )
,
const QString & interfaceStyle
@ -1326,7 +1326,7 @@ void MainWindow::updateAppearances( QString const & addonStyle,
)
{
#ifdef Q_OS_WIN32
if ( darkMode ) {
if ( darkMode == Config::Dark::On ) {
//https://forum.qt.io/topic/101391/windows-10-dark-theme
QPalette darkPalette;
@ -1381,7 +1381,7 @@ void MainWindow::updateAppearances( QString const & addonStyle,
// Load an additional stylesheet
// Dark Mode doesn't work nice with custom qt style sheets,
if ( !darkMode ) {
if ( darkMode == Config::Dark::Off ) {
QFile additionalStyle( QString( ":qt-%1.css" ).arg( displayStyle ) );
if ( additionalStyle.open( QFile::ReadOnly ) ) {
css += additionalStyle.readAll();
@ -1406,7 +1406,7 @@ void MainWindow::updateAppearances( QString const & addonStyle,
}
#ifdef Q_OS_WIN32
if ( darkMode ) {
if ( darkMode == Config::Dark::On ) {
css += "QToolTip { color: #ffffff; background-color: #2a82da; border: 1px solid white; }";
}
#endif

View file

@ -190,7 +190,7 @@ private:
/// Applies Qt stylesheets, use Windows dark palette etc....
void updateAppearances( const QString & addonStyle,
const QString & displayStyle,
const bool & darkMode
Config::Dark darkMode
#if !defined( Q_OS_WIN )
,
const QString & interfaceStyle

View file

@ -181,9 +181,25 @@ Preferences::Preferences( QWidget * parent, Config::Class & cfg_ ):
ui.selectBySingleClick->setChecked( p.selectWordBySingleClick );
ui.autoScrollToTargetArticle->setChecked( p.autoScrollToTargetArticle );
ui.escKeyHidesMainWindow->setChecked( p.escKeyHidesMainWindow );
ui.darkMode->setChecked( p.darkMode );
ui.darkReaderMode->setChecked( p.darkReaderMode );
ui.darkMode->addItem( tr( "On" ), QVariant::fromValue( Config::Dark::On ) );
ui.darkMode->addItem( tr( "Off" ), QVariant::fromValue( Config::Dark::Off ) );
if ( auto i = ui.darkMode->findData( QVariant::fromValue( p.darkMode ) ); i != -1 ) {
ui.darkMode->setCurrentIndex( i );
}
ui.darkReaderMode->addItem( tr( "On" ), QVariant::fromValue( Config::Dark::On ) );
ui.darkReaderMode->addItem( tr( "Off" ), QVariant::fromValue( Config::Dark::Off ) );
if ( auto i = ui.darkReaderMode->findData( QVariant::fromValue( p.darkReaderMode ) ); i != -1 ) {
ui.darkReaderMode->setCurrentIndex( i );
}
#ifndef Q_OS_WIN32
// TODO: make this availiable on other platforms
ui.darkModeLabel->hide();
ui.darkMode->hide();
#endif
@ -413,8 +429,9 @@ Config::Preferences Preferences::getPreferences()
p.autoScrollToTargetArticle = ui.autoScrollToTargetArticle->isChecked();
p.escKeyHidesMainWindow = ui.escKeyHidesMainWindow->isChecked();
p.darkMode = ui.darkMode->isChecked();
p.darkReaderMode = ui.darkReaderMode->isChecked();
p.darkMode = ui.darkMode->currentData().value< Config::Dark >();
p.darkReaderMode = ui.darkReaderMode->currentData().value< Config::Dark >();
p.enableMainWindowHotkey = ui.enableMainWindowHotkey->isChecked();
p.mainWindowHotkey = ui.mainWindowHotkey->keySequence();
p.enableClipboardHotkey = ui.enableClipboardHotkey->isChecked();

View file

@ -397,24 +397,46 @@ the application.</string>
</layout>
</item>
<item>
<widget class="QCheckBox" name="darkMode">
<property name="toolTip">
<string>Turn the UI to dark.</string>
</property>
<property name="text">
<string>Dark Mode</string>
</property>
</widget>
<layout class="QHBoxLayout" name="darkModeLayout">
<item>
<widget class="QLabel" name="darkModeLabel">
<property name="toolTip">
<string>Turn the UI to dark.</string>
</property>
<property name="text">
<string>Dark Mode</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="darkMode">
<property name="toolTip">
<string>Turn the UI to dark.</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QCheckBox" name="darkReaderMode">
<property name="toolTip">
<string>Turn the article display style to dark.</string>
</property>
<property name="text">
<string>Dark Reader Mode</string>
</property>
</widget>
<layout class="QHBoxLayout" name="darkReaderLayout">
<item>
<widget class="QLabel" name="darkReaderModeLabel">
<property name="toolTip">
<string>Turn the article display style to dark.</string>
</property>
<property name="text">
<string>Dark Reader Mode</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="darkReaderMode">
<property name="toolTip">
<string>Turn the article display style to dark.</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<spacer name="verticalSpacer_5">