add experimental darkmode

relate #188
This commit is contained in:
Xiao YiFang 2022-12-11 09:22:54 +08:00
parent fceee66f10
commit d969b63c1e
6 changed files with 64 additions and 5 deletions

View file

@ -219,6 +219,7 @@ Preferences::Preferences():
selectWordBySingleClick( false ),
autoScrollToTargetArticle( true ),
escKeyHidesMainWindow( false ),
darkMode( false ),
alwaysOnTop ( false ),
searchInDock ( false ),
@ -869,6 +870,9 @@ Class load()
if ( !preferences.namedItem( "escKeyHidesMainWindow" ).isNull() )
c.preferences.escKeyHidesMainWindow = ( preferences.namedItem( "escKeyHidesMainWindow" ).toElement().text() == "1" );
if ( !preferences.namedItem( "darkMode" ).isNull() )
c.preferences.darkMode = ( preferences.namedItem( "darkMode" ).toElement().text() == "1" );
if ( !preferences.namedItem( "zoomFactor" ).isNull() )
c.preferences.zoomFactor = preferences.namedItem( "zoomFactor" ).toElement().text().toDouble();
@ -1711,6 +1715,10 @@ void save( Class const & c )
opt.appendChild( dd.createTextNode( c.preferences.escKeyHidesMainWindow ? "1":"0" ) );
preferences.appendChild( opt );
opt = dd.createElement( "darkMode" );
opt.appendChild( dd.createTextNode( c.preferences.darkMode ? "1":"0" ) );
preferences.appendChild( opt );
opt = dd.createElement( "zoomFactor" );
opt.appendChild( dd.createTextNode( QString::number( c.preferences.zoomFactor ) ) );
preferences.appendChild( opt );

View file

@ -307,6 +307,7 @@ struct Preferences
bool selectWordBySingleClick;
bool autoScrollToTargetArticle;
bool escKeyHidesMainWindow;
bool darkMode;
bool alwaysOnTop;
/// An old UI mode when tranlateLine and wordList

View file

@ -834,7 +834,7 @@ MainWindow::MainWindow( Config::Class & cfg_ ):
translateLine->setFocus();
applyQtStyleSheet( cfg.preferences.displayStyle, cfg.preferences.addonStyle );
applyQtStyleSheet( cfg.preferences.displayStyle, cfg.preferences.addonStyle, cfg.preferences.darkMode );
makeScanPopup();
@ -1175,8 +1175,45 @@ QPrinter & MainWindow::getPrinter()
return *printer;
}
void MainWindow::applyQtStyleSheet( QString const & displayStyle, QString const & addonStyle )
void MainWindow::applyQtStyleSheet( QString const & displayStyle, QString const & addonStyle, bool const & darkMode )
{
if( darkMode )
{
//https://forum.qt.io/topic/101391/windows-10-dark-theme
#ifdef Q_OS_WIN32
qApp->setStyle( QStyleFactory::create( "Fusion" ) );
#endif
QPalette darkPalette;
QColor darkColor = QColor( 45, 45, 45 );
QColor disabledColor = QColor( 127, 127, 127 );
darkPalette.setColor( QPalette::Window, darkColor );
darkPalette.setColor( QPalette::WindowText, Qt::white );
darkPalette.setColor( QPalette::Base, QColor( 18, 18, 18 ) );
darkPalette.setColor( QPalette::AlternateBase, darkColor );
darkPalette.setColor( QPalette::ToolTipBase, Qt::white );
darkPalette.setColor( QPalette::ToolTipText, Qt::white );
darkPalette.setColor( QPalette::Text, Qt::white );
darkPalette.setColor( QPalette::Disabled, QPalette::Text, disabledColor );
darkPalette.setColor( QPalette::Button, darkColor );
darkPalette.setColor( QPalette::ButtonText, Qt::white );
darkPalette.setColor( QPalette::Disabled, QPalette::ButtonText, disabledColor );
darkPalette.setColor( QPalette::BrightText, Qt::red );
darkPalette.setColor( QPalette::Link, QColor( 42, 130, 218 ) );
darkPalette.setColor( QPalette::Highlight, QColor( 42, 130, 218 ) );
darkPalette.setColor( QPalette::HighlightedText, Qt::black );
darkPalette.setColor( QPalette::Disabled, QPalette::HighlightedText, disabledColor );
qApp->setPalette( darkPalette );
}
else
{
#ifdef Q_OS_WIN32
qApp->setStyle( QStyleFactory::create( "Windows" ) );
#endif
qApp->setPalette( QPalette() );
}
QFile builtInCssFile( ":/qt-style.css" );
builtInCssFile.open( QFile::ReadOnly );
QByteArray css = builtInCssFile.readAll();
@ -1210,6 +1247,10 @@ void MainWindow::applyQtStyleSheet( QString const & displayStyle, QString const
css += addonCss.readAll();
}
if(darkMode){
css += "QToolTip { color: #ffffff; background-color: #2a82da; border: 1px solid white; }";
}
setStyleSheet( css );
}
@ -2237,9 +2278,9 @@ void MainWindow::editPreferences()
bool needReload = false;
// See if we need to reapply stylesheets
if ( cfg.preferences.displayStyle != p.displayStyle || cfg.preferences.addonStyle != p.addonStyle )
if ( cfg.preferences.displayStyle != p.displayStyle || cfg.preferences.addonStyle != p.addonStyle || cfg.preferences.darkMode != p.darkMode)
{
applyQtStyleSheet( p.displayStyle, p.addonStyle );
applyQtStyleSheet( p.displayStyle, p.addonStyle, p.darkMode );
articleMaker.setDisplayStyle( p.displayStyle, p.addonStyle );
needReload = true;
}

View file

@ -184,7 +184,7 @@ private:
ResourceSchemeHandler * resourceSchemeHandler;
/// Applies the qt's stylesheet, given the style's name.
void applyQtStyleSheet( QString const & displayStyle, QString const & addonStyle );
void applyQtStyleSheet( QString const & displayStyle, QString const & addonStyle, bool const & darkMode );
/// Creates, destroys or otherwise updates tray icon, according to the
/// current configuration and situation.

View file

@ -181,6 +181,7 @@ 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.enableMainWindowHotkey->setChecked( p.enableMainWindowHotkey );
ui.mainWindowHotkey->setHotKey( p.mainWindowHotkey );
@ -400,6 +401,7 @@ Config::Preferences Preferences::getPreferences()
p.autoScrollToTargetArticle = ui.autoScrollToTargetArticle->isChecked();
p.escKeyHidesMainWindow = ui.escKeyHidesMainWindow->isChecked();
p.darkMode = ui.darkMode->isChecked();
p.enableMainWindowHotkey = ui.enableMainWindowHotkey->isChecked();
p.mainWindowHotkey = ui.mainWindowHotkey->getHotKey();
p.enableClipboardHotkey = ui.enableClipboardHotkey->isChecked();

View file

@ -412,6 +412,13 @@ be the last ones.</string>
</item>
</layout>
</item>
<item row="12" column="0">
<widget class="QCheckBox" name="darkMode">
<property name="text">
<string>dark mode</string>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QWidget" name="tab_4">