From 009017c8bd027ea65759fd27324e82d40ae72972 Mon Sep 17 00:00:00 2001 From: Roman Gilg Date: Tue, 29 May 2018 10:35:28 +0200 Subject: [PATCH] [server] Small code cleanup in SurfaceInterface Summary: Use lambda function to reduce code duplication and put function definitions in the same order as in the header file. Test Plan: All autotests succeed. Reviewers: #plasma, davidedmundson Reviewed By: #plasma, davidedmundson Subscribers: davidedmundson, kde-frameworks-devel Tags: #frameworks Differential Revision: https://phabricator.kde.org/D13191 --- src/wayland/surface_interface.cpp | 106 ++++++++++++++---------------- 1 file changed, 51 insertions(+), 55 deletions(-) diff --git a/src/wayland/surface_interface.cpp b/src/wayland/surface_interface.cpp index 3787794c26..d7cce04d87 100644 --- a/src/wayland/surface_interface.cpp +++ b/src/wayland/surface_interface.cpp @@ -181,34 +181,68 @@ void SurfaceInterface::Private::installPointerConstraint(LockedPointerInterface Q_ASSERT(lockedPointer.isNull()); Q_ASSERT(confinedPointer.isNull()); lockedPointer = QPointer(lock); + + auto cleanUp = [this]() { + lockedPointer.clear(); + disconnect(constrainsOneShotConnection); + constrainsOneShotConnection = QMetaObject::Connection(); + disconnect(constrainsUnboundConnection); + constrainsUnboundConnection = QMetaObject::Connection(); + emit q_func()->pointerConstraintsChanged(); + }; + if (lock->lifeTime() == LockedPointerInterface::LifeTime::OneShot) { constrainsOneShotConnection = QObject::connect(lock, &LockedPointerInterface::lockedChanged, q_func(), - [this] { - if (lockedPointer.isNull()) { + [this, cleanUp] { + if (lockedPointer.isNull() || lockedPointer->isLocked()) { return; } - if (!lockedPointer->isLocked()) { - lockedPointer.clear(); - disconnect(constrainsOneShotConnection); - constrainsOneShotConnection = QMetaObject::Connection(); - disconnect(constrainsUnboundConnection); - constrainsUnboundConnection = QMetaObject::Connection(); - emit q_func()->pointerConstraintsChanged(); - } + cleanUp(); } ); } constrainsUnboundConnection = QObject::connect(lock, &LockedPointerInterface::unbound, q_func(), - [this] { + [this, cleanUp] { if (lockedPointer.isNull()) { return; } - lockedPointer.clear(); - disconnect(constrainsOneShotConnection); - constrainsOneShotConnection = QMetaObject::Connection(); - disconnect(constrainsUnboundConnection); - constrainsUnboundConnection = QMetaObject::Connection(); - emit q_func()->pointerConstraintsChanged(); + cleanUp(); + } + ); + emit q_func()->pointerConstraintsChanged(); +} + +void SurfaceInterface::Private::installPointerConstraint(ConfinedPointerInterface *confinement) +{ + Q_ASSERT(lockedPointer.isNull()); + Q_ASSERT(confinedPointer.isNull()); + confinedPointer = QPointer(confinement); + + auto cleanUp = [this]() { + confinedPointer.clear(); + disconnect(constrainsOneShotConnection); + constrainsOneShotConnection = QMetaObject::Connection(); + disconnect(constrainsUnboundConnection); + constrainsUnboundConnection = QMetaObject::Connection(); + emit q_func()->pointerConstraintsChanged(); + }; + + if (confinement->lifeTime() == ConfinedPointerInterface::LifeTime::OneShot) { + constrainsOneShotConnection = QObject::connect(confinement, &ConfinedPointerInterface::confinedChanged, q_func(), + [this, cleanUp] { + if (confinedPointer.isNull() || confinedPointer->isConfined()) { + return; + } + cleanUp(); + } + ); + } + constrainsUnboundConnection = QObject::connect(confinement, &ConfinedPointerInterface::unbound, q_func(), + [this, cleanUp] { + if (confinedPointer.isNull()) { + return; + } + cleanUp(); } ); emit q_func()->pointerConstraintsChanged(); @@ -230,44 +264,6 @@ void SurfaceInterface::Private::installIdleInhibitor(IdleInhibitorInterface *inh } } -void SurfaceInterface::Private::installPointerConstraint(ConfinedPointerInterface *confinement) -{ - Q_ASSERT(lockedPointer.isNull()); - Q_ASSERT(confinedPointer.isNull()); - confinedPointer = QPointer(confinement); - if (confinement->lifeTime() == ConfinedPointerInterface::LifeTime::OneShot) { - constrainsOneShotConnection = QObject::connect(confinement, &ConfinedPointerInterface::confinedChanged, q_func(), - [this] { - if (confinedPointer.isNull()) { - return; - } - if (!confinedPointer->isConfined()) { - confinedPointer.clear(); - disconnect(constrainsOneShotConnection); - constrainsOneShotConnection = QMetaObject::Connection(); - disconnect(constrainsUnboundConnection); - constrainsUnboundConnection = QMetaObject::Connection(); - emit q_func()->pointerConstraintsChanged(); - } - } - ); - } - constrainsUnboundConnection = QObject::connect(confinement, &ConfinedPointerInterface::unbound, q_func(), - [this] { - if (confinedPointer.isNull()) { - return; - } - confinedPointer.clear(); - disconnect(constrainsOneShotConnection); - constrainsOneShotConnection = QMetaObject::Connection(); - disconnect(constrainsUnboundConnection); - constrainsUnboundConnection = QMetaObject::Connection(); - emit q_func()->pointerConstraintsChanged(); - } - ); - emit q_func()->pointerConstraintsChanged(); -} - #ifndef DOXYGEN_SHOULD_SKIP_THIS const struct wl_surface_interface SurfaceInterface::Private::s_interface = { resourceDestroyedCallback,