dialogs_topics_view.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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_topics_view.h"
  8. #include "dialogs/ui/dialogs_layout.h"
  9. #include "data/data_forum.h"
  10. #include "data/data_forum_topic.h"
  11. #include "core/ui_integration.h"
  12. #include "lang/lang_keys.h"
  13. #include "ui/painter.h"
  14. #include "ui/power_saving.h"
  15. #include "ui/text/text_options.h"
  16. #include "ui/text/text_utilities.h"
  17. #include "ui/effects/ripple_animation.h"
  18. #include "styles/style_dialogs.h"
  19. namespace Dialogs::Ui {
  20. namespace {
  21. constexpr auto kIconLoopCount = 1;
  22. } // namespace
  23. TopicsView::TopicsView(not_null<Data::Forum*> forum)
  24. : _forum(forum) {
  25. }
  26. TopicsView::~TopicsView() = default;
  27. bool TopicsView::prepared() const {
  28. return (_version == _forum->recentTopicsListVersion());
  29. }
  30. void TopicsView::prepare(MsgId frontRootId, Fn<void()> customEmojiRepaint) {
  31. const auto &list = _forum->recentTopics();
  32. _version = _forum->recentTopicsListVersion();
  33. _titles.reserve(list.size());
  34. auto index = 0;
  35. for (const auto &topic : list) {
  36. const auto from = begin(_titles) + index;
  37. const auto rootId = topic->rootId();
  38. const auto i = ranges::find(
  39. from,
  40. end(_titles),
  41. rootId,
  42. &Title::topicRootId);
  43. if (i != end(_titles)) {
  44. if (i != from) {
  45. ranges::rotate(from, i, i + 1);
  46. }
  47. } else if (index >= _titles.size()) {
  48. _titles.emplace_back();
  49. }
  50. auto &title = _titles[index++];
  51. const auto unread = topic->chatListBadgesState().unread;
  52. if (title.topicRootId == rootId
  53. && title.unread == unread
  54. && title.version == topic->titleVersion()) {
  55. continue;
  56. }
  57. const auto context = Core::TextContext({
  58. .session = &topic->session(),
  59. .repaint = customEmojiRepaint,
  60. .customEmojiLoopLimit = kIconLoopCount,
  61. });
  62. auto topicTitle = topic->titleWithIcon();
  63. title.topicRootId = rootId;
  64. title.version = topic->titleVersion();
  65. title.unread = unread;
  66. title.title.setMarkedText(
  67. st::dialogsTextStyle,
  68. (unread
  69. ? Ui::Text::Colorized(
  70. Ui::Text::Wrapped(
  71. std::move(topicTitle),
  72. EntityType::Bold))
  73. : std::move(topicTitle)),
  74. DialogTextOptions(),
  75. context);
  76. }
  77. while (_titles.size() > index) {
  78. _titles.pop_back();
  79. }
  80. const auto i = frontRootId
  81. ? ranges::find(_titles, frontRootId, &Title::topicRootId)
  82. : end(_titles);
  83. _jumpToTopic = (i != end(_titles));
  84. if (_jumpToTopic) {
  85. if (i != begin(_titles)) {
  86. ranges::rotate(begin(_titles), i, i + 1);
  87. }
  88. if (!_titles.front().unread) {
  89. _jumpToTopic = false;
  90. }
  91. }
  92. }
  93. int TopicsView::jumpToTopicWidth() const {
  94. return _jumpToTopic ? _titles.front().title.maxWidth() : 0;
  95. }
  96. void TopicsView::paint(
  97. Painter &p,
  98. const QRect &geometry,
  99. const PaintContext &context) const {
  100. p.setFont(st::dialogsTextFont);
  101. p.setPen(context.active
  102. ? st::dialogsTextFgActive
  103. : context.selected
  104. ? st::dialogsTextFgOver
  105. : st::dialogsTextFg);
  106. const auto palette = &(context.active
  107. ? st::dialogsTextPaletteArchiveActive
  108. : context.selected
  109. ? st::dialogsTextPaletteArchiveOver
  110. : st::dialogsTextPaletteArchive);
  111. auto rect = geometry;
  112. rect.setWidth(rect.width() - _lastTopicJumpGeometry.rightCut);
  113. auto skipBig = _jumpToTopic && !context.active;
  114. if (_titles.empty()) {
  115. p.drawText(
  116. rect.x(),
  117. rect.y() + st::normalFont->ascent,
  118. tr::lng_contacts_loading(tr::now));
  119. return;
  120. }
  121. for (const auto &title : _titles) {
  122. if (rect.width() < title.title.style()->font->elidew) {
  123. break;
  124. }
  125. title.title.draw(p, {
  126. .position = rect.topLeft(),
  127. .availableWidth = rect.width(),
  128. .palette = palette,
  129. .spoiler = Text::DefaultSpoilerCache(),
  130. .now = context.now,
  131. .pausedEmoji = context.paused || On(PowerSaving::kEmojiChat),
  132. .pausedSpoiler = context.paused || On(PowerSaving::kChatSpoiler),
  133. .elisionLines = 1,
  134. });
  135. const auto skip = skipBig
  136. ? context.st->topicsSkipBig
  137. : context.st->topicsSkip;
  138. rect.setLeft(rect.left() + title.title.maxWidth() + skip);
  139. skipBig = false;
  140. }
  141. }
  142. bool TopicsView::changeTopicJumpGeometry(JumpToLastGeometry geometry) {
  143. if (_lastTopicJumpGeometry != geometry) {
  144. _lastTopicJumpGeometry = geometry;
  145. return true;
  146. }
  147. return false;
  148. }
  149. void TopicsView::clearTopicJumpGeometry() {
  150. changeTopicJumpGeometry({});
  151. }
  152. bool TopicsView::isInTopicJumpArea(int x, int y) const {
  153. return _lastTopicJumpGeometry.area1.contains(x, y)
  154. || _lastTopicJumpGeometry.area2.contains(x, y);
  155. }
  156. void TopicsView::addTopicJumpRipple(
  157. QPoint origin,
  158. not_null<TopicJumpCache*> topicJumpCache,
  159. Fn<void()> updateCallback) {
  160. auto mask = topicJumpRippleMask(topicJumpCache);
  161. if (mask.isNull()) {
  162. return;
  163. }
  164. _ripple = std::make_unique<Ui::RippleAnimation>(
  165. st::dialogsRipple,
  166. std::move(mask),
  167. std::move(updateCallback));
  168. _ripple->add(origin);
  169. }
  170. void TopicsView::stopLastRipple() {
  171. if (_ripple) {
  172. _ripple->lastStop();
  173. }
  174. }
  175. void TopicsView::clearRipple() {
  176. _ripple = nullptr;
  177. }
  178. void TopicsView::paintRipple(
  179. QPainter &p,
  180. int x,
  181. int y,
  182. int outerWidth,
  183. const QColor *colorOverride) const {
  184. if (_ripple) {
  185. _ripple->paint(p, x, y, outerWidth, colorOverride);
  186. if (_ripple->empty()) {
  187. _ripple.reset();
  188. }
  189. }
  190. }
  191. QImage TopicsView::topicJumpRippleMask(
  192. not_null<TopicJumpCache*> topicJumpCache) const {
  193. const auto &st = st::forumDialogRow;
  194. const auto area1 = _lastTopicJumpGeometry.area1;
  195. if (area1.isEmpty()) {
  196. return QImage();
  197. }
  198. const auto area2 = _lastTopicJumpGeometry.area2;
  199. const auto drawer = [&](QPainter &p) {
  200. const auto white = style::complex_color([] { return Qt::white; });
  201. // p.setOpacity(.1);
  202. FillJumpToLastPrepared(p, {
  203. .st = &st,
  204. .corners = &topicJumpCache->rippleMask,
  205. .bg = white.color(),
  206. .prepared = _lastTopicJumpGeometry,
  207. });
  208. };
  209. return Ui::RippleAnimation::MaskByDrawer(
  210. QRect(0, 0, 1, 1).united(area1).united(area2).size(),
  211. false,
  212. drawer);
  213. }
  214. JumpToLastGeometry FillJumpToLastBg(QPainter &p, JumpToLastBg context) {
  215. const auto padding = st::forumDialogJumpPadding;
  216. const auto availableWidth = context.geometry.width();
  217. const auto want1 = std::min(context.width1, availableWidth);
  218. const auto use1 = std::min(want1, availableWidth - padding.right());
  219. const auto use2 = std::min(context.width2, availableWidth);
  220. const auto rightCut = want1 - use1;
  221. const auto origin = context.geometry.topLeft();
  222. const auto delta = std::abs(use1 - use2);
  223. if (delta <= context.st->topicsSkip / 2) {
  224. const auto w = std::max(use1, use2);
  225. const auto h = context.st->topicsHeight + st::normalFont->height;
  226. const auto fill = QRect(origin, QSize(w, h));
  227. const auto full = fill.marginsAdded(padding);
  228. auto result = JumpToLastGeometry{ rightCut, full };
  229. FillJumpToLastPrepared(p, {
  230. .st = context.st,
  231. .corners = context.corners,
  232. .bg = context.bg,
  233. .prepared = result,
  234. });
  235. return result;
  236. }
  237. const auto h1 = context.st->topicsHeight;
  238. const auto h2 = st::normalFont->height;
  239. const auto rect1 = QRect(origin, QSize(use1, h1));
  240. const auto fill1 = rect1.marginsAdded({
  241. padding.left(),
  242. padding.top(),
  243. padding.right(),
  244. (use1 < use2 ? -padding.top() : padding.bottom()),
  245. });
  246. const auto add = QPoint(0, h1);
  247. const auto rect2 = QRect(origin + add, QSize(use2, h2));
  248. const auto fill2 = rect2.marginsAdded({
  249. padding.left(),
  250. (use2 < use1 ? -padding.bottom() : padding.top()),
  251. padding.right(),
  252. padding.bottom(),
  253. });
  254. auto result = JumpToLastGeometry{ rightCut, fill1, fill2 };
  255. FillJumpToLastPrepared(p, {
  256. .st = context.st,
  257. .corners = context.corners,
  258. .bg = context.bg,
  259. .prepared = result,
  260. });
  261. return result;
  262. }
  263. void FillJumpToLastPrepared(QPainter &p, JumpToLastPrepared context) {
  264. auto &normal = context.corners->normal;
  265. auto &inverted = context.corners->inverted;
  266. auto &small = context.corners->small;
  267. const auto radius = st::forumDialogJumpRadius;
  268. const auto &bg = context.bg;
  269. const auto area1 = context.prepared.area1;
  270. const auto area2 = context.prepared.area2;
  271. if (area2.isNull()) {
  272. if (normal.p[0].isNull()) {
  273. normal = Ui::PrepareCornerPixmaps(radius, bg);
  274. }
  275. Ui::FillRoundRect(p, area1, bg, normal);
  276. return;
  277. }
  278. const auto width1 = area1.width();
  279. const auto width2 = area2.width();
  280. const auto delta = std::abs(width1 - width2);
  281. const auto h1 = context.st->topicsHeight;
  282. const auto h2 = st::normalFont->height;
  283. const auto hmin = std::min(h1, h2);
  284. const auto wantedInvertedRadius = hmin - radius;
  285. const auto invertedr = std::min(wantedInvertedRadius, delta / 2);
  286. const auto smallr = std::min(radius, delta - invertedr);
  287. const auto smallkey = (width1 < width2) ? smallr : (-smallr);
  288. if (normal.p[0].isNull()) {
  289. normal = Ui::PrepareCornerPixmaps(radius, bg);
  290. }
  291. if (inverted.p[0].isNull()
  292. || context.corners->invertedRadius != invertedr) {
  293. context.corners->invertedRadius = invertedr;
  294. inverted = Ui::PrepareInvertedCornerPixmaps(invertedr, bg);
  295. }
  296. if (smallr != radius
  297. && (small.isNull() || context.corners->smallKey != smallkey)) {
  298. context.corners->smallKey = smallr;
  299. auto pixmaps = Ui::PrepareCornerPixmaps(smallr, bg);
  300. small = pixmaps.p[(width1 < width2) ? 1 : 3];
  301. }
  302. auto no1 = normal;
  303. no1.p[2] = QPixmap();
  304. if (width1 < width2) {
  305. no1.p[3] = QPixmap();
  306. } else if (smallr != radius) {
  307. no1.p[3] = small;
  308. }
  309. Ui::FillRoundRect(p, area1, bg, no1);
  310. if (width1 < width2) {
  311. p.drawPixmap(
  312. area1.x() + width1,
  313. area1.y() + area1.height() - invertedr,
  314. inverted.p[3]);
  315. }
  316. auto no2 = normal;
  317. no2.p[0] = QPixmap();
  318. if (width2 < width1) {
  319. no2.p[1] = QPixmap();
  320. } else if (smallr != radius) {
  321. no2.p[1] = small;
  322. }
  323. Ui::FillRoundRect(p, area2, bg, no2);
  324. if (width2 < width1) {
  325. p.drawPixmap(
  326. area2.x() + width2,
  327. area2.y(),
  328. inverted.p[0]);
  329. }
  330. }
  331. } // namespace Dialogs::Ui