// This file is part of Desktop App Toolkit, // a set of libraries for developing nice desktop applications. // // For license and copyright information please follow this link: // https://github.com/desktop-app/legal/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 #include namespace Ui { void ToggleChildrenVisibility(not_null widget, bool visible); } // namespace Ui class TWidget; template class TWidgetHelper : public Base { public: using Base::Base; virtual QMargins getMargins() const { return QMargins(); } void hideChildren() { Ui::ToggleChildrenVisibility(this, false); } void showChildren() { Ui::ToggleChildrenVisibility(this, true); } 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: #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) void enterEvent(QEnterEvent *e) final override { if (auto parent = tparent()) { parent->leaveToChildEvent(e, this); } return enterEventHook(e); } #else // Qt >= 6.0.0 void enterEvent(QEvent *e) final override { if (auto parent = tparent()) { parent->leaveToChildEvent(e, this); } return enterEventHook(static_cast(e)); } #endif // Qt < 6.0.0 virtual void enterEventHook(QEnterEvent *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 RpWidgetBase; class RpWidgetWrap { public: virtual QWidget *rpWidget() = 0; virtual const QWidget *rpWidget() const = 0; virtual QObjectData *rpPrivate() = 0; virtual const QObjectData *rpPrivate() const = 0; rpl::producer> events() const; 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 ~RpWidgetWrap() = default; protected: bool handleEvent(QEvent *event); virtual bool eventHook(QEvent *event) = 0; private: template friend class RpWidgetBase; struct EventStreams { rpl::event_stream> events; rpl::event_stream geometry; rpl::event_stream paint; rpl::event_stream shown; rpl::event_stream<> alive; }; struct Initer { Initer(QWidget *parent, bool setZeroGeometry); }; virtual void callSetVisible(bool visible) = 0; void visibilityChangedHook(bool wasVisible, bool nowVisible); EventStreams &eventStreams() const; mutable std::unique_ptr _eventStreams; rpl::lifetime _lifetime; }; struct RpWidgetDefaultTraits { static constexpr bool kSetZeroGeometry = true; }; template class RpWidgetBase : public RpWidgetParent , public RpWidgetWrap { using Self = RpWidgetBase; using Parent = RpWidgetParent; public: using Parent::Parent; QWidget *rpWidget() final override { return this; } const QWidget *rpWidget() const final override { return this; } QObjectData *rpPrivate() final override { return qGetPtrHelper(this->d_ptr); } const QObjectData *rpPrivate() const final override { return qGetPtrHelper(this->d_ptr); } void setVisible(bool visible) final override { auto wasVisible = !this->isHidden(); setVisibleHook(visible); visibilityChangedHook(wasVisible, !this->isHidden()); } ~RpWidgetBase() { 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) final override { Self::setVisible(visible); // Save one virtual method invocation. } Initer _initer = { this, Traits::kSetZeroGeometry }; }; class RpWidget : public RpWidgetBase { public: using RpWidgetBase::RpWidgetBase; }; } // namespace Ui