diff --git a/ui/layers/box_content.cpp b/ui/layers/box_content.cpp index 8e45c8e..e25af58 100644 --- a/ui/layers/box_content.cpp +++ b/ui/layers/box_content.cpp @@ -18,8 +18,6 @@ #include "styles/style_layers.h" #include "styles/palette.h" -#include - namespace Ui { void BoxContent::setTitle(rpl::producer title) { @@ -48,7 +46,9 @@ void BoxContent::setInner(object_ptr inner) { setInner(std::move(inner), st::boxScroll); } -void BoxContent::setInner(object_ptr inner, const style::ScrollArea &st) { +void BoxContent::setInner( + object_ptr inner, + const style::ScrollArea &st) { if (inner) { getDelegate()->setLayerType(true); _scroll.create(this, st); @@ -90,11 +90,13 @@ void BoxContent::finishScrollCreate() { updateScrollAreaGeometry(); _scroll->scrolls( ) | rpl::start_with_next([=] { - onScroll(); + updateInnerVisibleTopBottom(); + updateShadowsVisibility(); }, lifetime()); _scroll->innerResizes( ) | rpl::start_with_next([=] { - onInnerResize(); + updateInnerVisibleTopBottom(); + updateShadowsVisibility(); }, lifetime()); } @@ -118,32 +120,39 @@ void BoxContent::scrollByDraggingDelta(int delta) { _draggingScrollDelta = _scroll ? delta : 0; if (_draggingScrollDelta) { if (!_draggingScrollTimer) { - _draggingScrollTimer.create(this); - _draggingScrollTimer->setSingleShot(false); - connect(_draggingScrollTimer, SIGNAL(timeout()), this, SLOT(onDraggingScrollTimer())); + _draggingScrollTimer = std::make_unique([=] { + draggingScrollTimerCallback(); + }); } - _draggingScrollTimer->start(15); + _draggingScrollTimer->callEach(15); } else { - _draggingScrollTimer.destroy(); + _draggingScrollTimer = nullptr; } } -void BoxContent::onDraggingScrollTimer() { - auto delta = (_draggingScrollDelta > 0) ? qMin(_draggingScrollDelta * 3 / 20 + 1, int32(kMaxScrollSpeed)) : qMax(_draggingScrollDelta * 3 / 20 - 1, -int32(kMaxScrollSpeed)); +void BoxContent::draggingScrollTimerCallback() { + const auto delta = (_draggingScrollDelta > 0) + ? qMin(_draggingScrollDelta * 3 / 20 + 1, int32(kMaxScrollSpeed)) + : qMax(_draggingScrollDelta * 3 / 20 - 1, -int32(kMaxScrollSpeed)); _scroll->scrollToY(_scroll->scrollTop() + delta); } void BoxContent::updateInnerVisibleTopBottom() { - if (auto widget = static_cast(_scroll ? _scroll->widget() : nullptr)) { - auto top = _scroll->scrollTop(); + const auto widget = static_cast(_scroll + ? _scroll->widget() + : nullptr); + if (widget) { + const auto top = _scroll->scrollTop(); widget->setVisibleTopBottom(top, top + _scroll->height()); } } void BoxContent::updateShadowsVisibility() { - if (!_scroll) return; + if (!_scroll) { + return; + } - auto top = _scroll->scrollTop(); + const auto top = _scroll->scrollTop(); _topShadow->toggle( (top > 0 || _innerTopSkip > 0), anim::type::normal); @@ -152,16 +161,6 @@ void BoxContent::updateShadowsVisibility() { anim::type::normal); } -void BoxContent::onScroll() { - updateInnerVisibleTopBottom(); - updateShadowsVisibility(); -} - -void BoxContent::onInnerResize() { - updateInnerVisibleTopBottom(); - updateShadowsVisibility(); -} - void BoxContent::setDimensionsToContent( int newWidth, not_null content) { @@ -174,7 +173,7 @@ void BoxContent::setDimensionsToContent( void BoxContent::setInnerTopSkip(int innerTopSkip, bool scrollBottomFixed) { if (_innerTopSkip != innerTopSkip) { - auto delta = innerTopSkip - _innerTopSkip; + const auto delta = innerTopSkip - _innerTopSkip; _innerTopSkip = innerTopSkip; if (_scroll && width() > 0) { auto scrollTopWas = _scroll->scrollTop(); @@ -202,13 +201,21 @@ void BoxContent::setInnerVisible(bool scrollAreaVisible) { } QPixmap BoxContent::grabInnerCache() { - auto isTopShadowVisible = !_topShadow->isHidden(); - auto isBottomShadowVisible = !_bottomShadow->isHidden(); - if (isTopShadowVisible) _topShadow->setVisible(false); - if (isBottomShadowVisible) _bottomShadow->setVisible(false); - auto result = GrabWidget(this, _scroll->geometry()); - if (isTopShadowVisible) _topShadow->setVisible(true); - if (isBottomShadowVisible) _bottomShadow->setVisible(true); + const auto isTopShadowVisible = !_topShadow->isHidden(); + const auto isBottomShadowVisible = !_bottomShadow->isHidden(); + if (isTopShadowVisible) { + _topShadow->setVisible(false); + } + if (isBottomShadowVisible) { + _bottomShadow->setVisible(false); + } + const auto result = GrabWidget(this, _scroll->geometry()); + if (isTopShadowVisible) { + _topShadow->setVisible(true); + } + if (isBottomShadowVisible) { + _bottomShadow->setVisible(true); + } return result; } @@ -227,8 +234,8 @@ void BoxContent::keyPressEvent(QKeyEvent *e) { } void BoxContent::updateScrollAreaGeometry() { - auto newScrollHeight = height() - _innerTopSkip - _innerBottomSkip; - auto changed = (_scroll->height() != newScrollHeight); + const auto newScrollHeight = height() - _innerTopSkip - _innerBottomSkip; + const auto changed = (_scroll->height() != newScrollHeight); _scroll->setGeometryToLeft(0, _innerTopSkip, width(), newScrollHeight); _topShadow->entity()->resize(width(), st::lineWidth); _topShadow->moveToLeft(0, _innerTopSkip); @@ -239,7 +246,7 @@ void BoxContent::updateScrollAreaGeometry() { if (changed) { updateInnerVisibleTopBottom(); - auto top = _scroll->scrollTop(); + const auto top = _scroll->scrollTop(); _topShadow->toggle( (top > 0 || _innerTopSkip > 0), anim::type::instant); diff --git a/ui/layers/box_content.h b/ui/layers/box_content.h index c76e34b..8b470d9 100644 --- a/ui/layers/box_content.h +++ b/ui/layers/box_content.h @@ -18,6 +18,10 @@ enum class RectPart; using RectParts = base::flags; +namespace base { +class Timer; +} // namespace base + namespace style { struct RoundButton; struct IconButton; @@ -97,7 +101,6 @@ public: }; class BoxContent : public RpWidget { - Q_OBJECT public: BoxContent() { @@ -211,7 +214,6 @@ public: void scrollByDraggingDelta(int delta); -public Q_SLOTS: void onScrollToY(int top, int bottom = -1); protected: @@ -274,12 +276,6 @@ protected: void paintEvent(QPaintEvent *e) override; void keyPressEvent(QKeyEvent *e) override; -private Q_SLOTS: - void onScroll(); - void onInnerResize(); - - void onDraggingScrollTimer(); - private: void finishPrepare(); void finishScrollCreate(); @@ -290,6 +286,8 @@ private: void updateShadowsVisibility(); object_ptr doTakeInnerWidget(); + void draggingScrollTimerCallback(); + BoxContentDelegate *_delegate = nullptr; bool _preparing = false; @@ -301,7 +299,7 @@ private: object_ptr _topShadow = { nullptr }; object_ptr _bottomShadow = { nullptr }; - object_ptr _draggingScrollTimer = { nullptr }; + std::unique_ptr _draggingScrollTimer; int _draggingScrollDelta = 0; rpl::event_stream<> _boxClosingStream;