Skip to content

Commit

Permalink
Dark theme for Qt application (#7318)
Browse files Browse the repository at this point in the history
* Dark theme for Qt application

* Fixed saved application geometry
  • Loading branch information
Reksotiv authored Jan 28, 2023
1 parent 090559d commit 0c6e3b7
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 0 deletions.
2 changes: 2 additions & 0 deletions engine/report/report_html_sim.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1239,6 +1239,8 @@ void print_html_head( report::sc_html_stream& os, const sim_t& sim )
os << "<title>Simulationcraft Results</title>\n";
os << "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n";

os << "<meta name=\"color-scheme\" content=\"light dark\">\n";

// Default target for links. Lets links to wowhead/armory escape iframes (wowhead does not display in iframes at all)
// Should have no effect on GUI reports or directly viewing the HTML report in a browser
os << "<base target=\"_top\">\n";
Expand Down
1 change: 1 addition & 0 deletions qt/MainWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ void SC_MainWindow::loadHistory()
{
savedApplicationGeometry.moveTopLeft( pos.toPoint() );
}
setGeometry(savedApplicationGeometry);
QVariant maximized = settings.value( "gui/maximized" );
if ( maximized.isValid() )
{
Expand Down
1 change: 1 addition & 0 deletions qt/Welcome.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<title>Welcome to SimulationCraft</title>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<meta name="color-scheme" content="light dark">

<style type="text/css">
* { border: none; margin: 0; padding: 0; }
Expand Down
8 changes: 8 additions & 0 deletions qt/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ int main( int argc, char* argv[] )
}
a.installTranslator( &myappTranslator );

// Chromium theme color
QString color;
color = settings.value( "options/theme_color", "Light" ).toString();
if ( color == "Dark" )
{
qputenv( "QTWEBENGINE_CHROMIUM_FLAGS", "--force-dark-mode --enable-features=WebUIDarkMode" );
}

QString iconlocation =
QStandardPaths::locate( QStandardPaths::AppDataLocation, QString( "icon" ), QStandardPaths::LocateDirectory );
QDir::addSearchPath( "icon", iconlocation );
Expand Down
86 changes: 86 additions & 0 deletions qt/sc_OptionsTab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,30 @@

#include <QtCore/QDateTime>

#ifdef Q_OS_WIN
#include <dwmapi.h>
#pragma comment( lib, "Dwmapi.lib" ) // fixes error LNK2019: unresolved external symbol __imp__DwmExtendFrameIntoClientArea

enum : WORD
{
DwmwaUseImmersiveDarkMode = 20,
DwmwaUseImmersiveDarkModeBefore20h1 = 19
};

bool setDarkBorderToWindow( HWND hwnd, bool dark )
{
const BOOL darkBorder = dark ? TRUE : FALSE;
const bool ok =
SUCCEEDED( DwmSetWindowAttribute( hwnd, DwmwaUseImmersiveDarkMode, &darkBorder, sizeof( darkBorder ) ) ) ||
SUCCEEDED( DwmSetWindowAttribute( hwnd, DwmwaUseImmersiveDarkModeBefore20h1, &darkBorder, sizeof( darkBorder ) ) );
if ( !ok )
{
qDebug() << QString( "%1: Unable to set %2 window border." ).arg( __FUNCTION__, dark ? "dark" : "light" );
}
return ok;
}
#endif

namespace
{ // unnamed namespace

Expand Down Expand Up @@ -396,6 +420,11 @@ void SC_OptionsTab::createGlobalsTab()
globalsLayout_right->addRow( tr( "Statistics Level" ),
choice.statistics_level = createChoice( 4, "0", "1", "2", "3" ) );
globalsLayout_right->addRow( tr( "Deterministic RNG" ), choice.deterministic_rng = createChoice( 2, "Yes", "No" ) );

globalsLayout_right->addRow( tr( "Theme Color" ), choice.theme_color = createChoice( 2, "Light", "Dark" ) );
connect( choice.theme_color, SIGNAL( currentTextChanged( const QString& ) ), this,
SLOT( _themeColorChanged( const QString& ) ) );

globalsLayout_right->addRow(
tr( "Auto-Save Reports" ),
choice.auto_save = createChoice( 3, "No", "Use current date/time", "Ask for filename on each simulation" ) );
Expand Down Expand Up @@ -835,6 +864,7 @@ void SC_OptionsTab::decodeOptions()
load_setting( settings, "statistics_level", choice.statistics_level, "1" );
load_setting( settings, "deterministic_rng", choice.deterministic_rng, "No" );
load_setting( settings, "challenge_mode", choice.challenge_mode );
load_setting( settings, "theme_color", choice.theme_color, "Light" );

load_setting( settings, "center_scale_delta", choice.center_scale_delta, "No" );
load_setting( settings, "scale_over", choice.scale_over );
Expand Down Expand Up @@ -931,6 +961,7 @@ void SC_OptionsTab::encodeOptions()
settings.setValue( "center_scale_delta", choice.center_scale_delta->currentText() );
settings.setValue( "scale_over", choice.scale_over->currentText() );
settings.setValue( "challenge_mode", choice.challenge_mode->currentText() );
settings.setValue( "theme_color", choice.theme_color->currentText() );

settings.setValue( "plot_points", choice.plots_points->currentText() );
settings.setValue( "plot_step", choice.plots_step->currentText() );
Expand Down Expand Up @@ -1126,6 +1157,8 @@ void SC_OptionsTab::createToolTips()
choice.reforgeplot_step->setToolTip(
tr( "The stat difference between two points.\n"
"It's NOT the number of steps: a lower value will generate more points!" ) );

choice.theme_color->setToolTip( tr( "Application theme color" ) );
}

QString SC_OptionsTab::get_globalSettings()
Expand Down Expand Up @@ -1604,3 +1637,56 @@ void SC_OptionsTab::_savefilelocation()

auto_save_location = f.selectedFiles().at( 0 );
}

