Increased speed of swipe-to-back.

This commit is contained in:
23rd 2025-02-18 00:04:51 +03:00 committed by John Preston
parent d02e55da06
commit 6bed3f3f09
8 changed files with 54 additions and 46 deletions

View file

@ -705,25 +705,22 @@ void Widget::setupSwipeBack() {
&& !controller()->openedFolder().current())) { && !controller()->openedFolder().current())) {
return HistoryView::SwipeHandlerFinishData(); return HistoryView::SwipeHandlerFinishData();
} }
return HistoryView::SwipeHandlerFinishData{ return HistoryView::DefaultSwipeBackHandlerFinishData([=] {
.callback = [=] { _swipeBackData = {};
_swipeBackData = {}; if (const auto forum = controller()->shownForum().current()) {
if (const auto forum = controller()->shownForum().current()) { const auto id = controller()->windowId();
const auto id = controller()->windowId(); const auto initial = id.forum();
const auto initial = id.forum(); if (!initial) {
if (!initial) { controller()->closeForum();
controller()->closeForum(); } else if (initial != forum) {
} else if (initial != forum) { controller()->showForum(initial);
controller()->showForum(initial);
}
} else if (controller()->openedFolder().current()) {
if (!controller()->windowId().folder()) {
controller()->closeFolder();
}
} }
}, } else if (controller()->openedFolder().current()) {
.msgBareId = HistoryView::kMsgBareIdSwipeBack, if (!controller()->windowId().folder()) {
}; controller()->closeFolder();
}
}
});
}, nullptr); }, nullptr);
} }

View file

@ -441,10 +441,9 @@ void Widget::setupSwipeReply() {
} }
}, [=](int, Qt::LayoutDirection direction) { }, [=](int, Qt::LayoutDirection direction) {
if (direction == Qt::RightToLeft) { if (direction == Qt::RightToLeft) {
return HistoryView::SwipeHandlerFinishData{ return HistoryView::DefaultSwipeBackHandlerFinishData([=] {
.callback = [=] { controller()->showBackFromStack(); }, controller()->showBackFromStack();
.msgBareId = HistoryView::kMsgBareIdSwipeBack, });
};
} }
return HistoryView::SwipeHandlerFinishData(); return HistoryView::SwipeHandlerFinishData();
}, nullptr); }, nullptr);

View file

