fade_wrap.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. #include "ui/wrap/fade_wrap.h"
  8. #include "ui/widgets/shadow.h"
  9. #include "ui/painter.h"
  10. #include "styles/palette.h"
  11. namespace Ui {
  12. FadeWrap<RpWidget>::FadeWrap(
  13. QWidget *parent,
  14. object_ptr<RpWidget> &&child,
  15. float64 scale)
  16. : Parent(parent, std::move(child))
  17. , _animation(this, scale)
  18. , _duration(st::fadeWrapDuration) {
  19. if (auto weak = wrapped()) {
  20. weak->show();
  21. }
  22. }
  23. FadeWrap<RpWidget> *FadeWrap<RpWidget>::setDuration(int duration) {
  24. _duration = duration;
  25. return this;
  26. }
  27. FadeWrap<RpWidget> *FadeWrap<RpWidget>::toggle(
  28. bool shown,
  29. anim::type animated) {
  30. auto changed = (shown != _animation.visible());
  31. if (!_duration) {
  32. animated = anim::type::instant;
  33. }
  34. if (shown) {
  35. if (animated == anim::type::normal) {
  36. if (!_animation.animating()) {
  37. wrapped()->show();
  38. }
  39. _animation.fadeIn(_duration);
  40. if (_animation.animating()) {
  41. wrapped()->hide();
  42. }
  43. } else {
  44. _animation.show();
  45. if (!_animation.animating()) {
  46. wrapped()->show();
  47. }
  48. }
  49. } else {
  50. if (animated == anim::type::normal) {
  51. if (!_animation.animating()) {
  52. wrapped()->show();
  53. }
  54. _animation.fadeOut(_duration);
  55. if (_animation.animating()) {
  56. wrapped()->hide();
  57. }
  58. } else {
  59. _animation.hide();
  60. if (!_animation.animating()) {
  61. wrapped()->show();
  62. }
  63. }
  64. }
  65. if (changed) {
  66. _toggledChanged.fire_copy(shown);
  67. }
  68. return this;
  69. }
  70. FadeWrap<RpWidget> *FadeWrap<RpWidget>::finishAnimating() {
  71. _animation.finish();
  72. wrapped()->show();
  73. return this;
  74. }
  75. FadeWrap<RpWidget> *FadeWrap<RpWidget>::toggleOn(
  76. rpl::producer<bool> &&shown) {
  77. std::move(
  78. shown
  79. ) | rpl::start_with_next([this](bool shown) {
  80. toggle(shown, anim::type::normal);
  81. }, lifetime());
  82. finishAnimating();
  83. return this;
  84. }
  85. void FadeWrap<RpWidget>::paintEvent(QPaintEvent *e) {
  86. Painter p(this);
  87. if (_animation.paint(p)) {
  88. if (!_animation.animating() && _animation.visible()) {
  89. crl::on_main(this, [=] {
  90. if (!_animation.animating() && _animation.visible()) {
  91. wrapped()->show();
  92. }
  93. });
  94. }
  95. return;
  96. }
  97. if (!_animation.animating()) {
  98. wrapped()->show();
  99. }
  100. }
  101. FadeShadow::FadeShadow(QWidget *parent)
  102. : FadeShadow(parent, st::shadowFg) {
  103. }
  104. FadeShadow::FadeShadow(QWidget *parent, style::color color)
  105. : Parent(parent, object_ptr<PlainShadow>(parent, color)) {
  106. hide(anim::type::instant);
  107. }
  108. } // namespace Ui