Improve similar channels pseudo-widget.

This commit is contained in:
John Preston 2023-11-22 14:30:59 +04:00
parent 30d72f1d1d
commit 2df6729f2d
3 changed files with 42 additions and 21 deletions

View file

@ -1428,12 +1428,17 @@ void HistoryInner::onTouchScrollTimer() {
} else if (_touchScrollState == Ui::TouchScrollState::Auto || _touchScrollState == Ui::TouchScrollState::Acceleration) { } else if (_touchScrollState == Ui::TouchScrollState::Auto || _touchScrollState == Ui::TouchScrollState::Acceleration) {
int32 elapsed = int32(nowTime - _touchTime); int32 elapsed = int32(nowTime - _touchTime);
QPoint delta = _touchSpeed * elapsed / 1000; QPoint delta = _touchSpeed * elapsed / 1000;
bool hasScrolled = consumeScrollAction(delta) const auto consumedHorizontal = consumeScrollAction(delta);
|| _widget->touchScroll(delta); if (consumedHorizontal) {
_horizontalScrollLocked = true;
}
const auto hasScrolled = consumedHorizontal
|| (!_horizontalScrollLocked && _widget->touchScroll(delta));
if (_touchSpeed.isNull() || !hasScrolled) { if (_touchSpeed.isNull() || !hasScrolled) {
_touchScrollState = Ui::TouchScrollState::Manual; _touchScrollState = Ui::TouchScrollState::Manual;
_touchScroll = false; _touchScroll = false;
_horizontalScrollLocked = false;
_touchScrollTimer.cancel(); _touchScrollTimer.cancel();
} else { } else {
_touchTime = nowTime; _touchTime = nowTime;
@ -1507,10 +1512,13 @@ void HistoryInner::touchDeaccelerate(int32 elapsed) {
void HistoryInner::touchEvent(QTouchEvent *e) { void HistoryInner::touchEvent(QTouchEvent *e) {
if (e->type() == QEvent::TouchCancel) { // cancel if (e->type() == QEvent::TouchCancel) { // cancel
if (!_touchInProgress) return; if (!_touchInProgress) {
return;
}
_touchInProgress = false; _touchInProgress = false;
_touchSelectTimer.cancel(); _touchSelectTimer.cancel();
_touchScroll = _touchSelect = false; _touchScroll = _touchSelect = false;
_horizontalScrollLocked = false;
_touchScrollState = Ui::TouchScrollState::Manual; _touchScrollState = Ui::TouchScrollState::Manual;
mouseActionCancel(); mouseActionCancel();
return; return;
@ -1527,10 +1535,12 @@ void HistoryInner::touchEvent(QTouchEvent *e) {
e->accept(); e->accept();
return; // ignore mouse press, that was hiding context menu return; // ignore mouse press, that was hiding context menu
} }
if (_touchInProgress) return; if (_touchInProgress || e->touchPoints().isEmpty()) {
if (e->touchPoints().isEmpty()) return; return;
}
_touchInProgress = true; _touchInProgress = true;
_horizontalScrollLocked = false;
if (_touchScrollState == Ui::TouchScrollState::Auto) { if (_touchScrollState == Ui::TouchScrollState::Auto) {
_touchScrollState = Ui::TouchScrollState::Acceleration; _touchScrollState = Ui::TouchScrollState::Acceleration;
_touchWaitingAcceleration = true; _touchWaitingAcceleration = true;
@ -1546,8 +1556,9 @@ void HistoryInner::touchEvent(QTouchEvent *e) {
} break; } break;
case QEvent::TouchUpdate: { case QEvent::TouchUpdate: {
if (!_touchInProgress) return; if (!_touchInProgress) {
if (_touchSelect) { return;
} else if (_touchSelect) {
mouseActionUpdate(_touchPos); mouseActionUpdate(_touchPos);
} else if (!_touchScroll && (_touchPos - _touchStart).manhattanLength() >= QApplication::startDragDistance()) { } else if (!_touchScroll && (_touchPos - _touchStart).manhattanLength() >= QApplication::startDragDistance()) {
_touchSelectTimer.cancel(); _touchSelectTimer.cancel();
@ -1568,7 +1579,9 @@ void HistoryInner::touchEvent(QTouchEvent *e) {
} break; } break;
case QEvent::TouchEnd: { case QEvent::TouchEnd: {
if (!_touchInProgress) return; if (!_touchInProgress) {
return;
}
_touchInProgress = false; _touchInProgress = false;
auto weak = Ui::MakeWeak(this); auto weak = Ui::MakeWeak(this);
if (_touchSelect) { if (_touchSelect) {
@ -1584,6 +1597,7 @@ void HistoryInner::touchEvent(QTouchEvent *e) {
_touchTime = crl::now(); _touchTime = crl::now();
} else if (_touchScrollState == Ui::TouchScrollState::Auto) { } else if (_touchScrollState == Ui::TouchScrollState::Auto) {
_touchScrollState = Ui::TouchScrollState::Manual; _touchScrollState = Ui::TouchScrollState::Manual;
_horizontalScrollLocked = false;
_touchScroll = false; _touchScroll = false;
touchResetSpeed(); touchResetSpeed();
} else if (_touchScrollState == Ui::TouchScrollState::Acceleration) { } else if (_touchScrollState == Ui::TouchScrollState::Acceleration) {
@ -1626,7 +1640,9 @@ void HistoryInner::mouseActionUpdate(const QPoint &screenPos) {
void HistoryInner::touchScrollUpdated(const QPoint &screenPos) { void HistoryInner::touchScrollUpdated(const QPoint &screenPos) {
_touchPos = screenPos; _touchPos = screenPos;
if (!consumeScrollAction(_touchPos - _touchPrevPos)) { if (consumeScrollAction(_touchPos - _touchPrevPos)) {
_horizontalScrollLocked = true;
} else if (!_horizontalScrollLocked) {
_widget->touchScroll(_touchPos - _touchPrevPos); _widget->touchScroll(_touchPos - _touchPrevPos);
} }
touchUpdateSpeed(); touchUpdateSpeed();
@ -4452,7 +4468,7 @@ void HistoryInner::onParentGeometryChanged() {
} }
bool HistoryInner::consumeScrollAction(QPoint delta) { bool HistoryInner::consumeScrollAction(QPoint delta) {
const auto horizontal = std::abs(delta.x()) > std::abs(delta.y()); const auto horizontal = (std::abs(delta.x()) > std::abs(delta.y()));
if (!horizontal || !_acceptsHorizontalScroll || !Element::Moused()) { if (!horizontal || !_acceptsHorizontalScroll || !Element::Moused()) {
return false; return false;
} }

View file

@ -492,6 +492,7 @@ private:
bool _useCornerReaction = false; bool _useCornerReaction = false;
bool _canHaveFromUserpicsSponsored = false; bool _canHaveFromUserpicsSponsored = false;
bool _acceptsHorizontalScroll = false; bool _acceptsHorizontalScroll = false;
bool _horizontalScrollLocked = false;
QPoint _trippleClickPoint; QPoint _trippleClickPoint;
base::Timer _trippleClickTimer; base::Timer _trippleClickTimer;

View file

@ -220,7 +220,7 @@ void SimilarChannels::draw(Painter &p, const PaintContext &context) const {
if (!channel.participants.isEmpty()) { if (!channel.participants.isEmpty()) {
validateParticipansBg(channel); validateParticipansBg(channel);
const auto participants = channel.participantsRect.translated( const auto participants = channel.participantsRect.translated(
QPoint(-_scrollLeft, 0)); geometry.topLeft());
q->drawImage(participants.topLeft(), channel.participantsBg); q->drawImage(participants.topLeft(), channel.participantsBg);
const auto badge = participants.marginsRemoved( const auto badge = participants.marginsRemoved(
st::chatSimilarBadgePadding); st::chatSimilarBadgePadding);
@ -303,14 +303,18 @@ void SimilarChannels::validateParticipansBg(const Channel &channel) const {
channel.thumbnail->image(photo).copy( channel.thumbnail->image(photo).copy(
QRect(photo / 3, photo / 3, photo / 3, photo / 3))); QRect(photo / 3, photo / 3, photo / 3, photo / 3)));
const auto lightness = color.lightness(); const auto hsl = color.toHsl();
if (!base::in_range(lightness, 160, 208)) { constexpr auto kMinSaturation = 0;
color = color.toHsl(); constexpr auto kMaxSaturation = 96;
color.setHsl( constexpr auto kMinLightness = 160;
color.hue(), constexpr auto kMaxLightness = 208;
color.saturation(), if (!base::in_range(hsl.saturation(), kMinSaturation, kMaxSaturation)
std::clamp(lightness, 160, 208)); || !base::in_range(hsl.lightness(), kMinLightness, kMaxLightness)) {
color = color.toRgb(); color = QColor::fromHsl(
hsl.hue(),
std::clamp(hsl.saturation(), kMinSaturation, kMaxSaturation),
std::clamp(hsl.lightness(), kMinLightness, kMaxLightness)
).toRgb();
} }
result.fill(color); result.fill(color);
@ -425,8 +429,8 @@ QSize SimilarChannels::countOptimalSize() {
const auto width = length + st::chatSimilarBadgeIcon.width(); const auto width = length + st::chatSimilarBadgeIcon.width();
const auto delta = (outer.width() - width) / 2; const auto delta = (outer.width() - width) / 2;
const auto badge = QRect( const auto badge = QRect(
x + delta, delta,
y + st::chatSimilarBadgeTop, st::chatSimilarBadgeTop,
outer.width() - 2 * delta, outer.width() - 2 * delta,
st::chatSimilarBadgeFont->height); st::chatSimilarBadgeFont->height);
_channels.back().participantsRect = badge.marginsAdded( _channels.back().participantsRect = badge.marginsAdded(