Skip to content

Commit

Permalink
Make the icon theme configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
evpo committed Mar 30, 2024
1 parent fab9933 commit d6b7496
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 13 deletions.
44 changes: 35 additions & 9 deletions qt_ui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,22 @@ namespace

return true;
}

ThemeAppearance ResolveCurrentThemeAppearance(PersistentPreferences::ThemeAppearanceConfig config, ThemeAppearance autoThemeAppearance)
{
switch(config)
{
case PersistentPreferences::ThemeAppearanceConfig::Auto:
return autoThemeAppearance;
case PersistentPreferences::ThemeAppearanceConfig::Light:
return ThemeAppearance::Light;
case PersistentPreferences::ThemeAppearanceConfig::Dark:
return ThemeAppearance::Dark;
default:
LOG_WARNING << "unknown ThemeAppearanceConfig";
return autoThemeAppearance;
}
}
}

const int MainWindow::maxZoomIn = 75;
Expand All @@ -120,7 +136,8 @@ MainWindow::MainWindow():
loadAdapter(this),
loadHandler(this, loadAdapter, metadata),
saveSuccess(false),
themeAppearance(ThemeAppearance::Light)
currentThemeAppearance(ThemeAppearance::Light),
autoThemeAppearance(ThemeAppearance::Light)
{
setWindowIcon(QIcon(":/images/application_icon.png"));
auto settings = readSettings();
Expand All @@ -146,6 +163,8 @@ MainWindow::MainWindow():
createToolBars();
createStatusBar();

setThemeAppearance(ResolveCurrentThemeAppearance(preferences.themeAppearance, autoThemeAppearance));

if(settings)
{
initSettings(*settings);
Expand Down Expand Up @@ -1217,6 +1236,7 @@ void MainWindow::onUpdatedPreferences()
enc.SetLibcurlPath(preferences.libCurlPath.toStdString());
enc.SetLibcurlParams(preferences.libCurlParameters.toStdString());
textEdit->setFont(preferences.font);
setThemeAppearance(ResolveCurrentThemeAppearance(preferences.themeAppearance, autoThemeAppearance));
QFontMetrics metrics(textEdit->font());

#if QT_VERSION < QT_VERSION_CHECK(5, 11, 0)
Expand Down Expand Up @@ -1791,7 +1811,7 @@ void MainWindow::setWindowsEol(bool flag)
void MainWindow::openFileEncryption()
{
FileEncryptionDialog dlg(this, file_request_service_);
dlg.SetThemeAppearance(themeAppearance);
dlg.SetThemeAppearance(currentThemeAppearance);
dlg.SetDefaultFileParameters(preferences.defaultFileProperties);
dlg.exec();
}
Expand All @@ -1812,14 +1832,9 @@ void MainWindow::showHelp()

}

ThemeAppearance MainWindow::getAutoThemeAppearance() const
void MainWindow::setThemeAppearance(ThemeAppearance value)
{
return themeAppearance;
}

void MainWindow::setAutoThemeAppearance(ThemeAppearance value)
{
themeAppearance = value;
currentThemeAppearance = value;
std::string prefix = value == ThemeAppearance::Light ? ":/images/breeze/light/" : ":/images/breeze/dark/";
auto combine = [&prefix] (const char *name) { return QString((prefix + name).c_str()); };
newAct->setIcon(QIcon(combine("snap-page.svg")));
Expand Down Expand Up @@ -1848,3 +1863,14 @@ void MainWindow::setAutoThemeAppearance(ThemeAppearance value)
openFileEncryptionAct->setIcon(QIcon(combine("file-encryption.svg")));
openPreferencesAct->setIcon(QIcon(combine("configure.svg")));
}

ThemeAppearance MainWindow::getAutoThemeAppearance() const
{
return autoThemeAppearance;
}

void MainWindow::setAutoThemeAppearance(ThemeAppearance value)
{
autoThemeAppearance = value;
setThemeAppearance(ResolveCurrentThemeAppearance(preferences.themeAppearance, value));
}
4 changes: 3 additions & 1 deletion qt_ui/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,8 @@ private slots:

FakeVimWrapper fakeVimWrapper;

ThemeAppearance themeAppearance;
ThemeAppearance currentThemeAppearance;
ThemeAppearance autoThemeAppearance;

void createActions();
void createMenus();
Expand Down Expand Up @@ -232,6 +233,7 @@ private slots:
void ConvertToWindowsEOL(QString &in, QByteArray &out);
QString accessRepositoryPath(const QString &fileName);
std::unique_ptr<QSettings> loadSettings();
void setThemeAppearance(ThemeAppearance value);

};

