Sine Striker
2023-12-07 1a1f26d9e4ab6a4fe51baa78e40c4cdf3f402bcf
Add context factory
10个文件已修改
119 ■■■■■ 已修改文件
examples/mainwindow/CMakeLists.txt 2 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
examples/mainwindow/mainwindow.cpp 23 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/core/contexts/abstractwindowcontext.cpp 20 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/core/contexts/abstractwindowcontext_p.h 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/core/contexts/qtwindowcontext.cpp 7 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/core/contexts/qtwindowcontext_p.h 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/core/contexts/win32windowcontext.cpp 38 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/core/contexts/win32windowcontext_p.h 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/core/corewindowagent.cpp 15 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/core/corewindowagent_p.h 2 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
examples/mainwindow/CMakeLists.txt
@@ -13,3 +13,5 @@
    QT_LINKS Core Gui Widgets
    LINKS QWKWidgets WidgetFrame
)
qm_add_win_manifest(${PROJECT_NAME})
examples/mainwindow/mainwindow.cpp
@@ -53,42 +53,19 @@
        return menuBar;
    }();
    static const auto buttonStyleSheet = QLatin1String{ R"(
QPushButton {
    background-color:white;
    color:black;
}
QPushButton:hover {
    background-color:black;
    color:white;
}
QPushButton:pressed {
    background-color:red;
    color:white;
}
)" };
    auto iconButton = new QPushButton("I");
    iconButton->setFlat(true);
    iconButton->setAttribute(Qt::WA_Hover);
    iconButton->setMouseTracking(true);
    iconButton->setStyleSheet(buttonStyleSheet);
    auto minButton = new QPushButton("—");
    minButton->setFlat(true);
    minButton->setAttribute(Qt::WA_Hover);
    minButton->setMouseTracking(true);
    minButton->setStyleSheet(buttonStyleSheet);
    auto maxButton = new QPushButton("🗖");
    maxButton->setCheckable(true);
    maxButton->setFlat(true);
    maxButton->setAttribute(Qt::WA_Hover);
    maxButton->setMouseTracking(true);
    maxButton->setStyleSheet(buttonStyleSheet);
    auto closeButton = new QPushButton("✖");
    closeButton->setFlat(true);
    closeButton->setAttribute(Qt::WA_Hover);
    closeButton->setMouseTracking(true);
    closeButton->setStyleSheet(buttonStyleSheet);
    auto windowBar = new QWK::WindowBar();
    windowBar->setIconButton(iconButton);
