scroll_area.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911
  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/widgets/scroll_area.h"
  8. #include "ui/painter.h"
  9. #include "ui/qt_weak_factory.h"
  10. #include "ui/ui_utility.h"
  11. #include "base/qt/qt_common_adapters.h"
  12. #include "base/debug_log.h"
  13. #include <QtWidgets/QScrollBar>
  14. #include <QtWidgets/QApplication>
  15. #include <QtGui/QGuiApplication>
  16. #include <QtGui/QWindow>
  17. namespace Ui {
  18. namespace {
  19. [[nodiscard]] int ComputeScrollTo(
  20. int toFrom,
  21. int toTill,
  22. int toMin,
  23. int toMax,
  24. int current,
  25. int size) {
  26. if (toFrom < toMin) {
  27. toFrom = toMin;
  28. } else if (toFrom > toMax) {
  29. toFrom = toMax;
  30. }
  31. const auto exact = (toTill < 0);
  32. const auto curBottom = current + size;
  33. auto scToFrom = toFrom;
  34. if (!exact && toFrom >= current) {
  35. if (toTill < toFrom) {
  36. toTill = toFrom;
  37. }
  38. if (toTill <= curBottom) {
  39. return current;
  40. }
  41. scToFrom = toTill - size;
  42. if (scToFrom > toFrom) {
  43. scToFrom = toFrom;
  44. }
  45. if (scToFrom == current) {
  46. return current;
  47. }
  48. } else {
  49. scToFrom = toFrom;
  50. }
  51. return scToFrom;
  52. }
  53. } // namespace
  54. // flick scroll taken from http://qt-project.org/doc/qt-4.8/demos-embedded-anomaly-src-flickcharm-cpp.html
  55. ScrollShadow::ScrollShadow(ScrollArea *parent, const style::ScrollArea *st)
  56. : QWidget(parent)
  57. , _st(st) {
  58. Expects(_st != nullptr);
  59. Expects(_st->shColor.get() != nullptr);
  60. setVisible(false);
  61. }
  62. void ScrollShadow::paintEvent(QPaintEvent *e) {
  63. QPainter p(this);
  64. p.fillRect(rect(), _st->shColor);
  65. }
  66. void ScrollShadow::changeVisibility(bool shown) {
  67. setVisible(shown);
  68. }
  69. ScrollBar::ScrollBar(ScrollArea *parent, bool vert, const style::ScrollArea *st) : TWidget(parent)
  70. , _st(st)
  71. , _vertical(vert)
  72. , _hiding(_st->hiding != 0)
  73. , _connected(vert ? parent->verticalScrollBar() : parent->horizontalScrollBar())
  74. , _scrollMax(_connected->maximum())
  75. , _hideTimer([=] { hideTimer(); }) {
  76. recountSize();
  77. connect(_connected, &QAbstractSlider::valueChanged, [=] {
  78. area()->scrolled();
  79. updateBar();
  80. });
  81. connect(_connected, &QAbstractSlider::rangeChanged, [=] {
  82. area()->innerResized();
  83. updateBar();
  84. });
  85. updateBar();
  86. }
  87. void ScrollBar::recountSize() {
  88. setGeometry(_vertical
  89. ? QRect(
  90. style::RightToLeft() ? 0 : (area()->width() - _st->width),
  91. _st->deltat,
  92. _st->width,
  93. area()->height() - _st->deltat - _st->deltab)
  94. : QRect(
  95. _st->deltat,
  96. area()->height() - _st->width,
  97. area()->width() - _st->deltat - _st->deltab,
  98. _st->width));
  99. }
  100. void ScrollBar::updateBar(bool force) {
  101. QRect newBar;
  102. if (_connected->maximum() != _scrollMax) {
  103. const auto oldMax = _scrollMax;
  104. const auto newMax = _connected->maximum();
  105. _scrollMax = newMax;
  106. area()->rangeChanged(oldMax, newMax, _vertical);
  107. }
  108. if (_vertical) {
  109. const auto sh = area()->scrollHeight();
  110. const auto rh = height();
  111. auto h = sh ? int32((rh * int64(area()->height())) / sh) : 0;
  112. if (_st->barHidden
  113. || h >= rh
  114. || !area()->scrollTopMax()
  115. || rh < _st->minHeight) {
  116. if (!isHidden()) {
  117. hide();
  118. }
  119. const auto newTopSh = (_st->topsh < 0);
  120. const auto newBottomSh = (_st->bottomsh < 0);
  121. if (newTopSh != _topSh || force) {
  122. _shadowVisibilityChanged.fire({
  123. .type = ScrollShadow::Type::Top,
  124. .visible = (_topSh = newTopSh),
  125. });
  126. }
  127. if (newBottomSh != _bottomSh || force) {
  128. _shadowVisibilityChanged.fire({
  129. .type = ScrollShadow::Type::Bottom,
  130. .visible = (_bottomSh = newBottomSh),
  131. });
  132. }
  133. return;
  134. }
  135. if (h <= _st->minHeight) {
  136. h = _st->minHeight;
  137. }
  138. const auto stm = area()->scrollTopMax();
  139. const auto y = stm
  140. ? std::min(
  141. int32(((rh - h) * int64(area()->scrollTop())) / stm),
  142. rh - h)
  143. : 0;
  144. newBar = QRect(_st->deltax, y, width() - 2 * _st->deltax, h);
  145. } else {
  146. const auto sw = area()->scrollWidth();
  147. const auto rw = width();
  148. auto w = sw ? int32((rw * int64(area()->width())) / sw) : 0;
  149. if (_st->barHidden
  150. || w >= rw
  151. || !area()->scrollLeftMax()
  152. || rw < _st->minHeight) {
  153. if (!isHidden()) {
  154. hide();
  155. }
  156. return;
  157. }
  158. if (w <= _st->minHeight) {
  159. w = _st->minHeight;
  160. }
  161. const auto slm = area()->scrollLeftMax();
  162. const auto x = slm
  163. ? std::min(
  164. int32(((rw - w) * int64(area()->scrollLeft())) / slm),
  165. rw - w)
  166. : 0;
  167. newBar = QRect(x, _st->deltax, w, height() - 2 * _st->deltax);
  168. }
  169. if (newBar != _bar) {
  170. _bar = newBar;
  171. update();
  172. }
  173. if (_vertical) {
  174. const auto newTopSh = (_st->topsh < 0)
  175. || (area()->scrollTop() > _st->topsh);
  176. const auto newBottomSh = (_st->bottomsh < 0)
  177. || (area()->scrollTop()
  178. < area()->scrollTopMax() - _st->bottomsh);
  179. if (newTopSh != _topSh || force) {
  180. _shadowVisibilityChanged.fire({
  181. .type = ScrollShadow::Type::Top,
  182. .visible = (_topSh = newTopSh),
  183. });
  184. }
  185. if (newBottomSh != _bottomSh || force) {
  186. _shadowVisibilityChanged.fire({
  187. .type = ScrollShadow::Type::Bottom,
  188. .visible = (_bottomSh = newBottomSh),
  189. });
  190. }
  191. }
  192. if (isHidden()) show();
  193. }
  194. void ScrollBar::hideTimer() {
  195. if (!_hiding) {
  196. _hiding = true;
  197. _a_opacity.start([this] { update(); }, 1., 0., _st->duration);
  198. }
  199. }
  200. ScrollArea *ScrollBar::area() {
  201. return static_cast<ScrollArea*>(parentWidget());
  202. }
  203. void ScrollBar::setOver(bool over) {
  204. if (_over != over) {
  205. auto wasOver = (_over || _moving);
  206. _over = over;
  207. auto nowOver = (_over || _moving);
  208. if (wasOver != nowOver) {
  209. _a_over.start([this] { update(); }, nowOver ? 0. : 1., nowOver ? 1. : 0., _st->duration);
  210. }
  211. if (nowOver && _hiding) {
  212. _hiding = false;
  213. _a_opacity.start([this] { update(); }, 0., 1., _st->duration);
  214. }
  215. }
  216. }
  217. void ScrollBar::setOverBar(bool overbar) {
  218. if (_overbar != overbar) {
  219. auto wasBarOver = (_overbar || _moving);
  220. _overbar = overbar;
  221. auto nowBarOver = (_overbar || _moving);
  222. if (wasBarOver != nowBarOver) {
  223. _a_barOver.start([this] { update(); }, nowBarOver ? 0. : 1., nowBarOver ? 1. : 0., _st->duration);
  224. }
  225. }
  226. }
  227. void ScrollBar::setMoving(bool moving) {
  228. if (_moving != moving) {
  229. auto wasOver = (_over || _moving);
  230. auto wasBarOver = (_overbar || _moving);
  231. _moving = moving;
  232. auto nowBarOver = (_overbar || _moving);
  233. if (wasBarOver != nowBarOver) {
  234. _a_barOver.start([this] { update(); }, nowBarOver ? 0. : 1., nowBarOver ? 1. : 0., _st->duration);
  235. }
  236. auto nowOver = (_over || _moving);
  237. if (wasOver != nowOver) {
  238. _a_over.start([this] { update(); }, nowOver ? 0. : 1., nowOver ? 1. : 0., _st->duration);
  239. }
  240. if (!nowOver && _st->hiding && !_hiding) {
  241. _hideTimer.callOnce(_hideIn);
  242. }
  243. }
  244. }
  245. void ScrollBar::paintEvent(QPaintEvent *e) {
  246. if (!_bar.width() && !_bar.height()) {
  247. hide();
  248. return;
  249. }
  250. auto opacity = _a_opacity.value(_hiding ? 0. : 1.);
  251. if (opacity == 0.) return;
  252. QPainter p(this);
  253. auto deltal = _vertical ? _st->deltax : 0, deltar = _vertical ? _st->deltax : 0;
  254. auto deltat = _vertical ? 0 : _st->deltax, deltab = _vertical ? 0 : _st->deltax;
  255. p.setPen(Qt::NoPen);
  256. auto bg = anim::color(_st->bg, _st->bgOver, _a_over.value((_over || _moving) ? 1. : 0.));
  257. bg.setAlpha(anim::interpolate(0, bg.alpha(), opacity));
  258. auto bar = anim::color(_st->barBg, _st->barBgOver, _a_barOver.value((_overbar || _moving) ? 1. : 0.));
  259. bar.setAlpha(anim::interpolate(0, bar.alpha(), opacity));
  260. const auto outer = QRect(deltal, deltat, width() - deltal - deltar, height() - deltat - deltab);
  261. const auto radius = (_st->round < 0)
  262. ? (std::min(outer.width(), outer.height()) / 2.)
  263. : _st->round;
  264. if (radius) {
  265. PainterHighQualityEnabler hq(p);
  266. p.setBrush(bg);
  267. p.drawRoundedRect(outer, radius, radius);
  268. p.setBrush(bar);
  269. p.drawRoundedRect(_bar, radius, radius);
  270. } else {
  271. p.fillRect(outer, bg);
  272. p.fillRect(_bar, bar);
  273. }
  274. }
  275. void ScrollBar::hideTimeout(crl::time dt) {
  276. if (_hiding && dt > 0) {
  277. _hiding = false;
  278. _a_opacity.start([this] { update(); }, 0., 1., _st->duration);
  279. }
  280. _hideIn = dt;
  281. if (!_moving) {
  282. _hideTimer.callOnce(_hideIn);
  283. }
  284. }
  285. void ScrollBar::enterEventHook(QEnterEvent *e) {
  286. _hideTimer.cancel();
  287. setMouseTracking(true);
  288. setOver(true);
  289. }
  290. void ScrollBar::leaveEventHook(QEvent *e) {
  291. if (!_moving) {
  292. setMouseTracking(false);
  293. }
  294. setOver(false);
  295. setOverBar(false);
  296. if (_st->hiding && !_hiding) {
  297. _hideTimer.callOnce(_hideIn);
  298. }
  299. }
  300. void ScrollBar::mouseMoveEvent(QMouseEvent *e) {
  301. setOverBar(_bar.contains(e->pos()));
  302. if (_moving) {
  303. int delta = 0, barDelta = _vertical ? (area()->height() - _bar.height()) : (area()->width() - _bar.width());
  304. if (barDelta > 0) {
  305. QPoint d = (e->globalPos() - _dragStart);
  306. delta = int32((_vertical ? (d.y() * int64(area()->scrollTopMax())) : (d.x() * int64(area()->scrollLeftMax()))) / barDelta);
  307. }
  308. _connected->setValue(_startFrom + delta);
  309. }
  310. }
  311. void ScrollBar::mousePressEvent(QMouseEvent *e) {
  312. if (!width() || !height()) return;
  313. _dragStart = e->globalPos();
  314. area()->setMovingByScrollBar(true);
  315. setMoving(true);
  316. if (_overbar) {
  317. _startFrom = _connected->value();
  318. } else {
  319. int32 val = _vertical ? e->pos().y() : e->pos().x(), div = _vertical ? height() : width();
  320. val = (val <= _st->deltat) ? 0 : (val - _st->deltat);
  321. div = (div <= _st->deltat + _st->deltab) ? 1 : (div - _st->deltat - _st->deltab);
  322. _startFrom = _vertical ? int32((val * int64(area()->scrollTopMax())) / div) : ((val * int64(area()->scrollLeftMax())) / div);
  323. _connected->setValue(_startFrom);
  324. setOverBar(true);
  325. }
  326. }
  327. void ScrollBar::mouseReleaseEvent(QMouseEvent *e) {
  328. if (_moving) {
  329. area()->setMovingByScrollBar(false);
  330. setMoving(false);
  331. }
  332. if (!_over) {
  333. setMouseTracking(false);
  334. }
  335. }
  336. void ScrollBar::resizeEvent(QResizeEvent *e) {
  337. updateBar();
  338. }
  339. void ScrollBar::wheelEvent(QWheelEvent *e) {
  340. static_cast<ScrollArea*>(parentWidget())->viewportEvent(e);
  341. }
  342. auto ScrollBar::shadowVisibilityChanged() const
  343. -> rpl::producer<ScrollBar::ShadowVisibility> {
  344. return _shadowVisibilityChanged.events();
  345. }
  346. ScrollArea::ScrollArea(
  347. QWidget *parent,
  348. const style::ScrollArea &st,
  349. bool handleTouch)
  350. : Parent(parent)
  351. , _st(st)
  352. , _horizontalBar(this, false, &_st)
  353. , _verticalBar(this, true, &_st)
  354. , _topShadow(this, &_st)
  355. , _bottomShadow(this, &_st)
  356. , _touchEnabled(handleTouch) {
  357. setLayoutDirection(style::LayoutDirection());
  358. setFocusPolicy(Qt::NoFocus);
  359. _verticalBar->shadowVisibilityChanged(
  360. ) | rpl::start_with_next([=](const ScrollBar::ShadowVisibility &data) {
  361. ((data.type == ScrollShadow::Type::Top)
  362. ? _topShadow
  363. : _bottomShadow)->changeVisibility(data.visible);
  364. }, lifetime());
  365. _verticalBar->updateBar(true);
  366. verticalScrollBar()->setSingleStep(style::ConvertScale(verticalScrollBar()->singleStep()));
  367. horizontalScrollBar()->setSingleStep(style::ConvertScale(horizontalScrollBar()->singleStep()));
  368. setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  369. setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  370. setFrameStyle(int(QFrame::NoFrame) | QFrame::Plain);
  371. viewport()->setAutoFillBackground(false);
  372. _horizontalValue = horizontalScrollBar()->value();
  373. _verticalValue = verticalScrollBar()->value();
  374. if (_touchEnabled) {
  375. viewport()->setAttribute(Qt::WA_AcceptTouchEvents);
  376. _touchTimer.setCallback([=] { _touchRightButton = true; });
  377. _touchScrollTimer.setCallback([=] { touchScrollTimer(); });
  378. }
  379. }
  380. void ScrollArea::touchDeaccelerate(int32 elapsed) {
  381. int32 x = _touchSpeed.x();
  382. int32 y = _touchSpeed.y();
  383. _touchSpeed.setX((x == 0) ? x : (x > 0) ? qMax(0, x - elapsed) : qMin(0, x + elapsed));
  384. _touchSpeed.setY((y == 0) ? y : (y > 0) ? qMax(0, y - elapsed) : qMin(0, y + elapsed));
  385. }
  386. void ScrollArea::scrolled() {
  387. if (const auto inner = widget()) {
  388. SendPendingMoveResizeEvents(inner);
  389. }
  390. bool em = false;
  391. int horizontalValue = horizontalScrollBar()->value();
  392. int verticalValue = verticalScrollBar()->value();
  393. if (_horizontalValue != horizontalValue) {
  394. if (_disabled) {
  395. horizontalScrollBar()->setValue(_horizontalValue);
  396. } else {
  397. _horizontalValue = horizontalValue;
  398. if (_st.hiding) {
  399. _horizontalBar->hideTimeout(_st.hiding);
  400. }
  401. em = true;
  402. }
  403. }
  404. if (_verticalValue != verticalValue) {
  405. if (_disabled) {
  406. verticalScrollBar()->setValue(_verticalValue);
  407. } else {
  408. _verticalValue = verticalValue;
  409. if (_st.hiding) {
  410. _verticalBar->hideTimeout(_st.hiding);
  411. }
  412. em = true;
  413. _scrollTopUpdated.fire_copy(_verticalValue);
  414. }
  415. }
  416. if (em) {
  417. _scrolls.fire({});
  418. if (!_movingByScrollBar) {
  419. SendSynteticMouseEvent(this, QEvent::MouseMove, Qt::NoButton);
  420. }
  421. }
  422. }
  423. void ScrollArea::innerResized() {
  424. _innerResizes.fire({});
  425. }
  426. int ScrollArea::scrollWidth() const {
  427. QWidget *w(widget());
  428. return w ? qMax(w->width(), width()) : width();
  429. }
  430. int ScrollArea::scrollHeight() const {
  431. QWidget *w(widget());
  432. return w ? qMax(w->height(), height()) : height();
  433. }
  434. int ScrollArea::scrollLeftMax() const {
  435. return scrollWidth() - width();
  436. }
  437. int ScrollArea::scrollTopMax() const {
  438. return scrollHeight() - height();
  439. }
  440. int ScrollArea::scrollLeft() const {
  441. return _horizontalValue;
  442. }
  443. int ScrollArea::scrollTop() const {
  444. return _verticalValue;
  445. }
  446. void ScrollArea::touchScrollTimer() {
  447. auto nowTime = crl::now();
  448. if (_touchScrollState == TouchScrollState::Acceleration && _touchWaitingAcceleration && (nowTime - _touchAccelerationTime) > 40) {
  449. _touchScrollState = TouchScrollState::Manual;
  450. touchResetSpeed();
  451. } else if (_touchScrollState == TouchScrollState::Auto || _touchScrollState == TouchScrollState::Acceleration) {
  452. int32 elapsed = int32(nowTime - _touchTime);
  453. QPoint delta = _touchSpeed * elapsed / 1000;
  454. bool hasScrolled = touchScroll(delta);
  455. if (_touchSpeed.isNull() || !hasScrolled) {
  456. _touchScrollState = TouchScrollState::Manual;
  457. _touchScroll = false;
  458. _touchScrollTimer.cancel();
  459. } else {
  460. _touchTime = nowTime;
  461. }
  462. touchDeaccelerate(elapsed);
  463. }
  464. }
  465. void ScrollArea::touchUpdateSpeed() {
  466. const auto nowTime = crl::now();
  467. if (_touchPrevPosValid) {
  468. const int elapsed = nowTime - _touchSpeedTime;
  469. if (elapsed) {
  470. const QPoint newPixelDiff = (_touchPos - _touchPrevPos);
  471. const QPoint pixelsPerSecond = newPixelDiff * (1000 / elapsed);
  472. // fingers are inacurates, we ignore small changes to avoid stopping the autoscroll because
  473. // of a small horizontal offset when scrolling vertically
  474. const int newSpeedY = (qAbs(pixelsPerSecond.y()) > kFingerAccuracyThreshold) ? pixelsPerSecond.y() : 0;
  475. const int newSpeedX = (qAbs(pixelsPerSecond.x()) > kFingerAccuracyThreshold) ? pixelsPerSecond.x() : 0;
  476. if (_touchScrollState == TouchScrollState::Auto) {
  477. const int oldSpeedY = _touchSpeed.y();
  478. const int oldSpeedX = _touchSpeed.x();
  479. if ((oldSpeedY <= 0 && newSpeedY <= 0) || ((oldSpeedY >= 0 && newSpeedY >= 0)
  480. && (oldSpeedX <= 0 && newSpeedX <= 0)) || (oldSpeedX >= 0 && newSpeedX >= 0)) {
  481. _touchSpeed.setY(std::clamp((oldSpeedY + (newSpeedY / 4)), -kMaxScrollAccelerated, +kMaxScrollAccelerated));
  482. _touchSpeed.setX(std::clamp((oldSpeedX + (newSpeedX / 4)), -kMaxScrollAccelerated, +kMaxScrollAccelerated));
  483. } else {
  484. _touchSpeed = QPoint();
  485. }
  486. } else {
  487. // we average the speed to avoid strange effects with the last delta
  488. if (!_touchSpeed.isNull()) {
  489. _touchSpeed.setX(std::clamp((_touchSpeed.x() / 4) + (newSpeedX * 3 / 4), -kMaxScrollFlick, +kMaxScrollFlick));
  490. _touchSpeed.setY(std::clamp((_touchSpeed.y() / 4) + (newSpeedY * 3 / 4), -kMaxScrollFlick, +kMaxScrollFlick));
  491. } else {
  492. _touchSpeed = QPoint(newSpeedX, newSpeedY);
  493. }
  494. }
  495. }
  496. } else {
  497. _touchPrevPosValid = true;
  498. }
  499. _touchSpeedTime = nowTime;
  500. _touchPrevPos = _touchPos;
  501. }
  502. void ScrollArea::touchResetSpeed() {
  503. _touchSpeed = QPoint();
  504. _touchPrevPosValid = false;
  505. }
  506. bool ScrollArea::eventHook(QEvent *e) {
  507. const auto was = (e->type() == QEvent::LayoutRequest)
  508. ? verticalScrollBar()->minimum()
  509. : 0;
  510. const auto result = RpWidgetBase<QScrollArea>::eventHook(e);
  511. if (was) {
  512. // Because LayoutRequest resets custom-set minimum allowed value.
  513. verticalScrollBar()->setMinimum(was);
  514. }
  515. return result;
  516. }
  517. bool ScrollArea::eventFilter(QObject *obj, QEvent *e) {
  518. const auto result = QScrollArea::eventFilter(obj, e);
  519. return (obj == widget() && filterOutTouchEvent(e)) || result;
  520. }
  521. bool ScrollArea::viewportEvent(QEvent *e) {
  522. if (filterOutTouchEvent(e)) {
  523. return true;
  524. } else if (e->type() == QEvent::Wheel) {
  525. if (_customWheelProcess
  526. && _customWheelProcess(static_cast<QWheelEvent*>(e))) {
  527. return true;
  528. }
  529. }
  530. return QScrollArea::viewportEvent(e);
  531. }
  532. bool ScrollArea::filterOutTouchEvent(QEvent *e) {
  533. const auto type = e->type();
  534. if (type == QEvent::TouchBegin
  535. || type == QEvent::TouchUpdate
  536. || type == QEvent::TouchEnd
  537. || type == QEvent::TouchCancel) {
  538. const auto ev = static_cast<QTouchEvent*>(e);
  539. if (ev->device()->type() == base::TouchDevice::TouchScreen) {
  540. if (_customTouchProcess && _customTouchProcess(ev)) {
  541. return true;
  542. } else if (_touchEnabled) {
  543. touchEvent(ev);
  544. return true;
  545. }
  546. }
  547. }
  548. return false;
  549. }
  550. void ScrollArea::touchEvent(QTouchEvent *e) {
  551. if (!e->touchPoints().isEmpty()) {
  552. _touchPrevPos = _touchPos;
  553. _touchPos = e->touchPoints().cbegin()->screenPos().toPoint();
  554. }
  555. switch (e->type()) {
  556. case QEvent::TouchBegin: {
  557. if (_touchPress || e->touchPoints().isEmpty()) return;
  558. _touchPress = true;
  559. if (_touchScrollState == TouchScrollState::Auto) {
  560. _touchScrollState = TouchScrollState::Acceleration;
  561. _touchWaitingAcceleration = true;
  562. _touchMaybePressing = false;
  563. _touchAccelerationTime = crl::now();
  564. touchUpdateSpeed();
  565. _touchStart = _touchPos;
  566. } else {
  567. _touchScroll = false;
  568. _touchMaybePressing = true;
  569. _touchTimer.callOnce(QApplication::startDragTime());
  570. }
  571. _touchStart = _touchPrevPos = _touchPos;
  572. _touchRightButton = false;
  573. } break;
  574. case QEvent::TouchUpdate: {
  575. if (!_touchPress) return;
  576. if (!_touchScroll && (_touchPos - _touchStart).manhattanLength() >= QApplication::startDragDistance()) {
  577. _touchTimer.cancel();
  578. _touchScroll = true;
  579. _touchMaybePressing = false;
  580. touchUpdateSpeed();
  581. }
  582. if (_touchScroll) {
  583. if (_touchScrollState == TouchScrollState::Manual) {
  584. touchScrollUpdated(_touchPos);
  585. } else if (_touchScrollState == TouchScrollState::Acceleration) {
  586. touchUpdateSpeed();
  587. _touchAccelerationTime = crl::now();
  588. if (_touchSpeed.isNull()) {
  589. _touchScrollState = TouchScrollState::Manual;
  590. }
  591. }
  592. }
  593. } break;
  594. case QEvent::TouchEnd: {
  595. if (!_touchPress) return;
  596. _touchPress = false;
  597. auto weak = MakeWeak(this);
  598. if (_touchScroll) {
  599. if (_touchScrollState == TouchScrollState::Manual) {
  600. _touchScrollState = TouchScrollState::Auto;
  601. _touchPrevPosValid = false;
  602. _touchScrollTimer.callEach(15);
  603. _touchTime = crl::now();
  604. } else if (_touchScrollState == TouchScrollState::Auto) {
  605. _touchScrollState = TouchScrollState::Manual;
  606. _touchScroll = false;
  607. touchResetSpeed();
  608. } else if (_touchScrollState == TouchScrollState::Acceleration) {
  609. _touchScrollState = TouchScrollState::Auto;
  610. _touchWaitingAcceleration = false;
  611. _touchPrevPosValid = false;
  612. }
  613. } else if (window()) { // one short tap -- like left mouse click, one long tap -- like right mouse click
  614. Qt::MouseButton btn(_touchRightButton ? Qt::RightButton : Qt::LeftButton);
  615. if (weak) SendSynteticMouseEvent(this, QEvent::MouseMove, Qt::NoButton, _touchStart);
  616. if (weak) SendSynteticMouseEvent(this, QEvent::MouseButtonPress, btn, _touchStart);
  617. if (weak) SendSynteticMouseEvent(this, QEvent::MouseButtonRelease, btn, _touchStart);
  618. if (weak && _touchRightButton) {
  619. auto windowHandle = window()->windowHandle();
  620. auto localPoint = windowHandle->mapFromGlobal(_touchStart);
  621. QContextMenuEvent ev(QContextMenuEvent::Mouse, localPoint, _touchStart, QGuiApplication::keyboardModifiers());
  622. ev.setTimestamp(crl::now());
  623. QGuiApplication::sendEvent(windowHandle, &ev);
  624. }
  625. }
  626. if (weak) {
  627. _touchTimer.cancel();
  628. _touchRightButton = false;
  629. _touchMaybePressing = false;
  630. }
  631. } break;
  632. case QEvent::TouchCancel: {
  633. _touchPress = false;
  634. _touchScroll = false;
  635. _touchMaybePressing = false;
  636. _touchScrollState = TouchScrollState::Manual;
  637. _touchTimer.cancel();
  638. } break;
  639. }
  640. }
  641. void ScrollArea::touchScrollUpdated(const QPoint &screenPos) {
  642. _touchPos = screenPos;
  643. touchScroll(_touchPos - _touchPrevPos);
  644. touchUpdateSpeed();
  645. }
  646. void ScrollArea::disableScroll(bool dis) {
  647. _disabled = dis;
  648. if (_disabled && _st.hiding) {
  649. _horizontalBar->hideTimeout(0);
  650. _verticalBar->hideTimeout(0);
  651. }
  652. }
  653. void ScrollArea::scrollContentsBy(int dx, int dy) {
  654. if (_disabled) {
  655. return;
  656. }
  657. QScrollArea::scrollContentsBy(dx, dy);
  658. }
  659. bool ScrollArea::touchScroll(const QPoint &delta) {
  660. const auto top = scrollTop();
  661. const auto topMax = scrollTopMax();
  662. const auto left = scrollLeft();
  663. const auto leftMax = scrollLeftMax();
  664. const auto xAbs = qAbs(delta.x());
  665. const auto yAbs = qAbs(delta.y());
  666. const auto direction = (leftMax <= 0 || yAbs > xAbs)
  667. ? Qt::Vertical
  668. : Qt::Horizontal;
  669. const auto was = (direction == Qt::Vertical) ? top : left;
  670. const auto now = (direction == Qt::Vertical)
  671. ? std::clamp(top - delta.y(), 0, topMax)
  672. : std::clamp(left - delta.x(), 0, leftMax);
  673. if (now == was) {
  674. return false;
  675. } else if (direction == Qt::Vertical) {
  676. scrollToY(now);
  677. } else {
  678. horizontalScrollBar()->setValue(now);
  679. }
  680. return true;
  681. }
  682. void ScrollArea::resizeEvent(QResizeEvent *e) {
  683. QScrollArea::resizeEvent(e);
  684. _horizontalBar->recountSize();
  685. _verticalBar->recountSize();
  686. _topShadow->setGeometry(QRect(0, 0, width(), qAbs(_st.topsh)));
  687. _bottomShadow->setGeometry(QRect(0, height() - qAbs(_st.bottomsh), width(), qAbs(_st.bottomsh)));
  688. _geometryChanged.fire({});
  689. }
  690. void ScrollArea::moveEvent(QMoveEvent *e) {
  691. QScrollArea::moveEvent(e);
  692. _geometryChanged.fire({});
  693. }
  694. void ScrollArea::keyPressEvent(QKeyEvent *e) {
  695. if ((e->key() == Qt::Key_Up || e->key() == Qt::Key_Down)
  696. && (e->modifiers().testFlag(Qt::AltModifier)
  697. || e->modifiers().testFlag(Qt::ControlModifier))) {
  698. e->ignore();
  699. } else if(e->key() == Qt::Key_Escape || e->key() == Qt::Key_Back) {
  700. ((QObject*)widget())->event(e);
  701. } else {
  702. QScrollArea::keyPressEvent(e);
  703. }
  704. }
  705. void ScrollArea::enterEventHook(QEnterEvent *e) {
  706. if (_disabled) return;
  707. if (_st.hiding) {
  708. _horizontalBar->hideTimeout(_st.hiding);
  709. _verticalBar->hideTimeout(_st.hiding);
  710. }
  711. return QScrollArea::enterEvent(e);
  712. }
  713. void ScrollArea::leaveEventHook(QEvent *e) {
  714. if (_st.hiding) {
  715. _horizontalBar->hideTimeout(0);
  716. _verticalBar->hideTimeout(0);
  717. }
  718. return QScrollArea::leaveEvent(e);
  719. }
  720. void ScrollArea::scrollTo(ScrollToRequest request) {
  721. scrollToY(request.ymin, request.ymax);
  722. }
  723. void ScrollArea::scrollToWidget(not_null<QWidget*> widget) {
  724. if (auto local = this->widget()) {
  725. auto globalPosition = widget->mapToGlobal(QPoint(0, 0));
  726. auto localPosition = local->mapFromGlobal(globalPosition);
  727. auto localTop = localPosition.y();
  728. auto localBottom = localTop + widget->height();
  729. scrollToY(localTop, localBottom);
  730. }
  731. }
  732. int ScrollArea::computeScrollToX(int toLeft, int toRight) {
  733. if (const auto inner = widget()) {
  734. SendPendingMoveResizeEvents(inner);
  735. }
  736. SendPendingMoveResizeEvents(this);
  737. return ComputeScrollTo(
  738. toLeft,
  739. toRight,
  740. 0,
  741. scrollLeftMax(),
  742. scrollLeft(),
  743. width());
  744. }
  745. int ScrollArea::computeScrollToY(int toTop, int toBottom) {
  746. if (const auto inner = widget()) {
  747. SendPendingMoveResizeEvents(inner);
  748. }
  749. SendPendingMoveResizeEvents(this);
  750. return ComputeScrollTo(
  751. toTop,
  752. toBottom,
  753. 0,
  754. scrollTopMax(),
  755. scrollTop(),
  756. height());
  757. }
  758. void ScrollArea::scrollToX(int toLeft, int toRight) {
  759. horizontalScrollBar()->setValue(computeScrollToX(toLeft, toRight));
  760. }
  761. void ScrollArea::scrollToY(int toTop, int toBottom) {
  762. verticalScrollBar()->setValue(computeScrollToY(toTop, toBottom));
  763. }
  764. void ScrollArea::doSetOwnedWidget(object_ptr<QWidget> w) {
  765. if (widget() && _touchEnabled) {
  766. widget()->removeEventFilter(this);
  767. if (!_widgetAcceptsTouch) widget()->setAttribute(Qt::WA_AcceptTouchEvents, false);
  768. }
  769. _widget = std::move(w);
  770. QScrollArea::setWidget(_widget);
  771. if (_widget) {
  772. _widget->setAutoFillBackground(false);
  773. if (_touchEnabled) {
  774. _widget->installEventFilter(this);
  775. _widgetAcceptsTouch = _widget->testAttribute(Qt::WA_AcceptTouchEvents);
  776. _widget->setAttribute(Qt::WA_AcceptTouchEvents);
  777. }
  778. }
  779. }
  780. object_ptr<QWidget> ScrollArea::doTakeWidget() {
  781. QScrollArea::takeWidget();
  782. return std::move(_widget);
  783. }
  784. void ScrollArea::rangeChanged(int oldMax, int newMax, bool vertical) {
  785. }
  786. void ScrollArea::updateBars() {
  787. _horizontalBar->updateBar(true);
  788. _verticalBar->updateBar(true);
  789. }
  790. bool ScrollArea::focusNextPrevChild(bool next) {
  791. if (QWidget::focusNextPrevChild(next)) {
  792. // if (QWidget *fw = focusWidget())
  793. // ensureWidgetVisible(fw);
  794. return true;
  795. }
  796. return false;
  797. }
  798. void ScrollArea::setMovingByScrollBar(bool movingByScrollBar) {
  799. _movingByScrollBar = movingByScrollBar;
  800. }
  801. rpl::producer<> ScrollArea::scrolls() const {
  802. return _scrolls.events();
  803. }
  804. rpl::producer<> ScrollArea::innerResizes() const {
  805. return _innerResizes.events();
  806. }
  807. rpl::producer<> ScrollArea::geometryChanged() const {
  808. return _geometryChanged.events();
  809. }
  810. rpl::producer<bool> ScrollArea::touchMaybePressing() const {
  811. return _touchMaybePressing.value();
  812. }
  813. } // namespace Ui