tdesktop/Telegram/SourceFiles/ui/wrap/padding_wrap.cpp

70 lines
1.7 KiB
C++
Raw Normal View History

2017-09-13 19:57:44 +03:00
/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
2017-09-13 19:57:44 +03:00
For license and copyright information please follow this link:
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
2017-09-13 19:57:44 +03:00
*/
#include "ui/wrap/padding_wrap.h"
namespace Ui {
PaddingWrap<RpWidget>::PaddingWrap(
QWidget *parent,
object_ptr<RpWidget> &&child,
2017-09-13 19:57:44 +03:00
const style::margins &padding)
: Parent(parent, std::move(child)) {
setPadding(padding);
}
void PaddingWrap<RpWidget>::setPadding(const style::margins &padding) {
if (_padding != padding) {
auto oldWidth = width() - _padding.left() - _padding.top();
_padding = padding;
2017-09-13 19:57:44 +03:00
if (auto weak = wrapped()) {
wrappedSizeUpdated(weak->size());
auto margins = weak->getMargins();
weak->moveToLeft(
_padding.left() + margins.left(),
_padding.top() + margins.top());
} else {
resize(QSize(
_padding.left() + oldWidth + _padding.right(),
_padding.top() + _padding.bottom()));
}
2017-09-13 19:57:44 +03:00
}
}
void PaddingWrap<RpWidget>::wrappedSizeUpdated(QSize size) {
resize(QRect(QPoint(), size).marginsAdded(_padding).size());
}
int PaddingWrap<RpWidget>::naturalWidth() const {
auto inner = [this] {
if (auto weak = wrapped()) {
return weak->naturalWidth();
}
return RpWidget::naturalWidth();
}();
return (inner < 0)
? inner
: (_padding.left() + inner + _padding.right());
}
int PaddingWrap<RpWidget>::resizeGetHeight(int newWidth) {
if (auto weak = wrapped()) {
weak->resizeToWidth(newWidth
- _padding.left()
- _padding.right());
} else {
resize(QSize(
_padding.left() + newWidth + _padding.right(),
_padding.top() + _padding.bottom()));
}
return heightNoMargins();
}
} // namespace Ui