src/core/contexts/abstractwindowcontext.cpp
@@ -2,12 +2,28 @@
namespace QWK {
    AbstractWindowContext::AbstractWindowContext(QObject *host, WindowItemDelegate *delegate)
        : m_host(host), m_delegate(delegate), m_windowHandle(delegate->hostWindow(host)) {
    AbstractWindowContext::AbstractWindowContext()
        : m_host(nullptr), m_delegate(nullptr), m_windowHandle(nullptr) {
    }
    AbstractWindowContext::~AbstractWindowContext() = default;
    bool AbstractWindowContext::setup(QObject *host, WindowItemDelegate *delegate) {
        if (!host || !delegate) {
            return false;
        }
        auto windowHandle = delegate->hostWindow(host);
        if (!windowHandle) {
            return false;
        }
        m_host = host;
        m_delegate.reset(delegate);
        m_windowHandle = windowHandle;
        return true;
    }
    bool AbstractWindowContext::setHitTestVisible(const QObject *obj, bool visible) {
        Q_ASSERT(obj);
        if (!obj) {
src/core/contexts/abstractwindowcontext_p.h
@@ -16,11 +16,11 @@
    class QWK_CORE_EXPORT AbstractWindowContext : public QObject {
        Q_OBJECT
    public:
        AbstractWindowContext(QObject *host, WindowItemDelegate *delegate);
        AbstractWindowContext();
        ~AbstractWindowContext() override;
    public:
        virtual bool setup() = 0;
        virtual bool setup(QObject *host, WindowItemDelegate *delegate);
        inline QObject *host() const;
        inline QWindow *window() const;
src/core/contexts/qtwindowcontext.cpp
@@ -40,15 +40,14 @@
#endif
    }
    QtWindowContext::QtWindowContext(QObject *host, WindowItemDelegate *delegate)
        : AbstractWindowContext(host, delegate) {
    QtWindowContext::QtWindowContext() : AbstractWindowContext() {
    }
    QtWindowContext::~QtWindowContext() {
    }
    bool QtWindowContext::setup() {
        if (!m_windowHandle) {
    bool QtWindowContext::setup(QObject *host, WindowItemDelegate *delegate) {
        if (!AbstractWindowContext::setup(host, delegate)) {
            return false;
        }
        return false;
src/core/contexts/qtwindowcontext_p.h
@@ -8,11 +8,11 @@
    class QWK_CORE_EXPORT QtWindowContext : public AbstractWindowContext {
        Q_OBJECT
    public:
        QtWindowContext(QObject *host, WindowItemDelegate *delegate);
        QtWindowContext();
        ~QtWindowContext();
    public:
        bool setup() override;
        bool setup(QObject *host, WindowItemDelegate *delegate) override;
    protected:
        bool eventFilter(QObject *obj, QEvent *event) override;
src/core/contexts/win32windowcontext.cpp
@@ -188,20 +188,12 @@
    }
    static inline void triggerFrameChange(HWND hwnd) {
        Q_ASSERT(hwnd);
        if (!hwnd) {
            return;
        }
        ::SetWindowPos(hwnd, nullptr, 0, 0, 0, 0,
                       SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOOWNERZORDER |
                           SWP_FRAMECHANGED);
    }
    static inline quint32 getDpiForWindow(HWND hwnd) {
        Q_ASSERT(hwnd);
        if (!hwnd) {
            return 0;
        }
        const DynamicApis &apis = DynamicApis::instance();
        if (apis.pGetDpiForWindow) { // Win10
            return apis.pGetDpiForWindow(hwnd);
@@ -221,10 +213,6 @@
    }
    static inline quint32 getResizeBorderThickness(HWND hwnd) {
        Q_ASSERT(hwnd);
        if (!hwnd) {
            return 0;
        }
        const DynamicApis &apis = DynamicApis::instance();
        if (apis.pGetSystemMetricsForDpi) {
            const quint32 dpi = getDpiForWindow(hwnd);
@@ -236,10 +224,6 @@
    }
    static inline quint32 getTitleBarHeight(HWND hwnd) {
        Q_ASSERT(hwnd);
        if (!hwnd) {
            return 0;
        }
        const auto captionHeight = [hwnd]() -> int {
            const DynamicApis &apis = DynamicApis::instance();
            if (apis.pGetSystemMetricsForDpi) {
@@ -253,11 +237,6 @@
    }
    static inline void updateInternalWindowFrameMargins(HWND hwnd, QWindow *window) {
        Q_ASSERT(hwnd);
        Q_ASSERT(window);
        if (!hwnd || !window) {
            return;
        }
        const auto margins = [hwnd]() -> QMargins {
            const int titleBarHeight = getTitleBarHeight(hwnd);
            if (isWin10OrGreater()) {
@@ -285,10 +264,6 @@
    }
    static inline MONITORINFOEXW getMonitorForWindow(HWND hwnd) {
        Q_ASSERT(hwnd);
        if (!hwnd) {
            return {};
        }
        // Use "MONITOR_DEFAULTTONEAREST" here so that we can still get the correct
        // monitor even if the window is minimized.
        HMONITOR monitor = ::MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);
@@ -299,10 +274,6 @@
    };
    static inline void moveToDesktopCenter(HWND hwnd) {
        Q_ASSERT(hwnd);
        if (!hwnd) {
            return;
        }
        const auto monitorInfo = getMonitorForWindow(hwnd);
        RECT windowRect{};
        ::GetWindowRect(hwnd, &windowRect);
@@ -539,8 +510,7 @@
        return ::CallWindowProcW(g_qtWindowProc, hWnd, message, wParam, lParam);
    }
    Win32WindowContext::Win32WindowContext(QObject *host, WindowItemDelegate *delegate)
        : AbstractWindowContext(host, delegate) {
    Win32WindowContext::Win32WindowContext() : AbstractWindowContext() {
    }
    Win32WindowContext::~Win32WindowContext() {
@@ -555,8 +525,8 @@
        }
    }
    bool Win32WindowContext::setup() {
        if (!m_windowHandle) {
    bool Win32WindowContext::setup(QObject *host, WindowItemDelegate *delegate) {
        if (!AbstractWindowContext::setup(host, delegate)) {
            return false;
        }
@@ -1294,8 +1264,6 @@
                                break;
                            case CoreWindowAgent::Close:
                                *result = HTCLOSE;
                                break;
                            case CoreWindowAgent::Unknown:
                                break;
                            default:
                                break; // unreachable
src/core/contexts/win32windowcontext_p.h
@@ -9,7 +9,7 @@
    class QWK_CORE_EXPORT Win32WindowContext : public AbstractWindowContext {
        Q_OBJECT
    public:
        Win32WindowContext(QObject *host, WindowItemDelegate *delegate);
        Win32WindowContext();
        ~Win32WindowContext() override;
        enum WindowPart {
@@ -22,7 +22,7 @@
        };
    public:
        bool setup() override;
        bool setup(QObject *host, WindowItemDelegate *delegate) override;
        bool windowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam, LRESULT *result);
src/core/corewindowagent.cpp
@@ -21,15 +21,20 @@
    void CoreWindowAgentPrivate::init() {
    }
    bool CoreWindowAgentPrivate::setup(QObject *host, WindowItemDelegate *delegate) {
        auto ctx =
    AbstractWindowContext *CoreWindowAgentPrivate::createContext() const {
        return
#ifdef Q_OS_WINDOWS
            std::make_unique<Win32WindowContext>(host, delegate)
            new Win32WindowContext()
#else
            std::make_unique<QtWindowContext>(host, window, delegate)
            new QtWindowContext()
#endif
            ;
        if (!ctx->setup()) {
    }
    bool CoreWindowAgentPrivate::setup(QObject *host, WindowItemDelegate *delegate) {
        std::unique_ptr<AbstractWindowContext> ctx(createContext());
        if (!ctx->setup(host, delegate)) {
            return false;
        }
        context = std::move(ctx);
src/core/corewindowagent_p.h
@@ -16,6 +16,8 @@
        CoreWindowAgent *q_ptr; // no need to initialize
        virtual AbstractWindowContext *createContext() const;
        bool setup(QObject *host, WindowItemDelegate *delegate);
        std::unique_ptr<AbstractWindowContext> context;