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
framelesshelper_global.h
framelesshelper.h
framelesshelper.cpp
framelesshelper_qt.h
framelesshelper_qt.cpp
framelesswindowsmanager.h
framelesswindowsmanager_p.h
framelesswindowsmanager.cpp
@ -78,12 +78,12 @@ if(WIN32)
framelesshelper_windows.h
qwinregistry_p.h
qwinregistry.cpp
utilities_win32.cpp
framelesshelper_win32.h
framelesshelper_win32.cpp
utilities_win.cpp
framelesshelper_win.h
framelesshelper_win.cpp
)
elseif(APPLE)
list(APPEND SOURCES utilities_macos.mm)
list(APPEND SOURCES utilities_mac.cpp)
elseif(UNIX)
list(APPEND SOURCES utilities_linux.cpp)
endif()
@ -121,7 +121,7 @@ if(MSVC)
target_compile_definitions(${PROJECT_NAME} PRIVATE
_CRT_NON_CONFORMING_SWPRINTFS _CRT_SECURE_NO_WARNINGS
_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}
_WIN32_IE=${_WIN32_WINNT_WIN10} NTDDI_VERSION=${NTDDI_WIN10_CO}
)

View File

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

View File

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

View File

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

View File

@ -25,10 +25,10 @@
#include "framelesswindowsmanager.h"
#include "framelesswindowsmanager_p.h"
#include <QtGui/qscreen.h>
#include "framelesshelper.h"
#include "framelesshelper_qt.h"
#include "utilities.h"
#ifdef Q_OS_WINDOWS
# include "framelesshelper_win32.h"
# include "framelesshelper_win.h"
#endif
FRAMELESSHELPER_BEGIN_NAMESPACE
@ -151,10 +151,10 @@ void FramelessWindowsManager::addWindow(QWindow *window)
const QUuid uuid = QUuid::createUuid();
g_managerPrivate()->windowMapping.insert(window, uuid);
g_managerPrivate()->winIdMapping.insert(window->winId(), uuid);
FramelessHelper *qtFramelessHelper = nullptr;
FramelessHelperQt *qtFramelessHelper = nullptr;
if (g_usePureQtImplementation) {
// 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);
}
#ifdef Q_OS_WINDOWS

View File

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

16
lib.pro
View File

@ -14,12 +14,12 @@ DEFINES += \
FRAMELESSHELPER_BUILD_LIBRARY
HEADERS += \
framelesshelper_global.h \
framelesshelper.h \
framelesshelper_qt.h \
framelesswindowsmanager.h \
framelesswindowsmanager_p.h \
utilities.h
SOURCES += \
framelesshelper.cpp \
framelesshelper_qt.cpp \
framelesswindowsmanager.cpp \
utilities.cpp
qtHaveModule(quick) {
@ -28,16 +28,20 @@ qtHaveModule(quick) {
SOURCES += framelessquickhelper.cpp
}
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 += \
framelesshelper_windows.h \
framelesshelper_win32.h \
framelesshelper_win.h \
qwinregistry_p.h
SOURCES += \
utilities_win32.cpp \
framelesshelper_win32.cpp \
utilities_win.cpp \
framelesshelper_win.cpp \
qwinregistry.cpp
LIBS += -luser32 -lshell32 -ladvapi32
RC_FILE = framelesshelper.rc
}
linux*: SOURCES += utilities_linux.cpp
macx: SOURCES += utilities_macos.mm
macx: SOURCES += utilities_mac.cpp