clean-up plugin files
C++ / fmt (pull_request) Successful in 4s
JS/TS / fmt (pull_request) Successful in 9s
JS/TS / lint (pull_request) Successful in 22s
Rust / fmt (pull_request) Successful in 56s
Python / static (pull_request) Successful in 1m0s
C++ / build (pull_request) Successful in 2m9s
Rust / build (pull_request) Successful in 2m8s
Rust / clippy (pull_request) Successful in 1m9s
Python / verify (pull_request) Successful in 2m57s
C++ / clang-tidy (pull_request) Successful in 3m58s

This commit is contained in:
2026-08-16 18:14:04 +02:00
parent 4037a1dd0f
commit 654632128f
2 changed files with 16 additions and 100 deletions
+6 -77
View File
@@ -11,9 +11,9 @@
namespace ZShell::components { namespace ZShell::components {
namespace { namespace {
constexpr qreal kDragThreshold = 4.0; // px before a press becomes a drag constexpr qreal kDragThreshold = 4.0;
constexpr qreal kFlickFriction = 0.0022; // px/ms^2 deceleration constexpr qreal kFlickFriction = 0.0022;
constexpr qreal kMinFlickVelocity = 60.0; // px/sec below which we just snap constexpr qreal kMinFlickVelocity = 60.0;
constexpr qreal kMaxFlickVelocity = 6000.0; constexpr qreal kMaxFlickVelocity = 6000.0;
} // namespace } // namespace
@@ -59,8 +59,6 @@ CarouselView::~CarouselView() {
releaseAllItems(); releaseAllItems();
} }
// ---------------------------------------------------------------- properties
void CarouselView::setDelegate(QQmlComponent* c) { void CarouselView::setDelegate(QQmlComponent* c) {
if (m_delegate == c) return; if (m_delegate == c) return;
m_delegate = c; m_delegate = c;
@@ -73,7 +71,6 @@ void CarouselView::setModel(const QVariantList& m) {
m_model = m; m_model = m;
emit modelChanged(); emit modelChanged();
// Re-anchor on the currently selected path if possible, otherwise clamp.
int newCount = m_model.size(); int newCount = m_model.size();
if (newCount == 0) { if (newCount == 0) {
m_currentRealIndex = -1; m_currentRealIndex = -1;
@@ -93,9 +90,6 @@ void CarouselView::setModel(const QVariantList& m) {
return; 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(); rebuildArrangement();
centerInstantlyOnReal(qBound(0, m_currentRealIndex, newCount - 1)); centerInstantlyOnReal(qBound(0, m_currentRealIndex, newCount - 1));
} }
@@ -173,16 +167,10 @@ void CarouselView::setCurrentIndex(int idx) {
goToIndex(idx); goToIndex(idx);
} }
// ---------------------------------------------------------------- geometry
void CarouselView::geometryChange(const QRectF& newGeo, const QRectF& oldGeo) { void CarouselView::geometryChange(const QRectF& newGeo, const QRectF& oldGeo) {
QQuickItem::geometryChange(newGeo, oldGeo); QQuickItem::geometryChange(newGeo, oldGeo);
if (!qFuzzyCompare(newGeo.width(), oldGeo.width()) || if (!qFuzzyCompare(newGeo.width(), oldGeo.width()) ||
!qFuzzyCompare(newGeo.height(), oldGeo.height())) { !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(); rebuildArrangement();
if (m_initializedLayout) if (m_initializedLayout)
m_contentX = contentXForIndex(m_currentVirtualIndex); 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() { void CarouselView::rebuildArrangement() {
Arrangement& a = m_arrangement; Arrangement& a = m_arrangement;
a = 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 W = m_maxWidth;
const qreal F = m_focalWidth; const qreal F = m_focalWidth;
const qreal M = m_minEdgeWidth; const qreal M = m_minEdgeWidth;
@@ -239,8 +215,6 @@ void CarouselView::rebuildArrangement() {
bestSizes = sizes; bestSizes = sizes;
bestWidth = requiredWidth; 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; if (n > 64) break;
} }
@@ -262,10 +236,6 @@ void CarouselView::rebuildArrangement() {
a.effectiveExtraWidth = extra; a.effectiveExtraWidth = extra;
setImplicitWidth(m_maxWidth); 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); setWidth(a.width);
} }
@@ -331,8 +301,6 @@ qreal CarouselView::sampleSize(qreal childLoc) const {
return keylineSize(a) + (keylineSize(b) - keylineSize(a)) * t; return keylineSize(a) + (keylineSize(b) - keylineSize(a)) * t;
} }
// ---------------------------------------------------------------- index math
int CarouselView::realIndexOf(int virtualIndex) const { int CarouselView::realIndexOf(int virtualIndex) const {
const int count = m_model.size(); const int count = m_model.size();
if (count == 0) return -1; if (count == 0) return -1;
@@ -373,8 +341,6 @@ void CarouselView::wrapContentIfNeeded() {
const qreal newEnd = m_flickAnim->endValue().toReal() - shift; const qreal newEnd = m_flickAnim->endValue().toReal() - shift;
m_flickAnim->setEndValue(newEnd); 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<int>(std::round(shift / pitch())); const int indexShift = static_cast<int>(std::round(shift / pitch()));
for (Slot& s : m_slots) { for (Slot& s : m_slots) {
if (s.active) s.virtualIndex -= indexShift; if (s.active) s.virtualIndex -= indexShift;
@@ -404,8 +370,6 @@ void CarouselView::updateCurrentIndexFromContentX(bool emitPreview) {
} }
} }
// ---------------------------------------------------------------- pool / delegates
QQuickItem* CarouselView::acquireItem() { QQuickItem* CarouselView::acquireItem() {
if (!m_delegate) return nullptr; if (!m_delegate) return nullptr;
@@ -479,8 +443,6 @@ void CarouselView::positionSlot(const Slot& slot) const {
const qreal center = sampleCenter(childLoc); const qreal center = sampleCenter(childLoc);
const qreal size = sampleSize(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 slotX = childLoc + width() / 2.0 - pitch() / 2.0;
const qreal itemX = (center - childLoc) + (pitch() - size) / 2.0 + slotX; const qreal itemX = (center - childLoc) + (pitch() - size) / 2.0 + slotX;
@@ -497,8 +459,6 @@ void CarouselView::releaseAllItems() {
m_slots.clear(); m_slots.clear();
} }
// ---------------------------------------------------------------- layout pass
void CarouselView::relayout() { void CarouselView::relayout() {
const int count = m_model.size(); const int count = m_model.size();
if (count == 0 || width() <= 0 || m_focalWidth <= 0) { if (count == 0 || width() <= 0 || m_focalWidth <= 0) {
@@ -508,20 +468,15 @@ void CarouselView::relayout() {
} }
const int n = m_arrangement.n; 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 half = n + 3;
const int lo = m_currentVirtualIndex - half; const int lo = m_currentVirtualIndex - half;
const int hi = m_currentVirtualIndex + half; const int hi = m_currentVirtualIndex + half;
const int needed = hi - lo + 1; 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) while (m_slots.size() < needed)
m_slots.append(Slot{}); m_slots.append(Slot{});
// Figure out which virtual indices are already backed by a slot. QHash<int, int> virtualToSlot;
QHash<int, int> virtualToSlot; // virtualIndex -> slot list position
virtualToSlot.reserve(m_slots.size()); virtualToSlot.reserve(m_slots.size());
for (int i = 0; i < m_slots.size(); ++i) { for (int i = 0; i < m_slots.size(); ++i) {
if (m_slots[i].active) virtualToSlot.insert(m_slots[i].virtualIndex, 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); auto it = virtualToSlot.find(v);
if (it != virtualToSlot.end()) { if (it != virtualToSlot.end()) {
slotUsedThisPass[it.value()] = true; slotUsedThisPass[it.value()] = true;
// Content (isCurrent / modelData) may still need refreshing if
// currentRealIndex changed since last pass.
applyPropertiesToItem( applyPropertiesToItem(
m_slots[it.value()].item, m_slots[it.value()].realIndex); m_slots[it.value()].item, m_slots[it.value()].realIndex);
continue; 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; int freeSlot = -1;
for (int i = 0; i < m_slots.size(); ++i) { for (int i = 0; i < m_slots.size(); ++i) {
if (slotUsedThisPass[i]) continue; if (slotUsedThisPass[i]) continue;
@@ -552,7 +503,6 @@ void CarouselView::relayout() {
break; break;
} }
if (freeSlot < 0) { if (freeSlot < 0) {
// Shouldn't happen given the pool growth above, but guard anyway.
m_slots.append(Slot{}); m_slots.append(Slot{});
slotUsedThisPass.append(false); slotUsedThisPass.append(false);
freeSlot = m_slots.size() - 1; freeSlot = m_slots.size() - 1;
@@ -573,8 +523,6 @@ void CarouselView::relayout() {
positionSlot(s); positionSlot(s);
} }
// ---------------------------------------------------------------- animation
void CarouselView::cancelAnimations() { void CarouselView::cancelAnimations() {
if (m_glideAnim->state() == QAbstractAnimation::Running) if (m_glideAnim->state() == QAbstractAnimation::Running)
m_glideAnim->stop(); m_glideAnim->stop();
@@ -596,8 +544,6 @@ void CarouselView::snapToNearest() {
glideTo(snapTargetX(m_contentX)); glideTo(snapTargetX(m_contentX));
} }
// ---------------------------------------------------------------- public API
void CarouselView::goToIndex(int realIndex) { void CarouselView::goToIndex(int realIndex) {
const int count = m_model.size(); const int count = m_model.size();
if (count == 0) return; if (count == 0) return;
@@ -659,19 +605,10 @@ void CarouselView::centerInstantlyOnReal(int realIndex) {
relayout(); relayout();
} }
// ---------------------------------------------------------------- input
void CarouselView::mousePressEvent(QMouseEvent* event) { 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_dragging = true;
m_dragActive = false; m_dragActive = false;
m_pressPos = event->position(); 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_pressContentX = m_contentX;
m_dragTimer.start(); m_dragTimer.start();
m_lastMoveX = event->position().x(); m_lastMoveX = event->position().x();
@@ -686,8 +623,7 @@ void CarouselView::mouseMoveEvent(QMouseEvent* event) {
const qreal dx = event->position().x() - m_pressPos.x(); const qreal dx = event->position().x() - m_pressPos.x();
if (!m_dragActive && std::abs(dx) > kDragThreshold) { if (!m_dragActive && std::abs(dx) > kDragThreshold) {
m_dragActive = true; m_dragActive = true;
m_pressContentX = m_pressContentX = m_contentX;
m_contentX; // re-anchor to the live (possibly mid-animation) value
cancelAnimations(); cancelAnimations();
grabMouse(); grabMouse();
} }
@@ -703,7 +639,6 @@ void CarouselView::mouseMoveEvent(QMouseEvent* event) {
if (dt > 0) { if (dt > 0) {
const qreal instVel = const qreal instVel =
(m_lastMoveX - event->position().x()) / (dt / 1000.0); (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_velocity = m_velocity * 0.7 + instVel * 0.3;
} }
m_lastMoveX = event->position().x(); m_lastMoveX = event->position().x();
@@ -718,8 +653,6 @@ void CarouselView::mouseReleaseEvent(QMouseEvent* event) {
ungrabMouse(); ungrabMouse();
if (!m_dragActive) { 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(); event->accept();
return; return;
} }
@@ -732,10 +665,8 @@ void CarouselView::mouseReleaseEvent(QMouseEvent* event) {
return; return;
} }
// Simple constant-deceleration flick: distance = v^2 / (2*friction).
const qreal duration = std::abs(v) / (kFlickFriction * 1000.0); // ms const qreal duration = std::abs(v) / (kFlickFriction * 1000.0); // ms
const qreal distance = const qreal distance = (v * (duration / 1000.0)) / 2.0;
(v * (duration / 1000.0)) / 2.0; // average-velocity approximation
const qreal rawDest = m_contentX + distance; const qreal rawDest = m_contentX + distance;
const qreal dest = snapTargetX(rawDest); const qreal dest = snapTargetX(rawDest);
@@ -764,8 +695,6 @@ void CarouselView::wheelEvent(QWheelEvent* event) {
event->accept(); event->accept();
} }
// ---------------------------------------------------------------- delegate signal
void CarouselView::handleDelegateActivate() { void CarouselView::handleDelegateActivate() {
QQuickItem* item = qobject_cast<QQuickItem*>(sender()); QQuickItem* item = qobject_cast<QQuickItem*>(sender());
if (!item) return; if (!item) return;
+10 -23
View File
@@ -47,8 +47,6 @@ class CarouselView : public QQuickItem {
QEasingCurve::Type glideEasingType READ glideEasingType WRITE QEasingCurve::Type glideEasingType READ glideEasingType WRITE
setGlideEasingType NOTIFY glideEasingTypeChanged) 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( Q_PROPERTY(
QVariantMap delegateProperties READ delegateProperties WRITE QVariantMap delegateProperties READ delegateProperties WRITE
setDelegateProperties NOTIFY delegatePropertiesChanged) setDelegateProperties NOTIFY delegatePropertiesChanged)
@@ -79,7 +77,7 @@ class CarouselView : public QQuickItem {
void setMaxWidth(qreal v); void setMaxWidth(qreal v);
int currentIndex() const { return m_currentRealIndex; } 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; } int glideDuration() const { return m_glideDuration; }
void setGlideDuration(int v); void setGlideDuration(int v);
@@ -90,13 +88,10 @@ class CarouselView : public QQuickItem {
QVariantMap delegateProperties() const { return m_delegateProperties; } QVariantMap delegateProperties() const { return m_delegateProperties; }
void setDelegateProperties(const QVariantMap& v); void setDelegateProperties(const QVariantMap& v);
// Animated (glides).
Q_INVOKABLE void goToIndex(int realIndex); Q_INVOKABLE void goToIndex(int realIndex);
Q_INVOKABLE void incrementCurrentIndex(); Q_INVOKABLE void incrementCurrentIndex();
Q_INVOKABLE void decrementCurrentIndex(); 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); Q_INVOKABLE void jumpToIndex(int realIndex);
signals: signals:
@@ -112,11 +107,7 @@ class CarouselView : public QQuickItem {
void glideEasingTypeChanged(); void glideEasingTypeChanged();
void delegatePropertiesChanged(); 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); 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); void previewIndexChanged(int realIndex, QVariant modelData);
protected: protected:
@@ -134,22 +125,20 @@ class CarouselView : public QQuickItem {
struct Arrangement { struct Arrangement {
int n = 0; int n = 0;
qreal width = 0; qreal width = 0;
QVector<qreal> sizes; // sizes[0] = focal, sizes[i] = i-th side item QVector<qreal> sizes;
QVector<qreal> QVector<qreal> centers;
centers; // centers[0] = 0, centers[i] = i-th side centre offset
qreal effectiveExtraWidth = 0; qreal effectiveExtraWidth = 0;
}; };
struct Slot { struct Slot {
QQuickItem* item = nullptr; QQuickItem* item = nullptr;
int virtualIndex = int virtualIndex = 0;
0; // which "virtual" (unbounded) index this slot currently renders int realIndex = -1;
int realIndex = -1; // resolved model index, -1 if none / hidden
bool active = false; bool active = false;
}; };
void rebuildArrangement(); void rebuildArrangement();
void relayout(); // recomputes window + positions every slot (one pass) void relayout();
void updateSlotContent(Slot& slot, int virtualIndex); void updateSlotContent(Slot& slot, int virtualIndex);
void positionSlot(const Slot& slot) const; void positionSlot(const Slot& slot) const;
void applyPropertiesToItem(QQuickItem* item, int realIndex) const; void applyPropertiesToItem(QQuickItem* item, int realIndex) const;
@@ -170,7 +159,7 @@ class CarouselView : public QQuickItem {
int realIndexOf(int virtualIndex) const; int realIndexOf(int virtualIndex) const;
void updateCurrentIndexFromContentX(bool emitPreview); void updateCurrentIndexFromContentX(bool emitPreview);
void wrapContentIfNeeded(); void wrapContentIfNeeded();
void centerInstantlyOnReal(int realIndex); // no animation, no glide void centerInstantlyOnReal(int realIndex);
QQuickItem* acquireItem(); QQuickItem* acquireItem();
void releaseAllItems(); void releaseAllItems();
@@ -195,22 +184,20 @@ class CarouselView : public QQuickItem {
static constexpr int kLoopMultiplier = 401; static constexpr int kLoopMultiplier = 401;
int m_centerBlockStart = 0; int m_centerBlockStart = 0;
QList<Slot> QList<Slot> m_slots;
m_slots; // pooled delegate slots, unordered; window recomputed each relayout
int m_glideDuration = 250; int m_glideDuration = 250;
QEasingCurve::Type m_glideEasingType = QEasingCurve::OutCubic; QEasingCurve::Type m_glideEasingType = QEasingCurve::OutCubic;
QVariantAnimation* m_glideAnim = nullptr; QVariantAnimation* m_glideAnim = nullptr;
// Drag / flick state
bool m_dragging = false; bool m_dragging = false;
bool m_dragActive = false; // exceeded the drag threshold bool m_dragActive = false;
QPointF m_pressPos; QPointF m_pressPos;
qreal m_pressContentX = 0; qreal m_pressContentX = 0;
QElapsedTimer m_dragTimer; QElapsedTimer m_dragTimer;
qreal m_lastMoveX = 0; qreal m_lastMoveX = 0;
qint64 m_lastMoveT = 0; qint64 m_lastMoveT = 0;
qreal m_velocity = 0; // px/sec qreal m_velocity = 0;
QVariantAnimation* m_flickAnim = nullptr; QVariantAnimation* m_flickAnim = nullptr;
bool m_initializedLayout = false; bool m_initializedLayout = false;