// // This file is part of Kepka, // an unofficial desktop version of Telegram messaging app, // see https://github.com/procxx/kepka // // Kepka is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // It is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // In addition, as a special exception, the copyright holders give permission // to link the code of portions of this program with the OpenSSL library. // // Full license: https://github.com/procxx/kepka/blob/master/LICENSE // Copyright (c) 2014-2017 John Preston, https://desktop.telegram.org // Copyright (c) 2017- Kepka Contributors, https://github.com/procxx // #pragma once #include "QImage" #include "QPixmap" #include "base/observer.h" #include "styles/palette.h" namespace Window { namespace Theme { namespace internal { constexpr qint32 kUninitializedBackground = -999; constexpr qint32 kTestingThemeBackground = -666; constexpr qint32 kTestingDefaultBackground = -665; constexpr qint32 kTestingEditorBackground = -664; } // namespace internal constexpr qint32 kThemeBackground = -2; constexpr qint32 kCustomBackground = -1; constexpr qint32 kInitialBackground = 0; constexpr qint32 kDefaultBackground = 105; struct Cached { QByteArray colors; QByteArray background; bool tiled = false; qint32 paletteChecksum = 0; qint32 contentChecksum = 0; }; bool Load(const QString &pathRelative, const QString &pathAbsolute, const QByteArray &content, Cached &cache); void Unload(); struct Instance { style::palette palette; QImage background; Cached cached; bool tiled = false; }; struct Preview { QString path; Instance instance; QByteArray content; QPixmap preview; }; bool Apply(const QString &filepath); bool Apply(std::unique_ptr preview); void ApplyDefault(); bool ApplyEditedPalette(const QString &path, const QByteArray &content); void KeepApplied(); bool IsNonDefaultUsed(); bool IsNightTheme(); void SwitchNightTheme(bool enabled); void Revert(); bool LoadFromFile(const QString &file, Instance *out, QByteArray *outContent); bool IsPaletteTestingPath(const QString &path); struct BackgroundUpdate { enum class Type { New, Changed, Start, TestingTheme, RevertingTheme, ApplyingTheme, }; BackgroundUpdate(Type type, bool tiled) : type(type) , tiled(tiled) {} bool paletteChanged() const { return (type == Type::TestingTheme || type == Type::RevertingTheme); } Type type; bool tiled; }; class ChatBackground : public base::Observable { public: // This method is allowed to (and should) be called before start(). void setThemeData(QImage &&themeImage, bool themeTile); // This method is setting the default (themed) image if none was set yet. void start(); void setImage(qint32 id, QImage &&image = QImage()); void setTile(bool tile); void reset(); enum class ChangeMode { SwitchToThemeBackground, LeaveCurrentCustomBackground, }; void setTestingTheme(Instance &&theme, ChangeMode mode = ChangeMode::SwitchToThemeBackground); void setTestingDefaultTheme(); void keepApplied(); void revert(); qint32 id() const; const QPixmap &pixmap() const { return _pixmap; } const QPixmap &pixmapForTiled() const { return _pixmapForTiled; } bool tile() const; bool tileForSave() const; private: void ensureStarted(); void saveForRevert(); void setPreparedImage(QImage &&image); void writeNewBackgroundSettings(); qint32 _id = internal::kUninitializedBackground; QPixmap _pixmap; QPixmap _pixmapForTiled; bool _tile = false; QImage _themeImage; bool _themeTile = false; qint32 _idForRevert = internal::kUninitializedBackground; QImage _imageForRevert; bool _tileForRevert = false; }; ChatBackground *Background(); void ComputeBackgroundRects(QRect wholeFill, QSize imageSize, QRect &to, QRect &from); bool CopyColorsToPalette(const QString &path, const QByteArray &themeContent); bool ReadPaletteValues(const QByteArray &content, Fn callback); } // namespace Theme } // namespace Window