platform_overlay_widget.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. #include "platform/platform_overlay_widget.h"
  8. #include "ui/effects/animations.h"
  9. #include "ui/platform/ui_platform_window_title.h"
  10. #include "ui/widgets/rp_window.h"
  11. #include "ui/abstract_button.h"
  12. #include "styles/style_media_view.h"
  13. namespace Media::View {
  14. QColor OverBackgroundColor() {
  15. auto c1 = st::mediaviewBg->c;
  16. auto c2 = QColor(255, 255, 255);
  17. const auto mix = [&](int a, int b) {
  18. constexpr auto k1 = 0.15 * 0.85 / (1. - 0.85 * 0.85);
  19. constexpr auto k2 = 0.15 / (1. - 0.85 * 0.85);
  20. return int(a * k1 + b * k2);
  21. };
  22. return QColor(
  23. mix(c1.red(), c2.red()),
  24. mix(c1.green(), c2.green()),
  25. mix(c1.blue(), c2.blue()));
  26. }
  27. } // namespace Media::View
  28. namespace Platform {
  29. namespace {
  30. using namespace Media::View;
  31. } // namespace
  32. class DefaultOverlayWidgetHelper::Buttons final
  33. : public Ui::Platform::AbstractTitleButtons {
  34. public:
  35. using Control = Ui::Platform::TitleControl;
  36. object_ptr<Ui::AbstractButton> create(
  37. not_null<QWidget*> parent,
  38. Control control,
  39. const style::WindowTitle &st) override;
  40. void updateState(
  41. bool active,
  42. bool maximized,
  43. const style::WindowTitle &st) override;
  44. void notifySynteticOver(Control control, bool over) override;
  45. void setMasterOpacity(float64 opacity);
  46. [[nodiscard]] rpl::producer<> activations() const;
  47. void clearState();
  48. private:
  49. rpl::event_stream<> _activations;
  50. rpl::variable<float64> _masterOpacity = 1.;
  51. rpl::variable<bool> _maximized = false;
  52. rpl::event_stream<> _clearStateRequests;
  53. };
  54. object_ptr<Ui::AbstractButton> DefaultOverlayWidgetHelper::Buttons::create(
  55. not_null<QWidget*> parent,
  56. Control control,
  57. const style::WindowTitle &st) {
  58. auto result = object_ptr<Ui::AbstractButton>(parent);
  59. const auto raw = result.data();
  60. struct State {
  61. Ui::Animations::Simple animation;
  62. float64 progress = -1.;
  63. QImage frame;
  64. bool maximized = false;
  65. bool over = false;
  66. };
  67. const auto state = raw->lifetime().make_state<State>();
  68. rpl::merge(
  69. _masterOpacity.changes() | rpl::to_empty,
  70. _maximized.changes() | rpl::to_empty
  71. ) | rpl::start_with_next([=] {
  72. raw->update();
  73. }, raw->lifetime());
  74. _clearStateRequests.events(
  75. ) | rpl::start_with_next([=] {
  76. raw->clearState();
  77. raw->update();
  78. state->over = raw->isOver();
  79. state->animation.stop();
  80. }, raw->lifetime());
  81. const auto icon = [&] {
  82. switch (control) {
  83. case Control::Minimize: return &st::mediaviewTitleMinimize;
  84. case Control::Maximize: return &st::mediaviewTitleMaximize;
  85. case Control::Close: return &st::mediaviewTitleClose;
  86. }
  87. Unexpected("Value in DefaultOverlayWidgetHelper::Buttons::create.");
  88. }();
  89. raw->resize(icon->size());
  90. state->frame = QImage(
  91. icon->size() * style::DevicePixelRatio(),
  92. QImage::Format_ARGB32_Premultiplied);
  93. state->frame.setDevicePixelRatio(style::DevicePixelRatio());
  94. const auto updateOver = [=] {
  95. const auto over = raw->isOver();
  96. if (state->over == over) {
  97. return;
  98. }
  99. state->over = over;
  100. state->animation.start(
  101. [=] { raw->update(); },
  102. state->over ? 0. : 1.,
  103. state->over ? 1. : 0.,
  104. st::mediaviewFadeDuration);
  105. };
  106. const auto prepareFrame = [=] {
  107. const auto progress = state->animation.value(state->over ? 1. : 0.);
  108. const auto maximized = _maximized.current();
  109. if (state->progress == progress && state->maximized == maximized) {
  110. return;
  111. }
  112. state->progress = progress;
  113. state->maximized = maximized;
  114. auto current = icon;
  115. if (control == Control::Maximize) {
  116. current = maximized ? &st::mediaviewTitleRestore : icon;
  117. }
  118. const auto alpha = progress * kOverBackgroundOpacity;
  119. auto color = OverBackgroundColor();
  120. color.setAlpha(anim::interpolate(0, 255, alpha));
  121. state->frame.fill(color);
  122. auto q = QPainter(&state->frame);
  123. const auto normal = maximized
  124. ? kMaximizedIconOpacity
  125. : kNormalIconOpacity;
  126. q.setOpacity(progress + (1 - progress) * normal);
  127. current->paint(q, 0, 0, raw->width());
  128. q.end();
  129. };
  130. raw->paintRequest(
  131. ) | rpl::start_with_next([=] {
  132. updateOver();
  133. prepareFrame();
  134. auto p = QPainter(raw);
  135. p.setOpacity(_masterOpacity.current());
  136. p.drawImage(0, 0, state->frame);
  137. }, raw->lifetime());
  138. return result;
  139. }
  140. void DefaultOverlayWidgetHelper::Buttons::updateState(
  141. bool active,
  142. bool maximized,
  143. const style::WindowTitle &st) {
  144. _maximized = maximized;
  145. }
  146. void DefaultOverlayWidgetHelper::Buttons::notifySynteticOver(
  147. Ui::Platform::TitleControl control,
  148. bool over) {
  149. if (over) {
  150. _activations.fire({});
  151. }
  152. }
  153. void DefaultOverlayWidgetHelper::Buttons::clearState() {
  154. _clearStateRequests.fire({});
  155. }
  156. void DefaultOverlayWidgetHelper::Buttons::setMasterOpacity(float64 opacity) {
  157. _masterOpacity = opacity;
  158. }
  159. rpl::producer<> DefaultOverlayWidgetHelper::Buttons::activations() const {
  160. return _activations.events();
  161. }
  162. void OverlayWidgetHelper::minimize(not_null<Ui::RpWindow*> window) {
  163. window->setWindowState(window->windowState() | Qt::WindowMinimized);
  164. }
  165. DefaultOverlayWidgetHelper::DefaultOverlayWidgetHelper(
  166. not_null<Ui::RpWindow*> window,
  167. Fn<void(bool)> maximize)
  168. : _buttons(new DefaultOverlayWidgetHelper::Buttons())
  169. , _controls(Ui::Platform::SetupSeparateTitleControls(
  170. window,
  171. std::make_unique<Ui::Platform::SeparateTitleControls>(
  172. window->body(),
  173. st::mediaviewTitle,
  174. std::unique_ptr<DefaultOverlayWidgetHelper::Buttons>(_buttons.get()),
  175. std::move(maximize)))) {
  176. }
  177. DefaultOverlayWidgetHelper::~DefaultOverlayWidgetHelper() = default;
  178. void DefaultOverlayWidgetHelper::orderWidgets() {
  179. _controls->wrap.raise();
  180. }
  181. bool DefaultOverlayWidgetHelper::skipTitleHitTest(QPoint position) {
  182. using namespace Ui::Platform;
  183. return _controls->controls.hitTest(position) != HitTestResult::None;
  184. }
  185. rpl::producer<> DefaultOverlayWidgetHelper::controlsActivations() {
  186. return _buttons->activations();
  187. }
  188. rpl::producer<bool> DefaultOverlayWidgetHelper::controlsSideRightValue() {
  189. return _controls->controls.layout().value(
  190. ) | rpl::map([=](const auto &layout) {
  191. return !layout.onLeft();
  192. }) | rpl::distinct_until_changed();
  193. }
  194. void DefaultOverlayWidgetHelper::beforeShow(bool fullscreen) {
  195. _buttons->clearState();
  196. }
  197. void DefaultOverlayWidgetHelper::clearState() {
  198. _buttons->clearState();
  199. }
  200. void DefaultOverlayWidgetHelper::setControlsOpacity(float64 opacity) {
  201. _buttons->setMasterOpacity(opacity);
  202. }
  203. auto DefaultOverlayWidgetHelper::mouseEvents() const
  204. -> rpl::producer<not_null<QMouseEvent*>> {
  205. return _controls->wrap.events(
  206. ) | rpl::filter([](not_null<QEvent*> e) {
  207. const auto type = e->type();
  208. return (type == QEvent::MouseButtonPress)
  209. || (type == QEvent::MouseButtonRelease)
  210. || (type == QEvent::MouseMove)
  211. || (type == QEvent::MouseButtonDblClick);
  212. }) | rpl::map([](not_null<QEvent*> e) {
  213. return not_null{ static_cast<QMouseEvent*>(e.get()) };
  214. });
  215. }
  216. } // namespace Platform