resize_area.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 "ui/rp_widget.h"
  9. namespace Ui {
  10. class ResizeArea : public RpWidget {
  11. public:
  12. ResizeArea(QWidget *parent) : RpWidget(parent) {
  13. setCursor(style::cur_sizehor);
  14. }
  15. rpl::producer<int> moveLeft() const {
  16. return _moveLeft.events();
  17. }
  18. template <typename Callback>
  19. void addMoveLeftCallback(Callback &&callback) {
  20. moveLeft(
  21. ) | rpl::start_with_next(
  22. std::forward<Callback>(callback),
  23. lifetime());
  24. }
  25. rpl::producer<> moveFinished() const {
  26. return _moveFinished.events();
  27. }
  28. template <typename Callback>
  29. void addMoveFinishedCallback(Callback &&callback) {
  30. moveFinished(
  31. ) | rpl::start_with_next(
  32. std::forward<Callback>(callback),
  33. lifetime());
  34. }
  35. ~ResizeArea() {
  36. moveFinish();
  37. }
  38. protected:
  39. void mousePressEvent(QMouseEvent *e) override {
  40. if (e->button() == Qt::LeftButton) {
  41. _moving = true;
  42. _moveStartLeft = e->pos().x();
  43. }
  44. }
  45. void mouseReleaseEvent(QMouseEvent *e) override {
  46. if (e->button() == Qt::LeftButton) {
  47. moveFinish();
  48. }
  49. }
  50. void mouseMoveEvent(QMouseEvent *e) override {
  51. if (_moving) {
  52. _moveLeft.fire(e->globalPos().x() - _moveStartLeft);
  53. }
  54. }
  55. private:
  56. void moveFinish() {
  57. if (base::take(_moving)) {
  58. _moveFinished.fire({});
  59. }
  60. }
  61. rpl::event_stream<int> _moveLeft;
  62. rpl::event_stream<> _moveFinished;
  63. int _moveStartLeft = 0;
  64. bool _moving = false;
  65. };
  66. } // namespace Ui