SineStriker
2024-01-29 bc8868d519702c5a55f21377711cf4db54714650
Fix centerize issue (#34)

9个文件已修改
62 ■■■■■ 已修改文件
examples/mainwindow/mainwindow.cpp 2 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/core/contexts/abstractwindowcontext.cpp 8 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/core/contexts/win32windowcontext.cpp 39 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/core/contexts/win32windowcontext_p.h 2 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/core/windowitemdelegate_p.h 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/quick/quickitemdelegate.cpp 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/quick/quickitemdelegate_p.h 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/widgets/widgetitemdelegate.cpp 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/widgets/widgetitemdelegate_p.h 1 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
examples/mainwindow/mainwindow.cpp
@@ -51,6 +51,8 @@
    setWindowTitle(tr("Example MainWindow"));
    resize(800, 600);
    windowAgent->centralize();
}
static inline void emulateLeaveEvent(QWidget *widget) {
src/core/contexts/abstractwindowcontext.cpp
@@ -124,8 +124,7 @@
    }
#ifdef Q_OS_MAC
    void
        AbstractWindowContext::setSystemButtonAreaCallback(const ScreenRectCallback &callback) {
    void AbstractWindowContext::setSystemButtonAreaCallback(const ScreenRectCallback &callback) {
        m_systemButtonAreaCallback = callback;
        virtual_hook(SystemButtonAreaChangedHook, nullptr);
    }
@@ -206,9 +205,10 @@
                if (!m_windowHandle)
                    return;
                QRect windowGeometry = m_delegate->getGeometry(m_host);
                QRect screenGeometry = m_windowHandle->screen()->geometry();
                int x = (screenGeometry.width() - m_windowHandle->width()) / 2;
                int y = (screenGeometry.height() - m_windowHandle->height()) / 2;
                int x = (screenGeometry.width() - windowGeometry.width()) / 2;
                int y = (screenGeometry.height() - windowGeometry.height()) / 2;
                QPoint pos(x, y);
                pos += screenGeometry.topLeft();
                m_windowHandle->setPosition(pos);
src/core/contexts/win32windowcontext.cpp
@@ -98,18 +98,6 @@
        return monitorInfo;
    }
    static inline void moveWindowToDesktopCenter(HWND hwnd) {
        MONITORINFOEXW monitorInfo = getMonitorForWindow(hwnd);
        RECT windowRect{};
        ::GetWindowRect(hwnd, &windowRect);
        const auto newX = monitorInfo.rcMonitor.left +
                          (RECT_WIDTH(monitorInfo.rcMonitor) - RECT_WIDTH(windowRect)) / 2;
        const auto newY = monitorInfo.rcMonitor.top +
                          (RECT_HEIGHT(monitorInfo.rcMonitor) - RECT_HEIGHT(windowRect)) / 2;
        ::SetWindowPos(hwnd, nullptr, newX, newY, 0, 0,
                       SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOOWNERZORDER);
    }
    static inline void moveWindowToMonitor(HWND hwnd, const MONITORINFOEXW &activeMonitor) {
        RECT currentMonitorRect = getMonitorForWindow(hwnd).rcMonitor;
        RECT activeMonitorRect = activeMonitor.rcMonitor;
@@ -563,14 +551,6 @@
    void Win32WindowContext::virtual_hook(int id, void *data) {
        switch (id) {
            case CentralizeHook: {
                if (!windowId)
                    return;
                const auto hwnd = reinterpret_cast<HWND>(windowId);
                moveWindowToDesktopCenter(hwnd);
                return;
            }
            case RaiseWindowHook: {
                if (!windowId)
                    return;
@@ -606,8 +586,8 @@
                return;
            }
            case DrawWindows10BorderHook: {
#if QWINDOWKIT_CONFIG(ENABLE_WINDOWS_SYSTEM_BORDERS)
            case DrawWindows10BorderHook: {
                if (!windowId)
                    return;
@@ -647,12 +627,10 @@
                    QPoint{m_windowHandle->width(), 0}
                });
                painter.restore();
#endif
                return;
            }
            case DrawWindows10BorderHook2: {
#if QWINDOWKIT_CONFIG(ENABLE_WINDOWS_SYSTEM_BORDERS)
                if (!m_windowHandle)
                    return;
@@ -673,9 +651,9 @@
                ::FillRect(hdc, &rcTopBorder,
                           reinterpret_cast<HBRUSH>(::GetStockObject(BLACK_BRUSH)));
                ::ReleaseDC(hWnd, hdc);
#endif
                return;
            }
#endif
            default:
                break;
@@ -1328,19 +1306,6 @@
    bool Win32WindowContext::customWindowHandler(HWND hWnd, UINT message, WPARAM wParam,
                                                 LPARAM lParam, LRESULT *result) {
        switch (message) {
            case WM_SHOWWINDOW: {
                if (!initialCentered) {
                    // If wParam is TRUE, the window is being shown.
                    // If lParam is zero, the message was sent because of a call to the ShowWindow
                    // function.
                    if (wParam && !lParam) {
                        initialCentered = true;
                        moveWindowToDesktopCenter(hWnd);
                    }
                }
                break;
            }
            case WM_NCHITTEST: {
                // 原生Win32窗口只有顶边是在窗口内部resize的,其余三边都是在窗口
                // 外部进行resize的,其原理是,WS_THICKFRAME这个窗口样式会在窗
src/core/contexts/win32windowcontext_p.h
@@ -72,8 +72,6 @@
        // Whether the last mouse leave message is blocked, mainly for handling the unexpected
        // WM_MOUSELEAVE.
        bool mouseLeaveBlocked = false;
        bool initialCentered = false;
    };
}
src/core/windowitemdelegate_p.h
@@ -40,6 +40,7 @@
        virtual bool isWindowActive(const QObject *host) const = 0;
        virtual Qt::WindowStates getWindowState(const QObject *host) const = 0;
        virtual Qt::WindowFlags getWindowFlags(const QObject *host) const = 0;
        virtual QRect getGeometry(const QObject *host) const = 0;
        // Callbacks
        virtual void resetQtGrabbedControl(QObject *host) const;
src/quick/quickitemdelegate.cpp
@@ -68,6 +68,10 @@
        return static_cast<const QQuickWindow *>(host)->flags();
    }
    QRect QuickItemDelegate::getGeometry(const QObject *host) const {
        return static_cast<const QQuickWindow *>(host)->geometry();
    }
    void QuickItemDelegate::setWindowFlags(QObject *host, Qt::WindowFlags flags) const {
        static_cast<QQuickWindow *>(host)->setFlags(flags);
    }
src/quick/quickitemdelegate_p.h
@@ -38,6 +38,7 @@
        bool isWindowActive(const QObject *host) const override;
        Qt::WindowStates getWindowState(const QObject *host) const override;
        Qt::WindowFlags getWindowFlags(const QObject *host) const override;
        QRect getGeometry(const QObject *host) const override;
        void setWindowState(QObject *host, Qt::WindowStates state) const override;
        void setCursorShape(QObject *host, Qt::CursorShape shape) const override;
src/widgets/widgetitemdelegate.cpp
@@ -92,6 +92,10 @@
        return static_cast<const QWidget *>(host)->windowFlags();
    }
    QRect WidgetItemDelegate::getGeometry(const QObject *host) const {
        return static_cast<const QWidget *>(host)->geometry();
    }
    void WidgetItemDelegate::setWindowFlags(QObject *host, Qt::WindowFlags flags) const {
        static_cast<QWidget *>(host)->setWindowFlags(flags);
    }
src/widgets/widgetitemdelegate_p.h
@@ -38,6 +38,7 @@
        bool isWindowActive(const QObject *host) const override;
        Qt::WindowStates getWindowState(const QObject *host) const override;
        Qt::WindowFlags getWindowFlags(const QObject *host) const override;
        QRect getGeometry(const QObject *host) const override;
        void resetQtGrabbedControl(QObject *host) const override;
        void setWindowState(QObject *host, Qt::WindowStates state) const override;