dragging_scroll_manager.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // This file is part of Desktop App Toolkit,
  2. // a set of libraries for developing nice desktop applications.
  3. //
  4. // For license and copyright information please follow this link:
  5. // https://github.com/desktop-app/legal/blob/master/LEGAL
  6. //
  7. #include "ui/dragging_scroll_manager.h"
  8. #include "base/timer.h"
  9. namespace Ui {
  10. namespace {
  11. // 37px per 15ms while select-by-drag.
  12. inline constexpr auto kMaxScrollSpeed = 37;
  13. } // namespace
  14. DraggingScrollManager::DraggingScrollManager() = default;
  15. void DraggingScrollManager::scrollByTimer() {
  16. const auto d = (_delta > 0)
  17. ? std::min(_delta * 3 / 20 + 1, kMaxScrollSpeed)
  18. : std::max(_delta * 3 / 20 - 1, -kMaxScrollSpeed);
  19. _scrolls.fire_copy(d);
  20. }
  21. void DraggingScrollManager::checkDeltaScroll(int delta) {
  22. _delta = delta;
  23. if (_delta) {
  24. if (!_timer) {
  25. _timer = std::make_unique<base::Timer>([=] { scrollByTimer(); });
  26. }
  27. _timer->callEach(15);
  28. } else {
  29. cancel();
  30. }
  31. }
  32. void DraggingScrollManager::checkDeltaScroll(
  33. const QPoint &point,
  34. int top,
  35. int bottom) {
  36. const auto diff = point.y() - top;
  37. checkDeltaScroll((diff < 0)
  38. ? diff
  39. : (point.y() >= bottom)
  40. ? (point.y() - bottom + 1)
  41. : 0);
  42. }
  43. void DraggingScrollManager::cancel() {
  44. if (_timer) {
  45. _timer->cancel();
  46. _timer = nullptr;
  47. }
  48. }
  49. rpl::producer<int> DraggingScrollManager::scrolls() const {
  50. return _scrolls.events();
  51. }
  52. } // namespace Ui