Minor tweaks

Signed-off-by: Yuhang Zhao <2546789017@qq.com>
This commit is contained in:
Yuhang Zhao 2021-07-20 09:58:00 +08:00
parent f30339ff54
commit 233ebba3ec
10 changed files with 103 additions and 94 deletions

View File

@ -35,7 +35,7 @@ Window {
title: qsTr("Hello, World!") title: qsTr("Hello, World!")
color: "#f0f0f0" color: "#f0f0f0"
property int _flh_margin: ((window.visibility === Window.Maximized) || (window.visibility === Window.FullScreen)) ? 0 : (1 / Screen.devicePixelRatio) property real _flh_margin: ((window.visibility === Window.Maximized) || (window.visibility === Window.FullScreen)) ? 0.0 : (1.0 / Screen.devicePixelRatio)
FramelessHelper { FramelessHelper {
id: framelessHelper id: framelessHelper
@ -51,7 +51,7 @@ Window {
Rectangle { Rectangle {
id: titleBar id: titleBar
height: framelessHelper.titleBarHeight height: framelessHelper.titleBarHeight + framelessHelper.resizeBorderHeight
color: "transparent" color: "transparent"
anchors { anchors {
top: parent.top top: parent.top

View File

@ -112,8 +112,9 @@ void Widget::paintEvent(QPaintEvent *event)
void Widget::setupUi() void Widget::setupUi()
{ {
const QWindow *win = windowHandle(); 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 titleBarHeight = Utilities::getSystemMetric(win, Utilities::SystemMetric::TitleBarHeight, false);
const QSize systemButtonSize = {qRound(titleBarHeight * 1.5), titleBarHeight}; const QSize systemButtonSize = {qRound(static_cast<qreal>(titleBarHeight + resizeBorderHeight) * 1.5), titleBarHeight};
m_minimizeButton = new QPushButton(this); m_minimizeButton = new QPushButton(this);
m_minimizeButton->setObjectName(QStringLiteral("MinimizeButton")); m_minimizeButton->setObjectName(QStringLiteral("MinimizeButton"));
m_minimizeButton->setFixedSize(systemButtonSize); m_minimizeButton->setFixedSize(systemButtonSize);

View File

@ -67,34 +67,34 @@ bool FramelessHelper::eventFilter(QObject *object, QEvent *event)
return false; return false;
} }
const auto currentWindow = qobject_cast<QWindow *>(object); const auto currentWindow = qobject_cast<QWindow *>(object);
const int m_borderWidth = FramelessWindowsManager::getResizeBorderWidth(currentWindow); const int m_resizeBorderWidth = FramelessWindowsManager::getResizeBorderWidth(currentWindow);
const int m_borderHeight = FramelessWindowsManager::getResizeBorderHeight(currentWindow); const int m_resizeBorderHeight = FramelessWindowsManager::getResizeBorderHeight(currentWindow);
const int m_titleBarHeight = FramelessWindowsManager::getTitleBarHeight(currentWindow); const int m_titleBarHeight = FramelessWindowsManager::getTitleBarHeight(currentWindow);
const bool m_resizable = FramelessWindowsManager::getResizable(currentWindow); const bool m_resizable = FramelessWindowsManager::getResizable(currentWindow);
const auto getWindowEdges = const auto getWindowEdges =
[m_borderWidth, m_borderHeight](const QPointF &point, const int ww, const int wh) -> Qt::Edges { [m_resizeBorderWidth, m_resizeBorderHeight](const QPointF &point, const int ww, const int wh) -> Qt::Edges {
if (point.y() <= m_borderHeight) { if (point.y() <= m_resizeBorderHeight) {
if (point.x() <= m_borderWidth) { if (point.x() <= m_resizeBorderWidth) {
return Qt::Edge::TopEdge | Qt::Edge::LeftEdge; return Qt::Edge::TopEdge | Qt::Edge::LeftEdge;
} }
if (point.x() >= (ww - m_borderWidth)) { if (point.x() >= (ww - m_resizeBorderWidth)) {
return Qt::Edge::TopEdge | Qt::Edge::RightEdge; return Qt::Edge::TopEdge | Qt::Edge::RightEdge;
} }
return Qt::Edge::TopEdge; return Qt::Edge::TopEdge;
} }
if (point.y() >= (wh - m_borderHeight)) { if (point.y() >= (wh - m_resizeBorderHeight)) {
if (point.x() <= m_borderWidth) { if (point.x() <= m_resizeBorderWidth) {
return Qt::Edge::BottomEdge | Qt::Edge::LeftEdge; return Qt::Edge::BottomEdge | Qt::Edge::LeftEdge;
} }
if (point.x() >= (ww - m_borderWidth)) { if (point.x() >= (ww - m_resizeBorderWidth)) {
return Qt::Edge::BottomEdge | Qt::Edge::RightEdge; return Qt::Edge::BottomEdge | Qt::Edge::RightEdge;
} }
return Qt::Edge::BottomEdge; return Qt::Edge::BottomEdge;
} }
if (point.x() <= m_borderWidth) { if (point.x() <= m_resizeBorderWidth) {
return Qt::Edge::LeftEdge; return Qt::Edge::LeftEdge;
} }
if (point.x() >= (ww - m_borderWidth)) { if (point.x() >= (ww - m_resizeBorderWidth)) {
return Qt::Edge::RightEdge; return Qt::Edge::RightEdge;
} }
return {}; return {};
@ -116,12 +116,13 @@ bool FramelessHelper::eventFilter(QObject *object, QEvent *event)
} }
return Qt::CursorShape::ArrowCursor; return Qt::CursorShape::ArrowCursor;
}; };
const auto isInTitlebarArea = [m_titleBarHeight](const QPointF &point, const QWindow *window) -> bool { const auto isInTitlebarArea = [m_resizeBorderHeight, m_titleBarHeight](const QPointF &point, const QWindow *window) -> bool {
Q_ASSERT(window); Q_ASSERT(window);
if (!window) { if (!window) {
return false; return false;
} }
return (point.y() <= m_titleBarHeight) && !Utilities::isHitTestVisibleInChrome(window); return (point.y() > m_resizeBorderHeight) && (point.y() <= (m_titleBarHeight + m_resizeBorderHeight))
&& !Utilities::isHitTestVisibleInChrome(window);
}; };
const auto moveOrResize = const auto moveOrResize =
[m_resizable, &getWindowEdges, &isInTitlebarArea](const QPointF &point, QWindow *window) { [m_resizable, &getWindowEdges, &isInTitlebarArea](const QPointF &point, QWindow *window) {

View File

@ -57,11 +57,18 @@
#define FRAMELESSHELPER_USE_UNIX_VERSION #define FRAMELESSHELPER_USE_UNIX_VERSION
#endif #endif
namespace _flh_global { #if (QT_VERSION >= QT_VERSION_CHECK(5, 15, 0))
#define Q_NODISCARD [[nodiscard]]
#else
#define Q_NODISCARD
#endif
namespace _flh_global
{
[[maybe_unused]] const char _flh_framelessEnabled_flag[] = "_FRAMELESSHELPER_FRAMELESS_MODE_ENABLED"; [[maybe_unused]] const char _flh_framelessEnabled_flag[] = "_FRAMELESSHELPER_FRAMELESS_MODE_ENABLED";
[[maybe_unused]] const char _flh_borderWidth_flag[] = "_FRAMELESSHELPER_WINDOW_RESIZE_BORDER_WIDTH"; [[maybe_unused]] const char _flh_resizeBorderWidth_flag[] = "_FRAMELESSHELPER_WINDOW_RESIZE_BORDER_WIDTH";
[[maybe_unused]] const char _flh_borderHeight_flag[] = "_FRAMELESSHELPER_WINDOW_RESIZE_BORDER_HEIGHT"; [[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_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_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_useNativeTitleBar_flag[] = "_FRAMELESSHELPER_USE_NATIVE_TITLE_BAR";

View File

@ -109,8 +109,8 @@ static inline void installHelper(QWindow *window, const bool enable)
return; return;
} }
Utilities::updateQtFrameMargins(window, enable); Utilities::updateQtFrameMargins(window, enable);
Utilities::updateFrameMargins(window, !enable); Utilities::updateFrameMargins(window->winId(), !enable);
Utilities::triggerFrameChange(window); Utilities::triggerFrameChange(window->winId());
window->setProperty(_flh_global::_flh_framelessEnabled_flag, enable); window->setProperty(_flh_global::_flh_framelessEnabled_flag, enable);
} }
@ -273,13 +273,13 @@ bool FramelessHelperWin::nativeEventFilter(const QByteArray &eventType, void *me
// The value of border width and border height should be // The value of border width and border height should be
// identical in most cases, when the scale factor is 1.0, it // identical in most cases, when the scale factor is 1.0, it
// should be eight pixels. // should be eight pixels.
const int bh = getSystemMetric(window, Utilities::SystemMetric::ResizeBorderHeight, true); const int rbh = getSystemMetric(window, Utilities::SystemMetric::ResizeBorderHeight, true);
clientRect->top += bh; clientRect->top += rbh;
if (!shouldHaveWindowFrame()) { if (!shouldHaveWindowFrame()) {
clientRect->bottom -= bh; clientRect->bottom -= rbh;
const int bw = getSystemMetric(window, Utilities::SystemMetric::ResizeBorderWidth, true); const int rbw = getSystemMetric(window, Utilities::SystemMetric::ResizeBorderWidth, true);
clientRect->left += bw; clientRect->left += rbw;
clientRect->right -= bw; clientRect->right -= rbw;
} }
nonClientAreaExists = true; nonClientAreaExists = true;
} }
@ -518,10 +518,11 @@ bool FramelessHelperWin::nativeEventFilter(const QByteArray &eventType, void *me
POINT winLocalMouse = {qRound(globalMouse.x()), qRound(globalMouse.y())}; POINT winLocalMouse = {qRound(globalMouse.x()), qRound(globalMouse.y())};
ScreenToClient(msg->hwnd, &winLocalMouse); ScreenToClient(msg->hwnd, &winLocalMouse);
const QPointF localMouse = {static_cast<qreal>(winLocalMouse.x), static_cast<qreal>(winLocalMouse.y)}; const QPointF localMouse = {static_cast<qreal>(winLocalMouse.x), static_cast<qreal>(winLocalMouse.y)};
const int bh = getSystemMetric(window, Utilities::SystemMetric::ResizeBorderHeight, true); const int rbh = getSystemMetric(window, Utilities::SystemMetric::ResizeBorderHeight, true);
const int tbh = getSystemMetric(window, Utilities::SystemMetric::TitleBarHeight, true); const int tbh = getSystemMetric(window, Utilities::SystemMetric::TitleBarHeight, true);
const bool isTitleBar = (localMouse.y() <= tbh) && !Utilities::isHitTestVisibleInChrome(window); const bool isTitleBar = (localMouse.y() > rbh) && (localMouse.y() <= (rbh + tbh))
const bool isTop = localMouse.y() <= bh; && !Utilities::isHitTestVisibleInChrome(window);
const bool isTop = localMouse.y() <= rbh;
if (shouldHaveWindowFrame()) { if (shouldHaveWindowFrame()) {
// 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.
@ -547,23 +548,23 @@ bool FramelessHelperWin::nativeEventFilter(const QByteArray &eventType, void *me
return true; return true;
} else { } else {
const auto getHitTestResult = const auto getHitTestResult =
[msg, isTitleBar, &localMouse, bh, isTop, window]() -> LRESULT { [msg, isTitleBar, &localMouse, rbh, isTop, window]() -> LRESULT {
RECT clientRect = {0, 0, 0, 0}; RECT clientRect = {0, 0, 0, 0};
GetClientRect(msg->hwnd, &clientRect); GetClientRect(msg->hwnd, &clientRect);
const LONG ww = clientRect.right; const LONG ww = clientRect.right;
const LONG wh = clientRect.bottom; const LONG wh = clientRect.bottom;
const int bw = getSystemMetric(window, Utilities::SystemMetric::ResizeBorderWidth, true); const int rbw = getSystemMetric(window, Utilities::SystemMetric::ResizeBorderWidth, true);
if (IsMaximized(msg->hwnd)) { if (IsMaximized(msg->hwnd)) {
if (isTitleBar) { if (isTitleBar) {
return HTCAPTION; return HTCAPTION;
} }
return HTCLIENT; return HTCLIENT;
} }
const bool isBottom = (localMouse.y() >= (wh - bh)); const bool isBottom = (localMouse.y() >= (wh - rbh));
// Make the border a little wider to let the user easy to resize on corners. // Make the border a little wider to let the user easy to resize on corners.
const int factor = (isTop || isBottom) ? 2 : 1; const qreal factor = (isTop || isBottom) ? 2.0 : 1.0;
const bool isLeft = (localMouse.x() <= (bw * factor)); const bool isLeft = (localMouse.x() <= qRound(static_cast<qreal>(rbw) * factor));
const bool isRight = (localMouse.x() >= (ww - (bw * factor))); const bool isRight = (localMouse.x() >= (ww - qRound(static_cast<qreal>(rbw) * factor)));
const bool fixedSize = Utilities::isWindowFixedSize(window); const bool fixedSize = Utilities::isWindowFixedSize(window);
const auto getBorderValue = [fixedSize](int value) -> int { const auto getBorderValue = [fixedSize](int value) -> int {
return fixedSize ? HTCLIENT : value; return fixedSize ? HTCLIENT : value;
@ -613,10 +614,10 @@ bool FramelessHelperWin::nativeEventFilter(const QByteArray &eventType, void *me
// Prevent Windows from drawing the default title bar by temporarily // Prevent Windows from drawing the default title bar by temporarily
// toggling the WS_VISIBLE style. // toggling the WS_VISIBLE style.
SetWindowLongPtrW(msg->hwnd, GWL_STYLE, oldStyle & ~WS_VISIBLE); SetWindowLongPtrW(msg->hwnd, GWL_STYLE, oldStyle & ~WS_VISIBLE);
Utilities::triggerFrameChange(window); Utilities::triggerFrameChange(reinterpret_cast<WId>(msg->hwnd));
const LRESULT ret = DefWindowProcW(msg->hwnd, msg->message, msg->wParam, msg->lParam); const LRESULT ret = DefWindowProcW(msg->hwnd, msg->message, msg->wParam, msg->lParam);
SetWindowLongPtrW(msg->hwnd, GWL_STYLE, oldStyle); SetWindowLongPtrW(msg->hwnd, GWL_STYLE, oldStyle);
Utilities::triggerFrameChange(window); Utilities::triggerFrameChange(reinterpret_cast<WId>(msg->hwnd));
*result = ret; *result = ret;
return true; return true;
} }

View File

@ -30,37 +30,37 @@ FramelessQuickHelper::FramelessQuickHelper(QQuickItem *parent) : QQuickItem(pare
{ {
} }
int FramelessQuickHelper::resizeBorderWidth() const qreal FramelessQuickHelper::resizeBorderWidth() const
{ {
return FramelessWindowsManager::getResizeBorderWidth(window()); return FramelessWindowsManager::getResizeBorderWidth(window());
} }
void FramelessQuickHelper::setResizeBorderWidth(const int val) void FramelessQuickHelper::setResizeBorderWidth(const qreal val)
{ {
FramelessWindowsManager::setResizeBorderWidth(window(), val); FramelessWindowsManager::setResizeBorderWidth(window(), qRound(val));
Q_EMIT resizeBorderWidthChanged(); Q_EMIT resizeBorderWidthChanged(val);
} }
int FramelessQuickHelper::resizeBorderHeight() const qreal FramelessQuickHelper::resizeBorderHeight() const
{ {
return FramelessWindowsManager::getResizeBorderHeight(window()); return FramelessWindowsManager::getResizeBorderHeight(window());
} }
void FramelessQuickHelper::setResizeBorderHeight(const int val) void FramelessQuickHelper::setResizeBorderHeight(const qreal val)
{ {
FramelessWindowsManager::setResizeBorderHeight(window(), val); FramelessWindowsManager::setResizeBorderHeight(window(), qRound(val));
Q_EMIT resizeBorderHeightChanged(); Q_EMIT resizeBorderHeightChanged(val);
} }
int FramelessQuickHelper::titleBarHeight() const qreal FramelessQuickHelper::titleBarHeight() const
{ {
return FramelessWindowsManager::getTitleBarHeight(window()); return FramelessWindowsManager::getTitleBarHeight(window());
} }
void FramelessQuickHelper::setTitleBarHeight(const int val) void FramelessQuickHelper::setTitleBarHeight(const qreal val)
{ {
FramelessWindowsManager::setTitleBarHeight(window(), val); FramelessWindowsManager::setTitleBarHeight(window(), qRound(val));
Q_EMIT titleBarHeightChanged(); Q_EMIT titleBarHeightChanged(val);
} }
bool FramelessQuickHelper::resizable() const bool FramelessQuickHelper::resizable() const
@ -71,7 +71,7 @@ bool FramelessQuickHelper::resizable() const
void FramelessQuickHelper::setResizable(const bool val) void FramelessQuickHelper::setResizable(const bool val)
{ {
FramelessWindowsManager::setResizable(window(), val); FramelessWindowsManager::setResizable(window(), val);
Q_EMIT resizableChanged(); Q_EMIT resizableChanged(val);
} }
void FramelessQuickHelper::removeWindowFrame() void FramelessQuickHelper::removeWindowFrame()

View File

@ -34,36 +34,36 @@ class FRAMELESSHELPER_API FramelessQuickHelper : public QQuickItem
#ifdef QML_NAMED_ELEMENT #ifdef QML_NAMED_ELEMENT
QML_NAMED_ELEMENT(FramelessHelper) QML_NAMED_ELEMENT(FramelessHelper)
#endif #endif
Q_PROPERTY(int resizeBorderWidth READ resizeBorderWidth WRITE setResizeBorderWidth NOTIFY resizeBorderWidthChanged) Q_PROPERTY(qreal resizeBorderWidth READ resizeBorderWidth WRITE setResizeBorderWidth NOTIFY resizeBorderWidthChanged)
Q_PROPERTY(int resizeBorderHeight READ resizeBorderHeight WRITE setResizeBorderHeight NOTIFY resizeBorderHeightChanged) Q_PROPERTY(qreal resizeBorderHeight READ resizeBorderHeight WRITE setResizeBorderHeight NOTIFY resizeBorderHeightChanged)
Q_PROPERTY(int titleBarHeight READ titleBarHeight WRITE setTitleBarHeight NOTIFY titleBarHeightChanged) Q_PROPERTY(qreal titleBarHeight READ titleBarHeight WRITE setTitleBarHeight NOTIFY titleBarHeightChanged)
Q_PROPERTY(bool resizable READ resizable WRITE setResizable NOTIFY resizableChanged) Q_PROPERTY(bool resizable READ resizable WRITE setResizable NOTIFY resizableChanged)
public: public:
explicit FramelessQuickHelper(QQuickItem *parent = nullptr); explicit FramelessQuickHelper(QQuickItem *parent = nullptr);
~FramelessQuickHelper() override = default; ~FramelessQuickHelper() override = default;
[[nodiscard]] int resizeBorderWidth() const; Q_NODISCARD qreal resizeBorderWidth() const;
void setResizeBorderWidth(const int val); void setResizeBorderWidth(const qreal val);
[[nodiscard]] int resizeBorderHeight() const; Q_NODISCARD qreal resizeBorderHeight() const;
void setResizeBorderHeight(const int val); void setResizeBorderHeight(const qreal val);
[[nodiscard]] int titleBarHeight() const; Q_NODISCARD qreal titleBarHeight() const;
void setTitleBarHeight(const int val); void setTitleBarHeight(const qreal val);
[[nodiscard]] bool resizable() const; Q_NODISCARD bool resizable() const;
void setResizable(const bool val); void setResizable(const bool val);
public Q_SLOTS: public Q_SLOTS:
void removeWindowFrame(); void removeWindowFrame();
void bringBackWindowFrame(); void bringBackWindowFrame();
[[nodiscard]] bool isWindowFrameless() const; Q_NODISCARD bool isWindowFrameless() const;
void setHitTestVisibleInChrome(QQuickItem *item, const bool visible); void setHitTestVisibleInChrome(QQuickItem *item, const bool visible);
Q_SIGNALS: Q_SIGNALS:
void resizeBorderWidthChanged(); void resizeBorderWidthChanged(qreal);
void resizeBorderHeightChanged(); void resizeBorderHeightChanged(qreal);
void titleBarHeightChanged(); void titleBarHeightChanged(qreal);
void resizableChanged(); void resizableChanged(bool);
}; };

View File

@ -91,7 +91,7 @@ int FramelessWindowsManager::getResizeBorderWidth(const QWindow *window)
return 8; return 8;
} }
#ifdef FRAMELESSHELPER_USE_UNIX_VERSION #ifdef FRAMELESSHELPER_USE_UNIX_VERSION
const int value = window->property(_flh_global::_flh_borderWidth_flag).toInt(); const int value = window->property(_flh_global::_flh_resizeBorderWidth_flag).toInt();
return value <= 0 ? 8 : value; return value <= 0 ? 8 : value;
#else #else
return Utilities::getSystemMetric(window, Utilities::SystemMetric::ResizeBorderWidth, false); return Utilities::getSystemMetric(window, Utilities::SystemMetric::ResizeBorderWidth, false);
@ -104,7 +104,7 @@ void FramelessWindowsManager::setResizeBorderWidth(QWindow *window, const int va
if (!window || (value <= 0)) { if (!window || (value <= 0)) {
return; return;
} }
window->setProperty(_flh_global::_flh_borderWidth_flag, value); window->setProperty(_flh_global::_flh_resizeBorderWidth_flag, value);
} }
int FramelessWindowsManager::getResizeBorderHeight(const QWindow *window) int FramelessWindowsManager::getResizeBorderHeight(const QWindow *window)
@ -114,7 +114,7 @@ int FramelessWindowsManager::getResizeBorderHeight(const QWindow *window)
return 8; return 8;
} }
#ifdef FRAMELESSHELPER_USE_UNIX_VERSION #ifdef FRAMELESSHELPER_USE_UNIX_VERSION
const int value = window->property(_flh_global::_flh_borderHeight_flag).toInt(); const int value = window->property(_flh_global::_flh_resizeBorderHeight_flag).toInt();
return value <= 0 ? 8 : value; return value <= 0 ? 8 : value;
#else #else
return Utilities::getSystemMetric(window, Utilities::SystemMetric::ResizeBorderHeight, false); return Utilities::getSystemMetric(window, Utilities::SystemMetric::ResizeBorderHeight, false);
@ -127,18 +127,18 @@ void FramelessWindowsManager::setResizeBorderHeight(QWindow *window, const int v
if (!window || (value <= 0)) { if (!window || (value <= 0)) {
return; return;
} }
window->setProperty(_flh_global::_flh_borderHeight_flag, value); window->setProperty(_flh_global::_flh_resizeBorderHeight_flag, value);
} }
int FramelessWindowsManager::getTitleBarHeight(const QWindow *window) int FramelessWindowsManager::getTitleBarHeight(const QWindow *window)
{ {
Q_ASSERT(window); Q_ASSERT(window);
if (!window) { if (!window) {
return 31; return 23;
} }
#ifdef FRAMELESSHELPER_USE_UNIX_VERSION #ifdef FRAMELESSHELPER_USE_UNIX_VERSION
const int value = window->property(_flh_global::_flh_titleBarHeight_flag).toInt(); const int value = window->property(_flh_global::_flh_titleBarHeight_flag).toInt();
return value <= 0 ? 31 : value; return value <= 0 ? 23 : value;
#else #else
return Utilities::getSystemMetric(window, Utilities::SystemMetric::TitleBarHeight, false); return Utilities::getSystemMetric(window, Utilities::SystemMetric::TitleBarHeight, false);
#endif #endif

View File

@ -49,8 +49,8 @@ enum class SystemMetric
[[nodiscard]] FRAMELESSHELPER_API bool isWin8Point1OrGreater(); [[nodiscard]] FRAMELESSHELPER_API bool isWin8Point1OrGreater();
[[nodiscard]] FRAMELESSHELPER_API bool isWin10OrGreater(); [[nodiscard]] FRAMELESSHELPER_API bool isWin10OrGreater();
[[nodiscard]] FRAMELESSHELPER_API bool isDwmCompositionAvailable(); [[nodiscard]] FRAMELESSHELPER_API bool isDwmCompositionAvailable();
FRAMELESSHELPER_API void triggerFrameChange(const QWindow *window); FRAMELESSHELPER_API void triggerFrameChange(const WId winId);
FRAMELESSHELPER_API void updateFrameMargins(const QWindow *window, const bool reset); FRAMELESSHELPER_API void updateFrameMargins(const WId winId, const bool reset);
FRAMELESSHELPER_API void updateQtFrameMargins(QWindow *window, const bool enable); FRAMELESSHELPER_API void updateQtFrameMargins(QWindow *window, const bool enable);
#endif #endif

View File

@ -47,7 +47,7 @@ Q_DECLARE_METATYPE(QMargins)
#endif #endif
// The standard values of resize border width, resize border height and title bar height when DPI is 96. // 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 = 31; static const int g_defaultResizeBorderWidth = 8, g_defaultResizeBorderHeight = 8, g_defaultTitleBarHeight = 23;
bool Utilities::isDwmCompositionAvailable() bool Utilities::isDwmCompositionAvailable()
{ {
@ -71,9 +71,9 @@ int Utilities::getSystemMetric(const QWindow *window, const SystemMetric metric,
} }
switch (metric) { switch (metric) {
case SystemMetric::ResizeBorderWidth: { case SystemMetric::ResizeBorderWidth: {
const int bw = window->property(_flh_global::_flh_borderWidth_flag).toInt(); const int rbw = window->property(_flh_global::_flh_resizeBorderWidth_flag).toInt();
if ((bw > 0) && !forceSystemValue) { if ((rbw > 0) && !forceSystemValue) {
return qRound(static_cast<qreal>(bw) * (dpiScale ? window->devicePixelRatio() : 1.0)); return qRound(static_cast<qreal>(rbw) * (dpiScale ? window->devicePixelRatio() : 1.0));
} else { } else {
const int result = GetSystemMetrics(SM_CXSIZEFRAME) + GetSystemMetrics(SM_CXPADDEDBORDER); const int result = GetSystemMetrics(SM_CXSIZEFRAME) + GetSystemMetrics(SM_CXPADDEDBORDER);
if (result > 0) { if (result > 0) {
@ -92,9 +92,9 @@ int Utilities::getSystemMetric(const QWindow *window, const SystemMetric metric,
} }
} }
case SystemMetric::ResizeBorderHeight: { case SystemMetric::ResizeBorderHeight: {
const int bh = window->property(_flh_global::_flh_borderHeight_flag).toInt(); const int rbh = window->property(_flh_global::_flh_resizeBorderHeight_flag).toInt();
if ((bh > 0) && !forceSystemValue) { if ((rbh > 0) && !forceSystemValue) {
return qRound(static_cast<qreal>(bh) * (dpiScale ? window->devicePixelRatio() : 1.0)); return qRound(static_cast<qreal>(rbh) * (dpiScale ? window->devicePixelRatio() : 1.0));
} else { } else {
// There is no "SM_CYPADDEDBORDER". // There is no "SM_CYPADDEDBORDER".
const int result = GetSystemMetrics(SM_CYSIZEFRAME) + GetSystemMetrics(SM_CXPADDEDBORDER); const int result = GetSystemMetrics(SM_CYSIZEFRAME) + GetSystemMetrics(SM_CXPADDEDBORDER);
@ -118,8 +118,7 @@ int Utilities::getSystemMetric(const QWindow *window, const SystemMetric metric,
if ((tbh > 0) && !forceSystemValue) { if ((tbh > 0) && !forceSystemValue) {
return qRound(static_cast<qreal>(tbh) * (dpiScale ? window->devicePixelRatio() : 1.0)); return qRound(static_cast<qreal>(tbh) * (dpiScale ? window->devicePixelRatio() : 1.0));
} else { } else {
// There is no "SM_CYPADDEDBORDER". const int result = GetSystemMetrics(SM_CYCAPTION);
const int result = GetSystemMetrics(SM_CYCAPTION) + GetSystemMetrics(SM_CYSIZEFRAME) + GetSystemMetrics(SM_CXPADDEDBORDER);
if (result > 0) { if (result > 0) {
if (dpiScale) { if (dpiScale) {
return result; return result;
@ -139,30 +138,30 @@ int Utilities::getSystemMetric(const QWindow *window, const SystemMetric metric,
return 0; return 0;
} }
void Utilities::triggerFrameChange(const QWindow *window) void Utilities::triggerFrameChange(const WId winId)
{ {
Q_ASSERT(window); Q_ASSERT(winId);
if (!window) { if (!winId) {
return; return;
} }
const auto hwnd = reinterpret_cast<HWND>(window->winId()); const auto hwnd = reinterpret_cast<HWND>(winId);
if (SetWindowPos(hwnd, nullptr, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER | SWP_NOOWNERZORDER) == FALSE) { if (SetWindowPos(hwnd, nullptr, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER | SWP_NOOWNERZORDER) == FALSE) {
qWarning() << "SetWindowPos() failed."; qWarning() << "SetWindowPos() failed.";
} }
} }
void Utilities::updateFrameMargins(const QWindow *window, const bool reset) void Utilities::updateFrameMargins(const WId winId, const bool reset)
{ {
// DwmExtendFrameIntoClientArea() will always fail if DWM Composition is disabled. // DwmExtendFrameIntoClientArea() will always fail if DWM Composition is disabled.
// No need to try in this case. // No need to try in this case.
if (!isDwmCompositionAvailable()) { if (!isDwmCompositionAvailable()) {
return; return;
} }
Q_ASSERT(window); Q_ASSERT(winId);
if (!window) { if (!winId) {
return; return;
} }
const auto hwnd = reinterpret_cast<HWND>(window->winId()); const auto hwnd = reinterpret_cast<HWND>(winId);
const MARGINS margins = reset ? MARGINS{0, 0, 0, 0} : MARGINS{1, 1, 1, 1}; const MARGINS margins = reset ? MARGINS{0, 0, 0, 0} : MARGINS{1, 1, 1, 1};
if (FAILED(DwmExtendFrameIntoClientArea(hwnd, &margins))) { if (FAILED(DwmExtendFrameIntoClientArea(hwnd, &margins))) {
qWarning() << "DwmExtendFrameIntoClientArea() failed."; qWarning() << "DwmExtendFrameIntoClientArea() failed.";
@ -176,9 +175,9 @@ void Utilities::updateQtFrameMargins(QWindow *window, const bool enable)
return; return;
} }
const int tbh = enable ? Utilities::getSystemMetric(window, Utilities::SystemMetric::TitleBarHeight, true, true) : 0; const int tbh = enable ? Utilities::getSystemMetric(window, Utilities::SystemMetric::TitleBarHeight, true, true) : 0;
const int bw = enable ? Utilities::getSystemMetric(window, Utilities::SystemMetric::ResizeBorderWidth, true, true) : 0; const int rbw = enable ? Utilities::getSystemMetric(window, Utilities::SystemMetric::ResizeBorderWidth, true, true) : 0;
const int bh = enable ? Utilities::getSystemMetric(window, Utilities::SystemMetric::ResizeBorderHeight, true, true) : 0; const int rbh = enable ? Utilities::getSystemMetric(window, Utilities::SystemMetric::ResizeBorderHeight, true, true) : 0;
const QMargins margins = {-bw, -tbh, -bw, -bh}; // left, top, right, bottom const QMargins margins = {-rbw, -(rbh + tbh), -rbw, -rbh}; // left, top, right, bottom
const QVariant marginsVar = QVariant::fromValue(margins); const QVariant marginsVar = QVariant::fromValue(margins);
window->setProperty("_q_windowsCustomMargins", marginsVar); window->setProperty("_q_windowsCustomMargins", marginsVar);
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)) #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))