1 // This file is part of Desktop App Toolkit,
2 // a set of libraries for developing nice desktop applications.
3 //
4 // For license and copyright information please follow this link:
5 // https://github.com/desktop-app/legal/blob/master/LEGAL
6 //
7 #include "ui/wrap/padding_wrap.h"
8 
9 namespace Ui {
10 
PaddingWrap(QWidget * parent,object_ptr<RpWidget> && child,const style::margins & padding)11 PaddingWrap<RpWidget>::PaddingWrap(
12 	QWidget *parent,
13 	object_ptr<RpWidget> &&child,
14 	const style::margins &padding)
15 : Parent(parent, std::move(child)) {
16 	setPadding(padding);
17 }
18 
setPadding(const style::margins & padding)19 void PaddingWrap<RpWidget>::setPadding(const style::margins &padding) {
20 	if (_padding != padding) {
21 		auto oldWidth = width() - _padding.left() - _padding.top();
22 		_padding = padding;
23 
24 		if (auto weak = wrapped()) {
25 			wrappedSizeUpdated(weak->size());
26 
27 			auto margins = weak->getMargins();
28 			weak->moveToLeft(
29 				_padding.left() + margins.left(),
30 				_padding.top() + margins.top());
31 		} else {
32 			resize(QSize(
33 				_padding.left() + oldWidth + _padding.right(),
34 				_padding.top() + _padding.bottom()));
35 		}
36 	}
37 }
38 
wrappedSizeUpdated(QSize size)39 void PaddingWrap<RpWidget>::wrappedSizeUpdated(QSize size) {
40 	resize(QRect(QPoint(), size).marginsAdded(_padding).size());
41 }
42 
naturalWidth() const43 int PaddingWrap<RpWidget>::naturalWidth() const {
44 	auto inner = [this] {
45 		if (auto weak = wrapped()) {
46 			return weak->naturalWidth();
47 		}
48 		return RpWidget::naturalWidth();
49 	}();
50 	return (inner < 0)
51 		? inner
52 		: (_padding.left() + inner + _padding.right());
53 }
54 
resizeGetHeight(int newWidth)55 int PaddingWrap<RpWidget>::resizeGetHeight(int newWidth) {
56 	if (auto weak = wrapped()) {
57 		weak->resizeToWidth(newWidth
58 			- _padding.left()
59 			- _padding.right());
60 		SendPendingMoveResizeEvents(weak);
61 	} else {
62 		resize(QSize(
63 			_padding.left() + newWidth + _padding.right(),
64 			_padding.top() + _padding.bottom()));
65 	}
66 	return heightNoMargins();
67 }
68 
CenterWrap(QWidget * parent,object_ptr<RpWidget> && child)69 CenterWrap<RpWidget>::CenterWrap(
70 	QWidget *parent,
71 	object_ptr<RpWidget> &&child)
72 : Parent(parent, std::move(child)) {
73 	if (const auto weak = wrapped()) {
74 		wrappedSizeUpdated(weak->size());
75 	}
76 }
77 
naturalWidth() const78 int CenterWrap<RpWidget>::naturalWidth() const {
79 	return -1;
80 }
81 
resizeGetHeight(int newWidth)82 int CenterWrap<RpWidget>::resizeGetHeight(int newWidth) {
83 	updateWrappedPosition(newWidth);
84 	return heightNoMargins();
85 }
86 
wrappedSizeUpdated(QSize size)87 void CenterWrap<RpWidget>::wrappedSizeUpdated(QSize size) {
88 	updateWrappedPosition(width());
89 }
90 
updateWrappedPosition(int forWidth)91 void CenterWrap<RpWidget>::updateWrappedPosition(int forWidth) {
92 	if (const auto weak = wrapped()) {
93 		const auto margins = weak->getMargins();
94 		weak->moveToLeft(
95 			(forWidth - weak->width()) / 2 + margins.left(),
96 			margins.top());
97 	}
98 }
99 
100 } // namespace Ui
101