Moved drawing preview image from HistoryWidget to WebPageData.

This commit is contained in:
23rd 2020-06-01 14:16:12 +03:00
parent 0b655450bb
commit 58281023bc
3 changed files with 63 additions and 17 deletions

View file

@ -6388,27 +6388,43 @@ void HistoryWidget::drawField(Painter &p, const QRect &rect) {
}
}
if (drawWebPagePreview) {
const auto textTop = backy + st::msgReplyPadding.top();
auto previewLeft = st::historyReplySkip + st::webPageLeft;
p.fillRect(st::historyReplySkip, backy + st::msgReplyPadding.top(), st::webPageBar, st::msgReplyBarSize.height(), st::msgInReplyBarColor);
if ((_previewData->photo && !_previewData->photo->isNull()) || (_previewData->document && _previewData->document->hasThumbnail() && !_previewData->document->isPatternWallPaper())) {
const auto preview = _previewData->photo
? _previewData->photo->getReplyPreview(Data::FileOrigin())
: _previewData->document->getReplyPreview(Data::FileOrigin());
if (preview) {
auto to = QRect(previewLeft, backy + st::msgReplyPadding.top(), st::msgReplyBarSize.height(), st::msgReplyBarSize.height());
if (preview->width() == preview->height()) {
p.drawPixmap(to.x(), to.y(), preview->pix());
} else {
auto from = (preview->width() > preview->height()) ? QRect((preview->width() - preview->height()) / 2, 0, preview->height(), preview->height()) : QRect(0, (preview->height() - preview->width()) / 2, preview->width(), preview->width());
p.drawPixmap(to, preview->pix(), from);
}
}
previewLeft += st::msgReplyBarSize.height() + st::msgReplyBarSkip - st::msgReplyBarSize.width() - st::msgReplyBarPos.x();
p.fillRect(
st::historyReplySkip,
textTop,
st::webPageBar,
st::msgReplyBarSize.height(),
st::msgInReplyBarColor);
const auto to = QRect(
previewLeft,
textTop,
st::msgReplyBarSize.height(),
st::msgReplyBarSize.height());
if (HistoryView::DrawWebPageDataPreview(p, _previewData, to)) {
previewLeft += st::msgReplyBarSize.height()
+ st::msgReplyBarSkip
- st::msgReplyBarSize.width()
- st::msgReplyBarPos.x();
}
p.setPen(st::historyReplyNameFg);
_previewTitle.drawElided(p, previewLeft, backy + st::msgReplyPadding.top(), width() - previewLeft - _fieldBarCancel->width() - st::msgReplyPadding.right());
const auto elidedWidth = width()
- previewLeft
- _fieldBarCancel->width()
- st::msgReplyPadding.right();
_previewTitle.drawElided(
p,
previewLeft,
textTop,
elidedWidth);
p.setPen(st::historyComposeAreaFg);
_previewDescription.drawElided(p, previewLeft, backy + st::msgReplyPadding.top() + st::msgServiceNameFont->height, width() - previewLeft - _fieldBarCancel->width() - st::msgReplyPadding.right());
_previewDescription.drawElided(
p,
previewLeft,
textTop + st::msgServiceNameFont->height,
elidedWidth);
}
}

View file

@ -7,6 +7,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#include "history/view/history_view_webpage_preview.h"
#include "data/data_file_origin.h"
#include "data/data_web_page.h"
namespace HistoryView {
@ -56,4 +57,32 @@ WebPageText TitleAndDescriptionFromWebPage(not_null<WebPageData*> d) {
return { resultTitle, resultDescription };
}
bool DrawWebPageDataPreview(Painter &p, not_null<WebPageData*> d, QRect to) {
const auto document = d->document;
const auto photo = d->photo;
if ((!photo || photo->isNull())
&& (!document
|| !document->hasThumbnail()
|| document->isPatternWallPaper())) {
return false;
}
const auto preview = photo
? photo->getReplyPreview(Data::FileOrigin())
: document->getReplyPreview(Data::FileOrigin());
const auto w = preview->width();
const auto h = preview->height();
if (preview) {
if (w == h) {
p.drawPixmap(to.x(), to.y(), preview->pix());
} else {
const auto from = (w > h)
? QRect((w - h) / 2, 0, h, h)
: QRect(0, (h - w) / 2, w, w);
p.drawPixmap(to, preview->pix(), from);
}
}
return true;
}
} // namespace HistoryView

View file

@ -15,5 +15,6 @@ struct WebPageText {
};
WebPageText TitleAndDescriptionFromWebPage(not_null<WebPageData*> d);
bool DrawWebPageDataPreview(Painter &p, not_null<WebPageData*> d, QRect to);
} // namespace HistoryView