From 5feb381cb26fe4dda0e3438f4635e70ccf05fafe Mon Sep 17 00:00:00 2001 From: John Preston Date: Mon, 1 Feb 2021 15:44:22 +0400 Subject: [PATCH] Allow showing images from cache in media viewer. Fixes #10237. --- Telegram/SourceFiles/data/data_document.cpp | 41 ++++++++++++++----- .../media/view/media_view_overlay_widget.cpp | 16 +++++++- 2 files changed, 44 insertions(+), 13 deletions(-) diff --git a/Telegram/SourceFiles/data/data_document.cpp b/Telegram/SourceFiles/data/data_document.cpp index c241c6b244..6f0bcf47f7 100644 --- a/Telegram/SourceFiles/data/data_document.cpp +++ b/Telegram/SourceFiles/data/data_document.cpp @@ -47,6 +47,8 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL #include "lottie/lottie_animation.h" #include "app.h" +#include + namespace { const auto kAnimatedStickerDimensions = QSize( @@ -320,21 +322,33 @@ void DocumentOpenClickHandler::Open( return; } - const auto openFile = [&] { + const auto media = data->createMediaView(); + const auto openImageInApp = [&] { + if (data->size >= App::kImageSizeLimit) { + return false; + } const auto &location = data->location(true); - if (data->size < App::kImageSizeLimit && location.accessEnable()) { + if (!location.isEmpty() && location.accessEnable()) { const auto guard = gsl::finally([&] { location.accessDisable(); }); const auto path = location.name(); - if (Core::MimeTypeForFile(path).name().startsWith("image/") && QImageReader(path).canRead()) { + if (Core::MimeTypeForFile(path).name().startsWith("image/") + && QImageReader(path).canRead()) { Core::App().showDocument(data, context); - return; + return true; + } + } else if (data->mimeString().startsWith("image/") + && !media->bytes().isEmpty()) { + auto bytes = media->bytes(); + auto buffer = QBuffer(&bytes); + if (QImageReader(&buffer).canRead()) { + Core::App().showDocument(data, context); + return true; } } - LaunchWithWarning(&data->session(), location.name(), context); + return false; }; - const auto media = data->createMediaView(); const auto &location = data->location(true); if (data->isTheme() && media->loaded(true)) { Core::App().showDocument(data, context); @@ -352,11 +366,16 @@ void DocumentOpenClickHandler::Open( } else { Core::App().showDocument(data, context); } - } else if (data->saveFromDataSilent()) { - openFile(); - } else if (data->status == FileReady - || data->status == FileDownloadFailed) { - DocumentSaveClickHandler::Save(origin, data); + } else { + data->saveFromDataSilent(); + if (!openImageInApp()) { + if (!data->filepath(true).isEmpty()) { + LaunchWithWarning(&data->session(), location.name(), context); + } else if (data->status == FileReady + || data->status == FileDownloadFailed) { + DocumentSaveClickHandler::Save(origin, data); + } + } } } diff --git a/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp b/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp index f16fde906d..f2088909c9 100644 --- a/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp +++ b/Telegram/SourceFiles/media/view/media_view_overlay_widget.cpp @@ -197,8 +197,7 @@ void PaintImageProfile(QPainter &p, const QImage &image, QRect rect, QRect fill) }); } -QPixmap PrepareStaticImage(const QString &path) { - auto image = App::readImage(path, nullptr, false); +QPixmap PrepareStaticImage(QImage image) { #if defined Q_OS_MAC && !defined OS_MAC_OLD if (image.width() > kMaxDisplayImageSize || image.height() > kMaxDisplayImageSize) { @@ -212,6 +211,14 @@ QPixmap PrepareStaticImage(const QString &path) { return App::pixmapFromImageInPlace(std::move(image)); } +QPixmap PrepareStaticImage(const QString &path) { + return PrepareStaticImage(App::readImage(path, nullptr, false)); +} + +QPixmap PrepareStaticImage(const QByteArray &bytes) { + return PrepareStaticImage(App::readImage(bytes, nullptr, false)); +} + } // namespace struct OverlayWidget::SharedMedia { @@ -2304,6 +2311,11 @@ void OverlayWidget::displayDocument( _staticContent = PrepareStaticImage(path); _touchbarDisplay.fire(TouchBarItemType::Photo); } + } else if (!_documentMedia->bytes().isEmpty()) { + _staticContent = PrepareStaticImage(_documentMedia->bytes()); + if (!_staticContent.isNull()) { + _touchbarDisplay.fire(TouchBarItemType::Photo); + } } location.accessDisable(); }