fade_wrap.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 "ui/wrap/wrap.h"
  9. #include "ui/effects/fade_animation.h"
  10. namespace Ui {
  11. template <typename Widget = RpWidget>
  12. class FadeWrapScaled;
  13. template <typename Widget = RpWidget>
  14. class FadeWrap;
  15. template <>
  16. class FadeWrap<RpWidget> : public Wrap<RpWidget> {
  17. using Parent = Wrap<RpWidget>;
  18. public:
  19. FadeWrap(
  20. QWidget *parent,
  21. object_ptr<RpWidget> &&child,
  22. float64 scale = 1.);
  23. FadeWrap *setDuration(int duration);
  24. FadeWrap *toggle(bool shown, anim::type animated);
  25. FadeWrap *show(anim::type animated) {
  26. return toggle(true, animated);
  27. }
  28. FadeWrap *hide(anim::type animated) {
  29. return toggle(false, animated);
  30. }
  31. FadeWrap *finishAnimating();
  32. FadeWrap *toggleOn(rpl::producer<bool> &&shown);
  33. bool animating() const {
  34. return _animation.animating();
  35. }
  36. bool toggled() const {
  37. return _animation.visible();
  38. }
  39. auto toggledValue() const {
  40. return _toggledChanged.events_starting_with(
  41. _animation.visible());
  42. }
  43. protected:
  44. void paintEvent(QPaintEvent *e) final override;
  45. private:
  46. rpl::event_stream<bool> _toggledChanged;
  47. FadeAnimation _animation;
  48. int _duration = 0;
  49. };
  50. template <typename Widget>
  51. class FadeWrap : public Wrap<Widget, FadeWrap<RpWidget>> {
  52. using Parent = Wrap<Widget, FadeWrap<RpWidget>>;
  53. public:
  54. FadeWrap(
  55. QWidget *parent,
  56. object_ptr<Widget> &&child,
  57. float64 scale = 1.)
  58. : Parent(parent, std::move(child), scale) {
  59. }
  60. FadeWrap *setDuration(int duration) {
  61. return chain(Parent::setDuration(duration));
  62. }
  63. FadeWrap *toggle(bool shown, anim::type animated) {
  64. return chain(Parent::toggle(shown, animated));
  65. }
  66. FadeWrap *show(anim::type animated) {
  67. return chain(Parent::show(animated));
  68. }
  69. FadeWrap *hide(anim::type animated) {
  70. return chain(Parent::hide(animated));
  71. }
  72. FadeWrap *finishAnimating() {
  73. return chain(Parent::finishAnimating());
  74. }
  75. FadeWrap *toggleOn(rpl::producer<bool> &&shown) {
  76. return chain(Parent::toggleOn(std::move(shown)));
  77. }
  78. private:
  79. FadeWrap *chain(FadeWrap<RpWidget> *result) {
  80. return static_cast<FadeWrap*>(result);
  81. }
  82. };
  83. template <typename Widget>
  84. class FadeWrapScaled : public FadeWrap<Widget> {
  85. using Parent = FadeWrap<Widget>;
  86. public:
  87. FadeWrapScaled(QWidget *parent, object_ptr<Widget> &&child)
  88. : Parent(parent, std::move(child), 0.) {
  89. }
  90. };
  91. class PlainShadow;
  92. class FadeShadow : public FadeWrap<PlainShadow> {
  93. using Parent = FadeWrap<PlainShadow>;
  94. public:
  95. FadeShadow(QWidget *parent);
  96. FadeShadow(QWidget *parent, style::color color);
  97. };
  98. } // namespace Ui