From 995dc0b4d52e66adac84812dedf32785a65bce83 Mon Sep 17 00:00:00 2001
From: Sine Striker <trueful@163.com>
Date: 周三, 20 12月 2023 21:36:48 +0800
Subject: [PATCH] Remove hot-switch

---
 src/core/contexts/abstractwindowcontext.cpp |   41 +++----------
 src/widgets/widgetwindowagent_win.cpp       |    3 
 src/core/contexts/cocoawindowcontext.mm     |    2 
 src/core/contexts/abstractwindowcontext_p.h |   13 ----
 src/core/windowagentbase.h                  |    6 --
 src/core/contexts/cocoawindowcontext_p.h    |    2 
 src/core/windowagentbase.cpp                |   14 ----
 examples/mainwindow/light-style.qss         |   14 ++--
 src/core/contexts/qtwindowcontext_p.h       |    2 
 src/core/contexts/win32windowcontext.cpp    |   56 +++++++++---------
 src/core/contexts/win32windowcontext_p.h    |    2 
 examples/mainwindow/dark-style.qss          |    2 
 src/core/contexts/qtwindowcontext.cpp       |   10 --
 src/widgets/widgetwindowagent.h             |    1 
 examples/mainwindow/mainwindow.cpp          |    9 ---
 15 files changed, 54 insertions(+), 123 deletions(-)

diff --git a/examples/mainwindow/dark-style.qss b/examples/mainwindow/dark-style.qss
index 383eee6..095b900 100644
--- a/examples/mainwindow/dark-style.qss
+++ b/examples/mainwindow/dark-style.qss
@@ -104,7 +104,7 @@
 QMenu {
     padding: 4px;
     background: #303030;
-    border: 1.25px solid transparent;
+    border: 1px solid transparent;
 }
 
 QMenu::indicator {
diff --git a/examples/mainwindow/light-style.qss b/examples/mainwindow/light-style.qss
index b2b7287..eef4066 100644
--- a/examples/mainwindow/light-style.qss
+++ b/examples/mainwindow/light-style.qss
@@ -103,8 +103,8 @@
 
 QMenu {
     padding: 4px;
-    background: #303030;
-    border: 1.25px solid transparent;
+    background: white;
+    border: 1px solid #E0E0E0;
 }
 
 QMenu::indicator {
@@ -119,23 +119,21 @@
 
 QMenu::item {
     background: transparent;
-    color: #CCCCCC;
+    color: #333333;
     padding: 6px 24px;
 }
 
 QMenu::item:selected {
-    color: white;
-    background-color: #0060C0;
+    background-color: rgba(0, 0, 0, 10%);
 }
 
 QMenu::item:disabled {
-    color: #666666;
-    background-color: transparent;
+    color: #CCCCCC;
 }
 
 QMenu::separator {
     height: 2px;
-    background-color: #5B5B5B;
+    background-color: #CCCCCC;
     margin: 6px 0;
 }
 
diff --git a/examples/mainwindow/mainwindow.cpp b/examples/mainwindow/mainwindow.cpp
index e289c3e..52b2880 100644
--- a/examples/mainwindow/mainwindow.cpp
+++ b/examples/mainwindow/mainwindow.cpp
@@ -131,19 +131,10 @@
             darkAction->setChecked(currentTheme == Dark); //
         });
 
-        // Agent action
-        auto framelessOnAction = new QAction(tr("Enable Frameless"), menuBar);
-        framelessOnAction->setCheckable(true);
-        framelessOnAction->setChecked(true);
-        connect(framelessOnAction, &QAction::triggered, agent, &QWK::WindowAgentBase::setEnabled);
-        connect(agent, &QWK::WindowAgentBase::enabledChanged, framelessOnAction,
-                &QAction::setChecked);
-
         // Real menu
         auto settings = new QMenu(tr("Settings(&S)"), menuBar);
         settings->addAction(darkAction);
         settings->addSeparator();
-        settings->addAction(framelessOnAction);
 
         menuBar->addMenu(file);
         menuBar->addMenu(edit);
diff --git a/src/core/contexts/abstractwindowcontext.cpp b/src/core/contexts/abstractwindowcontext.cpp
index 535ffc2..193cae2 100644
--- a/src/core/contexts/abstractwindowcontext.cpp
+++ b/src/core/contexts/abstractwindowcontext.cpp
@@ -18,7 +18,11 @@
         }
         m_host = host;
         m_delegate.reset(delegate);
