inner_dropdown.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 "styles/style_widgets.h"
  9. #include "ui/rp_widget.h"
  10. #include "ui/round_rect.h"
  11. #include "ui/effects/animations.h"
  12. #include "ui/effects/panel_animation.h"
  13. #include "base/object_ptr.h"
  14. #include "base/timer.h"
  15. namespace Ui {
  16. class ScrollArea;
  17. class InnerDropdown : public RpWidget {
  18. public:
  19. InnerDropdown(QWidget *parent, const style::InnerDropdown &st = st::defaultInnerDropdown);
  20. template <typename Widget>
  21. QPointer<Widget> setOwnedWidget(object_ptr<Widget> widget) {
  22. auto result = doSetOwnedWidget(std::move(widget));
  23. return QPointer<Widget>(static_cast<Widget*>(result.data()));
  24. }
  25. bool overlaps(const QRect &globalRect) {
  26. if (isHidden() || _a_show.animating() || _a_opacity.animating()) return false;
  27. return rect().marginsRemoved(_st.padding).contains(QRect(mapFromGlobal(globalRect.topLeft()), globalRect.size()));
  28. }
  29. void setAutoHiding(bool autoHiding) {
  30. _autoHiding = autoHiding;
  31. }
  32. void setMaxHeight(int newMaxHeight);
  33. void resizeToContent();
  34. void otherEnter();
  35. void otherLeave();
  36. void setShowStartCallback(Fn<void()> callback) {
  37. _showStartCallback = std::move(callback);
  38. }
  39. void setHideStartCallback(Fn<void()> callback) {
  40. _hideStartCallback = std::move(callback);
  41. }
  42. void setHiddenCallback(Fn<void()> callback) {
  43. _hiddenCallback = std::move(callback);
  44. }
  45. bool isHiding() const {
  46. return _hiding && _a_opacity.animating();
  47. }
  48. enum class HideOption {
  49. Default,
  50. IgnoreShow,
  51. };
  52. void showAnimated();
  53. void setOrigin(PanelAnimation::Origin origin);
  54. void showAnimated(PanelAnimation::Origin origin);
  55. void hideAnimated(HideOption option = HideOption::Default);
  56. void finishAnimating();
  57. void showFast();
  58. void hideFast();
  59. protected:
  60. void resizeEvent(QResizeEvent *e) override;
  61. void paintEvent(QPaintEvent *e) override;
  62. void enterEventHook(QEnterEvent *e) override;
  63. void leaveEventHook(QEvent *e) override;
  64. bool eventFilter(QObject *obj, QEvent *e) override;
  65. int resizeGetHeight(int newWidth) override;
  66. private:
  67. QPointer<RpWidget> doSetOwnedWidget(object_ptr<RpWidget> widget);
  68. QImage grabForPanelAnimation();
  69. void startShowAnimation();
  70. void startOpacityAnimation(bool hiding);
  71. void prepareCache();
  72. class Container;
  73. void showAnimationCallback();
  74. void opacityAnimationCallback();
  75. void hideFinished();
  76. void showStarted();
  77. void updateHeight();
  78. void scrolled();
  79. const style::InnerDropdown &_st;
  80. RoundRect _roundRect;
  81. PanelAnimation::Origin _origin = PanelAnimation::Origin::TopLeft;
  82. std::unique_ptr<PanelAnimation> _showAnimation;
  83. Animations::Simple _a_show;
  84. bool _autoHiding = true;
  85. bool _hiding = false;
  86. QPixmap _cache;
  87. Animations::Simple _a_opacity;
  88. base::Timer _hideTimer;
  89. bool _ignoreShowEvents = false;
  90. Fn<void()> _showStartCallback;
  91. Fn<void()> _hideStartCallback;
  92. Fn<void()> _hiddenCallback;
  93. object_ptr<ScrollArea> _scroll;
  94. int _maxHeight = 0;
  95. };
  96. class InnerDropdown::Container : public TWidget {
  97. public:
  98. Container(QWidget *parent, object_ptr<TWidget> child, const style::InnerDropdown &st);
  99. void resizeToContent();
  100. protected:
  101. int resizeGetHeight(int newWidth) override;
  102. void visibleTopBottomUpdated(
  103. int visibleTop,
  104. int visibleBottom) override;
  105. private:
  106. object_ptr<TWidget> _child;
  107. const style::InnerDropdown &_st;
  108. };
  109. } // namespace Ui