Expand Down
12 changes: 10 additions & 2 deletions qt_ui/preferences.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ namespace
}

template<class T>
void Assign(T &algo, const QVariant &setting)
void Assign(T &value, const QVariant &setting)
{
algo = static_cast<T>(setting.toInt());
value = static_cast<T>(setting.toInt());
}

template<class T>
Expand Down Expand Up @@ -87,6 +87,11 @@ namespace
return QVariant(static_cast<int>(value));
}

QVariant ToVariant(PersistentPreferences::ThemeAppearanceConfig &value)
{
return QVariant(static_cast<int>(value));
}

QVariant ToVariant(EncryptMsg::HashAlgo &value)
{
return QVariant(static_cast<int>(value));
Expand Down Expand Up @@ -136,6 +141,7 @@ namespace
mapper.Map("windows_eol", preferences.windowsEol);
mapper.Map("key_file_key_length", preferences.kfKeyLength);
mapper.Map("tab_size", preferences.tabSize);
mapper.Map("theme_appearance", preferences.themeAppearance);
}
}

Expand Down Expand Up @@ -164,6 +170,8 @@ void SetDefaultPreferences(PersistentPreferences &preferences)

preferences.font = QFontDatabase::systemFont(QFontDatabase::FixedFont);
preferences.tabSize = kDefaultTabSize;

preferences.themeAppearance = PersistentPreferences::ThemeAppearanceConfig::Auto;
}


Expand Down
8 changes: 8 additions & 0 deletions qt_ui/preferences.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@

struct PersistentPreferences
{
enum class ThemeAppearanceConfig
{
Auto = 0,
Light = 1,
Dark = 2,
};

EncryptPad::PacketMetadata defaultFileProperties;
EncryptPad::PacketMetadata keyFileProperties;
bool saveLastUsedDirectory;
Expand All @@ -20,6 +27,7 @@ struct PersistentPreferences
QString libCurlPath;
QString libCurlParameters;
int kfKeyLength;
ThemeAppearanceConfig themeAppearance;
};

void SetDefaultPreferences(PersistentPreferences &preferences);
Expand Down
13 changes: 13 additions & 0 deletions qt_ui/preferences_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,18 @@ namespace
}
}

void BindControl(QComboBox *cnt, PersistentPreferences::ThemeAppearanceConfig &value, BinderDirection direction)
{
if(direction == BinderDirection::Get)
{
value = static_cast<PersistentPreferences::ThemeAppearanceConfig>(cnt->currentIndex());
}
else if(direction == BinderDirection::Set)
{
cnt->setCurrentIndex(static_cast<int>(value));
}
}

struct ControlBinder
{
BinderDirection direction;
Expand All @@ -106,6 +118,7 @@ namespace
binder.Bind(ui.uiS2KResultsPoolSize, value.s2kResultsPoolSize);
binder.Bind(ui.uiKeyFileLength, value.kfKeyLength);
binder.Bind(ui.uiTabSize, value.tabSize);
binder.Bind(ui.uiIconTheme, value.themeAppearance);
}

void ParseComboBoxData(QComboBox *combo)
Expand Down
2 changes: 1 addition & 1 deletion qt_ui/preferences_dialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@
<item>
<widget class="QLabel" name="label_10">
<property name="text">
<string>Icon theme</string>
<string>Icon theme (restart required):</string>
</property>
<property name="buddy">
<cstring>uiIconTheme</cstring>
Expand Down

0 comments on commit d6b7496

Please sign in to comment.