toggle_arrow.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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/toggle_arrow.h"
  8. #include "ui/rp_widget.h"
  9. #include "ui/painter.h"
  10. #include "ui/rect.h"
  11. #include "styles/style_statistics.h"
  12. #include "styles/style_window.h"
  13. #include <QtCore/QtMath>
  14. namespace Ui {
  15. [[nodiscard]] QPainterPath ToggleUpDownArrowPath(
  16. float64 x,
  17. float64 y,
  18. float64 size,
  19. float64 fourStrokes,
  20. float64 progress) {
  21. const auto size2 = size / 2.;
  22. const auto stroke = (fourStrokes / 4.) / M_SQRT2;
  23. const auto left = x - size;
  24. const auto right = x + size;
  25. const auto bottom = y + size2;
  26. constexpr auto kPointCount = 6;
  27. auto points = std::array<QPointF, kPointCount>{ {
  28. { left - stroke, bottom - stroke },
  29. { x, bottom - stroke - size - stroke },
  30. { right + stroke, bottom - stroke },
  31. { right - stroke, bottom + stroke },
  32. { x, bottom + stroke - size + stroke },
  33. { left + stroke, bottom + stroke }
  34. } };
  35. const auto alpha = (progress - 1.) * M_PI;
  36. const auto cosalpha = cos(alpha);
  37. const auto sinalpha = sin(alpha);
  38. for (auto &point : points) {
  39. auto px = point.x() - x;
  40. auto py = point.y() - y;
  41. point.setX(x + px * cosalpha - py * sinalpha);
  42. point.setY(y + py * cosalpha + px * sinalpha);
  43. }
  44. auto path = QPainterPath();
  45. path.moveTo(points.front());
  46. for (int i = 1; i != kPointCount; ++i) {
  47. path.lineTo(points[i]);
  48. }
  49. path.lineTo(points.front());
  50. return path;
  51. }
  52. void AddToggleUpDownArrowToMoreButton(not_null<Ui::RpWidget*> parent) {
  53. const auto arrow = Ui::CreateChild<Ui::RpWidget>(parent.get());
  54. arrow->paintRequest() | rpl::start_with_next([=](const QRect &r) {
  55. auto p = QPainter(arrow);
  56. const auto path = ToggleUpDownArrowPath(
  57. st::statisticsShowMoreButtonArrowSize,
  58. st::statisticsShowMoreButtonArrowSize,
  59. st::statisticsShowMoreButtonArrowSize,
  60. st::mainMenuToggleFourStrokes,
  61. 0.);
  62. auto hq = PainterHighQualityEnabler(p);
  63. p.fillPath(path, st::lightButtonFg);
  64. }, arrow->lifetime());
  65. arrow->resize(Size(st::statisticsShowMoreButtonArrowSize * 2));
  66. arrow->move(st::statisticsShowMoreButtonArrowPosition);
  67. arrow->show();
  68. }
  69. } // namespace Ui