Lock scroll either horizontal or vertical.

This commit is contained in:
John Preston 2023-07-21 21:50:35 +04:00
parent 676a3f8cfa
commit 0b32a0a1ea
3 changed files with 35 additions and 10 deletions

View File

@ -793,7 +793,10 @@ void Widget::setupMainMenuToggle() {
}
void Widget::setupStories() {
trackScroll(_stories.get());
_stories->verticalScrollEvents(
) | rpl::start_with_next([=](not_null<QWheelEvent*> e) {
_scroll->viewportEvent(e);
}, _stories->lifetime());
_storiesContents.fire(Stories::ContentForSession(
&controller()->session(),

View File

@ -152,6 +152,10 @@ rpl::producer<> List::loadMoreRequests() const {
return _loadMoreRequests.events();
}
rpl::producer<not_null<QWheelEvent*>> List::verticalScrollEvents() const {
return _verticalScrollEvents.events();
}
void List::requestExpanded(bool expanded) {
if (_expanded != expanded) {
_expanded = expanded;
@ -635,18 +639,30 @@ void List::validateName(not_null<Item*> item) {
}
void List::wheelEvent(QWheelEvent *e) {
const auto horizontal = (e->angleDelta().x() != 0);
if (!horizontal || _state == State::Small) {
const auto phase = e->phase();
const auto fullDelta = e->pixelDelta().isNull()
? e->angleDelta()
: e->pixelDelta();
if (phase == Qt::ScrollBegin || phase == Qt::ScrollEnd) {
_scrollingLock = Qt::Orientation();
if (fullDelta.isNull()) {
return;
}
}
const auto vertical = qAbs(fullDelta.x()) < qAbs(fullDelta.y());
if (_scrollingLock == Qt::Orientation() && phase != Qt::NoScrollPhase) {
_scrollingLock = vertical ? Qt::Vertical : Qt::Horizontal;
}
if (_scrollingLock == Qt::Vertical || (vertical && !_scrollLeftMax)) {
_verticalScrollEvents.fire(e);
return;
} else if (_state == State::Small) {
e->ignore();
return;
}
auto delta = horizontal
? ((style::RightToLeft() ? -1 : 1) * (e->pixelDelta().x()
? e->pixelDelta().x()
: e->angleDelta().x()))
: (e->pixelDelta().y()
? e->pixelDelta().y()
: e->angleDelta().y());
const auto delta = vertical
? fullDelta.y()
: ((style::RightToLeft() ? -1 : 1) * fullDelta.x());
const auto now = _scrollLeft;
const auto used = now - delta;

View File

@ -91,6 +91,9 @@ public:
[[nodiscard]] rpl::producer<> entered() const;
[[nodiscard]] rpl::producer<> loadMoreRequests() const;
[[nodiscard]] auto verticalScrollEvents() const
-> rpl::producer<not_null<QWheelEvent*>>;
private:
struct Layout;
enum class State {
@ -177,6 +180,7 @@ private:
int _scrollLeft = 0;
int _scrollLeftMax = 0;
bool _dragging = false;
Qt::Orientation _scrollingLock = {};
Ui::Animations::Simple _expandedAnimation;
Ui::Animations::Simple _expandCatchUpAnimation;
@ -188,6 +192,8 @@ private:
int _selected = -1;
int _pressed = -1;
rpl::event_stream<not_null<QWheelEvent*>> _verticalScrollEvents;
base::unique_qptr<Ui::PopupMenu> _menu;
base::has_weak_ptr _menuGuard;