1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
|
/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
*/
#include "layout/layout_mosaic.h"
#include "styles/style_basic.h"
namespace Mosaic::Layout {
AbstractMosaicLayout::AbstractMosaicLayout(int bigWidth)
: _bigWidth(bigWidth) {
}
int AbstractMosaicLayout::rowHeightAt(int row) const {
Expects(row >= 0 && row < _rows.size());
return _rows[row].height;
}
int AbstractMosaicLayout::countDesiredHeight(int newWidth) {
auto result = 0;
for (auto &row : _rows) {
layoutRow(row, newWidth ? newWidth : _width);
result += row.height;
}
return _padding.top() + result + _padding.bottom();
}
FoundItem AbstractMosaicLayout::findByPoint(const QPoint &globalPoint) const {
auto sx = globalPoint.x() - _padding.left();
auto sy = globalPoint.y() - _padding.top();
auto row = -1;
auto col = -1;
auto sel = -1;
bool exact = true;
if (sy >= 0) {
row = 0;
for (auto rows = rowsCount(); row < rows; ++row) {
const auto rowHeight = _rows[row].height;
if (sy < rowHeight) {
break;
}
sy -= rowHeight;
}
} else {
row = 0;
exact = false;
}
if (row >= rowsCount()) {
row = rowsCount() - 1;
exact = false;
}
if (sx < 0) {
sx = 0;
exact = false;
}
if (sx >= 0 && row >= 0 && row < rowsCount()) {
const auto columnsCount = _rows[row].items.size();
col = 0;
for (int cols = columnsCount; col < cols; ++col) {
const auto item = itemAt(row, col);
const auto width = item->width();
if (sx < width) {
break;
}
sx -= width;
sx -= _rightSkip;
}
if (col >= columnsCount) {
col = columnsCount - 1;
exact = false;
}
sel = ::Layout::PositionToIndex(row, + col);
} else {
row = col = -1;
}
return { sel, exact, QPoint(sx, sy) };
}
QRect AbstractMosaicLayout::findRect(int index) const {
const auto clip = QRect(0, 0, _width, 100);
const auto fromX = style::RightToLeft()
? (_width - clip.x() - clip.width())
: clip.x();
const auto toX = style::RightToLeft()
? (_width - clip.x())
: (clip.x() + clip.width());
const auto rows = _rows.size();
auto top = 0;
for (auto row = 0; row != rows; ++row) {
auto &inlineRow = _rows[row];
// if ((top + inlineRow.height) > clip.top()) {
auto left = 0;
if (row == (rows - 1)) {
// context.lastRow = true;
}
for (const auto &item : inlineRow.items) {
if (left >= toX) {
break;
}
const auto w = item->width();
if ((left + w) > fromX) {
if (item->position() == index) {
return QRect(
left + _padding.left(),
top + _padding.top(),
item->width(),
item->height());
}
}
left += w;
left += _rightSkip;
}
// }
top += inlineRow.height;
}
return QRect();
}
void AbstractMosaicLayout::addItems(
gsl::span<const not_null<AbstractLayoutItem*>> items) {
_rows.reserve(items.size());
auto row = Row();
row.items.reserve(kInlineItemsMaxPerRow);
auto sumWidth = 0;
for (const auto &item : items) {
addItem(item, row, sumWidth);
}
rowFinalize(row, sumWidth, true);
}
void AbstractMosaicLayout::setRightSkip(int rightSkip) {
_rightSkip = rightSkip;
}
void AbstractMosaicLayout::setPadding(QMargins padding) {
_padding = padding;
}
void AbstractMosaicLayout::setFullWidth(int w) {
_width = w;
}
bool AbstractMosaicLayout::empty() const {
return _rows.empty();
}
int AbstractMosaicLayout::rowsCount() const {
return _rows.size();
}
not_null<AbstractLayoutItem*> AbstractMosaicLayout::itemAt(
int row,
int column) const {
Expects((row >= 0)
&& (row < _rows.size())
&& (column >= 0)
&& (column < _rows[row].items.size()));
return _rows[row].items[column];
}
not_null<AbstractLayoutItem*> AbstractMosaicLayout::itemAt(int index) const {
const auto &[row, column] = ::Layout::IndexToPosition(index);
return itemAt(row, column);
}
AbstractLayoutItem *AbstractMosaicLayout::maybeItemAt(
int row,
int column) const {
if ((row >= 0)
&& (row < _rows.size())
&& (column >= 0)
&& (column < _rows[row].items.size())) {
return _rows[row].items[column];
}
return nullptr;
}
AbstractLayoutItem *AbstractMosaicLayout::maybeItemAt(int index) const {
const auto &[row, column] = ::Layout::IndexToPosition(index);
return maybeItemAt(row, column);
}
void AbstractMosaicLayout::clearRows(bool resultsDeleted) {
if (!resultsDeleted) {
for (const auto &row : _rows) {
for (const auto &item : row.items) {
item->setPosition(-1);
}
}
}
_rows.clear();
}
void AbstractMosaicLayout::forEach(
Fn<void(not_null<const AbstractLayoutItem*>)> callback) {
for (const auto &row : _rows) {
for (const auto &item : row.items) {
callback(item);
}
}
}
void AbstractMosaicLayout::paint(
Fn<void(not_null<AbstractLayoutItem*>, QPoint)> paintItem,
const QRect &clip) const {
auto top = _padding.top();
const auto fromX = style::RightToLeft()
? (_width - clip.x() - clip.width())
: clip.x();
const auto toX = style::RightToLeft()
? (_width - clip.x())
: (clip.x() + clip.width());
const auto rows = _rows.size();
for (auto row = 0; row != rows; ++row) {
if (top >= clip.top() + clip.height()) {
break;
}
auto &inlineRow = _rows[row];
if ((top + inlineRow.height) > clip.top()) {
auto left = _padding.left();
if (row == (rows - 1)) {
// context.lastRow = true;
}
for (const auto &item : inlineRow.items) {
if (left >= toX) {
break;
}
const auto w = item->width();
if ((left + w) > fromX) {
paintItem(item, QPoint(left, top));
}
left += w;
left += _rightSkip;
}
}
top += inlineRow.height;
}
}
int AbstractMosaicLayout::validateExistingRows(
Fn<bool(not_null<const AbstractLayoutItem*>, int)> checkItem,
int count) {
auto until = 0;
auto untilRow = 0;
auto untilCol = 0;
while (until < count) {
if ((untilRow >= _rows.size())
|| checkItem(_rows[untilRow].items[untilCol], until)) {
break;
}
++until;
if (++untilCol == _rows[untilRow].items.size()) {
++untilRow;
untilCol = 0;
}
}
if (until == count) { // All items are layed out.
if (untilRow == _rows.size()) { // Nothing changed.
return until;
}
{
const auto rows = _rows.size();
auto skip = untilCol;
for (auto i = untilRow; i < rows; ++i) {
for (const auto &item : _rows[i].items) {
if (skip) {
--skip;
} else {
item->setPosition(-1);
}
}
}
}
if (!untilCol) { // All good rows are filled.
_rows.resize(untilRow);
return until;
}
_rows.resize(untilRow + 1);
_rows[untilRow].items.resize(untilCol);
_rows[untilRow].maxWidth = ranges::accumulate(
_rows[untilRow].items,
0,
[](int w, auto &row) { return w + row->maxWidth(); });
layoutRow(_rows[untilRow], _width);
return until;
}
if (untilRow && !untilCol) { // Remove last row, maybe it is not full.
--untilRow;
untilCol = _rows[untilRow].items.size();
}
until -= untilCol;
for (auto i = untilRow; i < _rows.size(); ++i) {
for (const auto &item : _rows[i].items) {
item->setPosition(-1);
}
}
_rows.resize(untilRow);
return until;
}
void AbstractMosaicLayout::addItem(
not_null<AbstractLayoutItem*> item,
Row &row,
int &sumWidth) {
// item->preload();
using namespace ::Layout;
item->setPosition(PositionToIndex(_rows.size(), row.items.size()));
if (rowFinalize(row, sumWidth, false)) {
item->setPosition(PositionToIndex(_rows.size(), 0));
}
sumWidth += item->maxWidth();
if (!row.items.empty() && _rightSkip) {
sumWidth += _rightSkip;
}
row.items.push_back(item);
}
bool AbstractMosaicLayout::rowFinalize(Row &row, int &sumWidth, bool force) {
if (row.items.empty()) {
return false;
}
const auto full = (row.items.size() >= kInlineItemsMaxPerRow);
// Currently use the same GIFs layout for all widget sizes.
const auto big = (sumWidth >= _bigWidth);
if (full || big || force) {
row.maxWidth = (full || big) ? sumWidth : 0;
layoutRow(row, _width);
_rows.push_back(std::move(row));
row = Row();
row.items.reserve(kInlineItemsMaxPerRow);
sumWidth = 0;
return true;
}
return false;
}
void AbstractMosaicLayout::layoutRow(Row &row, int fullWidth) {
const auto count = int(row.items.size());
Assert(count <= kInlineItemsMaxPerRow);
// Enumerate items in the order of growing maxWidth()
// for that sort item indices by maxWidth().
int indices[kInlineItemsMaxPerRow];
for (auto i = 0; i != count; ++i) {
indices[i] = i;
}
std::sort(indices, indices + count, [&](int a, int b) {
return row.items[a]->maxWidth() < row.items[b]->maxWidth();
});
auto desiredWidth = row.maxWidth;
row.height = 0;
auto availableWidth = fullWidth - _padding.left() - _padding.right();
for (auto i = 0; i < count; ++i) {
const auto index = indices[i];
const auto &item = row.items[index];
const auto w = desiredWidth
? (item->maxWidth() * availableWidth / desiredWidth)
: item->maxWidth();
const auto actualWidth = std::max(w, st::inlineResultsMinWidth);
row.height = std::max(
row.height,
item->resizeGetHeight(actualWidth));
if (desiredWidth) {
availableWidth -= actualWidth;
desiredWidth -= row.items[index]->maxWidth();
if (index > 0 && _rightSkip) {
availableWidth -= _rightSkip;
desiredWidth -= _rightSkip;
}
}
}
}
} // namespace Mosaic::Layout
|