Signed-off-by: Yuhang Zhao <2546789017@qq.com>
This commit is contained in:
Yuhang Zhao 2022-03-22 11:56:05 +08:00
parent f30d7a0f57
commit 6a43ef39f8
11 changed files with 94 additions and 26 deletions

View File

@ -78,6 +78,7 @@ FRAMELESSHELPER_CORE_API void startSystemResize(QWindow *window, const Qt::Edges
[[nodiscard]] FRAMELESSHELPER_CORE_API QVariant getSystemButtonIconResource [[nodiscard]] FRAMELESSHELPER_CORE_API QVariant getSystemButtonIconResource
(const SystemButtonType button, const SystemTheme theme, const ResourceType type); (const SystemButtonType button, const SystemTheme theme, const ResourceType type);
FRAMELESSHELPER_CORE_API void sendMouseReleaseEvent(); FRAMELESSHELPER_CORE_API void sendMouseReleaseEvent();
[[nodiscard]] FRAMELESSHELPER_CORE_API QWindow *findWindow(const WId winId);
#ifdef Q_OS_WINDOWS #ifdef Q_OS_WINDOWS
[[nodiscard]] FRAMELESSHELPER_CORE_API bool isWin8OrGreater(); [[nodiscard]] FRAMELESSHELPER_CORE_API bool isWin8OrGreater();
@ -93,7 +94,7 @@ FRAMELESSHELPER_CORE_API void updateInternalWindowFrameMargins(QWindow *window,
[[nodiscard]] FRAMELESSHELPER_CORE_API bool isFullScreen(const WId winId); [[nodiscard]] FRAMELESSHELPER_CORE_API bool isFullScreen(const WId winId);
[[nodiscard]] FRAMELESSHELPER_CORE_API bool isWindowNoState(const WId winId); [[nodiscard]] FRAMELESSHELPER_CORE_API bool isWindowNoState(const WId winId);
FRAMELESSHELPER_CORE_API void syncWmPaintWithDwm(); FRAMELESSHELPER_CORE_API void syncWmPaintWithDwm();
FRAMELESSHELPER_CORE_API void showSystemMenu(const WId winId, const QPoint &pos); FRAMELESSHELPER_CORE_API void showSystemMenu(const QWindow *window, const QPoint &pos);
[[nodiscard]] FRAMELESSHELPER_CORE_API QColor getDwmColorizationColor(); [[nodiscard]] FRAMELESSHELPER_CORE_API QColor getDwmColorizationColor();
[[nodiscard]] FRAMELESSHELPER_CORE_API bool shouldAppsUseDarkMode(); [[nodiscard]] FRAMELESSHELPER_CORE_API bool shouldAppsUseDarkMode();
[[nodiscard]] FRAMELESSHELPER_CORE_API DwmColorizationArea getDwmColorizationArea(); [[nodiscard]] FRAMELESSHELPER_CORE_API DwmColorizationArea getDwmColorizationArea();
@ -110,7 +111,7 @@ FRAMELESSHELPER_CORE_API void fixupQtInternals(const WId winId);
[[nodiscard]] FRAMELESSHELPER_CORE_API bool isWindowFrameBorderVisible(); [[nodiscard]] FRAMELESSHELPER_CORE_API bool isWindowFrameBorderVisible();
[[nodiscard]] FRAMELESSHELPER_CORE_API bool isTitleBarColorized(); [[nodiscard]] FRAMELESSHELPER_CORE_API bool isTitleBarColorized();
[[nodiscard]] FRAMELESSHELPER_CORE_API bool isFrameBorderColorized(); [[nodiscard]] FRAMELESSHELPER_CORE_API bool isFrameBorderColorized();
FRAMELESSHELPER_CORE_API void installSystemMenuHook(const WId winId); FRAMELESSHELPER_CORE_API void installSystemMenuHook(const QWindow *window);
FRAMELESSHELPER_CORE_API void uninstallSystemMenuHook(const WId winId); FRAMELESSHELPER_CORE_API void uninstallSystemMenuHook(const WId winId);
FRAMELESSHELPER_CORE_API void tryToBeCompatibleWithQtFramelessWindowHint(QWindow *window, const bool enable); FRAMELESSHELPER_CORE_API void tryToBeCompatibleWithQtFramelessWindowHint(QWindow *window, const bool enable);
#endif // Q_OS_WINDOWS #endif // Q_OS_WINDOWS

View File

@ -23,3 +23,20 @@
*/ */
#pragma once #pragma once
#include "framelesshelperquick_global.h"
#include <QtQuick/qquickwindow.h>
FRAMELESSHELPER_BEGIN_NAMESPACE
class FRAMELESSHELPER_QUICK_API FramelessQuickWindow : public QQuickWindow
{
Q_OBJECT
Q_DISABLE_COPY_MOVE(FramelessQuickWindow)
public:
explicit FramelessQuickWindow(QWindow *parent = nullptr);
~FramelessQuickWindow() override;
};
FRAMELESSHELPER_END_NAMESPACE

View File

@ -487,7 +487,7 @@ bool FramelessHelperWin::nativeEventFilter(const QByteArray &eventType, void *me
const bool full = Utils::isFullScreen(winId); const bool full = Utils::isFullScreen(winId);
const int frameSizeY = Utils::getResizeBorderThickness(winId, false, true); const int frameSizeY = Utils::getResizeBorderThickness(winId, false, true);
const bool isTop = (localPos.y < frameSizeY); const bool isTop = (localPos.y < frameSizeY);
const bool isTitleBar = (false || (options & Option::DisableDragging)); const bool isTitleBar = (false && !(options & Option::DisableDragging));
if (frameBorderVisible) { if (frameBorderVisible) {
// This will handle the left, right and bottom parts of the frame // This will handle the left, right and bottom parts of the frame
// because we didn't change them. // because we didn't change them.

View File

@ -140,6 +140,9 @@ WId FramelessWindowsManagerPrivate::findWinIdById(const QUuid &value) const
FramelessWindowsManager::FramelessWindowsManager(QObject *parent) : QObject(parent) FramelessWindowsManager::FramelessWindowsManager(QObject *parent) : QObject(parent)
{ {
d_ptr.reset(new FramelessWindowsManagerPrivate(this)); d_ptr.reset(new FramelessWindowsManagerPrivate(this));
if (!QCoreApplication::testAttribute(Qt::AA_DontCreateNativeWidgetSiblings)) {
QCoreApplication::setAttribute(Qt::AA_DontCreateNativeWidgetSiblings);
}
} }
FramelessWindowsManager::~FramelessWindowsManager() = default; FramelessWindowsManager::~FramelessWindowsManager() = default;
@ -194,7 +197,7 @@ void FramelessWindowsManager::addWindow(QWindow *window)
} }
const auto options = qvariant_cast<Options>(window->property(kInternalOptionsFlag)); const auto options = qvariant_cast<Options>(window->property(kInternalOptionsFlag));
if (!(options & Option::DontInstallSystemMenuHook)) { if (!(options & Option::DontInstallSystemMenuHook)) {
Utils::installSystemMenuHook(winId); Utils::installSystemMenuHook(window);
} }
#endif #endif
} }

View File

@ -25,6 +25,7 @@
#include "utils.h" #include "utils.h"
#include <QtCore/qvariant.h> #include <QtCore/qvariant.h>
#include <QtGui/qwindow.h> #include <QtGui/qwindow.h>
#include <QtGui/qguiapplication.h>
// The "Q_INIT_RESOURCE()" macro can't be used within a namespace, // The "Q_INIT_RESOURCE()" macro can't be used within a namespace,
// so we wrap it into a separate function outside of the namespace and // so we wrap it into a separate function outside of the namespace and
@ -150,4 +151,24 @@ QVariant Utils::getSystemButtonIconResource
return {}; return {};
} }
QWindow *Utils::findWindow(const WId winId)
{
Q_ASSERT(winId);
if (!winId) {
return nullptr;
}
const QWindowList windows = QGuiApplication::topLevelWindows();
if (windows.isEmpty()) {
return nullptr;
}
for (auto &&window : qAsConst(windows)) {
if (window && window->handle()) {
if (window->winId() == winId) {
return window;
}
}
}
return nullptr;
}
FRAMELESSHELPER_END_NAMESPACE FRAMELESSHELPER_END_NAMESPACE

