/* This file is part of Telegram Desktop, the official desktop application for the Telegram messaging service. For license and copyright information please follow this link: https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL */ #pragma once #include "base/unique_qptr.h" #include "ui/style/style_core_direction.h" #include "ui/ui_utility.h" #include #include #include #include #include class TWidget; template 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(child)->hide(); } } } void showChildren() { for (auto child : Base::children()) { if (child->isWidgetType()) { static_cast(child)->show(); } } } void moveToLeft(int x, int y, int outerw = 0) { auto margins = getMargins(); x -= margins.left(); y -= margins.top(); Base::move(style::RightToLeft() ? ((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(style::RightToLeft() ? 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(style::RightToLeft() ? ((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(style::RightToLeft() ? 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(Base::parentWidget()); } const TWidget *tparent() const { return qobject_cast(Base::parentWidget()); } template friend class TWidgetHelper; }; class TWidget : public TWidgetHelper { // The Q_OBJECT meta info is used for qobject_cast! Q_OBJECT public: TWidget(QWidget *parent = nullptr) : TWidgetHelper(parent) { } // Get the size of the widget as it should be. // Negative return value means no default width. virtual int naturalWidth() const { return -1; } // 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(); } } // 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( std::clamp(visibleTop, 0, max), std::clamp(visibleBottom, 0, max)); } 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) { } }; namespace Ui { class RpWidget; void ResizeFitChild(not_null parent, not_null child); template using RpWidgetParent = std::conditional_t< std::is_same_v, TWidget, TWidgetHelper>; template class RpWidgetWrap; class RpWidgetMethods { public: rpl::producer geometryValue() const; rpl::producer sizeValue() const; rpl::producer heightValue() const; rpl::producer widthValue() const; rpl::producer positionValue() const; rpl::producer leftValue() const; rpl::producer topValue() const; virtual rpl::producer desiredHeightValue() const; rpl::producer shownValue() const; rpl::producer paintRequest() const; rpl::producer<> alive() const; rpl::producer<> windowDeactivateEvents() const; rpl::producer<> macWindowDeactivateEvents() const; template void showOn(rpl::producer &&shown) { std::move( shown ) | rpl::start_with_next([this](bool visible) { callSetVisible(visible); }, lifetime()); } rpl::lifetime &lifetime(); virtual ~RpWidgetMethods() = default; protected: bool handleEvent(QEvent *event); virtual bool eventHook(QEvent *event) = 0; private: template friend class RpWidgetWrap; struct EventStreams { rpl::event_stream geometry; rpl::event_stream paint; rpl::event_stream shown; rpl::event_stream<> alive; }; struct Initer { Initer(QWidget *parent); }; virtual void callSetVisible(bool visible) = 0; virtual QWidget *callGetWidget() = 0; virtual const QWidget *callGetWidget() const = 0; virtual QPointer callCreateWeak() = 0; virtual QRect callGetGeometry() const = 0; virtual bool callIsHidden() const = 0; void visibilityChangedHook(bool wasVisible, bool nowVisible); EventStreams &eventStreams() const; mutable std::unique_ptr _eventStreams; rpl::lifetime _lifetime; }; template class RpWidgetWrap : public RpWidgetParent , public RpWidgetMethods { using Self = RpWidgetWrap; using Parent = RpWidgetParent; public: using Parent::Parent; void setVisible(bool visible) final override { auto wasVisible = !this->isHidden(); setVisibleHook(visible); visibilityChangedHook(wasVisible, !this->isHidden()); } ~RpWidgetWrap() { base::take(_lifetime); base::take(_eventStreams); } protected: bool event(QEvent *event) final override { return handleEvent(event); } bool eventHook(QEvent *event) override { return Parent::event(event); } virtual void setVisibleHook(bool visible) { Parent::setVisible(visible); } private: void callSetVisible(bool visible) override { Self::setVisible(visible); } QWidget *callGetWidget() override { return this; } const QWidget *callGetWidget() const override { return this; } QPointer callCreateWeak() override { return QPointer((QObject*)this); } QRect callGetGeometry() const override { return this->geometry(); } bool callIsHidden() const override { return this->isHidden(); } Initer _initer = { this }; }; class RpWidget : public RpWidgetWrap { public: using RpWidgetWrap::RpWidgetWrap; }; } // namespace Ui