-        setEnabled(true);
+
+        m_windowHandle = m_delegate->window(m_host);
+        if (m_windowHandle) {
+            winIdChanged();
+        }
     }
 
     void AbstractWindowContext::setWindowAttribute(const QString &key, const QVariant &var) {
@@ -152,6 +156,9 @@
     void AbstractWindowContext::virtual_hook(int id, void *data) {
         switch (id) {
             case CentralizeHook: {
+                if (!m_windowHandle)
+                    return;
+
                 QRect screenGeometry = m_windowHandle->screen()->geometry();
                 int x = (screenGeometry.width() - m_windowHandle->width()) / 2;
                 int y = (screenGeometry.height() - m_windowHandle->height()) / 2;
@@ -190,39 +197,11 @@
     }
 
     void AbstractWindowContext::notifyWinIdChange() {
-        if (!m_internalEnabled)
-            return;
-
         auto oldWindow = m_windowHandle;
+        m_windowHandle = m_delegate->window(m_host);
         if (oldWindow == m_windowHandle)
             return;
-        auto isDestroyed = oldWindow && m_windowHandleCache.isNull();
-        m_windowHandle = m_delegate->window(m_host);
-        m_windowHandleCache = m_windowHandle;
-        winIdChanged(oldWindow, isDestroyed);
-    }
-
-    void AbstractWindowContext::setEnabled(bool enabled) {
-        if (enabled == m_internalEnabled)
-            return;
-        m_internalEnabled = enabled;
-
-        if (enabled) {
-            m_windowHandle = m_delegate->window(m_host);
-            m_windowHandleCache = m_windowHandle;
-            if (m_windowHandle) {
-                winIdChanged(nullptr, false);
-            }
-            return;
-        }
-
-        if (!m_windowHandle)
-            return;
-
-        auto oldWindow = m_windowHandle;
-        m_windowHandle = nullptr;
-        m_windowHandleCache.clear();
-        winIdChanged(oldWindow, false);
+        winIdChanged();
     }
 
 }
diff --git a/src/core/contexts/abstractwindowcontext_p.h b/src/core/contexts/abstractwindowcontext_p.h
index 1882369..e2a6c3f 100644
--- a/src/core/contexts/abstractwindowcontext_p.h
+++ b/src/core/contexts/abstractwindowcontext_p.h
@@ -73,11 +73,8 @@
         void showSystemMenu(const QPoint &pos);
         void notifyWinIdChange();
 
-        inline bool isEnabled() const;
-        void setEnabled(bool enabled);
-
     protected:
-        virtual void winIdChanged(QWindow *oldWindow, bool isDestroyed) = 0;
+        virtual void winIdChanged() = 0;
 
     protected:
         QObject *m_host{};
@@ -93,10 +90,6 @@
         std::array<QObject *, WindowAgentBase::NumSystemButton> m_systemButtons{};
 
         QVariantHash m_windowAttributes;
-
-    private:
-        bool m_internalEnabled = false;
-        QPointer<QWindow> m_windowHandleCache;
     };
 
     inline QObject *AbstractWindowContext::host() const {
@@ -133,10 +126,6 @@
         return m_systemButtonArea;
     }
 #endif
-
-    inline bool AbstractWindowContext::isEnabled() const {
-        return m_internalEnabled;
-    }
 
 }
 
diff --git a/src/core/contexts/cocoawindowcontext.mm b/src/core/contexts/cocoawindowcontext.mm
index c16c5a2..69ed562 100644
--- a/src/core/contexts/cocoawindowcontext.mm
+++ b/src/core/contexts/cocoawindowcontext.mm
@@ -403,7 +403,7 @@
         AbstractWindowContext::virtual_hook(id, data);
     }
 