View File

@ -57,6 +57,7 @@ struct Win32UtilsHelper
{ {
QMutex mutex = {}; QMutex mutex = {};
QHash<HWND, WNDPROC> qtWindowProcs = {}; QHash<HWND, WNDPROC> qtWindowProcs = {};
QHash<HWND, QWindow *> windowMapping = {};
explicit Win32UtilsHelper() = default; explicit Win32UtilsHelper() = default;
~Win32UtilsHelper() = default; ~Win32UtilsHelper() = default;
@ -181,6 +182,12 @@ static const QString successErrorText = QStringLiteral("The operation completed
g_utilsHelper()->mutex.unlock(); g_utilsHelper()->mutex.unlock();
return DefWindowProcW(hWnd, uMsg, wParam, lParam); return DefWindowProcW(hWnd, uMsg, wParam, lParam);
} }
const QWindow * const window = g_utilsHelper()->windowMapping.value(hWnd);
Q_ASSERT(window);
if (!window) {
g_utilsHelper()->mutex.unlock();
return DefWindowProcW(hWnd, uMsg, wParam, lParam);
}
g_utilsHelper()->mutex.unlock(); g_utilsHelper()->mutex.unlock();
const auto winId = reinterpret_cast<WId>(hWnd); const auto winId = reinterpret_cast<WId>(hWnd);
const auto getGlobalPosFromMouse = [lParam]() -> QPoint { const auto getGlobalPosFromMouse = [lParam]() -> QPoint {
@ -237,7 +244,7 @@ static const QString successErrorText = QStringLiteral("The operation completed
} }
} }
if (shouldShowSystemMenu) { if (shouldShowSystemMenu) {
Utils::showSystemMenu(winId, globalPos); Utils::showSystemMenu(window, globalPos);
// QPA's internal code will handle system menu events separately, and its // QPA's internal code will handle system menu events separately, and its
// behavior is not what we would want to see because it doesn't know our // behavior is not what we would want to see because it doesn't know our
// window doesn't have any window frame now, so return early here to avoid // window doesn't have any window frame now, so return early here to avoid
@ -487,13 +494,13 @@ DwmColorizationArea Utils::getDwmColorizationArea()
return DwmColorizationArea::None; return DwmColorizationArea::None;
} }
void Utils::showSystemMenu(const WId winId, const QPoint &pos) void Utils::showSystemMenu(const QWindow *window, const QPoint &pos)
{ {
Q_ASSERT(winId); Q_ASSERT(window);
if (!winId) { if (!window) {
return; return;
} }
const auto hWnd = reinterpret_cast<HWND>(winId); const auto hWnd = reinterpret_cast<HWND>(window->winId());
const HMENU menu = GetSystemMenu(hWnd, FALSE); const HMENU menu = GetSystemMenu(hWnd, FALSE);
if (!menu) { if (!menu) {
qWarning() << getSystemErrorMessage(QStringLiteral("GetSystemMenu")); qWarning() << getSystemErrorMessage(QStringLiteral("GetSystemMenu"));
@ -514,19 +521,21 @@ void Utils::showSystemMenu(const WId winId, const QPoint &pos)
return true; return true;
}; };
const bool maxOrFull = (IsMaximized(hWnd) || isFullScreen(reinterpret_cast<WId>(hWnd))); const bool maxOrFull = (IsMaximized(hWnd) || isFullScreen(reinterpret_cast<WId>(hWnd)));
if (!setState(SC_RESTORE, maxOrFull, true)) { const auto options = qvariant_cast<Options>(window->property(kInternalOptionsFlag));
const bool fixedSize = (isWindowFixedSize(window) || (options & Option::DisableResizing));
if (!setState(SC_RESTORE, (maxOrFull && !fixedSize), true)) {
return; return;
} }
if (!setState(SC_MOVE, !maxOrFull, false)) { if (!setState(SC_MOVE, (!maxOrFull && !(options & Option::DisableDragging)), false)) {
return; return;
} }
if (!setState(SC_SIZE, !maxOrFull, false)) { if (!setState(SC_SIZE, (!maxOrFull && !fixedSize), false)) {
return; return;
} }
if (!setState(SC_MINIMIZE, true, false)) { if (!setState(SC_MINIMIZE, true, false)) {
return; return;
} }
if (!setState(SC_MAXIMIZE, !maxOrFull, false)) { if (!setState(SC_MAXIMIZE, (!maxOrFull && !fixedSize), false)) {
return; return;
} }
if (!setState(SC_CLOSE, true, false)) { if (!setState(SC_CLOSE, true, false)) {
@ -557,7 +566,8 @@ bool Utils::isFullScreen(const WId winId)
qWarning() << getSystemErrorMessage(QStringLiteral("GetWindowRect")); qWarning() << getSystemErrorMessage(QStringLiteral("GetWindowRect"));
return false; return false;
} }
// According to Microsoft Docs, we should compare to primary screen's geometry. // According to Microsoft Docs, we should compare to the primary screen's geometry
// (if we can't determine the correct screen of our window).
const HMONITOR mon = MonitorFromWindow(hwnd, MONITOR_DEFAULTTOPRIMARY); const HMONITOR mon = MonitorFromWindow(hwnd, MONITOR_DEFAULTTOPRIMARY);
if (!mon) { if (!mon) {
qWarning() << getSystemErrorMessage(QStringLiteral("MonitorFromWindow")); qWarning() << getSystemErrorMessage(QStringLiteral("MonitorFromWindow"));
@ -1007,13 +1017,13 @@ bool Utils::isFrameBorderColorized()
return isTitleBarColorized(); return isTitleBarColorized();
} }
void Utils::installSystemMenuHook(const WId winId) void Utils::installSystemMenuHook(const QWindow *window)
{ {
Q_ASSERT(winId); Q_ASSERT(window);
if (!winId) { if (!window) {
return; return;
} }
const auto hwnd = reinterpret_cast<HWND>(winId); const auto hwnd = reinterpret_cast<HWND>(window->winId());
QMutexLocker locker(&g_utilsHelper()->mutex); QMutexLocker locker(&g_utilsHelper()->mutex);
if (g_utilsHelper()->qtWindowProcs.contains(hwnd)) { if (g_utilsHelper()->qtWindowProcs.contains(hwnd)) {
return; return;
@ -1030,8 +1040,9 @@ void Utils::installSystemMenuHook(const WId winId)
qWarning() << getSystemErrorMessage(QStringLiteral("SetWindowLongPtrW")); qWarning() << getSystemErrorMessage(QStringLiteral("SetWindowLongPtrW"));
return; return;
} }
g_utilsHelper()->qtWindowProcs.insert(hwnd, originalWindowProc);
//triggerFrameChange(winId); //triggerFrameChange(winId);
g_utilsHelper()->qtWindowProcs.insert(hwnd, originalWindowProc);
g_utilsHelper()->windowMapping.insert(hwnd, const_cast<QWindow *>(window));
} }
void Utils::uninstallSystemMenuHook(const WId winId) void Utils::uninstallSystemMenuHook(const WId winId)
@ -1055,8 +1066,9 @@ void Utils::uninstallSystemMenuHook(const WId winId)
qWarning() << getSystemErrorMessage(QStringLiteral("SetWindowLongPtrW")); qWarning() << getSystemErrorMessage(QStringLiteral("SetWindowLongPtrW"));
return; return;
} }
g_utilsHelper()->qtWindowProcs.remove(hwnd);
//triggerFrameChange(winId); //triggerFrameChange(winId);
g_utilsHelper()->qtWindowProcs.remove(hwnd);
g_utilsHelper()->windowMapping.remove(hwnd);
} }
void Utils::sendMouseReleaseEvent() void Utils::sendMouseReleaseEvent()

