Simplify the code a bit

Introduce a small helper function to simplify the code a bit.

Signed-off-by: Yuhang Zhao <2546789017@qq.com>
This commit is contained in:
Yuhang Zhao 2020-11-28 12:10:22 +08:00
parent 68bc4c31ed
commit 27be916e11
1 changed files with 26 additions and 35 deletions

View File

@ -815,26 +815,14 @@ void setup()
} }
} }
} // namespace void installHelper(QWindow *window, const bool enable)
WinNativeEventFilter::WinNativeEventFilter() = default;
WinNativeEventFilter::~WinNativeEventFilter()
{
if (!coreData()->m_instance.isNull()) {
qApp->removeNativeEventFilter(coreData()->m_instance.get());
}
}
void WinNativeEventFilter::addFramelessWindow(QWindow *window)
{ {
Q_ASSERT(window); Q_ASSERT(window);
setup(); window->setProperty(m_framelessMode, enable);
window->setProperty(m_framelessMode, true); const int tbh = enable ? WinNativeEventFilter::getSystemMetric(
const QMargins margins = {0, window, WinNativeEventFilter::SystemMetric::TitleBarHeight, true, true)
-getSystemMetric(window, SystemMetric::TitleBarHeight, true, true), : 0;
0, const QMargins margins = {0, -tbh, 0, 0};
0};
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0)) #if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
QPlatformWindow *platformWindow = window->handle(); QPlatformWindow *platformWindow = window->handle();
if (platformWindow) { if (platformWindow) {
@ -850,10 +838,28 @@ void WinNativeEventFilter::addFramelessWindow(QWindow *window)
platformWindow->setCustomMargins(margins); platformWindow->setCustomMargins(margins);
} }
#endif #endif
updateFrameMargins(window, false); updateFrameMargins(window, !enable);
triggerFrameChange(window); triggerFrameChange(window);
} }
} // namespace
WinNativeEventFilter::WinNativeEventFilter() = default;
WinNativeEventFilter::~WinNativeEventFilter()
{
if (!coreData()->m_instance.isNull()) {
qApp->removeNativeEventFilter(coreData()->m_instance.get());
}
}
void WinNativeEventFilter::addFramelessWindow(QWindow *window)
{
Q_ASSERT(window);
setup();
installHelper(window, true);
}
bool WinNativeEventFilter::isWindowFrameless(const QWindow *window) bool WinNativeEventFilter::isWindowFrameless(const QWindow *window)
{ {
Q_ASSERT(window); Q_ASSERT(window);
@ -863,22 +869,7 @@ bool WinNativeEventFilter::isWindowFrameless(const QWindow *window)
void WinNativeEventFilter::removeFramelessWindow(QWindow *window) void WinNativeEventFilter::removeFramelessWindow(QWindow *window)
{ {
Q_ASSERT(window); Q_ASSERT(window);
window->setProperty(m_framelessMode, false); installHelper(window, false);
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
QPlatformWindow *platformWindow = window->handle();
if (platformWindow) {
QGuiApplication::platformNativeInterface()
->setWindowProperty(platformWindow, QString::fromUtf8("WindowsCustomMargins"), {});
}
#else
auto *platformWindow = dynamic_cast<QNativeInterface::Private::QWindowsWindow *>(
window->handle());
if (platformWindow) {
platformWindow->setCustomMargins({});
}
#endif
updateFrameMargins(window, true);
triggerFrameChange(window);
} }
void WinNativeEventFilter::setIgnoredObjects(QWindow *window, const QObjectList &objects) void WinNativeEventFilter::setIgnoredObjects(QWindow *window, const QObjectList &objects)