Removed Q_OBJECT from ScrollBar.

This commit is contained in:
23rd 2021-09-29 18:18:25 +03:00
parent 532affa1bf
commit b64f68dca6
2 changed files with 68 additions and 46 deletions

View file

@ -38,14 +38,18 @@ ScrollBar::ScrollBar(ScrollArea *parent, bool vert, const style::ScrollArea *st)
, _vertical(vert) , _vertical(vert)
, _hiding(_st->hiding != 0) , _hiding(_st->hiding != 0)
, _connected(vert ? parent->verticalScrollBar() : parent->horizontalScrollBar()) , _connected(vert ? parent->verticalScrollBar() : parent->horizontalScrollBar())
, _scrollMax(_connected->maximum()) { , _scrollMax(_connected->maximum())
, _hideTimer([=] { hideTimer(); }) {
recountSize(); recountSize();
_hideTimer.setSingleShot(true); connect(_connected, &QAbstractSlider::valueChanged, [=] {
connect(&_hideTimer, SIGNAL(timeout()), this, SLOT(onHideTimer())); area()->onScrolled();
updateBar();
connect(_connected, SIGNAL(valueChanged(int)), this, SLOT(onValueChanged())); });
connect(_connected, SIGNAL(rangeChanged(int, int)), this, SLOT(onRangeChanged())); connect(_connected, &QAbstractSlider::rangeChanged, [=] {
area()->onInnerResized();
updateBar();
});
updateBar(); updateBar();
} }
@ -54,16 +58,6 @@ void ScrollBar::recountSize() {
setGeometry(_vertical ? QRect(style::RightToLeft() ? 0 : (area()->width() - _st->width), _st->deltat, _st->width, area()->height() - _st->deltat - _st->deltab) : QRect(_st->deltat, area()->height() - _st->width, area()->width() - _st->deltat - _st->deltab, _st->width)); setGeometry(_vertical ? QRect(style::RightToLeft() ? 0 : (area()->width() - _st->width), _st->deltat, _st->width, area()->height() - _st->deltat - _st->deltab) : QRect(_st->deltat, area()->height() - _st->width, area()->width() - _st->deltat - _st->deltab, _st->width));
} }
void ScrollBar::onValueChanged() {
area()->onScrolled();
updateBar();
}
void ScrollBar::onRangeChanged() {
area()->onInnerResized();
updateBar();
}
void ScrollBar::updateBar(bool force) { void ScrollBar::updateBar(bool force) {
QRect newBar; QRect newBar;
if (_connected->maximum() != _scrollMax) { if (_connected->maximum() != _scrollMax) {
@ -76,8 +70,18 @@ void ScrollBar::updateBar(bool force) {
if (h >= rh || !area()->scrollTopMax() || rh < _st->minHeight) { if (h >= rh || !area()->scrollTopMax() || rh < _st->minHeight) {
if (!isHidden()) hide(); if (!isHidden()) hide();
bool newTopSh = (_st->topsh < 0), newBottomSh = (_st->bottomsh < 0); bool newTopSh = (_st->topsh < 0), newBottomSh = (_st->bottomsh < 0);
if (newTopSh != _topSh || force) topShadowVisibility(_topSh = newTopSh); if (newTopSh != _topSh || force) {
if (newBottomSh != _bottomSh || force) bottomShadowVisibility(_bottomSh = newBottomSh); _shadowVisibilityChanged.fire({
.type = ScrollShadow::Type::Top,
.visible = (_topSh = newTopSh),
});
}
if (newBottomSh != _bottomSh || force) {
_shadowVisibilityChanged.fire({
.type = ScrollShadow::Type::Bottom,
.visible = (_bottomSh = newBottomSh),
});
}
return; return;
} }
@ -105,13 +109,23 @@ void ScrollBar::updateBar(bool force) {
} }
if (_vertical) { if (_vertical) {
bool newTopSh = (_st->topsh < 0) || (area()->scrollTop() > _st->topsh), newBottomSh = (_st->bottomsh < 0) || (area()->scrollTop() < area()->scrollTopMax() - _st->bottomsh); bool newTopSh = (_st->topsh < 0) || (area()->scrollTop() > _st->topsh), newBottomSh = (_st->bottomsh < 0) || (area()->scrollTop() < area()->scrollTopMax() - _st->bottomsh);
if (newTopSh != _topSh || force) topShadowVisibility(_topSh = newTopSh); if (newTopSh != _topSh || force) {
if (newBottomSh != _bottomSh || force) bottomShadowVisibility(_bottomSh = newBottomSh); _shadowVisibilityChanged.fire({
.type = ScrollShadow::Type::Top,
.visible = (_topSh = newTopSh),
});
}
if (newBottomSh != _bottomSh || force) {
_shadowVisibilityChanged.fire({
.type = ScrollShadow::Type::Bottom,
.visible = (_bottomSh = newBottomSh),
});
}
} }
if (isHidden()) show(); if (isHidden()) show();
} }
void ScrollBar::onHideTimer() { void ScrollBar::hideTimer() {
if (!_hiding) { if (!_hiding) {
_hiding = true; _hiding = true;
_a_opacity.start([this] { update(); }, 1., 0., _st->duration); _a_opacity.start([this] { update(); }, 1., 0., _st->duration);
@ -162,7 +176,7 @@ void ScrollBar::setMoving(bool moving) {
_a_over.start([this] { update(); }, nowOver ? 0. : 1., nowOver ? 1. : 0., _st->duration); _a_over.start([this] { update(); }, nowOver ? 0. : 1., nowOver ? 1. : 0., _st->duration);
} }
if (!nowOver && _st->hiding && !_hiding) { if (!nowOver && _st->hiding && !_hiding) {
_hideTimer.start(_hideIn); _hideTimer.callOnce(_hideIn);
} }
} }
} }
@ -202,12 +216,12 @@ void ScrollBar::hideTimeout(crl::time dt) {
} }
_hideIn = dt; _hideIn = dt;
if (!_moving) { if (!_moving) {
_hideTimer.start(_hideIn); _hideTimer.callOnce(_hideIn);
} }
} }
void ScrollBar::enterEventHook(QEvent *e) { void ScrollBar::enterEventHook(QEvent *e) {
_hideTimer.stop(); _hideTimer.cancel();
setMouseTracking(true); setMouseTracking(true);
setOver(true); setOver(true);
} }
@ -219,7 +233,7 @@ void ScrollBar::leaveEventHook(QEvent *e) {
setOver(false); setOver(false);
setOverBar(false); setOverBar(false);
if (_st->hiding && !_hiding) { if (_st->hiding && !_hiding) {
_hideTimer.start(_hideIn); _hideTimer.callOnce(_hideIn);
} }
} }
@ -271,6 +285,11 @@ void ScrollBar::resizeEvent(QResizeEvent *e) {
updateBar(); updateBar();
} }
auto ScrollBar::shadowVisibilityChanged() const
-> rpl::producer<ScrollBar::ShadowVisibility> {
return _shadowVisibilityChanged.events();
}
ScrollArea::ScrollArea(QWidget *parent, const style::ScrollArea &st, bool handleTouch) ScrollArea::ScrollArea(QWidget *parent, const style::ScrollArea &st, bool handleTouch)
: Parent(parent) : Parent(parent)
, _st(st) , _st(st)
@ -282,16 +301,13 @@ ScrollArea::ScrollArea(QWidget *parent, const style::ScrollArea &st, bool handle
setLayoutDirection(style::LayoutDirection()); setLayoutDirection(style::LayoutDirection());
setFocusPolicy(Qt::NoFocus); setFocusPolicy(Qt::NoFocus);
connect( _verticalBar->shadowVisibilityChanged(
_verticalBar, ) | rpl::start_with_next([=](const ScrollBar::ShadowVisibility &data) {
&ScrollBar::topShadowVisibility, ((data.type == ScrollShadow::Type::Top)
_topShadow, ? _topShadow
&ScrollShadow::changeVisibility); : _bottomShadow)->changeVisibility(data.visible);
connect( }, _lifetime);
_verticalBar,
&ScrollBar::bottomShadowVisibility,
_bottomShadow,
&ScrollShadow::changeVisibility);
_verticalBar->updateBar(true); _verticalBar->updateBar(true);
verticalScrollBar()->setSingleStep(style::ConvertScale(verticalScrollBar()->singleStep())); verticalScrollBar()->setSingleStep(style::ConvertScale(verticalScrollBar()->singleStep()));

View file

@ -9,6 +9,7 @@
#include "ui/rp_widget.h" #include "ui/rp_widget.h"
#include "ui/effects/animations.h" #include "ui/effects/animations.h"
#include "base/object_ptr.h" #include "base/object_ptr.h"
#include "base/timer.h"
#include "styles/style_widgets.h" #include "styles/style_widgets.h"
#include <QtWidgets/QScrollArea> #include <QtWidgets/QScrollArea>
@ -50,6 +51,10 @@ struct ScrollToRequest {
class ScrollShadow final : public QWidget { class ScrollShadow final : public QWidget {
public: public:
enum class Type {
Top,
Bottom,
};
ScrollShadow(ScrollArea *parent, const style::ScrollArea *st); ScrollShadow(ScrollArea *parent, const style::ScrollArea *st);
void paintEvent(QPaintEvent *e); void paintEvent(QPaintEvent *e);
@ -61,9 +66,11 @@ private:
}; };
class ScrollBar : public TWidget { class ScrollBar : public TWidget {
Q_OBJECT
public: public:
struct ShadowVisibility {
ScrollShadow::Type type;
bool visible = false;
};
ScrollBar(ScrollArea *parent, bool vertical, const style::ScrollArea *st); ScrollBar(ScrollArea *parent, bool vertical, const style::ScrollArea *st);
void recountSize(); void recountSize();
@ -71,14 +78,8 @@ public:
void hideTimeout(crl::time dt); void hideTimeout(crl::time dt);
private Q_SLOTS: [[nodiscard]] auto shadowVisibilityChanged() const
void onValueChanged(); -> rpl::producer<ShadowVisibility>;
void onRangeChanged();
void onHideTimer();
Q_SIGNALS:
void topShadowVisibility(bool);
void bottomShadowVisibility(bool);
protected: protected:
void paintEvent(QPaintEvent *e) override; void paintEvent(QPaintEvent *e) override;
@ -96,6 +97,8 @@ private:
void setOverBar(bool overbar); void setOverBar(bool overbar);
void setMoving(bool moving); void setMoving(bool moving);
void hideTimer();
const style::ScrollArea *_st; const style::ScrollArea *_st;
bool _vertical = true; bool _vertical = true;
@ -112,13 +115,15 @@ private:
int32 _startFrom, _scrollMax; int32 _startFrom, _scrollMax;
crl::time _hideIn = 0; crl::time _hideIn = 0;
QTimer _hideTimer; base::Timer _hideTimer;
Animations::Simple _a_over; Animations::Simple _a_over;
Animations::Simple _a_barOver; Animations::Simple _a_barOver;
Animations::Simple _a_opacity; Animations::Simple _a_opacity;
QRect _bar; QRect _bar;
rpl::event_stream<ShadowVisibility> _shadowVisibilityChanged;
}; };
class ScrollArea : public RpWidgetBase<QScrollArea> { class ScrollArea : public RpWidgetBase<QScrollArea> {
@ -239,6 +244,7 @@ private:
object_ptr<QWidget> _widget = { nullptr }; object_ptr<QWidget> _widget = { nullptr };
rpl::event_stream<int> _scrollTopUpdated; rpl::event_stream<int> _scrollTopUpdated;
rpl::lifetime _lifetime;
}; };