Correctly show custom themes in list.

This commit is contained in:
John Preston 2019-09-05 10:42:06 +03:00
parent 7485f0c960
commit 79106e0c01
9 changed files with 49 additions and 39 deletions

View File

@ -154,6 +154,10 @@ QPixmap PrepareStaticImage(const QString &path) {
return App::pixmapFromImageInPlace(std::move(image)); return App::pixmapFromImageInPlace(std::move(image));
} }
[[nodiscard]] QString CachedThemePath(uint64 documentId) {
return QString::fromLatin1("special://cached-%1").arg(documentId);
}
} // namespace } // namespace
struct OverlayWidget::SharedMedia { struct OverlayWidget::SharedMedia {
@ -2198,6 +2202,8 @@ void OverlayWidget::playbackWaitingChange(bool waiting) {
} }
void OverlayWidget::initThemePreview() { void OverlayWidget::initThemePreview() {
using namespace Window::Theme;
Assert(_doc && _doc->isTheme()); Assert(_doc && _doc->isTheme());
const auto bytes = _doc->data(); const auto bytes = _doc->data();
@ -2208,10 +2214,10 @@ void OverlayWidget::initThemePreview() {
} }
_themePreviewShown = true; _themePreviewShown = true;
Window::Theme::CurrentData current; auto current = CurrentData();
current.backgroundId = Window::Theme::Background()->id(); current.backgroundId = Background()->id();
current.backgroundImage = Window::Theme::Background()->createCurrentImage(); current.backgroundImage = Background()->createCurrentImage();
current.backgroundTiled = Window::Theme::Background()->tile(); current.backgroundTiled = Background()->tile();
const auto &cloudList = _doc->session().data().cloudThemes().list(); const auto &cloudList = _doc->session().data().cloudThemes().list();
const auto i = ranges::find( const auto i = ranges::find(
@ -2221,15 +2227,14 @@ void OverlayWidget::initThemePreview() {
const auto cloud = (i != end(cloudList)) ? *i : Data::CloudTheme(); const auto cloud = (i != end(cloudList)) ? *i : Data::CloudTheme();
const auto isTrusted = (cloud.documentId != 0); const auto isTrusted = (cloud.documentId != 0);
const auto path = _doc->location().name(); const auto realPath = _doc->location().name();
const auto path = realPath.isEmpty()
? CachedThemePath(_doc->id)
: realPath;
const auto id = _themePreviewId = rand_value<uint64>(); const auto id = _themePreviewId = rand_value<uint64>();
const auto weak = make_weak(this); const auto weak = make_weak(this);
crl::async([=, data = std::move(current)]() mutable { crl::async([=, data = std::move(current)]() mutable {
auto preview = Window::Theme::GeneratePreview( auto preview = GeneratePreview(bytes, path, cloud, std::move(data));
path,
bytes,
cloud,
std::move(data));
crl::on_main(weak, [=, result = std::move(preview)]() mutable { crl::on_main(weak, [=, result = std::move(preview)]() mutable {
if (id != _themePreviewId) { if (id != _themePreviewId) {
return; return;
@ -2243,15 +2248,14 @@ void OverlayWidget::initThemePreview() {
st::themePreviewApplyButton); st::themePreviewApplyButton);
_themeApply->show(); _themeApply->show();
_themeApply->setClickedCallback([=] { _themeApply->setClickedCallback([=] {
const auto &object = Window::Theme::Background()->themeObject(); const auto &object = Background()->themeObject();
const auto currentlyIsCustom = !object.pathAbsolute.isEmpty() const auto currentlyIsCustom = !object.cloud.id
&& !object.pathAbsolute.startsWith(qstr(":/gui/")) && !IsEmbeddedTheme(object.pathAbsolute);
&& !object.cloud.id;
auto preview = std::move(_themePreview); auto preview = std::move(_themePreview);
close(); close();
Window::Theme::Apply(std::move(preview)); Apply(std::move(preview));
if (isTrusted && !currentlyIsCustom) { if (isTrusted && !currentlyIsCustom) {
Window::Theme::KeepApplied(); KeepApplied();
} }
}); });
_themeCancel.create( _themeCancel.create(

View File

@ -4221,7 +4221,7 @@ Window::Theme::Saved readThemeUsingKey(FileKey key) {
auto ignoreCache = false; auto ignoreCache = false;
if (!object.cloud.id) { if (!object.cloud.id) {
QFile file(object.pathRelative); auto file = QFile(object.pathRelative);
if (object.pathRelative.isEmpty() || !file.exists()) { if (object.pathRelative.isEmpty() || !file.exists()) {
file.setFileName(object.pathAbsolute); file.setFileName(object.pathAbsolute);
} }
@ -4480,9 +4480,8 @@ std::vector<Lang::Language> readRecentLanguages() {
} }
bool copyThemeColorsToPalette(const QString &destination) { bool copyThemeColorsToPalette(const QString &destination) {
auto &themeKey = Window::Theme::IsNightMode() using namespace Window::Theme;
? _themeKeyNight auto &themeKey = IsNightMode() ? _themeKeyNight : _themeKeyDay;
: _themeKeyDay;
if (!themeKey) { if (!themeKey) {
return false; return false;
} }
@ -4499,10 +4498,7 @@ bool copyThemeColorsToPalette(const QString &destination) {
return false; return false;
} }
return Window::Theme::CopyColorsToPalette( return CopyColorsToPalette(destination, pathAbsolute, themeContent);
destination,
pathAbsolute,
themeContent);
} }
void writeRecentHashtagsAndBots() { void writeRecentHashtagsAndBots() {

View File

@ -856,6 +856,7 @@ void ChatBackground::setTileNightValue(bool tile) {
void ChatBackground::setThemeObject(const Object &object) { void ChatBackground::setThemeObject(const Object &object) {
_themeObject = object; _themeObject = object;
_themeObject.content = QByteArray();
} }
const Object &ChatBackground::themeObject() const { const Object &ChatBackground::themeObject() const {
@ -1100,6 +1101,10 @@ ChatBackground *Background() {
return GlobalBackground.data(); return GlobalBackground.data();
} }
bool IsEmbeddedTheme(const QString &path) {
return path.isEmpty() || path.startsWith(qstr(":/gui/"));
}
bool Initialize(Saved &&saved) { bool Initialize(Saved &&saved) {
if (InitializeFromSaved(std::move(saved))) { if (InitializeFromSaved(std::move(saved))) {
Background()->setThemeObject(saved.object); Background()->setThemeObject(saved.object);
@ -1116,7 +1121,7 @@ void Uninitialize() {
bool Apply( bool Apply(
const QString &filepath, const QString &filepath,
const Data::CloudTheme &cloud) { const Data::CloudTheme &cloud) {
if (auto preview = PreviewFromFile(filepath, {}, cloud)) { if (auto preview = PreviewFromFile(QByteArray(), filepath, cloud)) {
return Apply(std::move(preview)); return Apply(std::move(preview));
} }
return false; return false;
@ -1134,7 +1139,7 @@ bool Apply(std::unique_ptr<Preview> preview) {
void ApplyDefaultWithPath(const QString &themePath) { void ApplyDefaultWithPath(const QString &themePath) {
if (!themePath.isEmpty()) { if (!themePath.isEmpty()) {
if (auto preview = PreviewFromFile(themePath, {}, {})) { if (auto preview = PreviewFromFile(QByteArray(), themePath, {})) {
Apply(std::move(preview)); Apply(std::move(preview));
} }
} else { } else {

View File

@ -19,6 +19,8 @@ namespace Theme {
inline constexpr auto kThemeSchemeSizeLimit = 1024 * 1024; inline constexpr auto kThemeSchemeSizeLimit = 1024 * 1024;
[[nodiscard]] bool IsEmbeddedTheme(const QString &path);
struct Object { struct Object {
QString pathRelative; QString pathRelative;
QString pathAbsolute; QString pathAbsolute;

View File

@ -216,11 +216,13 @@ void ImportFromFile(
qsl("Theme files (*.tdesktop-theme *.tdesktop-palette)")); qsl("Theme files (*.tdesktop-theme *.tdesktop-palette)"));
filters.push_back(FileDialog::AllFilesFilter()); filters.push_back(FileDialog::AllFilesFilter());
const auto callback = crl::guard(session, [=]( const auto callback = crl::guard(session, [=](
const FileDialog::OpenResult &result) { const FileDialog::OpenResult &result) {
if (result.paths.isEmpty()) { const auto path = result.paths.isEmpty()
return; ? QString()
: result.paths.front();
if (!path.isEmpty()) {
Window::Theme::Apply(path);
} }
Window::Theme::Apply(result.paths.front());
}); });
FileDialog::GetOpenPath( FileDialog::GetOpenPath(
parent.get(), parent.get(),

View File

@ -909,9 +909,11 @@ void Generator::restoreTextPalette() {
} // namespace } // namespace
std::unique_ptr<Preview> PreviewFromFile( std::unique_ptr<Preview> PreviewFromFile(
const QString &filepath,
const QByteArray &bytes, const QByteArray &bytes,
const QString &filepath,
const Data::CloudTheme &cloud) { const Data::CloudTheme &cloud) {
Expects(!filepath.isEmpty()); // Use
auto result = std::make_unique<Preview>(); auto result = std::make_unique<Preview>();
auto &object = result->object; auto &object = result->object;
object.cloud = cloud; object.cloud = cloud;
@ -935,11 +937,11 @@ std::unique_ptr<Preview> PreviewFromFile(
} }
std::unique_ptr<Preview> GeneratePreview( std::unique_ptr<Preview> GeneratePreview(
const QString &filepath,
const QByteArray &bytes, const QByteArray &bytes,
const QString &filepath,
const Data::CloudTheme &cloud, const Data::CloudTheme &cloud,
CurrentData &&data) { CurrentData &&data) {
auto result = PreviewFromFile(filepath, bytes, cloud); auto result = PreviewFromFile(bytes, filepath, cloud);
if (!result) { if (!result) {
return nullptr; return nullptr;
} }

View File

@ -23,12 +23,12 @@ struct CurrentData {
}; };
std::unique_ptr<Preview> PreviewFromFile( std::unique_ptr<Preview> PreviewFromFile(
const QString &filepath,
const QByteArray &bytes, const QByteArray &bytes,
const QString &filepath,
const Data::CloudTheme &cloud); const Data::CloudTheme &cloud);
std::unique_ptr<Preview> GeneratePreview( std::unique_ptr<Preview> GeneratePreview(
const QString &filepath,
const QByteArray &bytes, const QByteArray &bytes,
const QString &filepath,
const Data::CloudTheme &cloud, const Data::CloudTheme &cloud,
CurrentData &&data); CurrentData &&data);

View File

@ -61,7 +61,7 @@ constexpr auto kShowPerRow = 4;
takeh * to.width() / to.height(), takeh * to.width() / to.height(),
1); 1);
return (small.width() != takew) return (small.width() != takew)
? small.copy(0, (small.width() - takew) / 2, takew, takeh) ? small.copy((small.width() - takew) / 2, 0, takew, takeh)
: small; : small;
} }
} }
@ -181,8 +181,7 @@ void CloudList::setup() {
std::vector<Data::CloudTheme> CloudList::collectAll() const { std::vector<Data::CloudTheme> CloudList::collectAll() const {
const auto &object = Background()->themeObject(); const auto &object = Background()->themeObject();
const auto isDefault = object.pathAbsolute.isEmpty() const auto isDefault = IsEmbeddedTheme(object.pathAbsolute);
|| object.pathAbsolute.startsWith(qstr(":/gui/"));
auto result = _window->session().data().cloudThemes().list(); auto result = _window->session().data().cloudThemes().list();
if (!isDefault) { if (!isDefault) {
const auto i = ranges::find( const auto i = ranges::find(

View File

@ -166,7 +166,7 @@ Colorizer ColorizerFrom(const EmbeddedScheme &scheme, const QColor &color) {
} }
Colorizer ColorizerForTheme(const QString &absolutePath) { Colorizer ColorizerForTheme(const QString &absolutePath) {
if (!absolutePath.startsWith(qstr(":/gui"))) { if (absolutePath.isEmpty() || !IsEmbeddedTheme(absolutePath)) {
return Colorizer(); return Colorizer();
} }
const auto schemes = EmbeddedThemes(); const auto schemes = EmbeddedThemes();