silent_toggle.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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/controls/silent_toggle.h"
  8. #include "ui/effects/ripple_animation.h"
  9. #include "data/notify/data_notify_settings.h"
  10. #include "data/data_session.h"
  11. #include "data/data_channel.h"
  12. #include "lang/lang_keys.h"
  13. #include "ui/ui_utility.h"
  14. #include "styles/style_chat.h"
  15. #include "styles/style_chat_helpers.h"
  16. namespace Ui {
  17. SilentToggle::SilentToggle(QWidget *parent, not_null<ChannelData*> channel)
  18. : RippleButton(parent, st::historySilentToggle.ripple)
  19. , _st(st::historySilentToggle)
  20. , _channel(channel)
  21. , _checked(channel->owner().notifySettings().silentPosts(_channel)) {
  22. Expects(!channel->owner().notifySettings().silentPostsUnknown(_channel));
  23. resize(_st.width, _st.height);
  24. setMouseTracking(true);
  25. clicks(
  26. ) | rpl::start_with_next([=] {
  27. setChecked(!_checked);
  28. Ui::Tooltip::Show(0, this);
  29. _channel->owner().notifySettings().update(_channel, {}, _checked);
  30. }, lifetime());
  31. }
  32. void SilentToggle::paintEvent(QPaintEvent *e) {
  33. auto p = QPainter(this);
  34. paintRipple(p, _st.rippleAreaPosition, nullptr);
  35. //const auto checked = _crossLineAnimation.value(_checked ? 1. : 0.);
  36. const auto over = isOver();
  37. (_checked
  38. ? (over
  39. ? st::historySilentToggleOnOver
  40. : st::historySilentToggleOn)
  41. : (over
  42. ? st::historySilentToggle.iconOver
  43. : st::historySilentToggle.icon)).paintInCenter(p, rect());
  44. }
  45. void SilentToggle::mouseMoveEvent(QMouseEvent *e) {
  46. RippleButton::mouseMoveEvent(e);
  47. if (rect().contains(e->pos())) {
  48. Ui::Tooltip::Show(1000, this);
  49. } else {
  50. Ui::Tooltip::Hide();
  51. }
  52. }
  53. void SilentToggle::setChecked(bool checked) {
  54. if (_checked != checked) {
  55. _checked = checked;
  56. update();
  57. // _crossLineAnimation.start(
  58. // [=] { update(); },
  59. // _checked ? 0. : 1.,
  60. // _checked ? 1. : 0.,
  61. // kAnimationDuration);
  62. }
  63. }
  64. void SilentToggle::leaveEventHook(QEvent *e) {
  65. RippleButton::leaveEventHook(e);
  66. Ui::Tooltip::Hide();
  67. }
  68. QString SilentToggle::tooltipText() const {
  69. return _checked
  70. ? tr::lng_wont_be_notified(tr::now)
  71. : tr::lng_will_be_notified(tr::now);
  72. }
  73. QPoint SilentToggle::tooltipPos() const {
  74. return QCursor::pos();
  75. }
  76. bool SilentToggle::tooltipWindowActive() const {
  77. return Ui::AppInFocus() && InFocusChain(window());
  78. }
  79. QPoint SilentToggle::prepareRippleStartPosition() const {
  80. const auto result = mapFromGlobal(QCursor::pos())
  81. - _st.rippleAreaPosition;
  82. const auto rect = QRect(0, 0, _st.rippleAreaSize, _st.rippleAreaSize);
  83. return rect.contains(result)
  84. ? result
  85. : DisabledRippleStartPosition();
  86. }
  87. QImage SilentToggle::prepareRippleMask() const {
  88. return RippleAnimation::EllipseMask(
  89. QSize(_st.rippleAreaSize, _st.rippleAreaSize));
  90. }
  91. } // namespace Ui