layout_mosaic.cpp 9.2 KB

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