window_history_hider.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 "window/window_history_hider.h"
  8. #include "lang/lang_keys.h"
  9. #include "ui/widgets/buttons.h"
  10. #include "ui/widgets/shadow.h"
  11. #include "ui/cached_round_corners.h"
  12. #include "mainwidget.h"
  13. #include "styles/style_layers.h"
  14. #include "styles/style_chat.h"
  15. namespace Window {
  16. HistoryHider::HistoryHider(QWidget *parent, const QString &text)
  17. : RpWidget(parent)
  18. , _text(text) {
  19. Lang::Updated(
  20. ) | rpl::start_with_next([=] {
  21. refreshLang();
  22. }, lifetime());
  23. _chooseWidth = st::historyForwardChooseFont->width(_text);
  24. resizeEvent(0);
  25. _a_opacity.start([=] { update(); }, 0., 1., st::boxDuration);
  26. }
  27. HistoryHider::~HistoryHider() = default;
  28. void HistoryHider::refreshLang() {
  29. InvokeQueued(this, [this] { updateControlsGeometry(); });
  30. }
  31. void HistoryHider::paintEvent(QPaintEvent *e) {
  32. auto p = QPainter(this);
  33. auto opacity = _a_opacity.value(_hiding ? 0. : 1.);
  34. if (opacity == 0.) {
  35. if (_hiding) {
  36. _hidden.fire({});
  37. }
  38. return;
  39. }
  40. p.setOpacity(opacity);
  41. p.fillRect(rect(), st::layerBg);
  42. p.setFont(st::historyForwardChooseFont);
  43. auto w = st::historyForwardChooseMargins.left() + _chooseWidth + st::historyForwardChooseMargins.right();
  44. auto h = st::historyForwardChooseMargins.top() + st::historyForwardChooseFont->height + st::historyForwardChooseMargins.bottom();
  45. Ui::FillRoundRect(p, (width() - w) / 2, (height() - h) / 2, w, h, st::historyForwardChooseBg, Ui::ForwardCorners);
  46. p.setPen(st::historyForwardChooseFg);
  47. p.drawText(_box, _text, QTextOption(style::al_center));
  48. }
  49. void HistoryHider::keyPressEvent(QKeyEvent *e) {
  50. if (e->key() == Qt::Key_Escape) {
  51. startHide();
  52. }
  53. }
  54. void HistoryHider::mousePressEvent(QMouseEvent *e) {
  55. if (e->button() == Qt::LeftButton) {
  56. if (!_box.contains(e->pos())) {
  57. startHide();
  58. }
  59. }
  60. }
  61. void HistoryHider::startHide() {
  62. if (_hiding) return;
  63. _hiding = true;
  64. _a_opacity.start([=] { animationCallback(); }, 1., 0., st::boxDuration);
  65. }
  66. void HistoryHider::animationCallback() {
  67. update();
  68. if (!_a_opacity.animating() && _hiding) {
  69. crl::on_main(this, [=] { _hidden.fire({}); });
  70. }
  71. }
  72. rpl::producer<> HistoryHider::hidden() const {
  73. return _hidden.events();
  74. }
  75. void HistoryHider::resizeEvent(QResizeEvent *e) {
  76. updateControlsGeometry();
  77. }
  78. void HistoryHider::updateControlsGeometry() {
  79. auto w = st::boxWidth;
  80. auto h = st::boxPadding.top() + st::boxPadding.bottom();
  81. h += st::historyForwardChooseFont->height;
  82. _box = QRect((width() - w) / 2, (height() - h) / 2, w, h);
  83. }
  84. } // namespace Window