Replaced SetupSwipeHandler signature from arguments to struct.

This commit is contained in:
23rd 2025-03-24 14:50:11 +03:00
parent a30951dc91
commit f734c0475b
10 changed files with 120 additions and 46 deletions

View file

@ -533,8 +533,7 @@ TabbedSelector::~TabbedSelector() = default;
void TabbedSelector::reinstallSwipe(not_null<Ui::RpWidget*> widget) { void TabbedSelector::reinstallSwipe(not_null<Ui::RpWidget*> widget) {
_swipeLifetime.destroy(); _swipeLifetime.destroy();
Ui::Controls::SetupSwipeHandler(widget, _scroll.data(), [=]( auto update = [=](Ui::Controls::SwipeContextData data) {
Ui::Controls::SwipeContextData data) {
if (data.translation != 0) { if (data.translation != 0) {
if (!_swipeBackData.callback) { if (!_swipeBackData.callback) {
_swipeBackData = Ui::Controls::SetupSwipeBack( _swipeBackData = Ui::Controls::SetupSwipeBack(
@ -552,7 +551,9 @@ void TabbedSelector::reinstallSwipe(not_null<Ui::RpWidget*> widget) {
} else if (_swipeBackData.lifetime) { } else if (_swipeBackData.lifetime) {
_swipeBackData = {}; _swipeBackData = {};
} }
}, [=](int, Qt::LayoutDirection direction) { };
auto init = [=](int, Qt::LayoutDirection direction) {
if (!_tabsSlider) { if (!_tabsSlider) {
return Ui::Controls::SwipeHandlerFinishData(); return Ui::Controls::SwipeHandlerFinishData();
} }
@ -571,7 +572,16 @@ void TabbedSelector::reinstallSwipe(not_null<Ui::RpWidget*> widget) {
}); });
} }
return Ui::Controls::SwipeHandlerFinishData(); return Ui::Controls::SwipeHandlerFinishData();
}, nullptr, &_swipeLifetime); };
Ui::Controls::SetupSwipeHandler({
.widget = widget,
.scroll = _scroll.data(),
.update = std::move(update),
.init = std::move(init),
.dontStart = nullptr,
.onLifetime = &_swipeLifetime,
});
} }
const style::EmojiPan &TabbedSelector::st() const { const style::EmojiPan &TabbedSelector::st() const {

View file

@ -691,8 +691,8 @@ void Widget::setupSwipeBack() {
} }
return !current; return !current;
}; };
Ui::Controls::SetupSwipeHandler(_inner, _scroll.data(), [=](
Ui::Controls::SwipeContextData data) { auto update = [=](Ui::Controls::SwipeContextData data) {
if (data.translation != 0) { if (data.translation != 0) {
if (data.translation < 0 if (data.translation < 0
&& _inner && _inner
@ -724,7 +724,9 @@ void Widget::setupSwipeBack() {
_inner->update(); _inner->update();
} }
} }
}, [=](int top, Qt::LayoutDirection direction) { };
auto init = [=](int top, Qt::LayoutDirection direction) {
_swipeBackIconMirrored = false; _swipeBackIconMirrored = false;
_swipeBackMirrored = false; _swipeBackMirrored = false;
if (_childListShown.current()) { if (_childListShown.current()) {
@ -812,7 +814,15 @@ void Widget::setupSwipeBack() {
}); });
} }
} }
return Ui::Controls::SwipeHandlerFinishData(); return Ui::Controls::SwipeHandlerFinishData();
};
Ui::Controls::SetupSwipeHandler({
.widget = _inner,
.scroll = _scroll.data(),
.update = std::move(update),
.init = std::move(init),
}); });
} }

View file