void SC_OptionsTab::_themeColorChanged( const QString& color )
{
// if ( qApp->style()->name() != "fusion" )
// {
// qDebug()<<"Current style is not Fusion";
// return;
// }

if ( color == "Light" )
{
QPalette pal;
QToolTip::setPalette( pal );
qApp->setPalette( pal );
#ifdef Q_OS_WIN
setDarkBorderToWindow( (HWND)window()->winId(), false );
#endif
}
else if ( color == "Dark" )
{
QPalette pal;
pal.setColor( QPalette::Window, QColor( 52, 54, 58 ) );
pal.setColor( QPalette::Button, QColor( 52, 54, 58 ) );
pal.setColor( QPalette::Disabled, QPalette::Button, QColor( 40, 41, 45 ) );
pal.setColor( QPalette::Base, QColor( 47, 48, 52 ) );
pal.setColor( QPalette::Disabled, QPalette::Base, QColor( 35, 36, 40 ) );
pal.setColor( QPalette::AlternateBase, QColor( 66, 66, 66 ) );
pal.setColor( QPalette::ToolTipBase, QColor( 75, 76, 77 ) );

pal.setColor( QPalette::Dark, QColor( 66, 66, 66 ) );
pal.setColor( QPalette::Light, QColor( 66, 66, 66 ) );
pal.setColor( QPalette::Shadow, QColor( 20, 20, 20 ) );

pal.setColor( QPalette::Text, QColor( 230, 231, 232 ) );
pal.setColor( QPalette::Disabled, QPalette::Text, QColor( 127, 127, 127 ) );
pal.setColor( QPalette::WindowText, QColor( 230, 231, 232 ) );
pal.setColor( QPalette::Disabled, QPalette::WindowText, QColor( 127, 127, 127 ) );
pal.setColor( QPalette::ToolTipText, QColor( 230, 231, 232 ) );
pal.setColor( QPalette::ButtonText, QColor( 230, 231, 232 ) );
pal.setColor( QPalette::Disabled, QPalette::ButtonText, QColor( 127, 127, 127 ) );
pal.setColor( QPalette::BrightText, Qt::red );
pal.setColor( QPalette::Link, QColor( 20, 100, 170 ) );
pal.setColor( QPalette::Highlight, QColor( 20, 100, 170 ) );
pal.setColor( QPalette::Disabled, QPalette::Highlight, QColor( 80, 80, 80 ) );
pal.setColor( QPalette::HighlightedText, QColor( 230, 231, 232 ) );
pal.setColor( QPalette::Disabled, QPalette::HighlightedText, QColor( 127, 127, 127 ) );
QToolTip::setPalette( pal );
qApp->setPalette( pal );
#ifdef Q_OS_WIN
setDarkBorderToWindow( (HWND)window()->winId(), true );
#endif
}
}
2 changes: 2 additions & 0 deletions qt/sc_OptionsTab.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ class SC_OptionsTab : public QTabWidget
QComboBox* deterministic_rng;
QComboBox* center_scale_delta;
QComboBox* challenge_mode;
QComboBox* theme_color;
// scaling
QComboBox* scale_over;
QComboBox* plots_points;
Expand All @@ -105,6 +106,7 @@ public slots:
void _resetallSettings();
void _savefilelocation();
void _armoryRegionChanged( const QString& );
void _themeColorChanged( const QString& );

protected:
SC_MainWindow* mainWindow;
Expand Down

0 comments on commit 0c6e3b7

Please sign in to comment.