View File

@ -27,6 +27,7 @@
#include "framelesshelperimageprovider.h" #include "framelesshelperimageprovider.h"
#include "framelessquickhelper.h" #include "framelessquickhelper.h"
#include "framelessquickutils.h" #include "framelessquickutils.h"
#include "framelessquickwindow.h"
// The "Q_INIT_RESOURCE()" macro can't be used inside a namespace, // The "Q_INIT_RESOURCE()" macro can't be used inside a namespace,
// the official workaround is to wrap it into a global function // the official workaround is to wrap it into a global function
@ -64,6 +65,8 @@ void FramelessHelper::Quick::registerTypes(QQmlEngine *engine)
Q_UNUSED(scriptEngine); Q_UNUSED(scriptEngine);
return new FramelessQuickUtils; return new FramelessQuickUtils;
}); });
qmlRegisterType<FramelessQuickWindow>(FRAMELESSHELPER_QUICK_URI, 1, 0, "FramelessWindow");
qmlRegisterAnonymousType<QWindow, 254>(FRAMELESSHELPER_QUICK_URI, 1);
initResource(); initResource();
qmlRegisterType(getQmlFileUrl(QStringLiteral("MinimizeButton")), FRAMELESSHELPER_QUICK_URI, 1, 0, "MinimizeButton"); qmlRegisterType(getQmlFileUrl(QStringLiteral("MinimizeButton")), FRAMELESSHELPER_QUICK_URI, 1, 0, "MinimizeButton");
qmlRegisterType(getQmlFileUrl(QStringLiteral("MaximizeButton")), FRAMELESSHELPER_QUICK_URI, 1, 0, "MaximizeButton"); qmlRegisterType(getQmlFileUrl(QStringLiteral("MaximizeButton")), FRAMELESSHELPER_QUICK_URI, 1, 0, "MaximizeButton");

