tdesktop/Telegram/SourceFiles/ui/rp_widget.h

381 lines
9.3 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
*/
#pragma once
2019-09-13 15:22:54 +03:00
#include "base/unique_qptr.h"
#include "ui/style/style_core_direction.h"
#include "ui/ui_utility.h"
2017-09-13 19:57:44 +03:00
#include <rpl/event_stream.h>
#include <rpl/map.h>
#include <rpl/distinct_until_changed.h>
2019-09-13 15:22:54 +03:00
class TWidget;
2017-11-06 22:03:20 +04:00
2019-09-13 15:22:54 +03:00
template <typename Base>
class TWidgetHelper : public Base {
public:
using Base::Base;
virtual QMargins getMargins() const {
return QMargins();
}
bool inFocusChain() const {
return Ui::InFocusChain(this);
}
void hideChildren() {
for (auto child : Base::children()) {
if (child->isWidgetType()) {
static_cast<QWidget*>(child)->hide();
}
}
}
void showChildren() {
for (auto child : Base::children()) {
if (child->isWidgetType()) {
static_cast<QWidget*>(child)->show();
}
}
}
void moveToLeft(int x, int y, int outerw = 0) {
auto margins = getMargins();
x -= margins.left();
y -= margins.top();
Base::move(rtl() ? ((outerw > 0 ? outerw : Base::parentWidget()->width()) - x - Base::width()) : x, y);
}
void moveToRight(int x, int y, int outerw = 0) {
auto margins = getMargins();
x -= margins.right();
y -= margins.top();
Base::move(rtl() ? x : ((outerw > 0 ? outerw : Base::parentWidget()->width()) - x - Base::width()), y);
}
void setGeometryToLeft(int x, int y, int w, int h, int outerw = 0) {
auto margins = getMargins();
x -= margins.left();
y -= margins.top();
w -= margins.left() - margins.right();
h -= margins.top() - margins.bottom();
Base::setGeometry(rtl() ? ((outerw > 0 ? outerw : Base::parentWidget()->width()) - x - w) : x, y, w, h);
}
void setGeometryToRight(int x, int y, int w, int h, int outerw = 0) {
auto margins = getMargins();
x -= margins.right();
y -= margins.top();
w -= margins.left() - margins.right();
h -= margins.top() - margins.bottom();
Base::setGeometry(rtl() ? x : ((outerw > 0 ? outerw : Base::parentWidget()->width()) - x - w), y, w, h);
}
QPoint myrtlpoint(int x, int y) const {
return style::rtlpoint(x, y, Base::width());
}
QPoint myrtlpoint(const QPoint point) const {
return style::rtlpoint(point, Base::width());
}
QRect myrtlrect(int x, int y, int w, int h) const {
return style::rtlrect(x, y, w, h, Base::width());
}
QRect myrtlrect(const QRect &rect) const {
return style::rtlrect(rect, Base::width());
}
void rtlupdate(const QRect &rect) {
Base::update(myrtlrect(rect));
}
void rtlupdate(int x, int y, int w, int h) {
Base::update(myrtlrect(x, y, w, h));
}
QPoint mapFromGlobal(const QPoint &point) const {
return Base::mapFromGlobal(point);
}
QPoint mapToGlobal(const QPoint &point) const {
return Base::mapToGlobal(point);
}
QRect mapFromGlobal(const QRect &rect) const {
return QRect(mapFromGlobal(rect.topLeft()), rect.size());
}
QRect mapToGlobal(const QRect &rect) const {
return QRect(mapToGlobal(rect.topLeft()), rect.size());
}
protected:
void enterEvent(QEvent *e) final override {
if (auto parent = tparent()) {
parent->leaveToChildEvent(e, this);
}
return enterEventHook(e);
}
virtual void enterEventHook(QEvent *e) {
return Base::enterEvent(e);
}
void leaveEvent(QEvent *e) final override {
if (auto parent = tparent()) {
parent->enterFromChildEvent(e, this);
}
return leaveEventHook(e);
}
virtual void leaveEventHook(QEvent *e) {
return Base::leaveEvent(e);
}
// e - from enterEvent() of child TWidget
virtual void leaveToChildEvent(QEvent *e, QWidget *child) {
}
// e - from leaveEvent() of child TWidget
virtual void enterFromChildEvent(QEvent *e, QWidget *child) {
}
private:
TWidget *tparent() {
return qobject_cast<TWidget*>(Base::parentWidget());
}
const TWidget *tparent() const {
return qobject_cast<const TWidget*>(Base::parentWidget());
}
template <typename OtherBase>
friend class TWidgetHelper;
2018-11-21 14:09:46 +04:00
};
2019-09-13 15:22:54 +03:00
class TWidget : public TWidgetHelper<QWidget> {
// The Q_OBJECT meta info is used for qobject_cast!
Q_OBJECT
2017-11-06 22:03:20 +04:00
public:
2019-09-13 15:22:54 +03:00
TWidget(QWidget *parent = nullptr) : TWidgetHelper<QWidget>(parent) {
2017-11-06 22:03:20 +04:00
}
2019-09-13 15:22:54 +03:00
// Get the size of the widget as it should be.
// Negative return value means no default width.
virtual int naturalWidth() const {
return -1;
2018-11-21 14:09:46 +04:00
}
2019-09-13 15:22:54 +03:00
// Count new height for width=newWidth and resize to it.
void resizeToWidth(int newWidth) {
auto margins = getMargins();
auto fullWidth = margins.left() + newWidth + margins.right();
auto fullHeight = margins.top() + resizeGetHeight(newWidth) + margins.bottom();
auto newSize = QSize(fullWidth, fullHeight);
if (newSize != size()) {
resize(newSize);
update();
}
2018-09-06 00:01:50 +03:00
}
2019-09-13 15:22:54 +03:00
// Resize to minimum of natural width and available width.
void resizeToNaturalWidth(int newWidth) {
auto maxWidth = naturalWidth();
resizeToWidth((maxWidth >= 0) ? qMin(newWidth, maxWidth) : newWidth);
}
QRect rectNoMargins() const {
return rect().marginsRemoved(getMargins());
}
int widthNoMargins() const {
return rectNoMargins().width();
}
int heightNoMargins() const {
return rectNoMargins().height();
}
int bottomNoMargins() const {
auto rectWithoutMargins = rectNoMargins();
return y() + rectWithoutMargins.y() + rectWithoutMargins.height();
}
QSize sizeNoMargins() const {
return rectNoMargins().size();
}
// Updates the area that is visible inside the scroll container.
void setVisibleTopBottom(int visibleTop, int visibleBottom) {
auto max = height();
visibleTopBottomUpdated(
snap(visibleTop, 0, max),
snap(visibleBottom, 0, max));
}
signals:
// Child widget is responsible for emitting this signal.
void heightUpdated();
protected:
void setChildVisibleTopBottom(
TWidget *child,
int visibleTop,
int visibleBottom) {
if (child) {
auto top = child->y();
child->setVisibleTopBottom(
visibleTop - top,
visibleBottom - top);
}
}
// Resizes content and counts natural widget height for the desired width.
virtual int resizeGetHeight(int newWidth) {
return heightNoMargins();
}
virtual void visibleTopBottomUpdated(
int visibleTop,
int visibleBottom) {
}
2017-11-06 22:03:20 +04:00
};
2019-09-13 15:22:54 +03:00
namespace Ui {
2017-09-13 19:57:44 +03:00
2018-09-05 22:05:49 +03:00
class RpWidget;
2019-09-13 15:22:54 +03:00
void ResizeFitChild(not_null<RpWidget*> parent, not_null<RpWidget*> child);
2017-11-06 22:03:20 +04:00
2017-09-25 19:06:53 +03:00
template <typename Widget>
using RpWidgetParent = std::conditional_t<
std::is_same_v<Widget, QWidget>,
TWidget,
TWidgetHelper<Widget>>;
template <typename Widget>
2017-11-21 13:20:56 +04:00
class RpWidgetWrap;
2017-09-25 19:06:53 +03:00
2017-11-21 13:20:56 +04:00
class RpWidgetMethods {
2017-09-13 19:57:44 +03:00
public:
2017-11-21 13:20:56 +04:00
rpl::producer<QRect> geometryValue() const;
rpl::producer<QSize> sizeValue() const;
rpl::producer<int> heightValue() const;
rpl::producer<int> widthValue() const;
rpl::producer<QPoint> positionValue() const;
rpl::producer<int> leftValue() const;
rpl::producer<int> topValue() const;
virtual rpl::producer<int> desiredHeightValue() const;
rpl::producer<bool> shownValue() const;
rpl::producer<QRect> paintRequest() const;
rpl::producer<> alive() const;
2019-09-04 10:19:15 +03:00
rpl::producer<> windowDeactivateEvents() const;
rpl::producer<> macWindowDeactivateEvents() const;
2017-10-01 12:39:07 +03:00
2017-09-27 15:04:19 +03:00
template <typename Error, typename Generator>
void showOn(rpl::producer<bool, Error, Generator> &&shown) {
std::move(
shown
) | rpl::start_with_next([this](bool visible) {
callSetVisible(visible);
}, lifetime());
2017-09-25 19:06:53 +03:00
}
2019-09-04 10:19:15 +03:00
2017-11-21 13:20:56 +04:00
rpl::lifetime &lifetime();
2017-09-13 19:57:44 +03:00
virtual ~RpWidgetMethods() = default;
protected:
2017-11-21 13:20:56 +04:00
bool handleEvent(QEvent *event);
virtual bool eventHook(QEvent *event) = 0;
private:
2017-11-21 13:20:56 +04:00
template <typename Widget>
friend class RpWidgetWrap;
struct EventStreams {
rpl::event_stream<QRect> geometry;
rpl::event_stream<QRect> paint;
2017-10-01 12:39:07 +03:00
rpl::event_stream<bool> shown;
rpl::event_stream<> alive;
2017-09-13 19:57:44 +03:00
};
2017-11-21 13:20:56 +04:00
struct Initer {
Initer(QWidget *parent);
};
2017-09-13 19:57:44 +03:00
2017-11-21 13:20:56 +04:00
virtual void callSetVisible(bool visible) = 0;
2019-09-04 10:19:15 +03:00
virtual QWidget *callGetWidget() = 0;
virtual const QWidget *callGetWidget() const = 0;
2017-11-21 13:20:56 +04:00
virtual QPointer<QObject> callCreateWeak() = 0;
virtual QRect callGetGeometry() const = 0;
virtual bool callIsHidden() const = 0;
void visibilityChangedHook(bool wasVisible, bool nowVisible);
EventStreams &eventStreams() const;
2017-09-13 19:57:44 +03:00
mutable std::unique_ptr<EventStreams> _eventStreams;
2017-11-21 13:20:56 +04:00
rpl::lifetime _lifetime;
};
template <typename Widget>
class RpWidgetWrap
: public RpWidgetParent<Widget>
, public RpWidgetMethods {
using Self = RpWidgetWrap<Widget>;
using Parent = RpWidgetParent<Widget>;
public:
using Parent::Parent;
void setVisible(bool visible) final override {
auto wasVisible = !this->isHidden();
2018-05-07 20:44:33 +03:00
setVisibleHook(visible);
2017-11-21 13:20:56 +04:00
visibilityChangedHook(wasVisible, !this->isHidden());
}
~RpWidgetWrap() {
base::take(_lifetime);
base::take(_eventStreams);
}
2017-11-21 13:20:56 +04:00
protected:
bool event(QEvent *event) final override {
return handleEvent(event);
}
bool eventHook(QEvent *event) override {
return Parent::event(event);
}
2018-05-07 20:44:33 +03:00
virtual void setVisibleHook(bool visible) {
Parent::setVisible(visible);
}
2017-11-21 13:20:56 +04:00
private:
void callSetVisible(bool visible) override {
Self::setVisible(visible);
}
2019-09-04 10:19:15 +03:00
QWidget *callGetWidget() override {
return this;
}
const QWidget *callGetWidget() const override {
return this;
}
2017-11-21 13:20:56 +04:00
QPointer<QObject> callCreateWeak() override {
return QPointer<QObject>((QObject*)this);
}
QRect callGetGeometry() const override {
return this->geometry();
}
bool callIsHidden() const override {
return this->isHidden();
}
2017-09-13 19:57:44 +03:00
2017-11-21 13:20:56 +04:00
Initer _initer = { this };
};
2017-09-25 19:06:53 +03:00
class RpWidget : public RpWidgetWrap<QWidget> {
public:
2017-09-25 19:06:53 +03:00
using RpWidgetWrap<QWidget>::RpWidgetWrap;
2017-09-13 19:57:44 +03:00
};
} // namespace Ui