glare.cpp 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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/effects/glare.h"
  8. #include "styles/style_boxes.h"
  9. namespace Ui {
  10. namespace {
  11. constexpr auto kMaxGlareOpaque = 0.5;
  12. } // namespace
  13. float64 GlareEffect::progress(crl::time now) const {
  14. return (now - glare.birthTime)
  15. / float64(glare.deathTime - glare.birthTime);
  16. }
  17. QLinearGradient GlareEffect::computeGradient(const QColor &color) const {
  18. auto gradient = QLinearGradient(
  19. QPointF(0, 0),
  20. QPointF(width, 0));
  21. auto tempColor = color;
  22. tempColor.setAlphaF(0);
  23. const auto edge = tempColor;
  24. tempColor.setAlphaF(kMaxGlareOpaque);
  25. const auto middle = tempColor;
  26. gradient.setStops({
  27. { 0., edge },
  28. { .5, middle },
  29. { 1., edge },
  30. });
  31. return gradient;
  32. }
  33. void GlareEffect::validate(
  34. const QColor &color,
  35. Fn<void()> updateCallback,
  36. crl::time timeout,
  37. crl::time duration) {
  38. if (anim::Disabled()) {
  39. return;
  40. }
  41. if (!width) {
  42. width = st::gradientButtonGlareWidth;
  43. }
  44. animation.init([=](crl::time now) {
  45. if (const auto diff = (now - glare.deathTime); diff > 0) {
  46. if (diff > timeout && !paused) {
  47. glare = {
  48. .birthTime = now,
  49. .deathTime = now + duration,
  50. };
  51. updateCallback();
  52. }
  53. } else {
  54. updateCallback();
  55. }
  56. });
  57. animation.start();
  58. {
  59. auto newPixmap = QPixmap(QSize(width, 1)
  60. * style::DevicePixelRatio());
  61. newPixmap.setDevicePixelRatio(style::DevicePixelRatio());
  62. newPixmap.fill(Qt::transparent);
  63. {
  64. auto p = QPainter(&newPixmap);
  65. p.fillRect(newPixmap.rect(), QBrush(computeGradient(color)));
  66. }
  67. pixmap = std::move(newPixmap);
  68. }
  69. }
  70. } // namespace Ui