From b6fa66df92fe22154cf5e7b507f8f67de0452b08 Mon Sep 17 00:00:00 2001 From: shenleban tongying Date: Tue, 5 Nov 2024 22:27:19 -0500 Subject: [PATCH] refactor: prepare config code to implement auto dark (reader) mode --- src/article_maker.cc | 2 +- src/config.cc | 11 ++++----- src/config.hh | 11 +++++++-- src/ui/articleview.cc | 2 +- src/ui/mainwindow.cc | 8 +++---- src/ui/mainwindow.hh | 2 +- src/ui/preferences.cc | 25 ++++++++++++++++---- src/ui/preferences.ui | 54 ++++++++++++++++++++++++++++++------------- 8 files changed, 80 insertions(+), 35 deletions(-) diff --git a/src/article_maker.cc b/src/article_maker.cc index e6301ecd..0429afa4 100644 --- a/src/article_maker.cc +++ b/src/article_maker.cc @@ -151,7 +151,7 @@ std::string ArticleMaker::makeHtmlHeader( QString const & word, QString const & result += R"()"; result += R"()"; - 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"()"; diff --git a/src/config.cc b/src/config.cc index 96b3d842..eb77d295 100644 --- a/src/config.cc +++ b/src/config.cc @@ -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" ); diff --git a/src/config.hh b/src/config.hh index bc4c960d..f489f0fa 100644 --- a/src/config.hh +++ b/src/config.hh @@ -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) diff --git a/src/ui/articleview.cc b/src/ui/articleview.cc index cf03b557..b1c799a7 100644 --- a/src/ui/articleview.cc +++ b/src/ui/articleview.cc @@ -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 { diff --git a/src/ui/mainwindow.cc b/src/ui/mainwindow.cc index b5de33bb..d8bb3910 100644 --- a/src/ui/mainwindow.cc +++ b/src/ui/mainwindow.cc @@ -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 diff --git a/src/ui/mainwindow.hh b/src/ui/mainwindow.hh index 1cc0e145..81752189 100644 --- a/src/ui/mainwindow.hh +++ b/src/ui/mainwindow.hh @@ -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 diff --git a/src/ui/preferences.cc b/src/ui/preferences.cc index 9b97c9ef..d9a85474 100644 --- a/src/ui/preferences.cc +++ b/src/ui/preferences.cc @@ -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(); diff --git a/src/ui/preferences.ui b/src/ui/preferences.ui index d616e44c..6c2bba01 100644 --- a/src/ui/preferences.ui +++ b/src/ui/preferences.ui @@ -397,24 +397,46 @@ the application. - - - Turn the UI to dark. - - - Dark Mode - - + + + + + Turn the UI to dark. + + + Dark Mode + + + + + + + Turn the UI to dark. + + + + - - - Turn the article display style to dark. - - - Dark Reader Mode - - + + + + + Turn the article display style to dark. + + + Dark Reader Mode + + + + + + + Turn the article display style to dark. + + + +