abstractwindowcontext_p.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // Copyright (C) 2023-2024 Stdware Collections (https://www.github.com/stdware)
  2. // Copyright (C) 2021-2023 wangwenx190 (Yuhang Zhao)
  3. // SPDX-License-Identifier: Apache-2.0
  4. #ifndef ABSTRACTWINDOWCONTEXT_P_H
  5. #define ABSTRACTWINDOWCONTEXT_P_H
  6. //
  7. // W A R N I N G !!!
  8. // -----------------
  9. //
  10. // This file is not part of the QWindowKit API. It is used purely as an
  11. // implementation detail. This header file may change from version to
  12. // version without notice, or may even be removed.
  13. //
  14. #include <array>
  15. #include <list>
  16. #include <memory>
  17. #include <utility>
  18. #include <QtCore/QSet>
  19. #include <QtCore/QPointer>
  20. #include <QtGui/QRegion>
  21. #include <QtGui/QWindow>
  22. #include <core/windowagentbase.h>
  23. #include <core/kernel/nativeeventfilter_p.h>
  24. #include <core/kernel/sharedeventfilter_p.h>
  25. #include <core/kernel/winidchangeeventfilter_p.h>
  26. #include <core/windowitemdelegate_p.h>
  27. namespace QWK {
  28. class QWK_CORE_EXPORT AbstractWindowContext : public QObject,
  29. public NativeEventDispatcher,
  30. public SharedEventDispatcher {
  31. Q_OBJECT
  32. public:
  33. AbstractWindowContext();
  34. ~AbstractWindowContext() override;
  35. public:
  36. void setup(QObject *host, WindowItemDelegate *delegate);
  37. inline QObject *host() const;
  38. inline QWindow *window() const;
  39. inline WId windowId() const;
  40. inline WindowItemDelegate *delegate() const;
  41. inline bool isHitTestVisible(const QObject *obj) const;
  42. bool setHitTestVisible(QObject *obj, bool visible);
  43. inline QObject *systemButton(WindowAgentBase::SystemButton button) const;
  44. bool setSystemButton(WindowAgentBase::SystemButton button, QObject *obj);
  45. inline QObject *titleBar() const;
  46. bool setTitleBar(QObject *obj);
  47. #ifdef Q_OS_MAC
  48. inline ScreenRectCallback systemButtonAreaCallback() const;
  49. void setSystemButtonAreaCallback(const ScreenRectCallback &callback);
  50. #endif
  51. bool isInSystemButtons(const QPoint &pos, WindowAgentBase::SystemButton *button) const;
  52. bool isInTitleBarDraggableArea(const QPoint &pos) const;
  53. inline bool isHostWidthFixed() const {
  54. return m_windowHandle
  55. ? ((m_windowHandle->flags() & Qt::MSWindowsFixedSizeDialogHint) ||
  56. m_windowHandle->minimumWidth() == m_windowHandle->maximumWidth())
  57. : false;
  58. }
  59. inline bool isHostHeightFixed() const {
  60. return m_windowHandle
  61. ? ((m_windowHandle->flags() & Qt::MSWindowsFixedSizeDialogHint) ||
  62. m_windowHandle->minimumHeight() == m_windowHandle->maximumHeight())
  63. : false;
  64. }
  65. inline bool isHostSizeFixed() const {
  66. return m_windowHandle ? ((m_windowHandle->flags() & Qt::MSWindowsFixedSizeDialogHint) ||
  67. m_windowHandle->minimumSize() == m_windowHandle->maximumSize())
  68. : false;
  69. }
  70. virtual QString key() const;
  71. enum WindowContextHook {
  72. CentralizeHook = 1,
  73. RaiseWindowHook,
  74. ShowSystemMenuHook,
  75. DefaultColorsHook,
  76. DrawWindows10BorderHook_Emulated, // Only works on Windows 10, emulated workaround
  77. DrawWindows10BorderHook_Native, // Only works on Windows 10, native workaround
  78. SystemButtonAreaChangedHook, // Only works on Mac
  79. };
  80. virtual void virtual_hook(int id, void *data);
  81. void showSystemMenu(const QPoint &pos);
  82. void notifyWinIdChange();
  83. virtual QVariant windowAttribute(const QString &key) const;
  84. virtual bool setWindowAttribute(const QString &key, const QVariant &attribute);
  85. protected:
  86. bool eventFilter(QObject *obj, QEvent *event) override;
  87. protected:
  88. virtual void winIdChanged(WId winId, WId oldWinId) = 0;
  89. virtual bool windowAttributeChanged(const QString &key, const QVariant &attribute,
  90. const QVariant &oldAttribute);
  91. protected:
  92. QObject *m_host{};
  93. std::unique_ptr<WindowItemDelegate> m_delegate;
  94. QPointer<QWindow> m_windowHandle;
  95. WId m_windowId{};
  96. QVector<QPointer<QObject>> m_hitTestVisibleItems;
  97. #ifdef Q_OS_MAC
  98. ScreenRectCallback m_systemButtonAreaCallback;
  99. #endif
  100. QPointer<QObject> m_titleBar{};
  101. std::array<QPointer<QObject>, WindowAgentBase::Close + 1> m_systemButtons{};
  102. std::list<std::pair<QString, QVariant>> m_windowAttributesOrder;
  103. QHash<QString, decltype(m_windowAttributesOrder)::iterator> m_windowAttributes;
  104. std::unique_ptr<WinIdChangeEventFilter> m_winIdChangeEventFilter;
  105. void removeSystemButtonsAndHitTestItems();
  106. };
  107. inline QObject *AbstractWindowContext::host() const {
  108. return m_host;
  109. }
  110. inline QWindow *AbstractWindowContext::window() const {
  111. return m_windowHandle;
  112. }
  113. inline WId AbstractWindowContext::windowId() const {
  114. return m_windowId;
  115. }
  116. inline WindowItemDelegate *AbstractWindowContext::delegate() const {
  117. return m_delegate.get();
  118. }
  119. inline bool AbstractWindowContext::isHitTestVisible(const QObject *obj) const {
  120. return m_hitTestVisibleItems.contains(const_cast<QObject *>(obj));
  121. }
  122. inline QObject *
  123. AbstractWindowContext::systemButton(WindowAgentBase::SystemButton button) const {
  124. return m_systemButtons[button];
  125. }
  126. inline QObject *AbstractWindowContext::titleBar() const {
  127. return m_titleBar;
  128. }
  129. #ifdef Q_OS_MAC
  130. inline ScreenRectCallback AbstractWindowContext::systemButtonAreaCallback() const {
  131. return m_systemButtonAreaCallback;
  132. }
  133. #endif
  134. }
  135. #endif // ABSTRACTWINDOWCONTEXT_P_H