Files
qbz/crates/qbz-slint/ui/immersive/SpectrumPanel.slint
T
vicrodh 5146acf286 feat(slint): immersive Spectrum bar colors derived from album art (negative fallback)
The bars used a fixed cyan->purple default. Now they take contrast colors from
the album artwork, with a negative-fallback for dark covers (a near-black
Metallica album would vanish against the black background -> invert so the bars
pop).

- immersive.rs: spectrum_colors() extracts the most saturated sample as primary
  + a hue-distinct secondary (channel-rotated if none distinct); if the cover is
  too dark/desaturated it uses the NEGATIVE of the average. ensure_visible_on_black()
  inverts a too-dark color and lifts a dim one so both stops read on black.
- ImmersiveState gains spectrum-primary / spectrum-secondary (defaults cyan->purple
  until art resolves); set in playback.rs from the same decoded artwork as
  glow-color / bg-image.
- SpectrumPanel binds the bar gradient to those (base = primary, tip = secondary).

cc
2026-06-13 11:08:29 -06:00

252 lines
11 KiB
Plaintext

// Spectrum foreground (Immersive mode 3) — a faithful port of the Tauri
// `src/lib/components/immersive/panels/VisualizerPanel.svelte` (the 16-bar
// "Spectrum" panel; modeList maps Spectrum -> that file).
//
// THE 16-BAR MIRRORED CUBE SPECTRUM. Backend publishes NUM_BARS=16 to
// VisualizerState.bars. The Svelte panel EXCLUDES bins {1, 15}, leaving
// ACTIVE_BIN_INDICES = [0,2,3,4,5,6,7,8,9,10,11,12,13,14] (14 active bins).
// The layout is MIRRORED about the vertical center: visualBars = 14*2 = 28
// columns. Each active bin i draws TWO stacks — a LEFT column at index i and a
// RIGHT (mirror) column at (28-1-i) — both reading the SAME source bin. The
// stale in-code Svelte comment says "32 visual bars"; the real post-exclusion
// count is 28 (recon visualizers.md:101-103). Use 28.
//
// GEOMETRY (Svelte 197-209): barGap=4px, totalBarWidth = canvasW/28,
// barWidth = totalBarWidth-4. xLeft(i) = i*totalBarWidth + barGap/2.
// xRight(i) = canvasW - (i+1)*totalBarWidth + barGap/2. baseY = 0.85*H (bars
// grow UP), maxBarHeight = 0.7*H. Bars are STACKS OF DISCRETE CUBELETS:
// cubeHeight=6px (GPU/hwaccel — Slint is GPU by default, ADR-011), cubeGap=2px,
// no corner radius. cube j (0-based) top Y = baseY - (j+1)*(cubeH+cubeGap).
// numCubes = floor(barHeight / (cubeH+cubeGap)).
//
// COLORS (Svelte 68-119, 210-254): GPU path = per-cubelet vertical gradient.
// gradientRatio(j) = min(j/20, 1). channel = primary + ratio*(secondary-primary)
// — fades from PRIMARY at the base to SECONDARY at j>=20, identical left/right.
// The album-derived two-color extraction is NOT published to Slint (only a
// single ImmersiveState.glow-color exists), so v1 ships the Svelte DEFAULTS:
// primary = cyan rgb(0,220,200), secondary = purple rgb(150,50,255). Full
// album-color parity is a flagged Rust follow-up (publish primary/secondary),
// NOT a blocker.
//
// NO PEAK CAPS (peak-hold belongs to SpectralRibbon, out of scope — do NOT add).
//
// SMOOTHING (CRITICAL parity note): the Svelte panel applies a symmetric 0.6
// EMA in JS, but the Slint pipeline ALREADY smooths in Rust — qbz-audio
// processor.rs:150-161 runs an ASYMMETRIC EMA (fast attack 0.7 rising / slow
// decay 0.6 falling) on the 16 bars and clamps 0..1 (processor.rs:321) BEFORE
// publishing. So VisualizerState.bars arrive pre-smoothed AND pre-clamped. We
// bind them RAW (no second EMA — that would double-smooth) and add only a short
// `animate height`/`animate threshold` for inter-frame settle. This is a
// documented parity deviation (Slint Rust attack/decay != Svelte symmetric 0.6)
// — it is the EXISTING shared pipeline AlbumReactivePanel already depends on and
// must NOT be touched here.
//
// OPAQUE BLACK: unlike Static/AlbumReactive (transparent over the atmosphere),
// the Svelte .visualizer-panel paints solid #000000, so mode 3 HIDES the
// atmosphere (intended faithful behavior). The root background is #000000.
//
// `parent` is NEVER read in the root binding (Slint gotcha); viewport dims are
// passed in. `opacity:` is NEVER set on the root or on any container holding the
// hand-positioned cubelets (it corrupts absolute positioning) — the chrome fade
// lives on the player/ModeBar. Cube count/visibility is driven ONLY from the
// bound model value (never a `changed x/y/width/height` handler) to avoid the
// "Recursion detected" panic.
import { QualityBadgeFull } from "../primitives/QualityBadgeFull.slint";
import { NowPlayingState, VisualizerState, ImmersiveState } from "../state.slint";
// One mirrored pair of bars for a single active source bin. Draws the LEFT
// column at `x-left` and the RIGHT (mirror) column at `x-right`, both from the
// same amplitude.
//
// PERF: this used to be ~80 cubelet Rectangles PER column (x2 mirror x14 bins =
// ~2240 retained nodes) whose `visible` flipped every frame off an animated
// `shown-cubes` — a retained-scene-graph churn that femtovg cannot keep up with
// (~5fps, CPU spike). Tauri is cheap because it is immediate-mode Canvas2D
// (fillRect), not a retained tree. The fix: ONE solid gradient bar per column
// (2 nodes/pair, ~28 total) with an animated height. The discrete-cubelet
// texture is dropped for the perf win (can return cheaply via a static stripe
// overlay if wanted). Cyan->purple is the Svelte default gradient
// (primary rgb(0,220,200) at base -> secondary rgb(150,50,255) at the tip).
component BarPair inherits Rectangle {
// Geometry passed in from the panel (computed from viewport-width).
in property <length> x-left;
in property <length> x-right;
in property <length> bar-width;
in property <length> base-y;
in property <length> max-bar-height;
// 0..1 amplitude for this bin (already smoothed + clamped in Rust).
in property <float> amp;
// barHeight = amp * maxBarHeight. Animated for an inter-frame settle (NOT a
// re-smooth — the Rust EMA already smoothed; this only eases the step).
property <length> bar-height: root.amp * root.max-bar-height;
animate bar-height { duration: 90ms; easing: ease; }
width: 0px; // purely a positioning host; children are absolute.
height: 0px;
// LEFT bar. Grows UP from base-y (y derives from the animated height).
Rectangle {
x: root.x-left;
width: root.bar-width;
height: root.bar-height;
y: root.base-y - self.height;
// cyan at base -> purple at the tip (Svelte default). If it looks
// inverted, swap the two stops.
background: @linear-gradient(0deg, ImmersiveState.spectrum-primary 0%, ImmersiveState.spectrum-secondary 100%);
}
// RIGHT (mirror) bar — same source bin.
Rectangle {
x: root.x-right;
width: root.bar-width;
height: root.bar-height;
y: root.base-y - self.height;
background: @linear-gradient(0deg, ImmersiveState.spectrum-primary 0%, ImmersiveState.spectrum-secondary 100%);
}
}
export component SpectrumPanel inherits Rectangle {
in property <length> viewport-width: 1280px;
in property <length> viewport-height: 720px;
width: root.viewport-width;
height: root.viewport-height;
// Solid black — the Svelte .visualizer-panel is opaque #000000 (hides the
// atmosphere behind it; intended faithful behavior).
background: #000000;
// --- Bars geometry (Svelte 197-209) -----------------------------------
// 28 visual columns = 14 active bins x 2 (mirror).
property <int> visual-bars: 28;
property <length> bar-gap: 4px;
property <length> total-bar-width: root.viewport-width / 28;
property <length> bar-width: root.total-bar-width - 4px;
property <length> base-y: root.viewport-height * 0.85;
property <length> max-bar-height: root.viewport-height * 0.7;
// Active SOURCE bins after excluding {1, 15} (length 14). for src[i] iterates
// these; the LEFT column sits at visual index i, the RIGHT at (28-1-i).
property <[int]> active-bins: [0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14];
// --- Bars (drawn FIRST, behind the song card) --------------------------
// One BarPair per active bin; each draws its own left + mirrored-right
// stack. The amplitude is read with a SAFE bounds check (bars may be the
// initial all-zeros len-16 model, or ever swapped) — fallback 0.0 = flat
// baseline, no index-out-of-range.
for src[i] in root.active-bins: BarPair {
x: 0px;
y: 0px;
x-left: i * root.total-bar-width + root.bar-gap / 2;
x-right: root.viewport-width - (i + 1) * root.total-bar-width + root.bar-gap / 2;
bar-width: root.bar-width;
base-y: root.base-y;
max-bar-height: root.max-bar-height;
amp: VisualizerState.bars.length > src
? Math.max(0.0, Math.min(1.0, VisualizerState.bars[src]))
: 0.0;
}
// --- Song card (drawn SECOND, in front of the bars) --------------------
// Same now-playing pattern as StaticPanel.slint / AlbumReactivePanel.slint
// (artwork + title + artist + album-if-present + QualityBadgeFull), centered
// over the black bars canvas. No atmosphere blur here — the bars fill the
// whole panel behind the card. Player clearance (132px padding-bottom) is
// KEPT IDENTICAL to the siblings so the quality badge never clips under the
// floating ImmersivePlayerBar.
//
// Artwork is smaller than the Static/Album card (the Spectrum card is
// secondary to the bars): max(160px, min(min(0.45*H, 0.5*W), 360px)).
property <length> art-size: Math.max(160px,
Math.min(Math.min(root.viewport-height * 0.45, root.viewport-width * 0.5), 360px));
VerticalLayout {
x: 0px;
y: 0px;
width: root.viewport-width;
height: root.viewport-height;
padding-top: 70px;
padding-bottom: 132px;
alignment: center;
spacing: 20px;
// --- Artwork ------------------------------------------------------
artwork-block := Rectangle {
height: root.art-size;
horizontal-stretch: 1;
art := Rectangle {
width: root.art-size;
height: root.art-size;
x: (parent.width - self.width) / 2;
y: (parent.height - self.height) / 2;
border-radius: 8px;
clip: true;
// Halo box-shadow (Svelte .now-playing-card artwork). The two-
// layer CSS shadow collapses to one blur in Slint (same accepted
// gap as the siblings).
drop-shadow-blur: 36px;
drop-shadow-color: #00000099;
drop-shadow-offset-y: 8px;
Image {
source: NowPlayingState.artwork-large.width > 0
? NowPlayingState.artwork-large
: NowPlayingState.artwork;
image-fit: cover;
width: parent.width;
height: parent.height;
}
}
}
// --- Track info ---------------------------------------------------
info-block := VerticalLayout {
alignment: center;
spacing: 6px;
// Track title. (Explicit badge OMITTED — NowPlayingState has no
// `explicit` field; consistent with the sibling panels.)
Text {
text: NowPlayingState.title;
color: #ffffff;
font-size: 28px;
font-weight: 700;
horizontal-alignment: center;
overflow: elide;
}
// Artist.
Text {
text: NowPlayingState.artist;
color: #ffffffb3;
font-size: 18px;
horizontal-alignment: center;
overflow: elide;
}
// Album (italic, dimmer). Shown only when present.
if NowPlayingState.album != "": Text {
text: NowPlayingState.album;
color: #ffffff80;
font-size: 14px;
font-italic: true;
horizontal-alignment: center;
overflow: elide;
}
// Quality badge (Svelte QualityBadgeStatic full). Self-hides when
// tier == "". Centered under the text.
HorizontalLayout {
alignment: center;
padding-top: 12px;
QualityBadgeFull {
tier: NowPlayingState.quality-tier;
detail: NowPlayingState.quality-detail;
}
}
}
}
}