From fe50d30edcc31618905ec3e3dc39c8b78cee49db Mon Sep 17 00:00:00 2001
From: Yuhang Zhao <zhaoyuhang@rankyee.com>
Date: 周二, 19 12月 2023 18:09:23 +0800
Subject: [PATCH] minor tweaks

---
 src/widgets/widgetitemdelegate.cpp          |    4 ++++
 src/widgets/widgetitemdelegate_p.h          |    1 +
 src/core/contexts/abstractwindowcontext.cpp |   20 ++++++++++++--------
 src/core/windowitemdelegate_p.h             |    1 +
 src/quick/quickitemdelegate_p.h             |    1 +
 src/quick/quickitemdelegate.cpp             |    4 ++++
 src/core/contexts/win32windowcontext.cpp    |   17 +----------------
 7 files changed, 24 insertions(+), 24 deletions(-)

diff --git a/src/core/contexts/abstractwindowcontext.cpp b/src/core/contexts/abstractwindowcontext.cpp
index 5ce7c8c..0602bdc 100644
--- a/src/core/contexts/abstractwindowcontext.cpp
+++ b/src/core/contexts/abstractwindowcontext.cpp
@@ -156,17 +156,21 @@
         switch (id) {
             case CentralizeHook: {
                 QRect screenGeometry = m_windowHandle->screen()->geometry();
-                int x = screenGeometry.width() / 2 - m_windowHandle->width() / 2;
-                int y = screenGeometry.height() / 2 - m_windowHandle->height() / 2;
-                m_windowHandle->setPosition(x, y);
-                break;
+                int x = (screenGeometry.width() - m_windowHandle->width()) / 2;
+                int y = (screenGeometry.height() - m_windowHandle->height()) / 2;
+                QPoint pos(x, y);
+                pos += screenGeometry.topLeft();
+                m_windowHandle->setPosition(pos);
+                return;
             }
 
             case RaiseWindowHook: {
-                if (m_windowHandle->windowStates() & Qt::WindowMinimized)
-                    m_windowHandle->showNormal();
-                m_windowHandle->raise();
-                break;
+                Qt::WindowStates state = m_delegate->getWindowState(m_host);
+                if (state & Qt::WindowMinimized) {
+                    m_delegate->setWindowState(m_host, state & ~Qt::WindowMinimized);
+                }
+                m_delegate->bringWindowToTop(m_host);
+                return;
             }
 
             case DefaultColorsHook: {
diff --git a/src/core/contexts/win32windowcontext.cpp b/src/core/contexts/win32windowcontext.cpp
index 9b3e179..0e84db1 100644
--- a/src/core/contexts/win32windowcontext.cpp
+++ b/src/core/contexts/win32windowcontext.cpp
@@ -813,22 +813,7 @@
             }
 
             case RaiseWindowHook: {
-                if (m_windowHandle->windowStates() & Qt::WindowMinimized)
-                    m_windowHandle->showNormal();
-
-                auto hWnd = reinterpret_cast<HWND>(windowId);
-
-                // I have no idea what this does, but it works mostly
-                // https://www.codeproject.com/Articles/1724/Some-handy-dialog-box-tricks-tips-and-workarounds
-
-                ::AttachThreadInput(::GetWindowThreadProcessId(::GetForegroundWindow(), nullptr),
-                                    ::GetCurrentThreadId(), TRUE);
-
-                ::SetForegroundWindow(hWnd);
-                ::SetFocus(hWnd);
-
-                ::AttachThreadInput(GetWindowThreadProcessId(GetForegroundWindow(), nullptr),
-                                    GetCurrentThreadId(), FALSE);
+                // FIXME
                 return;
             }
 
diff --git a/src/core/windowitemdelegate_p.h b/src/core/windowitemdelegate_p.h
index 8418c59..e0229bb 100644
--- a/src/core/windowitemdelegate_p.h
+++ b/src/core/windowitemdelegate_p.h
@@ -43,6 +43,7 @@
         virtual void setCursorShape(QObject *host, Qt::CursorShape shape) const = 0;
         virtual void restoreCursorShape(QObject *host) const = 0;
         virtual void setWindowFlags(QObject *host, Qt::WindowFlags flags) const = 0;
+        virtual void bringWindowToTop(QObject *host) const = 0;
 
     private:
         Q_DISABLE_COPY(WindowItemDelegate)
diff --git a/src/quick/quickitemdelegate.cpp b/src/quick/quickitemdelegate.cpp
index 4e64de3..b324d9e 100644
--- a/src/quick/quickitemdelegate.cpp
+++ b/src/quick/quickitemdelegate.cpp
@@ -66,4 +66,8 @@
         static_cast<QQuickWindow *>(host)->setFlags(flags);
     }
 
+    void QuickItemDelegate::bringWindowToTop(QObject *host) const {
+        static_cast<QQuickWindow *>(host)->raise();
+    }
+
 }
\ No newline at end of file
diff --git a/src/quick/quickitemdelegate_p.h b/src/quick/quickitemdelegate_p.h
index eb2430d..8d07ab7 100644
--- a/src/quick/quickitemdelegate_p.h
+++ b/src/quick/quickitemdelegate_p.h
@@ -39,6 +39,7 @@
         void setCursorShape(QObject *host, Qt::CursorShape shape) const override;
         void restoreCursorShape(QObject *host) const override;
         void setWindowFlags(QObject *host, Qt::WindowFlags flags) const override;
+        void bringWindowToTop(QObject *host) const override;
     };
 
 }
diff --git a/src/widgets/widgetitemdelegate.cpp b/src/widgets/widgetitemdelegate.cpp
index f6cb467..07c8aa3 100644
--- a/src/widgets/widgetitemdelegate.cpp
+++ b/src/widgets/widgetitemdelegate.cpp
@@ -98,4 +98,8 @@
         static_cast<QWidget *>(host)->setWindowFlags(flags);
     }
 
+    void WidgetItemDelegate::bringWindowToTop(QObject *host) const {
+        static_cast<QWidget *>(host)->raise();
+    }
+
 }
\ No newline at end of file
diff --git a/src/widgets/widgetitemdelegate_p.h b/src/widgets/widgetitemdelegate_p.h
index 6eb7fb3..ba7e73f 100644
--- a/src/widgets/widgetitemdelegate_p.h
+++ b/src/widgets/widgetitemdelegate_p.h
@@ -40,6 +40,7 @@
         void setCursorShape(QObject *host, Qt::CursorShape shape) const override;
         void restoreCursorShape(QObject *host) const override;
         void setWindowFlags(QObject *host, Qt::WindowFlags flags) const override;
+        void bringWindowToTop(QObject *host) const override;
     };
 
 }

--
Gitblit v1.9.1