Replaced floorclamp and ceilclamp with Ui::RowsInRange.

This commit is contained in:
23rd
2026-09-15 08:49:44 +03:00
parent 3a346455fc
commit 24e6443fc6
12 changed files with 67 additions and 113 deletions

View File

@@ -1688,8 +1688,11 @@ void PeerListContent::paintEvent(QPaintEvent *e) {
if (_mode != Mode::Custom) {
auto fill = QRegion(clip);
if (count > 0 && !sectionsShown()) {
const auto from = floorclamp(yFrom, _rowHeight, 0, count);
const auto to = ceilclamp(yTo, _rowHeight, 0, count);
const auto [from, to] = Ui::RowsInRange(
yFrom,
yTo,
_rowHeight,
count);
for (auto index = from; index != to; ++index) {
if (getRow(RowIndex(index))->opacity() == 1.) {
fill -= QRect(
@@ -1737,8 +1740,11 @@ void PeerListContent::paintEvent(QPaintEvent *e) {
p.translate(0, -top);
}
} else if (count > 0) {
const auto from = floorclamp(yFrom, _rowHeight, 0, count);
const auto to = ceilclamp(yTo, _rowHeight, 0, count);
const auto [from, to] = Ui::RowsInRange(
yFrom,
yTo,
_rowHeight,
count);
p.translate(0, from * _rowHeight);
for (auto index = from; index != to; ++index) {
handleRepaintAfter(paintRow(p, now, RowIndex(index)));

View File

@@ -37,6 +37,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "ui/widgets/shadow.h"
#include "ui/cached_round_corners.h"
#include "ui/painter.h"
#include "ui/ui_utility.h"
#include "ui/rect.h"
#include "ui/unread_badge_paint.h"
#include "styles/style_boxes.h"
@@ -1343,15 +1344,10 @@ void StickersBox::Inner::paintEvent(QPaintEvent *e) {
const auto yFrom = clip.y() - _itemsTop;
const auto yTo = clip.y() + clip.height() - _itemsTop;
const auto from = floorclamp(
const auto [from, to] = Ui::RowsInRange(
yFrom - _rowHeight,
_rowHeight,
0,
_rows.size());
const auto to = ceilclamp(
yTo + _rowHeight,
_rowHeight,
0,
_rows.size());
p.translate(0, from * _rowHeight);
for (auto i = from; i < to; ++i) {
@@ -1944,9 +1940,8 @@ void StickersBox::Inner::updateSelected() {
const auto now = crl::now();
const auto firstSetIndex = _rows.front()->isRecentSet() ? 1 : 0;
if (_dragStart.y() > local.y() && _dragging > 0) {
shift = -floorclamp(
_dragStart.y() - local.y() + (_rowHeight / 2),
_rowHeight,
shift = -std::clamp(
(_dragStart.y() - local.y() + (_rowHeight / 2)) / _rowHeight,
0,
_dragging - firstSetIndex);
const auto to = _dragging + shift;
@@ -1958,11 +1953,10 @@ void StickersBox::Inner::updateSelected() {
}
} else if (_dragStart.y() < local.y()
&& _dragging + 1 < _rows.size()) {
shift = floorclamp(
local.y() - _dragStart.y() + (_rowHeight / 2),
_rowHeight,
shift = std::clamp(
(local.y() - _dragStart.y() + (_rowHeight / 2)) / _rowHeight,
0,
_rows.size() - _dragging - 1);
int(_rows.size()) - _dragging - 1);
const auto to = _dragging + shift;
for (auto from = _dragging; from < to; ++from) {
std::swap(_rows[from], _rows[from + 1]);
@@ -2003,11 +1997,10 @@ void StickersBox::Inner::updateSelected() {
auto actionSel = -1;
auto inDragArea = false;
if (in && !_rows.empty()) {
const auto selectedIndex = floorclamp(
local.y() - _itemsTop,
_rowHeight,
const auto selectedIndex = std::clamp(
(local.y() - _itemsTop) / _rowHeight,
0,
_rows.size() - 1);
int(_rows.size()) - 1);
selected = selectedIndex;
local.setY(local.y() - _itemsTop - selectedIndex * _rowHeight);
const auto row = _rows[selectedIndex].get();
@@ -2860,15 +2853,10 @@ void StickersBox::Inner::checkLoadMore() {
void StickersBox::Inner::readVisibleSets() {
const auto itemsVisibleTop = _visibleTop - _itemsTop;
const auto itemsVisibleBottom = _visibleBottom - _itemsTop;
const auto rowFrom = floorclamp(
const auto [rowFrom, rowTo] = Ui::RowsInRange(
itemsVisibleTop,
_rowHeight,
0,
_rows.size());
const auto rowTo = ceilclamp(
itemsVisibleBottom,
_rowHeight,
0,
_rows.size());
for (auto i = rowFrom; i < rowTo; ++i) {
const auto row = _rows[i].get();

View File

@@ -2370,15 +2370,10 @@ void EmojiListWidget::paint(
_paintAsPremium = session().premium();
auto fromColumn = floorclamp(
auto [fromColumn, toColumn] = Ui::RowsInRange(
clip.x() - _rowsLeft,
_singleSize.width(),
0,
_columnCount);
auto toColumn = ceilclamp(
clip.x() + clip.width() - _rowsLeft,
_singleSize.width(),
0,
_columnCount);
if (rtl()) {
std::swap(fromColumn, toColumn);
@@ -2501,15 +2496,10 @@ void EmojiListWidget::paint(
}
if (clip.top() + clip.height() > info.rowsTop) {
ensureLoaded(info.section);
auto fromRow = floorclamp(
const auto [fromRow, toRow] = Ui::RowsInRange(
clip.y() - info.rowsTop,
_singleSize.height(),
0,
info.rowsCount);
auto toRow = ceilclamp(
clip.y() + clip.height() - info.rowsTop,
_singleSize.height(),
0,
info.rowsCount);
for (auto i = fromRow; i < toRow; ++i) {
for (auto j = fromColumn; j < toColumn; ++j) {

View File

@@ -1145,25 +1145,15 @@ void FieldAutocomplete::Inner::paintStickers(Painter &p, QRect clip) {
const auto padding = st::stickerPanPadding;
const auto &single = st::stickerPanSize;
const auto rows = rowscount(_srows->size(), _stickersPerRow);
const auto fromRow = floorclamp(
const auto [fromRow, tillRow] = Ui::RowsInRange(
clip.y() - padding,
single.height(),
0,
rows);
const auto tillRow = ceilclamp(
clip.y() + clip.height() - padding,
single.height(),
0,
rows);
const auto fromColumn = floorclamp(
const auto [fromColumn, tillColumn] = Ui::RowsInRange(
clip.x() - padding,
single.width(),
0,
_stickersPerRow);
const auto tillColumn = ceilclamp(
clip.x() + clip.width() - padding,
single.width(),
0,
_stickersPerRow);
for (auto row = fromRow; row != tillRow; ++row) {
for (auto column = fromColumn; column != tillColumn; ++column) {

View File

@@ -408,15 +408,10 @@ void StickersListWidget::checkVisibleFeatured(
}
const auto rowHeight = featuredRowHeight();
const auto destroyAbove = floorclamp(
const auto [destroyAbove, destroyBelow] = Ui::RowsInRange(
visibleTop - visibleHeight,
rowHeight,
0,
_officialSets.size());
const auto destroyBelow = ceilclamp(
visibleBottom + visibleHeight,
rowHeight,
0,
_officialSets.size());
for (auto i = 0; i != destroyAbove; ++i) {
clearHeavyIn(_officialSets[i]);
@@ -464,15 +459,10 @@ void StickersListWidget::readVisibleFeatured(
int visibleTop,
int visibleBottom) {
const auto rowHeight = featuredRowHeight();
const auto rowFrom = floorclamp(
const auto [rowFrom, rowTo] = Ui::RowsInRange(
visibleTop,
rowHeight,
0,
_featuredSetsCount);
const auto rowTo = ceilclamp(
visibleBottom,
rowHeight,
0,
_featuredSetsCount);
for (auto i = rowFrom; i < rowTo; ++i) {
const auto &set = _officialSets[i];
@@ -1607,15 +1597,10 @@ void StickersListWidget::paintSearchShortcutIcon(
}
void StickersListWidget::paintStickers(Painter &p, QRect clip) {
auto fromColumn = floorclamp(
auto [fromColumn, toColumn] = Ui::RowsInRange(
clip.x() - stickersLeft(),
_singleSize.width(),
0,
_columnCount);
auto toColumn = ceilclamp(
clip.x() + clip.width() - stickersLeft(),
_singleSize.width(),
0,
_columnCount);
if (rtl()) {
std::swap(fromColumn, toColumn);
@@ -1884,15 +1869,10 @@ void StickersListWidget::paintStickers(Painter &p, QRect clip) {
paintMegagroupEmptySet(p, info.rowsTop, buttonSelected);
return true;
}
const auto fromRow = floorclamp(
const auto [fromRow, toRow] = Ui::RowsInRange(
clip.y() - info.rowsTop,
_singleSize.height(),
0,
info.rowsCount);
const auto toRow = ceilclamp(
clip.y() + clip.height() - info.rowsTop,
_singleSize.height(),
0,
info.rowsCount);
for (auto i = fromRow; i < toRow; ++i) {
for (auto j = fromColumn; j < toColumn; ++j) {

View File

@@ -125,21 +125,3 @@ QString rusKeyboardLayoutSwitch(const QString &from);
inline int rowscount(int fullCount, int countPerRow) {
return (fullCount + countPerRow - 1) / countPerRow;
}
inline int floorclamp(int value, int step, int lowest, int highest) {
return std::clamp(value / step, lowest, highest);
}
inline int floorclamp(float64 value, int step, int lowest, int 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 std::clamp((value + step - 1) / step, lowest, highest);
}
inline int ceilclamp(float64 value, int32 step, int32 lowest, int32 highest) {
return std::clamp(
static_cast<int>(std::ceil(value / step)),
lowest,
highest);
}

View File

@@ -1397,8 +1397,11 @@ void InnerWidget::paintEvent(QPaintEvent *e) {
}
if (!_hashtagResults.empty()) {
const auto skip = hashtagsOffset();
auto from = floorclamp(r.y() - skip, st::mentionHeight, 0, _hashtagResults.size());
auto to = ceilclamp(r.y() + r.height() - skip, st::mentionHeight, 0, _hashtagResults.size());
auto [from, to] = Ui::RowsInRange(
r.y() - skip,
r.y() + r.height() - skip,
st::mentionHeight,
_hashtagResults.size());
p.translate(0, from * st::mentionHeight);
if (from < _hashtagResults.size()) {
const auto htagleft = st::defaultDialogRow.padding.left();
@@ -1472,8 +1475,11 @@ void InnerWidget::paintEvent(QPaintEvent *e) {
p.translate(0, st::searchedBarHeight);
auto skip = peerSearchOffset();
auto from = floorclamp(r.y() - skip, st::dialogsRowHeight, 0, _peerSearchResults.size());
auto to = ceilclamp(r.y() + r.height() - skip, st::dialogsRowHeight, 0, _peerSearchResults.size());
auto [from, to] = Ui::RowsInRange(
r.y() - skip,
r.y() + r.height() - skip,
st::dialogsRowHeight,
_peerSearchResults.size());
p.translate(0, from * st::dialogsRowHeight);
if (from < _peerSearchResults.size()) {
const auto activePeer = activeEntry.key.peer();
@@ -1560,8 +1566,11 @@ void InnerWidget::paintEvent(QPaintEvent *e) {
p.translate(0, st::searchedBarHeight);
}
auto skip = previewOffset();
auto from = floorclamp(r.y() - skip, _st->height, 0, _previewResults.size());
auto to = ceilclamp(r.y() + r.height() - skip, _st->height, 0, _previewResults.size());
auto [from, to] = Ui::RowsInRange(
r.y() - skip,
r.y() + r.height() - skip,
_st->height,
_previewResults.size());
p.translate(0, from * _st->height);
if (from < _previewResults.size()) {
const auto searchLowerText = (_searchHashOrCashtag == HashOrCashtag::None)
@@ -1644,8 +1653,11 @@ void InnerWidget::paintEvent(QPaintEvent *e) {
p.translate(0, st::searchedBarHeight);
auto skip = searchedOffset();
auto from = floorclamp(r.y() - skip, _st->height, 0, _searchResults.size());
auto to = ceilclamp(r.y() + r.height() - skip, _st->height, 0, _searchResults.size());
auto [from, to] = Ui::RowsInRange(
r.y() - skip,
r.y() + r.height() - skip,
_st->height,
_searchResults.size());
p.translate(0, from * _st->height);
if (from < _searchResults.size()) {
for (; from < to; ++from) {

View File

@@ -176,9 +176,8 @@ ListFoundItem ListSection::findItemByPoint(QPoint point) const {
auto item = *itemIt;
auto rect = findItemRect(item);
if (point.y() >= rect.top()) {
auto shift = floorclamp(
point.x(),
(_itemWidth + st::infoMediaSkip),
auto shift = std::clamp(
point.x() / (_itemWidth + st::infoMediaSkip),
0,
_itemsInRow);
while (shift-- && itemIt != _items.end()) {

View File

@@ -17,6 +17,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "ui/widgets/scroll_area.h"
#include "ui/painter.h"
#include "ui/screen_reader_mode.h"
#include "ui/ui_utility.h"
#include "styles/style_country_select_box.h"
#include "styles/style_intro.h"
#include "styles/style_layers.h"
@@ -366,8 +367,11 @@ void CountrySelectBox::Inner::paintEvent(QPaintEvent *e) {
if (r.intersects(QRect(0, 0, width(), st::countriesSkip))) {
p.fillRect(r.intersected(QRect(0, 0, width(), st::countriesSkip)), st::countryRowBg);
}
int32 from = std::clamp((r.y() - st::countriesSkip) / _rowHeight, 0, l);
int32 to = std::clamp((r.y() + r.height() - st::countriesSkip + _rowHeight - 1) / _rowHeight, 0, l);
const auto [from, to] = RowsInRange(
r.y() - st::countriesSkip,
r.y() + r.height() - st::countriesSkip,
_rowHeight,
l);
for (int32 i = from; i < to; ++i) {
auto selected = (i == (_pressed >= 0 ? _pressed : _selected));
auto y = st::countriesSkip + i * _rowHeight;

View File

@@ -8,6 +8,7 @@ https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
#include "ui/controls/peer_list_dummy.h"
#include "ui/painter.h"
#include "ui/ui_utility.h"
#include "styles/style_widgets.h"
PeerListDummy::PeerListDummy(
@@ -25,11 +26,10 @@ void PeerListDummy::paintEvent(QPaintEvent *e) {
PainterHighQualityEnabler hq(p);
const auto fill = e->rect();
const auto bottom = fill.top() + fill.height();
const auto from = std::clamp(fill.top() / _st.item.height, 0, _count);
const auto till = std::clamp(
(bottom + _st.item.height - 1) / _st.item.height,
0,
const auto [from, till] = Ui::RowsInRange(
fill.top(),
fill.top() + fill.height(),
_st.item.height,
_count);
p.translate(0, _st.item.height * from);
p.setPen(Qt::NoPen);

View File

@@ -533,7 +533,10 @@ void Editor::Inner::selectSkipPage(int delta, int direction) {
+ st::themeEditorDescriptionSkip
+ st::defaultTextStyle.font->height
+ st::themeEditorMargin.bottom();
for (auto i = 0, count = ceilclamp(delta, defaultRowHeight, 1, delta); i != count; ++i) {
const auto count = std::max(
(delta + defaultRowHeight - 1) / defaultRowHeight,
1);
for (auto i = 0; i != count; ++i) {
selectSkip(direction);
}
}