mirror of
https://github.com/xiaoyifang/goldendict-ng.git
synced 2024-11-23 20:14:05 +00:00
refactor: prepare config code to implement auto dark (reader) mode
Some checks are pending
SonarCloud / Build and analyze (push) Waiting to run
Some checks are pending
SonarCloud / Build and analyze (push) Waiting to run
This commit is contained in:
parent
9315dda365
commit
b6fa66df92
|
@ -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">)";
|
||||
|
|
|
@ -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" );
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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">
|
||||
|
|
Loading…
Reference in a new issue