@ -563,10 +563,9 @@ void HistoryInner::setupSwipeReplyAndBack() {
int cursorTop, int cursorTop,
Qt::LayoutDirection direction) { Qt::LayoutDirection direction) {
if (direction == Qt::RightToLeft) { if (direction == Qt::RightToLeft) {
return HistoryView::SwipeHandlerFinishData{ return HistoryView::DefaultSwipeBackHandlerFinishData([=] {
.callback = [=] { _controller->showBackFromStack(); }, _controller->showBackFromStack();
.msgBareId = HistoryView::kMsgBareIdSwipeBack, });
};
} }
auto result = HistoryView::SwipeHandlerFinishData(); auto result = HistoryView::SwipeHandlerFinishData();
if (inSelectionMode().inSelectionMode if (inSelectionMode().inSelectionMode

View file

@ -27,6 +27,9 @@ namespace {
constexpr auto kSwipeSlow = 0.2; constexpr auto kSwipeSlow = 0.2;
constexpr auto kMsgBareIdSwipeBack = std::numeric_limits<int64>::max() - 77;
constexpr auto kSwipedBackSpeedRatio = 0.35;
} // namespace } // namespace
void SetupSwipeHandler( void SetupSwipeHandler(
@ -37,7 +40,6 @@ void SetupSwipeHandler(
rpl::producer<bool> dontStart) { rpl::producer<bool> dontStart) {
constexpr auto kThresholdWidth = 50; constexpr auto kThresholdWidth = 50;
constexpr auto kMaxRatio = 1.5; constexpr auto kMaxRatio = 1.5;
const auto threshold = style::ConvertFloatScale(kThresholdWidth);
struct UpdateArgs { struct UpdateArgs {
QPoint globalCursor; QPoint globalCursor;
QPointF position; QPointF position;
@ -52,6 +54,7 @@ void SetupSwipeHandler(
SwipeHandlerFinishData finishByTopData; SwipeHandlerFinishData finishByTopData;
std::optional<Qt::Orientation> orientation; std::optional<Qt::Orientation> orientation;
std::optional<Qt::LayoutDirection> direction; std::optional<Qt::LayoutDirection> direction;
float64 threshold = style::ConvertFloatScale(kThresholdWidth);
int directionInt = 1.; int directionInt = 1.;
QPointF startAt; QPointF startAt;
QPointF delta; QPointF delta;
@ -75,9 +78,9 @@ void SetupSwipeHandler(
state->data.ratio = ratio; state->data.ratio = ratio;
const auto overscrollRatio = std::max(ratio - 1., 0.); const auto overscrollRatio = std::max(ratio - 1., 0.);
const auto translation = int( const auto translation = int(
base::SafeRound(-std::min(ratio, 1.) * threshold) base::SafeRound(-std::min(ratio, 1.) * state->threshold)
) + Ui::OverscrollFromAccumulated(int( ) + Ui::OverscrollFromAccumulated(int(
base::SafeRound(-overscrollRatio * threshold) base::SafeRound(-overscrollRatio * state->threshold)
)); ));
state->data.msgBareId = state->finishByTopData.msgBareId; state->data.msgBareId = state->finishByTopData.msgBareId;
state->data.translation = translation state->data.translation = translation
@ -102,7 +105,7 @@ void SetupSwipeHandler(
if (state->orientation == Qt::Horizontal) { if (state->orientation == Qt::Horizontal) {
const auto ratio = std::clamp( const auto ratio = std::clamp(
delta.value_or(state->delta).x() delta.value_or(state->delta).x()
/ threshold / state->threshold
* state->directionInt, * state->directionInt,
0., 0.,
kMaxRatio); kMaxRatio);
@ -159,6 +162,8 @@ void SetupSwipeHandler(
state->finishByTopData = generateFinish( state->finishByTopData = generateFinish(
state->cursorTop, state->cursorTop,
state->direction.value_or(Qt::RightToLeft)); state->direction.value_or(Qt::RightToLeft));
state->threshold = style::ConvertFloatScale(kThresholdWidth)
* state->finishByTopData.speedRatio;
if (!state->finishByTopData.callback) { if (!state->finishByTopData.callback) {
setOrientation(Qt::Vertical); setOrientation(Qt::Vertical);
} }
@ -180,7 +185,7 @@ void SetupSwipeHandler(
state->delta = args.delta; state->delta = args.delta;
const auto ratio = args.delta.x() const auto ratio = args.delta.x()
* state->directionInt * state->directionInt
/ threshold; / state->threshold;
updateRatio(ratio); updateRatio(ratio);
constexpr auto kResetReachedOn = 0.95; constexpr auto kResetReachedOn = 0.95;
constexpr auto kBounceDuration = crl::time(500); constexpr auto kBounceDuration = crl::time(500);
@ -414,4 +419,13 @@ SwipeBackResult SetupSwipeBack(
return { std::move(lifetime), std::move(callback) }; return { std::move(lifetime), std::move(callback) };
} }
SwipeHandlerFinishData DefaultSwipeBackHandlerFinishData(
Fn<void(void)> callback) {
return {
.callback = std::move(callback),
.msgBareId = kMsgBareIdSwipeBack,
.speedRatio = kSwipedBackSpeedRatio,
};
}
} // namespace HistoryView } // namespace HistoryView

View file

@ -18,11 +18,10 @@ namespace HistoryView {
struct ChatPaintGestureHorizontalData; struct ChatPaintGestureHorizontalData;
struct SwipeBackResult; struct SwipeBackResult;
constexpr auto kMsgBareIdSwipeBack = std::numeric_limits<int64>::max() - 77;
struct SwipeHandlerFinishData { struct SwipeHandlerFinishData {
Fn<void(void)> callback; Fn<void(void)> callback;
int64 msgBareId = 0; int64 msgBareId = 0;
float64 speedRatio = 1.0;
}; };
using Scroll = std::variant< using Scroll = std::variant<
@ -37,8 +36,11 @@ void SetupSwipeHandler(
Fn<SwipeHandlerFinishData(int, Qt::LayoutDirection)> generateFinishByTop, Fn<SwipeHandlerFinishData(int, Qt::LayoutDirection)> generateFinishByTop,
rpl::producer<bool> dontStart = nullptr); rpl::producer<bool> dontStart = nullptr);
SwipeBackResult SetupSwipeBack( [[nodiscard]] SwipeBackResult SetupSwipeBack(
not_null<Ui::RpWidget*> widget, not_null<Ui::RpWidget*> widget,
Fn<std::pair<QColor, QColor>()> colors); Fn<std::pair<QColor, QColor>()> colors);
[[nodiscard]] SwipeHandlerFinishData DefaultSwipeBackHandlerFinishData(
Fn<void(void)> callback);
} // namespace HistoryView } // namespace HistoryView

View file

@ -45,10 +45,9 @@ void SetupSwipeBackSection(
if (direction != Qt::RightToLeft) { if (direction != Qt::RightToLeft) {
return HistoryView::SwipeHandlerFinishData(); return HistoryView::SwipeHandlerFinishData();
} }
return HistoryView::SwipeHandlerFinishData{ return HistoryView::DefaultSwipeBackHandlerFinishData([=] {
.callback = [=] { list->controller()->showBackFromStack(); }, list->controller()->showBackFromStack();
.msgBareId = HistoryView::kMsgBareIdSwipeBack, });
};
}, list->touchMaybeSelectingValue()); }, list->touchMaybeSelectingValue());
} }

View file

@ -927,10 +927,9 @@ void RepliesWidget::setupSwipeReplyAndBack() {
int cursorTop, int cursorTop,
Qt::LayoutDirection direction) { Qt::LayoutDirection direction) {
if (direction == Qt::RightToLeft) { if (direction == Qt::RightToLeft) {
return HistoryView::SwipeHandlerFinishData{ return HistoryView::DefaultSwipeBackHandlerFinishData([=] {
.callback = [=] { controller()->showBackFromStack(); }, controller()->showBackFromStack();
.msgBareId = HistoryView::kMsgBareIdSwipeBack, });
};
} }
auto result = HistoryView::SwipeHandlerFinishData(); auto result = HistoryView::SwipeHandlerFinishData();
if (_inner->elementInSelectionMode(nullptr).inSelectionMode) { if (_inner->elementInSelectionMode(nullptr).inSelectionMode) {

View file

@ -408,10 +408,9 @@ void ContentWidget::setupSwipeReply() {
|| direction != Qt::RightToLeft) { || direction != Qt::RightToLeft) {
return HistoryView::SwipeHandlerFinishData(); return HistoryView::SwipeHandlerFinishData();
} }
return HistoryView::SwipeHandlerFinishData{ return HistoryView::DefaultSwipeBackHandlerFinishData([=] {
.callback = [=] { _controller->showBackFromStack(); }, _controller->showBackFromStack();
.msgBareId = HistoryView::kMsgBareIdSwipeBack, });
};
}, nullptr); }, nullptr);
} }