Files
qbz/crates/qbz-slint/ui/immersive/CoverflowPanel.slint
T
vicrodh 5ded7b1610 perf(slint): immersive Coverflow - persistent elements + animated scroll (CSS/iPhone model)
Rebuild that finally kills the per-click CPU spike, using the classic
coverflow architecture (the owner's reference: Addy Osmani / iPhone-2007).

Root cause of the spike (now removed): every navigation re-ran refresh_async
which REPLACED the coverflow VecModels, so Slint dropped + rebuilt all 7 bound
Image elements and re-uploaded/re-decoded every cover on each click.

New architecture:
- A single STABLE flat cover model, QueueState.coverflow-tracks =
  [history oldest..newest, NOW, upcoming...], + an int coverflow-index. An
  order-sensitive id-sequence hash gates the model setter: a pure track advance
  (same sequence, pointer moves) does NOT rebuild the model - only the index
  updates. So the Repeater is never rebuilt and visible covers never re-decode.
  Rebuild happens only on a real content change (new queue / shuffle / add /
  remove).
- CoverflowPanel: a Repeater over coverflow-tracks; each card's transform is a
  pure function of offset = index - scroll. `scroll` is a float bound to
  coverflow-index with `animate scroll { 320ms }` - GPU-composited slide that
  repaints only during the transition then idles (no animation-tick / Timer, so
  idle = no continuous render). Flat scaled cards (no rotateY/skew/reflection),
  per-depth black-overlay dim, center painted last + non-interactive, ~7 visible.
- Windowed lazy decode (+-4) reusing the DECODED_PIXEL_CACHE; at most the one
  newly-entering cover decodes per advance.
- Side cards navigate via queue-wide play_history / play_coverflow_upcoming.
- Bonus: atmosphere bg de-dupes by URL (ATMOSPHERE_LAST_URL) so a track change
  no longer re-blurs the same cover. [coverflow-perf] debug timers added.

cc
2026-06-13 21:34:30 -06:00

328 lines
14 KiB
Plaintext

// Coverflow foreground (Immersive mode 2) — a CHEAP classic-CSS / iPhone-2007
// coverflow built on the PERSISTENT-ELEMENT + ANIMATED-SCROLL architecture.
//
// ARCHITECTURE (binding — replaces the old 7 fixed-slot design):
// 1. PERSISTENT covers. The covers are a SINGLE stable, ordered list of the
// whole queue — `QueueState.coverflow-tracks` = [history oldest..newest,
// NOW-PLAYING, upcoming...]. This list + its decoded cover images do NOT
// change on a pure track advance/navigation; only the int
// `QueueState.coverflow-index` (the flat position of NOW) moves. The Rust
// controller rebuilds the model ONLY when the queue CONTENTS change
// (id-sequence diff). So a track advance NEVER rebuilds this Repeater and
// NEVER re-decodes a visible cover.
// 2. ANIMATED SCROLL. A single float `scroll` is bound to `coverflow-index`
// with `animate scroll { 320ms ease-in-out }`. When Rust sets the new int
// index, `scroll` EASES to it and the fan repaints ONLY during the 320ms,
// then idles. NO `animation-tick` (that would force continuous fps = the
// CPU cost we are avoiding); NO Timer.
// 3. TRANSFORM-ONLY layout. Each cover i derives offset = i - scroll;
// x = center + offset*pitch; size = base * scale(|offset|); a per-depth
// black overlay dims by |offset| on an INNER Rectangle (never root
// opacity). Covers with |offset| > 3 are `visible:false` (~7 on screen).
// This is CSS `transform: translateX()+scale()` done with Slint geometry —
// FLAT scaled cards (NO rotateY/skew/perspective/reflection; Slint has no
// 3D and the owner accepted flat).
// 4. NAVIGATION. A side card at flat index i plays that queue position by
// mapping i -> the existing queue-wide play-history / play-coverflow-
// upcoming calls; the subsequent refresh moves `coverflow-index` and
// `scroll` animates to it. The center (offset ~0) is non-interactive.
//
// SLINT GOTCHAS honored: `parent` is NEVER read in any root binding (viewport
// dims + the per-cover stage geometry are passed as props). Everything derives
// from the DATA value `i` and the `scroll` float — NO `changed x/y/width/height`
// watchers on rows (avoids the "Recursion detected" panic). `dim` is on an INNER
// Rectangle, never the slot root opacity. Math.min/max are 2-arg; clamp =
// Math.max(lo, Math.min(hi, x)). The center card is a SEPARATE top-most instance
// (Repeater can't reorder by data) so it paints over its neighbors.
import { QualityBadgeFull } from "../primitives/QualityBadgeFull.slint";
import { NowPlayingState, QueueState } from "../state.slint";
// Four-bar "now playing" equalizer. Continuous loop driven by the global
// animation-tick() (pull-based) — NOT a Timer. Verbatim copy of the
// StaticPanel.slint EqualizerBars; duplicated on purpose so this session does
// not touch the siblings. (Follow-up: lift into a shared EqualizerBars.slint.)
component EqualizerBars inherits Rectangle {
in property <color> tint: #7c3aed;
width: 18px;
height: 14px;
property <float> t: Math.mod(animation-tick() / 1ms, 800.0) / 800.0;
function wave(phase: float) -> float {
return 0.3 + 0.7 * (0.5 - 0.5 * Math.cos((Math.mod(self.t + phase, 1.0)) * 6.283185 * 1rad));
}
HorizontalLayout {
alignment: start;
spacing: 2px;
Rectangle {
width: 3px;
y: parent.height - self.height;
height: parent.height * 0.6 * root.wave(0.0);
border-radius: 1px;
background: root.tint;
}
Rectangle {
width: 3px;
y: parent.height - self.height;
height: parent.height * 1.0 * root.wave(0.25);
border-radius: 1px;
background: root.tint;
}
Rectangle {
width: 3px;
y: parent.height - self.height;
height: parent.height * 0.4 * root.wave(0.125);
border-radius: 1px;
background: root.tint;
}
Rectangle {
width: 3px;
y: parent.height - self.height;
height: parent.height * 0.8 * root.wave(0.375);
border-radius: 1px;
background: root.tint;
}
}
}
// One flat coverflow card. ALL geometry is derived from `offset` (= i - scroll)
// + the stage props passed in (NEVER `parent` in the root binding). The card
// position/size/dim are pure functions of `offset`, so when the parent's
// animated `scroll` changes, every card eases to its new transform with NO
// model change and NO source reassignment. `dim` is on an INNER Rectangle.
component CoverCard inherits Rectangle {
in property <image> art;
in property <bool> has-art: false;
in property <float> offset; // signed distance from center (i - scroll)
in property <length> base-art; // depth-0 (center) edge length
in property <length> pitch; // x spacing between adjacent depths
in property <length> stage-cx; // window-x of the center
in property <length> stage-height;
in property <bool> interactive: false;
callback navigate();
property <float> ao: Math.abs(root.offset);
// Step-down scale: center 1.0 -> ~0.5 at |offset| >= ~2.8.
property <float> scale: Math.max(0.5, 1.0 - 0.18 * root.ao);
property <length> slot: root.base-art * root.scale;
// Per-depth black overlay: 0 at center, up to 0.55 outward.
property <float> dim: Math.min(0.55, root.ao * 0.18);
// Only ~7 cards near the center paint; the rest are dropped (no texture
// cost from off-screen rows — visible:false stops painting).
visible: root.ao <= 3.2;
width: root.slot;
height: root.slot;
// Center the card on (stage-cx + offset*pitch); small upward bias to leave
// room for the song card below.
x: root.stage-cx + root.offset * root.pitch - root.slot / 2;
y: (root.stage-height - root.slot) / 2 - 12px;
border-radius: 8px;
clip: true;
// Static drop-shadow on (near-)center cards only — the attenuation carries
// the depth for the side cards. Never animated.
drop-shadow-blur: root.ao < 0.5 ? 28px : 0px;
drop-shadow-color: #00000066;
drop-shadow-offset-y: root.ao < 0.5 ? 8px : 0px;
// Neutral placeholder when the item exists but its art is unresolved.
Rectangle {
width: parent.width;
height: parent.height;
border-radius: 8px;
background: #ffffff14;
visible: !root.has-art;
}
Image {
source: root.art;
image-fit: cover;
width: parent.width;
height: parent.height;
visible: root.has-art;
}
// Per-depth attenuation on an INNER Rectangle (never the root opacity).
Rectangle {
width: parent.width;
height: parent.height;
border-radius: 8px;
background: #000000;
opacity: root.dim;
}
// Side cards navigate; the center is non-interactive.
if root.interactive && root.ao > 0.5: TouchArea {
mouse-cursor: pointer;
clicked => { root.navigate(); }
}
}
export component CoverflowPanel inherits Rectangle {
in property <length> viewport-width: 1280px;
in property <length> viewport-height: 720px;
width: root.viewport-width;
height: root.viewport-height;
background: transparent;
// --- Geometry --------------------------------------------------------
// Center cover edge (base-art = depth-0 scale). Honors the 132px player
// clearance: reserve = pad-top(52) + info + clearance(132) = 364px.
property <length> base-art: Math.max(160px,
Math.min(Math.min(root.viewport-height - 364px, root.viewport-width * 0.42), 440px));
// X spacing between adjacent depths (overlapping inward).
property <length> pitch: root.base-art * 0.42;
// --- Animated scroll (the ONLY motion) -------------------------------
// Bound to the int current-index; Rust moving the index eases `scroll` over
// 320ms (repaints only during the transition, then idles).
property <float> scroll: QueueState.coverflow-index;
animate scroll { duration: 320ms; easing: ease-in-out; }
// Center card art prefers the unified large now-playing cover (sharper),
// falling back to the model row's small cover while it resolves.
property <int> center-idx: QueueState.coverflow-index;
property <image> center-art: NowPlayingState.artwork-large.width > 0
? NowPlayingState.artwork-large
: NowPlayingState.artwork;
property <bool> center-has-art: root.center-art.width > 0;
// --- Cover fan stage -------------------------------------------------
Rectangle {
x: 0px;
y: 52px; // pad-top, matches the siblings
width: root.viewport-width;
height: root.base-art + 56px;
property <length> cx: root.viewport-width / 2;
// All side + far covers from the stable flat model. The center is drawn
// separately (below) so it paints on top — a Repeater renders in data
// order and cannot reorder by |offset|.
for item[i] in QueueState.coverflow-tracks: CoverCard {
offset: i - root.scroll;
base-art: root.base-art;
pitch: root.pitch;
stage-cx: parent.cx;
stage-height: parent.height;
art: item.artwork;
has-art: item.artwork.width > 0;
// The center (i == current index) is drawn by the top instance; this
// Repeater copy stays non-interactive and hides exactly at center so
// there is no double-draw flicker through the 320ms ease.
interactive: i != root.center-idx;
navigate => {
// Flat index -> queue-wide play call. Flat order is
// [history.reversed (oldest-first) ++ NOW ++ upcoming], and
// center-idx = history.len():
// i < center-idx -> a history cover. flat history is
// oldest-first while QueueStateFull.history is most-recent-
// first, so hist_index = center-idx - 1 - i.
// i > center-idx -> an upcoming cover.
// up_index = i - center-idx - 1.
if (i < root.center-idx) {
QueueState.play-history(root.center-idx - 1 - i);
} else if (i > root.center-idx) {
QueueState.play-coverflow-upcoming(i - root.center-idx - 1);
}
}
}
// CENTER card — drawn LAST so it paints over its neighbors. Bound to the
// current-index row's offset (eases to 0 with `scroll`). Uses the unified
// large cover for sharpness; non-interactive (matches the disabled Tauri
// center button).
CoverCard {
offset: root.center-idx - root.scroll;
base-art: root.base-art;
pitch: root.pitch;
stage-cx: parent.cx;
stage-height: parent.height;
art: root.center-has-art ? root.center-art
: (QueueState.coverflow-tracks.length > root.center-idx
? QueueState.coverflow-tracks[root.center-idx].artwork
: root.center-art);
has-art: root.center-has-art
|| (QueueState.coverflow-tracks.length > root.center-idx
&& QueueState.coverflow-tracks[root.center-idx].artwork.width > 0);
interactive: false;
}
}
// --- Song card below the pyramid ------------------------------------
// Reuses the StaticPanel pattern verbatim, fed by NowPlayingState. Pinned
// under the fan, respecting the 132px player clearance.
VerticalLayout {
x: 0px;
y: 0px;
width: root.viewport-width;
height: root.viewport-height;
padding-top: 52px;
padding-bottom: 132px;
alignment: end;
spacing: 6px;
info-block := VerticalLayout {
alignment: center;
spacing: 6px;
// "Now Playing" indicator — shown only while playing.
if NowPlayingState.playing: HorizontalLayout {
alignment: center;
spacing: 8px;
padding-bottom: 8px;
VerticalLayout {
alignment: center;
EqualizerBars {
tint: #7c3aed;
}
}
Text {
text: @tr("Now Playing");
color: #7c3aed;
font-size: 12px;
font-weight: 600;
letter-spacing: 0.5px;
vertical-alignment: center;
}
}
Text {
text: NowPlayingState.title;
color: #ffffff;
font-size: 28px;
font-weight: 700;
horizontal-alignment: center;
overflow: elide;
}
Text {
text: NowPlayingState.artist;
color: #ffffffb3;
font-size: 18px;
horizontal-alignment: center;
overflow: elide;
}
if NowPlayingState.album != "": Text {
text: NowPlayingState.album;
color: #ffffff80;
font-size: 14px;
font-italic: true;
horizontal-alignment: center;
overflow: elide;
}
HorizontalLayout {
alignment: center;
padding-top: 12px;
QualityBadgeFull {
tier: NowPlayingState.quality-tier;
detail: NowPlayingState.quality-detail;
}
}
}
}
}