iv_controller.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. This file is part of Telegram Desktop,
  3. the official desktop application for the Telegram messaging service.
  4. For license and copyright information please follow this link:
  5. https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
  6. */
  7. #pragma once
  8. #include "base/invoke_queued.h"
  9. #include "base/object_ptr.h"
  10. #include "base/unique_qptr.h"
  11. #include "iv/iv_delegate.h"
  12. #include "ui/effects/animations.h"
  13. #include "ui/text/text.h"
  14. #include "webview/webview_common.h"
  15. class Painter;
  16. namespace Webview {
  17. struct DataRequest;
  18. class Window;
  19. } // namespace Webview
  20. namespace Ui {
  21. class RpWidget;
  22. class RpWindow;
  23. class PopupMenu;
  24. class FlatLabel;
  25. class IconButton;
  26. template <typename Widget>
  27. class FadeWrapScaled;
  28. } // namespace Ui
  29. namespace Iv {
  30. struct Prepared;
  31. struct ShareBoxResult {
  32. Fn<void()> focus;
  33. Fn<void()> hide;
  34. rpl::producer<> destroyRequests;
  35. };
  36. struct ShareBoxDescriptor {
  37. not_null<Ui::RpWidget*> parent;
  38. QString url;
  39. };
  40. class Controller final {
  41. public:
  42. Controller(
  43. not_null<Delegate*> delegate,
  44. Fn<ShareBoxResult(ShareBoxDescriptor)> showShareBox);
  45. ~Controller();
  46. struct Event {
  47. enum class Type {
  48. Close,
  49. Quit,
  50. OpenChannel,
  51. JoinChannel,
  52. OpenPage,
  53. OpenLink,
  54. OpenLinkExternal,
  55. OpenMedia,
  56. Report,
  57. };
  58. Type type = Type::Close;
  59. QString url;
  60. QString context;
  61. };
  62. void show(
  63. const Webview::StorageId &storageId,
  64. Prepared page,
  65. base::flat_map<QByteArray, rpl::producer<bool>> inChannelValues);
  66. void update(Prepared page);
  67. [[nodiscard]] static bool IsGoodTonSiteUrl(const QString &uri);
  68. void showTonSite(const Webview::StorageId &storageId, QString uri);
  69. [[nodiscard]] bool active() const;
  70. void showJoinedTooltip();
  71. void minimize();
  72. [[nodiscard]] rpl::producer<Webview::DataRequest> dataRequests() const {
  73. return _dataRequests.events();
  74. }
  75. [[nodiscard]] rpl::producer<Event> events() const {
  76. return _events.events();
  77. }
  78. [[nodiscard]] rpl::lifetime &lifetime();
  79. private:
  80. void createWindow();
  81. void createWebview(const Webview::StorageId &storageId);
  82. [[nodiscard]] QByteArray navigateScript(int index, const QString &hash);
  83. [[nodiscard]] QByteArray reloadScript(int index);
  84. void showInWindow(const Webview::StorageId &storageId, Prepared page);
  85. [[nodiscard]] QByteArray fillInChannelValuesScript(
  86. base::flat_map<QByteArray, rpl::producer<bool>> inChannelValues);
  87. [[nodiscard]] QByteArray toggleInChannelScript(
  88. const QByteArray &id,
  89. bool in) const;
  90. void processKey(const QString &key, const QString &modifier);
  91. void processLink(const QString &url, const QString &context);
  92. void initControls();
  93. void updateTitleGeometry(int newWidth) const;
  94. void activate();
  95. void setInnerFocus();
  96. void showMenu();
  97. void escape();
  98. void close();
  99. void quit();
  100. [[nodiscard]] QString composeCurrentUrl() const;
  101. [[nodiscard]] uint64 compuseCurrentPageId() const;
  102. void showShareMenu();
  103. void destroyShareMenu();
  104. void showWebviewError();
  105. void showWebviewError(TextWithEntities text);
  106. const not_null<Delegate*> _delegate;
  107. std::unique_ptr<Ui::RpWindow> _window;
  108. std::unique_ptr<Ui::RpWidget> _subtitleWrap;
  109. rpl::variable<QString> _url;
  110. rpl::variable<QString> _subtitleText;
  111. rpl::variable<QString> _windowTitleText;
  112. std::unique_ptr<Ui::FlatLabel> _subtitle;
  113. Ui::Animations::Simple _subtitleBackShift;
  114. Ui::Animations::Simple _subtitleForwardShift;
  115. object_ptr<Ui::IconButton> _menuToggle = { nullptr };
  116. object_ptr<Ui::FadeWrapScaled<Ui::IconButton>> _back = { nullptr };
  117. object_ptr<Ui::FadeWrapScaled<Ui::IconButton>> _forward = { nullptr };
  118. base::unique_qptr<Ui::PopupMenu> _menu;
  119. Ui::RpWidget *_container = nullptr;
  120. std::unique_ptr<Webview::Window> _webview;
  121. rpl::event_stream<Webview::DataRequest> _dataRequests;
  122. rpl::event_stream<Event> _events;
  123. base::flat_map<QByteArray, bool> _inChannelChanged;
  124. base::flat_set<QByteArray> _inChannelSubscribed;
  125. SingleQueuedInvokation _updateStyles;
  126. bool _reloadInitialWhenReady = false;
  127. bool _subscribedToColors = false;
  128. bool _ready = false;
  129. rpl::variable<int> _index = -1;
  130. QString _hash;
  131. Fn<ShareBoxResult(ShareBoxDescriptor)> _showShareBox;
  132. std::unique_ptr<Ui::RpWidget> _shareWrap;
  133. std::unique_ptr<QWidget> _shareContainer;
  134. Fn<void()> _shareFocus;
  135. Fn<void()> _shareHide;
  136. bool _shareHidesContent = false;
  137. std::vector<Prepared> _pages;
  138. base::flat_map<QString, int> _indices;
  139. QString _navigateToHashWhenReady;
  140. int _navigateToIndexWhenReady = -1;
  141. rpl::lifetime _lifetime;
  142. };
  143. } // namespace Iv