attach_album_preview.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  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 "ui/chat/attach/attach_album_preview.h"
  8. #include "ui/chat/attach/attach_album_thumbnail.h"
  9. #include "ui/chat/attach/attach_prepare.h"
  10. #include "ui/effects/spoiler_mess.h"
  11. #include "ui/widgets/popup_menu.h"
  12. #include "ui/painter.h"
  13. #include "lang/lang_keys.h"
  14. #include "styles/style_chat.h"
  15. #include "styles/style_boxes.h"
  16. #include "styles/style_layers.h"
  17. #include "styles/style_menu_icons.h"
  18. #include <QtWidgets/QApplication>
  19. namespace Media::Streaming {
  20. [[nodiscard]] QImage PrepareBlurredBackground(QSize outer, QImage frame);
  21. } // namespace Media::Streaming
  22. namespace Ui {
  23. namespace {
  24. constexpr auto kDragDuration = crl::time(200);
  25. } // namespace
  26. AlbumPreview::AlbumPreview(
  27. QWidget *parent,
  28. const style::ComposeControls &st,
  29. gsl::span<Ui::PreparedFile> items,
  30. SendFilesWay way,
  31. Fn<bool(int, AttachActionType)> actionAllowed)
  32. : RpWidget(parent)
  33. , _st(st)
  34. , _sendWay(way)
  35. , _actionAllowed(std::move(actionAllowed))
  36. , _dragTimer([=] { switchToDrag(); }) {
  37. setMouseTracking(true);
  38. prepareThumbs(items);
  39. updateSize();
  40. updateFileRows();
  41. }
  42. AlbumPreview::~AlbumPreview() = default;
  43. void AlbumPreview::setSendWay(SendFilesWay way) {
  44. if (_sendWay != way) {
  45. cancelDrag();
  46. _sendWay = way;
  47. }
  48. updateSize();
  49. updateFileRows();
  50. update();
  51. }
  52. void AlbumPreview::updateFileRows() {
  53. Expects(_order.size() == _thumbs.size());
  54. const auto isFile = !_sendWay.sendImagesAsPhotos();
  55. auto top = 0;
  56. for (auto i = 0; i < _order.size(); i++) {
  57. const auto &thumb = _thumbs[_order[i]];
  58. thumb->setButtonVisible(isFile && !thumb->isCompressedSticker());
  59. thumb->moveButtons(top);
  60. top += thumb->fileHeight() + st::sendMediaRowSkip;
  61. }
  62. }
  63. base::flat_set<int> AlbumPreview::collectSpoileredIndices() {
  64. auto result = base::flat_set<int>();
  65. result.reserve(_thumbs.size());
  66. auto i = 0;
  67. for (const auto &thumb : _thumbs) {
  68. if (thumb->hasSpoiler()) {
  69. result.emplace(i);
  70. }
  71. ++i;
  72. }
  73. return result;
  74. }
  75. bool AlbumPreview::canHaveSpoiler(int index) const {
  76. return _sendWay.sendImagesAsPhotos();
  77. }
  78. void AlbumPreview::toggleSpoilers(bool enabled) {
  79. for (auto &thumb : _thumbs) {
  80. thumb->setSpoiler(enabled);
  81. }
  82. }
  83. std::vector<int> AlbumPreview::takeOrder() {
  84. //Expects(_thumbs.size() == _order.size());
  85. //Expects(_itemsShownDimensions.size() == _order.size());
  86. auto reordered = std::vector<std::unique_ptr<AlbumThumbnail>>();
  87. auto reorderedShownDimensions = std::vector<QSize>();
  88. reordered.reserve(_thumbs.size());
  89. reorderedShownDimensions.reserve(_itemsShownDimensions.size());
  90. for (auto index : _order) {
  91. reordered.push_back(std::move(_thumbs[index]));
  92. reorderedShownDimensions.push_back(_itemsShownDimensions[index]);
  93. }
  94. _thumbs = std::move(reordered);
  95. _itemsShownDimensions = std::move(reorderedShownDimensions);
  96. return std::exchange(_order, defaultOrder());
  97. }
  98. auto AlbumPreview::generateOrderedLayout() const
  99. -> std::vector<GroupMediaLayout> {
  100. auto layout = LayoutMediaGroup(
  101. _itemsShownDimensions,
  102. st::sendMediaPreviewSize,
  103. st::historyGroupWidthMin / 2,
  104. st::historyGroupSkip / 2);
  105. Assert(layout.size() == _order.size());
  106. return layout;
  107. }
  108. std::vector<int> AlbumPreview::defaultOrder(int count) const {
  109. if (count < 0) {
  110. count = _order.size();
  111. }
  112. return ranges::views::ints(0, count) | ranges::to_vector;
  113. }
  114. void AlbumPreview::prepareThumbs(gsl::span<Ui::PreparedFile> items) {
  115. _order = defaultOrder(items.size());
  116. _itemsShownDimensions = ranges::views::all(
  117. _order
  118. ) | ranges::views::transform([&](int index) {
  119. return items[index].shownDimensions;
  120. }) | ranges::to_vector;
  121. const auto count = int(_order.size());
  122. const auto layout = generateOrderedLayout();
  123. _thumbs.reserve(count);
  124. for (auto i = 0; i != count; ++i) {
  125. _thumbs.push_back(std::make_unique<AlbumThumbnail>(
  126. _st,
  127. items[i],
  128. layout[i],
  129. this,
  130. [=] { update(); },
  131. [=] { changeThumbByIndex(orderIndex(thumbUnderCursor())); },
  132. [=] { deleteThumbByIndex(orderIndex(thumbUnderCursor())); }));
  133. if (_thumbs.back()->isCompressedSticker()) {
  134. _hasMixedFileHeights = true;
  135. }
  136. }
  137. _thumbsHeight = countLayoutHeight(layout);
  138. _photosHeight = ranges::accumulate(ranges::views::all(
  139. _thumbs
  140. ) | ranges::views::transform([](const auto &thumb) {
  141. return thumb->photoHeight();
  142. }), 0) + (count - 1) * st::sendMediaRowSkip;
  143. if (!_hasMixedFileHeights) {
  144. _filesHeight = count * _thumbs.front()->fileHeight()
  145. + (count - 1) * st::sendMediaRowSkip;
  146. } else {
  147. _filesHeight = ranges::accumulate(ranges::views::all(
  148. _thumbs
  149. ) | ranges::views::transform([](const auto &thumb) {
  150. return thumb->fileHeight();
  151. }), 0) + (count - 1) * st::sendMediaRowSkip;
  152. }
  153. }
  154. int AlbumPreview::contentLeft() const {
  155. return (st::boxWideWidth - st::sendMediaPreviewSize) / 2;
  156. }
  157. int AlbumPreview::contentTop() const {
  158. return 0;
  159. }
  160. AlbumThumbnail *AlbumPreview::findThumb(QPoint position) const {
  161. position -= QPoint(contentLeft(), contentTop());
  162. auto top = 0;
  163. const auto isPhotosWay = _sendWay.sendImagesAsPhotos();
  164. const auto skip = st::sendMediaRowSkip;
  165. auto find = [&](const auto &thumb) {
  166. if (_sendWay.groupFiles() && _sendWay.sendImagesAsPhotos()) {
  167. return thumb->containsPoint(position);
  168. } else {
  169. const auto bottom = top + (isPhotosWay
  170. ? thumb->photoHeight()
  171. : thumb->fileHeight());
  172. const auto isUnderTop = (position.y() > top);
  173. top = bottom + skip;
  174. return isUnderTop && (position.y() < bottom);
  175. }
  176. return false;
  177. };
  178. const auto i = ranges::find_if(_thumbs, std::move(find));
  179. return (i == _thumbs.end()) ? nullptr : i->get();
  180. }
  181. not_null<AlbumThumbnail*> AlbumPreview::findClosestThumb(
  182. QPoint position) const {
  183. Expects(_draggedThumb != nullptr);
  184. if (const auto exact = findThumb(position)) {
  185. return exact;
  186. }
  187. auto result = _draggedThumb;
  188. auto distance = _draggedThumb->distanceTo(position);
  189. for (const auto &thumb : _thumbs) {
  190. const auto check = thumb->distanceTo(position);
  191. if (check < distance) {
  192. distance = check;
  193. result = thumb.get();
  194. }
  195. }
  196. return result;
  197. }
  198. int AlbumPreview::orderIndex(not_null<AlbumThumbnail*> thumb) const {
  199. const auto i = ranges::find_if(_order, [&](int index) {
  200. return (_thumbs[index].get() == thumb);
  201. });
  202. Assert(i != _order.end());
  203. return int(i - _order.begin());
  204. }
  205. void AlbumPreview::cancelDrag() {
  206. _thumbsHeightAnimation.stop();
  207. _finishDragAnimation.stop();
  208. _shrinkAnimation.stop();
  209. if (_draggedThumb) {
  210. _draggedThumb->moveInAlbum({ 0, 0 });
  211. _draggedThumb = nullptr;
  212. }
  213. if (_suggestedThumb) {
  214. const auto suggestedIndex = orderIndex(_suggestedThumb);
  215. if (suggestedIndex > 0) {
  216. _thumbs[_order[suggestedIndex - 1]]->suggestMove(0., [] {});
  217. }
  218. if (suggestedIndex < int(_order.size() - 1)) {
  219. _thumbs[_order[suggestedIndex + 1]]->suggestMove(0., [] {});
  220. }
  221. _suggestedThumb->suggestMove(0., [] {});
  222. _suggestedThumb->finishAnimations();
  223. _suggestedThumb = nullptr;
  224. }
  225. _paintedAbove = nullptr;
  226. update();
  227. }
  228. void AlbumPreview::finishDrag() {
  229. Expects(_draggedThumb != nullptr);
  230. Expects(_suggestedThumb != nullptr);
  231. if (_suggestedThumb != _draggedThumb) {
  232. const auto currentIndex = orderIndex(_draggedThumb);
  233. const auto newIndex = orderIndex(_suggestedThumb);
  234. const auto delta = (currentIndex < newIndex) ? 1 : -1;
  235. const auto realIndex = _order[currentIndex];
  236. for (auto i = currentIndex; i != newIndex; i += delta) {
  237. _order[i] = _order[i + delta];
  238. }
  239. _order[newIndex] = realIndex;
  240. const auto layout = generateOrderedLayout();
  241. for (auto i = 0, count = int(_order.size()); i != count; ++i) {
  242. _thumbs[_order[i]]->moveToLayout(layout[i]);
  243. }
  244. _finishDragAnimation.start([=] { update(); }, 0., 1., kDragDuration);
  245. updateSizeAnimated(layout);
  246. _orderUpdated.fire({});
  247. } else {
  248. for (const auto &thumb : _thumbs) {
  249. thumb->resetLayoutAnimation();
  250. }
  251. _draggedThumb->animateLayoutToInitial();
  252. _finishDragAnimation.start([=] { update(); }, 0., 1., kDragDuration);
  253. }
  254. }
  255. int AlbumPreview::countLayoutHeight(
  256. const std::vector<GroupMediaLayout> &layout) const {
  257. const auto accumulator = [](int current, const auto &item) {
  258. return std::max(current, item.geometry.y() + item.geometry.height());
  259. };
  260. return ranges::accumulate(layout, 0, accumulator);
  261. }
  262. void AlbumPreview::updateSizeAnimated(
  263. const std::vector<GroupMediaLayout> &layout) {
  264. const auto newHeight = countLayoutHeight(layout);
  265. if (newHeight != _thumbsHeight) {
  266. _thumbsHeightAnimation.start(
  267. [=] { updateSize(); },
  268. _thumbsHeight,
  269. newHeight,
  270. kDragDuration);
  271. _thumbsHeight = newHeight;
  272. }
  273. }
  274. void AlbumPreview::updateSize() {
  275. const auto newHeight = [&] {
  276. if (!_sendWay.sendImagesAsPhotos()) {
  277. return _filesHeight;
  278. } else if (!_sendWay.groupFiles()) {
  279. return _photosHeight;
  280. } else {
  281. return int(base::SafeRound(_thumbsHeightAnimation.value(
  282. _thumbsHeight)));
  283. }
  284. }();
  285. if (height() != newHeight) {
  286. resize(st::boxWideWidth, newHeight);
  287. }
  288. }
  289. void AlbumPreview::paintEvent(QPaintEvent *e) {
  290. Painter p(this);
  291. if (!_sendWay.sendImagesAsPhotos()) {
  292. paintFiles(p, e->rect());
  293. } else if (!_sendWay.groupFiles()) {
  294. paintPhotos(p, e->rect());
  295. } else {
  296. paintAlbum(p);
  297. }
  298. }
  299. void AlbumPreview::paintAlbum(Painter &p) const {
  300. const auto shrink = _shrinkAnimation.value(_draggedThumb ? 1. : 0.);
  301. const auto moveProgress = _finishDragAnimation.value(1.);
  302. const auto left = contentLeft();
  303. const auto top = contentTop();
  304. for (const auto &thumb : _thumbs) {
  305. if (thumb.get() != _paintedAbove) {
  306. thumb->paintInAlbum(p, left, top, shrink, moveProgress);
  307. }
  308. }
  309. if (_paintedAbove) {
  310. _paintedAbove->paintInAlbum(p, left, top, shrink, moveProgress);
  311. }
  312. }
  313. void AlbumPreview::paintPhotos(Painter &p, QRect clip) const {
  314. const auto left = (st::boxWideWidth - st::sendMediaPreviewSize) / 2;
  315. auto top = 0;
  316. const auto outerWidth = width();
  317. for (const auto &thumb : _thumbs) {
  318. const auto bottom = top + thumb->photoHeight();
  319. const auto guard = gsl::finally([&] {
  320. top = bottom + st::sendMediaRowSkip;
  321. });
  322. if (top >= clip.y() + clip.height()) {
  323. break;
  324. } else if (bottom <= clip.y()) {
  325. continue;
  326. }
  327. thumb->paintPhoto(p, left, top, outerWidth);
  328. }
  329. }
  330. void AlbumPreview::paintFiles(Painter &p, QRect clip) const {
  331. const auto left = (st::boxWideWidth - st::sendMediaPreviewSize) / 2;
  332. const auto outerWidth = width();
  333. if (!_hasMixedFileHeights) {
  334. const auto fileHeight = st::attachPreviewThumbLayout.thumbSize
  335. + st::sendMediaRowSkip;
  336. const auto bottom = clip.y() + clip.height();
  337. const auto from = std::clamp(
  338. clip.y() / fileHeight,
  339. 0,
  340. int(_thumbs.size()));
  341. const auto till = std::clamp(
  342. (bottom + fileHeight - 1) / fileHeight,
  343. 0,
  344. int(_thumbs.size()));
  345. auto top = from * fileHeight;
  346. for (auto i = from; i != till; ++i) {
  347. _thumbs[i]->paintFile(p, left, top, outerWidth);
  348. top += fileHeight;
  349. }
  350. } else {
  351. auto top = 0;
  352. for (const auto &thumb : _thumbs) {
  353. const auto bottom = top + thumb->fileHeight();
  354. const auto guard = gsl::finally([&] {
  355. top = bottom + st::sendMediaRowSkip;
  356. });
  357. if (top >= clip.y() + clip.height()) {
  358. break;
  359. } else if (bottom <= clip.y()) {
  360. continue;
  361. }
  362. thumb->paintFile(p, left, top, outerWidth);
  363. }
  364. }
  365. }
  366. AlbumThumbnail *AlbumPreview::thumbUnderCursor() {
  367. return findThumb(mapFromGlobal(QCursor::pos()));
  368. }
  369. void AlbumPreview::deleteThumbByIndex(int index) {
  370. if (index < 0) {
  371. return;
  372. }
  373. _thumbDeleted.fire(std::move(index));
  374. }
  375. void AlbumPreview::changeThumbByIndex(int index) {
  376. if (index < 0) {
  377. return;
  378. }
  379. _thumbChanged.fire(std::move(index));
  380. }
  381. void AlbumPreview::modifyThumbByIndex(int index) {
  382. if (index < 0) {
  383. return;
  384. }
  385. _thumbModified.fire(std::move(index));
  386. }
  387. void AlbumPreview::thumbButtonsCallback(
  388. not_null<AlbumThumbnail*> thumb,
  389. AttachButtonType type) {
  390. const auto index = orderIndex(thumb);
  391. switch (type) {
  392. case AttachButtonType::None: return;
  393. case AttachButtonType::Edit: changeThumbByIndex(index); break;
  394. case AttachButtonType::Delete: deleteThumbByIndex(index); break;
  395. case AttachButtonType::Modify:
  396. cancelDrag();
  397. modifyThumbByIndex(index);
  398. break;
  399. }
  400. }
  401. void AlbumPreview::mousePressEvent(QMouseEvent *e) {
  402. if (_finishDragAnimation.animating()) {
  403. return;
  404. }
  405. const auto position = e->pos();
  406. cancelDrag();
  407. if (const auto thumb = findThumb(position)) {
  408. _draggedStartPosition = position;
  409. _pressedThumb = thumb;
  410. _pressedButtonType = thumb->buttonTypeFromPoint(position);
  411. const auto isAlbum = _sendWay.sendImagesAsPhotos()
  412. && _sendWay.groupFiles();
  413. if (!isAlbum || e->button() != Qt::LeftButton) {
  414. _dragTimer.cancel();
  415. return;
  416. }
  417. if (_pressedButtonType == AttachButtonType::None) {
  418. switchToDrag();
  419. } else if (_pressedButtonType == AttachButtonType::Modify) {
  420. _dragTimer.callOnce(QApplication::startDragTime());
  421. }
  422. }
  423. }
  424. void AlbumPreview::mouseMoveEvent(QMouseEvent *e) {
  425. if (!_sendWay.sendImagesAsPhotos() && !_hasMixedFileHeights) {
  426. applyCursor(style::cur_default);
  427. return;
  428. }
  429. if (_dragTimer.isActive()) {
  430. _dragTimer.cancel();
  431. switchToDrag();
  432. }
  433. const auto isAlbum = _sendWay.sendImagesAsPhotos()
  434. && _sendWay.groupFiles();
  435. if (isAlbum && _draggedThumb) {
  436. const auto position = e->pos();
  437. _draggedThumb->moveInAlbum(position - _draggedStartPosition);
  438. updateSuggestedDrag(_draggedThumb->center());
  439. update();
  440. } else {
  441. const auto thumb = findThumb(e->pos());
  442. const auto regularCursor = isAlbum
  443. ? style::cur_pointer
  444. : style::cur_default;
  445. const auto cursor = thumb
  446. ? (thumb->buttonsContainPoint(e->pos())
  447. ? style::cur_pointer
  448. : regularCursor)
  449. : style::cur_default;
  450. applyCursor(cursor);
  451. }
  452. }
  453. void AlbumPreview::applyCursor(style::cursor cursor) {
  454. if (_cursor != cursor) {
  455. _cursor = cursor;
  456. setCursor(_cursor);
  457. }
  458. }
  459. void AlbumPreview::updateSuggestedDrag(QPoint position) {
  460. auto closest = findClosestThumb(position);
  461. auto closestIndex = orderIndex(closest);
  462. const auto draggedIndex = orderIndex(_draggedThumb);
  463. const auto closestIsBeforePoint = closest->isPointAfter(position);
  464. if (closestIndex < draggedIndex && closestIsBeforePoint) {
  465. closest = _thumbs[_order[++closestIndex]].get();
  466. } else if (closestIndex > draggedIndex && !closestIsBeforePoint) {
  467. closest = _thumbs[_order[--closestIndex]].get();
  468. }
  469. if (_suggestedThumb == closest) {
  470. return;
  471. }
  472. const auto last = int(_order.size()) - 1;
  473. if (_suggestedThumb) {
  474. const auto suggestedIndex = orderIndex(_suggestedThumb);
  475. if (suggestedIndex < draggedIndex && suggestedIndex > 0) {
  476. const auto previous = _thumbs[_order[suggestedIndex - 1]].get();
  477. previous->suggestMove(0., [=] { update(); });
  478. } else if (suggestedIndex > draggedIndex && suggestedIndex < last) {
  479. const auto next = _thumbs[_order[suggestedIndex + 1]].get();
  480. next->suggestMove(0., [=] { update(); });
  481. }
  482. _suggestedThumb->suggestMove(0., [=] { update(); });
  483. }
  484. _suggestedThumb = closest;
  485. const auto suggestedIndex = closestIndex;
  486. if (_suggestedThumb != _draggedThumb) {
  487. const auto delta = (suggestedIndex < draggedIndex) ? 1. : -1.;
  488. if (delta > 0. && suggestedIndex > 0) {
  489. const auto previous = _thumbs[_order[suggestedIndex - 1]].get();
  490. previous->suggestMove(-delta, [=] { update(); });
  491. } else if (delta < 0. && suggestedIndex < last) {
  492. const auto next = _thumbs[_order[suggestedIndex + 1]].get();
  493. next->suggestMove(-delta, [=] { update(); });
  494. }
  495. _suggestedThumb->suggestMove(delta, [=] { update(); });
  496. }
  497. }
  498. void AlbumPreview::mouseReleaseEvent(QMouseEvent *e) {
  499. if (_draggedThumb) {
  500. finishDrag();
  501. _shrinkAnimation.start(
  502. [=] { update(); },
  503. 1.,
  504. 0.,
  505. AlbumThumbnail::kShrinkDuration);
  506. _draggedThumb = nullptr;
  507. _suggestedThumb = nullptr;
  508. update();
  509. } else if (const auto thumb = base::take(_pressedThumb)) {
  510. const auto was = _pressedButtonType;
  511. const auto now = thumb->buttonTypeFromPoint(e->pos());
  512. if (e->button() == Qt::RightButton) {
  513. showContextMenu(thumb, e->globalPos());
  514. } else if (was == now) {
  515. thumbButtonsCallback(thumb, now);
  516. }
  517. }
  518. _pressedButtonType = AttachButtonType::None;
  519. }
  520. void AlbumPreview::showContextMenu(
  521. not_null<AlbumThumbnail*> thumb,
  522. QPoint position) {
  523. _menu = base::make_unique_q<Ui::PopupMenu>(
  524. this,
  525. st::popupMenuWithIcons);
  526. const auto index = orderIndex(thumb);
  527. if (_actionAllowed(index, AttachActionType::ToggleSpoiler)
  528. && _sendWay.sendImagesAsPhotos()) {
  529. const auto spoilered = thumb->hasSpoiler();
  530. _menu->addAction(spoilered
  531. ? tr::lng_context_disable_spoiler(tr::now)
  532. : tr::lng_context_spoiler_effect(tr::now), [=] {
  533. thumb->setSpoiler(!spoilered);
  534. }, spoilered ? &st::menuIconSpoilerOff : &st::menuIconSpoiler);
  535. }
  536. if (_actionAllowed(index, AttachActionType::EditCover)) {
  537. _menu->addAction(tr::lng_context_edit_cover(tr::now), [=] {
  538. _thumbEditCoverRequested.fire_copy(index);
  539. }, &st::menuIconEdit);
  540. if (_actionAllowed(index, AttachActionType::ClearCover)) {
  541. _menu->addAction(tr::lng_context_clear_cover(tr::now), [=] {
  542. _thumbClearCoverRequested.fire_copy(index);
  543. }, &st::menuIconCancel);
  544. }
  545. }
  546. if (_menu->empty()) {
  547. _menu = nullptr;
  548. } else {
  549. _menu->popup(position);
  550. }
  551. }
  552. void AlbumPreview::switchToDrag() {
  553. _paintedAbove
  554. = _suggestedThumb
  555. = _draggedThumb
  556. = base::take(_pressedThumb);
  557. _shrinkAnimation.start(
  558. [=] { update(); },
  559. 0.,
  560. 1.,
  561. AlbumThumbnail::kShrinkDuration);
  562. applyCursor(style::cur_sizeall);
  563. update();
  564. }
  565. QImage AlbumPreview::generatePriceTagBackground() const {
  566. auto wmax = 0;
  567. auto hmax = 0;
  568. for (auto &thumb : _thumbs) {
  569. const auto geometry = thumb->geometry();
  570. accumulate_max(wmax, geometry.x() + geometry.width());
  571. accumulate_max(hmax, geometry.y() + geometry.height());
  572. }
  573. const auto size = QSize(wmax, hmax);
  574. if (size.isEmpty()) {
  575. return {};
  576. }
  577. const auto ratio = style::DevicePixelRatio();
  578. const auto full = size * ratio;
  579. const auto skip = st::historyGroupSkip;
  580. auto result = QImage(full, QImage::Format_ARGB32_Premultiplied);
  581. result.setDevicePixelRatio(ratio);
  582. result.fill(Qt::black);
  583. auto p = QPainter(&result);
  584. auto hq = PainterHighQualityEnabler(p);
  585. for (auto &thumb : _thumbs) {
  586. const auto geometry = thumb->geometry();
  587. if (geometry.isEmpty()) {
  588. continue;
  589. }
  590. const auto w = geometry.width();
  591. const auto h = geometry.height();
  592. const auto wscale = (w + skip) / float64(w);
  593. const auto hscale = (h + skip) / float64(h);
  594. p.save();
  595. p.translate(geometry.center());
  596. p.scale(wscale, hscale);
  597. p.translate(-geometry.center());
  598. thumb->paintInAlbum(p, 0, 0, 1., 1.);
  599. p.restore();
  600. }
  601. p.end();
  602. return ::Media::Streaming::PrepareBlurredBackground(
  603. full,
  604. std::move(result));
  605. }
  606. } // namespace Ui