tdesktop/Telegram/SourceFiles/ui/wrap/wrap.h

200 lines
4.1 KiB
C
Raw Normal View History

2017-09-13 16:57:44 +00:00
/*
This file is part of Telegram Desktop,
the official desktop application for the Telegram messaging service.
2017-09-13 16:57:44 +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
*/
#pragma once
#include "ui/rp_widget.h"
namespace Ui {
template <typename Widget, typename ParentType = RpWidget>
class Wrap;
namespace details {
struct UnwrapHelper {
2017-09-18 17:04:45 +00:00
template <
typename Widget,
typename = typename std::decay_t<Widget>::WrapParentType>
static std::true_type Is(Widget &&widget);
static std::false_type Is(...);
2017-09-13 16:57:44 +00:00
template <typename Entity>
static auto Unwrap(Entity *entity, std::true_type) {
return entity
? entity->entity()
: nullptr;
}
template <typename Entity>
static Entity *Unwrap(Entity *entity, std::false_type) {
return entity;
}
template <typename Entity>
static auto Unwrap(Entity *entity) {
2017-09-18 17:04:45 +00:00
using Selector = decltype(Is(std::declval<Entity>()));
2017-09-13 16:57:44 +00:00
return Unwrap(
entity,
2017-09-18 17:04:45 +00:00
Selector());
2017-09-13 16:57:44 +00:00
}
2017-09-18 17:04:45 +00:00
2017-09-13 16:57:44 +00:00
};
} // namespace details
template <typename Widget>
class Wrap<Widget, RpWidget> : public RpWidget {
public:
Wrap(QWidget *parent, object_ptr<Widget> &&child);
2017-09-13 16:57:44 +00:00
Widget *wrapped() {
return _wrapped;
}
const Widget *wrapped() const {
return _wrapped;
}
auto entity() {
return details::UnwrapHelper::Unwrap(wrapped());
}
auto entity() const {
return details::UnwrapHelper::Unwrap(wrapped());
}
QMargins getMargins() const override {
if (auto weak = wrapped()) {
return weak->getMargins();
}
return RpWidget::getMargins();
}
int naturalWidth() const override {
if (auto weak = wrapped()) {
return weak->naturalWidth();
}
return RpWidget::naturalWidth();
}
protected:
int resizeGetHeight(int newWidth) override {
if (auto weak = wrapped()) {
weak->resizeToWidth(newWidth);
return weak->heightNoMargins();
}
return heightNoMargins();
}
void visibleTopBottomUpdated(
2017-11-21 09:20:56 +00:00
int visibleTop,
int visibleBottom) override {
2017-09-13 16:57:44 +00:00
setChildVisibleTopBottom(
wrapped(),
visibleTop,
visibleBottom);
}
virtual void wrappedSizeUpdated(QSize size) {
resize(size);
}
private:
object_ptr<Widget> _wrapped;
};
template <typename Widget>
Wrap<Widget, RpWidget>::Wrap(
QWidget *parent,
object_ptr<Widget> &&child)
2017-09-13 16:57:44 +00:00
: RpWidget(parent)
, _wrapped(std::move(child)) {
if (_wrapped) {
_wrapped->sizeValue(
) | rpl::start_with_next([this](const QSize &value) {
wrappedSizeUpdated(value);
}, lifetime());
2017-09-13 16:57:44 +00:00
AttachParentChild(this, _wrapped);
_wrapped->move(0, 0);
_wrapped->alive(
) | rpl::start_with_done([this] {
_wrapped->setParent(nullptr);
_wrapped = nullptr;
delete this;
}, lifetime());
2017-09-13 16:57:44 +00:00
}
}
template <typename Widget, typename ParentType>
class Wrap : public ParentType {
public:
using ParentType::ParentType;
Widget *wrapped() {
return static_cast<Widget*>(ParentType::wrapped());
}
const Widget *wrapped() const {
return static_cast<const Widget*>(ParentType::wrapped());
}
auto entity() {
return details::UnwrapHelper::Unwrap(wrapped());
}
auto entity() const {
return details::UnwrapHelper::Unwrap(wrapped());
}
2017-09-18 17:04:45 +00:00
using WrapParentType = ParentType;
2017-09-13 16:57:44 +00:00
};
2018-09-07 12:19:56 +00:00
class OverrideMargins : public Wrap<RpWidget> {
2017-11-10 15:45:10 +00:00
using Parent = Wrap<RpWidget>;
public:
2018-09-07 12:19:56 +00:00
OverrideMargins(
QWidget *parent,
object_ptr<RpWidget> &&child,
QMargins margins = QMargins())
: Parent(parent, std::move(child)), _margins(margins) {
2017-11-10 15:45:10 +00:00
if (auto weak = wrapped()) {
auto margins = weak->getMargins();
resizeToWidth(weak->width()
- margins.left()
- margins.right());
}
}
QMargins getMargins() const override {
2018-09-07 12:19:56 +00:00
return _margins;
2017-11-10 15:45:10 +00:00
}
protected:
int resizeGetHeight(int newWidth) override {
if (auto weak = wrapped()) {
weak->resizeToWidth(newWidth);
2018-09-07 12:19:56 +00:00
weak->moveToLeft(_margins.left(), _margins.top());
2017-11-10 15:45:10 +00:00
return weak->heightNoMargins();
}
return height();
}
private:
void wrappedSizeUpdated(QSize size) override {
auto margins = wrapped()->getMargins();
resize(
2018-09-07 12:19:56 +00:00
(size.width()
- margins.left()
- margins.right()
+ _margins.left()
+ _margins.right()),
(size.height()
- margins.top()
- margins.bottom()
+ _margins.top()
+ _margins.bottom()));
2017-11-10 15:45:10 +00:00
}
2018-09-07 12:19:56 +00:00
QMargins _margins;
2017-11-10 15:45:10 +00:00
};
2017-09-13 16:57:44 +00:00
} // namespace Ui