info_profile_cover.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113
  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 "info/profile/info_profile_cover.h"
  8. #include "api/api_user_privacy.h"
  9. #include "base/timer_rpl.h"
  10. #include "data/data_peer_values.h"
  11. #include "data/data_channel.h"
  12. #include "data/data_chat.h"
  13. #include "data/data_emoji_statuses.h"
  14. #include "data/data_peer.h"
  15. #include "data/data_user.h"
  16. #include "data/data_document.h"
  17. #include "data/data_document_media.h"
  18. #include "data/data_changes.h"
  19. #include "data/data_session.h"
  20. #include "data/data_forum_topic.h"
  21. #include "data/stickers/data_custom_emoji.h"
  22. #include "info/profile/info_profile_values.h"
  23. #include "info/profile/info_profile_badge.h"
  24. #include "info/profile/info_profile_emoji_status_panel.h"
  25. #include "info/info_controller.h"
  26. #include "boxes/peers/edit_forum_topic_box.h"
  27. #include "boxes/report_messages_box.h"
  28. #include "history/view/media/history_view_sticker_player.h"
  29. #include "lang/lang_keys.h"
  30. #include "ui/boxes/show_or_premium_box.h"
  31. #include "ui/controls/userpic_button.h"
  32. #include "ui/widgets/buttons.h"
  33. #include "ui/widgets/labels.h"
  34. #include "ui/widgets/popup_menu.h"
  35. #include "ui/text/text_utilities.h"
  36. #include "ui/ui_utility.h"
  37. #include "ui/painter.h"
  38. #include "base/event_filter.h"
  39. #include "base/unixtime.h"
  40. #include "window/window_controller.h"
  41. #include "window/window_session_controller.h"
  42. #include "main/main_session.h"
  43. #include "settings/settings_premium.h"
  44. #include "chat_helpers/stickers_lottie.h"
  45. #include "apiwrap.h"
  46. #include "api/api_peer_photo.h"
  47. #include "styles/style_boxes.h"
  48. #include "styles/style_info.h"
  49. #include "styles/style_dialogs.h"
  50. #include "styles/style_menu_icons.h"
  51. namespace Info::Profile {
  52. namespace {
  53. constexpr auto kWaitBeforeGiftBadge = crl::time(1000);
  54. constexpr auto kGiftBadgeGlares = 3;
  55. constexpr auto kGlareDurationStep = crl::time(320);
  56. constexpr auto kGlareTimeout = crl::time(1000);
  57. auto MembersStatusText(int count) {
  58. return tr::lng_chat_status_members(tr::now, lt_count_decimal, count);
  59. };
  60. auto OnlineStatusText(int count) {
  61. return tr::lng_chat_status_online(tr::now, lt_count_decimal, count);
  62. };
  63. auto ChatStatusText(int fullCount, int onlineCount, bool isGroup) {
  64. if (onlineCount > 1 && onlineCount <= fullCount) {
  65. return tr::lng_chat_status_members_online(
  66. tr::now,
  67. lt_members_count,
  68. MembersStatusText(fullCount),
  69. lt_online_count,
  70. OnlineStatusText(onlineCount));
  71. } else if (fullCount > 0) {
  72. return isGroup
  73. ? tr::lng_chat_status_members(
  74. tr::now,
  75. lt_count_decimal,
  76. fullCount)
  77. : tr::lng_chat_status_subscribers(
  78. tr::now,
  79. lt_count_decimal,
  80. fullCount);
  81. }
  82. return isGroup
  83. ? tr::lng_group_status(tr::now)
  84. : tr::lng_channel_status(tr::now);
  85. };
  86. [[nodiscard]] const style::InfoProfileCover &CoverStyle(
  87. not_null<PeerData*> peer,
  88. Data::ForumTopic *topic,
  89. Cover::Role role) {
  90. return (role == Cover::Role::EditContact)
  91. ? st::infoEditContactCover
  92. : topic
  93. ? st::infoTopicCover
  94. : peer->isMegagroup()
  95. ? st::infoProfileMegagroupCover
  96. : st::infoProfileCover;
  97. }
  98. [[nodiscard]] QMargins LargeCustomEmojiMargins() {
  99. const auto ratio = style::DevicePixelRatio();
  100. const auto emoji = Ui::Emoji::GetSizeLarge() / ratio;
  101. const auto size = Data::FrameSizeFromTag(Data::CustomEmojiSizeTag::Large)
  102. / ratio;
  103. const auto left = (size - emoji) / 2;
  104. const auto right = size - emoji - left;
  105. return { left, left, right, right };
  106. }
  107. } // namespace
  108. class Cover::BadgeTooltip final : public Ui::RpWidget {
  109. public:
  110. BadgeTooltip(
  111. not_null<QWidget*> parent,
  112. std::shared_ptr<Data::EmojiStatusCollectible> collectible,
  113. not_null<QWidget*> pointTo);
  114. void fade(bool shown);
  115. void finishAnimating();
  116. [[nodiscard]] crl::time glarePeriod() const;
  117. private:
  118. void paintEvent(QPaintEvent *e) override;
  119. void setupGeometry(not_null<QWidget*> pointTo);
  120. void prepareImage();
  121. void showGlare();
  122. const style::ImportantTooltip &_st;
  123. std::shared_ptr<Data::EmojiStatusCollectible> _collectible;
  124. QString _text;
  125. const style::font &_font;
  126. QSize _inner;
  127. QSize _outer;
  128. int _stroke = 0;
  129. int _skip = 0;
  130. QSize _full;
  131. int _glareSize = 0;
  132. int _glareRange = 0;
  133. crl::time _glareDuration = 0;
  134. base::Timer _glareTimer;
  135. Ui::Animations::Simple _showAnimation;
  136. Ui::Animations::Simple _glareAnimation;
  137. QImage _image;
  138. int _glareRight = 0;
  139. int _imageGlareRight = 0;
  140. int _arrowMiddle = 0;
  141. int _imageArrowMiddle = 0;
  142. bool _shown = false;
  143. };
  144. Cover::BadgeTooltip::BadgeTooltip(
  145. not_null<QWidget*> parent,
  146. std::shared_ptr<Data::EmojiStatusCollectible> collectible,
  147. not_null<QWidget*> pointTo)
  148. : Ui::RpWidget(parent)
  149. , _st(st::infoGiftTooltip)
  150. , _collectible(std::move(collectible))
  151. , _text(_collectible->title)
  152. , _font(st::infoGiftTooltipFont)
  153. , _inner(_font->width(_text), _font->height)
  154. , _outer(_inner.grownBy(_st.padding))
  155. , _stroke(st::lineWidth)
  156. , _skip(2 * _stroke)
  157. , _full(_outer + QSize(2 * _skip, _st.arrow + 2 * _skip))
  158. , _glareSize(_outer.height() * 3)
  159. , _glareRange(_outer.width() + _glareSize)
  160. , _glareDuration(_glareRange * kGlareDurationStep / _glareSize)
  161. , _glareTimer([=] { showGlare(); }) {
  162. resize(_full + QSize(0, _st.shift));
  163. setupGeometry(pointTo);
  164. }
  165. void Cover::BadgeTooltip::fade(bool shown) {
  166. if (_shown == shown) {
  167. return;
  168. }
  169. show();
  170. _shown = shown;
  171. _showAnimation.start([=] {
  172. update();
  173. if (!_showAnimation.animating()) {
  174. if (!_shown) {
  175. hide();
  176. } else {
  177. showGlare();
  178. }
  179. }
  180. }, _shown ? 0. : 1., _shown ? 1. : 0., _st.duration, anim::easeInCirc);
  181. }
  182. void Cover::BadgeTooltip::showGlare() {
  183. _glareAnimation.start([=] {
  184. update();
  185. if (!_glareAnimation.animating()) {
  186. _glareTimer.callOnce(kGlareTimeout);
  187. }
  188. }, 0., 1., _glareDuration);
  189. }
  190. void Cover::BadgeTooltip::finishAnimating() {
  191. _showAnimation.stop();
  192. if (!_shown) {
  193. hide();
  194. }
  195. }
  196. crl::time Cover::BadgeTooltip::glarePeriod() const {
  197. return _glareDuration + kGlareTimeout;
  198. }
  199. void Cover::BadgeTooltip::paintEvent(QPaintEvent *e) {
  200. const auto glare = _glareAnimation.value(0.);
  201. _glareRight = anim::interpolate(0, _glareRange, glare);
  202. prepareImage();
  203. auto p = QPainter(this);
  204. const auto shown = _showAnimation.value(_shown ? 1. : 0.);
  205. p.setOpacity(shown);
  206. const auto imageHeight = _image.height() / _image.devicePixelRatio();
  207. const auto top = anim::interpolate(0, height() - imageHeight, shown);
  208. p.drawImage(0, top, _image);
  209. }
  210. void Cover::BadgeTooltip::setupGeometry(not_null<QWidget*> pointTo) {
  211. auto widget = pointTo.get();
  212. const auto parent = parentWidget();
  213. const auto refresh = [=] {
  214. const auto rect = Ui::MapFrom(parent, pointTo, pointTo->rect());
  215. const auto point = QPoint(rect.center().x(), rect.y());
  216. const auto left = point.x() - (width() / 2);
  217. const auto skip = _st.padding.left();
  218. setGeometry(
  219. std::min(std::max(left, skip), parent->width() - width() - skip),
  220. std::max(point.y() - height() - _st.margin.bottom(), skip),
  221. width(),
  222. height());
  223. const auto arrowMiddle = point.x() - x();
  224. if (_arrowMiddle != arrowMiddle) {
  225. _arrowMiddle = arrowMiddle;
  226. update();
  227. }
  228. };
  229. refresh();
  230. while (widget && widget != parent) {
  231. base::install_event_filter(this, widget, [=](not_null<QEvent*> e) {
  232. if (e->type() == QEvent::Resize || e->type() == QEvent::Move || e->type() == QEvent::ZOrderChange) {
  233. refresh();
  234. raise();
  235. }
  236. return base::EventFilterResult::Continue;
  237. });
  238. widget = widget->parentWidget();
  239. }
  240. }
  241. void Cover::BadgeTooltip::prepareImage() {
  242. const auto ratio = style::DevicePixelRatio();
  243. const auto arrow = _st.arrow;
  244. const auto size = _full * ratio;
  245. if (_image.size() != size) {
  246. _image = QImage(size, QImage::Format_ARGB32_Premultiplied);
  247. _image.setDevicePixelRatio(ratio);
  248. } else if (_imageGlareRight == _glareRight
  249. && _imageArrowMiddle == _arrowMiddle) {
  250. return;
  251. }
  252. _imageGlareRight = _glareRight;
  253. _imageArrowMiddle = _arrowMiddle;
  254. _image.fill(Qt::transparent);
  255. const auto gfrom = _imageGlareRight - _glareSize;
  256. const auto gtill = _imageGlareRight;
  257. auto path = QPainterPath();
  258. const auto width = _outer.width();
  259. const auto height = _outer.height();
  260. const auto radius = (height + 1) / 2;
  261. const auto diameter = height;
  262. path.moveTo(radius, 0);
  263. path.lineTo(width - radius, 0);
  264. path.arcTo(
  265. QRect(QPoint(width - diameter, 0), QSize(diameter, diameter)),
  266. 90,
  267. -180);
  268. const auto xarrow = _arrowMiddle - _skip;
  269. if (xarrow - arrow <= radius || xarrow + arrow >= width - radius) {
  270. path.lineTo(radius, height);
  271. } else {
  272. path.lineTo(xarrow + arrow, height);
  273. path.lineTo(xarrow, height + arrow);
  274. path.lineTo(xarrow - arrow, height);
  275. path.lineTo(radius, height);
  276. }
  277. path.arcTo(
  278. QRect(QPoint(0, 0), QSize(diameter, diameter)),
  279. -90,
  280. -180);
  281. path.closeSubpath();
  282. auto p = QPainter(&_image);
  283. auto hq = PainterHighQualityEnabler(p);
  284. p.setPen(Qt::NoPen);
  285. if (gtill > 0) {
  286. auto gradient = QLinearGradient(gfrom, 0, gtill, 0);
  287. gradient.setStops({
  288. { 0., _collectible->edgeColor },
  289. { 0.5, _collectible->centerColor },
  290. { 1., _collectible->edgeColor },
  291. });
  292. p.setBrush(gradient);
  293. } else {
  294. p.setBrush(_collectible->edgeColor);
  295. }
  296. p.translate(_skip, _skip);
  297. p.drawPath(path);
  298. p.setCompositionMode(QPainter::CompositionMode_Source);
  299. p.setBrush(Qt::NoBrush);
  300. auto copy = _collectible->textColor;
  301. copy.setAlpha(0);
  302. if (gtill > 0) {
  303. auto gradient = QLinearGradient(gfrom, 0, gtill, 0);
  304. gradient.setStops({
  305. { 0., copy },
  306. { 0.5, _collectible->textColor },
  307. { 1., copy },
  308. });
  309. p.setPen(QPen(gradient, _stroke));
  310. } else {
  311. p.setPen(QPen(copy, _stroke));
  312. }
  313. p.drawPath(path);
  314. p.setCompositionMode(QPainter::CompositionMode_SourceOver);
  315. p.setFont(_font);
  316. p.setPen(QColor(255, 255, 255));
  317. p.drawText(_st.padding.left(), _st.padding.top() + _font->ascent, _text);
  318. }
  319. TopicIconView::TopicIconView(
  320. not_null<Data::ForumTopic*> topic,
  321. Fn<bool()> paused,
  322. Fn<void()> update)
  323. : TopicIconView(
  324. topic,
  325. std::move(paused),
  326. std::move(update),
  327. st::windowSubTextFg) {
  328. }
  329. TopicIconView::TopicIconView(
  330. not_null<Data::ForumTopic*> topic,
  331. Fn<bool()> paused,
  332. Fn<void()> update,
  333. const style::color &generalIconFg)
  334. : _topic(topic)
  335. , _generalIconFg(generalIconFg)
  336. , _paused(std::move(paused))
  337. , _update(std::move(update)) {
  338. setup(topic);
  339. }
  340. void TopicIconView::paintInRect(QPainter &p, QRect rect) {
  341. const auto paint = [&](const QImage &image) {
  342. const auto size = image.size() / style::DevicePixelRatio();
  343. p.drawImage(
  344. QRect(
  345. rect.x() + (rect.width() - size.width()) / 2,
  346. rect.y() + (rect.height() - size.height()) / 2,
  347. size.width(),
  348. size.height()),
  349. image);
  350. };
  351. if (_player && _player->ready()) {
  352. const auto colored = _playerUsesTextColor
  353. ? st::windowFg->c
  354. : QColor(0, 0, 0, 0);
  355. paint(_player->frame(
  356. st::infoTopicCover.photo.size,
  357. colored,
  358. false,
  359. crl::now(),
  360. _paused()).image);
  361. _player->markFrameShown();
  362. } else if (!_topic->iconId() && !_image.isNull()) {
  363. paint(_image);
  364. }
  365. }
  366. void TopicIconView::setup(not_null<Data::ForumTopic*> topic) {
  367. setupPlayer(topic);
  368. setupImage(topic);
  369. }
  370. void TopicIconView::setupPlayer(not_null<Data::ForumTopic*> topic) {
  371. IconIdValue(
  372. topic
  373. ) | rpl::map([=](DocumentId id) -> rpl::producer<DocumentData*> {
  374. if (!id) {
  375. return rpl::single((DocumentData*)nullptr);
  376. }
  377. return topic->owner().customEmojiManager().resolve(
  378. id
  379. ) | rpl::map([=](not_null<DocumentData*> document) {
  380. return document.get();
  381. }) | rpl::map_error_to_done();
  382. }) | rpl::flatten_latest(
  383. ) | rpl::map([=](DocumentData *document)
  384. -> rpl::producer<std::shared_ptr<StickerPlayer>> {
  385. if (!document) {
  386. return rpl::single(std::shared_ptr<StickerPlayer>());
  387. }
  388. const auto media = document->createMediaView();
  389. media->checkStickerLarge();
  390. media->goodThumbnailWanted();
  391. return rpl::single() | rpl::then(
  392. document->owner().session().downloaderTaskFinished()
  393. ) | rpl::filter([=] {
  394. return media->loaded();
  395. }) | rpl::take(1) | rpl::map([=] {
  396. auto result = std::shared_ptr<StickerPlayer>();
  397. const auto sticker = document->sticker();
  398. if (sticker->isLottie()) {
  399. result = std::make_shared<HistoryView::LottiePlayer>(
  400. ChatHelpers::LottiePlayerFromDocument(
  401. media.get(),
  402. ChatHelpers::StickerLottieSize::StickerSet,
  403. st::infoTopicCover.photo.size,
  404. Lottie::Quality::High));
  405. } else if (sticker->isWebm()) {
  406. result = std::make_shared<HistoryView::WebmPlayer>(
  407. media->owner()->location(),
  408. media->bytes(),
  409. st::infoTopicCover.photo.size);
  410. } else {
  411. result = std::make_shared<HistoryView::StaticStickerPlayer>(
  412. media->owner()->location(),
  413. media->bytes(),
  414. st::infoTopicCover.photo.size);
  415. }
  416. result->setRepaintCallback(_update);
  417. _playerUsesTextColor = media->owner()->emojiUsesTextColor();
  418. return result;
  419. });
  420. }) | rpl::flatten_latest(
  421. ) | rpl::start_with_next([=](std::shared_ptr<StickerPlayer> player) {
  422. _player = std::move(player);
  423. if (!_player) {
  424. _update();
  425. }
  426. }, _lifetime);
  427. }
  428. void TopicIconView::setupImage(not_null<Data::ForumTopic*> topic) {
  429. using namespace Data;
  430. if (topic->isGeneral()) {
  431. rpl::single(rpl::empty) | rpl::then(
  432. style::PaletteChanged()
  433. ) | rpl::start_with_next([=] {
  434. _image = ForumTopicGeneralIconFrame(
  435. st::infoForumTopicIcon.size,
  436. _generalIconFg->c);
  437. _update();
  438. }, _lifetime);
  439. return;
  440. }
  441. rpl::combine(
  442. TitleValue(topic),
  443. ColorIdValue(topic)
  444. ) | rpl::map([=](const QString &title, int32 colorId) {
  445. return ForumTopicIconFrame(colorId, title, st::infoForumTopicIcon);
  446. }) | rpl::start_with_next([=](QImage &&image) {
  447. _image = std::move(image);
  448. _update();
  449. }, _lifetime);
  450. }
  451. TopicIconButton::TopicIconButton(
  452. QWidget *parent,
  453. not_null<Window::SessionController*> controller,
  454. not_null<Data::ForumTopic*> topic)
  455. : TopicIconButton(parent, topic, [=] {
  456. return controller->isGifPausedAtLeastFor(Window::GifPauseReason::Layer);
  457. }) {
  458. }
  459. TopicIconButton::TopicIconButton(
  460. QWidget *parent,
  461. not_null<Data::ForumTopic*> topic,
  462. Fn<bool()> paused)
  463. : AbstractButton(parent)
  464. , _view(topic, paused, [=] { update(); }) {
  465. resize(st::infoTopicCover.photo.size);
  466. paintRequest(
  467. ) | rpl::start_with_next([=] {
  468. auto p = QPainter(this);
  469. _view.paintInRect(p, rect());
  470. }, lifetime());
  471. }
  472. Cover::Cover(
  473. QWidget *parent,
  474. not_null<Window::SessionController*> controller,
  475. not_null<PeerData*> peer,
  476. Fn<not_null<QWidget*>()> parentForTooltip)
  477. : Cover(
  478. parent,
  479. controller,
  480. peer,
  481. nullptr,
  482. Role::Info,
  483. NameValue(peer),
  484. parentForTooltip) {
  485. }
  486. Cover::Cover(
  487. QWidget *parent,
  488. not_null<Window::SessionController*> controller,
  489. not_null<Data::ForumTopic*> topic)
  490. : Cover(
  491. parent,
  492. controller,
  493. topic->channel(),
  494. topic,
  495. Role::Info,
  496. TitleValue(topic),
  497. nullptr) {
  498. }
  499. Cover::Cover(
  500. QWidget *parent,
  501. not_null<Window::SessionController*> controller,
  502. not_null<PeerData*> peer,
  503. Role role,
  504. rpl::producer<QString> title)
  505. : Cover(
  506. parent,
  507. controller,
  508. peer,
  509. nullptr,
  510. role,
  511. std::move(title),
  512. nullptr) {
  513. }
  514. [[nodiscard]] rpl::producer<Badge::Content> BotVerifyBadgeForPeer(
  515. not_null<PeerData*> peer) {
  516. return peer->session().changes().peerFlagsValue(
  517. peer,
  518. Data::PeerUpdate::Flag::VerifyInfo
  519. ) | rpl::map([=] {
  520. const auto info = peer->botVerifyDetails();
  521. return Badge::Content{
  522. .badge = info ? BadgeType::BotVerified : BadgeType::None,
  523. .emojiStatusId = { info ? info->iconId : DocumentId() },
  524. };
  525. });
  526. }
  527. Cover::Cover(
  528. QWidget *parent,
  529. not_null<Window::SessionController*> controller,
  530. not_null<PeerData*> peer,
  531. Data::ForumTopic *topic,
  532. Role role,
  533. rpl::producer<QString> title,
  534. Fn<not_null<QWidget*>()> parentForTooltip)
  535. : FixedHeightWidget(parent, CoverStyle(peer, topic, role).height)
  536. , _st(CoverStyle(peer, topic, role))
  537. , _role(role)
  538. , _controller(controller)
  539. , _peer(peer)
  540. , _emojiStatusPanel(peer->isSelf()
  541. ? std::make_unique<EmojiStatusPanel>()
  542. : nullptr)
  543. , _botVerify(
  544. std::make_unique<Badge>(
  545. this,
  546. st::infoPeerBadge,
  547. &peer->session(),
  548. BotVerifyBadgeForPeer(peer),
  549. nullptr,
  550. [=] {
  551. return controller->isGifPausedAtLeastFor(
  552. Window::GifPauseReason::Layer);
  553. }))
  554. , _badgeContent(BadgeContentForPeer(peer))
  555. , _badge(
  556. std::make_unique<Badge>(
  557. this,
  558. st::infoPeerBadge,
  559. &peer->session(),
  560. _badgeContent.value(),
  561. _emojiStatusPanel.get(),
  562. [=] {
  563. return controller->isGifPausedAtLeastFor(
  564. Window::GifPauseReason::Layer);
  565. }))
  566. , _verified(
  567. std::make_unique<Badge>(
  568. this,
  569. st::infoPeerBadge,
  570. &peer->session(),
  571. VerifiedContentForPeer(peer),
  572. _emojiStatusPanel.get(),
  573. [=] {
  574. return controller->isGifPausedAtLeastFor(
  575. Window::GifPauseReason::Layer);
  576. }))
  577. , _parentForTooltip(std::move(parentForTooltip))
  578. , _badgeTooltipHide([=] { hideBadgeTooltip(); })
  579. , _userpic(topic
  580. ? nullptr
  581. : object_ptr<Ui::UserpicButton>(
  582. this,
  583. controller,
  584. _peer,
  585. Ui::UserpicButton::Role::OpenPhoto,
  586. Ui::UserpicButton::Source::PeerPhoto,
  587. _st.photo))
  588. , _changePersonal((role == Role::Info
  589. || topic
  590. || !_peer->isUser()
  591. || _peer->isSelf()
  592. || _peer->asUser()->isBot())
  593. ? nullptr
  594. : CreateUploadSubButton(this, _peer->asUser(), controller).get())
  595. , _iconButton(topic
  596. ? object_ptr<TopicIconButton>(this, controller, topic)
  597. : nullptr)
  598. , _name(this, _st.name)
  599. , _status(this, _st.status)
  600. , _showLastSeen(this, tr::lng_status_lastseen_when(), _st.showLastSeen)
  601. , _refreshStatusTimer([this] { refreshStatusText(); }) {
  602. _peer->updateFull();
  603. _name->setSelectable(true);
  604. _name->setContextCopyText(tr::lng_profile_copy_fullname(tr::now));
  605. if (!_peer->isMegagroup()) {
  606. _status->setAttribute(Qt::WA_TransparentForMouseEvents);
  607. }
  608. setupShowLastSeen();
  609. _badge->setPremiumClickCallback([=] {
  610. if (const auto panel = _emojiStatusPanel.get()) {
  611. panel->show(_controller, _badge->widget(), _badge->sizeTag());
  612. } else {
  613. ::Settings::ShowEmojiStatusPremium(_controller, _peer);
  614. }
  615. });
  616. rpl::merge(
  617. _botVerify->updated(),
  618. _badge->updated(),
  619. _verified->updated()
  620. ) | rpl::start_with_next([=] {
  621. refreshNameGeometry(width());
  622. }, _name->lifetime());
  623. initViewers(std::move(title));
  624. setupChildGeometry();
  625. setupUniqueBadgeTooltip();
  626. if (_userpic) {
  627. } else if (topic->canEdit()) {
  628. _iconButton->setClickedCallback([=] {
  629. _controller->show(Box(
  630. EditForumTopicBox,
  631. _controller,
  632. topic->history(),
  633. topic->rootId()));
  634. });
  635. } else {
  636. _iconButton->setAttribute(Qt::WA_TransparentForMouseEvents);
  637. }
  638. }
  639. void Cover::setupShowLastSeen() {
  640. const auto user = _peer->asUser();
  641. if (_st.showLastSeenVisible
  642. && user
  643. && !user->isSelf()
  644. && !user->isBot()
  645. && !user->isServiceUser()
  646. && user->session().premiumPossible()) {
  647. if (user->session().premium()) {
  648. if (user->lastseen().isHiddenByMe()) {
  649. user->updateFullForced();
  650. }
  651. _showLastSeen->hide();
  652. return;
  653. }
  654. rpl::combine(
  655. user->session().changes().peerFlagsValue(
  656. user,
  657. Data::PeerUpdate::Flag::OnlineStatus),
  658. Data::AmPremiumValue(&user->session())
  659. ) | rpl::start_with_next([=](auto, bool premium) {
  660. const auto wasShown = !_showLastSeen->isHidden();
  661. const auto hiddenByMe = user->lastseen().isHiddenByMe();
  662. const auto shown = hiddenByMe
  663. && !user->lastseen().isOnline(base::unixtime::now())
  664. && !premium
  665. && user->session().premiumPossible();
  666. _showLastSeen->setVisible(shown);
  667. if (wasShown && premium && hiddenByMe) {
  668. user->updateFullForced();
  669. }
  670. }, _showLastSeen->lifetime());
  671. _controller->session().api().userPrivacy().value(
  672. Api::UserPrivacy::Key::LastSeen
  673. ) | rpl::filter([=](Api::UserPrivacy::Rule rule) {
  674. return (rule.option == Api::UserPrivacy::Option::Everyone);
  675. }) | rpl::start_with_next([=] {
  676. if (user->lastseen().isHiddenByMe()) {
  677. user->updateFullForced();
  678. }
  679. }, _showLastSeen->lifetime());
  680. } else {
  681. _showLastSeen->hide();
  682. }
  683. using TextTransform = Ui::RoundButton::TextTransform;
  684. _showLastSeen->setTextTransform(TextTransform::NoTransform);
  685. _showLastSeen->setFullRadius(true);
  686. _showLastSeen->setClickedCallback([=] {
  687. const auto type = Ui::ShowOrPremium::LastSeen;
  688. auto box = Box(Ui::ShowOrPremiumBox, type, user->shortName(), [=] {
  689. _controller->session().api().userPrivacy().save(
  690. ::Api::UserPrivacy::Key::LastSeen,
  691. {});
  692. }, [=] {
  693. ::Settings::ShowPremium(_controller, u"lastseen_hidden"_q);
  694. });
  695. _controller->show(std::move(box));
  696. });
  697. }
  698. void Cover::setupChildGeometry() {
  699. widthValue(
  700. ) | rpl::start_with_next([this](int newWidth) {
  701. if (_userpic) {
  702. _userpic->moveToLeft(_st.photoLeft, _st.photoTop, newWidth);
  703. } else {
  704. _iconButton->moveToLeft(_st.photoLeft, _st.photoTop, newWidth);
  705. }
  706. if (_changePersonal) {
  707. _changePersonal->moveToLeft(
  708. (_st.photoLeft
  709. + _st.photo.photoSize
  710. - _changePersonal->width()
  711. + st::infoEditContactPersonalLeft),
  712. (_userpic->y()
  713. + _userpic->height()
  714. - _changePersonal->height()));
  715. }
  716. refreshNameGeometry(newWidth);
  717. refreshStatusGeometry(newWidth);
  718. }, lifetime());
  719. }
  720. Cover *Cover::setOnlineCount(rpl::producer<int> &&count) {
  721. _onlineCount = std::move(count);
  722. return this;
  723. }
  724. std::optional<QImage> Cover::updatedPersonalPhoto() const {
  725. return _personalChosen;
  726. }
  727. void Cover::initViewers(rpl::producer<QString> title) {
  728. using Flag = Data::PeerUpdate::Flag;
  729. std::move(
  730. title
  731. ) | rpl::start_with_next([=](const QString &title) {
  732. _name->setText(title);
  733. refreshNameGeometry(width());
  734. }, lifetime());
  735. rpl::combine(
  736. _peer->session().changes().peerFlagsValue(
  737. _peer,
  738. Flag::OnlineStatus | Flag::Members),
  739. _onlineCount.value()
  740. ) | rpl::start_with_next([=] {
  741. refreshStatusText();
  742. }, lifetime());
  743. _peer->session().changes().peerFlagsValue(
  744. _peer,
  745. (_peer->isUser() ? Flag::IsContact : Flag::Rights)
  746. ) | rpl::start_with_next([=] {
  747. refreshUploadPhotoOverlay();
  748. }, lifetime());
  749. setupChangePersonal();
  750. }
  751. void Cover::refreshUploadPhotoOverlay() {
  752. if (!_userpic) {
  753. return;
  754. } else if (_role == Role::EditContact) {
  755. _userpic->setAttribute(Qt::WA_TransparentForMouseEvents);
  756. return;
  757. }
  758. const auto canChange = [&] {
  759. if (const auto chat = _peer->asChat()) {
  760. return chat->canEditInformation();
  761. } else if (const auto channel = _peer->asChannel()) {
  762. return channel->canEditInformation();
  763. } else if (const auto user = _peer->asUser()) {
  764. return user->isSelf()
  765. || (user->isContact()
  766. && !user->isInaccessible()
  767. && !user->isServiceUser());
  768. }
  769. Unexpected("Peer type in Info::Profile::Cover.");
  770. }();
  771. _userpic->switchChangePhotoOverlay(canChange, [=](
  772. Ui::UserpicButton::ChosenImage chosen) {
  773. using ChosenType = Ui::UserpicButton::ChosenType;
  774. auto result = Api::PeerPhoto::UserPhoto{
  775. base::take<QImage>(chosen.image), // Strange MSVC bug with take.
  776. chosen.markup.documentId,
  777. chosen.markup.colors,
  778. };
  779. switch (chosen.type) {
  780. case ChosenType::Set:
  781. _userpic->showCustom(base::duplicate(result.image));
  782. _peer->session().api().peerPhoto().upload(
  783. _peer,
  784. std::move(result));
  785. break;
  786. case ChosenType::Suggest:
  787. _peer->session().api().peerPhoto().suggest(
  788. _peer,
  789. std::move(result));
  790. break;
  791. }
  792. });
  793. const auto canReport = [=, peer = _peer] {
  794. if (!peer->hasUserpic()) {
  795. return false;
  796. }
  797. const auto user = peer->asUser();
  798. if (!user) {
  799. if (canChange) {
  800. return false;
  801. }
  802. } else if (user->hasPersonalPhoto()
  803. || user->isSelf()
  804. || user->isInaccessible()
  805. || user->isRepliesChat()
  806. || user->isVerifyCodes()
  807. || (user->botInfo && user->botInfo->canEditInformation)
  808. || user->isServiceUser()) {
  809. return false;
  810. }
  811. return true;
  812. };
  813. const auto contextMenu = _userpic->lifetime()
  814. .make_state<base::unique_qptr<Ui::PopupMenu>>();
  815. const auto showMenu = [=, peer = _peer, controller = _controller](
  816. not_null<Ui::RpWidget*> parent) {
  817. if (!canReport()) {
  818. return false;
  819. }
  820. *contextMenu = base::make_unique_q<Ui::PopupMenu>(
  821. parent,
  822. st::popupMenuWithIcons);
  823. contextMenu->get()->addAction(tr::lng_profile_report(tr::now), [=] {
  824. controller->show(
  825. ReportProfilePhotoBox(
  826. peer,
  827. peer->owner().photo(peer->userpicPhotoId())),
  828. Ui::LayerOption::CloseOther);
  829. }, &st::menuIconReport);
  830. contextMenu->get()->popup(QCursor::pos());
  831. return true;
  832. };
  833. base::install_event_filter(_userpic, [showMenu, raw = _userpic.data()](
  834. not_null<QEvent*> e) {
  835. return (e->type() == QEvent::ContextMenu && showMenu(raw))
  836. ? base::EventFilterResult::Cancel
  837. : base::EventFilterResult::Continue;
  838. });
  839. if (const auto user = _peer->asUser()) {
  840. _userpic->resetPersonalRequests(
  841. ) | rpl::start_with_next([=] {
  842. user->session().api().peerPhoto().clearPersonal(user);
  843. _userpic->showSource(Ui::UserpicButton::Source::PeerPhoto);
  844. }, lifetime());
  845. }
  846. }
  847. void Cover::setupChangePersonal() {
  848. if (!_changePersonal) {
  849. return;
  850. }
  851. _changePersonal->chosenImages(
  852. ) | rpl::start_with_next([=](Ui::UserpicButton::ChosenImage &&chosen) {
  853. if (chosen.type == Ui::UserpicButton::ChosenType::Suggest) {
  854. _peer->session().api().peerPhoto().suggest(
  855. _peer,
  856. {
  857. std::move(chosen.image),
  858. chosen.markup.documentId,
  859. chosen.markup.colors,
  860. });
  861. } else {
  862. _personalChosen = std::move(chosen.image);
  863. _userpic->showCustom(base::duplicate(*_personalChosen));
  864. _changePersonal->overrideHasPersonalPhoto(true);
  865. _changePersonal->showSource(
  866. Ui::UserpicButton::Source::NonPersonalIfHasPersonal);
  867. }
  868. }, _changePersonal->lifetime());
  869. _changePersonal->resetPersonalRequests(
  870. ) | rpl::start_with_next([=] {
  871. _personalChosen = QImage();
  872. _userpic->showSource(
  873. Ui::UserpicButton::Source::NonPersonalPhoto);
  874. _changePersonal->overrideHasPersonalPhoto(false);
  875. _changePersonal->showCustom(QImage());
  876. }, _changePersonal->lifetime());
  877. }
  878. void Cover::refreshStatusText() {
  879. auto hasMembersLink = [&] {
  880. if (auto megagroup = _peer->asMegagroup()) {
  881. return megagroup->canViewMembers();
  882. }
  883. return false;
  884. }();
  885. auto statusText = [&]() -> TextWithEntities {
  886. using namespace Ui::Text;
  887. auto currentTime = base::unixtime::now();
  888. if (auto user = _peer->asUser()) {
  889. const auto result = Data::OnlineTextFull(user, currentTime);
  890. const auto showOnline = Data::OnlineTextActive(user, currentTime);
  891. const auto updateIn = Data::OnlineChangeTimeout(user, currentTime);
  892. if (showOnline) {
  893. _refreshStatusTimer.callOnce(updateIn);
  894. }
  895. return showOnline
  896. ? Ui::Text::Colorized(result)
  897. : TextWithEntities{ .text = result };
  898. } else if (auto chat = _peer->asChat()) {
  899. if (!chat->amIn()) {
  900. return tr::lng_chat_status_unaccessible({}, WithEntities);
  901. }
  902. const auto onlineCount = _onlineCount.current();
  903. const auto fullCount = std::max(
  904. chat->count,
  905. int(chat->participants.size()));
  906. return { .text = ChatStatusText(fullCount, onlineCount, true) };
  907. } else if (auto channel = _peer->asChannel()) {
  908. const auto onlineCount = _onlineCount.current();
  909. const auto fullCount = qMax(channel->membersCount(), 1);
  910. auto result = ChatStatusText(
  911. fullCount,
  912. onlineCount,
  913. channel->isMegagroup());
  914. return hasMembersLink
  915. ? Ui::Text::Link(result)
  916. : TextWithEntities{ .text = result };
  917. }
  918. return tr::lng_chat_status_unaccessible(tr::now, WithEntities);
  919. }();
  920. _status->setMarkedText(statusText);
  921. if (hasMembersLink) {
  922. _status->setLink(1, std::make_shared<LambdaClickHandler>([=] {
  923. _showSection.fire(Section::Type::Members);
  924. }));
  925. }
  926. refreshStatusGeometry(width());
  927. }
  928. Cover::~Cover() {
  929. base::take(_badgeTooltip);
  930. base::take(_badgeOldTooltips);
  931. }
  932. void Cover::refreshNameGeometry(int newWidth) {
  933. auto nameWidth = newWidth - _st.nameLeft - _st.rightSkip;
  934. const auto verifiedWidget = _verified->widget();
  935. const auto badgeWidget = _badge->widget();
  936. if (verifiedWidget) {
  937. nameWidth -= verifiedWidget->width();
  938. }
  939. if (badgeWidget) {
  940. nameWidth -= badgeWidget->width();
  941. }
  942. if (verifiedWidget || badgeWidget) {
  943. nameWidth -= st::infoVerifiedCheckPosition.x();
  944. }
  945. auto nameLeft = _st.nameLeft;
  946. const auto badgeTop = _st.nameTop;
  947. const auto badgeBottom = _st.nameTop + _name->height();
  948. const auto margins = LargeCustomEmojiMargins();
  949. _botVerify->move(nameLeft - margins.left(), badgeTop, badgeBottom);
  950. if (const auto widget = _botVerify->widget()) {
  951. const auto skip = widget->width()
  952. + st::infoVerifiedCheckPosition.x();
  953. nameLeft += skip;
  954. nameWidth -= skip;
  955. }
  956. _name->resizeToNaturalWidth(nameWidth);
  957. _name->moveToLeft(nameLeft, _st.nameTop, newWidth);
  958. const auto badgeLeft = nameLeft + _name->width();
  959. _badge->move(badgeLeft, badgeTop, badgeBottom);
  960. _verified->move(
  961. badgeLeft + (badgeWidget ? badgeWidget->width() : 0),
  962. badgeTop,
  963. badgeBottom);
  964. }
  965. void Cover::refreshStatusGeometry(int newWidth) {
  966. auto statusWidth = newWidth - _st.statusLeft - _st.rightSkip;
  967. _status->resizeToWidth(statusWidth);
  968. _status->moveToLeft(_st.statusLeft, _st.statusTop, newWidth);
  969. const auto left = _st.statusLeft + _status->textMaxWidth();
  970. _showLastSeen->moveToLeft(
  971. left + _st.showLastSeenPosition.x(),
  972. _st.showLastSeenPosition.y(),
  973. newWidth);
  974. }
  975. void Cover::hideBadgeTooltip() {
  976. _badgeTooltipHide.cancel();
  977. if (auto old = base::take(_badgeTooltip)) {
  978. const auto raw = old.get();
  979. _badgeOldTooltips.push_back(std::move(old));
  980. raw->fade(false);
  981. raw->shownValue(
  982. ) | rpl::filter(
  983. !rpl::mappers::_1
  984. ) | rpl::start_with_next([=] {
  985. const auto i = ranges::find(
  986. _badgeOldTooltips,
  987. raw,
  988. &std::unique_ptr<BadgeTooltip>::get);
  989. if (i != end(_badgeOldTooltips)) {
  990. _badgeOldTooltips.erase(i);
  991. }
  992. }, raw->lifetime());
  993. }
  994. }
  995. void Cover::setupUniqueBadgeTooltip() {
  996. base::timer_once(kWaitBeforeGiftBadge) | rpl::then(
  997. _badge->updated()
  998. ) | rpl::start_with_next([=] {
  999. const auto widget = _badge->widget();
  1000. const auto &content = _badgeContent.current();
  1001. const auto &collectible = content.emojiStatusId.collectible;
  1002. const auto premium = (content.badge == BadgeType::Premium);
  1003. const auto id = (collectible && widget && premium)
  1004. ? collectible->id
  1005. : uint64();
  1006. if (_badgeCollectibleId == id) {
  1007. return;
  1008. }
  1009. hideBadgeTooltip();
  1010. if (!collectible) {
  1011. return;
  1012. }
  1013. const auto parent = _parentForTooltip
  1014. ? _parentForTooltip()
  1015. : _controller->window().widget()->bodyWidget();
  1016. _badgeTooltip = std::make_unique<BadgeTooltip>(
  1017. parent,
  1018. collectible,
  1019. widget);
  1020. const auto raw = _badgeTooltip.get();
  1021. raw->fade(true);
  1022. _badgeTooltipHide.callOnce(kGiftBadgeGlares * raw->glarePeriod()
  1023. - st::infoGiftTooltip.duration * 1.5);
  1024. }, lifetime());
  1025. if (const auto raw = _badgeTooltip.get()) {
  1026. raw->finishAnimating();
  1027. }
  1028. }
  1029. } // namespace Info::Profile