2024-09-02 20:14:33 +03:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop application for the Telegram messaging service.
|
|
|
|
|
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
|
|
|
*/
|
|
|
|
#include "history/history_view_swipe.h"
|
|
|
|
|
|
|
|
#include "base/platform/base_platform_haptic.h"
|
2024-09-03 17:39:19 +04:00
|
|
|
#include "base/platform/base_platform_info.h"
|
|
|
|
#include "base/qt/qt_common_adapters.h"
|
|
|
|
#include "base/event_filter.h"
|
2024-09-02 20:14:33 +03:00
|
|
|
#include "history/history_view_swipe_data.h"
|
|
|
|
#include "ui/chat/chat_style.h"
|
|
|
|
#include "ui/ui_utility.h"
|
2024-09-03 17:39:19 +04:00
|
|
|
#include "ui/widgets/elastic_scroll.h"
|
2024-09-02 20:14:33 +03:00
|
|
|
#include "ui/widgets/scroll_area.h"
|
|
|
|
|
|
|
|
#include <QtWidgets/QApplication>
|
|
|
|
|
|
|
|
namespace HistoryView {
|
|
|
|
|
|
|
|
void SetupSwipeHandler(
|
|
|
|
not_null<Ui::RpWidget*> widget,
|
|
|
|
not_null<Ui::ScrollArea*> scroll,
|
|
|
|
Fn<void(ChatPaintGestureHorizontalData)> update,
|
|
|
|
Fn<SwipeHandlerFinishData(int)> generateFinishByTop) {
|
|
|
|
constexpr auto kThresholdWidth = 50;
|
|
|
|
const auto threshold = style::ConvertFloatScale(kThresholdWidth);
|
|
|
|
struct State {
|
|
|
|
base::unique_qptr<QObject> filter;
|
2024-09-03 10:19:41 +03:00
|
|
|
Ui::Animations::Simple animationReach;
|
2024-09-02 20:14:33 +03:00
|
|
|
Ui::Animations::Simple animationEnd;
|
|
|
|
SwipeHandlerFinishData finishByTopData;
|
|
|
|
std::optional<Qt::Orientation> orientation;
|
|
|
|
QPointF startAt;
|
2024-09-03 17:39:19 +04:00
|
|
|
QPointF delta;
|
2024-09-02 20:14:33 +03:00
|
|
|
int cursorTop = 0;
|
2024-09-03 17:39:19 +04:00
|
|
|
bool started = false;
|
2024-09-02 20:14:33 +03:00
|
|
|
bool reached = false;
|
2024-09-03 17:39:19 +04:00
|
|
|
bool touch = false;
|
2024-09-02 20:14:33 +03:00
|
|
|
|
|
|
|
rpl::lifetime lifetime;
|
|
|
|
};
|
|
|
|
const auto state = widget->lifetime().make_state<State>();
|
|
|
|
const auto updateRatio = [=](float64 ratio) {
|
|
|
|
update({
|
2024-09-03 11:39:52 +03:00
|
|
|
.ratio = std::clamp(ratio, 0., 1.5),
|
2024-09-03 10:19:41 +03:00
|
|
|
.reachRatio = state->animationReach.value(0.),
|
2024-09-04 09:59:05 +03:00
|
|
|
.translation = int(
|
|
|
|
base::SafeRound(-std::clamp(ratio, 0., 1.5) * threshold)),
|
2024-09-02 20:14:33 +03:00
|
|
|
.msgBareId = state->finishByTopData.msgBareId,
|
|
|
|
.cursorTop = state->cursorTop,
|
|
|
|
});
|
|
|
|
};
|
2024-09-03 17:39:19 +04:00
|
|
|
const auto setOrientation = [=](std::optional<Qt::Orientation> o) {
|
2024-09-02 20:14:33 +03:00
|
|
|
state->orientation = o;
|
2024-09-03 17:39:19 +04:00
|
|
|
const auto isHorizontal = (o == Qt::Horizontal);
|
2024-09-02 20:14:33 +03:00
|
|
|
scroll->viewport()->setAttribute(
|
|
|
|
Qt::WA_AcceptTouchEvents,
|
|
|
|
!isHorizontal);
|
|
|
|
scroll->disableScroll(isHorizontal);
|
|
|
|
};
|
2024-09-03 17:39:19 +04:00
|
|
|
const auto processEnd = [=](std::optional<QPointF> delta = {}) {
|
|
|
|
if (state->orientation == Qt::Horizontal) {
|
|
|
|
const auto ratio = delta.value_or(state->delta).x() / threshold;
|
|
|
|
if ((ratio >= 1) && state->finishByTopData.callback) {
|
|
|
|
Ui::PostponeCall(
|
|
|
|
widget,
|
|
|
|
state->finishByTopData.callback);
|
2024-09-02 20:14:33 +03:00
|
|
|
}
|
2024-09-03 17:39:19 +04:00
|
|
|
state->animationReach.stop();
|
|
|
|
state->animationEnd.stop();
|
|
|
|
state->animationEnd.start(
|
|
|
|
updateRatio,
|
|
|
|
ratio,
|
|
|
|
0.,
|
|
|
|
st::slideWrapDuration);
|
2024-09-02 20:14:33 +03:00
|
|
|
}
|
|
|
|
setOrientation(std::nullopt);
|
2024-09-03 17:39:19 +04:00
|
|
|
state->started = false;
|
2024-09-02 20:14:33 +03:00
|
|
|
state->reached = false;
|
|
|
|
};
|
|
|
|
scroll->scrolls() | rpl::start_with_next([=] {
|
2024-09-03 17:39:19 +04:00
|
|
|
processEnd();
|
2024-09-02 20:14:33 +03:00
|
|
|
}, state->lifetime);
|
2024-09-03 10:19:41 +03:00
|
|
|
const auto animationReachCallback = [=] {
|
2024-09-03 17:39:19 +04:00
|
|
|
updateRatio(state->delta.x() / threshold);
|
2024-09-03 10:19:41 +03:00
|
|
|
};
|
2024-09-03 17:39:19 +04:00
|
|
|
struct UpdateArgs {
|
|
|
|
QPointF position;
|
|
|
|
QPointF delta;
|
|
|
|
bool touch = false;
|
|
|
|
};
|
|
|
|
const auto updateWith = [=](UpdateArgs &&args) {
|
|
|
|
if (!state->started || state->touch != args.touch) {
|
|
|
|
state->started = true;
|
|
|
|
state->touch = args.touch;
|
|
|
|
state->startAt = args.position;
|
|
|
|
state->delta = QPointF();
|
|
|
|
state->cursorTop = widget->mapFromGlobal(
|
|
|
|
QCursor::pos()).y();
|
|
|
|
state->finishByTopData = generateFinishByTop(
|
|
|
|
state->cursorTop);
|
|
|
|
if (!state->finishByTopData.callback) {
|
|
|
|
setOrientation(Qt::Vertical);
|
|
|
|
}
|
|
|
|
} else if (!state->orientation) {
|
|
|
|
state->delta = args.delta;
|
|
|
|
const auto diffXtoY = std::abs(args.delta.x())
|
|
|
|
- std::abs(args.delta.y());
|
|
|
|
if (diffXtoY > 0) {
|
|
|
|
setOrientation(Qt::Horizontal);
|
|
|
|
} else if (diffXtoY < 0) {
|
|
|
|
setOrientation(Qt::Vertical);
|
|
|
|
} else {
|
|
|
|
setOrientation(std::nullopt);
|
|
|
|
}
|
|
|
|
} else if (*state->orientation == Qt::Horizontal) {
|
|
|
|
state->delta = args.delta;
|
|
|
|
const auto ratio = args.delta.x() / threshold;
|
|
|
|
updateRatio(ratio);
|
|
|
|
constexpr auto kResetReachedOn = 0.95;
|
|
|
|
constexpr auto kBounceDuration = crl::time(500);
|
|
|
|
if (!state->reached && ratio >= 1.) {
|
|
|
|
state->reached = true;
|
|
|
|
state->animationReach.stop();
|
|
|
|
state->animationReach.start(
|
|
|
|
animationReachCallback,
|
|
|
|
0.,
|
|
|
|
1.,
|
|
|
|
kBounceDuration);
|
|
|
|
base::Platform::Haptic();
|
|
|
|
} else if (state->reached
|
|
|
|
&& ratio < kResetReachedOn) {
|
|
|
|
state->reached = false;
|
|
|
|
}
|
2024-09-02 20:14:33 +03:00
|
|
|
}
|
2024-09-03 17:39:19 +04:00
|
|
|
};
|
|
|
|
const auto filter = [=](not_null<QEvent*> e) {
|
|
|
|
const auto type = e->type();
|
|
|
|
switch (type) {
|
2024-09-03 17:01:33 +03:00
|
|
|
case QEvent::Leave: {
|
2024-09-03 17:39:19 +04:00
|
|
|
if (state->orientation) {
|
|
|
|
processEnd();
|
|
|
|
}
|
2024-09-03 17:01:33 +03:00
|
|
|
} break;
|
|
|
|
case QEvent::MouseMove: {
|
2024-09-03 17:39:19 +04:00
|
|
|
if (state->orientation) {
|
|
|
|
const auto m = static_cast<QMouseEvent*>(e.get());
|
|
|
|
if (std::abs(m->pos().y() - state->cursorTop)
|
2024-09-02 20:14:33 +03:00
|
|
|
> QApplication::startDragDistance()) {
|
2024-09-03 17:39:19 +04:00
|
|
|
processEnd();
|
|
|
|
}
|
2024-09-02 20:14:33 +03:00
|
|
|
}
|
2024-09-03 17:01:33 +03:00
|
|
|
} break;
|
2024-09-03 17:39:19 +04:00
|
|
|
case QEvent::TouchBegin:
|
|
|
|
case QEvent::TouchUpdate:
|
|
|
|
case QEvent::TouchEnd:
|
|
|
|
case QEvent::TouchCancel: {
|
2024-09-02 20:14:33 +03:00
|
|
|
const auto t = static_cast<QTouchEvent*>(e.get());
|
2024-09-03 17:39:19 +04:00
|
|
|
const auto touchscreen = t->device()
|
2024-09-03 17:01:33 +03:00
|
|
|
&& (t->device()->type() == base::TouchDevice::TouchScreen);
|
2024-09-03 17:39:19 +04:00
|
|
|
if (!Platform::IsMac() && !touchscreen) {
|
|
|
|
break;
|
|
|
|
}
|
2024-09-02 20:14:33 +03:00
|
|
|
const auto &touches = t->touchPoints();
|
2024-09-03 17:39:19 +04:00
|
|
|
const auto released = [&](int index) {
|
|
|
|
return (touches.size() > index)
|
|
|
|
&& (touches.at(index).state() & Qt::TouchPointReleased);
|
|
|
|
};
|
|
|
|
const auto cancel = released(0)
|
|
|
|
|| released(1)
|
2024-09-04 09:59:05 +03:00
|
|
|
|| (touchscreen
|
|
|
|
? (touches.size() != 1)
|
|
|
|
: (touches.size() <= 0 || touches.size() > 2))
|
2024-09-03 17:39:19 +04:00
|
|
|
|| (type == QEvent::TouchEnd)
|
|
|
|
|| (type == QEvent::TouchCancel);
|
|
|
|
if (cancel) {
|
|
|
|
processEnd(touches.empty()
|
|
|
|
? std::optional<QPointF>()
|
|
|
|
: (state->startAt - touches[0].pos()));
|
|
|
|
} else {
|
|
|
|
updateWith({
|
|
|
|
.position = touches[0].pos(),
|
|
|
|
.delta = state->startAt - touches[0].pos(),
|
|
|
|
.touch = true,
|
|
|
|
});
|
|
|
|
}
|
2024-09-04 09:59:05 +03:00
|
|
|
return base::EventFilterResult::Cancel;
|
2024-09-03 17:39:19 +04:00
|
|
|
} break;
|
|
|
|
case QEvent::Wheel: {
|
|
|
|
const auto w = static_cast<QWheelEvent*>(e.get());
|
|
|
|
const auto phase = w->phase();
|
|
|
|
if (Platform::IsMac() || phase == Qt::NoScrollPhase) {
|
|
|
|
break;
|
2024-09-02 20:14:33 +03:00
|
|
|
}
|
2024-09-03 17:39:19 +04:00
|
|
|
const auto cancel = w->buttons()
|
|
|
|
|| (phase == Qt::ScrollEnd)
|
|
|
|
|| (phase == Qt::ScrollMomentum);
|
|
|
|
if (cancel) {
|
|
|
|
processEnd();
|
|
|
|
} else {
|
|
|
|
updateWith({
|
|
|
|
.position = QPointF(),
|
|
|
|
.delta = state->delta - Ui::ScrollDelta(w),
|
|
|
|
.touch = false,
|
|
|
|
});
|
2024-09-02 20:14:33 +03:00
|
|
|
}
|
2024-09-03 17:39:19 +04:00
|
|
|
} break;
|
2024-09-02 20:14:33 +03:00
|
|
|
}
|
|
|
|
return base::EventFilterResult::Continue;
|
|
|
|
};
|
|
|
|
state->filter = base::make_unique_q<QObject>(
|
|
|
|
base::install_event_filter(widget, filter));
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace HistoryView
|