layer_widget.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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/rp_widget.h"
  9. #include "ui/effects/animations.h"
  10. #include "base/object_ptr.h"
  11. #include "base/flags.h"
  12. namespace Window {
  13. class SectionMemento;
  14. struct SectionShow;
  15. } // namespace Window
  16. namespace style {
  17. struct Box;
  18. } // namespace style
  19. namespace Ui {
  20. class BoxContent;
  21. enum class LayerOption {
  22. CloseOther = (1 << 0),
  23. KeepOther = (1 << 1),
  24. ShowAfterOther = (1 << 2),
  25. };
  26. using LayerOptions = base::flags<LayerOption>;
  27. inline constexpr auto is_flag_type(LayerOption) { return true; };
  28. class Show;
  29. using ShowPtr = std::shared_ptr<Show>;
  30. using ShowFactory = Fn<ShowPtr()>;
  31. class LayerWidget : public RpWidget {
  32. public:
  33. using RpWidget::RpWidget;
  34. virtual void parentResized() = 0;
  35. virtual void showFinished() {
  36. }
  37. void setInnerFocus();
  38. bool setClosing() {
  39. if (!_closing) {
  40. _closing = true;
  41. closeHook();
  42. return true;
  43. }
  44. return false;
  45. }
  46. bool overlaps(const QRect &globalRect);
  47. void setClosedCallback(Fn<void()> callback) {
  48. _closedCallback = std::move(callback);
  49. }
  50. void setResizedCallback(Fn<void()> callback) {
  51. _resizedCallback = std::move(callback);
  52. }
  53. virtual bool takeToThirdSection() {
  54. return false;
  55. }
  56. virtual bool showSectionInternal(
  57. not_null<::Window::SectionMemento*> memento,
  58. const ::Window::SectionShow &params) {
  59. return false;
  60. }
  61. virtual bool closeByOutsideClick() const {
  62. return true;
  63. }
  64. void closeLayer() {
  65. if (const auto callback = base::take(_closedCallback)) {
  66. callback();
  67. }
  68. }
  69. protected:
  70. void mousePressEvent(QMouseEvent *e) override;
  71. void resizeEvent(QResizeEvent *e) override;
  72. bool focusNextPrevChild(bool next) override;
  73. virtual void doSetInnerFocus() {
  74. setFocus();
  75. }
  76. virtual void closeHook() {
  77. }
  78. private:
  79. bool _closing = false;
  80. Fn<void()> _closedCallback;
  81. Fn<void()> _resizedCallback;
  82. };
  83. class LayerStackWidget : public RpWidget {
  84. public:
  85. LayerStackWidget(QWidget *parent, ShowFactory showFactory);
  86. void finishAnimating();
  87. rpl::producer<> hideFinishEvents() const;
  88. void setStyleOverrides(
  89. const style::Box *boxSt,
  90. const style::Box *layerSt);
  91. [[nodiscard]] const style::Box *boxStyleOverrideLayer() const {
  92. return _layerSt;
  93. }
  94. [[nodiscard]] const style::Box *boxStyleOverride() const {
  95. return _boxSt;
  96. }
  97. [[nodiscard]] ShowFactory showFactory() const {
  98. return _showFactory;
  99. }
  100. void showBox(
  101. object_ptr<BoxContent> box,
  102. LayerOptions options,
  103. anim::type animated);
  104. void showLayer(
  105. std::unique_ptr<LayerWidget> layer,
  106. LayerOptions options,
  107. anim::type animated);
  108. void showSpecialLayer(
  109. object_ptr<LayerWidget> layer,
  110. anim::type animated);
  111. void showMainMenu(
  112. object_ptr<LayerWidget> menu,
  113. anim::type animated);
  114. bool takeToThirdSection();
  115. bool canSetFocus() const;
  116. void setInnerFocus();
  117. bool contentOverlapped(const QRect &globalRect);
  118. void hideSpecialLayer(anim::type animated);
  119. void hideLayers(anim::type animated);
  120. void hideAll(anim::type animated);
  121. void hideTopLayer(anim::type animated);
  122. void setHideByBackgroundClick(bool hide);
  123. void removeBodyCache();
  124. // If you need to divide animated hideAll().
  125. void hideAllAnimatedPrepare();
  126. void hideAllAnimatedRun();
  127. bool showSectionInternal(
  128. not_null<::Window::SectionMemento*> memento,
  129. const ::Window::SectionShow &params);
  130. bool layerShown() const;
  131. const LayerWidget *topShownLayer() const;
  132. ~LayerStackWidget();
  133. protected:
  134. void keyPressEvent(QKeyEvent *e) override;
  135. void mousePressEvent(QMouseEvent *e) override;
  136. void resizeEvent(QResizeEvent *e) override;
  137. private:
  138. void appendLayer(
  139. std::unique_ptr<LayerWidget> layer,
  140. anim::type animated);
  141. void prependLayer(
  142. std::unique_ptr<LayerWidget> layer,
  143. anim::type animated);
  144. void replaceLayer(
  145. std::unique_ptr<LayerWidget> layer,
  146. anim::type animated);
  147. void backgroundClicked();
  148. LayerWidget *pushLayer(
  149. std::unique_ptr<LayerWidget> layer,
  150. anim::type animated);
  151. void showFinished();
  152. void hideCurrent(anim::type animated);
  153. void closeLayer(not_null<LayerWidget*> layer);
  154. enum class Action {
  155. ShowMainMenu,
  156. ShowSpecialLayer,
  157. ShowLayer,
  158. HideSpecialLayer,
  159. HideLayer,
  160. HideAll,
  161. };
  162. template <typename SetupNew, typename ClearOld>
  163. bool prepareAnimation(
  164. SetupNew &&setupNewWidgets,
  165. ClearOld &&clearOldWidgets,
  166. Action action,
  167. anim::type animated);
  168. template <typename SetupNew, typename ClearOld>
  169. void startAnimation(
  170. SetupNew &&setupNewWidgets,
  171. ClearOld &&clearOldWidgets,
  172. Action action,
  173. anim::type animated);
  174. void prepareForAnimation();
  175. void animationDone();
  176. void setCacheImages();
  177. void clearLayers();
  178. void clearSpecialLayer();
  179. void initChildLayer(LayerWidget *layer);
  180. void updateLayerBoxes();
  181. void fixOrder();
  182. void sendFakeMouseEvent();
  183. void clearClosingLayers();
  184. LayerWidget *currentLayer() {
  185. return _layers.empty() ? nullptr : _layers.back().get();
  186. }
  187. const LayerWidget *currentLayer() const {
  188. return const_cast<LayerStackWidget*>(this)->currentLayer();
  189. }
  190. std::vector<std::unique_ptr<LayerWidget>> _layers;
  191. std::vector<std::unique_ptr<LayerWidget>> _closingLayers;
  192. object_ptr<LayerWidget> _specialLayer = { nullptr };
  193. object_ptr<LayerWidget> _mainMenu = { nullptr };
  194. class BackgroundWidget;
  195. object_ptr<BackgroundWidget> _background;
  196. ShowFactory _showFactory;
  197. const style::Box *_boxSt = nullptr;
  198. const style::Box *_layerSt = nullptr;
  199. bool _hideByBackgroundClick = true;
  200. rpl::event_stream<> _hideFinishStream;
  201. };
  202. } // namespace Ui