2021-07-26 00:10:54 +00:00
|
|
|
/*
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "layout/abstract_layout_item.h"
|
|
|
|
#include "layout/layout_position.h"
|
|
|
|
|
|
|
|
#include "styles/style_chat_helpers.h"
|
|
|
|
|
|
|
|
namespace Mosaic::Layout {
|
|
|
|
|
|
|
|
struct FoundItem {
|
|
|
|
int index = -1;
|
|
|
|
bool exact = false;
|
|
|
|
QPoint relative;
|
|
|
|
};
|
|
|
|
|
2021-07-28 14:00:46 +00:00
|
|
|
class AbstractMosaicLayout {
|
2021-07-26 00:10:54 +00:00
|
|
|
public:
|
2021-07-28 14:00:46 +00:00
|
|
|
AbstractMosaicLayout(int bigWidth);
|
2021-07-26 00:10:54 +00:00
|
|
|
|
2021-07-28 14:00:46 +00:00
|
|
|
[[nodiscard]] int rowHeightAt(int row) const;
|
|
|
|
[[nodiscard]] int countDesiredHeight(int newWidth);
|
2021-07-26 00:10:54 +00:00
|
|
|
|
2021-07-28 14:00:46 +00:00
|
|
|
[[nodiscard]] FoundItem findByPoint(const QPoint &globalPoint) const;
|
2021-07-26 00:10:54 +00:00
|
|
|
|
2021-07-28 14:00:46 +00:00
|
|
|
[[nodiscard]] QRect findRect(int index) const;
|
2021-07-26 00:10:54 +00:00
|
|
|
|
2021-07-28 14:00:46 +00:00
|
|
|
void setRightSkip(int rightSkip);
|
|
|
|
void setOffset(int left, int top);
|
|
|
|
void setFullWidth(int w);
|
2021-07-26 00:10:54 +00:00
|
|
|
|
2021-07-28 14:00:46 +00:00
|
|
|
[[nodiscard]] bool empty() const;
|
|
|
|
[[nodiscard]] int rowsCount() const;
|
2021-07-26 00:10:54 +00:00
|
|
|
|
2021-07-28 14:00:46 +00:00
|
|
|
void clearRows(bool resultsDeleted);
|
2021-07-26 00:10:54 +00:00
|
|
|
|
2021-07-28 14:00:46 +00:00
|
|
|
protected:
|
|
|
|
void addItems(gsl::span<const not_null<AbstractLayoutItem*>> items);
|
|
|
|
|
|
|
|
[[nodiscard]] not_null<AbstractLayoutItem*> itemAt(int row, int column) const;
|
|
|
|
[[nodiscard]] not_null<AbstractLayoutItem*> itemAt(int index) const;
|
|
|
|
|
|
|
|
[[nodiscard]] AbstractLayoutItem *maybeItemAt(int row, int column) const;
|
|
|
|
[[nodiscard]] AbstractLayoutItem *maybeItemAt(int index) const;
|
|
|
|
|
|
|
|
void forEach(Fn<void(not_null<const AbstractLayoutItem*>)> callback);
|
|
|
|
|
|
|
|
void paint(
|
|
|
|
Fn<void(not_null<AbstractLayoutItem*>, QPoint)> paintItem,
|
|
|
|
const QRect &clip) const;
|
|
|
|
int validateExistingRows(
|
|
|
|
Fn<bool(not_null<const AbstractLayoutItem*>, int)> checkItem,
|
|
|
|
int count);
|
|
|
|
|
|
|
|
private:
|
|
|
|
static constexpr auto kInlineItemsMaxPerRow = 5;
|
|
|
|
struct Row {
|
|
|
|
int maxWidth = 0;
|
|
|
|
int height = 0;
|
|
|
|
std::vector<AbstractLayoutItem*> items;
|
|
|
|
};
|
|
|
|
|
|
|
|
void addItem(not_null<AbstractLayoutItem*> item, Row &row, int &sumWidth);
|
|
|
|
bool rowFinalize(Row &row, int &sumWidth, bool force);
|
|
|
|
void layoutRow(Row &row, int fullWidth);
|
2021-07-26 00:10:54 +00:00
|
|
|
|
2021-07-28 14:00:46 +00:00
|
|
|
int _bigWidth;
|
|
|
|
int _width = 0;
|
|
|
|
int _rightSkip = 0;
|
|
|
|
QPoint _offset;
|
|
|
|
std::vector<Row> _rows;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
template <
|
|
|
|
typename ItemBase,
|
|
|
|
typename = std::enable_if_t<
|
|
|
|
std::is_base_of_v<AbstractLayoutItem, ItemBase>>>
|
|
|
|
class MosaicLayout final : public AbstractMosaicLayout {
|
|
|
|
using Parent = AbstractMosaicLayout;
|
|
|
|
|
|
|
|
public:
|
|
|
|
using Parent::Parent;
|
|
|
|
|
|
|
|
void addItems(const std::vector<not_null<ItemBase*>> &items) {
|
|
|
|
Parent::addItems({
|
|
|
|
reinterpret_cast<const not_null<AbstractLayoutItem*>*>(
|
|
|
|
items.data()),
|
|
|
|
items.size() });
|
2021-07-26 00:10:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]] not_null<ItemBase*> itemAt(int row, int column) const {
|
2021-07-28 14:00:46 +00:00
|
|
|
return Downcast(Parent::itemAt(row, column));
|
2021-07-26 00:10:54 +00:00
|
|
|
}
|
|
|
|
[[nodiscard]] not_null<ItemBase*> itemAt(int index) const {
|
2021-07-28 14:00:46 +00:00
|
|
|
return Downcast(Parent::itemAt(index));
|
2021-07-26 00:10:54 +00:00
|
|
|
}
|
|
|
|
[[nodiscard]] ItemBase *maybeItemAt(int row, int column) const {
|
2021-07-28 14:00:46 +00:00
|
|
|
return Downcast(Parent::maybeItemAt(row, column));
|
2021-07-26 00:10:54 +00:00
|
|
|
}
|
|
|
|
[[nodiscard]] ItemBase *maybeItemAt(int index) const {
|
2021-07-28 14:00:46 +00:00
|
|
|
return Downcast(Parent::maybeItemAt(index));
|
2021-07-26 00:10:54 +00:00
|
|
|
}
|
|
|
|
|
2021-07-28 14:00:46 +00:00
|
|
|
void forEach(Fn<void(not_null<const ItemBase*>)> callback) {
|
|
|
|
Parent::forEach([&](
|
|
|
|
not_null<const AbstractLayoutItem*> item) {
|
|
|
|
callback(Downcast(item));
|
|
|
|
});
|
2021-07-26 00:10:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void paint(
|
2021-07-28 14:00:46 +00:00
|
|
|
Fn<void(not_null<ItemBase*>, QPoint)> paintItem,
|
2021-07-26 00:10:54 +00:00
|
|
|
const QRect &clip) const {
|
2021-07-28 14:00:46 +00:00
|
|
|
Parent::paint([&](
|
|
|
|
not_null<AbstractLayoutItem*> item,
|
|
|
|
QPoint point) {
|
|
|
|
paintItem(Downcast(item), point);
|
|
|
|
}, clip);
|
2021-07-26 00:10:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int validateExistingRows(
|
2021-07-28 14:00:46 +00:00
|
|
|
Fn<bool(not_null<const ItemBase*>, int)> checkItem,
|
2021-07-26 00:10:54 +00:00
|
|
|
int count) {
|
2021-07-28 14:00:46 +00:00
|
|
|
return Parent::validateExistingRows([&](
|
|
|
|
not_null<const AbstractLayoutItem*> item,
|
|
|
|
int until) {
|
|
|
|
return checkItem(Downcast(item), until);
|
|
|
|
}, count);
|
2021-07-26 00:10:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-07-28 14:00:46 +00:00
|
|
|
[[nodiscard]] static not_null<ItemBase*> Downcast(
|
|
|
|
not_null<AbstractLayoutItem*> item) {
|
|
|
|
return static_cast<ItemBase*>(item.get());
|
2021-07-26 00:10:54 +00:00
|
|
|
}
|
2021-07-28 14:00:46 +00:00
|
|
|
[[nodiscard]] static ItemBase *Downcast(
|
|
|
|
AbstractLayoutItem *item) {
|
|
|
|
return static_cast<ItemBase*>(item);
|
2021-07-26 00:10:54 +00:00
|
|
|
}
|
2021-07-28 14:00:46 +00:00
|
|
|
[[nodiscard]] static not_null<const ItemBase*> Downcast(
|
|
|
|
not_null<const AbstractLayoutItem*> item) {
|
|
|
|
return static_cast<const ItemBase*>(item.get());
|
|
|
|
}
|
|
|
|
[[nodiscard]] static const ItemBase *Downcast(
|
|
|
|
const AbstractLayoutItem *item) {
|
|
|
|
return static_cast<const ItemBase*>(item);
|
2021-07-26 00:10:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace Mosaic::Layout
|