continuous_scroll.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 "ui/chat/continuous_scroll.h"
  8. #include <QScrollBar>
  9. #include <QWheelEvent>
  10. namespace Ui {
  11. void ContinuousScroll::wheelEvent(QWheelEvent *e) {
  12. if (_tracking
  13. && !e->angleDelta().isNull()
  14. && (e->angleDelta().y() < 0)
  15. && (scrollTopMax() == scrollTop())) {
  16. _addContentRequests.fire({});
  17. if (base::take(_contentAdded)) {
  18. viewportEvent(e);
  19. }
  20. return;
  21. }
  22. ScrollArea::wheelEvent(e);
  23. }
  24. void ContinuousScroll::setTrackingContent(bool value) {
  25. if (_tracking == value) {
  26. return;
  27. }
  28. _tracking = value;
  29. reconnect();
  30. }
  31. void ContinuousScroll::reconnect() {
  32. if (!_tracking) {
  33. _connection.release();
  34. return;
  35. }
  36. const auto handleAction = [=](int action) {
  37. const auto scroll = verticalScrollBar();
  38. const auto step = (action == QAbstractSlider::SliderSingleStepAdd)
  39. ? scroll->singleStep()
  40. : (action == QAbstractSlider::SliderPageStepAdd)
  41. ? scroll->pageStep()
  42. : 0;
  43. if (!action) {
  44. return;
  45. }
  46. const auto newTop = scrollTop() + step;
  47. if (newTop > scrollTopMax()) {
  48. _addContentRequests.fire({});
  49. if (base::take(_contentAdded)) {
  50. scroll->setSliderPosition(newTop);
  51. }
  52. }
  53. };
  54. _connection = QObject::connect(
  55. verticalScrollBar(),
  56. &QAbstractSlider::actionTriggered,
  57. handleAction);
  58. }
  59. void ContinuousScroll::contentAdded() {
  60. _contentAdded = true;
  61. }
  62. rpl::producer<> ContinuousScroll::addContentRequests() const {
  63. return _addContentRequests.events();
  64. }
  65. } // namespace Ui