GH-903 simple theme switching and dark theme

This commit is contained in:
Petr Mrázek 2016-10-21 09:07:26 +02:00
parent f07496ac6d
commit 872cfe036d
11 changed files with 237 additions and 4 deletions

View File

@ -106,6 +106,13 @@ SET(MULTIMC_SOURCES
InstanceWindow.h
InstanceWindow.cpp
# GUI - themes
themes/DarkTheme.cpp
themes/DarkTheme.h
themes/ITheme.h
themes/SystemTheme.cpp
themes/SystemTheme.h
# GUI - settings-specific wrappers for paged dialog
SettingsUI.h

View File

@ -9,6 +9,10 @@
#include "pages/global/AccountListPage.h"
#include "pages/global/PasteEEPage.h"
#include "themes/ITheme.h"
#include "themes/SystemTheme.h"
#include "themes/DarkTheme.h"
#include <iostream>
#include <QDir>
#include <QFileInfo>
@ -241,6 +245,9 @@ MultiMC::MultiMC(int &argc, char **argv, bool test_mode) : QApplication(argc, ar
// load icons
initIcons();
// load themes
initThemes();
// and instances
auto InstDirSetting = m_settings->getSetting("InstanceDir");
// instance path: check for problems with '!' in instance path and warn the user in the log
@ -442,7 +449,10 @@ void MultiMC::initGlobalSettings(bool test_mode)
// Updates
m_settings->registerSetting("UpdateChannel", BuildConfig.VERSION_CHANNEL);
m_settings->registerSetting("AutoUpdate", true);
// Theming
m_settings->registerSetting("IconTheme", QString("multimc"));
m_settings->registerSetting("ApplicationTheme", QString("system"));
// Notifications
m_settings->registerSetting("ShownNotifications", QString());
@ -943,6 +953,45 @@ FAILED:
QMessageBox::critical(nullptr, tr("Update failed!"), msg);
}
std::vector<ITheme *> MultiMC::getValidApplicationThemes()
{
std::vector<ITheme *> ret;
auto iter = m_themes.cbegin();
while (iter != m_themes.cend())
{
ret.push_back((*iter).second.get());
iter++;
}
return ret;
}
void MultiMC::initThemes()
{
auto insertTheme = [this](ITheme * theme)
{
m_themes.insert(std::make_pair(theme->id(), std::unique_ptr<ITheme>(theme)));
};
insertTheme(new SystemTheme());
insertTheme(new DarkTheme());
}
void MultiMC::setApplicationTheme(const QString& name)
{
auto systemPalette = qApp->palette();
auto themeIter = m_themes.find(name);
if(themeIter != m_themes.end())
{
auto & theme = (*themeIter).second;
setPalette(theme->colorScheme());
setStyleSheet(theme->appStyleSheet());
//setStyle(QStyleFactory::create("Fusion"));
}
else
{
qWarning() << "Tried to set invalid theme:" << name;
}
}
void MultiMC::setIconTheme(const QString& name)
{
XdgIcon::setThemeName(name);

View File

@ -25,6 +25,7 @@ class UpdateChecker;
class BaseProfilerFactory;
class BaseDetachedToolFactory;
class TranslationDownloader;
class ITheme;
#if defined(MMC)
#undef MMC
@ -69,6 +70,9 @@ public:
void setIconTheme(const QString& name);
std::vector<ITheme *> getValidApplicationThemes();
void setApplicationTheme(const QString& name);
// DownloadUpdateTask
std::shared_ptr<UpdateChecker> updateChecker()
{
@ -145,6 +149,7 @@ private slots:
private:
void initLogger();
void initIcons();
void initThemes();
void initGlobalSettings(bool test_mode);
void initTranslations();
void initSSL();
@ -169,6 +174,7 @@ private:
std::shared_ptr<JavaInstallList> m_javalist;
std::shared_ptr<TranslationDownloader> m_translationChecker;
std::shared_ptr<GenericPageProvider> m_globalSettingsProvider;
std::map<QString, std::unique_ptr<ITheme>> m_themes;
QMap<QString, std::shared_ptr<BaseProfilerFactory>> m_profilers;
QMap<QString, std::shared_ptr<BaseDetachedToolFactory>> m_tools;

View File

@ -28,6 +28,8 @@ int launchInstance(MultiMC &app, InstancePtr inst)
int main_gui(MultiMC &app)
{
app.setIconTheme(MMC->settings()->get("IconTheme").toString());
app.setApplicationTheme(MMC->settings()->get("ApplicationTheme").toString());
// show main window
auto inst = app.instances()->getInstanceById(app.launchId);
if(inst)

View File

@ -28,6 +28,7 @@
#include <FileSystem.h>
#include "MultiMC.h"
#include "BuildConfig.h"
#include "themes/ITheme.h"
// FIXME: possibly move elsewhere
enum InstSortMode
@ -305,6 +306,14 @@ void MultiMCPage::applySettings()
MMC->setIconTheme(s->get("IconTheme").toString());
}
auto originalAppTheme = s->get("ApplicationTheme").toString();
auto newAppTheme = ui->themeComboBoxColors->currentData().toString();
if(originalAppTheme != newAppTheme)
{
s->set("ApplicationTheme", newAppTheme);
MMC->setApplicationTheme(newAppTheme);
}
// Console settings
s->set("ShowConsole", ui->showConsoleCheck->isChecked());
s->set("AutoCloseConsole", ui->autoCloseConsoleCheck->isChecked());
@ -386,6 +395,21 @@ void MultiMCPage::loadSettings()
ui->themeComboBox->setCurrentIndex(0);
}
{
auto currentTheme = s->get("ApplicationTheme").toString();
auto themes = MMC->getValidApplicationThemes();
int idx = 0;
for(auto &theme: themes)
{
ui->themeComboBoxColors->addItem(theme->name(), theme->id());
if(currentTheme == theme->id())
{
ui->themeComboBoxColors->setCurrentIndex(idx);
}
idx++;
}
}
// Console settings
ui->showConsoleCheck->setChecked(s->get("ShowConsole").toBool());
ui->autoCloseConsoleCheck->setChecked(s->get("AutoCloseConsole").toBool());

View File

@ -329,13 +329,23 @@
<item>
<widget class="QGroupBox" name="themeBox">
<property name="title">
<string>Icon Theme</string>
<string>Theme</string>
</property>
<layout class="QHBoxLayout" name="themeBoxLayout">
<item>
<layout class="QFormLayout" name="formLayout">
<item row="0" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
<string>&amp;Icons</string>
</property>
<property name="buddy">
<cstring>themeComboBox</cstring>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QComboBox" name="themeComboBox">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Fixed">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
@ -380,6 +390,29 @@
</item>
</widget>
</item>
<item row="1" column="1">
<widget class="QComboBox" name="themeComboBoxColors">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="focusPolicy">
<enum>Qt::StrongFocus</enum>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_4">
<property name="text">
<string>Colors</string>
</property>
<property name="buddy">
<cstring>themeComboBoxColors</cstring>
</property>
</widget>
</item>
</layout>
</widget>
</item>
@ -552,6 +585,7 @@
<tabstop>sortByNameBtn</tabstop>
<tabstop>languageBox</tabstop>
<tabstop>themeComboBox</tabstop>
<tabstop>themeComboBoxColors</tabstop>
<tabstop>showConsoleCheck</tabstop>
<tabstop>autoCloseConsoleCheck</tabstop>
<tabstop>lineLimitSpinBox</tabstop>

View File

@ -0,0 +1,37 @@
#include "DarkTheme.h"
QString DarkTheme::id()
{
return "dark";
}
QString DarkTheme::name()
{
return QObject::tr("Dark");
}
QPalette DarkTheme::colorScheme()
{
QPalette darkPalette;
darkPalette.setColor(QPalette::Window, QColor(49,54,59));
darkPalette.setColor(QPalette::WindowText, Qt::white);
darkPalette.setColor(QPalette::Base, QColor(35,38,41));
darkPalette.setColor(QPalette::AlternateBase, QColor(49,54,59));
darkPalette.setColor(QPalette::ToolTipBase, Qt::white);
darkPalette.setColor(QPalette::ToolTipText, Qt::white);
darkPalette.setColor(QPalette::Text, Qt::white);
darkPalette.setColor(QPalette::Button, QColor(49,54,59));
darkPalette.setColor(QPalette::ButtonText, Qt::white);
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);
return darkPalette;
}
QString DarkTheme::appStyleSheet()
{
return "QToolTip { color: #ffffff; background-color: #2a82da; border: 1px solid white; }";
}

View File

@ -0,0 +1,14 @@
#pragma once
#include "ITheme.h"
class DarkTheme: public ITheme
{
public:
virtual ~DarkTheme() {}
QString id() override;
QString name() override;
QString appStyleSheet() override;
QPalette colorScheme() override;
};

View File

@ -0,0 +1,13 @@
#pragma once
#include <QString>
#include <QPalette>
class ITheme
{
public:
virtual ~ITheme() {}
virtual QString id() = 0;
virtual QString name() = 0;
virtual QString appStyleSheet() = 0;
virtual QPalette colorScheme() = 0;
};

View File

@ -0,0 +1,28 @@
#include "SystemTheme.h"
#include <QApplication>
#include <QStyle>
SystemTheme::SystemTheme()
{
systemPalette = QApplication::style()->standardPalette();
}
QString SystemTheme::id()
{
return "system";
}
QString SystemTheme::name()
{
return QObject::tr("System");
}
QPalette SystemTheme::colorScheme()
{
return systemPalette;
}
QString SystemTheme::appStyleSheet()
{
return QString();
}

View File

@ -0,0 +1,19 @@
#pragma once
#include "ITheme.h"
class SystemTheme: public ITheme
{
public:
SystemTheme();
virtual ~SystemTheme() {}
QString id() override;
QString name() override;
QString appStyleSheet() override;
QPalette colorScheme() override;
private:
QPalette systemPalette;
QString systemTheme;
};