Automatically reload theme on file change

This commit is contained in:
Ilya Fedin 2022-06-23 03:32:01 +04:00 committed by John Preston
parent 28f75525b2
commit f7bc84fdd6
2 changed files with 23 additions and 0 deletions

View file

@ -41,6 +41,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include <QtCore/QBuffer> #include <QtCore/QBuffer>
#include <QtCore/QJsonDocument> #include <QtCore/QJsonDocument>
#include <QtCore/QJsonObject> #include <QtCore/QJsonObject>
#include <QtCore/QFileSystemWatcher>
namespace Window { namespace Window {
namespace Theme { namespace Theme {
@ -540,6 +541,8 @@ ChatBackground::ChatBackground() : _adjustableColors({
st::historyScrollBarBgOver }) { st::historyScrollBarBgOver }) {
} }
ChatBackground::~ChatBackground() = default;
void ChatBackground::setThemeData(QImage &&themeImage, bool themeTile) { void ChatBackground::setThemeData(QImage &&themeImage, bool themeTile) {
_themeImage = PostprocessBackgroundImage( _themeImage = PostprocessBackgroundImage(
std::move(themeImage), std::move(themeImage),
@ -566,6 +569,22 @@ void ChatBackground::start() {
_updates.events( _updates.events(
) | rpl::start_with_next([=](const BackgroundUpdate &update) { ) | rpl::start_with_next([=](const BackgroundUpdate &update) {
if (const auto path = _themeObject.pathAbsolute
; !path.isEmpty() && QFileInfo(path).isNativePath()) {
if (!_themeWatcher || !_themeWatcher->files().contains(path)) {
_themeWatcher = std::make_unique<QFileSystemWatcher>(
QStringList(path));
QObject::connect(
_themeWatcher.get(),
&QFileSystemWatcher::fileChanged,
[](const QString &path) {
Apply(path);
KeepApplied();
});
}
} else {
_themeWatcher = nullptr;
}
if (update.paletteChanged()) { if (update.paletteChanged()) {
style::NotifyPaletteChanged(); style::NotifyPaletteChanged();
} }

View file

@ -11,6 +11,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "data/data_cloud_themes.h" #include "data/data_cloud_themes.h"
#include "ui/style/style_core_palette.h" #include "ui/style/style_core_palette.h"
class QFileSystemWatcher;
namespace style { namespace style {
struct colorizer; struct colorizer;
} // namespace style } // namespace style
@ -154,6 +156,7 @@ enum class ClearEditing {
class ChatBackground final { class ChatBackground final {
public: public:
ChatBackground(); ChatBackground();
~ChatBackground();
[[nodiscard]] rpl::producer<BackgroundUpdate> updates() const { [[nodiscard]] rpl::producer<BackgroundUpdate> updates() const {
return _updates.events(); return _updates.events();
@ -276,6 +279,7 @@ private:
QImage _themeImage; QImage _themeImage;
bool _themeTile = false; bool _themeTile = false;
std::optional<Data::CloudTheme> _editingTheme; std::optional<Data::CloudTheme> _editingTheme;
std::unique_ptr<QFileSystemWatcher> _themeWatcher;
Data::WallPaper _paperForRevert Data::WallPaper _paperForRevert
= Data::details::UninitializedWallPaper(); = Data::details::UninitializedWallPaper();