Skip to content

Commit 6cb061a

Browse files
committed
Add GUI light/dark theme toggle button
This adds a button next to the logo in the sidebar that switches the active GUI theme between light and dark mode.
1 parent 1e6aa73 commit 6cb061a

File tree

12 files changed

+123
-14
lines changed

12 files changed

+123
-14
lines changed

src/Makefile.qt.include

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,8 @@ RES_ICONS = \
306306
qt/res/icons/connect2.svg \
307307
qt/res/icons/connect3.svg \
308308
qt/res/icons/connect4.svg \
309+
qt/res/icons/dark_mode.svg \
310+
qt/res/icons/dark_mode_active.svg \
309311
qt/res/icons/debugwindow.png \
310312
qt/res/icons/edit.png \
311313
qt/res/icons/editcopy.png \
@@ -316,6 +318,8 @@ RES_ICONS = \
316318
qt/res/icons/gridcoin.ico \
317319
qt/res/icons/gridcoin_testnet.ico \
318320
qt/res/icons/key.png \
321+
qt/res/icons/light_mode.svg \
322+
qt/res/icons/light_mode_active.svg \
319323
qt/res/icons/menu.svg \
320324
qt/res/icons/menu_active.svg \
321325
qt/res/icons/message.svg \

src/qt/bitcoin.qrc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,11 @@
171171
<file alias="message">res/icons/message.svg</file>
172172
<file alias="menu">res/icons/menu.svg</file>
173173
<file alias="menu_active">res/icons/menu_active.svg</file>
174+
<file alias="dark_mode">res/icons/dark_mode.svg</file>
175+
<file alias="dark_mode_active">res/icons/dark_mode_active.svg</file>
176+
<file alias="light_mode">res/icons/light_mode.svg</file>
177+
<file alias="light_mode_active">res/icons/light_mode_active.svg</file>
178+
174179
</qresource>
175180
<qresource prefix="/images">
176181
<file alias="splash">res/images/splash3.png</file>

src/qt/bitcoingui.cpp

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -588,22 +588,47 @@ void BitcoinGUI::createToolBars()
588588
{
589589
ClickLabel *logoLabel = new ClickLabel();
590590
logoLabel->setObjectName("toolbarLogoLabel");
591+
QSizePolicy logoLabelSizePolicy = logoLabel->sizePolicy();
592+
logoLabelSizePolicy.setHorizontalStretch(2);
593+
logoLabel->setSizePolicy(logoLabelSizePolicy);
591594
connect(logoLabel, SIGNAL(clicked()), this, SLOT(websiteClicked()));
592595

596+
QHBoxLayout *logoWrapperLayout = new QHBoxLayout();
597+
logoWrapperLayout->setContentsMargins(2, 0, 2, 0);
598+
logoWrapperLayout->setSpacing(0);
599+
593600
QWidget *logoWrapper = new QWidget();
594601
logoWrapper->setObjectName("toolbarLogoWrapper");
595-
logoWrapper->setLayout(new QVBoxLayout());
596-
logoWrapper->layout()->addWidget(logoLabel);
602+
logoWrapper->setLayout(logoWrapperLayout);
597603

598604
#ifndef Q_OS_MAC
599605
// Windows and Linux: collapse the main application's menu bar into a menu
600606
// button. On macOS, we'll continue to use the system's separate menu bar.
601-
QPushButton *menuButton = new QPushButton(logoWrapper);
607+
QPushButton *menuButton = new QPushButton();
602608
menuButton->setObjectName("toolbarMenuButton");
603-
menuButton->resize(GRC::ScaleSize(this, 24));
609+
menuButton->setToolTip(tr("Open menu."));
604610
menuButton->setMenu(appMenuBar);
611+
QSizePolicy menuButtonSizePolicy = menuButton->sizePolicy();
612+
menuButtonSizePolicy.setHorizontalStretch(1);
613+
menuButton->setSizePolicy(menuButtonSizePolicy);
614+
logoWrapperLayout->addWidget(menuButton);
615+
logoWrapperLayout->setAlignment(menuButton, Qt::AlignHCenter | Qt::AlignVCenter);
616+
#else
617+
logoWrapperLayout->addStretch(1);
605618
#endif
606619

620+
logoWrapperLayout->addWidget(logoLabel);
621+
622+
QPushButton *themeToggleButton = new QPushButton();
623+
themeToggleButton->setObjectName("themeToggleButton");
624+
themeToggleButton->setToolTip(tr("Toggle light/dark mode."));
625+
QSizePolicy themeToggleButtonSizePolicy = themeToggleButton->sizePolicy();
626+
themeToggleButtonSizePolicy.setHorizontalStretch(1);
627+
themeToggleButton->setSizePolicy(themeToggleButtonSizePolicy);
628+
connect(themeToggleButton, SIGNAL(clicked()), this, SLOT(themeToggled()));
629+
logoWrapperLayout->addWidget(themeToggleButton);
630+
logoWrapperLayout->setAlignment(themeToggleButton, Qt::AlignHCenter | Qt::AlignVCenter);
631+
607632
QWidget *boincLabelSpacer = new QWidget();
608633
boincLabelSpacer->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
609634

@@ -863,6 +888,11 @@ void BitcoinGUI::optionsClicked()
863888
dlg.exec();
864889
}
865890

891+
void BitcoinGUI::themeToggled()
892+
{
893+
clientModel->getOptionsModel()->setCurrentStyle(sSheet == "light" ? "dark" : "light");
894+
}
895+
866896
void BitcoinGUI::openConfigClicked()
867897
{
868898
boost::filesystem::path pathConfig = GetConfigFile();

src/qt/bitcoingui.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,8 @@ private slots:
217217

218218
/** Show configuration dialog */
219219
void optionsClicked();
220+
/** Switch the active light/dark theme */
221+
void themeToggled();
220222
/** Show researcher/beacon configuration dialog */
221223
void researcherClicked();
222224
/** Show about dialog */

src/qt/optionsmodel.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,11 @@ QString OptionsModel::getCurrentStyle()
354354
return walletStylesheet;
355355
}
356356

357+
void OptionsModel::setCurrentStyle(QString theme)
358+
{
359+
setData(QAbstractItemModel::createIndex(WalletStylesheet, 0), theme, Qt::EditRole);
360+
}
361+
357362
QString OptionsModel::getDataDir()
358363
{
359364
return dataDir;

src/qt/optionsmodel.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ class OptionsModel : public QAbstractListModel
6565
QString getCurrentStyle();
6666
QString getDataDir();
6767

68+
/* Explicit setters */
69+
void setCurrentStyle(QString theme);
70+
6871
private:
6972
int nDisplayUnit;
7073
bool fMinimizeToTray;

src/qt/res/icons/dark_mode.svg

Lines changed: 4 additions & 0 deletions
Loading
Lines changed: 4 additions & 0 deletions
Loading

src/qt/res/icons/light_mode.svg

Lines changed: 4 additions & 0 deletions
Loading
Lines changed: 4 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)