From 17a10cf6bb759bf55c8827552beab10703c4d062 Mon Sep 17 00:00:00 2001 From: 23rd <23rd@vivaldi.net> Date: Mon, 17 Feb 2025 12:39:06 +0300 Subject: [PATCH] Added initial ability to handle history view swipe in both directions. --- .../history/history_view_swipe.cpp | 30 ++++++++++++++++--- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/Telegram/SourceFiles/history/history_view_swipe.cpp b/Telegram/SourceFiles/history/history_view_swipe.cpp index cbe9d65a14..16190bc261 100644 --- a/Telegram/SourceFiles/history/history_view_swipe.cpp +++ b/Telegram/SourceFiles/history/history_view_swipe.cpp @@ -48,6 +48,8 @@ void SetupSwipeHandler( ChatPaintGestureHorizontalData data; SwipeHandlerFinishData finishByTopData; std::optional orientation; + std::optional direction; + int directionInt = 1.; QPointF startAt; QPointF delta; int cursorTop = 0; @@ -75,7 +77,8 @@ void SetupSwipeHandler( base::SafeRound(-overscrollRatio * threshold) )); state->data.msgBareId = state->finishByTopData.msgBareId; - state->data.translation = translation; + state->data.translation = translation + * state->directionInt; state->data.cursorTop = state->cursorTop; update(state->data); }; @@ -90,7 +93,9 @@ void SetupSwipeHandler( const auto processEnd = [=](std::optional delta = {}) { if (state->orientation == Qt::Horizontal) { const auto ratio = std::clamp( - delta.value_or(state->delta).x() / threshold, + delta.value_or(state->delta).x() + / threshold + * state->directionInt, 0., kMaxRatio); if ((ratio >= 1) && state->finishByTopData.callback) { @@ -108,6 +113,9 @@ void SetupSwipeHandler( setOrientation(std::nullopt); state->started = false; state->reached = false; + state->direction = std::nullopt; + state->startAt = {}; + state->delta = {}; }; scroll->scrolls() | rpl::start_with_next([=] { if (state->orientation != Qt::Vertical) { @@ -119,7 +127,19 @@ void SetupSwipeHandler( update(state->data); }; const auto updateWith = [=](UpdateArgs args) { - if (!state->started || state->touch != args.touch) { + if (!state->started + || state->touch != args.touch + || !state->direction) { + state->direction = (args.delta.x() == 0) + ? std::nullopt + : args.delta.x() < 0 + ? std::make_optional(Qt::RightToLeft) + : std::make_optional(Qt::LeftToRight); + state->directionInt = (!state->direction + || (*state->direction) == Qt::LeftToRight) + ? 1 + : -1; + state->started = true; state->touch = args.touch; state->startAt = args.position; @@ -146,7 +166,9 @@ void SetupSwipeHandler( } } else if (*state->orientation == Qt::Horizontal) { state->delta = args.delta; - const auto ratio = args.delta.x() / threshold; + const auto ratio = args.delta.x() + * state->directionInt + / threshold; updateRatio(ratio); constexpr auto kResetReachedOn = 0.95; constexpr auto kBounceDuration = crl::time(500);