dialogs_message_view.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. /*
  2. This file is part of Telegram Desktop,
  3. the official desktop application for the Telegram messaging service.
  4. For license and copyright information please follow this link:
  5. https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
  6. */
  7. #include "dialogs/ui/dialogs_message_view.h"
  8. #include "history/history.h"
  9. #include "history/history_item.h"
  10. #include "history/view/history_view_item_preview.h"
  11. #include "main/main_session.h"
  12. #include "dialogs/dialogs_three_state_icon.h"
  13. #include "dialogs/ui/dialogs_layout.h"
  14. #include "dialogs/ui/dialogs_topics_view.h"
  15. #include "ui/effects/spoiler_mess.h"
  16. #include "ui/text/text_options.h"
  17. #include "ui/text/text_utilities.h"
  18. #include "ui/painter.h"
  19. #include "ui/power_saving.h"
  20. #include "core/ui_integration.h"
  21. #include "lang/lang_keys.h"
  22. #include "lang/lang_text_entity.h"
  23. #include "styles/style_dialogs.h"
  24. namespace {
  25. constexpr auto kEmojiLoopCount = 2;
  26. template <ushort kTag>
  27. struct TextWithTagOffset {
  28. TextWithTagOffset(TextWithEntities text) : text(std::move(text)) {
  29. }
  30. TextWithTagOffset(QString text) : text({ std::move(text) }) {
  31. }
  32. static TextWithTagOffset FromString(const QString &text) {
  33. return { { text } };
  34. }
  35. TextWithEntities text;
  36. int offset = -1;
  37. };
  38. } // namespace
  39. namespace Lang {
  40. template <ushort kTag>
  41. struct ReplaceTag<TextWithTagOffset<kTag>> {
  42. static TextWithTagOffset<kTag> Call(
  43. TextWithTagOffset<kTag> &&original,
  44. ushort tag,
  45. const TextWithTagOffset<kTag> &replacement);
  46. };
  47. template <ushort kTag>
  48. TextWithTagOffset<kTag> ReplaceTag<TextWithTagOffset<kTag>>::Call(
  49. TextWithTagOffset<kTag> &&original,
  50. ushort tag,
  51. const TextWithTagOffset<kTag> &replacement) {
  52. const auto replacementPosition = FindTagReplacementPosition(
  53. original.text.text,
  54. tag);
  55. if (replacementPosition < 0) {
  56. return std::move(original);
  57. }
  58. original.text = ReplaceTag<TextWithEntities>::Replace(
  59. std::move(original.text),
  60. replacement.text,
  61. replacementPosition);
  62. if (tag == kTag) {
  63. original.offset = replacementPosition;
  64. } else if (original.offset > replacementPosition) {
  65. constexpr auto kReplaceCommandLength = 4;
  66. const auto replacementSize = replacement.text.text.size();
  67. original.offset += replacementSize - kReplaceCommandLength;
  68. }
  69. return std::move(original);
  70. }
  71. } // namespace Lang
  72. namespace Dialogs::Ui {
  73. TextWithEntities DialogsPreviewText(TextWithEntities text) {
  74. auto result = Ui::Text::Filtered(
  75. std::move(text),
  76. {
  77. EntityType::Pre,
  78. EntityType::Code,
  79. EntityType::Spoiler,
  80. EntityType::StrikeOut,
  81. EntityType::Underline,
  82. EntityType::Italic,
  83. EntityType::CustomEmoji,
  84. EntityType::Colorized,
  85. });
  86. for (auto &entity : result.entities) {
  87. if (entity.type() == EntityType::Pre) {
  88. entity = EntityInText(
  89. EntityType::Code,
  90. entity.offset(),
  91. entity.length());
  92. } else if (entity.type() == EntityType::Colorized
  93. && !entity.data().isEmpty()) {
  94. // Drop 'data' so that only link-color colorization takes place.
  95. entity = EntityInText(
  96. EntityType::Colorized,
  97. entity.offset(),
  98. entity.length());
  99. }
  100. }
  101. return result;
  102. }
  103. struct MessageView::LoadingContext {
  104. std::any context;
  105. rpl::lifetime lifetime;
  106. };
  107. MessageView::MessageView()
  108. : _senderCache(st::dialogsTextWidthMin)
  109. , _textCache(st::dialogsTextWidthMin) {
  110. }
  111. MessageView::~MessageView() = default;
  112. void MessageView::itemInvalidated(not_null<const HistoryItem*> item) {
  113. if (_textCachedFor == item.get()) {
  114. _textCachedFor = nullptr;
  115. }
  116. }
  117. bool MessageView::dependsOn(not_null<const HistoryItem*> item) const {
  118. return (_textCachedFor == item.get());
  119. }
  120. bool MessageView::prepared(
  121. not_null<const HistoryItem*> item,
  122. Data::Forum *forum) const {
  123. return (_textCachedFor == item.get())
  124. && (!forum
  125. || (_topics
  126. && _topics->forum() == forum
  127. && _topics->prepared()));
  128. }
  129. void MessageView::prepare(
  130. not_null<const HistoryItem*> item,
  131. Data::Forum *forum,
  132. Fn<void()> customEmojiRepaint,
  133. ToPreviewOptions options) {
  134. if (!forum) {
  135. _topics = nullptr;
  136. } else if (!_topics || _topics->forum() != forum) {
  137. _topics = std::make_unique<TopicsView>(forum);
  138. _topics->prepare(item->topicRootId(), customEmojiRepaint);
  139. } else if (!_topics->prepared()) {
  140. _topics->prepare(item->topicRootId(), customEmojiRepaint);
  141. }
  142. if (_textCachedFor == item.get()) {
  143. return;
  144. }
  145. options.existing = &_imagesCache;
  146. options.ignoreTopic = true;
  147. options.spoilerLoginCode = true;
  148. auto preview = item->toPreview(options);
  149. _leftIcon = (preview.icon == ItemPreview::Icon::ForwardedMessage)
  150. ? &st::dialogsMiniForward
  151. : (preview.icon == ItemPreview::Icon::ReplyToStory)
  152. ? &st::dialogsMiniReplyStory
  153. : nullptr;
  154. const auto hasImages = !preview.images.empty();
  155. const auto history = item->history();
  156. const auto context = Core::TextContext({
  157. .session = &history->session(),
  158. .repaint = customEmojiRepaint,
  159. .customEmojiLoopLimit = kEmojiLoopCount,
  160. });
  161. const auto senderTill = (preview.arrowInTextPosition > 0)
  162. ? preview.arrowInTextPosition
  163. : preview.imagesInTextPosition;
  164. if ((hasImages || _leftIcon) && senderTill > 0) {
  165. auto sender = Text::Mid(preview.text, 0, senderTill);
  166. TextUtilities::Trim(sender);
  167. _senderCache.setMarkedText(
  168. st::dialogsTextStyle,
  169. std::move(sender),
  170. DialogTextOptions());
  171. preview.text = Text::Mid(preview.text, senderTill);
  172. } else {
  173. _senderCache = { st::dialogsTextWidthMin };
  174. }
  175. TextUtilities::Trim(preview.text);
  176. auto textToCache = DialogsPreviewText(std::move(preview.text));
  177. _hasPlainLinkAtBegin = !textToCache.entities.empty()
  178. && (textToCache.entities.front().type() == EntityType::Colorized);
  179. _textCache.setMarkedText(
  180. st::dialogsTextStyle,
  181. std::move(textToCache),
  182. DialogTextOptions(),
  183. context);
  184. _textCachedFor = item;
  185. _imagesCache = std::move(preview.images);
  186. if (!ranges::any_of(_imagesCache, &ItemPreviewImage::hasSpoiler)) {
  187. _spoiler = nullptr;
  188. } else if (!_spoiler) {
  189. _spoiler = std::make_unique<SpoilerAnimation>(customEmojiRepaint);
  190. }
  191. if (preview.loadingContext.has_value()) {
  192. if (!_loadingContext) {
  193. _loadingContext = std::make_unique<LoadingContext>();
  194. item->history()->session().downloaderTaskFinished(
  195. ) | rpl::start_with_next([=] {
  196. _textCachedFor = nullptr;
  197. }, _loadingContext->lifetime);
  198. }
  199. _loadingContext->context = std::move(preview.loadingContext);
  200. } else {
  201. _loadingContext = nullptr;
  202. }
  203. }
  204. bool MessageView::isInTopicJump(int x, int y) const {
  205. return _topics && _topics->isInTopicJumpArea(x, y);
  206. }
  207. void MessageView::addTopicJumpRipple(
  208. QPoint origin,
  209. not_null<TopicJumpCache*> topicJumpCache,
  210. Fn<void()> updateCallback) {
  211. if (_topics) {
  212. _topics->addTopicJumpRipple(
  213. origin,
  214. topicJumpCache,
  215. std::move(updateCallback));
  216. }
  217. }
  218. void MessageView::stopLastRipple() {
  219. if (_topics) {
  220. _topics->stopLastRipple();
  221. }
  222. }
  223. void MessageView::clearRipple() {
  224. if (_topics) {
  225. _topics->clearRipple();
  226. }
  227. }
  228. int MessageView::countWidth() const {
  229. auto result = 0;
  230. if (!_senderCache.isEmpty()) {
  231. result += _senderCache.maxWidth();
  232. if (!_imagesCache.empty()) {
  233. result += st::dialogsMiniPreviewSkip
  234. + st::dialogsMiniPreviewRight;
  235. }
  236. }
  237. if (!_imagesCache.empty()) {
  238. result += (_imagesCache.size()
  239. * (st::dialogsMiniPreview + st::dialogsMiniPreviewSkip))
  240. + st::dialogsMiniPreviewRight;
  241. }
  242. return result + _textCache.maxWidth();
  243. }
  244. void MessageView::paint(
  245. Painter &p,
  246. const QRect &geometry,
  247. const PaintContext &context) const {
  248. if (geometry.isEmpty()) {
  249. return;
  250. }
  251. p.setFont(st::dialogsTextFont);
  252. p.setPen(context.active
  253. ? st::dialogsTextFgActive
  254. : context.selected
  255. ? st::dialogsTextFgOver
  256. : st::dialogsTextFg);
  257. const auto withTopic = _topics && context.st->topicsHeight;
  258. const auto palette = &(withTopic
  259. ? (context.active
  260. ? st::dialogsTextPaletteInTopicActive
  261. : context.selected
  262. ? st::dialogsTextPaletteInTopicOver
  263. : st::dialogsTextPaletteInTopic)
  264. : (context.active
  265. ? st::dialogsTextPaletteActive
  266. : context.selected
  267. ? st::dialogsTextPaletteOver
  268. : st::dialogsTextPalette));
  269. auto rect = geometry;
  270. const auto checkJump = withTopic && !context.active;
  271. const auto jump1 = checkJump ? _topics->jumpToTopicWidth() : 0;
  272. if (jump1) {
  273. paintJumpToLast(p, rect, context, jump1);
  274. } else if (_topics) {
  275. _topics->clearTopicJumpGeometry();
  276. }
  277. if (withTopic) {
  278. _topics->paint(p, rect, context);
  279. rect.setTop(rect.top() + context.st->topicsHeight);
  280. }
  281. auto finalRight = rect.x() + rect.width();
  282. if (jump1) {
  283. rect.setWidth(rect.width() - st::forumDialogJumpArrowSkip);
  284. finalRight -= st::forumDialogJumpArrowSkip;
  285. }
  286. const auto pausedSpoiler = context.paused
  287. || On(PowerSaving::kChatSpoiler);
  288. if (!_senderCache.isEmpty()) {
  289. _senderCache.draw(p, {
  290. .position = rect.topLeft(),
  291. .availableWidth = rect.width(),
  292. .palette = palette,
  293. .elisionHeight = rect.height(),
  294. });
  295. rect.setLeft(rect.x() + _senderCache.maxWidth());
  296. if (!_imagesCache.empty() && !_leftIcon) {
  297. const auto skip = st::dialogsMiniPreviewSkip
  298. + st::dialogsMiniPreviewRight;
  299. rect.setLeft(rect.x() + skip);
  300. }
  301. }
  302. if (_leftIcon) {
  303. const auto &icon = ThreeStateIcon(
  304. _leftIcon->icon,
  305. context.active,
  306. context.selected);
  307. const auto w = (icon.width());
  308. if (rect.width() > w) {
  309. if (_hasPlainLinkAtBegin && !context.active) {
  310. icon.paint(
  311. p,
  312. rect.topLeft(),
  313. rect.width(),
  314. palette->linkFg->c);
  315. } else {
  316. icon.paint(p, rect.topLeft(), rect.width());
  317. }
  318. rect.setLeft(rect.x()
  319. + w
  320. + (_imagesCache.empty()
  321. ? _leftIcon->skipText
  322. : _leftIcon->skipMedia));
  323. }
  324. }
  325. for (const auto &image : _imagesCache) {
  326. const auto w = st::dialogsMiniPreview + st::dialogsMiniPreviewSkip;
  327. if (rect.width() < w) {
  328. break;
  329. }
  330. const auto mini = QRect(
  331. rect.x(),
  332. rect.y() + st::dialogsMiniPreviewTop,
  333. st::dialogsMiniPreview,
  334. st::dialogsMiniPreview);
  335. if (!image.data.isNull()) {
  336. p.drawImage(mini, image.data);
  337. if (image.hasSpoiler()) {
  338. const auto frame = DefaultImageSpoiler().frame(
  339. _spoiler->index(context.now, pausedSpoiler));
  340. if (image.isEllipse()) {
  341. const auto radius = st::dialogsMiniPreview / 2;
  342. static auto mask = Images::CornersMask(radius);
  343. FillSpoilerRect(
  344. p,
  345. mini,
  346. Images::CornersMaskRef(mask),
  347. frame,
  348. _cornersCache);
  349. } else {
  350. FillSpoilerRect(p, mini, frame);
  351. }
  352. }
  353. }
  354. rect.setLeft(rect.x() + w);
  355. }
  356. if (!_imagesCache.empty()) {
  357. rect.setLeft(rect.x() + st::dialogsMiniPreviewRight);
  358. }
  359. // Style of _textCache.
  360. static const auto ellipsisWidth = st::dialogsTextStyle.font->width(
  361. kQEllipsis);
  362. if (rect.width() > ellipsisWidth) {
  363. _textCache.draw(p, {
  364. .position = rect.topLeft(),
  365. .availableWidth = rect.width(),
  366. .palette = palette,
  367. .spoiler = Text::DefaultSpoilerCache(),
  368. .now = context.now,
  369. .pausedEmoji = context.paused || On(PowerSaving::kEmojiChat),
  370. .pausedSpoiler = pausedSpoiler,
  371. .elisionHeight = rect.height(),
  372. });
  373. rect.setLeft(rect.x() + _textCache.maxWidth());
  374. }
  375. if (jump1) {
  376. const auto position = st::forumDialogJumpArrowPosition
  377. + QPoint((rect.width() > 0) ? rect.x() : finalRight, rect.y());
  378. (context.selected
  379. ? st::forumDialogJumpArrowOver
  380. : st::forumDialogJumpArrow).paint(p, position, context.width);
  381. }
  382. }
  383. void MessageView::paintJumpToLast(
  384. Painter &p,
  385. const QRect &rect,
  386. const PaintContext &context,
  387. int width1) const {
  388. if (!context.topicJumpCache) {
  389. _topics->clearTopicJumpGeometry();
  390. return;
  391. }
  392. const auto width2 = countWidth() + st::forumDialogJumpArrowSkip;
  393. const auto geometry = FillJumpToLastBg(p, {
  394. .st = context.st,
  395. .corners = (context.selected
  396. ? &context.topicJumpCache->over
  397. : &context.topicJumpCache->corners),
  398. .geometry = rect,
  399. .bg = (context.selected
  400. ? st::dialogsRippleBg
  401. : st::dialogsBgOver),
  402. .width1 = width1,
  403. .width2 = width2,
  404. });
  405. if (context.topicJumpSelected) {
  406. p.setOpacity(0.1);
  407. FillJumpToLastPrepared(p, {
  408. .st = context.st,
  409. .corners = &context.topicJumpCache->selected,
  410. .bg = st::dialogsTextFg,
  411. .prepared = geometry,
  412. });
  413. p.setOpacity(1.);
  414. }
  415. if (!_topics->changeTopicJumpGeometry(geometry)) {
  416. auto color = st::dialogsTextFg->c;
  417. color.setAlpha(color.alpha() / 10);
  418. if (color.alpha() > 0) {
  419. _topics->paintRipple(p, 0, 0, context.width, &color);
  420. }
  421. }
  422. }
  423. HistoryView::ItemPreview PreviewWithSender(
  424. HistoryView::ItemPreview &&preview,
  425. const QString &sender,
  426. TextWithEntities topic) {
  427. const auto wrappedSender = st::wrap_rtl(sender);
  428. auto senderWithOffset = topic.empty()
  429. ? TextWithTagOffset<lt_from>::FromString(wrappedSender)
  430. : tr::lng_dialogs_text_from_in_topic(
  431. tr::now,
  432. lt_from,
  433. { wrappedSender },
  434. lt_topic,
  435. std::move(topic),
  436. TextWithTagOffset<lt_from>::FromString);
  437. auto wrappedWithOffset = tr::lng_dialogs_text_from_wrapped(
  438. tr::now,
  439. lt_from,
  440. std::move(senderWithOffset.text),
  441. TextWithTagOffset<lt_from>::FromString);
  442. const auto wrappedSize = wrappedWithOffset.text.text.size();
  443. auto fullWithOffset = tr::lng_dialogs_text_with_from(
  444. tr::now,
  445. lt_from_part,
  446. Ui::Text::Colorized(std::move(wrappedWithOffset.text)),
  447. lt_message,
  448. std::move(preview.text),
  449. TextWithTagOffset<lt_from_part>::FromString);
  450. preview.text = std::move(fullWithOffset.text);
  451. preview.arrowInTextPosition = (fullWithOffset.offset < 0
  452. || wrappedWithOffset.offset < 0
  453. || senderWithOffset.offset < 0)
  454. ? -1
  455. : (fullWithOffset.offset
  456. + wrappedWithOffset.offset
  457. + senderWithOffset.offset
  458. + sender.size());
  459. preview.imagesInTextPosition = (fullWithOffset.offset < 0)
  460. ? 0
  461. : (fullWithOffset.offset + wrappedSize);
  462. return std::move(preview);
  463. }
  464. } // namespace Dialogs::Ui