@ -420,8 +420,7 @@ void Widget::setupShortcuts() {
} }
void Widget::setupSwipeReply() { void Widget::setupSwipeReply() {
Ui::Controls::SetupSwipeHandler(_inner.data(), _scroll.data(), [=]( auto update = [=](Ui::Controls::SwipeContextData data) {
Ui::Controls::SwipeContextData data) {
if (data.translation > 0) { if (data.translation > 0) {
if (!_swipeBackData.callback) { if (!_swipeBackData.callback) {
_swipeBackData = Ui::Controls::SetupSwipeBack( _swipeBackData = Ui::Controls::SetupSwipeBack(
@ -439,13 +438,22 @@ void Widget::setupSwipeReply() {
} else if (_swipeBackData.lifetime) { } else if (_swipeBackData.lifetime) {
_swipeBackData = {}; _swipeBackData = {};
} }
}, [=](int, Qt::LayoutDirection direction) { };
auto init = [=](int, Qt::LayoutDirection direction) {
if (direction == Qt::RightToLeft) { if (direction == Qt::RightToLeft) {
return Ui::Controls::DefaultSwipeBackHandlerFinishData([=] { return Ui::Controls::DefaultSwipeBackHandlerFinishData([=] {
controller()->showBackFromStack(); controller()->showBackFromStack();
}); });
} }
return Ui::Controls::SwipeHandlerFinishData(); return Ui::Controls::SwipeHandlerFinishData();
};
Ui::Controls::SetupSwipeHandler({
.widget = _inner.data(),
.scroll = _scroll.data(),
.update = std::move(update),
.init = std::move(init),
}); });
} }

View file

@ -528,8 +528,8 @@ void HistoryInner::setupSwipeReplyAndBack() {
return; return;
} }
const auto peer = _peer; const auto peer = _peer;
Ui::Controls::SetupSwipeHandler(this, _scroll, [=, history = _history](
Ui::Controls::SwipeContextData data) { auto update = [=, history = _history](Ui::Controls::SwipeContextData data) {
if (data.translation > 0) { if (data.translation > 0) {
if (!_swipeBackData.callback) { if (!_swipeBackData.callback) {
_swipeBackData = Ui::Controls::SetupSwipeBack( _swipeBackData = Ui::Controls::SetupSwipeBack(
@ -559,7 +559,9 @@ void HistoryInner::setupSwipeReplyAndBack() {
repaintItem(item); repaintItem(item);
} }
} }
}, [=, show = _controller->uiShow()]( };
auto init = [=, show = _controller->uiShow()](
int cursorTop, int cursorTop,
Qt::LayoutDirection direction) { Qt::LayoutDirection direction) {
if (direction == Qt::RightToLeft) { if (direction == Qt::RightToLeft) {
@ -607,7 +609,15 @@ void HistoryInner::setupSwipeReplyAndBack() {
return false; return false;
}); });
return result; return result;
}, _touchMaybeSelecting.value()); };
Ui::Controls::SetupSwipeHandler({
.widget = this,
.scroll = _scroll,
.update = std::move(update),
.init = std::move(init),
.dontStart = _touchMaybeSelecting.value(),
});
} }
bool HistoryInner::hasSelectRestriction() const { bool HistoryInner::hasSelectRestriction() const {

View file

@ -21,8 +21,7 @@ void SetupSwipeBackSection(
not_null<HistoryView::ListWidget*> list) { not_null<HistoryView::ListWidget*> list) {
const auto swipeBackData const auto swipeBackData
= list->lifetime().make_state<Ui::Controls::SwipeBackResult>(); = list->lifetime().make_state<Ui::Controls::SwipeBackResult>();
Ui::Controls::SetupSwipeHandler(list, scroll, [=]( auto update = [=](Ui::Controls::SwipeContextData data) {
Ui::Controls::SwipeContextData data) {
if (data.translation > 0) { if (data.translation > 0) {
if (!swipeBackData->callback) { if (!swipeBackData->callback) {
const auto color = [=]() -> std::pair<QColor, QColor> { const auto color = [=]() -> std::pair<QColor, QColor> {
@ -43,14 +42,22 @@ void SetupSwipeBackSection(
} else if (swipeBackData->lifetime) { } else if (swipeBackData->lifetime) {
(*swipeBackData) = {}; (*swipeBackData) = {};
} }
}, [=](int, Qt::LayoutDirection direction) { };
auto init = [=](int, Qt::LayoutDirection direction) {
if (direction != Qt::RightToLeft) { if (direction != Qt::RightToLeft) {
return Ui::Controls::SwipeHandlerFinishData(); return Ui::Controls::SwipeHandlerFinishData();
} }
return Ui::Controls::DefaultSwipeBackHandlerFinishData([=] { return Ui::Controls::DefaultSwipeBackHandlerFinishData([=] {
list->controller()->showBackFromStack(); list->controller()->showBackFromStack();
}); });
}, list->touchMaybeSelectingValue()); };
Ui::Controls::SetupSwipeHandler({
.widget = list,
.scroll = scroll,
.update = std::move(update),
.init = std::move(init),
.dontStart = list->touchMaybeSelectingValue(),
});
} }
} // namespace Window } // namespace Window

View file

@ -890,8 +890,8 @@ void RepliesWidget::setupSwipeReplyAndBack() {
} }
return false; return false;
}; };
Ui::Controls::SetupSwipeHandler(_inner, _scroll.get(), [=](
Ui::Controls::SwipeContextData data) { auto update = [=](Ui::Controls::SwipeContextData data) {
if (data.translation > 0) { if (data.translation > 0) {
if (!_swipeBackData.callback) { if (!_swipeBackData.callback) {
_swipeBackData = Ui::Controls::SetupSwipeBack( _swipeBackData = Ui::Controls::SetupSwipeBack(
@ -923,7 +923,9 @@ void RepliesWidget::setupSwipeReplyAndBack() {
_history->owner().requestItemRepaint(item); _history->owner().requestItemRepaint(item);
} }
} }
}, [=, show = controller()->uiShow()]( };
auto init = [=, show = controller()->uiShow()](
int cursorTop, int cursorTop,
Qt::LayoutDirection direction) { Qt::LayoutDirection direction) {
if (direction == Qt::RightToLeft) { if (direction == Qt::RightToLeft) {
@ -962,7 +964,15 @@ void RepliesWidget::setupSwipeReplyAndBack() {
}); });
}; };
return result; return result;
}, _inner->touchMaybeSelectingValue()); };
Ui::Controls::SetupSwipeHandler({
.widget = _inner,
.scroll = _scroll.get(),
.update = std::move(update),
.init = std::move(init),
.dontStart = _inner->touchMaybeSelectingValue(),
});
} }
void RepliesWidget::chooseAttach( void RepliesWidget::chooseAttach(

View file

@ -411,8 +411,7 @@ not_null<Ui::ScrollArea*> ContentWidget::scroll() const {
} }
void ContentWidget::setupSwipeHandler(not_null<Ui::RpWidget*> widget) { void ContentWidget::setupSwipeHandler(not_null<Ui::RpWidget*> widget) {
Ui::Controls::SetupSwipeHandler(widget, _scroll.data(), [=]( auto update = [=](Ui::Controls::SwipeContextData data) {
Ui::Controls::SwipeContextData data) {
if (data.translation > 0) { if (data.translation > 0) {
if (!_swipeBackData.callback) { if (!_swipeBackData.callback) {
_swipeBackData = Ui::Controls::SetupSwipeBack( _swipeBackData = Ui::Controls::SetupSwipeBack(
@ -429,7 +428,9 @@ void ContentWidget::setupSwipeHandler(not_null<Ui::RpWidget*> widget) {
} else if (_swipeBackData.lifetime) { } else if (_swipeBackData.lifetime) {
_swipeBackData = {}; _swipeBackData = {};
} }
}, [=](int, Qt::LayoutDirection direction) { };
auto init = [=](int, Qt::LayoutDirection direction) {
return (direction == Qt::RightToLeft && _controller->hasBackButton()) return (direction == Qt::RightToLeft && _controller->hasBackButton())
? Ui::Controls::DefaultSwipeBackHandlerFinishData([=] { ? Ui::Controls::DefaultSwipeBackHandlerFinishData([=] {
checkBeforeClose(crl::guard(this, [=] { checkBeforeClose(crl::guard(this, [=] {
@ -438,6 +439,13 @@ void ContentWidget::setupSwipeHandler(not_null<Ui::RpWidget*> widget) {
})); }));
}) })
: Ui::Controls::SwipeHandlerFinishData(); : Ui::Controls::SwipeHandlerFinishData();
};
Ui::Controls::SetupSwipeHandler({
.widget = widget,
.scroll = _scroll.data(),
.update = std::move(update),
.init = std::move(init),
}); });
} }

View file

@ -61,16 +61,14 @@ private:
} // namespace } // namespace
void SetupSwipeHandler( void SetupSwipeHandler(SwipeHandlerArgs &&args) {
not_null<Ui::RpWidget*> widget,
Scroll scroll,
Fn<void(SwipeContextData)> update,
Fn<SwipeHandlerFinishData(int, Qt::LayoutDirection)> generateFinish,
rpl::producer<bool> dontStart,
rpl::lifetime *onLifetime) {
static constexpr auto kThresholdWidth = 50; static constexpr auto kThresholdWidth = 50;
static constexpr auto kMaxRatio = 1.5; static constexpr auto kMaxRatio = 1.5;
const auto widget = std::move(args.widget);
const auto scroll = std::move(args.scroll);
const auto update = std::move(args.update);
struct UpdateArgs { struct UpdateArgs {
QPoint globalCursor; QPoint globalCursor;
QPointF position; QPointF position;
@ -98,11 +96,13 @@ void SetupSwipeHandler(
rpl::lifetime lifetime; rpl::lifetime lifetime;
}; };
auto &useLifetime = onLifetime ? *onLifetime : widget->lifetime(); auto &useLifetime = args.onLifetime
? *(args.onLifetime)
: args.widget->lifetime();
const auto state = useLifetime.make_state<State>(); const auto state = useLifetime.make_state<State>();
if (dontStart) { if (args.dontStart) {
std::move( std::move(
dontStart args.dontStart
) | rpl::start_with_next([=](bool dontStart) { ) | rpl::start_with_next([=](bool dontStart) {
state->dontStart = dontStart; state->dontStart = dontStart;
}, state->lifetime); }, state->lifetime);
@ -188,7 +188,7 @@ void SetupSwipeHandler(
state->data.reachRatio = value; state->data.reachRatio = value;
update(state->data); update(state->data);
}; };
const auto updateWith = [=](UpdateArgs args) { const auto updateWith = [=, generateFinish = args.init](UpdateArgs args) {
const auto fillFinishByTop = [&] { const auto fillFinishByTop = [&] {
if (!args.delta.x()) { if (!args.delta.x()) {
LOG(("SKIPPING fillFinishByTop.")); LOG(("SKIPPING fillFinishByTop."));

View file

@ -32,13 +32,16 @@ using Scroll = std::variant<
not_null<Ui::ScrollArea*>, not_null<Ui::ScrollArea*>,
not_null<Ui::ElasticScroll*>>; not_null<Ui::ElasticScroll*>>;
void SetupSwipeHandler( struct SwipeHandlerArgs {
not_null<Ui::RpWidget*> widget, not_null<Ui::RpWidget*> widget;
Scroll scroll, Scroll scroll;
Fn<void(SwipeContextData)> update, Fn<void(SwipeContextData)> update;
Fn<SwipeHandlerFinishData(int, Qt::LayoutDirection)> generateFinishByTop, Fn<SwipeHandlerFinishData(int, Qt::LayoutDirection)> init;
rpl::producer<bool> dontStart = nullptr, rpl::producer<bool> dontStart = nullptr;
rpl::lifetime *onLifetime = nullptr); rpl::lifetime *onLifetime = nullptr;
};
void SetupSwipeHandler(SwipeHandlerArgs &&args);
[[nodiscard]] SwipeBackResult SetupSwipeBack( [[nodiscard]] SwipeBackResult SetupSwipeBack(
not_null<Ui::RpWidget*> widget, not_null<Ui::RpWidget*> widget,

View file

@ -1051,8 +1051,7 @@ void MainMenu::setupSwipe() {
}); });
} }
Ui::Controls::SetupSwipeHandler(_inner, _scroll.data(), [=]( auto update = [=](Ui::Controls::SwipeContextData data) {
Ui::Controls::SwipeContextData data) {
if (data.translation < 0) { if (data.translation < 0) {
if (!_swipeBackData.callback) { if (!_swipeBackData.callback) {
_swipeBackData = Ui::Controls::SetupSwipeBack( _swipeBackData = Ui::Controls::SetupSwipeBack(
@ -1069,13 +1068,22 @@ void MainMenu::setupSwipe() {
} else if (_swipeBackData.lifetime) { } else if (_swipeBackData.lifetime) {
_swipeBackData = {}; _swipeBackData = {};
} }
}, [=](int, Qt::LayoutDirection direction) { };
auto init = [=](int, Qt::LayoutDirection direction) {
if (direction != Qt::LeftToRight) { if (direction != Qt::LeftToRight) {
return Ui::Controls::SwipeHandlerFinishData(); return Ui::Controls::SwipeHandlerFinishData();
} }
return Ui::Controls::DefaultSwipeBackHandlerFinishData([=] { return Ui::Controls::DefaultSwipeBackHandlerFinishData([=] {
closeLayer(); closeLayer();
}); });
};
Ui::Controls::SetupSwipeHandler({
.widget = _inner,
.scroll = _scroll.data(),
.update = std::move(update),
.init = std::move(init),
}); });
} }