Signed-off-by: Yuhang Zhao <2546789017@qq.com>
This commit is contained in:
Yuhang Zhao 2022-03-16 10:32:53 +08:00
parent d6fe3fd8bd
commit 2180568efc
10 changed files with 53 additions and 49 deletions

View File

@ -57,8 +57,8 @@ find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Quick)
set(SOURCES set(SOURCES
framelesshelper_global.h framelesshelper_global.h
framelesshelper.h framelesshelper_qt.h
framelesshelper.cpp framelesshelper_qt.cpp
framelesswindowsmanager.h framelesswindowsmanager.h
framelesswindowsmanager_p.h framelesswindowsmanager_p.h
framelesswindowsmanager.cpp framelesswindowsmanager.cpp
@ -78,12 +78,12 @@ if(WIN32)
framelesshelper_windows.h framelesshelper_windows.h
qwinregistry_p.h qwinregistry_p.h
qwinregistry.cpp qwinregistry.cpp
utilities_win32.cpp utilities_win.cpp
framelesshelper_win32.h framelesshelper_win.h
framelesshelper_win32.cpp framelesshelper_win.cpp
) )
elseif(APPLE) elseif(APPLE)
list(APPEND SOURCES utilities_macos.mm) list(APPEND SOURCES utilities_mac.cpp)
elseif(UNIX) elseif(UNIX)
list(APPEND SOURCES utilities_linux.cpp) list(APPEND SOURCES utilities_linux.cpp)
endif() endif()
@ -121,7 +121,7 @@ if(MSVC)
target_compile_definitions(${PROJECT_NAME} PRIVATE target_compile_definitions(${PROJECT_NAME} PRIVATE
_CRT_NON_CONFORMING_SWPRINTFS _CRT_SECURE_NO_WARNINGS _CRT_NON_CONFORMING_SWPRINTFS _CRT_SECURE_NO_WARNINGS
_ENABLE_EXTENDED_ALIGNED_STORAGE NOMINMAX UNICODE _ENABLE_EXTENDED_ALIGNED_STORAGE NOMINMAX UNICODE
_UNICODE WIN32_LEAN_AND_MEAN WINRT_LEAN_AND_MEAN _DWMAPI_ _UNICODE WIN32_LEAN_AND_MEAN WINRT_LEAN_AND_MEAN
WINVER=${_WIN32_WINNT_WIN10} _WIN32_WINNT=${_WIN32_WINNT_WIN10} WINVER=${_WIN32_WINNT_WIN10} _WIN32_WINNT=${_WIN32_WINNT_WIN10}
_WIN32_IE=${_WIN32_WINNT_WIN10} NTDDI_VERSION=${NTDDI_WIN10_CO} _WIN32_IE=${_WIN32_WINNT_WIN10} NTDDI_VERSION=${NTDDI_WIN10_CO}
) )

View File

