2021-10-01 12:42:44 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop application for the Telegram messaging service.
|
|
|
|
|
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
|
|
|
*/
|
|
|
|
#include "dialogs/ui/dialogs_message_view.h"
|
|
|
|
|
2021-10-01 14:58:41 +00:00
|
|
|
#include "history/history.h"
|
2021-10-01 12:42:44 +00:00
|
|
|
#include "history/history_item.h"
|
2021-12-21 17:43:15 +00:00
|
|
|
#include "history/view/history_view_item_preview.h"
|
2021-10-01 14:58:41 +00:00
|
|
|
#include "main/main_session.h"
|
2021-10-01 12:42:44 +00:00
|
|
|
#include "ui/text/text_options.h"
|
2021-12-26 16:29:25 +00:00
|
|
|
#include "ui/text/text_utilities.h"
|
2021-10-01 14:58:41 +00:00
|
|
|
#include "ui/image/image.h"
|
2021-10-04 07:58:31 +00:00
|
|
|
#include "lang/lang_keys.h"
|
2021-10-01 12:42:44 +00:00
|
|
|
#include "styles/style_dialogs.h"
|
|
|
|
|
2021-10-04 07:58:31 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
template <ushort kTag>
|
|
|
|
struct TextWithTagOffset {
|
|
|
|
TextWithTagOffset(QString text) : text(text) {
|
|
|
|
}
|
|
|
|
static TextWithTagOffset FromString(const QString &text) {
|
|
|
|
return { text };
|
|
|
|
}
|
|
|
|
|
|
|
|
QString text;
|
|
|
|
int offset = -1;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace Lang {
|
|
|
|
|
|
|
|
template <ushort kTag>
|
|
|
|
struct ReplaceTag<TextWithTagOffset<kTag>> {
|
|
|
|
static TextWithTagOffset<kTag> Call(
|
|
|
|
TextWithTagOffset<kTag> &&original,
|
|
|
|
ushort tag,
|
|
|
|
const TextWithTagOffset<kTag> &replacement);
|
|
|
|
};
|
|
|
|
|
|
|
|
template <ushort kTag>
|
|
|
|
TextWithTagOffset<kTag> ReplaceTag<TextWithTagOffset<kTag>>::Call(
|
|
|
|
TextWithTagOffset<kTag> &&original,
|
|
|
|
ushort tag,
|
|
|
|
const TextWithTagOffset<kTag> &replacement) {
|
|
|
|
const auto replacementPosition = FindTagReplacementPosition(
|
|
|
|
original.text,
|
|
|
|
tag);
|
|
|
|
if (replacementPosition < 0) {
|
|
|
|
return std::move(original);
|
|
|
|
}
|
|
|
|
original.text = ReplaceTag<QString>::Replace(
|
|
|
|
std::move(original.text),
|
|
|
|
replacement.text,
|
|
|
|
replacementPosition);
|
|
|
|
if (tag == kTag) {
|
|
|
|
original.offset = replacementPosition;
|
|
|
|
} else if (original.offset > replacementPosition) {
|
|
|
|
constexpr auto kReplaceCommandLength = 4;
|
|
|
|
original.offset += replacement.text.size() - kReplaceCommandLength;
|
|
|
|
}
|
|
|
|
return std::move(original);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Lang
|
|
|
|
|
2021-10-01 12:42:44 +00:00
|
|
|
namespace Dialogs::Ui {
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2021-10-01 14:58:41 +00:00
|
|
|
struct MessageView::LoadingContext {
|
|
|
|
std::any context;
|
|
|
|
rpl::lifetime lifetime;
|
|
|
|
};
|
|
|
|
|
2021-10-01 12:42:44 +00:00
|
|
|
MessageView::MessageView()
|
2021-10-04 12:18:47 +00:00
|
|
|
: _senderCache(st::dialogsTextWidthMin)
|
|
|
|
, _textCache(st::dialogsTextWidthMin) {
|
2021-10-01 12:42:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MessageView::~MessageView() = default;
|
|
|
|
|
|
|
|
void MessageView::itemInvalidated(not_null<const HistoryItem*> item) {
|
|
|
|
if (_textCachedFor == item.get()) {
|
|
|
|
_textCachedFor = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MessageView::dependsOn(not_null<const HistoryItem*> item) const {
|
|
|
|
return (_textCachedFor == item.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
void MessageView::paint(
|
|
|
|
Painter &p,
|
|
|
|
not_null<const HistoryItem*> item,
|
|
|
|
const QRect &geometry,
|
|
|
|
bool active,
|
|
|
|
bool selected,
|
|
|
|
ToPreviewOptions options) const {
|
|
|
|
if (geometry.isEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (_textCachedFor != item.get()) {
|
2021-10-04 19:37:55 +00:00
|
|
|
options.existing = &_imagesCache;
|
2021-10-01 14:58:41 +00:00
|
|
|
auto preview = item->toPreview(options);
|
2021-10-04 07:58:31 +00:00
|
|
|
if (!preview.images.empty() && preview.imagesInTextPosition > 0) {
|
2021-12-26 16:29:25 +00:00
|
|
|
auto sender = ::Ui::Text::Mid(
|
|
|
|
preview.text,
|
|
|
|
0,
|
|
|
|
preview.imagesInTextPosition);
|
|
|
|
TextUtilities::Trim(sender);
|
|
|
|
_senderCache.setMarkedText(
|
2021-10-04 07:58:31 +00:00
|
|
|
st::dialogsTextStyle,
|
2021-12-26 16:29:25 +00:00
|
|
|
std::move(sender),
|
2021-10-04 07:58:31 +00:00
|
|
|
DialogTextOptions());
|
2021-12-26 16:29:25 +00:00
|
|
|
preview.text = ::Ui::Text::Mid(
|
|
|
|
preview.text,
|
|
|
|
preview.imagesInTextPosition);
|
2021-10-04 12:18:47 +00:00
|
|
|
} else {
|
|
|
|
_senderCache = { st::dialogsTextWidthMin };
|
2021-10-04 07:58:31 +00:00
|
|
|
}
|
2021-12-26 16:29:25 +00:00
|
|
|
TextUtilities::Trim(preview.text);
|
|
|
|
_textCache.setMarkedText(
|
2021-10-01 12:42:44 +00:00
|
|
|
st::dialogsTextStyle,
|
2021-12-26 16:29:25 +00:00
|
|
|
preview.text,
|
2021-10-01 12:42:44 +00:00
|
|
|
DialogTextOptions());
|
|
|
|
_textCachedFor = item;
|
2021-10-01 14:58:41 +00:00
|
|
|
_imagesCache = std::move(preview.images);
|
|
|
|
if (preview.loadingContext.has_value()) {
|
|
|
|
if (!_loadingContext) {
|
|
|
|
_loadingContext = std::make_unique<LoadingContext>();
|
|
|
|
item->history()->session().downloaderTaskFinished(
|
|
|
|
) | rpl::start_with_next([=] {
|
|
|
|
_textCachedFor = nullptr;
|
|
|
|
}, _loadingContext->lifetime);
|
|
|
|
}
|
|
|
|
_loadingContext->context = std::move(preview.loadingContext);
|
|
|
|
} else {
|
|
|
|
_loadingContext = nullptr;
|
|
|
|
}
|
|
|
|
}
|
2021-10-04 07:58:31 +00:00
|
|
|
p.setTextPalette(active
|
|
|
|
? st::dialogsTextPaletteActive
|
|
|
|
: selected
|
|
|
|
? st::dialogsTextPaletteOver
|
|
|
|
: st::dialogsTextPalette);
|
|
|
|
p.setFont(st::dialogsTextFont);
|
2021-10-04 19:37:55 +00:00
|
|
|
p.setPen(active
|
|
|
|
? st::dialogsTextFgActive
|
|
|
|
: selected
|
|
|
|
? st::dialogsTextFgOver
|
|
|
|
: st::dialogsTextFg);
|
2021-10-04 07:58:31 +00:00
|
|
|
const auto guard = gsl::finally([&] {
|
|
|
|
p.restoreTextPalette();
|
|
|
|
});
|
|
|
|
|
2021-10-01 14:58:41 +00:00
|
|
|
auto rect = geometry;
|
2021-10-04 07:58:31 +00:00
|
|
|
if (!_senderCache.isEmpty()) {
|
|
|
|
_senderCache.drawElided(
|
|
|
|
p,
|
|
|
|
rect.left(),
|
|
|
|
rect.top(),
|
|
|
|
rect.width(),
|
|
|
|
rect.height() / st::dialogsTextFont->height);
|
|
|
|
const auto skip = st::dialogsMiniPreviewSkip
|
|
|
|
+ st::dialogsMiniPreviewRight;
|
|
|
|
rect.setLeft(rect.x() + _senderCache.maxWidth() + skip);
|
|
|
|
}
|
2021-10-01 14:58:41 +00:00
|
|
|
for (const auto &image : _imagesCache) {
|
|
|
|
if (rect.width() < st::dialogsMiniPreview) {
|
|
|
|
break;
|
|
|
|
}
|
2021-10-04 19:37:55 +00:00
|
|
|
p.drawImage(
|
|
|
|
rect.x(),
|
|
|
|
rect.y() + st::dialogsMiniPreviewTop,
|
|
|
|
image.data);
|
2021-10-01 14:58:41 +00:00
|
|
|
rect.setLeft(rect.x()
|
|
|
|
+ st::dialogsMiniPreview
|
|
|
|
+ st::dialogsMiniPreviewSkip);
|
|
|
|
}
|
|
|
|
if (!_imagesCache.empty()) {
|
|
|
|
rect.setLeft(rect.x() + st::dialogsMiniPreviewRight);
|
|
|
|
}
|
|
|
|
if (rect.isEmpty()) {
|
|
|
|
return;
|
2021-10-01 12:42:44 +00:00
|
|
|
}
|
|
|
|
_textCache.drawElided(
|
|
|
|
p,
|
2021-10-01 14:58:41 +00:00
|
|
|
rect.left(),
|
|
|
|
rect.top(),
|
|
|
|
rect.width(),
|
|
|
|
rect.height() / st::dialogsTextFont->height);
|
2021-10-04 07:58:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
HistoryView::ItemPreview PreviewWithSender(
|
|
|
|
HistoryView::ItemPreview &&preview,
|
2021-12-26 16:29:25 +00:00
|
|
|
const TextWithEntities &sender) {
|
|
|
|
const auto textWithOffset = tr::lng_dialogs_text_with_from(
|
|
|
|
tr::now,
|
|
|
|
lt_from_part,
|
|
|
|
sender.text,
|
|
|
|
lt_message,
|
|
|
|
preview.text.text,
|
|
|
|
TextWithTagOffset<lt_from_part>::FromString);
|
|
|
|
preview.text = tr::lng_dialogs_text_with_from(
|
2021-10-04 07:58:31 +00:00
|
|
|
tr::now,
|
|
|
|
lt_from_part,
|
|
|
|
sender,
|
|
|
|
lt_message,
|
|
|
|
std::move(preview.text),
|
2021-12-26 16:29:25 +00:00
|
|
|
Ui::Text::WithEntities);
|
2021-10-04 07:58:31 +00:00
|
|
|
preview.imagesInTextPosition = (textWithOffset.offset < 0)
|
|
|
|
? 0
|
2021-12-26 16:29:25 +00:00
|
|
|
: textWithOffset.offset + sender.text.size();
|
2021-10-06 07:10:06 +00:00
|
|
|
return std::move(preview);
|
2021-10-01 12:42:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Dialogs::Ui
|