From 2b6d0fccb6a8c413c0f00df04b1577f62864d2c4 Mon Sep 17 00:00:00 2001 From: Yuhang Zhao <2546789017@qq.com> Date: Fri, 23 Jul 2021 11:04:48 +0800 Subject: [PATCH] Minor tweaks Signed-off-by: Yuhang Zhao <2546789017@qq.com> --- examples/mainwindow/mainwindow.cpp | 2 ++ examples/quick/main.cpp | 2 +- examples/widget/widget.cpp | 6 ++-- framelesshelper.cpp | 10 +++++-- framelesshelper.h | 6 ++++ framelesshelper_global.h | 44 +++++++++++++++++++++++------- framelesshelper_win32.cpp | 39 ++++++++++++++------------ framelesshelper_win32.h | 4 +++ framelessquickhelper.cpp | 4 +++ framelessquickhelper.h | 4 +++ framelesswindowsmanager.cpp | 32 ++++++++++++---------- framelesswindowsmanager.h | 4 +++ utilities.cpp | 8 ++++-- utilities.h | 7 ++++- utilities_win32.cpp | 16 +++++++---- 15 files changed, 133 insertions(+), 55 deletions(-) diff --git a/examples/mainwindow/mainwindow.cpp b/examples/mainwindow/mainwindow.cpp index 0ff2492..8340203 100644 --- a/examples/mainwindow/mainwindow.cpp +++ b/examples/mainwindow/mainwindow.cpp @@ -26,6 +26,8 @@ #include #include "../../framelesswindowsmanager.h" +FRAMELESSHELPER_USE_NAMESPACE + MainWindow::MainWindow(QWidget *parent, Qt::WindowFlags flags) : QMainWindow(parent, flags) { setAttribute(Qt::WA_DontCreateNativeAncestors); diff --git a/examples/quick/main.cpp b/examples/quick/main.cpp index a72d505..36617b2 100644 --- a/examples/quick/main.cpp +++ b/examples/quick/main.cpp @@ -60,7 +60,7 @@ int main(int argc, char *argv[]) QQuickStyle::setStyle(QStringLiteral("Default")); #endif - qmlRegisterType("wangwenx190.Utils", 1, 0, "FramelessHelper"); + qmlRegisterType("wangwenx190.Utils", 1, 0, "FramelessHelper"); const QUrl mainQmlUrl(QStringLiteral("qrc:///qml/main.qml")); const QMetaObject::Connection connection = QObject::connect( diff --git a/examples/widget/widget.cpp b/examples/widget/widget.cpp index 7a18731..691d1d7 100644 --- a/examples/widget/widget.cpp +++ b/examples/widget/widget.cpp @@ -32,6 +32,8 @@ #include "../../utilities.h" #include "../../framelesswindowsmanager.h" +FRAMELESSHELPER_USE_NAMESPACE + Widget::Widget(QWidget *parent) : QWidget(parent) { setAttribute(Qt::WA_DontCreateNativeAncestors); @@ -112,8 +114,8 @@ void Widget::paintEvent(QPaintEvent *event) void Widget::setupUi() { const QWindow *win = windowHandle(); - const int resizeBorderHeight = Utilities::getSystemMetric(win, Utilities::SystemMetric::ResizeBorderHeight, false); - const int titleBarHeight = Utilities::getSystemMetric(win, Utilities::SystemMetric::TitleBarHeight, false); + const int resizeBorderHeight = Utilities::getSystemMetric(win, SystemMetric::ResizeBorderHeight, false); + const int titleBarHeight = Utilities::getSystemMetric(win, SystemMetric::TitleBarHeight, false); const int systemButtonHeight = titleBarHeight + resizeBorderHeight; const QSize systemButtonSize = {qRound(static_cast(systemButtonHeight) * 1.5), systemButtonHeight}; m_minimizeButton = new QPushButton(this); diff --git a/framelesshelper.cpp b/framelesshelper.cpp index f32b319..b22bca8 100644 --- a/framelesshelper.cpp +++ b/framelesshelper.cpp @@ -25,12 +25,15 @@ #include "framelesshelper.h" #if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)) + #include "utilities.h" #include "framelesswindowsmanager.h" #include #include #include +FRAMELESSHELPER_BEGIN_NAMESPACE + FramelessHelper::FramelessHelper(QObject *parent) : QObject(parent) {} void FramelessHelper::removeWindowFrame(QWindow *window) @@ -41,7 +44,7 @@ void FramelessHelper::removeWindowFrame(QWindow *window) } window->setFlags(window->flags() | Qt::FramelessWindowHint); window->installEventFilter(this); - window->setProperty(_flh_global::_flh_framelessEnabled_flag, true); + window->setProperty(Constants::framelessMode_flag, true); } void FramelessHelper::bringBackWindowFrame(QWindow *window) @@ -52,7 +55,7 @@ void FramelessHelper::bringBackWindowFrame(QWindow *window) } window->removeEventFilter(this); window->setFlags(window->flags() & ~Qt::FramelessWindowHint); - window->setProperty(_flh_global::_flh_framelessEnabled_flag, false); + window->setProperty(Constants::framelessMode_flag, false); } bool FramelessHelper::eventFilter(QObject *object, QEvent *event) @@ -166,4 +169,7 @@ bool FramelessHelper::eventFilter(QObject *object, QEvent *event) } return false; } + +FRAMELESSHELPER_END_NAMESPACE + #endif diff --git a/framelesshelper.h b/framelesshelper.h index 98c28c2..5b7dd86 100644 --- a/framelesshelper.h +++ b/framelesshelper.h @@ -27,12 +27,15 @@ #include "framelesshelper_global.h" #if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)) + #include QT_BEGIN_NAMESPACE QT_FORWARD_DECLARE_CLASS(QWindow) QT_END_NAMESPACE +FRAMELESSHELPER_BEGIN_NAMESPACE + class FRAMELESSHELPER_API FramelessHelper : public QObject { Q_OBJECT @@ -48,4 +51,7 @@ public: protected: bool eventFilter(QObject *object, QEvent *event) override; }; + +FRAMELESSHELPER_END_NAMESPACE + #endif diff --git a/framelesshelper_global.h b/framelesshelper_global.h index b9e4982..191c5c3 100644 --- a/framelesshelper_global.h +++ b/framelesshelper_global.h @@ -63,17 +63,41 @@ #define Q_NODISCARD #endif -namespace _flh_global +#ifndef FRAMELESSHELPER_NAMESPACE +#define FRAMELESSHELPER_NAMESPACE __flh_ns +#endif + +#ifndef FRAMELESSHELPER_BEGIN_NAMESPACE +#define FRAMELESSHELPER_BEGIN_NAMESPACE namespace FRAMELESSHELPER_NAMESPACE { +#endif + +#ifndef FRAMELESSHELPER_END_NAMESPACE +#define FRAMELESSHELPER_END_NAMESPACE } +#endif + +#ifndef FRAMELESSHELPER_USE_NAMESPACE +#define FRAMELESSHELPER_USE_NAMESPACE using namespace FRAMELESSHELPER_NAMESPACE; +#endif + +#ifndef FRAMELESSHELPER_PREPEND_NAMESPACE +#define FRAMELESSHELPER_PREPEND_NAMESPACE(X) ::FRAMELESSHELPER_NAMESPACE::X +#endif + +FRAMELESSHELPER_BEGIN_NAMESPACE + +namespace Constants { -[[maybe_unused]] const char _flh_framelessEnabled_flag[] = "_FRAMELESSHELPER_FRAMELESS_MODE_ENABLED"; -[[maybe_unused]] const char _flh_resizeBorderWidth_flag[] = "_FRAMELESSHELPER_WINDOW_RESIZE_BORDER_WIDTH"; -[[maybe_unused]] const char _flh_resizeBorderHeight_flag[] = "_FRAMELESSHELPER_WINDOW_RESIZE_BORDER_HEIGHT"; -[[maybe_unused]] const char _flh_titleBarHeight_flag[] = "_FRAMELESSHELPER_WINDOW_TITLE_BAR_HEIGHT"; -[[maybe_unused]] const char _flh_hitTestVisibleInChrome_flag[] = "_FRAMELESSHELPER_HIT_TEST_VISIBLE_IN_CHROME"; -[[maybe_unused]] const char _flh_useNativeTitleBar_flag[] = "_FRAMELESSHELPER_USE_NATIVE_TITLE_BAR"; -[[maybe_unused]] const char _flh_preserveNativeFrame_flag[] = "_FRAMELESSHELPER_PRESERVE_NATIVE_WINDOW_FRAME"; -[[maybe_unused]] const char _flh_forcePreserveNativeFrame_flag[] = "_FRAMELESSHELPER_FORCE_PRESERVE_NATIVE_WINDOW_FRAME"; -[[maybe_unused]] const char _flh_windowFixedSize_flag[] = "_FRAMELESSHELPER_WINDOW_FIXED_SIZE"; +[[maybe_unused]] const char framelessMode_flag[] = "_FRAMELESSHELPER_FRAMELESS_MODE"; +[[maybe_unused]] const char resizeBorderWidth_flag[] = "_FRAMELESSHELPER_RESIZE_BORDER_WIDTH"; +[[maybe_unused]] const char resizeBorderHeight_flag[] = "_FRAMELESSHELPER_RESIZE_BORDER_HEIGHT"; +[[maybe_unused]] const char titleBarHeight_flag[] = "_FRAMELESSHELPER_TITLE_BAR_HEIGHT"; +[[maybe_unused]] const char hitTestVisibleInChrome_flag[] = "_FRAMELESSHELPER_HIT_TEST_VISIBLE_IN_CHROME"; +[[maybe_unused]] const char useNativeTitleBar_flag[] = "_FRAMELESSHELPER_USE_NATIVE_TITLE_BAR"; +[[maybe_unused]] const char preserveNativeFrame_flag[] = "_FRAMELESSHELPER_PRESERVE_NATIVE_WINDOW_FRAME"; +[[maybe_unused]] const char forcePreserveNativeFrame_flag[] = "_FRAMELESSHELPER_FORCE_PRESERVE_NATIVE_WINDOW_FRAME"; +[[maybe_unused]] const char windowFixedSize_flag[] = "_FRAMELESSHELPER_WINDOW_FIXED_SIZE"; } + +FRAMELESSHELPER_END_NAMESPACE diff --git a/framelesshelper_win32.cpp b/framelesshelper_win32.cpp index c0c04b6..03ccc79 100644 --- a/framelesshelper_win32.cpp +++ b/framelesshelper_win32.cpp @@ -48,24 +48,26 @@ #ifndef IsMinimized // Only available since Windows 2000 -#define IsMinimized(h) IsIconic(h) +#define IsMinimized(window) IsIconic(window) #endif #ifndef IsMaximized // Only available since Windows 2000 -#define IsMaximized(h) IsZoomed(h) +#define IsMaximized(window) IsZoomed(window) #endif #ifndef GET_X_LPARAM // Only available since Windows 2000 -#define GET_X_LPARAM(lp) ((int) (short) LOWORD(lp)) +#define GET_X_LPARAM(lp) ((int)(short)LOWORD(lp)) #endif #ifndef GET_Y_LPARAM // Only available since Windows 2000 -#define GET_Y_LPARAM(lp) ((int) (short) HIWORD(lp)) +#define GET_Y_LPARAM(lp) ((int)(short)HIWORD(lp)) #endif +FRAMELESSHELPER_BEGIN_NAMESPACE + [[nodiscard]] static inline bool shouldHaveWindowFrame() { if (Utilities::shouldUseNativeTitleBar()) { @@ -73,8 +75,8 @@ // want to use the native title bar. return true; } - const bool should = qEnvironmentVariableIsSet(_flh_global::_flh_preserveNativeFrame_flag); - const bool force = qEnvironmentVariableIsSet(_flh_global::_flh_forcePreserveNativeFrame_flag); + const bool should = qEnvironmentVariableIsSet(Constants::preserveNativeFrame_flag); + const bool force = qEnvironmentVariableIsSet(Constants::forcePreserveNativeFrame_flag); if (should || force) { if (force) { return true; @@ -112,7 +114,7 @@ static inline void installHelper(QWindow *window, const bool enable) const WId winId = window->winId(); Utilities::updateFrameMargins(winId, !enable); Utilities::triggerFrameChange(winId); - window->setProperty(_flh_global::_flh_framelessEnabled_flag, enable); + window->setProperty(Constants::framelessMode_flag, enable); } FramelessHelperWin::FramelessHelperWin() = default; @@ -164,7 +166,7 @@ bool FramelessHelperWin::nativeEventFilter(const QByteArray &eventType, void *me return false; } const QWindow *window = Utilities::findWindow(reinterpret_cast(msg->hwnd)); - if (!window || !window->property(_flh_global::_flh_framelessEnabled_flag).toBool()) { + if (!window || !window->property(Constants::framelessMode_flag).toBool()) { return false; } switch (msg->message) { @@ -254,7 +256,7 @@ bool FramelessHelperWin::nativeEventFilter(const QByteArray &eventType, void *me // applies the default frame. const LONG originalTop = clientRect->top; // Apply the default frame - const LRESULT ret = DefWindowProcW(msg->hwnd, WM_NCCALCSIZE, msg->wParam, msg->lParam); + const LRESULT ret = DefWindowProcW(msg->hwnd, msg->message, msg->wParam, msg->lParam); if (ret != 0) { *result = ret; return true; @@ -274,11 +276,11 @@ bool FramelessHelperWin::nativeEventFilter(const QByteArray &eventType, void *me // The value of border width and border height should be // identical in most cases, when the scale factor is 1.0, it // should be eight pixels. - const int rbh = getSystemMetric(window, Utilities::SystemMetric::ResizeBorderHeight, true); + const int rbh = Utilities::getSystemMetric(window, SystemMetric::ResizeBorderHeight, true); clientRect->top += rbh; if (!shouldHaveWindowFrame()) { clientRect->bottom -= rbh; - const int rbw = getSystemMetric(window, Utilities::SystemMetric::ResizeBorderWidth, true); + const int rbw = Utilities::getSystemMetric(window, SystemMetric::ResizeBorderWidth, true); clientRect->left += rbw; clientRect->right -= rbw; } @@ -522,9 +524,9 @@ bool FramelessHelperWin::nativeEventFilter(const QByteArray &eventType, void *me RECT clientRect = {0, 0, 0, 0}; GetClientRect(msg->hwnd, &clientRect); const LONG ww = clientRect.right; - const int rbw = getSystemMetric(window, Utilities::SystemMetric::ResizeBorderWidth, true); - const int rbh = getSystemMetric(window, Utilities::SystemMetric::ResizeBorderHeight, true); - const int tbh = getSystemMetric(window, Utilities::SystemMetric::TitleBarHeight, true); + const int rbw = Utilities::getSystemMetric(window, SystemMetric::ResizeBorderWidth, true); + const int rbh = Utilities::getSystemMetric(window, SystemMetric::ResizeBorderHeight, true); + const int tbh = Utilities::getSystemMetric(window, SystemMetric::TitleBarHeight, true); const bool isTitleBar = (localMouse.y() > rbh) && (localMouse.y() <= (rbh + tbh)) && (localMouse.x() > rbw) && (localMouse.y() < (ww - rbw)) && !Utilities::isHitTestVisibleInChrome(window); @@ -532,7 +534,7 @@ bool FramelessHelperWin::nativeEventFilter(const QByteArray &eventType, void *me if (shouldHaveWindowFrame()) { // This will handle the left, right and bottom parts of the frame // because we didn't change them. - const LRESULT originalRet = DefWindowProcW(msg->hwnd, WM_NCHITTEST, msg->wParam, msg->lParam); + const LRESULT originalRet = DefWindowProcW(msg->hwnd, msg->message, msg->wParam, msg->lParam); if (originalRet != HTCLIENT) { *result = originalRet; return true; @@ -615,10 +617,11 @@ bool FramelessHelperWin::nativeEventFilter(const QByteArray &eventType, void *me // Prevent Windows from drawing the default title bar by temporarily // toggling the WS_VISIBLE style. SetWindowLongPtrW(msg->hwnd, GWL_STYLE, oldStyle & ~WS_VISIBLE); - Utilities::triggerFrameChange(reinterpret_cast(msg->hwnd)); + const WId winId = window->winId(); + Utilities::triggerFrameChange(winId); const LRESULT ret = DefWindowProcW(msg->hwnd, msg->message, msg->wParam, msg->lParam); SetWindowLongPtrW(msg->hwnd, GWL_STYLE, oldStyle); - Utilities::triggerFrameChange(reinterpret_cast(msg->hwnd)); + Utilities::triggerFrameChange(winId); *result = ret; return true; } @@ -627,3 +630,5 @@ bool FramelessHelperWin::nativeEventFilter(const QByteArray &eventType, void *me } return false; } + +FRAMELESSHELPER_END_NAMESPACE diff --git a/framelesshelper_win32.h b/framelesshelper_win32.h index 540373e..e320bcf 100644 --- a/framelesshelper_win32.h +++ b/framelesshelper_win32.h @@ -32,6 +32,8 @@ QT_BEGIN_NAMESPACE QT_FORWARD_DECLARE_CLASS(QWindow) QT_END_NAMESPACE +FRAMELESSHELPER_BEGIN_NAMESPACE + class FRAMELESSHELPER_API FramelessHelperWin : public QAbstractNativeEventFilter { Q_DISABLE_COPY_MOVE(FramelessHelperWin) @@ -49,3 +51,5 @@ public: bool nativeEventFilter(const QByteArray &eventType, void *message, long *result) override; #endif }; + +FRAMELESSHELPER_END_NAMESPACE diff --git a/framelessquickhelper.cpp b/framelessquickhelper.cpp index 3cce28a..8259295 100644 --- a/framelessquickhelper.cpp +++ b/framelessquickhelper.cpp @@ -26,6 +26,8 @@ #include "framelesswindowsmanager.h" #include +FRAMELESSHELPER_BEGIN_NAMESPACE + FramelessQuickHelper::FramelessQuickHelper(QQuickItem *parent) : QQuickItem(parent) { } @@ -97,3 +99,5 @@ void FramelessQuickHelper::setHitTestVisibleInChrome(QQuickItem *item, const boo } FramelessWindowsManager::setHitTestVisibleInChrome(window(), item, visible); } + +FRAMELESSHELPER_END_NAMESPACE diff --git a/framelessquickhelper.h b/framelessquickhelper.h index 98ef01e..986ddcd 100644 --- a/framelessquickhelper.h +++ b/framelessquickhelper.h @@ -27,6 +27,8 @@ #include "framelesshelper_global.h" #include +FRAMELESSHELPER_BEGIN_NAMESPACE + class FRAMELESSHELPER_API FramelessQuickHelper : public QQuickItem { Q_OBJECT @@ -67,3 +69,5 @@ Q_SIGNALS: void titleBarHeightChanged(qreal); void resizableChanged(bool); }; + +FRAMELESSHELPER_END_NAMESPACE diff --git a/framelesswindowsmanager.cpp b/framelesswindowsmanager.cpp index 9ea6c55..f9fbb44 100644 --- a/framelesswindowsmanager.cpp +++ b/framelesswindowsmanager.cpp @@ -35,6 +35,8 @@ #include "framelesshelper_win32.h" #endif +FRAMELESSHELPER_BEGIN_NAMESPACE + #ifdef FRAMELESSHELPER_USE_UNIX_VERSION Q_GLOBAL_STATIC(FramelessHelper, framelessHelperUnix) #endif @@ -71,7 +73,7 @@ void FramelessWindowsManager::setHitTestVisibleInChrome(QWindow *window, QObject qWarning() << object << "is not a QWidget or QQuickItem."; return; } - auto objList = qvariant_cast(window->property(_flh_global::_flh_hitTestVisibleInChrome_flag)); + auto objList = qvariant_cast(window->property(Constants::hitTestVisibleInChrome_flag)); if (value) { if (objList.isEmpty() || !objList.contains(object)) { objList.append(object); @@ -81,7 +83,7 @@ void FramelessWindowsManager::setHitTestVisibleInChrome(QWindow *window, QObject objList.removeAll(object); } } - window->setProperty(_flh_global::_flh_hitTestVisibleInChrome_flag, QVariant::fromValue(objList)); + window->setProperty(Constants::hitTestVisibleInChrome_flag, QVariant::fromValue(objList)); } int FramelessWindowsManager::getResizeBorderWidth(const QWindow *window) @@ -91,10 +93,10 @@ int FramelessWindowsManager::getResizeBorderWidth(const QWindow *window) return 8; } #ifdef FRAMELESSHELPER_USE_UNIX_VERSION - const int value = window->property(_flh_global::_flh_resizeBorderWidth_flag).toInt(); + const int value = window->property(Constants::resizeBorderWidth_flag).toInt(); return value <= 0 ? 8 : value; #else - return Utilities::getSystemMetric(window, Utilities::SystemMetric::ResizeBorderWidth, false); + return Utilities::getSystemMetric(window, SystemMetric::ResizeBorderWidth, false); #endif } @@ -104,7 +106,7 @@ void FramelessWindowsManager::setResizeBorderWidth(QWindow *window, const int va if (!window || (value <= 0)) { return; } - window->setProperty(_flh_global::_flh_resizeBorderWidth_flag, value); + window->setProperty(Constants::resizeBorderWidth_flag, value); } int FramelessWindowsManager::getResizeBorderHeight(const QWindow *window) @@ -114,10 +116,10 @@ int FramelessWindowsManager::getResizeBorderHeight(const QWindow *window) return 8; } #ifdef FRAMELESSHELPER_USE_UNIX_VERSION - const int value = window->property(_flh_global::_flh_resizeBorderHeight_flag).toInt(); + const int value = window->property(Constants::resizeBorderHeight_flag).toInt(); return value <= 0 ? 8 : value; #else - return Utilities::getSystemMetric(window, Utilities::SystemMetric::ResizeBorderHeight, false); + return Utilities::getSystemMetric(window, SystemMetric::ResizeBorderHeight, false); #endif } @@ -127,7 +129,7 @@ void FramelessWindowsManager::setResizeBorderHeight(QWindow *window, const int v if (!window || (value <= 0)) { return; } - window->setProperty(_flh_global::_flh_resizeBorderHeight_flag, value); + window->setProperty(Constants::resizeBorderHeight_flag, value); } int FramelessWindowsManager::getTitleBarHeight(const QWindow *window) @@ -137,10 +139,10 @@ int FramelessWindowsManager::getTitleBarHeight(const QWindow *window) return 23; } #ifdef FRAMELESSHELPER_USE_UNIX_VERSION - const int value = window->property(_flh_global::_flh_titleBarHeight_flag).toInt(); + const int value = window->property(Constants::titleBarHeight_flag).toInt(); return value <= 0 ? 23 : value; #else - return Utilities::getSystemMetric(window, Utilities::SystemMetric::TitleBarHeight, false); + return Utilities::getSystemMetric(window, SystemMetric::TitleBarHeight, false); #endif } @@ -150,7 +152,7 @@ void FramelessWindowsManager::setTitleBarHeight(QWindow *window, const int value if (!window || (value <= 0)) { return; } - window->setProperty(_flh_global::_flh_titleBarHeight_flag, value); + window->setProperty(Constants::titleBarHeight_flag, value); } bool FramelessWindowsManager::getResizable(const QWindow *window) @@ -160,7 +162,7 @@ bool FramelessWindowsManager::getResizable(const QWindow *window) return false; } #ifdef FRAMELESSHELPER_USE_UNIX_VERSION - return !window->property(_flh_global::_flh_windowFixedSize_flag).toBool(); + return !window->property(Constants::windowFixedSize_flag).toBool(); #else return !Utilities::isWindowFixedSize(window); #endif @@ -173,7 +175,7 @@ void FramelessWindowsManager::setResizable(QWindow *window, const bool value) return; } #ifdef FRAMELESSHELPER_USE_UNIX_VERSION - window->setProperty(_flh_global::_flh_windowFixedSize_flag, !value); + window->setProperty(Constants::windowFixedSize_flag, !value); #else window->setFlag(Qt::MSWindowsFixedSizeDialogHint, !value); #endif @@ -198,5 +200,7 @@ bool FramelessWindowsManager::isWindowFrameless(const QWindow *window) if (!window) { return false; } - return window->property(_flh_global::_flh_framelessEnabled_flag).toBool(); + return window->property(Constants::framelessMode_flag).toBool(); } + +FRAMELESSHELPER_END_NAMESPACE diff --git a/framelesswindowsmanager.h b/framelesswindowsmanager.h index 724057c..1c93ac9 100644 --- a/framelesswindowsmanager.h +++ b/framelesswindowsmanager.h @@ -31,6 +31,8 @@ QT_FORWARD_DECLARE_CLASS(QObject) QT_FORWARD_DECLARE_CLASS(QWindow) QT_END_NAMESPACE +FRAMELESSHELPER_BEGIN_NAMESPACE + namespace FramelessWindowsManager { @@ -48,3 +50,5 @@ FRAMELESSHELPER_API void setTitleBarHeight(QWindow *window, const int value); FRAMELESSHELPER_API void setResizable(QWindow *window, const bool value = true); } + +FRAMELESSHELPER_END_NAMESPACE diff --git a/utilities.cpp b/utilities.cpp index fc38c28..3ff9685 100644 --- a/utilities.cpp +++ b/utilities.cpp @@ -28,6 +28,8 @@ #include #include +FRAMELESSHELPER_BEGIN_NAMESPACE + QWindow *Utilities::findWindow(const WId winId) { Q_ASSERT(winId); @@ -47,7 +49,7 @@ QWindow *Utilities::findWindow(const WId winId) bool Utilities::shouldUseNativeTitleBar() { - return qEnvironmentVariableIsSet(_flh_global::_flh_useNativeTitleBar_flag); + return qEnvironmentVariableIsSet(Constants::useNativeTitleBar_flag); } bool Utilities::isWindowFixedSize(const QWindow *window) @@ -84,7 +86,7 @@ bool Utilities::isHitTestVisibleInChrome(const QWindow *window) if (!window) { return false; } - const auto objs = qvariant_cast(window->property(_flh_global::_flh_hitTestVisibleInChrome_flag)); + const auto objs = qvariant_cast(window->property(Constants::hitTestVisibleInChrome_flag)); if (objs.isEmpty()) { return false; } @@ -126,3 +128,5 @@ QPointF Utilities::mapOriginPointToWindow(const QObject *object) } return point; } + +FRAMELESSHELPER_END_NAMESPACE diff --git a/utilities.h b/utilities.h index 787b2a5..fdd5f54 100644 --- a/utilities.h +++ b/utilities.h @@ -27,7 +27,7 @@ #include "framelesshelper_global.h" #include -namespace Utilities { +FRAMELESSHELPER_BEGIN_NAMESPACE enum class SystemMetric { @@ -36,6 +36,9 @@ enum class SystemMetric TitleBarHeight }; +namespace Utilities +{ + [[nodiscard]] FRAMELESSHELPER_API int getSystemMetric(const QWindow *window, const SystemMetric metric, const bool dpiScale, const bool forceSystemValue = false); [[nodiscard]] FRAMELESSHELPER_API QWindow *findWindow(const WId winId); [[nodiscard]] FRAMELESSHELPER_API bool shouldUseNativeTitleBar(); @@ -55,3 +58,5 @@ FRAMELESSHELPER_API void updateQtFrameMargins(QWindow *window, const bool enable #endif } + +FRAMELESSHELPER_END_NAMESPACE diff --git a/utilities_win32.cpp b/utilities_win32.cpp index 51a9c43..f2141ff 100644 --- a/utilities_win32.cpp +++ b/utilities_win32.cpp @@ -46,6 +46,8 @@ Q_DECLARE_METATYPE(QMargins) #define SM_CXPADDEDBORDER 92 #endif +FRAMELESSHELPER_BEGIN_NAMESPACE + // The standard values of resize border width, resize border height and title bar height when DPI is 96. static const int g_defaultResizeBorderWidth = 8, g_defaultResizeBorderHeight = 8, g_defaultTitleBarHeight = 23; @@ -71,7 +73,7 @@ int Utilities::getSystemMetric(const QWindow *window, const SystemMetric metric, } switch (metric) { case SystemMetric::ResizeBorderWidth: { - const int rbw = window->property(_flh_global::_flh_resizeBorderWidth_flag).toInt(); + const int rbw = window->property(Constants::resizeBorderWidth_flag).toInt(); if ((rbw > 0) && !forceSystemValue) { return qRound(static_cast(rbw) * (dpiScale ? window->devicePixelRatio() : 1.0)); } else { @@ -92,7 +94,7 @@ int Utilities::getSystemMetric(const QWindow *window, const SystemMetric metric, } } case SystemMetric::ResizeBorderHeight: { - const int rbh = window->property(_flh_global::_flh_resizeBorderHeight_flag).toInt(); + const int rbh = window->property(Constants::resizeBorderHeight_flag).toInt(); if ((rbh > 0) && !forceSystemValue) { return qRound(static_cast(rbh) * (dpiScale ? window->devicePixelRatio() : 1.0)); } else { @@ -114,7 +116,7 @@ int Utilities::getSystemMetric(const QWindow *window, const SystemMetric metric, } } case SystemMetric::TitleBarHeight: { - const int tbh = window->property(_flh_global::_flh_titleBarHeight_flag).toInt(); + const int tbh = window->property(Constants::titleBarHeight_flag).toInt(); if ((tbh > 0) && !forceSystemValue) { return qRound(static_cast(tbh) * (dpiScale ? window->devicePixelRatio() : 1.0)); } else { @@ -174,9 +176,9 @@ void Utilities::updateQtFrameMargins(QWindow *window, const bool enable) if (!window) { return; } - const int tbh = enable ? Utilities::getSystemMetric(window, Utilities::SystemMetric::TitleBarHeight, true, true) : 0; - const int rbw = enable ? Utilities::getSystemMetric(window, Utilities::SystemMetric::ResizeBorderWidth, true, true) : 0; - const int rbh = enable ? Utilities::getSystemMetric(window, Utilities::SystemMetric::ResizeBorderHeight, true, true) : 0; + const int tbh = enable ? Utilities::getSystemMetric(window, SystemMetric::TitleBarHeight, true, true) : 0; + const int rbw = enable ? Utilities::getSystemMetric(window, SystemMetric::ResizeBorderWidth, true, true) : 0; + const int rbh = enable ? Utilities::getSystemMetric(window, SystemMetric::ResizeBorderHeight, true, true) : 0; const QMargins margins = {-rbw, -(rbh + tbh), -rbw, -rbh}; // left, top, right, bottom const QVariant marginsVar = QVariant::fromValue(margins); window->setProperty("_q_windowsCustomMargins", marginsVar); @@ -220,3 +222,5 @@ bool Utilities::isWin10OrGreater() return QSysInfo::WindowsVersion >= QSysInfo::WV_WINDOWS10; #endif } + +FRAMELESSHELPER_END_NAMESPACE