@ -22,7 +22,7 @@
* SOFTWARE. * SOFTWARE.
*/ */
#include "framelesshelper.h" #include "framelesshelper_qt.h"
#include <QtCore/qmutex.h> #include <QtCore/qmutex.h>
#include <QtGui/qevent.h> #include <QtGui/qevent.h>
#include <QtGui/qwindow.h> #include <QtGui/qwindow.h>
@ -30,59 +30,59 @@
FRAMELESSHELPER_BEGIN_NAMESPACE FRAMELESSHELPER_BEGIN_NAMESPACE
struct UnixHelper struct QtHelper
{ {
QMutex mutex = {}; QMutex mutex = {};
QWindowList acceptableWindows = {}; QWindowList acceptableWindows = {};
explicit UnixHelper() = default; explicit QtHelper() = default;
~UnixHelper() = default; ~QtHelper() = default;
private: private:
Q_DISABLE_COPY_MOVE(UnixHelper) Q_DISABLE_COPY_MOVE(QtHelper)
}; };
Q_GLOBAL_STATIC(UnixHelper, g_unixHelper) Q_GLOBAL_STATIC(QtHelper, g_qtHelper)
FramelessHelper::FramelessHelper(QObject *parent) : QObject(parent) {} FramelessHelperQt::FramelessHelperQt(QObject *parent) : QObject(parent) {}
FramelessHelper::~FramelessHelper() = default; FramelessHelperQt::~FramelessHelperQt() = default;
void FramelessHelper::addWindow(QWindow *window) void FramelessHelperQt::addWindow(QWindow *window)
{ {
Q_ASSERT(window); Q_ASSERT(window);
if (!window) { if (!window) {
return; return;
} }
g_unixHelper()->mutex.lock(); g_qtHelper()->mutex.lock();
if (g_unixHelper()->acceptableWindows.contains(window)) { if (g_qtHelper()->acceptableWindows.contains(window)) {
g_unixHelper()->mutex.unlock(); g_qtHelper()->mutex.unlock();
return; return;
} }
g_unixHelper()->acceptableWindows.append(window); g_qtHelper()->acceptableWindows.append(window);
g_unixHelper()->mutex.unlock(); g_qtHelper()->mutex.unlock();
window->setFlags(window->flags() | Qt::FramelessWindowHint); window->setFlags(window->flags() | Qt::FramelessWindowHint);
window->installEventFilter(this); window->installEventFilter(this);
} }
void FramelessHelper::removeWindow(QWindow *window) void FramelessHelperQt::removeWindow(QWindow *window)
{ {
Q_ASSERT(window); Q_ASSERT(window);
if (!window) { if (!window) {
return; return;
} }
g_unixHelper()->mutex.lock(); g_qtHelper()->mutex.lock();
if (!g_unixHelper()->acceptableWindows.contains(window)) { if (!g_qtHelper()->acceptableWindows.contains(window)) {
g_unixHelper()->mutex.unlock(); g_qtHelper()->mutex.unlock();
return; return;
} }
g_unixHelper()->acceptableWindows.removeAll(window); g_qtHelper()->acceptableWindows.removeAll(window);
g_unixHelper()->mutex.unlock(); g_qtHelper()->mutex.unlock();
window->removeEventFilter(this); window->removeEventFilter(this);
window->setFlags(window->flags() & ~Qt::FramelessWindowHint); window->setFlags(window->flags() & ~Qt::FramelessWindowHint);
} }
bool FramelessHelper::eventFilter(QObject *object, QEvent *event) bool FramelessHelperQt::eventFilter(QObject *object, QEvent *event)
{ {
Q_ASSERT(object); Q_ASSERT(object);
Q_ASSERT(event); Q_ASSERT(event);
@ -99,12 +99,12 @@ bool FramelessHelper::eventFilter(QObject *object, QEvent *event)
return false; return false;
} }
const auto window = qobject_cast<QWindow *>(object); const auto window = qobject_cast<QWindow *>(object);
g_unixHelper()->mutex.lock(); g_qtHelper()->mutex.lock();
if (!g_unixHelper()->acceptableWindows.contains(window)) { if (!g_qtHelper()->acceptableWindows.contains(window)) {
g_unixHelper()->mutex.unlock(); g_qtHelper()->mutex.unlock();
return false; return false;
} }
g_unixHelper()->mutex.unlock(); g_qtHelper()->mutex.unlock();
if (Utilities::isWindowFixedSize(window)) { if (Utilities::isWindowFixedSize(window)) {
return false; return false;
} }

View File

