Skip to content

Commit

Permalink
feat: add system theme on Qt 6.5
Browse files Browse the repository at this point in the history
  • Loading branch information
Nerixyz committed Jan 21, 2024
1 parent 65d3e73 commit e314a26
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
47 changes: 46 additions & 1 deletion src/singletons/Theme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@
#include "common/QLogging.hpp"
#include "singletons/Paths.hpp"
#include "singletons/Resources.hpp"
#include "singletons/WindowManager.hpp"

#include <QColor>
#include <QDir>
#include <QElapsedTimer>
#include <QFile>
#include <QJsonDocument>
#include <QSet>
#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
# include <QStyleHints>
#endif

#include <cmath>

Expand Down Expand Up @@ -208,7 +212,15 @@ const std::vector<ThemeDescriptor> Theme::builtInThemes{
.key = "Black",
.path = ":/themes/Black.json",
.name = "Black",
}
#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
,
{
.key = "System",
.path = ":/themes/Dark.json",
.name = "System",
},
#endif
};

// Dark is our default & fallback theme
Expand All @@ -219,6 +231,11 @@ bool Theme::isLightTheme() const
return this->isLight_;
}

bool Theme::isSystemTheme() const
{
return this->themeName == u"System";
}

void Theme::initialize(Settings &settings, const Paths &paths)
{
this->themeName.connect(
Expand All @@ -230,12 +247,40 @@ void Theme::initialize(Settings &settings, const Paths &paths)

this->loadAvailableThemes(paths);

#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
QObject::connect(qApp->styleHints(), &QStyleHints::colorSchemeChanged,
&this->lifetime_, [this] {
if (this->isSystemTheme())
{
this->update();
getIApp()->getWindows()->forceLayoutChannelViews();
}
});
#endif

this->update();
}

void Theme::update()
{
auto oTheme = this->findThemeByKey(this->themeName);
auto currentTheme = [&]() -> QString {
#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
if (this->isSystemTheme())
{
switch (qApp->styleHints()->colorScheme())
{
case Qt::ColorScheme::Light:
return u"Light"_s;
case Qt::ColorScheme::Unknown:
case Qt::ColorScheme::Dark:
return u"Dark"_s;
}
}
#endif
return this->themeName;
};

auto oTheme = this->findThemeByKey(currentTheme());

constexpr const double nsToMs = 1.0 / 1000000.0;
QElapsedTimer timer;
Expand Down
3 changes: 3 additions & 0 deletions src/singletons/Theme.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class Theme final : public Singleton
void initialize(Settings &settings, const Paths &paths) final;

bool isLightTheme() const;
bool isSystemTheme() const;

struct TabColors {
QColor text;
Expand Down Expand Up @@ -164,6 +165,8 @@ class Theme final : public Singleton
// This will only be populated when auto-reloading themes
QJsonObject currentThemeJson_;

QObject lifetime_;

/**
* Figure out which themes are available in the Themes directory
*
Expand Down

0 comments on commit e314a26

Please sign in to comment.