photo_editor_controls.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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 "editor/photo_editor_controls.h"
  8. #include "editor/controllers/controllers.h"
  9. #include "lang/lang_keys.h"
  10. #include "ui/image/image_prepare.h"
  11. #include "ui/widgets/buttons.h"
  12. #include "ui/widgets/labels.h"
  13. #include "ui/wrap/fade_wrap.h"
  14. #include "ui/painter.h"
  15. #include "styles/style_editor.h"
  16. namespace Editor {
  17. class EdgeButton final : public Ui::RippleButton {
  18. public:
  19. EdgeButton(
  20. not_null<Ui::RpWidget*> parent,
  21. const QString &text,
  22. int height,
  23. bool left,
  24. const style::color &bg,
  25. const style::color &fg,
  26. const style::RippleAnimation &st);
  27. protected:
  28. QImage prepareRippleMask() const override;
  29. QPoint prepareRippleStartPosition() const override;
  30. private:
  31. void init();
  32. const style::color &_fg;
  33. Ui::Text::String _text;
  34. const int _width;
  35. const QRect _rippleRect;
  36. const QColor _bg;
  37. const bool _left;
  38. QImage rounded(std::optional<QColor> color) const;
  39. };
  40. EdgeButton::EdgeButton(
  41. not_null<Ui::RpWidget*> parent,
  42. const QString &text,
  43. int height,
  44. bool left,
  45. const style::color &bg,
  46. const style::color &fg,
  47. const style::RippleAnimation &st)
  48. : Ui::RippleButton(parent, st)
  49. , _fg(fg)
  50. , _text(st::photoEditorButtonStyle, text)
  51. , _width(_text.maxWidth()
  52. + st::photoEditorTextButtonPadding.left()
  53. + st::photoEditorTextButtonPadding.right())
  54. , _rippleRect(QRect(0, 0, _width, height))
  55. , _bg(bg->c)
  56. , _left(left) {
  57. resize(_width, height);
  58. init();
  59. }
  60. void EdgeButton::init() {
  61. // const auto bg = rounded(_bg);
  62. paintRequest(
  63. ) | rpl::start_with_next([=] {
  64. Painter p(this);
  65. // p.drawImage(QPoint(), bg);
  66. paintRipple(p, _rippleRect.x(), _rippleRect.y());
  67. p.setPen(_fg);
  68. const auto textTop = st::photoEditorButtonTextTop;
  69. _text.draw(p, 0, textTop, width(), style::al_center);
  70. }, lifetime());
  71. }
  72. QImage EdgeButton::rounded(std::optional<QColor> color) const {
  73. auto result = QImage(
  74. _rippleRect.size() * style::DevicePixelRatio(),
  75. QImage::Format_ARGB32_Premultiplied);
  76. result.setDevicePixelRatio(style::DevicePixelRatio());
  77. result.fill(color.value_or(Qt::white));
  78. const auto parts = RectPart::None
  79. | (_left ? RectPart::TopLeft : RectPart::TopRight)
  80. | (_left ? RectPart::BottomLeft : RectPart::BottomRight);
  81. return Images::Round(std::move(result), ImageRoundRadius::Large, parts);
  82. }
  83. QImage EdgeButton::prepareRippleMask() const {
  84. return rounded(std::nullopt);
  85. }
  86. QPoint EdgeButton::prepareRippleStartPosition() const {
  87. return mapFromGlobal(QCursor::pos()) - _rippleRect.topLeft();
  88. }
  89. class ButtonBar final : public Ui::RpWidget {
  90. public:
  91. ButtonBar(
  92. not_null<Ui::RpWidget*> parent,
  93. const style::color &bg);
  94. private:
  95. QImage _roundedBg;
  96. };
  97. ButtonBar::ButtonBar(
  98. not_null<Ui::RpWidget*> parent,
  99. const style::color &bg)
  100. : RpWidget(parent) {
  101. sizeValue(
  102. ) | rpl::start_with_next([=](const QSize &size) {
  103. const auto children = RpWidget::children();
  104. const auto widgets = ranges::views::all(
  105. children
  106. ) | ranges::views::filter([](not_null<const QObject*> object) {
  107. return object->isWidgetType();
  108. }) | ranges::views::transform([](not_null<QObject*> object) {
  109. return static_cast<QWidget*>(object.get());
  110. }) | ranges::to_vector;
  111. if (widgets.size() < 2) {
  112. return;
  113. }
  114. const auto layout = [&](bool symmetrical) {
  115. auto widths = widgets | ranges::views::transform(
  116. &QWidget::width
  117. ) | ranges::to_vector;
  118. const auto count = int(widths.size());
  119. const auto middle = count / 2;
  120. if (symmetrical) {
  121. for (auto i = 0; i != middle; ++i) {
  122. const auto j = count - i - 1;
  123. widths[i] = widths[j] = std::max(widths[i], widths[j]);
  124. }
  125. }
  126. const auto residualWidth = size.width()
  127. - ranges::accumulate(widths, 0);
  128. if (symmetrical && residualWidth < 0) {
  129. return false;
  130. }
  131. const auto step = residualWidth / float(count - 1);
  132. auto left = 0.;
  133. auto &&ints = ranges::views::ints(0, ranges::unreachable);
  134. auto &&list = ranges::views::zip(widgets, widths, ints);
  135. for (const auto &[widget, width, index] : list) {
  136. widget->move(int((index >= middle)
  137. ? (left + width - widget->width())
  138. : left), 0);
  139. left += width + step;
  140. }
  141. return true;
  142. };
  143. if (!layout(true)) {
  144. layout(false);
  145. }
  146. auto result = QImage(
  147. size * style::DevicePixelRatio(),
  148. QImage::Format_ARGB32_Premultiplied);
  149. result.setDevicePixelRatio(style::DevicePixelRatio());
  150. result.fill(bg->c);
  151. _roundedBg = Images::Round(
  152. std::move(result),
  153. ImageRoundRadius::Large);
  154. }, lifetime());
  155. paintRequest(
  156. ) | rpl::start_with_next([=] {
  157. auto p = QPainter(this);
  158. p.drawImage(QPoint(), _roundedBg);
  159. }, lifetime());
  160. }
  161. PhotoEditorControls::PhotoEditorControls(
  162. not_null<Ui::RpWidget*> parent,
  163. std::shared_ptr<Controllers> controllers,
  164. const PhotoModifications modifications,
  165. const EditorData &data)
  166. : RpWidget(parent)
  167. , _bg(st::roundedBg)
  168. , _buttonHeight(st::photoEditorButtonBarHeight)
  169. , _transformButtons(base::make_unique_q<ButtonBar>(this, _bg))
  170. , _paintTopButtons(base::make_unique_q<ButtonBar>(this, _bg))
  171. , _paintBottomButtons(base::make_unique_q<ButtonBar>(this, _bg))
  172. , _about(data.about.empty()
  173. ? nullptr
  174. : base::make_unique_q<Ui::FadeWrap<Ui::FlatLabel>>(
  175. this,
  176. object_ptr<Ui::FlatLabel>(
  177. this,
  178. rpl::single(data.about),
  179. st::photoEditorAbout)))
  180. , _transformCancel(base::make_unique_q<EdgeButton>(
  181. _transformButtons,
  182. tr::lng_cancel(tr::now),
  183. _buttonHeight,
  184. true,
  185. _bg,
  186. st::mediaviewCaptionFg,
  187. st::photoEditorRotateButton.ripple))
  188. , _flipButton(base::make_unique_q<Ui::IconButton>(
  189. _transformButtons,
  190. st::photoEditorFlipButton))
  191. , _rotateButton(base::make_unique_q<Ui::IconButton>(
  192. _transformButtons,
  193. st::photoEditorRotateButton))
  194. , _paintModeButton(base::make_unique_q<Ui::IconButton>(
  195. _transformButtons,
  196. st::photoEditorPaintModeButton))
  197. , _transformDone(base::make_unique_q<EdgeButton>(
  198. _transformButtons,
  199. (data.confirm.isEmpty() ? tr::lng_box_done(tr::now) : data.confirm),
  200. _buttonHeight,
  201. false,
  202. _bg,
  203. st::mediaviewTextLinkFg,
  204. st::photoEditorRotateButton.ripple))
  205. , _paintCancel(base::make_unique_q<EdgeButton>(
  206. _paintBottomButtons,
  207. tr::lng_cancel(tr::now),
  208. _buttonHeight,
  209. true,
  210. _bg,
  211. st::mediaviewCaptionFg,
  212. st::photoEditorRotateButton.ripple))
  213. , _undoButton(base::make_unique_q<Ui::IconButton>(
  214. _paintTopButtons,
  215. st::photoEditorUndoButton))
  216. , _redoButton(base::make_unique_q<Ui::IconButton>(
  217. _paintTopButtons,
  218. st::photoEditorRedoButton))
  219. , _paintModeButtonActive(base::make_unique_q<Ui::IconButton>(
  220. _paintBottomButtons,
  221. st::photoEditorPaintModeButton))
  222. , _stickersButton(controllers->stickersPanelController
  223. ? base::make_unique_q<Ui::IconButton>(
  224. _paintBottomButtons,
  225. st::photoEditorStickersButton)
  226. : nullptr)
  227. , _paintDone(base::make_unique_q<EdgeButton>(
  228. _paintBottomButtons,
  229. tr::lng_box_done(tr::now),
  230. _buttonHeight,
  231. false,
  232. _bg,
  233. st::mediaviewTextLinkFg,
  234. st::photoEditorRotateButton.ripple)) {
  235. {
  236. const auto icon = &st::photoEditorPaintIconActive;
  237. _paintModeButtonActive->setIconOverride(icon, icon);
  238. }
  239. _paintModeButtonActive->setAttribute(Qt::WA_TransparentForMouseEvents);
  240. sizeValue(
  241. ) | rpl::start_with_next([=](const QSize &size) {
  242. if (size.isEmpty()) {
  243. return;
  244. }
  245. const auto &padding = st::photoEditorButtonBarPadding;
  246. const auto w = std::min(st::photoEditorButtonBarWidth, size.width())
  247. - padding.left()
  248. - padding.right();
  249. _transformButtons->resize(w, _buttonHeight);
  250. _paintBottomButtons->resize(w, _buttonHeight);
  251. _paintTopButtons->resize(w, _buttonHeight);
  252. const auto buttonsTop = bottomButtonsTop();
  253. const auto &current = _transformButtons->isHidden()
  254. ? _paintBottomButtons
  255. : _transformButtons;
  256. current->moveToLeft(
  257. (size.width() - current->width()) / 2,
  258. buttonsTop);
  259. if (_about) {
  260. const auto &margin = st::photoEditorAboutMargin;
  261. const auto skip = st::photoEditorCropPointSize;
  262. _about->resizeToWidth(
  263. size.width() - margin.left() - margin.right());
  264. _about->moveToLeft(
  265. (size.width() - _about->width()) / 2,
  266. margin.top() - skip);
  267. }
  268. }, lifetime());
  269. _mode.changes(
  270. ) | rpl::start_with_next([=](const PhotoEditorMode &mode) {
  271. if (mode.mode == PhotoEditorMode::Mode::Out) {
  272. return;
  273. }
  274. const auto animated = (_paintBottomButtons->isVisible()
  275. == _transformButtons->isVisible())
  276. ? anim::type::instant
  277. : anim::type::normal;
  278. showAnimated(mode.mode, animated);
  279. }, lifetime());
  280. _paintBottomButtons->positionValue(
  281. ) | rpl::start_with_next([=](const QPoint &containerPos) {
  282. _paintTopButtons->moveToLeft(
  283. containerPos.x(),
  284. containerPos.y()
  285. - st::photoEditorControlsCenterSkip
  286. - _paintTopButtons->height());
  287. }, _paintBottomButtons->lifetime());
  288. _paintBottomButtons->shownValue(
  289. ) | rpl::start_with_next([=](bool shown) {
  290. _paintTopButtons->setVisible(shown);
  291. }, _paintBottomButtons->lifetime());
  292. controllers->undoController->setPerformRequestChanges(rpl::merge(
  293. _undoButton->clicks() | rpl::map_to(Undo::Undo),
  294. _redoButton->clicks() | rpl::map_to(Undo::Redo),
  295. _keyPresses.events(
  296. ) | rpl::filter([=](not_null<QKeyEvent*> e) {
  297. using Mode = PhotoEditorMode::Mode;
  298. return (e->matches(QKeySequence::Undo)
  299. && !_undoButton->isHidden()
  300. && !_undoButton->testAttribute(
  301. Qt::WA_TransparentForMouseEvents)
  302. && (_mode.current().mode == Mode::Paint))
  303. || (e->matches(QKeySequence::Redo)
  304. && !_redoButton->isHidden()
  305. && !_redoButton->testAttribute(
  306. Qt::WA_TransparentForMouseEvents)
  307. && (_mode.current().mode == Mode::Paint));
  308. }) | rpl::map([=](not_null<QKeyEvent*> e) {
  309. return e->matches(QKeySequence::Undo) ? Undo::Undo : Undo::Redo;
  310. })));
  311. controllers->undoController->canPerformChanges(
  312. ) | rpl::start_with_next([=](const UndoController::EnableRequest &r) {
  313. const auto isUndo = (r.command == Undo::Undo);
  314. const auto &button = isUndo ? _undoButton : _redoButton;
  315. button->setAttribute(Qt::WA_TransparentForMouseEvents, !r.enable);
  316. if (!r.enable) {
  317. button->clearState();
  318. }
  319. button->setIconOverride(r.enable
  320. ? nullptr
  321. : isUndo
  322. ? &st::photoEditorUndoButtonInactive
  323. : &st::photoEditorRedoButtonInactive);
  324. }, lifetime());
  325. if (_stickersButton) {
  326. using ShowRequest = StickersPanelController::ShowRequest;
  327. controllers->stickersPanelController->setShowRequestChanges(
  328. rpl::merge(
  329. _mode.value(
  330. ) | rpl::map_to(ShowRequest::HideFast),
  331. _stickersButton->clicks(
  332. ) | rpl::map_to(ShowRequest::ToggleAnimated)
  333. ));
  334. controllers->stickersPanelController->setMoveRequestChanges(
  335. _paintBottomButtons->positionValue(
  336. ) | rpl::map([=](const QPoint &containerPos) {
  337. return QPoint(
  338. (x() + width()) / 2,
  339. y() + containerPos.y() + _stickersButton->y());
  340. }));
  341. controllers->stickersPanelController->panelShown(
  342. ) | rpl::start_with_next([=](bool shown) {
  343. const auto icon = shown
  344. ? &st::photoEditorStickersIconActive
  345. : nullptr;
  346. _stickersButton->setIconOverride(icon, icon);
  347. }, _stickersButton->lifetime());
  348. }
  349. rpl::single(rpl::empty) | rpl::skip(
  350. modifications.flipped ? 0 : 1
  351. ) | rpl::then(
  352. _flipButton->clicks() | rpl::to_empty
  353. ) | rpl::start_with_next([=] {
  354. _flipped = !_flipped;
  355. const auto icon = _flipped ? &st::photoEditorFlipIconActive : nullptr;
  356. _flipButton->setIconOverride(icon, icon);
  357. }, _flipButton->lifetime());
  358. }
  359. rpl::producer<int> PhotoEditorControls::rotateRequests() const {
  360. return _rotateButton->clicks() | rpl::map_to(90);
  361. }
  362. rpl::producer<> PhotoEditorControls::flipRequests() const {
  363. return _flipButton->clicks() | rpl::to_empty;
  364. }
  365. rpl::producer<> PhotoEditorControls::paintModeRequests() const {
  366. return _paintModeButton->clicks() | rpl::to_empty;
  367. }
  368. rpl::producer<> PhotoEditorControls::doneRequests() const {
  369. return rpl::merge(
  370. _transformDone->clicks() | rpl::to_empty,
  371. _paintDone->clicks() | rpl::to_empty,
  372. _keyPresses.events(
  373. ) | rpl::filter([=](not_null<QKeyEvent*> e) {
  374. const auto key = e->key();
  375. return ((key == Qt::Key_Enter) || (key == Qt::Key_Return))
  376. && !_toggledBarAnimation.animating();
  377. }) | rpl::to_empty);
  378. }
  379. rpl::producer<> PhotoEditorControls::cancelRequests() const {
  380. return rpl::merge(
  381. _transformCancel->clicks() | rpl::to_empty,
  382. _paintCancel->clicks() | rpl::to_empty,
  383. _keyPresses.events(
  384. ) | rpl::filter([=](not_null<QKeyEvent*> e) {
  385. const auto key = e->key();
  386. return (key == Qt::Key_Escape)
  387. && !_toggledBarAnimation.animating();
  388. }) | rpl::to_empty);
  389. }
  390. int PhotoEditorControls::bottomButtonsTop() const {
  391. return height()
  392. - st::photoEditorControlsBottomSkip
  393. - _transformButtons->height();
  394. }
  395. void PhotoEditorControls::showAnimated(
  396. PhotoEditorMode::Mode mode,
  397. anim::type animated) {
  398. using Mode = PhotoEditorMode::Mode;
  399. const auto duration = st::photoEditorBarAnimationDuration;
  400. const auto isTransform = (mode == Mode::Transform);
  401. if (_about) {
  402. _about->toggle(isTransform, animated);
  403. }
  404. const auto buttonsLeft = (width() - _transformButtons->width()) / 2;
  405. const auto buttonsTop = bottomButtonsTop();
  406. const auto visibleBar = _transformButtons->isVisible()
  407. ? _transformButtons.get()
  408. : _paintBottomButtons.get();
  409. const auto shouldVisibleBar = isTransform
  410. ? _transformButtons.get()
  411. : _paintBottomButtons.get(); // Mode::Paint
  412. const auto computeTop = [=](float64 progress) {
  413. return anim::interpolate(buttonsTop, height() * 2, progress);
  414. };
  415. const auto showShouldVisibleBar = [=] {
  416. _toggledBarAnimation.stop();
  417. auto callback = [=](float64 value) {
  418. shouldVisibleBar->moveToLeft(buttonsLeft, computeTop(value));
  419. };
  420. if (animated == anim::type::instant) {
  421. callback(1.);
  422. } else {
  423. _toggledBarAnimation.start(
  424. std::move(callback),
  425. 1.,
  426. 0.,
  427. duration);
  428. }
  429. };
  430. auto animationCallback = [=](float64 value) {
  431. if (shouldVisibleBar == visibleBar) {
  432. showShouldVisibleBar();
  433. return;
  434. }
  435. visibleBar->moveToLeft(buttonsLeft, computeTop(value));
  436. if (value == 1.) {
  437. shouldVisibleBar->show();
  438. shouldVisibleBar->moveToLeft(buttonsLeft, computeTop(1.));
  439. visibleBar->hide();
  440. showShouldVisibleBar();
  441. }
  442. };
  443. if (animated == anim::type::instant) {
  444. animationCallback(1.);
  445. } else {
  446. _toggledBarAnimation.start(
  447. std::move(animationCallback),
  448. 0.,
  449. 1.,
  450. duration);
  451. }
  452. }
  453. void PhotoEditorControls::applyMode(const PhotoEditorMode &mode) {
  454. _mode = mode;
  455. }
  456. rpl::producer<QPoint> PhotoEditorControls::colorLinePositionValue() const {
  457. return rpl::merge(
  458. geometryValue() | rpl::to_empty,
  459. _paintTopButtons->geometryValue() | rpl::to_empty
  460. ) | rpl::map([=] {
  461. const auto r = _paintTopButtons->geometry();
  462. return mapToParent(r.topLeft())
  463. + QPoint(r.width() / 2, r.height() / 2);
  464. });
  465. }
  466. rpl::producer<bool> PhotoEditorControls::colorLineShownValue() const {
  467. return _paintTopButtons->shownValue();
  468. }
  469. bool PhotoEditorControls::handleKeyPress(not_null<QKeyEvent*> e) const {
  470. _keyPresses.fire(std::move(e));
  471. return true;
  472. }
  473. bool PhotoEditorControls::animating() const {
  474. return _toggledBarAnimation.animating();
  475. }
  476. } // namespace Editor