group_call_bar.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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/group_call_bar.h"
  8. #include "ui/chat/group_call_userpics.h"
  9. #include "ui/widgets/shadow.h"
  10. #include "ui/widgets/buttons.h"
  11. #include "ui/painter.h"
  12. #include "lang/lang_keys.h"
  13. #include "base/unixtime.h"
  14. #include "styles/style_chat.h"
  15. #include "styles/style_chat_helpers.h"
  16. #include "styles/style_calls.h"
  17. #include "styles/style_info.h" // st::topBarArrowPadding, like TopBarWidget.
  18. #include "styles/style_window.h" // st::columnMinimalWidthLeft
  19. #include "styles/palette.h"
  20. #include <QtGui/QtEvents>
  21. #include <QtCore/QLocale>
  22. namespace Ui {
  23. GroupCallScheduledLeft::GroupCallScheduledLeft(TimeId date)
  24. : _date(date)
  25. , _datePrecise(computePreciseDate())
  26. , _timer([=] { update(); }) {
  27. update();
  28. base::unixtime::updates(
  29. ) | rpl::start_with_next([=] {
  30. restart();
  31. }, _lifetime);
  32. }
  33. crl::time GroupCallScheduledLeft::computePreciseDate() const {
  34. return crl::now() + (_date - base::unixtime::now()) * crl::time(1000);
  35. }
  36. void GroupCallScheduledLeft::setDate(TimeId date) {
  37. if (_date == date) {
  38. return;
  39. }
  40. _date = date;
  41. restart();
  42. }
  43. void GroupCallScheduledLeft::restart() {
  44. _datePrecise = computePreciseDate();
  45. _timer.cancel();
  46. update();
  47. }
  48. rpl::producer<QString> GroupCallScheduledLeft::text(Negative negative) const {
  49. return (negative == Negative::Show)
  50. ? _text.value()
  51. : _textNonNegative.value();
  52. }
  53. rpl::producer<bool> GroupCallScheduledLeft::late() const {
  54. return _late.value();
  55. }
  56. void GroupCallScheduledLeft::update() {
  57. const auto now = crl::now();
  58. const auto duration = (_datePrecise - now);
  59. const auto left = crl::time(base::SafeRound(std::abs(duration) / 1000.));
  60. const auto late = (duration < 0) && (left > 0);
  61. _late = late;
  62. constexpr auto kDay = 24 * 60 * 60;
  63. if (left >= kDay) {
  64. const auto days = (left / kDay);
  65. _textNonNegative = tr::lng_days(tr::now, lt_count, days);
  66. _text = late
  67. ? tr::lng_days(tr::now, lt_count, -days)
  68. : _textNonNegative.current();
  69. } else {
  70. const auto hours = left / (60 * 60);
  71. const auto minutes = (left % (60 * 60)) / 60;
  72. const auto seconds = (left % 60);
  73. _textNonNegative = (hours > 0)
  74. ? (u"%1:%2:%3"_q
  75. .arg(hours, 2, 10, QChar('0'))
  76. .arg(minutes, 2, 10, QChar('0'))
  77. .arg(seconds, 2, 10, QChar('0')))
  78. : (u"%1:%2"_q
  79. .arg(minutes, 2, 10, QChar('0'))
  80. .arg(seconds, 2, 10, QChar('0')));
  81. _text = (late ? QString(QChar(0x2212)) : QString())
  82. + _textNonNegative.current();
  83. }
  84. if (left >= kDay) {
  85. _timer.callOnce((left % kDay) * crl::time(1000));
  86. } else {
  87. const auto fraction = (std::abs(duration) + 500) % 1000;
  88. if (fraction < 400 || fraction > 600) {
  89. const auto next = std::abs(duration) % 1000;
  90. _timer.callOnce((duration < 0) ? (1000 - next) : next);
  91. } else if (!_timer.isActive()) {
  92. _timer.callEach(1000);
  93. }
  94. }
  95. }
  96. GroupCallBar::GroupCallBar(
  97. not_null<QWidget*> parent,
  98. rpl::producer<GroupCallBarContent> content,
  99. rpl::producer<bool> &&hideBlobs)
  100. : _wrap(parent, object_ptr<RpWidget>(parent))
  101. , _inner(_wrap.entity())
  102. , _shadow(std::make_unique<PlainShadow>(_wrap.parentWidget()))
  103. , _userpics(std::make_unique<GroupCallUserpics>(
  104. st::historyGroupCallUserpics,
  105. std::move(hideBlobs),
  106. [=] { updateUserpics(); })) {
  107. _wrap.hide(anim::type::instant);
  108. _shadow->hide();
  109. _wrap.entity()->paintRequest(
  110. ) | rpl::start_with_next([=](QRect clip) {
  111. QPainter(_wrap.entity()).fillRect(clip, st::historyPinnedBg);
  112. }, lifetime());
  113. _wrap.setAttribute(Qt::WA_OpaquePaintEvent);
  114. auto copy = std::move(
  115. content
  116. ) | rpl::start_spawning(_wrap.lifetime());
  117. rpl::duplicate(
  118. copy
  119. ) | rpl::start_with_next([=](GroupCallBarContent &&content) {
  120. _content = content;
  121. _userpics->update(_content.users, !_wrap.isHidden());
  122. _inner->update();
  123. refreshScheduledProcess();
  124. }, lifetime());
  125. if (!_open && !_join) {
  126. refreshScheduledProcess();
  127. }
  128. std::move(
  129. copy
  130. ) | rpl::map([=](const GroupCallBarContent &content) {
  131. return !content.shown;
  132. }) | rpl::start_with_next_done([=](bool hidden) {
  133. _shouldBeShown = !hidden;
  134. if (!_forceHidden) {
  135. _wrap.toggle(_shouldBeShown, anim::type::normal);
  136. }
  137. }, [=] {
  138. _forceHidden = true;
  139. _wrap.toggle(false, anim::type::normal);
  140. }, lifetime());
  141. setupInner();
  142. }
  143. GroupCallBar::~GroupCallBar() = default;
  144. void GroupCallBar::refreshOpenBrush() {
  145. Expects(_open != nullptr);
  146. const auto width = _open->width();
  147. if (_openBrushForWidth == width) {
  148. return;
  149. }
  150. auto gradient = QLinearGradient(QPoint(width, 0), QPoint(0, 0));
  151. gradient.setStops(QGradientStops{
  152. { 0.0, st::groupCallForceMutedBar1->c },
  153. { .7, st::groupCallForceMutedBar2->c },
  154. { 1.0, st::groupCallForceMutedBar3->c }
  155. });
  156. _openBrushOverride = QBrush(std::move(gradient));
  157. _openBrushForWidth = width;
  158. _open->setBrushOverride(_openBrushOverride);
  159. }
  160. void GroupCallBar::refreshScheduledProcess() {
  161. const auto date = _content.scheduleDate;
  162. if (!date) {
  163. if (_scheduledProcess) {
  164. _scheduledProcess = nullptr;
  165. _open = nullptr;
  166. _openBrushForWidth = 0;
  167. }
  168. if (!_join) {
  169. _join = std::make_unique<RoundButton>(
  170. _inner.get(),
  171. tr::lng_group_call_join(),
  172. st::groupCallTopBarJoin);
  173. setupRightButton(_join.get());
  174. }
  175. } else if (!_scheduledProcess) {
  176. _scheduledProcess = std::make_unique<GroupCallScheduledLeft>(date);
  177. _join = nullptr;
  178. _open = std::make_unique<RoundButton>(
  179. _inner.get(),
  180. _scheduledProcess->text(GroupCallScheduledLeft::Negative::Show),
  181. st::groupCallTopBarOpen);
  182. setupRightButton(_open.get());
  183. _open->widthValue(
  184. ) | rpl::start_with_next([=] {
  185. refreshOpenBrush();
  186. }, _open->lifetime());
  187. } else {
  188. _scheduledProcess->setDate(date);
  189. }
  190. }
  191. void GroupCallBar::setupInner() {
  192. _inner->resize(0, st::historyReplyHeight);
  193. _inner->paintRequest(
  194. ) | rpl::start_with_next([=](QRect rect) {
  195. auto p = Painter(_inner);
  196. paint(p);
  197. }, _inner->lifetime());
  198. // Clicks.
  199. _inner->setCursor(style::cur_pointer);
  200. _inner->events(
  201. ) | rpl::filter([=](not_null<QEvent*> event) {
  202. return (event->type() == QEvent::MouseButtonPress)
  203. && (static_cast<QMouseEvent*>(event.get())->button()
  204. == Qt::LeftButton);
  205. }) | rpl::map([=] {
  206. return _inner->events(
  207. ) | rpl::filter([=](not_null<QEvent*> event) {
  208. return (event->type() == QEvent::MouseButtonRelease);
  209. }) | rpl::take(1) | rpl::filter([=](not_null<QEvent*> event) {
  210. return _inner->rect().contains(
  211. static_cast<QMouseEvent*>(event.get())->pos());
  212. });
  213. }) | rpl::flatten_latest(
  214. ) | rpl::to_empty | rpl::start_to_stream(_barClicks, _inner->lifetime());
  215. _wrap.geometryValue(
  216. ) | rpl::start_with_next([=](QRect rect) {
  217. updateShadowGeometry(rect);
  218. updateControlsGeometry(rect);
  219. }, _inner->lifetime());
  220. }
  221. void GroupCallBar::setupRightButton(not_null<RoundButton*> button) {
  222. button->setFullRadius(true);
  223. rpl::combine(
  224. _inner->widthValue(),
  225. button->widthValue()
  226. ) | rpl::start_with_next([=](int outerWidth, int buttonWidth) {
  227. // Skip shadow of the bar above.
  228. const auto top = (st::historyReplyHeight
  229. - st::lineWidth
  230. - button->height()) / 2 + st::lineWidth;
  231. const auto narrow = (outerWidth < st::columnMinimalWidthLeft / 2);
  232. if (narrow) {
  233. button->moveToLeft(
  234. (outerWidth - buttonWidth) / 2,
  235. top,
  236. outerWidth);
  237. } else {
  238. button->moveToRight(top, top, outerWidth);
  239. }
  240. }, button->lifetime());
  241. button->clicks() | rpl::start_to_stream(_joinClicks, button->lifetime());
  242. }
  243. void GroupCallBar::paint(Painter &p) {
  244. p.fillRect(_inner->rect(), st::historyComposeAreaBg);
  245. const auto narrow = (_inner->width() < st::columnMinimalWidthLeft / 2);
  246. if (!narrow) {
  247. paintTitleAndStatus(p);
  248. paintUserpics(p);
  249. }
  250. }
  251. void GroupCallBar::paintTitleAndStatus(Painter &p) {
  252. const auto left = st::topBarArrowPadding.right();
  253. const auto titleTop = st::msgReplyPadding.top();
  254. const auto textTop = titleTop + st::msgServiceNameFont->height;
  255. const auto width = _inner->width();
  256. const auto &font = st::defaultMessageBar.title.font;
  257. p.setPen(st::defaultMessageBar.textFg);
  258. p.setFont(font);
  259. const auto available = (_join ? _join->x() : _open->x()) - left;
  260. const auto titleWidth = font->width(_content.title);
  261. p.drawTextLeft(
  262. left,
  263. titleTop,
  264. width,
  265. (!_content.scheduleDate
  266. ? (_content.livestream
  267. ? tr::lng_group_call_title_channel
  268. : tr::lng_group_call_title)(tr::now)
  269. : _content.title.isEmpty()
  270. ? (_content.livestream
  271. ? tr::lng_group_call_scheduled_title_channel
  272. : tr::lng_group_call_scheduled_title)(tr::now)
  273. : (titleWidth > available)
  274. ? font->elided(_content.title, available)
  275. : _content.title));
  276. p.setPen(st::historyStatusFg);
  277. p.setFont(st::defaultMessageBar.text.font);
  278. const auto when = [&] {
  279. if (!_content.scheduleDate) {
  280. return QString();
  281. }
  282. const auto parsed = base::unixtime::parse(_content.scheduleDate);
  283. const auto date = parsed.date();
  284. const auto time = QLocale().toString(
  285. parsed.time(),
  286. QLocale::ShortFormat);
  287. const auto today = QDate::currentDate();
  288. if (date == today) {
  289. return tr::lng_group_call_starts_today(tr::now, lt_time, time);
  290. } else if (date == today.addDays(1)) {
  291. return tr::lng_group_call_starts_tomorrow(
  292. tr::now,
  293. lt_time,
  294. time);
  295. } else {
  296. return tr::lng_group_call_starts_date(
  297. tr::now,
  298. lt_date,
  299. langDayOfMonthFull(date),
  300. lt_time,
  301. time);
  302. }
  303. }();
  304. p.drawTextLeft(
  305. left,
  306. textTop,
  307. width,
  308. (_content.scheduleDate
  309. ? (_content.title.isEmpty()
  310. ? tr::lng_group_call_starts_short
  311. : _content.livestream
  312. ? tr::lng_group_call_starts_channel
  313. : tr::lng_group_call_starts)(tr::now, lt_when, when)
  314. : _content.count > 0
  315. ? tr::lng_group_call_members(
  316. tr::now,
  317. lt_count_decimal,
  318. _content.count)
  319. : tr::lng_group_call_no_members(tr::now)));
  320. }
  321. void GroupCallBar::paintUserpics(Painter &p) {
  322. const auto size = st::historyGroupCallUserpics.size;
  323. // Skip shadow of the bar above.
  324. const auto top = (st::historyReplyHeight - st::lineWidth - size) / 2
  325. + st::lineWidth;
  326. _userpics->paint(p, _inner->width() / 2, top, size);
  327. }
  328. void GroupCallBar::updateControlsGeometry(QRect wrapGeometry) {
  329. const auto hidden = _wrap.isHidden() || !wrapGeometry.height();
  330. if (_shadow->isHidden() != hidden) {
  331. _shadow->setVisible(!hidden);
  332. }
  333. }
  334. void GroupCallBar::setShadowGeometryPostprocess(Fn<QRect(QRect)> postprocess) {
  335. _shadowGeometryPostprocess = std::move(postprocess);
  336. updateShadowGeometry(_wrap.geometry());
  337. }
  338. void GroupCallBar::updateShadowGeometry(QRect wrapGeometry) {
  339. const auto regular = QRect(
  340. wrapGeometry.x(),
  341. wrapGeometry.y() + wrapGeometry.height(),
  342. wrapGeometry.width(),
  343. st::lineWidth);
  344. _shadow->setGeometry(_shadowGeometryPostprocess
  345. ? _shadowGeometryPostprocess(regular)
  346. : regular);
  347. }
  348. void GroupCallBar::updateUserpics() {
  349. const auto widget = _wrap.entity();
  350. const auto middle = widget->width() / 2;
  351. const auto width = _userpics->maxWidth();
  352. widget->update(
  353. (middle - width / 2),
  354. 0,
  355. width,
  356. widget->height());
  357. }
  358. void GroupCallBar::show() {
  359. if (!_forceHidden) {
  360. return;
  361. }
  362. _forceHidden = false;
  363. if (_shouldBeShown) {
  364. _wrap.show(anim::type::instant);
  365. _shadow->show();
  366. }
  367. }
  368. void GroupCallBar::hide() {
  369. if (_forceHidden) {
  370. return;
  371. }
  372. _forceHidden = true;
  373. _wrap.hide(anim::type::instant);
  374. _shadow->hide();
  375. }
  376. void GroupCallBar::raise() {
  377. _wrap.raise();
  378. _shadow->raise();
  379. }
  380. void GroupCallBar::finishAnimating() {
  381. _wrap.finishAnimating();
  382. }
  383. void GroupCallBar::move(int x, int y) {
  384. _wrap.move(x, y);
  385. }
  386. void GroupCallBar::resizeToWidth(int width) {
  387. _wrap.entity()->resizeToWidth(width);
  388. _inner->resizeToWidth(width);
  389. }
  390. int GroupCallBar::height() const {
  391. return !_forceHidden
  392. ? _wrap.height()
  393. : _shouldBeShown
  394. ? st::historyReplyHeight
  395. : 0;
  396. }
  397. rpl::producer<int> GroupCallBar::heightValue() const {
  398. return _wrap.heightValue();
  399. }
  400. rpl::producer<> GroupCallBar::barClicks() const {
  401. return _barClicks.events();
  402. }
  403. rpl::producer<> GroupCallBar::joinClicks() const {
  404. using namespace rpl::mappers;
  405. return _joinClicks.events()
  406. | rpl::filter(_1 == Qt::LeftButton)
  407. | rpl::to_empty;
  408. }
  409. } // namespace Ui