-    void CocoaWindowContext::winIdChanged(QWindow *oldWindow, bool isDestroyed) {
+    void CocoaWindowContext::winIdChanged() {
         // If the original window id is valid, remove all resources related
         if (windowId) {
             releaseWindowProxy(windowId);
diff --git a/src/core/contexts/cocoawindowcontext_p.h b/src/core/contexts/cocoawindowcontext_p.h
index 0bbf661..e9d09d7 100644
--- a/src/core/contexts/cocoawindowcontext_p.h
+++ b/src/core/contexts/cocoawindowcontext_p.h
@@ -24,7 +24,7 @@
         void virtual_hook(int id, void *data) override;
 
     protected:
-        void winIdChanged(QWindow *oldWindow, bool isDestroyed) override;
+        void winIdChanged() override;
 
     protected:
         WId windowId = 0;
diff --git a/src/core/contexts/qtwindowcontext.cpp b/src/core/contexts/qtwindowcontext.cpp
index d0abb6d..9c05fa4 100644
--- a/src/core/contexts/qtwindowcontext.cpp
+++ b/src/core/contexts/qtwindowcontext.cpp
@@ -247,17 +247,11 @@
         AbstractWindowContext::virtual_hook(id, data);
     }
 
-    void QtWindowContext::winIdChanged(QWindow *oldWindow, bool isDestroyed) {
-        Q_UNUSED(isDestroyed)
-
-        // If the original window id is valid, remove all resources related
-        if (oldWindow) {
-            qtWindowEventFilter.reset();
-        }
-
+    void QtWindowContext::winIdChanged() {
         if (!m_windowHandle) {
             m_delegate->setWindowFlags(m_host, m_delegate->getWindowFlags(m_host) &
                                                    ~Qt::FramelessWindowHint);
+            qtWindowEventFilter.reset();
             return;
         }
 
diff --git a/src/core/contexts/qtwindowcontext_p.h b/src/core/contexts/qtwindowcontext_p.h
index c7d04eb..bb823da 100644
--- a/src/core/contexts/qtwindowcontext_p.h
+++ b/src/core/contexts/qtwindowcontext_p.h
@@ -24,7 +24,7 @@
         void virtual_hook(int id, void *data) override;
 
     protected:
-        void winIdChanged(QWindow *oldWindow, bool isDestroyed) override;
+        void winIdChanged() override;
 
     protected:
         std::unique_ptr<QObject> qtWindowEventFilter;
diff --git a/src/core/contexts/win32windowcontext.cpp b/src/core/contexts/win32windowcontext.cpp
index 7a8ad24..418a211 100644
--- a/src/core/contexts/win32windowcontext.cpp
+++ b/src/core/contexts/win32windowcontext.cpp
@@ -379,16 +379,7 @@
                getSystemMetricsForDpi(SM_CXPADDEDBORDER, dpi);
     }
 
-    static void updateInternalWindowFrameMargins(HWND hwnd, QWindow *window) {
-        const auto margins = [hwnd]() -> QMargins {
-            const auto titleBarHeight = int(getTitleBarHeight(hwnd));
-            if (isWin10OrGreater()) {
-                return {0, -titleBarHeight, 0, 0};
-            } else {
-                const auto frameSize = int(getResizeBorderThickness(hwnd));
-                return {-frameSize, -titleBarHeight, -frameSize, -frameSize};
-            }
-        }();
+    static void setInternalWindowFrameMargins(QWindow *window, const QMargins &margins) {
         const QVariant marginsVar = QVariant::fromValue(margins);
         window->setProperty("_q_windowsCustomMargins", marginsVar);
 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
@@ -829,7 +820,20 @@
         return ::CallWindowProcW(g_qtWindowProc, hWnd, message, wParam, lParam);
     }
 
-    static inline void addManagedWindow(HWND hWnd, Win32WindowContext *ctx) {
+    static inline void addManagedWindow(QWindow *window, HWND hWnd, Win32WindowContext *ctx) {
+        const auto margins = [hWnd]() -> QMargins {
+            const auto titleBarHeight = int(getTitleBarHeight(hWnd));
+            if (isWin10OrGreater()) {
+                return {0, -titleBarHeight, 0, 0};
+            } else {
+                const auto frameSize = int(getResizeBorderThickness(hWnd));
+                return {-frameSize, -titleBarHeight, -frameSize, -frameSize};
+            }
+        }();
+
+        // Inform Qt we want and have set custom margins
+        setInternalWindowFrameMargins(window, margins);
+
         // Store original window proc
         if (!g_qtWindowProc) {
             g_qtWindowProc = reinterpret_cast<WNDPROC>(::GetWindowLongPtrW(hWnd, GWLP_WNDPROC));
@@ -845,7 +849,6 @@
         g_wndProcHash->insert(hWnd, ctx);
     }
 
-    template <bool Destroyed = true>
     static inline void removeManagedWindow(HWND hWnd) {
         // Remove window handle mapping
         if (!g_wndProcHash->remove(hWnd))
@@ -854,11 +857,6 @@
         // Remove event filter if the all windows has been destroyed
         if (g_wndProcHash->empty()) {
             WindowsNativeEventFilter::uninstall();
-        }
-
-        // Restore window proc
-        if constexpr (!Destroyed) {
-            ::SetWindowLongPtrW(hWnd, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(g_qtWindowProc));
         }
     }
 
@@ -878,18 +876,24 @@
     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;
                 const auto hwnd = reinterpret_cast<HWND>(windowId);
                 bringWindowToFront(hwnd);
                 return;
             }
 
             case ShowSystemMenuHook: {
+                if (!windowId)
+                    return;
                 const auto &pos = *static_cast<const QPoint *>(data);
                 auto hWnd = reinterpret_cast<HWND>(windowId);
 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0)
@@ -931,6 +935,9 @@
             }
 
             case DrawWindows10BorderHook: {
+                if (!windowId)
+                    return;
+
                 auto args = static_cast<void **>(data);
                 auto &painter = *static_cast<QPainter *>(args[0]);
                 const auto &rect = *static_cast<const QRect *>(args[1]);
@@ -984,16 +991,10 @@
         return getWindowFrameBorderThickness(reinterpret_cast<HWND>(windowId));
     }
 
-    void Win32WindowContext::winIdChanged(QWindow *oldWindow, bool isDestroyed) {
-        Q_UNUSED(isDestroyed)
-
+    void Win32WindowContext::winIdChanged() {
         // If the original window id is valid, remove all resources related
         if (windowId) {
-            if (isDestroyed) {
-                removeManagedWindow(reinterpret_cast<HWND>(windowId));
-            } else {
-                removeManagedWindow<false>(reinterpret_cast<HWND>(windowId));
-            }
+            removeManagedWindow(reinterpret_cast<HWND>(windowId));
             windowId = 0;
         }
 
@@ -1015,11 +1016,8 @@
         }
 #endif
 
-        // Inform Qt we want and have set custom margins
-        updateInternalWindowFrameMargins(hWnd, m_windowHandle); // TODO: Restore?
-
         // Add managed window
-        addManagedWindow(hWnd, this);
+        addManagedWindow(m_windowHandle, hWnd, this);
 
         // Cache win id
         windowId = winId;
diff --git a/src/core/contexts/win32windowcontext_p.h b/src/core/contexts/win32windowcontext_p.h
index 698958f..982a89d 100644
--- a/src/core/contexts/win32windowcontext_p.h
+++ b/src/core/contexts/win32windowcontext_p.h
@@ -39,7 +39,7 @@
         int borderThickness() const;
 
     protected:
-        void winIdChanged(QWindow *oldWindow, bool isDestroyed) override;
+        void winIdChanged() override;
 
     public:
         bool windowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam, LRESULT *result);
diff --git a/src/core/windowagentbase.cpp b/src/core/windowagentbase.cpp
index 28c9945..c043838 100644
--- a/src/core/windowagentbase.cpp
+++ b/src/core/windowagentbase.cpp
@@ -60,20 +60,6 @@
         d->context->setWindowAttribute(key, var);
     }
 
-    bool WindowAgentBase::isEnabled() const {
-        Q_D(const WindowAgentBase);
-        return d->context->isEnabled();
-    }
-
-    void WindowAgentBase::setEnabled(bool enabled) {
-        Q_D(WindowAgentBase);
-        if (enabled == d->context->isEnabled()) {
-            return;
-        }
-        d->context->setEnabled(enabled);
-        Q_EMIT enabledChanged(enabled);
-    }
-
     void WindowAgentBase::showSystemMenu(const QPoint &pos) {
         Q_D(WindowAgentBase);
         d->context->showSystemMenu(pos);
diff --git a/src/core/windowagentbase.h b/src/core/windowagentbase.h
index 2524829..d5b02fa 100644
--- a/src/core/windowagentbase.h
+++ b/src/core/windowagentbase.h
@@ -31,16 +31,10 @@
         QVariant windowAttribute(const QString &key) const;
         void setWindowAttribute(const QString &key, const QVariant &var);
 
-        bool isEnabled() const;
-        void setEnabled(bool enabled);
-
     public Q_SLOTS:
         void showSystemMenu(const QPoint &pos);
         void centralize();
         void raise();
-
-    Q_SIGNALS:
-        void enabledChanged(bool enabled);
 
     protected:
         explicit WindowAgentBase(WindowAgentBasePrivate &d, QObject *parent = nullptr);
diff --git a/src/widgets/widgetwindowagent.h b/src/widgets/widgetwindowagent.h
index ea20025..1136812 100644
--- a/src/widgets/widgetwindowagent.h
+++ b/src/widgets/widgetwindowagent.h
@@ -26,6 +26,7 @@
         QWidget *systemButton(SystemButton button) const;
         void setSystemButton(SystemButton button, QWidget *w);
 
+        // Not implement, don't use now
 #ifdef Q_OS_MAC
         QWidget *systemButtonArea() const;
         void setSystemButtonArea(QWidget *widget);
diff --git a/src/widgets/widgetwindowagent_win.cpp b/src/widgets/widgetwindowagent_win.cpp
index 9197dca..1305ba7 100644
--- a/src/widgets/widgetwindowagent_win.cpp
+++ b/src/widgets/widgetwindowagent_win.cpp
@@ -67,7 +67,8 @@
             Q_UNUSED(obj)
             switch (event->type()) {
                 case QEvent::Paint: {
-                    if (widget->windowState() & (Qt::WindowMinimized | Qt::WindowMaximized | Qt::WindowFullScreen))
+                    if (widget->windowState() &
+                        (Qt::WindowMinimized | Qt::WindowMaximized | Qt::WindowFullScreen))
                         break;
 
                     auto paintEvent = static_cast<QPaintEvent *>(event);

--
Gitblit v1.9.1