Skip to content
Extraits de code Groupes Projets
Vérifiée Valider 4027fe7e rédigé par Kubat's avatar Kubat
Parcourir les fichiers

UI: The theme now supports the "VivyTheme" and "SystemTheme" modes

We do that to move all the logic to switch between palettes and qss
files into the same class.
parent 96fb877e
Aucune branche associée trouvée
Aucune étiquette associée trouvée
2 requêtes de fusion!22Theme system,!21Add clean logs support + dependent MR
...@@ -21,6 +21,11 @@ ...@@ -21,6 +21,11 @@
#define VIVY_DEPRECATED __attribute__((__deprecated__)) #define VIVY_DEPRECATED __attribute__((__deprecated__))
#define VIVY_CONSTRUCTOR __attribute__((constructor)) #define VIVY_CONSTRUCTOR __attribute__((constructor))
#define VIVY_DESTRUCTOR __attribute__((destructor)) #define VIVY_DESTRUCTOR __attribute__((destructor))
#define VIVY_ASSERT(cond) \
{ \
if (!cond) \
VIVY_ASSERT_STRING(#cond); \
}
// Use chrono instead of std::chrono... // Use chrono instead of std::chrono...
namespace chrono = std::chrono; namespace chrono = std::chrono;
......
#include "Theme.hh" #include "Theme.hh"
#include "../../VivyApplication.hh"
#include "../../Lib/HostOsInfo.hh" #include "../../Lib/HostOsInfo.hh"
#ifdef Q_OS_MACOS #if VIVY_MACOS
#import "ThemeMac.hh" #import "ThemeMac.hh"
#endif #endif
...@@ -18,16 +19,27 @@ Theme::ThemePrivate::ThemePrivate() noexcept ...@@ -18,16 +19,27 @@ Theme::ThemePrivate::ThemePrivate() noexcept
void void
Theme::applyToApplication() const noexcept Theme::applyToApplication() const noexcept
{ {
#ifdef Q_OS_MACOS if (d->classType == Type::QssFile) {
QFile stylesheet(d->fileName);
if (!stylesheet.exists()) {
qFatal("Missing stylesheet");
} else {
stylesheet.open(QFile::ReadOnly | QFile::Text);
QTextStream stylesheetStream(&stylesheet);
vivyApp->setStyleSheet(stylesheetStream.readAll());
}
}
else {
#if VIVY_MACOS
// Match the native UI theme and palette with the creator // Match the native UI theme and palette with the creator
// theme by forcing light aqua for light creator themes. // theme by forcing light aqua for light creator themes.
if (!flag(Theme::DarkUserInterface)) if (!flag(Theme::DarkUserInterface))
Internal::forceMacOSLightAquaApperance(); Utils::forceMacOSLightAquaApperance();
#endif #endif
// if (flag(Theme::ApplyThemePaletteGlobally))
QApplication::setPalette(palette()); QApplication::setPalette(palette());
} }
}
Theme::Theme(const QString &id, QObject *parent) noexcept Theme::Theme(const QString &id, QObject *parent) noexcept
: QObject(parent) : QObject(parent)
...@@ -115,10 +127,36 @@ Theme::setDisplayName(const QString &name) noexcept ...@@ -115,10 +127,36 @@ Theme::setDisplayName(const QString &name) noexcept
d->displayName = name; d->displayName = name;
} }
Theme *
Theme::fromQssFile(const QString &id, const QString &file) noexcept
{
Theme *ret = new Theme(id);
ret->d->fileName = file;
ret->d->classType = Type::QssFile;
return ret;
}
Theme *
Theme::fromSettings(const QString &id, QSettings *const settings) noexcept
{
Theme *ret = new Theme(id);
ret->readSettings(settings);
return ret;
}
Theme *
Theme::fromVivyTheme(const QString &id, const VivyTheme which) noexcept
{
return fromQssFile(id, which == VivyTheme::Dark
? QStringLiteral(":qdarkstyle/dark/style.qss")
: QStringLiteral(":qdarkstyle/light/style.qss"));
}
void void
Theme::readSettings(QSettings *const settings) noexcept Theme::readSettings(QSettings *const settings) noexcept
{ {
d->fileName = settings->fileName(); d->fileName = settings->fileName();
d->classType = Type::QtCreator;
const QMetaObject &m = *metaObject(); const QMetaObject &m = *metaObject();
{ {
...@@ -169,10 +207,9 @@ Theme::readSettings(QSettings *const settings) noexcept ...@@ -169,10 +207,9 @@ Theme::readSettings(QSettings *const settings) noexcept
int size = settings->beginReadArray(key); int size = settings->beginReadArray(key);
for (int j = 0; j < size; ++j) { for (int j = 0; j < size; ++j) {
settings->setArrayIndex(j); settings->setArrayIndex(j);
TODO(Assert things when needed !); VIVY_ASSERT(settings->contains(QLatin1String("pos")));
settings->contains(QLatin1String("pos")); // TODO: Assert it VIVY_ASSERT(settings->contains(QLatin1String("color")));
const double pos = settings->value(QLatin1String("pos")).toDouble(); const double pos = settings->value(QLatin1String("pos")).toDouble();
settings->contains(QLatin1String("color")); // TODO: Assert it
const QColor c('#' + settings->value(QLatin1String("color")).toString()); const QColor c('#' + settings->value(QLatin1String("color")).toString());
stops.append(qMakePair(pos, c)); stops.append(qMakePair(pos, c));
} }
...@@ -186,8 +223,7 @@ Theme::readSettings(QSettings *const settings) noexcept ...@@ -186,8 +223,7 @@ Theme::readSettings(QSettings *const settings) noexcept
QMetaEnum e = m.enumerator(m.indexOfEnumerator("Flag")); QMetaEnum e = m.enumerator(m.indexOfEnumerator("Flag"));
for (int i = 0, total = e.keyCount(); i < total; ++i) { for (int i = 0, total = e.keyCount(); i < total; ++i) {
const QString key = QLatin1String(e.key(i)); const QString key = QLatin1String(e.key(i));
TODO(Assert it); VIVY_ASSERT(settings->contains(key));
settings->contains(key); // TODO: Assert it!
d->flags[i] = settings->value(key).toBool(); d->flags[i] = settings->value(key).toBool();
} }
settings->endGroup(); settings->endGroup();
...@@ -238,8 +274,6 @@ QPalette ...@@ -238,8 +274,6 @@ QPalette
Theme::palette() const noexcept Theme::palette() const noexcept
{ {
QPalette pal = initialPalette(); QPalette pal = initialPalette();
// if (!flag(DerivePaletteFromTheme))
// return pal;
const static struct { const static struct {
Color themeColor; Color themeColor;
......
...@@ -10,9 +10,16 @@ class Theme; ...@@ -10,9 +10,16 @@ class Theme;
class Theme final : public QObject { class Theme final : public QObject {
Q_OBJECT Q_OBJECT
public:
enum class Type { QtCreator, System, QssFile };
enum class VivyTheme { Dark, Light };
private:
struct ThemePrivate final { struct ThemePrivate final {
ThemePrivate() noexcept; ThemePrivate() noexcept;
Theme::Type classType{ Theme::Type::System };
QString id; QString id;
QString fileName; QString fileName;
QString displayName; QString displayName;
...@@ -25,10 +32,17 @@ class Theme final : public QObject { ...@@ -25,10 +32,17 @@ class Theme final : public QObject {
QMap<QString, QColor> palette; QMap<QString, QColor> palette;
}; };
public:
Theme(const QString &id, QObject *parent = nullptr) noexcept; Theme(const QString &id, QObject *parent = nullptr) noexcept;
void readSettings(QSettings *const settings) noexcept;
public:
~Theme() noexcept override; ~Theme() noexcept override;
[[nodiscard]] static Theme *fromQssFile(const QString &, const QString &) noexcept;
[[nodiscard]] static Theme *fromSettings(const QString &, QSettings *const) noexcept;
[[nodiscard]] static Theme *fromVivyTheme(const QString &, const VivyTheme) noexcept;
void applyToApplication() const noexcept; void applyToApplication() const noexcept;
enum Color { enum Color {
...@@ -463,8 +477,6 @@ public: ...@@ -463,8 +477,6 @@ public:
QString displayName() const noexcept; QString displayName() const noexcept;
void setDisplayName(const QString &displayName) noexcept; void setDisplayName(const QString &displayName) noexcept;
void readSettings(QSettings *const settings) noexcept;
static bool systemUsesDarkMode() noexcept; static bool systemUsesDarkMode() noexcept;
static QPalette initialPalette() noexcept; static QPalette initialPalette() noexcept;
......
#include "VivyApplication.hh" #include "VivyApplication.hh"
#include "UI/MainWindow.hh" #include "UI/MainWindow.hh"
#include "UI/DockWidgetTitleBar.hh" #include "UI/DockWidgetTitleBar.hh"
#include "UI/LogConsole.hh"
#include "UI/Theme/Theme.hh" #include "UI/Theme/Theme.hh"
#include "Lib/Document/VivyDocumentStore.hh" #include "Lib/Document/VivyDocumentStore.hh"
#include "Lib/Document/VivyDocument.hh" #include "Lib/Document/VivyDocument.hh"
...@@ -44,34 +45,25 @@ VivyApplication::VivyApplication(int &argc, char **argv) ...@@ -44,34 +45,25 @@ VivyApplication::VivyApplication(int &argc, char **argv)
VivyApplication::~VivyApplication() noexcept {} VivyApplication::~VivyApplication() noexcept {}
void void
VivyApplication::setTheme(Theme theme) noexcept VivyApplication::setTheme(Theme::Type theme, Theme::VivyTheme preferedColor) noexcept
{ {
if (theme == Theme::System) { if (theme == Theme::Type::System) {
logInfo() << "Set theme to system theme";
return; return;
} }
else if (theme == Theme::QtCreator) { else if (theme == Theme::Type::QtCreator) {
qtCreatorThemePtr.reset(new Vivy::Theme("QtCreator"));
QSettings settings(QStringLiteral(":theme/design.creatortheme"), QSettings::IniFormat); QSettings settings(QStringLiteral(":theme/design.creatortheme"), QSettings::IniFormat);
qtCreatorThemePtr->readSettings(&settings); qtCreatorThemePtr.reset(Vivy::Theme::fromSettings("QtCreator", &settings));
qtCreatorThemePtr->applyToApplication(); qtCreatorThemePtr->applyToApplication();
return; return;
} }
const QString sheet = theme == Theme::Dark ? QStringLiteral(":qdarkstyle/dark/style.qss") if (theme != Theme::Type::QssFile) {
: QStringLiteral(":qdarkstyle/light/style.qss"); qFatal("Unknown Theme type");
QFile stylesheet(sheet);
if (!stylesheet.exists()) {
logFatal() << "Missing stylesheet!";
exit(EXIT_FAILURE);
} else {
stylesheet.open(QFile::ReadOnly | QFile::Text);
QTextStream stylesheetStream(&stylesheet);
setStyleSheet(stylesheetStream.readAll());
logInfo() << "Theme set using " << VIVY_LOG_QUOTED(sheet);
} }
qtCreatorThemePtr.reset(Vivy::Theme::fromVivyTheme("VivyTheme", preferedColor));
qtCreatorThemePtr->applyToApplication();
} }
int int
...@@ -93,8 +85,7 @@ VivyApplication::exec() noexcept ...@@ -93,8 +85,7 @@ VivyApplication::exec() noexcept
setAttribute(Qt::AA_DontShowIconsInMenus, false); setAttribute(Qt::AA_DontShowIconsInMenus, false);
setAttribute(Qt::AA_DontShowShortcutsInContextMenus, false); setAttribute(Qt::AA_DontShowShortcutsInContextMenus, false);
setFont(getApplicationFont(Font::Default)); setFont(getApplicationFont(Font::Default));
setTheme(Theme::QtCreator); // TODO: Set system theme for now (because we setTheme(Theme::Type::QtCreator, Theme::VivyTheme::Dark);
// don't have a central theme provider).
// Cursor blinking // Cursor blinking
setCursorFlashTime(0); setCursorFlashTime(0);
......
...@@ -36,11 +36,13 @@ ...@@ -36,11 +36,13 @@
// Detect MacOS // Detect MacOS
#if defined(Q_OS_DARWIN) || defined(Q_OS_MACOS) #if defined(Q_OS_DARWIN) || defined(Q_OS_MACOS)
#define VIVY_MACOS #define VIVY_MACOS 1
#else
#define VIVY_MACOS 0
#endif #endif
#include "Lib/Log.hh" #include "Lib/Log.hh"
#include "UI/LogConsole.hh" #include "UI/Theme/Theme.hh"
namespace Vivy namespace Vivy
{ {
...@@ -74,8 +76,6 @@ public: ...@@ -74,8 +76,6 @@ public:
DefaultBoldItalic DefaultBoldItalic
}; };
enum class Theme { System, Dark, Light, QtCreator };
private: private:
int fontIdMonospace; int fontIdMonospace;
int fontIdMonospaceBold; int fontIdMonospaceBold;
...@@ -103,11 +103,10 @@ public: ...@@ -103,11 +103,10 @@ public:
[[nodiscard]] MainWindow *getMainWindow() const; [[nodiscard]] MainWindow *getMainWindow() const;
[[nodiscard]] AbstractDocument *getCurrentDocument() const; [[nodiscard]] AbstractDocument *getCurrentDocument() const;
[[nodiscard]] bool getUseFakeVimEditor() const noexcept; [[nodiscard]] bool getUseFakeVimEditor() const noexcept;
std::shared_ptr<LogSink> getLogSink() noexcept { return logSink; }
void setUseFakeVimEditor(bool) noexcept; void setUseFakeVimEditor(bool) noexcept;
void setTheme(Theme) noexcept; void setTheme(Theme::Type, Theme::VivyTheme) noexcept;
std::shared_ptr<LogSink> getLogSink() noexcept { return logSink; }
}; };
} }
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter