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 timeout = (data.vexpires().v - base::unixtime::now());
_termsUpdateSendAt = crl::now() + snap(
_termsUpdateSendAt = crl::now() + std::clamp(
timeout * crl::time(1000),
kTermsUpdateTimeoutMin,
kTermsUpdateTimeoutMax);

View File

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

View File

@ -66,10 +66,10 @@ private:
[[nodiscard]] QColor applyLimits(QColor color) const;
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) {
return snap(qRound(percent * 255 / 100.), 0, 255);
return std::clamp(qRound(percent * 255 / 100.), 0, 255);
}
class Picker;

View File

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

View File

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

View File

@ -404,7 +404,7 @@ void StickersListWidget::Footer::setSelectedIcon(
auto iconsCountForCentering = (2 * _iconSel + 1);
auto iconsWidthForCentering = iconsCountForCentering
* st::stickerIconWidth;
auto iconsXFinal = snap(
auto iconsXFinal = std::clamp(
(_iconsLeft + iconsWidthForCentering + _iconsRight - width()) / 2,
0,
_iconsMax);
@ -486,13 +486,19 @@ void StickersListWidget::Footer::paintSelectionBar(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) {
p.setOpacity(o_left);
st::stickerIconLeft.fill(p, style::rtlrect(_iconsLeft, _iconsTop, st::stickerIconLeft.width(), st::emojiFooterHeight, width()));
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) {
p.setOpacity(o_right);
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) {
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())) {
_iconsX = anim::value(newX, newX);
_iconsStartAnim = 0;
@ -589,7 +599,10 @@ void StickersListWidget::Footer::mouseReleaseEvent(QMouseEvent *e) {
}
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())) {
_iconsX = anim::value(newX, newX);
_iconsStartAnim = 0;
@ -621,9 +634,19 @@ void StickersListWidget::Footer::scrollByWheelEvent(
}
auto newX = qRound(_iconsX.current());
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) {
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())) {
_iconsX = anim::value(newX, newX);

View File

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

View File

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

View File

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

View File

@ -124,11 +124,6 @@ T rand_value() {
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 rusKeyboardLayoutSwitch(const QString &from);
@ -150,16 +145,22 @@ inline int rowscount(int fullCount, int countPerRow) {
return (fullCount + countPerRow - 1) / countPerRow;
}
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) {
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) {
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) {
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;

View File

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

View File

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

View File

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

View File

@ -2437,7 +2437,13 @@ void InnerWidget::selectSkip(int32 direction) {
? _collapsedSelected
: int(_collapsedRows.size()
+ (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()) {
_collapsedSelected = cur;
_selected = nullptr;
@ -2477,7 +2483,13 @@ void InnerWidget::selectSkip(int32 direction) {
: (base::in_range(_peerSearchSelected, 0, _peerSearchResults.size())
? (_peerSearchSelected + _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()) {
_hashtagSelected = cur;
_filteredSelected = _peerSearchSelected = _searchedSelected = -1;

View File

@ -1535,7 +1535,9 @@ void InnerWidget::mouseActionFinish(const QPoint &screenPos, Qt::MouseButton but
void InnerWidget::updateSelected() {
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 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) {
if (const auto scrollTop = _inner->scrollTopForPosition(position)) {
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 limit = _scroll->height();
const auto scrollDelta = snap(fullDelta, -limit, limit);
const auto scrollDelta = std::clamp(fullDelta, -limit, limit);
_inner->animatedScrollTo(
wanted,
position,
@ -432,7 +435,10 @@ void Widget::listContentRefreshed() {
}
const auto position = *base::take(_nextAnimatedScrollPosition);
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(
wanted,
position,

View File

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

View File

@ -976,7 +976,7 @@ void HistoryWidget::animatedScrollToItem(MsgId msgId) {
return;
}
auto scrollTo = snap(
auto scrollTo = std::clamp(
itemTopForHighlight(to->mainView()),
0,
_scroll->scrollTopMax());
@ -6625,7 +6625,8 @@ void HistoryWidget::noSelectingScroll() {
}
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;
_scroll->scrollToY(scNew);

View File

@ -2269,7 +2269,9 @@ void ListWidget::mouseActionFinish(
void ListWidget::mouseActionUpdate() {
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 item = view ? view->data().get() : nullptr;

View File

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

View File

@ -221,10 +221,13 @@ bool PinnedWidget::showAtPositionNow(
const auto use = item ? item->position() : position;
if (const auto scrollTop = _inner->scrollTopForPosition(use)) {
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 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)
? AnimatedScroll::None
: (std::abs(fullDelta) > limit)

View File

@ -1219,10 +1219,13 @@ bool RepliesWidget::showAtPositionNow(
calculateNextReplyReturn();
}
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 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)
? AnimatedScroll::None
: (std::abs(fullDelta) > limit)

View File

@ -433,7 +433,7 @@ void TimeInput::paintEvent(QPaintEvent *e) {
auto borderShownDegree = _a_borderShown.value(1.);
auto borderOpacity = _a_borderOpacity.value(_borderVisible ? 1. : 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 borderTo = borderStart + qRound((width() - borderStart) * borderShownDegree);
if (borderTo > borderFrom) {

View File

@ -810,10 +810,13 @@ void ScheduledWidget::showAtPosition(Data::MessagePosition position) {
bool ScheduledWidget::showAtPositionNow(Data::MessagePosition position) {
if (const auto scrollTop = _inner->scrollTopForPosition(position)) {
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 limit = _scroll->height();
const auto scrollDelta = snap(fullDelta, -limit, limit);
const auto scrollDelta = std::clamp(fullDelta, -limit, limit);
_inner->scrollTo(
wanted,
position,

View File

@ -778,7 +778,11 @@ void Document::updatePressed(QPoint point) {
const auto &st = thumbed ? st::msgFileThumbLayout : st::msgFileLayout;
const auto nameleft = st.padding.left() + st.thumbSize + st.padding.right();
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);
}
}
@ -863,7 +867,12 @@ bool Document::updateStatusText() const {
bool was = (voice->_playback != nullptr);
voice->ensurePlayback(this);
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) {
voice->_playback->progress.start(prg);
} else {

View File

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

View File

@ -87,7 +87,10 @@ void Widget::moveBottom(int bottom) {
void Widget::updateContentHeight() {
auto addedHeight = innerPadding().top() + innerPadding().bottom();
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, _contentMaxHeight);
auto resultTop = _bottom - addedHeight - contentHeight;

View File

@ -411,7 +411,11 @@ int Step::contentTop() const {
accumulate_max(result, st::introStepTopMin);
if (_hasCover) {
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);
}
return result;

View File

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

View File

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

View File

@ -111,7 +111,7 @@ float64 Float::outRatio() const {
if (y() + height() > parent.y() + parent.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) {

View File

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

View File

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

View File

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

View File

@ -1073,7 +1073,10 @@ void SessionPrivate::onSentSome(uint64 size) {
if (!_oldConnection) {
// 8kb / sec, so 512 kb give 64 sec
auto remainBySize = size * _waitForReceived / 8192;
remain = snap(remainBySize, remain, uint64(kMaxReceiveTimeout));
remain = std::clamp(
remainBySize,
remain,
uint64(kMaxReceiveTimeout));
if (remain != _waitForReceived) {
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 borderOpacity = _a_borderOpacity.value(_borderVisible ? 1. : 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 borderTo = borderStart + qRound((width - borderStart) * borderShownDegree);
if (borderTo > borderFrom) {

View File

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

View File

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

View File

@ -1053,7 +1053,7 @@ bool ReadSetting(
stream >> v;
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;
} break;
@ -1062,7 +1062,7 @@ bool ReadSetting(
stream >> v;
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;
} break;

View File

@ -80,7 +80,7 @@ QString StateDescription(const BlobState &state, tr::phrase<> activeText) {
return activeText(tr::now);
}, [](const Loading &data) {
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.;
return tr::lng_emoji_set_loading(
tr::now,

View File

@ -528,7 +528,7 @@ void ConfirmContactBox::prepare() {
_contact->initDimensions();
accumulate_max(maxWidth, _contact->maxWidth());
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
- st::boxPadding.left()
- st::boxPadding.right();

View File

@ -423,7 +423,7 @@ void RoundImageCheckbox::paint(Painter &p, int x, int y, int outerWidth) {
if (selectionLevel > 0) {
PainterHighQualityEnabler hq(p);
p.setOpacity(snap(selectionLevel, 0., 1.));
p.setOpacity(std::clamp(selectionLevel, 0., 1.));
p.setBrush(Qt::NoBrush);
auto pen = _st.selectFg->p;
pen.setWidth(_st.selectWidth);
@ -438,7 +438,7 @@ void RoundImageCheckbox::paint(Painter &p, int x, int y, int outerWidth) {
}
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) {

View File

@ -78,7 +78,7 @@ float64 ContinuousSlider::computeValue(const QPoint &pos) const {
const auto result = isHorizontal() ?
(pos.x() - seekRect.x()) / float64(seekRect.width()) :
(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;
}
@ -120,7 +120,7 @@ void ContinuousSlider::wheelEvent(QWheelEvent *e) {
deltaX *= -1;
}
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);
if (_changeProgressCallback) {
_changeProgressCallback(finalValue);

View File

@ -280,7 +280,11 @@ void SettingsSlider::paintEvent(QPaintEvent *e) {
auto activeLeft = getCurrentActiveLeft();
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) {
auto color = anim::color(_st.rippleBg, _st.rippleBgActive, active);
section.ripple->paint(p, section.left, 0, width(), &color);