tooltip.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // This file is part of Desktop App Toolkit,
  2. // a set of libraries for developing nice desktop applications.
  3. //
  4. // For license and copyright information please follow this link:
  5. // https://github.com/desktop-app/legal/blob/master/LEGAL
  6. //
  7. #pragma once
  8. #include "base/timer.h"
  9. #include "base/object_ptr.h"
  10. #include "ui/effects/animations.h"
  11. #include "ui/text/text.h"
  12. #include "ui/rp_widget.h"
  13. #include "ui/rect_part.h"
  14. namespace style {
  15. struct Tooltip;
  16. struct ImportantTooltip;
  17. struct FlatLabel;
  18. struct PopupMenu;
  19. } // namespace style
  20. namespace st {
  21. extern const style::FlatLabel &defaultFlatLabel;
  22. extern const style::PopupMenu &defaultPopupMenu;
  23. } // namespace st
  24. namespace Ui {
  25. class FlatLabel;
  26. class AbstractTooltipShower {
  27. public:
  28. virtual QString tooltipText() const = 0;
  29. virtual QPoint tooltipPos() const = 0;
  30. virtual bool tooltipWindowActive() const = 0;
  31. virtual const style::Tooltip *tooltipSt() const;
  32. virtual ~AbstractTooltipShower();
  33. };
  34. class Tooltip : public RpWidget {
  35. public:
  36. static void Show(int32 delay, const AbstractTooltipShower *shower);
  37. static void Hide();
  38. protected:
  39. void paintEvent(QPaintEvent *e) override;
  40. void hideEvent(QHideEvent *e) override;
  41. bool eventFilter(QObject *o, QEvent *e) override;
  42. private:
  43. void performShow();
  44. Tooltip();
  45. ~Tooltip();
  46. void popup(const QPoint &p, const QString &text, const style::Tooltip *st);
  47. friend class AbstractTooltipShower;
  48. const AbstractTooltipShower *_shower = nullptr;
  49. base::Timer _showTimer;
  50. Text::String _text;
  51. QPoint _point;
  52. const style::Tooltip *_st = nullptr;
  53. base::Timer _hideByLeaveTimer;
  54. bool _isEventFilter = false;
  55. bool _useTransparency = true;
  56. };
  57. class ImportantTooltip : public RpWidget {
  58. public:
  59. ImportantTooltip(
  60. QWidget *parent,
  61. object_ptr<RpWidget> content,
  62. const style::ImportantTooltip &st);
  63. void pointAt(
  64. QRect area,
  65. RectParts preferSide = RectPart::Top | RectPart::Left,
  66. Fn<QPoint(QSize)> countPosition = nullptr);
  67. void toggleAnimated(bool visible);
  68. void toggleFast(bool visible);
  69. void hideAfter(crl::time timeout);
  70. void updateGeometry();
  71. void setHiddenCallback(Fn<void()> callback) {
  72. _hiddenCallback = std::move(callback);
  73. }
  74. protected:
  75. void resizeEvent(QResizeEvent *e) override;
  76. void paintEvent(QPaintEvent *e) override;
  77. private:
  78. void animationCallback();
  79. [[nodiscard]] QRect countInner() const;
  80. void countApproachSide(RectParts preferSide);
  81. void resizeToContent();
  82. void checkAnimationFinish();
  83. void refreshAnimationCache();
  84. [[nodiscard]] QPoint countPosition() const;
  85. base::Timer _hideTimer;
  86. const style::ImportantTooltip &_st;
  87. object_ptr<RpWidget> _content;
  88. QRect _area;
  89. RectParts _side = RectPart::Top | RectPart::Left;
  90. QPixmap _arrow;
  91. Ui::Animations::Simple _visibleAnimation;
  92. Fn<QPoint(QSize)> _countPosition;
  93. bool _visible = false;
  94. Fn<void()> _hiddenCallback;
  95. QPixmap _cache;
  96. };
  97. [[nodiscard]] int FindNiceTooltipWidth(
  98. int minWidth,
  99. int maxWidth,
  100. Fn<int(int width)> heightForWidth);
  101. [[nodiscard]] object_ptr<FlatLabel> MakeNiceTooltipLabel(
  102. QWidget *parent,
  103. rpl::producer<TextWithEntities> &&text,
  104. int maxWidth,
  105. const style::FlatLabel &st = st::defaultFlatLabel,
  106. const style::PopupMenu &stMenu = st::defaultPopupMenu);
  107. } // namespace Ui