From 654632128f411807d7429dc7bf9608687604bed5 Mon Sep 17 00:00:00 2001 From: zach Date: Sun, 16 Aug 2026 18:14:04 +0200 Subject: [PATCH] clean-up plugin files --- Plugins/ZShell/Components/carouselview.cpp | 83 ++-------------------- Plugins/ZShell/Components/carouselview.hpp | 33 +++------ 2 files changed, 16 insertions(+), 100 deletions(-) diff --git a/Plugins/ZShell/Components/carouselview.cpp b/Plugins/ZShell/Components/carouselview.cpp index e896c39..e7a8bda 100644 --- a/Plugins/ZShell/Components/carouselview.cpp +++ b/Plugins/ZShell/Components/carouselview.cpp @@ -11,9 +11,9 @@ namespace ZShell::components { namespace { -constexpr qreal kDragThreshold = 4.0; // px before a press becomes a drag -constexpr qreal kFlickFriction = 0.0022; // px/ms^2 deceleration -constexpr qreal kMinFlickVelocity = 60.0; // px/sec below which we just snap +constexpr qreal kDragThreshold = 4.0; +constexpr qreal kFlickFriction = 0.0022; +constexpr qreal kMinFlickVelocity = 60.0; constexpr qreal kMaxFlickVelocity = 6000.0; } // namespace @@ -59,8 +59,6 @@ CarouselView::~CarouselView() { releaseAllItems(); } -// ---------------------------------------------------------------- properties - void CarouselView::setDelegate(QQmlComponent* c) { if (m_delegate == c) return; m_delegate = c; @@ -73,7 +71,6 @@ void CarouselView::setModel(const QVariantList& m) { m_model = m; emit modelChanged(); - // Re-anchor on the currently selected path if possible, otherwise clamp. int newCount = m_model.size(); if (newCount == 0) { m_currentRealIndex = -1; @@ -93,9 +90,6 @@ void CarouselView::setModel(const QVariantList& m) { return; } - // Model contents changed under us (e.g. search text filtered the list). - // Re-anchor instantly on the clamped selection -- no glide, since this - // isn't a user-initiated navigation. rebuildArrangement(); centerInstantlyOnReal(qBound(0, m_currentRealIndex, newCount - 1)); } @@ -173,16 +167,10 @@ void CarouselView::setCurrentIndex(int idx) { goToIndex(idx); } -// ---------------------------------------------------------------- geometry - void CarouselView::geometryChange(const QRectF& newGeo, const QRectF& oldGeo) { QQuickItem::geometryChange(newGeo, oldGeo); if (!qFuzzyCompare(newGeo.width(), oldGeo.width()) || !qFuzzyCompare(newGeo.height(), oldGeo.height())) { - // Recentre on the same virtual index rather than letting contentX - // drift, exactly like the QML version's onWidthChanged handler -- - // but here it happens in the same pass as the arrangement rebuild, - // so x/width for every slot are recomputed together. rebuildArrangement(); if (m_initializedLayout) m_contentX = contentXForIndex(m_currentVirtualIndex); @@ -190,22 +178,10 @@ void CarouselView::geometryChange(const QRectF& newGeo, const QRectF& oldGeo) { } } -// ---------------------------------------------------------------- arrangement math -// Direct port of the QML `arrangement` computed property + keylineCenter/ -// keylineSize/sampleCenter/sampleSize helpers. Kept numerically identical on -// purpose so behaviour doesn't change, just *when* it's applied. - void CarouselView::rebuildArrangement() { Arrangement& a = m_arrangement; a = Arrangement{}; - // IMPORTANT: this must be the external constraint (m_maxWidth), never - // width() -- width() is a *result* of this computation (see below), and - // using it here would make the arrangement depend on its own previous - // output. On first layout, before width() has ever been set, that - // bootstraps to W==0 -> n==0 -> every side item falls back to a flat - // minEdgeWidth with no taper at all, which is what caused items to slide - // off looking "pushed out" instead of shrinking. const qreal W = m_maxWidth; const qreal F = m_focalWidth; const qreal M = m_minEdgeWidth; @@ -239,8 +215,6 @@ void CarouselView::rebuildArrangement() { bestSizes = sizes; bestWidth = requiredWidth; - // Safety valve: arrangement size is bounded by how many halvings fit, - // this just guards against pathological inputs (F <= M etc). if (n > 64) break; } @@ -262,10 +236,6 @@ void CarouselView::rebuildArrangement() { a.effectiveExtraWidth = extra; setImplicitWidth(m_maxWidth); - // This mirrors the original `width: arrangement.width` binding. It's - // safe against feedback because the arrangement above only ever reads - // m_maxWidth, never width() -- so if a.width doesn't change, setWidth() - // here is a no-op and geometryChange() won't recurse. setWidth(a.width); } @@ -331,8 +301,6 @@ qreal CarouselView::sampleSize(qreal childLoc) const { return keylineSize(a) + (keylineSize(b) - keylineSize(a)) * t; } -// ---------------------------------------------------------------- index math - int CarouselView::realIndexOf(int virtualIndex) const { const int count = m_model.size(); if (count == 0) return -1; @@ -373,8 +341,6 @@ void CarouselView::wrapContentIfNeeded() { const qreal newEnd = m_flickAnim->endValue().toReal() - shift; m_flickAnim->setEndValue(newEnd); } - // Also shift every currently-live slot's virtual index bookkeeping so - // it doesn't think it needs to be recycled just because of the wrap. const int indexShift = static_cast(std::round(shift / pitch())); for (Slot& s : m_slots) { if (s.active) s.virtualIndex -= indexShift; @@ -404,8 +370,6 @@ void CarouselView::updateCurrentIndexFromContentX(bool emitPreview) { } } -// ---------------------------------------------------------------- pool / delegates - QQuickItem* CarouselView::acquireItem() { if (!m_delegate) return nullptr; @@ -479,8 +443,6 @@ void CarouselView::positionSlot(const Slot& slot) const { const qreal center = sampleCenter(childLoc); const qreal size = sampleSize(childLoc); - // slot's "column" spans one pitch, centred at width()/2 + center; item is - // centred within that column, matching the QML delegate's x expression. const qreal slotX = childLoc + width() / 2.0 - pitch() / 2.0; const qreal itemX = (center - childLoc) + (pitch() - size) / 2.0 + slotX; @@ -497,8 +459,6 @@ void CarouselView::releaseAllItems() { m_slots.clear(); } -// ---------------------------------------------------------------- layout pass - void CarouselView::relayout() { const int count = m_model.size(); if (count == 0 || width() <= 0 || m_focalWidth <= 0) { @@ -508,20 +468,15 @@ void CarouselView::relayout() { } const int n = m_arrangement.n; - // Window of virtual indices that can ever be visible, plus a small buffer - // so items don't pop in/out right at the edge during a fast flick. const int half = n + 3; const int lo = m_currentVirtualIndex - half; const int hi = m_currentVirtualIndex + half; const int needed = hi - lo + 1; - // Grow the pool if needed (shrinking is unnecessary: idle items are just - // marked inactive/invisible, which is cheap and avoids churn). while (m_slots.size() < needed) m_slots.append(Slot{}); - // Figure out which virtual indices are already backed by a slot. - QHash virtualToSlot; // virtualIndex -> slot list position + QHash virtualToSlot; virtualToSlot.reserve(m_slots.size()); for (int i = 0; i < m_slots.size(); ++i) { if (m_slots[i].active) virtualToSlot.insert(m_slots[i].virtualIndex, i); @@ -533,15 +488,11 @@ void CarouselView::relayout() { auto it = virtualToSlot.find(v); if (it != virtualToSlot.end()) { slotUsedThisPass[it.value()] = true; - // Content (isCurrent / modelData) may still need refreshing if - // currentRealIndex changed since last pass. applyPropertiesToItem( m_slots[it.value()].item, m_slots[it.value()].realIndex); continue; } - // find a free slot: one not used this pass and not already at a - // virtual index inside [lo, hi] (those are all still needed). int freeSlot = -1; for (int i = 0; i < m_slots.size(); ++i) { if (slotUsedThisPass[i]) continue; @@ -552,7 +503,6 @@ void CarouselView::relayout() { break; } if (freeSlot < 0) { - // Shouldn't happen given the pool growth above, but guard anyway. m_slots.append(Slot{}); slotUsedThisPass.append(false); freeSlot = m_slots.size() - 1; @@ -573,8 +523,6 @@ void CarouselView::relayout() { positionSlot(s); } -// ---------------------------------------------------------------- animation - void CarouselView::cancelAnimations() { if (m_glideAnim->state() == QAbstractAnimation::Running) m_glideAnim->stop(); @@ -596,8 +544,6 @@ void CarouselView::snapToNearest() { glideTo(snapTargetX(m_contentX)); } -// ---------------------------------------------------------------- public API - void CarouselView::goToIndex(int realIndex) { const int count = m_model.size(); if (count == 0) return; @@ -659,19 +605,10 @@ void CarouselView::centerInstantlyOnReal(int realIndex) { relayout(); } -// ---------------------------------------------------------------- input - void CarouselView::mousePressEvent(QMouseEvent* event) { - // Deliberately do NOT cancel animations here. A press that turns out to - // be a plain tap (never crosses the drag threshold) should leave any - // in-flight glide/flick completely untouched. We only touch the - // animation once we know it's actually a drag (see mouseMoveEvent). m_dragging = true; m_dragActive = false; m_pressPos = event->position(); - // Capture wherever contentX currently is, even mid-animation -- if this - // does turn into a drag, it should pick up smoothly from the visual - // position, not teleport to some rest position first. m_pressContentX = m_contentX; m_dragTimer.start(); m_lastMoveX = event->position().x(); @@ -686,8 +623,7 @@ void CarouselView::mouseMoveEvent(QMouseEvent* event) { const qreal dx = event->position().x() - m_pressPos.x(); if (!m_dragActive && std::abs(dx) > kDragThreshold) { m_dragActive = true; - m_pressContentX = - m_contentX; // re-anchor to the live (possibly mid-animation) value + m_pressContentX = m_contentX; cancelAnimations(); grabMouse(); } @@ -703,7 +639,6 @@ void CarouselView::mouseMoveEvent(QMouseEvent* event) { if (dt > 0) { const qreal instVel = (m_lastMoveX - event->position().x()) / (dt / 1000.0); - // Light smoothing so a single jittery sample doesn't dominate the flick. m_velocity = m_velocity * 0.7 + instVel * 0.3; } m_lastMoveX = event->position().x(); @@ -718,8 +653,6 @@ void CarouselView::mouseReleaseEvent(QMouseEvent* event) { ungrabMouse(); if (!m_dragActive) { - // A plain click/tap with no drag: let it fall through to the - // delegate's own StateLayer/MouseArea via requestActivate(). event->accept(); return; } @@ -732,10 +665,8 @@ void CarouselView::mouseReleaseEvent(QMouseEvent* event) { return; } - // Simple constant-deceleration flick: distance = v^2 / (2*friction). const qreal duration = std::abs(v) / (kFlickFriction * 1000.0); // ms - const qreal distance = - (v * (duration / 1000.0)) / 2.0; // average-velocity approximation + const qreal distance = (v * (duration / 1000.0)) / 2.0; const qreal rawDest = m_contentX + distance; const qreal dest = snapTargetX(rawDest); @@ -764,8 +695,6 @@ void CarouselView::wheelEvent(QWheelEvent* event) { event->accept(); } -// ---------------------------------------------------------------- delegate signal - void CarouselView::handleDelegateActivate() { QQuickItem* item = qobject_cast(sender()); if (!item) return; diff --git a/Plugins/ZShell/Components/carouselview.hpp b/Plugins/ZShell/Components/carouselview.hpp index 23a8e36..2925cf9 100644 --- a/Plugins/ZShell/Components/carouselview.hpp +++ b/Plugins/ZShell/Components/carouselview.hpp @@ -47,8 +47,6 @@ class CarouselView : public QQuickItem { QEasingCurve::Type glideEasingType READ glideEasingType WRITE setGlideEasingType NOTIFY glideEasingTypeChanged) - // Extra properties (e.g. { visibilities: root.visibilities }) forwarded - // verbatim onto every delegate instance as a property of the same name. Q_PROPERTY( QVariantMap delegateProperties READ delegateProperties WRITE setDelegateProperties NOTIFY delegatePropertiesChanged) @@ -79,7 +77,7 @@ class CarouselView : public QQuickItem { void setMaxWidth(qreal v); int currentIndex() const { return m_currentRealIndex; } - void setCurrentIndex(int idx); // jumps (animated glide) to a real index + void setCurrentIndex(int idx); int glideDuration() const { return m_glideDuration; } void setGlideDuration(int v); @@ -90,13 +88,10 @@ class CarouselView : public QQuickItem { QVariantMap delegateProperties() const { return m_delegateProperties; } void setDelegateProperties(const QVariantMap& v); - // Animated (glides). Q_INVOKABLE void goToIndex(int realIndex); Q_INVOKABLE void incrementCurrentIndex(); Q_INVOKABLE void decrementCurrentIndex(); - // Instant, no animation -- use for initial placement (e.g. restoring the - // saved wallpaper on Component.onCompleted) where a glide would be wrong. Q_INVOKABLE void jumpToIndex(int realIndex); signals: @@ -112,11 +107,7 @@ class CarouselView : public QQuickItem { void glideEasingTypeChanged(); void delegatePropertiesChanged(); - // Emitted when the currently-centred delegate is clicked while already - // centred (i.e. "activate/select this one"). void activated(int realIndex, QVariant modelData); - // Emitted continuously as the centred item changes, for preview-on-scroll - // behaviour (used to drive Wallpapers.preview() from QML). void previewIndexChanged(int realIndex, QVariant modelData); protected: @@ -134,22 +125,20 @@ class CarouselView : public QQuickItem { struct Arrangement { int n = 0; qreal width = 0; - QVector sizes; // sizes[0] = focal, sizes[i] = i-th side item - QVector - centers; // centers[0] = 0, centers[i] = i-th side centre offset + QVector sizes; + QVector centers; qreal effectiveExtraWidth = 0; }; struct Slot { QQuickItem* item = nullptr; - int virtualIndex = - 0; // which "virtual" (unbounded) index this slot currently renders - int realIndex = -1; // resolved model index, -1 if none / hidden + int virtualIndex = 0; + int realIndex = -1; bool active = false; }; void rebuildArrangement(); - void relayout(); // recomputes window + positions every slot (one pass) + void relayout(); void updateSlotContent(Slot& slot, int virtualIndex); void positionSlot(const Slot& slot) const; void applyPropertiesToItem(QQuickItem* item, int realIndex) const; @@ -170,7 +159,7 @@ class CarouselView : public QQuickItem { int realIndexOf(int virtualIndex) const; void updateCurrentIndexFromContentX(bool emitPreview); void wrapContentIfNeeded(); - void centerInstantlyOnReal(int realIndex); // no animation, no glide + void centerInstantlyOnReal(int realIndex); QQuickItem* acquireItem(); void releaseAllItems(); @@ -195,22 +184,20 @@ class CarouselView : public QQuickItem { static constexpr int kLoopMultiplier = 401; int m_centerBlockStart = 0; - QList - m_slots; // pooled delegate slots, unordered; window recomputed each relayout + QList m_slots; int m_glideDuration = 250; QEasingCurve::Type m_glideEasingType = QEasingCurve::OutCubic; QVariantAnimation* m_glideAnim = nullptr; - // Drag / flick state bool m_dragging = false; - bool m_dragActive = false; // exceeded the drag threshold + bool m_dragActive = false; QPointF m_pressPos; qreal m_pressContentX = 0; QElapsedTimer m_dragTimer; qreal m_lastMoveX = 0; qint64 m_lastMoveT = 0; - qreal m_velocity = 0; // px/sec + qreal m_velocity = 0; QVariantAnimation* m_flickAnim = nullptr; bool m_initializedLayout = false;