View File

@ -242,11 +242,11 @@ bool FramelessQuickEventFilter::eventFilter(QObject *object, QEvent *event)
const QPoint globalPos = mouseEvent->globalPos(); const QPoint globalPos = mouseEvent->globalPos();
#endif #endif
const QPoint nativePos = QPointF(QPointF(globalPos) * window->effectiveDevicePixelRatio()).toPoint(); const QPoint nativePos = QPointF(QPointF(globalPos) * window->effectiveDevicePixelRatio()).toPoint();
Utils::showSystemMenu(window->winId(), nativePos); Utils::showSystemMenu(window, nativePos);
return true; return true;
} }
case QEvent::MouseButtonDblClick: { case QEvent::MouseButtonDblClick: {
if (options & Option::NoDoubleClickMaximizeToggle) { if ((options & Option::NoDoubleClickMaximizeToggle) || (options & Option::DisableResizing)) {
return false; return false;
} }
if (button != Qt::LeftButton) { if (button != Qt::LeftButton) {

View File

@ -184,7 +184,7 @@ void FramelessQuickUtils::showSystemMenu(QQuickWindow *window, const QPoint &pos
const QPoint globalPos = window->mapToGlobal(pos); const QPoint globalPos = window->mapToGlobal(pos);
# endif # endif
const QPoint nativePos = QPointF(QPointF(globalPos) * window->effectiveDevicePixelRatio()).toPoint(); const QPoint nativePos = QPointF(QPointF(globalPos) * window->effectiveDevicePixelRatio()).toPoint();
Utils::showSystemMenu(window->winId(), nativePos); Utils::showSystemMenu(window, nativePos);
#endif #endif
} }

View File

@ -23,3 +23,11 @@
*/ */
#include "framelessquickwindow.h" #include "framelessquickwindow.h"
FRAMELESSHELPER_BEGIN_NAMESPACE
FramelessQuickWindow::FramelessQuickWindow(QWindow *parent) : QQuickWindow(parent) {}
FramelessQuickWindow::~FramelessQuickWindow() = default;
FRAMELESSHELPER_END_NAMESPACE

View File

@ -248,7 +248,7 @@ void FramelessWidgetsHelper::mouseReleaseEventHandler(QMouseEvent *event)
const QPoint globalPos = event->globalPos(); const QPoint globalPos = event->globalPos();
# endif # endif
const QPoint nativePos = QPointF(QPointF(globalPos) * q->devicePixelRatioF()).toPoint(); const QPoint nativePos = QPointF(QPointF(globalPos) * q->devicePixelRatioF()).toPoint();
Utils::showSystemMenu(q->winId(), nativePos); Utils::showSystemMenu(q->windowHandle(), nativePos);
#endif #endif
} }
@ -258,7 +258,7 @@ void FramelessWidgetsHelper::mouseDoubleClickEventHandler(QMouseEvent *event)
if (!event) { if (!event) {
return; return;
} }
if (m_options & Option::NoDoubleClickMaximizeToggle) { if ((m_options & Option::NoDoubleClickMaximizeToggle) || (m_options & Option::DisableResizing)) {
return; return;
} }
if (event->button() != Qt::LeftButton) { if (event->button() != Qt::LeftButton) {
@ -291,7 +291,7 @@ void FramelessWidgetsHelper::initialize()
q->setAttribute(Qt::WA_DontCreateNativeAncestors); q->setAttribute(Qt::WA_DontCreateNativeAncestors);
// Force the widget become a native window now so that we can deal with its // Force the widget become a native window now so that we can deal with its
// win32 events as soon as possible. // win32 events as soon as possible.
q->createWinId(); q->setAttribute(Qt::WA_NativeWindow);
QWindow * const window = q->windowHandle(); QWindow * const window = q->windowHandle();
Q_ASSERT(window); Q_ASSERT(window);
if (!window) { if (!window) {
@ -506,6 +506,9 @@ void FramelessWidgetsHelper::updateSystemButtonsIcon()
void FramelessWidgetsHelper::toggleMaximized() void FramelessWidgetsHelper::toggleMaximized()
{ {
if (m_options & Option::DisableResizing) {
return;
}
if (isZoomed()) { if (isZoomed()) {
q->showNormal(); q->showNormal();
} else { } else {