2017-09-13 16:57:44 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2017-09-13 16:57:44 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2017-09-13 16:57:44 +00:00
|
|
|
*/
|
|
|
|
#include "ui/wrap/vertical_layout.h"
|
|
|
|
|
|
|
|
namespace Ui {
|
|
|
|
|
|
|
|
QMargins VerticalLayout::getMargins() const {
|
|
|
|
auto result = QMargins();
|
|
|
|
if (!_rows.empty()) {
|
|
|
|
auto &top = _rows.front();
|
|
|
|
auto topMargin = top.widget->getMargins().top();
|
|
|
|
result.setTop(
|
|
|
|
qMax(topMargin - top.margin.top(), 0));
|
|
|
|
auto &bottom = _rows.back();
|
|
|
|
auto bottomMargin = bottom.widget->getMargins().bottom();
|
|
|
|
result.setBottom(
|
|
|
|
qMax(bottomMargin - bottom.margin.bottom(), 0));
|
|
|
|
for (auto &row : _rows) {
|
|
|
|
auto margins = row.widget->getMargins();
|
|
|
|
result.setLeft(qMax(
|
|
|
|
margins.left() - row.margin.left(),
|
|
|
|
result.left()));
|
|
|
|
result.setRight(qMax(
|
|
|
|
margins.right() - row.margin.right(),
|
|
|
|
result.right()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
int VerticalLayout::naturalWidth() const {
|
|
|
|
auto result = 0;
|
|
|
|
for (auto &row : _rows) {
|
|
|
|
auto natural = row.widget->naturalWidth();
|
|
|
|
if (natural < 0) {
|
|
|
|
return natural;
|
|
|
|
}
|
|
|
|
accumulate_max(result, natural);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
int VerticalLayout::resizeGetHeight(int newWidth) {
|
2017-11-16 07:45:55 +00:00
|
|
|
_inResize = true;
|
|
|
|
auto guard = gsl::finally([&] { _inResize = false; });
|
|
|
|
|
2017-09-13 16:57:44 +00:00
|
|
|
auto margins = getMargins();
|
|
|
|
auto result = 0;
|
|
|
|
for (auto &row : _rows) {
|
|
|
|
updateChildGeometry(
|
|
|
|
margins,
|
|
|
|
row.widget,
|
|
|
|
row.margin,
|
|
|
|
newWidth,
|
|
|
|
result);
|
|
|
|
result += row.margin.top()
|
|
|
|
+ row.widget->heightNoMargins()
|
|
|
|
+ row.margin.bottom();
|
|
|
|
}
|
2018-09-07 12:19:56 +00:00
|
|
|
return result;
|
2017-09-13 16:57:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void VerticalLayout::visibleTopBottomUpdated(
|
|
|
|
int visibleTop,
|
|
|
|
int visibleBottom) {
|
|
|
|
for (auto &row : _rows) {
|
|
|
|
setChildVisibleTopBottom(
|
|
|
|
row.widget,
|
|
|
|
visibleTop,
|
|
|
|
visibleBottom);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void VerticalLayout::updateChildGeometry(
|
|
|
|
const style::margins &margins,
|
|
|
|
RpWidget *child,
|
|
|
|
const style::margins &margin,
|
|
|
|
int width,
|
|
|
|
int top) const {
|
|
|
|
auto availRowWidth = width
|
|
|
|
- margin.left()
|
|
|
|
- margin.right();
|
|
|
|
child->resizeToNaturalWidth(availRowWidth);
|
|
|
|
child->moveToLeft(
|
|
|
|
margins.left() + margin.left(),
|
|
|
|
margins.top() + margin.top() + top,
|
|
|
|
width);
|
|
|
|
}
|
|
|
|
|
2018-04-28 14:06:59 +00:00
|
|
|
RpWidget *VerticalLayout::insertChild(
|
|
|
|
int atPosition,
|
2017-09-13 16:57:44 +00:00
|
|
|
object_ptr<RpWidget> child,
|
|
|
|
const style::margins &margin) {
|
2018-04-28 14:06:59 +00:00
|
|
|
Expects(atPosition >= 0 && atPosition <= _rows.size());
|
|
|
|
|
|
|
|
if (const auto weak = AttachParentChild(this, child)) {
|
|
|
|
_rows.insert(
|
|
|
|
begin(_rows) + atPosition,
|
|
|
|
{ std::move(child), margin });
|
|
|
|
const auto margins = getMargins();
|
2017-09-13 16:57:44 +00:00
|
|
|
updateChildGeometry(
|
|
|
|
margins,
|
|
|
|
weak,
|
|
|
|
margin,
|
|
|
|
width() - margins.left() - margins.right(),
|
|
|
|
height() - margins.top() - margins.bottom());
|
2017-12-22 07:05:20 +00:00
|
|
|
weak->heightValue(
|
2018-04-28 14:06:59 +00:00
|
|
|
) | rpl::start_with_next_done([=] {
|
2017-12-22 07:05:20 +00:00
|
|
|
if (!_inResize) {
|
|
|
|
childHeightUpdated(weak);
|
|
|
|
}
|
2018-04-28 14:06:59 +00:00
|
|
|
}, [=] {
|
2017-12-22 07:05:20 +00:00
|
|
|
removeChild(weak);
|
|
|
|
}, lifetime());
|
2017-09-13 16:57:44 +00:00
|
|
|
return weak;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VerticalLayout::childHeightUpdated(RpWidget *child) {
|
2017-11-20 12:23:20 +00:00
|
|
|
auto it = ranges::find_if(_rows, [child](const Row &row) {
|
2017-09-13 16:57:44 +00:00
|
|
|
return (row.widget == child);
|
|
|
|
});
|
|
|
|
|
|
|
|
auto margins = getMargins();
|
|
|
|
auto top = [&] {
|
|
|
|
if (it == _rows.begin()) {
|
|
|
|
return margins.top();
|
|
|
|
}
|
|
|
|
auto prev = it - 1;
|
|
|
|
return prev->widget->bottomNoMargins() + prev->margin.bottom();
|
|
|
|
}() - margins.top();
|
|
|
|
for (auto end = _rows.end(); it != end; ++it) {
|
|
|
|
auto &row = *it;
|
|
|
|
auto margin = row.margin;
|
|
|
|
auto widget = row.widget.data();
|
|
|
|
widget->moveToLeft(
|
|
|
|
margins.left() + margin.left(),
|
|
|
|
margins.top() + top + margin.top());
|
|
|
|
top += margin.top()
|
|
|
|
+ widget->heightNoMargins()
|
|
|
|
+ margin.bottom();
|
|
|
|
}
|
|
|
|
resize(width(), margins.top() + top + margins.bottom());
|
|
|
|
}
|
|
|
|
|
|
|
|
void VerticalLayout::removeChild(RpWidget *child) {
|
2017-11-20 12:23:20 +00:00
|
|
|
auto it = ranges::find_if(_rows, [child](const Row &row) {
|
2017-09-13 16:57:44 +00:00
|
|
|
return (row.widget == child);
|
|
|
|
});
|
|
|
|
auto end = _rows.end();
|
|
|
|
Assert(it != end);
|
|
|
|
|
|
|
|
auto margins = getMargins();
|
|
|
|
auto top = [&] {
|
|
|
|
if (it == _rows.begin()) {
|
|
|
|
return margins.top();
|
|
|
|
}
|
|
|
|
auto prev = it - 1;
|
|
|
|
return prev->widget->bottomNoMargins() + prev->margin.bottom();
|
|
|
|
}() - margins.top();
|
2018-04-28 15:58:22 +00:00
|
|
|
for (auto next = it + 1; next != end; ++next) {
|
|
|
|
auto &row = *next;
|
2017-09-13 16:57:44 +00:00
|
|
|
auto margin = row.margin;
|
|
|
|
auto widget = row.widget.data();
|
|
|
|
widget->moveToLeft(
|
|
|
|
margins.left() + margin.left(),
|
|
|
|
margins.top() + top + margin.top());
|
|
|
|
top += margin.top()
|
|
|
|
+ widget->heightNoMargins()
|
|
|
|
+ margin.bottom();
|
|
|
|
}
|
2018-04-28 15:58:22 +00:00
|
|
|
it->widget = nullptr;
|
2017-09-13 16:57:44 +00:00
|
|
|
_rows.erase(it);
|
|
|
|
|
|
|
|
resize(width(), margins.top() + top + margins.bottom());
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Ui
|