From 53d8e7a38c8524ca8b110c0e693762a2dcef4eff Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Mon, 21 Dec 2015 15:15:43 +0100 Subject: Material: sync overlay background dimming with popup anims Change-Id: I709ce35ae21cf105d6328071cf8408e1f21e5d42 Reviewed-by: Mitch Curtis --- .../controls/material/ApplicationWindow.qml | 2 +- src/templates/qquickoverlay.cpp | 71 +++++++++++++--------- 2 files changed, 44 insertions(+), 29 deletions(-) (limited to 'src') diff --git a/src/imports/controls/material/ApplicationWindow.qml b/src/imports/controls/material/ApplicationWindow.qml index bbaeac70..e69c46da 100644 --- a/src/imports/controls/material/ApplicationWindow.qml +++ b/src/imports/controls/material/ApplicationWindow.qml @@ -46,6 +46,6 @@ T.ApplicationWindow { overlay.background: Rectangle { color: window.Material.backgroundDimColor - Behavior on opacity { NumberAnimation { duration: 100 } } + Behavior on opacity { NumberAnimation { duration: 150 } } } } diff --git a/src/templates/qquickoverlay.cpp b/src/templates/qquickoverlay.cpp index 949715c4..e06d28ea 100644 --- a/src/templates/qquickoverlay.cpp +++ b/src/templates/qquickoverlay.cpp @@ -50,7 +50,9 @@ class QQuickOverlayPrivate : public QQuickItemPrivate public: QQuickOverlayPrivate(); - void updateBackground(); + void popupAboutToShow(); + void popupAboutToHide(); + void drawerPositionChange(); void resizeBackground(); QQuickItem *background; @@ -59,32 +61,44 @@ public: int modalPopups; }; -void QQuickOverlayPrivate::updateBackground() +void QQuickOverlayPrivate::popupAboutToShow() { - if (!background) + Q_Q(QQuickOverlay); + if (!background || modalPopups > 1) return; - bool anim = true; - qreal level = 0.0; - if (modalPopups > 0) { - level = 1.0; - } else { - foreach (QQuickDrawer *drawer, drawers) { - qreal pos = drawer->position(); - if (pos > 0.0 && pos < 1.0) - anim = false; - level = qMax(level, pos); - } - } + QQuickPopup *popup = qobject_cast(q->sender()); + if (!popup || !popup->isModal()) + return; - if (anim) { - // use QQmlProperty instead of QQuickItem::setOpacity() to trigger QML Behaviors - QQmlProperty::write(background, QStringLiteral("opacity"), level); - } else { - // except while a drawer is opening/closing, or else the background - // fading feels laggy compared to the drawer movement - background->setOpacity(level); - } + // use QQmlProperty instead of QQuickItem::setOpacity() to trigger QML Behaviors + QQmlProperty::write(background, QStringLiteral("opacity"), 1.0); +} + +void QQuickOverlayPrivate::popupAboutToHide() +{ + Q_Q(QQuickOverlay); + if (!background || modalPopups > 1) + return; + + QQuickPopup *popup = qobject_cast(q->sender()); + if (!popup || !popup->isModal()) + return; + + // use QQmlProperty instead of QQuickItem::setOpacity() to trigger QML Behaviors + QQmlProperty::write(background, QStringLiteral("opacity"), 0.0); +} + +void QQuickOverlayPrivate::drawerPositionChange() +{ + Q_Q(QQuickOverlay); + QQuickDrawer *drawer = qobject_cast(q->sender()); + if (!background || !drawer || modalPopups > 0) + return; + + // call QQuickItem::setOpacity() directly to avoid triggering QML Behaviors + // which would make the fading feel laggy compared to the drawer movement + background->setOpacity(drawer->position()); } void QQuickOverlayPrivate::resizeBackground() @@ -143,13 +157,12 @@ void QQuickOverlay::itemChange(ItemChange change, const ItemChangeData &data) QQuickDrawer *drawer = qobject_cast(data.item); if (drawer) { if (change == ItemChildAddedChange) { - QObjectPrivate::connect(drawer, &QQuickDrawer::positionChanged, d, &QQuickOverlayPrivate::updateBackground); + QObjectPrivate::connect(drawer, &QQuickDrawer::positionChanged, d, &QQuickOverlayPrivate::drawerPositionChange); d->drawers.append(drawer); } else { - QObjectPrivate::disconnect(drawer, &QQuickDrawer::positionChanged, d, &QQuickOverlayPrivate::updateBackground); + QObjectPrivate::disconnect(drawer, &QQuickDrawer::positionChanged, d, &QQuickOverlayPrivate::drawerPositionChange); d->drawers.removeOne(drawer); } - d->updateBackground(); } else { popup = qobject_cast(data.item->parent()); } @@ -171,18 +184,20 @@ void QQuickOverlay::itemChange(ItemChange change, const ItemChangeData &data) connect(this, &QQuickOverlay::pressed, popup, &QQuickPopup::pressedOutside); connect(this, &QQuickOverlay::released, popup, &QQuickPopup::releasedOutside); + QObjectPrivate::connect(popup, &QQuickPopup::aboutToShow, d, &QQuickOverlayPrivate::popupAboutToShow); + QObjectPrivate::connect(popup, &QQuickPopup::aboutToHide, d, &QQuickOverlayPrivate::popupAboutToHide); } else if (change == ItemChildRemovedChange) { Q_ASSERT(popup == d->popups.value(data.item)); disconnect(this, &QQuickOverlay::pressed, popup, &QQuickPopup::pressedOutside); disconnect(this, &QQuickOverlay::released, popup, &QQuickPopup::releasedOutside); + QObjectPrivate::disconnect(popup, &QQuickPopup::aboutToShow, d, &QQuickOverlayPrivate::popupAboutToShow); + QObjectPrivate::disconnect(popup, &QQuickPopup::aboutToHide, d, &QQuickOverlayPrivate::popupAboutToHide); if (popup->isModal()) --d->modalPopups; d->popups.remove(data.item); } - - d->updateBackground(); } void QQuickOverlay::geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry) -- cgit v1.2.3