Replaced snap util function with std::clamp.

This commit is contained in:
23rd 2021-01-23 06:29:50 +03:00
parent 4895e5e110
commit dd01ece14a
44 changed files with 193 additions and 92 deletions

View file

@ -354,7 +354,7 @@ void ApiWrap::requestTermsUpdate() {
const auto requestNext = [&](auto &&data) { const auto requestNext = [&](auto &&data) {
const auto timeout = (data.vexpires().v - base::unixtime::now()); const auto timeout = (data.vexpires().v - base::unixtime::now());
_termsUpdateSendAt = crl::now() + snap( _termsUpdateSendAt = crl::now() + std::clamp(
timeout * crl::time(1000), timeout * crl::time(1000),
kTermsUpdateTimeoutMin, kTermsUpdateTimeoutMin,
kTermsUpdateTimeoutMax); kTermsUpdateTimeoutMax);

View file

@ -228,8 +228,8 @@ void EditColorBox::Picker::preparePaletteHSL() {
} }
void EditColorBox::Picker::updateCurrentPoint(QPoint localPosition) { void EditColorBox::Picker::updateCurrentPoint(QPoint localPosition) {
auto x = snap(localPosition.x(), 0, width()) / float64(width()); auto x = std::clamp(localPosition.x(), 0, width()) / float64(width());
auto y = snap(localPosition.y(), 0, height()) / float64(height()); auto y = std::clamp(localPosition.y(), 0, height()) / float64(height());
if (_x != x || _y != y) { if (_x != x || _y != y) {
_x = x; _x = x;
_y = y; _y = y;
@ -245,14 +245,14 @@ void EditColorBox::Picker::setHSB(HSB hsb) {
_topright = _topright.toRgb(); _topright = _topright.toRgb();
_bottomleft = _bottomright = QColor(0, 0, 0); _bottomleft = _bottomright = QColor(0, 0, 0);
_x = snap(hsb.saturation / 255., 0., 1.); _x = std::clamp(hsb.saturation / 255., 0., 1.);
_y = 1. - snap(hsb.brightness / 255., 0., 1.); _y = 1. - std::clamp(hsb.brightness / 255., 0., 1.);
} else { } else {
_topleft = _topright = QColor::fromHsl(0, 255, hsb.brightness); _topleft = _topright = QColor::fromHsl(0, 255, hsb.brightness);
_bottomleft = _bottomright = QColor::fromHsl(0, 0, hsb.brightness); _bottomleft = _bottomright = QColor::fromHsl(0, 0, hsb.brightness);
_x = snap(hsb.hue / 360., 0., 1.); _x = std::clamp(hsb.hue / 360., 0., 1.);
_y = 1. - snap(hsb.saturation / 255., 0., 1.); _y = 1. - std::clamp(hsb.saturation / 255., 0., 1.);
} }
_paletteInvalidated = true; _paletteInvalidated = true;
@ -291,7 +291,7 @@ public:
return _value; return _value;
} }
void setValue(float64 value) { void setValue(float64 value) {
_value = snap(value, 0., 1.); _value = std::clamp(value, 0., 1.);
update(); update();
} }
void setHSB(HSB hsb); void setHSB(HSB hsb);
@ -508,12 +508,12 @@ float64 EditColorBox::Slider::valueFromColor(QColor color) const {
} }
float64 EditColorBox::Slider::valueFromHue(int hue) const { float64 EditColorBox::Slider::valueFromHue(int hue) const {
return (1. - snap(hue, 0, 360) / 360.); return (1. - std::clamp(hue, 0, 360) / 360.);
} }
void EditColorBox::Slider::setAlpha(int alpha) { void EditColorBox::Slider::setAlpha(int alpha) {
if (_type == Type::Opacity) { if (_type == Type::Opacity) {
_value = snap(alpha, 0, 255) / 255.; _value = std::clamp(alpha, 0, 255) / 255.;
update(); update();
} }
} }
@ -534,7 +534,7 @@ void EditColorBox::Slider::updatePixmapFromMask() {
void EditColorBox::Slider::updateCurrentPoint(QPoint localPosition) { void EditColorBox::Slider::updateCurrentPoint(QPoint localPosition) {
auto coord = (isHorizontal() ? localPosition.x() : localPosition.y()) - st::colorSliderSkip; auto coord = (isHorizontal() ? localPosition.x() : localPosition.y()) - st::colorSliderSkip;
auto maximum = (isHorizontal() ? width() : height()) - 2 * st::colorSliderSkip; auto maximum = (isHorizontal() ? width() : height()) - 2 * st::colorSliderSkip;
auto value = snap(coord, 0, maximum) / float64(maximum); auto value = std::clamp(coord, 0, maximum) / float64(maximum);
if (_value != value) { if (_value != value) {
_value = value; _value = value;
update(); update();
@ -663,7 +663,7 @@ void EditColorBox::Field::wheelEvent(QWheelEvent *e) {
void EditColorBox::Field::changeValue(int delta) { void EditColorBox::Field::changeValue(int delta) {
auto currentValue = value(); auto currentValue = value();
auto newValue = snap(currentValue + delta, 0, _limit); auto newValue = std::clamp(currentValue + delta, 0, _limit);
if (newValue != currentValue) { if (newValue != currentValue) {
setText(QString::number(newValue)); setText(QString::number(newValue));
setFocus(); setFocus();

View file

@ -66,10 +66,10 @@ private:
[[nodiscard]] QColor applyLimits(QColor color) const; [[nodiscard]] QColor applyLimits(QColor color) const;
int percentFromByte(int byte) { int percentFromByte(int byte) {
return snap(qRound(byte * 100 / 255.), 0, 100); return std::clamp(qRound(byte * 100 / 255.), 0, 100);
} }
int percentToByte(int percent) { int percentToByte(int percent) {
return snap(qRound(percent * 255 / 100.), 0, 255); return std::clamp(qRound(percent * 255 / 100.), 0, 255);
} }
class Picker; class Picker;

View file

@ -1440,7 +1440,10 @@ PeerListContent::SkipResult PeerListContent::selectSkip(int direction) {
} }
// Snap the index. // Snap the index.
newSelectedIndex = snap(newSelectedIndex, firstEnabled - 1, lastEnabled); newSelectedIndex = std::clamp(
newSelectedIndex,
firstEnabled - 1,
lastEnabled);
// Skip the disabled rows. // Skip the disabled rows.
if (newSelectedIndex < firstEnabled) { if (newSelectedIndex < firstEnabled) {

View file

@ -179,12 +179,15 @@ void SuggestionsWidget::scrollByWheelEvent(not_null<QWheelEvent*> e) {
const auto delta = e->pixelDelta().x() const auto delta = e->pixelDelta().x()
? e->pixelDelta().x() ? e->pixelDelta().x()
: e->angleDelta().x(); : e->angleDelta().x();
return snap(current - ((rtl() ? -1 : 1) * delta), 0, _scrollMax); return std::clamp(
current - ((rtl() ? -1 : 1) * delta),
0,
_scrollMax);
} else if (vertical) { } else if (vertical) {
const auto delta = e->pixelDelta().y() const auto delta = e->pixelDelta().y()
? e->pixelDelta().y() ? e->pixelDelta().y()
: e->angleDelta().y(); : e->angleDelta().y();
return snap(current - delta, 0, _scrollMax); return std::clamp(current - delta, 0, _scrollMax);
} }
return current; return current;
}(); }();
@ -241,7 +244,7 @@ void SuggestionsWidget::paintEvent(QPaintEvent *e) {
void SuggestionsWidget::paintFadings(Painter &p) const { void SuggestionsWidget::paintFadings(Painter &p) const {
const auto scroll = scrollCurrent(); const auto scroll = scrollCurrent();
const auto o_left = snap( const auto o_left = std::clamp(
scroll / float64(st::emojiSuggestionsFadeAfter), scroll / float64(st::emojiSuggestionsFadeAfter),
0., 0.,
1.); 1.);
@ -256,7 +259,7 @@ void SuggestionsWidget::paintFadings(Painter &p) const {
st::emojiSuggestionsFadeLeft.fill(p, rect); st::emojiSuggestionsFadeLeft.fill(p, rect);
p.setOpacity(1.); p.setOpacity(1.);
} }
const auto o_right = snap( const auto o_right = std::clamp(
(_scrollMax - scroll) / float64(st::emojiSuggestionsFadeAfter), (_scrollMax - scroll) / float64(st::emojiSuggestionsFadeAfter),
0., 0.,
1.); 1.);
@ -422,7 +425,7 @@ void SuggestionsWidget::mouseMoveEvent(QMouseEvent *e) {
const auto globalPosition = e->globalPos(); const auto globalPosition = e->globalPos();
if (_dragScrollStart >= 0) { if (_dragScrollStart >= 0) {
const auto delta = (_mousePressPosition.x() - globalPosition.x()); const auto delta = (_mousePressPosition.x() - globalPosition.x());
const auto scroll = snap( const auto scroll = std::clamp(
_dragScrollStart + (rtl() ? -1 : 1) * delta, _dragScrollStart + (rtl() ? -1 : 1) * delta,
0, 0,
_scrollMax); _scrollMax);

View file

@ -404,7 +404,7 @@ void StickersListWidget::Footer::setSelectedIcon(
auto iconsCountForCentering = (2 * _iconSel + 1); auto iconsCountForCentering = (2 * _iconSel + 1);
auto iconsWidthForCentering = iconsCountForCentering auto iconsWidthForCentering = iconsCountForCentering
* st::stickerIconWidth; * st::stickerIconWidth;
auto iconsXFinal = snap( auto iconsXFinal = std::clamp(
(_iconsLeft + iconsWidthForCentering + _iconsRight - width()) / 2, (_iconsLeft + iconsWidthForCentering + _iconsRight - width()) / 2,
0, 0,
_iconsMax); _iconsMax);
@ -486,13 +486,19 @@ void StickersListWidget::Footer::paintSelectionBar(Painter &p) const {
} }
void StickersListWidget::Footer::paintLeftRightFading(Painter &p) const { void StickersListWidget::Footer::paintLeftRightFading(Painter &p) const {
auto o_left = snap(_iconsX.current() / st::stickerIconLeft.width(), 0., 1.); auto o_left = std::clamp(
_iconsX.current() / st::stickerIconLeft.width(),
0.,
1.);
if (o_left > 0) { if (o_left > 0) {
p.setOpacity(o_left); p.setOpacity(o_left);
st::stickerIconLeft.fill(p, style::rtlrect(_iconsLeft, _iconsTop, st::stickerIconLeft.width(), st::emojiFooterHeight, width())); st::stickerIconLeft.fill(p, style::rtlrect(_iconsLeft, _iconsTop, st::stickerIconLeft.width(), st::emojiFooterHeight, width()));
p.setOpacity(1.); p.setOpacity(1.);
} }
auto o_right = snap((_iconsMax - _iconsX.current()) / st::stickerIconRight.width(), 0., 1.); auto o_right = std::clamp(
(_iconsMax - _iconsX.current()) / st::stickerIconRight.width(),
0.,
1.);
if (o_right > 0) { if (o_right > 0) {
p.setOpacity(o_right); p.setOpacity(o_right);
st::stickerIconRight.fill(p, style::rtlrect(width() - _iconsRight - st::stickerIconRight.width(), _iconsTop, st::stickerIconRight.width(), st::emojiFooterHeight, width())); st::stickerIconRight.fill(p, style::rtlrect(width() - _iconsRight - st::stickerIconRight.width(), _iconsTop, st::stickerIconRight.width(), st::emojiFooterHeight, width()));
@ -554,7 +560,11 @@ void StickersListWidget::Footer::mouseMoveEvent(QMouseEvent *e) {
} }
} }
if (_iconsDragging) { if (_iconsDragging) {
auto newX = snap(_iconsStartX + (rtl() ? -1 : 1) * (_iconsMouseDown.x() - _iconsMousePos.x()), 0, _iconsMax); auto newX = std::clamp(
(rtl() ? -1 : 1) * (_iconsMouseDown.x() - _iconsMousePos.x())
+ _iconsStartX,
0,
_iconsMax);
if (newX != qRound(_iconsX.current())) { if (newX != qRound(_iconsX.current())) {
_iconsX = anim::value(newX, newX); _iconsX = anim::value(newX, newX);
_iconsStartAnim = 0; _iconsStartAnim = 0;
@ -589,7 +599,10 @@ void StickersListWidget::Footer::mouseReleaseEvent(QMouseEvent *e) {
} }
void StickersListWidget::Footer::finishDragging() { void StickersListWidget::Footer::finishDragging() {
auto newX = snap(_iconsStartX + _iconsMouseDown.x() - _iconsMousePos.x(), 0, _iconsMax); auto newX = std::clamp(
_iconsStartX + _iconsMouseDown.x() - _iconsMousePos.x(),
0,
_iconsMax);
if (newX != qRound(_iconsX.current())) { if (newX != qRound(_iconsX.current())) {
_iconsX = anim::value(newX, newX); _iconsX = anim::value(newX, newX);
_iconsStartAnim = 0; _iconsStartAnim = 0;
@ -621,9 +634,19 @@ void StickersListWidget::Footer::scrollByWheelEvent(
} }
auto newX = qRound(_iconsX.current()); auto newX = qRound(_iconsX.current());
if (/*_horizontal && */horizontal) { if (/*_horizontal && */horizontal) {
newX = snap(newX - (rtl() ? -1 : 1) * (e->pixelDelta().x() ? e->pixelDelta().x() : e->angleDelta().x()), 0, _iconsMax); newX = std::clamp(
newX - (rtl() ? -1 : 1) * (e->pixelDelta().x()
? e->pixelDelta().x()
: e->angleDelta().x()),
0,
_iconsMax);
} else if (/*!_horizontal && */vertical) { } else if (/*!_horizontal && */vertical) {
newX = snap(newX - (e->pixelDelta().y() ? e->pixelDelta().y() : e->angleDelta().y()), 0, _iconsMax); newX = std::clamp(
newX - (e->pixelDelta().y()
? e->pixelDelta().y()
: e->angleDelta().y()),
0,
_iconsMax);
} }
if (newX != qRound(_iconsX.current())) { if (newX != qRound(_iconsX.current())) {
_iconsX = anim::value(newX, newX); _iconsX = anim::value(newX, newX);

View file

@ -151,7 +151,7 @@ void TabbedPanel::updateContentHeight() {
auto marginsHeight = _selector->marginTop() + _selector->marginBottom(); auto marginsHeight = _selector->marginTop() + _selector->marginBottom();
auto availableHeight = _bottom - marginsHeight; auto availableHeight = _bottom - marginsHeight;
auto wantedContentHeight = qRound(_heightRatio * availableHeight) - addedHeight; auto wantedContentHeight = qRound(_heightRatio * availableHeight) - addedHeight;
auto contentHeight = marginsHeight + snap( auto contentHeight = marginsHeight + std::clamp(
wantedContentHeight, wantedContentHeight,
_minContentHeight, _minContentHeight,
_maxContentHeight); _maxContentHeight);

View file

@ -153,8 +153,9 @@ void TabbedSelector::SlideAnimation::paintFrame(QPainter &p, float64 dt, float64
auto rightAlpha = (leftToRight ? departingAlpha : arrivingAlpha); auto rightAlpha = (leftToRight ? departingAlpha : arrivingAlpha);
// _innerLeft ..(left).. leftTo ..(both).. bothTo ..(none).. noneTo ..(right).. _innerRight // _innerLeft ..(left).. leftTo ..(both).. bothTo ..(none).. noneTo ..(right).. _innerRight
auto leftTo = _innerLeft + snap(_innerWidth + leftCoord, 0, _innerWidth); auto leftTo = _innerLeft
auto rightFrom = _innerLeft + snap(rightCoord, 0, _innerWidth); + std::clamp(_innerWidth + leftCoord, 0, _innerWidth);
auto rightFrom = _innerLeft + std::clamp(rightCoord, 0, _innerWidth);
auto painterRightFrom = rightFrom / cIntRetinaFactor(); auto painterRightFrom = rightFrom / cIntRetinaFactor();
if (opacity < 1.) { if (opacity < 1.) {
_frame.fill(Qt::transparent); _frame.fill(Qt::transparent);

View file

@ -101,7 +101,7 @@ QByteArray Settings::serialize() const {
<< qint32(_floatPlayerColumn) << qint32(_floatPlayerColumn)
<< qint32(_floatPlayerCorner) << qint32(_floatPlayerCorner)
<< qint32(_thirdSectionInfoEnabled ? 1 : 0) << qint32(_thirdSectionInfoEnabled ? 1 : 0)
<< qint32(snap( << qint32(std::clamp(
qRound(_dialogsWidthRatio.current() * 1000000), qRound(_dialogsWidthRatio.current() * 1000000),
0, 0,
1000000)) 1000000))
@ -259,7 +259,10 @@ void Settings::addFromSerialized(const QByteArray &serialized) {
>> thirdColumnWidth >> thirdColumnWidth
>> thirdSectionExtendedBy >> thirdSectionExtendedBy
>> notifyFromAll; >> notifyFromAll;
dialogsWidthRatio = snap(dialogsWidthRatioInt / 1000000., 0., 1.); dialogsWidthRatio = std::clamp(
dialogsWidthRatioInt / 1000000.,
0.,
1.);
} }
if (!stream.atEnd()) { if (!stream.atEnd()) {
stream >> nativeWindowFrame; stream >> nativeWindowFrame;

View file

@ -124,11 +124,6 @@ T rand_value() {
return result; return result;
} }
template <typename T>
inline T snap(const T &v, const T &_min, const T &_max) {
return (v < _min) ? _min : ((v > _max) ? _max : v);
}
QString translitRusEng(const QString &rus); QString translitRusEng(const QString &rus);
QString rusKeyboardLayoutSwitch(const QString &from); QString rusKeyboardLayoutSwitch(const QString &from);
@ -150,16 +145,22 @@ inline int rowscount(int fullCount, int countPerRow) {
return (fullCount + countPerRow - 1) / countPerRow; return (fullCount + countPerRow - 1) / countPerRow;
} }
inline int floorclamp(int value, int step, int lowest, int highest) { inline int floorclamp(int value, int step, int lowest, int highest) {
return qMin(qMax(value / step, lowest), highest); return std::clamp(value / step, lowest, highest);
} }
inline int floorclamp(float64 value, int step, int lowest, int highest) { inline int floorclamp(float64 value, int step, int lowest, int highest) {
return qMin(qMax(static_cast<int>(std::floor(value / step)), lowest), highest); return std::clamp(
static_cast<int>(std::floor(value / step)),
lowest,
highest);
} }
inline int ceilclamp(int value, int step, int lowest, int highest) { inline int ceilclamp(int value, int step, int lowest, int highest) {
return qMax(qMin((value + step - 1) / step, highest), lowest); return std::clamp((value + step - 1) / step, lowest, highest);
} }
inline int ceilclamp(float64 value, int32 step, int32 lowest, int32 highest) { inline int ceilclamp(float64 value, int32 step, int32 lowest, int32 highest) {
return qMax(qMin(static_cast<int>(std::ceil(value / step)), highest), lowest); return std::clamp(
static_cast<int>(std::ceil(value / step)),
lowest,
highest);
} }
static int32 FullArcLength = 360 * 16; static int32 FullArcLength = 360 * 16;

View file

@ -893,7 +893,7 @@ float64 DocumentData::progress() const {
if (uploadingData->size > 0) { if (uploadingData->size > 0) {
const auto result = float64(uploadingData->offset) const auto result = float64(uploadingData->offset)
/ uploadingData->size; / uploadingData->size;
return snap(result, 0., 1.); return std::clamp(result, 0., 1.);
} }
return 0.; return 0.;
} }

View file

@ -350,7 +350,7 @@ TimeId SortByOnlineValue(not_null<UserData*> user, TimeId now) {
crl::time OnlineChangeTimeout(TimeId online, TimeId now) { crl::time OnlineChangeTimeout(TimeId online, TimeId now) {
const auto result = OnlinePhraseChangeInSeconds(online, now); const auto result = OnlinePhraseChangeInSeconds(online, now);
Assert(result >= 0); Assert(result >= 0);
return snap( return std::clamp(
result * crl::time(1000), result * crl::time(1000),
kMinOnlineChangeTimeout, kMinOnlineChangeTimeout,
kMaxOnlineChangeTimeout); kMaxOnlineChangeTimeout);

View file

@ -166,7 +166,7 @@ float64 PhotoData::progress() const {
if (uploadingData->size > 0) { if (uploadingData->size > 0) {
const auto result = float64(uploadingData->offset) const auto result = float64(uploadingData->offset)
/ uploadingData->size; / uploadingData->size;
return snap(result, 0., 1.); return std::clamp(result, 0., 1.);
} }
return 0.; return 0.;
} }

View file

@ -2437,7 +2437,13 @@ void InnerWidget::selectSkip(int32 direction) {
? _collapsedSelected ? _collapsedSelected
: int(_collapsedRows.size() : int(_collapsedRows.size()
+ (list->cfind(_selected) - list->cbegin() - _skipTopDialogs)); + (list->cfind(_selected) - list->cbegin() - _skipTopDialogs));
cur = snap(cur + direction, 0, static_cast<int>(_collapsedRows.size() + list->size() - _skipTopDialogs - 1)); cur = std::clamp(
cur + direction,
0,
static_cast<int>(_collapsedRows.size()
+ list->size()
- _skipTopDialogs
- 1));
if (cur < _collapsedRows.size()) { if (cur < _collapsedRows.size()) {
_collapsedSelected = cur; _collapsedSelected = cur;
_selected = nullptr; _selected = nullptr;
@ -2477,7 +2483,13 @@ void InnerWidget::selectSkip(int32 direction) {
: (base::in_range(_peerSearchSelected, 0, _peerSearchResults.size()) : (base::in_range(_peerSearchSelected, 0, _peerSearchResults.size())
? (_peerSearchSelected + _filterResults.size() + _hashtagResults.size()) ? (_peerSearchSelected + _filterResults.size() + _hashtagResults.size())
: (_searchedSelected + _peerSearchResults.size() + _filterResults.size() + _hashtagResults.size()))); : (_searchedSelected + _peerSearchResults.size() + _filterResults.size() + _hashtagResults.size())));
cur = snap(cur + direction, 0, static_cast<int>(_hashtagResults.size() + _filterResults.size() + _peerSearchResults.size() + _searchResults.size()) - 1); cur = std::clamp(
cur + direction,
0,
static_cast<int>(_hashtagResults.size()
+ _filterResults.size()
+ _peerSearchResults.size()
+ _searchResults.size()) - 1);
if (cur < _hashtagResults.size()) { if (cur < _hashtagResults.size()) {
_hashtagSelected = cur; _hashtagSelected = cur;
_filteredSelected = _peerSearchSelected = _searchedSelected = -1; _filteredSelected = _peerSearchSelected = _searchedSelected = -1;

View file

@ -1535,7 +1535,9 @@ void InnerWidget::mouseActionFinish(const QPoint &screenPos, Qt::MouseButton but
void InnerWidget::updateSelected() { void InnerWidget::updateSelected() {
auto mousePosition = mapFromGlobal(_mousePosition); auto mousePosition = mapFromGlobal(_mousePosition);
auto point = QPoint(snap(mousePosition.x(), 0, width()), snap(mousePosition.y(), _visibleTop, _visibleBottom)); auto point = QPoint(
std::clamp(mousePosition.x(), 0, width()),
std::clamp(mousePosition.y(), _visibleTop, _visibleBottom));
auto itemPoint = QPoint(); auto itemPoint = QPoint();
auto begin = std::rbegin(_items), end = std::rend(_items); auto begin = std::rbegin(_items), end = std::rend(_items);

View file

@ -181,10 +181,13 @@ void Widget::showAtPosition(Data::MessagePosition position) {
bool Widget::showAtPositionNow(Data::MessagePosition position) { bool Widget::showAtPositionNow(Data::MessagePosition position) {
if (const auto scrollTop = _inner->scrollTopForPosition(position)) { if (const auto scrollTop = _inner->scrollTopForPosition(position)) {
const auto currentScrollTop = _scroll->scrollTop(); const auto currentScrollTop = _scroll->scrollTop();
const auto wanted = snap(*scrollTop, 0, _scroll->scrollTopMax()); const auto wanted = std::clamp(
*scrollTop,
0,
_scroll->scrollTopMax());
const auto fullDelta = (wanted - currentScrollTop); const auto fullDelta = (wanted - currentScrollTop);
const auto limit = _scroll->height(); const auto limit = _scroll->height();
const auto scrollDelta = snap(fullDelta, -limit, limit); const auto scrollDelta = std::clamp(fullDelta, -limit, limit);
_inner->animatedScrollTo( _inner->animatedScrollTo(
wanted, wanted,
position, position,
@ -432,7 +435,10 @@ void Widget::listContentRefreshed() {
} }
const auto position = *base::take(_nextAnimatedScrollPosition); const auto position = *base::take(_nextAnimatedScrollPosition);
if (const auto scrollTop = _inner->scrollTopForPosition(position)) { if (const auto scrollTop = _inner->scrollTopForPosition(position)) {
const auto wanted = snap(*scrollTop, 0, _scroll->scrollTopMax()); const auto wanted = std::clamp(
*scrollTop,
0,
_scroll->scrollTopMax());
_inner->animatedScrollTo( _inner->animatedScrollTo(
wanted, wanted,
position, position,

View file

@ -838,16 +838,28 @@ void HistoryInner::touchUpdateSpeed() {
const int oldSpeedX = _touchSpeed.x(); const int oldSpeedX = _touchSpeed.x();
if ((oldSpeedY <= 0 && newSpeedY <= 0) || ((oldSpeedY >= 0 && newSpeedY >= 0) if ((oldSpeedY <= 0 && newSpeedY <= 0) || ((oldSpeedY >= 0 && newSpeedY >= 0)
&& (oldSpeedX <= 0 && newSpeedX <= 0)) || (oldSpeedX >= 0 && newSpeedX >= 0)) { && (oldSpeedX <= 0 && newSpeedX <= 0)) || (oldSpeedX >= 0 && newSpeedX >= 0)) {
_touchSpeed.setY(snap((oldSpeedY + (newSpeedY / 4)), -Ui::kMaxScrollAccelerated, +Ui::kMaxScrollAccelerated)); _touchSpeed.setY(std::clamp(
_touchSpeed.setX(snap((oldSpeedX + (newSpeedX / 4)), -Ui::kMaxScrollAccelerated, +Ui::kMaxScrollAccelerated)); (oldSpeedY + (newSpeedY / 4)),
-Ui::kMaxScrollAccelerated,
+Ui::kMaxScrollAccelerated));
_touchSpeed.setX(std::clamp(
(oldSpeedX + (newSpeedX / 4)),
-Ui::kMaxScrollAccelerated,
+Ui::kMaxScrollAccelerated));
} else { } else {
_touchSpeed = QPoint(); _touchSpeed = QPoint();
} }
} else { } else {
// we average the speed to avoid strange effects with the last delta // we average the speed to avoid strange effects with the last delta
if (!_touchSpeed.isNull()) { if (!_touchSpeed.isNull()) {
_touchSpeed.setX(snap((_touchSpeed.x() / 4) + (newSpeedX * 3 / 4), -Ui::kMaxScrollFlick, +Ui::kMaxScrollFlick)); _touchSpeed.setX(std::clamp(
_touchSpeed.setY(snap((_touchSpeed.y() / 4) + (newSpeedY * 3 / 4), -Ui::kMaxScrollFlick, +Ui::kMaxScrollFlick)); (_touchSpeed.x() / 4) + (newSpeedX * 3 / 4),
-Ui::kMaxScrollFlick,
+Ui::kMaxScrollFlick));
_touchSpeed.setY(std::clamp(
(_touchSpeed.y() / 4) + (newSpeedY * 3 / 4),
-Ui::kMaxScrollFlick,
+Ui::kMaxScrollFlick));
} else { } else {
_touchSpeed = QPoint(newSpeedX, newSpeedY); _touchSpeed = QPoint(newSpeedX, newSpeedY);
} }

View file

@ -976,7 +976,7 @@ void HistoryWidget::animatedScrollToItem(MsgId msgId) {
return; return;
} }
auto scrollTo = snap( auto scrollTo = std::clamp(
itemTopForHighlight(to->mainView()), itemTopForHighlight(to->mainView()),
0, 0,
_scroll->scrollTopMax()); _scroll->scrollTopMax());
@ -6625,7 +6625,8 @@ void HistoryWidget::noSelectingScroll() {
} }
bool HistoryWidget::touchScroll(const QPoint &delta) { bool HistoryWidget::touchScroll(const QPoint &delta) {
int32 scTop = _scroll->scrollTop(), scMax = _scroll->scrollTopMax(), scNew = snap(scTop - delta.y(), 0, scMax); int32 scTop = _scroll->scrollTop(), scMax = _scroll->scrollTopMax();
const auto scNew = std::clamp(scTop - delta.y(), 0, scMax);
if (scNew == scTop) return false; if (scNew == scTop) return false;
_scroll->scrollToY(scNew); _scroll->scrollToY(scNew);

View file

@ -2269,7 +2269,9 @@ void ListWidget::mouseActionFinish(
void ListWidget::mouseActionUpdate() { void ListWidget::mouseActionUpdate() {
auto mousePosition = mapFromGlobal(_mousePosition); auto mousePosition = mapFromGlobal(_mousePosition);
auto point = QPoint(snap(mousePosition.x(), 0, width()), snap(mousePosition.y(), _visibleTop, _visibleBottom)); auto point = QPoint(
std::clamp(mousePosition.x(), 0, width()),
std::clamp(mousePosition.y(), _visibleTop, _visibleBottom));
const auto view = strictFindItemByY(point.y()); const auto view = strictFindItemByY(point.y());
const auto item = view ? view->data().get() : nullptr; const auto item = view ? view->data().get() : nullptr;

View file

@ -1319,7 +1319,7 @@ TextState Message::textState(
} }
checkForPointInTime(); checkForPointInTime();
if (const auto size = rightActionSize()) { if (const auto size = rightActionSize()) {
const auto fastShareSkip = snap( const auto fastShareSkip = std::clamp(
(g.height() - size->height()) / 2, (g.height() - size->height()) / 2,
0, 0,
st::historyFastShareBottom); st::historyFastShareBottom);

View file

@ -221,10 +221,13 @@ bool PinnedWidget::showAtPositionNow(
const auto use = item ? item->position() : position; const auto use = item ? item->position() : position;
if (const auto scrollTop = _inner->scrollTopForPosition(use)) { if (const auto scrollTop = _inner->scrollTopForPosition(use)) {
const auto currentScrollTop = _scroll->scrollTop(); const auto currentScrollTop = _scroll->scrollTop();
const auto wanted = snap(*scrollTop, 0, _scroll->scrollTopMax()); const auto wanted = std::clamp(
*scrollTop,
0,
_scroll->scrollTopMax());
const auto fullDelta = (wanted - currentScrollTop); const auto fullDelta = (wanted - currentScrollTop);
const auto limit = _scroll->height(); const auto limit = _scroll->height();
const auto scrollDelta = snap(fullDelta, -limit, limit); const auto scrollDelta = std::clamp(fullDelta, -limit, limit);
const auto type = (animated == anim::type::instant) const auto type = (animated == anim::type::instant)
? AnimatedScroll::None ? AnimatedScroll::None
: (std::abs(fullDelta) > limit) : (std::abs(fullDelta) > limit)

View file

@ -1219,10 +1219,13 @@ bool RepliesWidget::showAtPositionNow(
calculateNextReplyReturn(); calculateNextReplyReturn();
} }
const auto currentScrollTop = _scroll->scrollTop(); const auto currentScrollTop = _scroll->scrollTop();
const auto wanted = snap(*scrollTop, 0, _scroll->scrollTopMax()); const auto wanted = std::clamp(
*scrollTop,
0,
_scroll->scrollTopMax());
const auto fullDelta = (wanted - currentScrollTop); const auto fullDelta = (wanted - currentScrollTop);
const auto limit = _scroll->height(); const auto limit = _scroll->height();
const auto scrollDelta = snap(fullDelta, -limit, limit); const auto scrollDelta = std::clamp(fullDelta, -limit, limit);
const auto type = (animated == anim::type::instant) const auto type = (animated == anim::type::instant)
? AnimatedScroll::None ? AnimatedScroll::None
: (std::abs(fullDelta) > limit) : (std::abs(fullDelta) > limit)

View file

@ -433,7 +433,7 @@ void TimeInput::paintEvent(QPaintEvent *e) {
auto borderShownDegree = _a_borderShown.value(1.); auto borderShownDegree = _a_borderShown.value(1.);
auto borderOpacity = _a_borderOpacity.value(_borderVisible ? 1. : 0.); auto borderOpacity = _a_borderOpacity.value(_borderVisible ? 1. : 0.);
if (_st.borderActive && (borderOpacity > 0.)) { if (_st.borderActive && (borderOpacity > 0.)) {
auto borderStart = snap(_borderAnimationStart, 0, width()); auto borderStart = std::clamp(_borderAnimationStart, 0, width());
auto borderFrom = qRound(borderStart * (1. - borderShownDegree)); auto borderFrom = qRound(borderStart * (1. - borderShownDegree));
auto borderTo = borderStart + qRound((width() - borderStart) * borderShownDegree); auto borderTo = borderStart + qRound((width() - borderStart) * borderShownDegree);
if (borderTo > borderFrom) { if (borderTo > borderFrom) {

View file

@ -810,10 +810,13 @@ void ScheduledWidget::showAtPosition(Data::MessagePosition position) {
bool ScheduledWidget::showAtPositionNow(Data::MessagePosition position) { bool ScheduledWidget::showAtPositionNow(Data::MessagePosition position) {
if (const auto scrollTop = _inner->scrollTopForPosition(position)) { if (const auto scrollTop = _inner->scrollTopForPosition(position)) {
const auto currentScrollTop = _scroll->scrollTop(); const auto currentScrollTop = _scroll->scrollTop();
const auto wanted = snap(*scrollTop, 0, _scroll->scrollTopMax()); const auto wanted = std::clamp(
*scrollTop,
0,
_scroll->scrollTopMax());
const auto fullDelta = (wanted - currentScrollTop); const auto fullDelta = (wanted - currentScrollTop);
const auto limit = _scroll->height(); const auto limit = _scroll->height();
const auto scrollDelta = snap(fullDelta, -limit, limit); const auto scrollDelta = std::clamp(fullDelta, -limit, limit);
_inner->scrollTo( _inner->scrollTo(
wanted, wanted,
position, position,

View file

@ -778,7 +778,11 @@ void Document::updatePressed(QPoint point) {
const auto &st = thumbed ? st::msgFileThumbLayout : st::msgFileLayout; const auto &st = thumbed ? st::msgFileThumbLayout : st::msgFileLayout;
const auto nameleft = st.padding.left() + st.thumbSize + st.padding.right(); const auto nameleft = st.padding.left() + st.thumbSize + st.padding.right();
const auto nameright = st.padding.left(); const auto nameright = st.padding.left();
voice->setSeekingCurrent(snap((point.x() - nameleft) / float64(width() - nameleft - nameright), 0., 1.)); voice->setSeekingCurrent(std::clamp(
(point.x() - nameleft)
/ float64(width() - nameleft - nameright),
0.,
1.));
history()->owner().requestViewRepaint(_parent); history()->owner().requestViewRepaint(_parent);
} }
} }
@ -863,7 +867,12 @@ bool Document::updateStatusText() const {
bool was = (voice->_playback != nullptr); bool was = (voice->_playback != nullptr);
voice->ensurePlayback(this); voice->ensurePlayback(this);
if (!was || state.position != voice->_playback->position) { if (!was || state.position != voice->_playback->position) {
auto prg = state.length ? snap(float64(state.position) / state.length, 0., 1.) : 0.; auto prg = state.length
? std::clamp(
float64(state.position) / state.length,
0.,
1.)
: 0.;
if (voice->_playback->position < state.position) { if (voice->_playback->position < state.position) {
voice->_playback->progress.start(prg); voice->_playback->progress.start(prg);
} else { } else {

View file

@ -207,7 +207,7 @@ int LayerWidget::resizeGetHeight(int newWidth) {
auto windowWidth = parentSize.width(); auto windowWidth = parentSize.width();
auto windowHeight = parentSize.height(); auto windowHeight = parentSize.height();
auto newLeft = (windowWidth - newWidth) / 2; auto newLeft = (windowWidth - newWidth) / 2;
auto newTop = snap( auto newTop = std::clamp(
windowHeight / 24, windowHeight / 24,
st::infoLayerTopMinimal, st::infoLayerTopMinimal,
st::infoLayerTopMaximal); st::infoLayerTopMaximal);

View file

@ -87,7 +87,10 @@ void Widget::moveBottom(int bottom) {
void Widget::updateContentHeight() { void Widget::updateContentHeight() {
auto addedHeight = innerPadding().top() + innerPadding().bottom(); auto addedHeight = innerPadding().top() + innerPadding().bottom();
auto wantedContentHeight = qRound(st::emojiPanHeightRatio * _bottom) - addedHeight; auto wantedContentHeight = qRound(st::emojiPanHeightRatio * _bottom) - addedHeight;
auto contentHeight = snap(wantedContentHeight, st::inlineResultsMinHeight, st::inlineResultsMaxHeight); auto contentHeight = std::clamp(
wantedContentHeight,
st::inlineResultsMinHeight,
st::inlineResultsMaxHeight);
accumulate_min(contentHeight, _bottom - addedHeight); accumulate_min(contentHeight, _bottom - addedHeight);
accumulate_min(contentHeight, _contentMaxHeight); accumulate_min(contentHeight, _contentMaxHeight);
auto resultTop = _bottom - addedHeight - contentHeight; auto resultTop = _bottom - addedHeight - contentHeight;

View file

@ -411,7 +411,11 @@ int Step::contentTop() const {
accumulate_max(result, st::introStepTopMin); accumulate_max(result, st::introStepTopMin);
if (_hasCover) { if (_hasCover) {
const auto currentHeightFull = result + st::introNextTop + st::introContentTopAdd; const auto currentHeightFull = result + st::introNextTop + st::introContentTopAdd;
auto added = 1. - snap(float64(currentHeightFull - st::windowMinHeight) / (st::introStepHeightFull - st::windowMinHeight), 0., 1.); auto added = 1. - std::clamp(
float64(currentHeightFull - st::windowMinHeight)
/ (st::introStepHeightFull - st::windowMinHeight),
0.,
1.);
result += qRound(added * st::introContentTopAdd); result += qRound(added * st::introContentTopAdd);
} }
return result; return result;

View file

@ -189,7 +189,7 @@ void SessionSettings::addFromSerialized(const QByteArray &serialized) {
if (!stream.atEnd()) { if (!stream.atEnd()) {
qint32 value = 0; qint32 value = 0;
stream >> value; stream >> value;
appDialogsWidthRatio = snap(value / 1000000., 0., 1.); appDialogsWidthRatio = std::clamp(value / 1000000., 0., 1.);
stream >> value; stream >> value;
appThirdColumnWidth = value; appThirdColumnWidth = value;

View file

@ -2406,7 +2406,7 @@ void MainWidget::ensureThirdColumnResizeAreaCreated() {
if (!Adaptive::ThreeColumn() || !_thirdSection) { if (!Adaptive::ThreeColumn() || !_thirdSection) {
return; return;
} }
Core::App().settings().setThirdColumnWidth(snap( Core::App().settings().setThirdColumnWidth(std::clamp(
Core::App().settings().thirdColumnWidth(), Core::App().settings().thirdColumnWidth(),
st::columnMinimalWidthThird, st::columnMinimalWidthThird,
st::columnMaximalWidthThird)); st::columnMaximalWidthThird));

View file

@ -111,7 +111,7 @@ float64 Float::outRatio() const {
if (y() + height() > parent.y() + parent.height()) { if (y() + height() > parent.y() + parent.height()) {
accumulate_min(min, 1. - (y() + height() - parent.y() - parent.height()) / float64(height())); accumulate_min(min, 1. - (y() + height() - parent.y() - parent.height()) / float64(height()));
} }
return snap(min, 0., 1.); return std::clamp(min, 0., 1.);
} }
void Float::mouseReleaseEvent(QMouseEvent *e) { void Float::mouseReleaseEvent(QMouseEvent *e) {

View file

@ -250,7 +250,7 @@ Widget::~Widget() = default;
void Widget::handleSeekProgress(float64 progress) { void Widget::handleSeekProgress(float64 progress) {
if (!_lastDurationMs) return; if (!_lastDurationMs) return;
const auto positionMs = snap( const auto positionMs = std::clamp(
static_cast<crl::time>(progress * _lastDurationMs), static_cast<crl::time>(progress * _lastDurationMs),
crl::time(0), crl::time(0),
_lastDurationMs); _lastDurationMs);
@ -265,7 +265,7 @@ void Widget::handleSeekProgress(float64 progress) {
void Widget::handleSeekFinished(float64 progress) { void Widget::handleSeekFinished(float64 progress) {
if (!_lastDurationMs) return; if (!_lastDurationMs) return;
const auto positionMs = snap( const auto positionMs = std::clamp(
static_cast<crl::time>(progress * _lastDurationMs), static_cast<crl::time>(progress * _lastDurationMs),
crl::time(0), crl::time(0),
_lastDurationMs); _lastDurationMs);

View file

@ -104,7 +104,7 @@ PlaybackControls::PlaybackControls(
void PlaybackControls::handleSeekProgress(float64 progress) { void PlaybackControls::handleSeekProgress(float64 progress) {
if (!_lastDurationMs) return; if (!_lastDurationMs) return;
const auto positionMs = snap( const auto positionMs = std::clamp(
static_cast<crl::time>(progress * _lastDurationMs), static_cast<crl::time>(progress * _lastDurationMs),
crl::time(0), crl::time(0),
_lastDurationMs); _lastDurationMs);
@ -120,7 +120,7 @@ void PlaybackControls::handleSeekProgress(float64 progress) {
void PlaybackControls::handleSeekFinished(float64 progress) { void PlaybackControls::handleSeekFinished(float64 progress) {
if (!_lastDurationMs) return; if (!_lastDurationMs) return;
const auto positionMs = snap( const auto positionMs = std::clamp(
static_cast<crl::time>(progress * _lastDurationMs), static_cast<crl::time>(progress * _lastDurationMs),
crl::time(0), crl::time(0),
_lastDurationMs); _lastDurationMs);

View file

@ -58,10 +58,10 @@ void PlaybackProgress::updateState(
const auto progress = (position > length) const auto progress = (position > length)
? 1. ? 1.
: length : length
? snap(float64(position) / length, 0., 1.) ? std::clamp(float64(position) / length, 0., 1.)
: 0.; : 0.;
const auto availableTillProgress = (availableTill > position) const auto availableTillProgress = (availableTill > position)
? snap(float64(availableTill) / length, 0., 1.) ? std::clamp(float64(availableTill) / length, 0., 1.)
: -1.; : -1.;
const auto animatedPosition = position + (state.frequency * kPlaybackAnimationDurationMs / 1000); const auto animatedPosition = position + (state.frequency * kPlaybackAnimationDurationMs / 1000);
const auto animatedProgress = length ? qMax(float64(animatedPosition) / length, 0.) : 0.; const auto animatedProgress = length ? qMax(float64(animatedPosition) / length, 0.) : 0.;

View file

@ -1073,7 +1073,10 @@ void SessionPrivate::onSentSome(uint64 size) {
if (!_oldConnection) { if (!_oldConnection) {
// 8kb / sec, so 512 kb give 64 sec // 8kb / sec, so 512 kb give 64 sec
auto remainBySize = size * _waitForReceived / 8192; auto remainBySize = size * _waitForReceived / 8192;
remain = snap(remainBySize, remain, uint64(kMaxReceiveTimeout)); remain = std::clamp(
remainBySize,
remain,
uint64(kMaxReceiveTimeout));
if (remain != _waitForReceived) { if (remain != _waitForReceived) {
DEBUG_LOG(("Checking connect for request with size %1 bytes, delay will be %2").arg(size).arg(remain)); DEBUG_LOG(("Checking connect for request with size %1 bytes, delay will be %2").arg(size).arg(remain));
} }

View file

@ -680,7 +680,7 @@ void DateRow::paintEvent(QPaintEvent *e) {
auto borderShownDegree = _a_borderShown.value(1.); auto borderShownDegree = _a_borderShown.value(1.);
auto borderOpacity = _a_borderOpacity.value(_borderVisible ? 1. : 0.); auto borderOpacity = _a_borderOpacity.value(_borderVisible ? 1. : 0.);
if (_st.borderActive && (borderOpacity > 0.)) { if (_st.borderActive && (borderOpacity > 0.)) {
auto borderStart = snap(_borderAnimationStart, 0, width); auto borderStart = std::clamp(_borderAnimationStart, 0, width);
auto borderFrom = qRound(borderStart * (1. - borderShownDegree)); auto borderFrom = qRound(borderStart * (1. - borderShownDegree));
auto borderTo = borderStart + qRound((width - borderStart) * borderShownDegree); auto borderTo = borderStart + qRound((width - borderStart) * borderShownDegree);
if (borderTo > borderFrom) { if (borderTo > borderFrom) {

View file

@ -473,7 +473,7 @@ int LayerWidget::resizeGetHeight(int newWidth) {
_tillTop = _tillBottom = true; _tillTop = _tillBottom = true;
return windowHeight; return windowHeight;
} }
auto newTop = snap( auto newTop = std::clamp(
windowHeight / 24, windowHeight / 24,
st::infoLayerTopMinimal, st::infoLayerTopMinimal,
st::infoLayerTopMaximal); st::infoLayerTopMaximal);

View file

@ -41,7 +41,7 @@ namespace {
constexpr auto kMaxNotificationsCount = 5; constexpr auto kMaxNotificationsCount = 5;
[[nodiscard]] int CurrentCount() { [[nodiscard]] int CurrentCount() {
return snap( return std::clamp(
Core::App().settings().notificationsCount(), Core::App().settings().notificationsCount(),
1, 1,
kMaxNotificationsCount); kMaxNotificationsCount);

View file

@ -1053,7 +1053,7 @@ bool ReadSetting(
stream >> v; stream >> v;
if (!CheckStreamStatus(stream)) return false; if (!CheckStreamStatus(stream)) return false;
Core::App().settings().setSongVolume(snap(v / 1e6, 0., 1.)); Core::App().settings().setSongVolume(std::clamp(v / 1e6, 0., 1.));
context.legacyRead = true; context.legacyRead = true;
} break; } break;
@ -1062,7 +1062,7 @@ bool ReadSetting(
stream >> v; stream >> v;
if (!CheckStreamStatus(stream)) return false; if (!CheckStreamStatus(stream)) return false;
Core::App().settings().setVideoVolume(snap(v / 1e6, 0., 1.)); Core::App().settings().setVideoVolume(std::clamp(v / 1e6, 0., 1.));
context.legacyRead = true; context.legacyRead = true;
} break; } break;

View file

@ -80,7 +80,7 @@ QString StateDescription(const BlobState &state, tr::phrase<> activeText) {
return activeText(tr::now); return activeText(tr::now);
}, [](const Loading &data) { }, [](const Loading &data) {
const auto percent = (data.size > 0) const auto percent = (data.size > 0)
? snap((data.already * 100) / float64(data.size), 0., 100.) ? std::clamp((data.already * 100) / float64(data.size), 0., 100.)
: 0.; : 0.;
return tr::lng_emoji_set_loading( return tr::lng_emoji_set_loading(
tr::now, tr::now,

View file

@ -528,7 +528,7 @@ void ConfirmContactBox::prepare() {
_contact->initDimensions(); _contact->initDimensions();
accumulate_max(maxWidth, _contact->maxWidth()); accumulate_max(maxWidth, _contact->maxWidth());
maxWidth += st::boxPadding.left() + st::boxPadding.right(); maxWidth += st::boxPadding.left() + st::boxPadding.right();
const auto width = snap(maxWidth, st::boxWidth, st::boxWideWidth); const auto width = std::clamp(maxWidth, st::boxWidth, st::boxWideWidth);
const auto available = width const auto available = width
- st::boxPadding.left() - st::boxPadding.left()
- st::boxPadding.right(); - st::boxPadding.right();

View file

@ -423,7 +423,7 @@ void RoundImageCheckbox::paint(Painter &p, int x, int y, int outerWidth) {
if (selectionLevel > 0) { if (selectionLevel > 0) {
PainterHighQualityEnabler hq(p); PainterHighQualityEnabler hq(p);
p.setOpacity(snap(selectionLevel, 0., 1.)); p.setOpacity(std::clamp(selectionLevel, 0., 1.));
p.setBrush(Qt::NoBrush); p.setBrush(Qt::NoBrush);
auto pen = _st.selectFg->p; auto pen = _st.selectFg->p;
pen.setWidth(_st.selectWidth); pen.setWidth(_st.selectWidth);
@ -438,7 +438,7 @@ void RoundImageCheckbox::paint(Painter &p, int x, int y, int outerWidth) {
} }
float64 RoundImageCheckbox::checkedAnimationRatio() const { float64 RoundImageCheckbox::checkedAnimationRatio() const {
return snap(_selection.value(checked() ? 1. : 0.), 0., 1.); return std::clamp(_selection.value(checked() ? 1. : 0.), 0., 1.);
} }
void RoundImageCheckbox::setChecked(bool newChecked, anim::type animated) { void RoundImageCheckbox::setChecked(bool newChecked, anim::type animated) {

View file

@ -78,7 +78,7 @@ float64 ContinuousSlider::computeValue(const QPoint &pos) const {
const auto result = isHorizontal() ? const auto result = isHorizontal() ?
(pos.x() - seekRect.x()) / float64(seekRect.width()) : (pos.x() - seekRect.x()) / float64(seekRect.width()) :
(1. - (pos.y() - seekRect.y()) / float64(seekRect.height())); (1. - (pos.y() - seekRect.y()) / float64(seekRect.height()));
const auto snapped = snap(result, 0., 1.); const auto snapped = std::clamp(result, 0., 1.);
return _adjustCallback ? _adjustCallback(snapped) : snapped; return _adjustCallback ? _adjustCallback(snapped) : snapped;
} }
@ -120,7 +120,7 @@ void ContinuousSlider::wheelEvent(QWheelEvent *e) {
deltaX *= -1; deltaX *= -1;
} }
auto delta = (qAbs(deltaX) > qAbs(deltaY)) ? deltaX : deltaY; auto delta = (qAbs(deltaX) > qAbs(deltaY)) ? deltaX : deltaY;
auto finalValue = snap(_value + delta * coef, 0., 1.); auto finalValue = std::clamp(_value + delta * coef, 0., 1.);
setValue(finalValue); setValue(finalValue);
if (_changeProgressCallback) { if (_changeProgressCallback) {
_changeProgressCallback(finalValue); _changeProgressCallback(finalValue);

View file

@ -280,7 +280,11 @@ void SettingsSlider::paintEvent(QPaintEvent *e) {
auto activeLeft = getCurrentActiveLeft(); auto activeLeft = getCurrentActiveLeft();
enumerateSections([&](Section &section) { enumerateSections([&](Section &section) {
auto active = 1. - snap(qAbs(activeLeft - section.left) / float64(section.width), 0., 1.); auto active = 1.
- std::clamp(
qAbs(activeLeft - section.left) / float64(section.width),
0.,
1.);
if (section.ripple) { if (section.ripple) {
auto color = anim::color(_st.rippleBg, _st.rippleBgActive, active); auto color = anim::color(_st.rippleBg, _st.rippleBgActive, active);
section.ripple->paint(p, section.left, 0, width(), &color); section.ripple->paint(p, section.left, 0, width(), &color);