@ -33,14 +33,14 @@ QT_END_NAMESPACE
FRAMELESSHELPER_BEGIN_NAMESPACE FRAMELESSHELPER_BEGIN_NAMESPACE
class FRAMELESSHELPER_API FramelessHelper : public QObject class FRAMELESSHELPER_API FramelessHelperQt : public QObject
{ {
Q_OBJECT Q_OBJECT
Q_DISABLE_COPY_MOVE(FramelessHelper) Q_DISABLE_COPY_MOVE(FramelessHelperQt)
public: public:
explicit FramelessHelper(QObject *parent = nullptr); explicit FramelessHelperQt(QObject *parent = nullptr);
~FramelessHelper() override; ~FramelessHelperQt() override;
void addWindow(QWindow *window); void addWindow(QWindow *window);
void removeWindow(QWindow *window); void removeWindow(QWindow *window);

View File

@ -22,7 +22,7 @@
* SOFTWARE. * SOFTWARE.
*/ */
#include "framelesshelper_win32.h" #include "framelesshelper_win.h"
#include <QtCore/qdebug.h> #include <QtCore/qdebug.h>
#include <QtCore/qhash.h> #include <QtCore/qhash.h>
#include <QtCore/qmutex.h> #include <QtCore/qmutex.h>

View File

@ -25,10 +25,10 @@
#include "framelesswindowsmanager.h" #include "framelesswindowsmanager.h"
#include "framelesswindowsmanager_p.h" #include "framelesswindowsmanager_p.h"
#include <QtGui/qscreen.h> #include <QtGui/qscreen.h>
#include "framelesshelper.h" #include "framelesshelper_qt.h"
#include "utilities.h" #include "utilities.h"
#ifdef Q_OS_WINDOWS #ifdef Q_OS_WINDOWS
# include "framelesshelper_win32.h" # include "framelesshelper_win.h"
#endif #endif
FRAMELESSHELPER_BEGIN_NAMESPACE FRAMELESSHELPER_BEGIN_NAMESPACE
@ -151,10 +151,10 @@ void FramelessWindowsManager::addWindow(QWindow *window)
const QUuid uuid = QUuid::createUuid(); const QUuid uuid = QUuid::createUuid();
g_managerPrivate()->windowMapping.insert(window, uuid); g_managerPrivate()->windowMapping.insert(window, uuid);
g_managerPrivate()->winIdMapping.insert(window->winId(), uuid); g_managerPrivate()->winIdMapping.insert(window->winId(), uuid);
FramelessHelper *qtFramelessHelper = nullptr; FramelessHelperQt *qtFramelessHelper = nullptr;
if (g_usePureQtImplementation) { if (g_usePureQtImplementation) {
// Give it a parent so that it can be deleted even if we forget to do. // Give it a parent so that it can be deleted even if we forget to do.
qtFramelessHelper = new FramelessHelper(window); qtFramelessHelper = new FramelessHelperQt(window);
g_managerPrivate()->qtFramelessHelpers.insert(uuid, qtFramelessHelper); g_managerPrivate()->qtFramelessHelpers.insert(uuid, qtFramelessHelper);
} }
#ifdef Q_OS_WINDOWS #ifdef Q_OS_WINDOWS

View File

@ -32,7 +32,7 @@
FRAMELESSHELPER_BEGIN_NAMESPACE FRAMELESSHELPER_BEGIN_NAMESPACE
class FramelessHelper; class FramelessHelperQt;
class FRAMELESSHELPER_API FramelessManagerPrivate class FRAMELESSHELPER_API FramelessManagerPrivate
{ {
@ -56,7 +56,7 @@ private:
mutable QMutex mutex = {}; mutable QMutex mutex = {};
QHash<QWindow *, QUuid> windowMapping = {}; QHash<QWindow *, QUuid> windowMapping = {};
QHash<WId, QUuid> winIdMapping = {}; QHash<WId, QUuid> winIdMapping = {};
QHash<QUuid, FramelessHelper *> qtFramelessHelpers = {}; QHash<QUuid, FramelessHelperQt *> qtFramelessHelpers = {};
#ifdef Q_OS_WINDOWS #ifdef Q_OS_WINDOWS
QHash<QUuid, QMetaObject::Connection> win32WorkaroundConnections = {}; QHash<QUuid, QMetaObject::Connection> win32WorkaroundConnections = {};
#endif #endif

16
lib.pro
View File

@ -14,12 +14,12 @@ DEFINES += \
FRAMELESSHELPER_BUILD_LIBRARY FRAMELESSHELPER_BUILD_LIBRARY
HEADERS += \ HEADERS += \
framelesshelper_global.h \ framelesshelper_global.h \
framelesshelper.h \ framelesshelper_qt.h \
framelesswindowsmanager.h \ framelesswindowsmanager.h \
framelesswindowsmanager_p.h \ framelesswindowsmanager_p.h \
utilities.h utilities.h
SOURCES += \ SOURCES += \
framelesshelper.cpp \ framelesshelper_qt.cpp \
framelesswindowsmanager.cpp \ framelesswindowsmanager.cpp \
utilities.cpp utilities.cpp
qtHaveModule(quick) { qtHaveModule(quick) {
@ -28,16 +28,20 @@ qtHaveModule(quick) {
SOURCES += framelessquickhelper.cpp SOURCES += framelessquickhelper.cpp
} }
win32 { win32 {
DEFINES += \
_CRT_NON_CONFORMING_SWPRINTFS _CRT_SECURE_NO_WARNINGS \
_ENABLE_EXTENDED_ALIGNED_STORAGE NOMINMAX UNICODE \
_UNICODE WIN32_LEAN_AND_MEAN WINRT_LEAN_AND_MEAN
HEADERS += \ HEADERS += \
framelesshelper_windows.h \ framelesshelper_windows.h \
framelesshelper_win32.h \ framelesshelper_win.h \
qwinregistry_p.h qwinregistry_p.h
SOURCES += \ SOURCES += \
utilities_win32.cpp \ utilities_win.cpp \
framelesshelper_win32.cpp \ framelesshelper_win.cpp \
qwinregistry.cpp qwinregistry.cpp
LIBS += -luser32 -lshell32 -ladvapi32 LIBS += -luser32 -lshell32 -ladvapi32
RC_FILE = framelesshelper.rc RC_FILE = framelesshelper.rc
} }
linux*: SOURCES += utilities_linux.cpp linux*: SOURCES += utilities_linux.cpp
macx: SOURCES += utilities_macos.mm macx: SOURCES += utilities_mac.cpp