feat(slint): per-visual-line karaoke highlight for lyrics
A wrapped lyric line (one logical line over two visual rows) lit BOTH rows at
once because the active line was a single word-wrap Text masked by one clip
Rectangle of full block height + scalar width. Now it progresses per visual
line — row 1 fills to 100% before row 2 starts — matching the final Tauri
behavior. NOT a Slint limitation (proven in the investigation doc).
- lyrics_measure.rs (new): swash advance pass over the active line at the same
font (by font-index) + ppem + wght=700 + 0.2px letter-spacing the view
renders with, greedy word-wrap (CJK per-char) -> per-visual-line segments
with pixel widths. Embeds the same TTFs the view registers. swash is the
shaper femtovg uses, so widths match the render.
- lyrics_sync.rs: on active-index / content-width / font / size / uppercase
change, segment the active line and push [LyricsSegment{text, start-weight,
width-weight}] (Tauri's width-weighted partition); pushed atomically with the
active index. Per-surface cache keys (main + mini). line-progress unchanged.
- state.slint: LyricsSegment struct + active-segments + content-width on
LyricsState.
- LyricsLinesView.slint: active row renders a VerticalLayout of per-segment
rows (no-wrap), each the existing dim-base + clipped-bright-overlay with clip
width = segwidth * clamp((line-progress - start-weight)/width-weight, 0, 1).
Root publishes content-width. Inactive rows unchanged.
- miniplayer.rs: mini lyrics surface segmentation fan-out.
Investigation + plan: qbz-nix-docs/lyrics/2026-06-18-karaoke-highlight-investigation/.
Owner-verified working. Known tuning point: wrap-boundary precision per font tier.
cc
This commit is contained in:
8 files changed
+573
-22
No files matched your search
Generated
+1
@@ -5776,6 +5776,7 @@ dependencies = [
|
|||||||
"serde_json",
|
"serde_json",
|
||||||
"slint",
|
"slint",
|
||||||
"slint-build",
|
"slint-build",
|
||||||
|
"swash",
|
||||||
"tokio",
|
"tokio",
|
||||||
"urlencoding",
|
"urlencoding",
|
||||||
"uuid",
|
"uuid",
|
||||||
|
|||||||
@@ -76,6 +76,11 @@ qbz-mixtape = { path = "../qbz-mixtape" }
|
|||||||
qbz-playlist-import = { path = "../qbz-playlist-import" }
|
qbz-playlist-import = { path = "../qbz-playlist-import" }
|
||||||
qbz-qobuz = { path = "../qbz-qobuz" }
|
qbz-qobuz = { path = "../qbz-qobuz" }
|
||||||
qbz-lyrics = { path = "../qbz-lyrics" }
|
qbz-lyrics = { path = "../qbz-lyrics" }
|
||||||
|
# Karaoke highlight per-visual-line measurement (lyrics_measure.rs): swash is
|
||||||
|
# the SAME shaper femtovg uses to raster glyphs, so an advance pass at the same
|
||||||
|
# font + ppem + wght axis + letter-spacing matches the rendered wrap. Already in
|
||||||
|
# the workspace lock (pulled transitively via femtovg) — no new external dep.
|
||||||
|
swash = "0.2"
|
||||||
qbz-integrations = { path = "../qbz-integrations" }
|
qbz-integrations = { path = "../qbz-integrations" }
|
||||||
rusqlite = { version = "0.31", features = ["bundled"] }
|
rusqlite = { version = "0.31", features = ["bundled"] }
|
||||||
# DJ-mix sampler thread RNG (rand::rng()), confined to a sync scope — the
|
# DJ-mix sampler thread RNG (rand::rng()), confined to a sync scope — the
|
||||||
|
|||||||
@@ -0,0 +1,291 @@
|
|||||||
|
//! Per-visual-line wrap measurement for the karaoke highlight.
|
||||||
|
//!
|
||||||
|
//! # Why this exists
|
||||||
|
//!
|
||||||
|
//! A logical lyric line that word-wraps to two (or more) visual lines used to
|
||||||
|
//! light up ALL visual rows at once, because the active line was rendered as a
|
||||||
|
//! single `wrap: word-wrap` Text masked by ONE clip rectangle of full block
|
||||||
|
//! height and a single-scalar width (`LyricsLinesView.slint`). A
|
||||||
|
//! single-scalar-width rectangle of full height reveals the same left fraction
|
||||||
|
//! of *every* visual row simultaneously — Tauri instead splits the active line
|
||||||
|
//! into one element PER VISUAL LINE and fills them sequentially (row 1 to
|
||||||
|
//! 100%, then row 2). This module is the Rust analogue of Tauri's Pretext
|
||||||
|
//! layout pass: it reproduces Slint's wrap decision and reports, per visual
|
||||||
|
//! line, the segment text and its pixel ink width so the engine can partition
|
||||||
|
//! the single global progress fraction across the segments by width share.
|
||||||
|
//!
|
||||||
|
//! # How it measures
|
||||||
|
//!
|
||||||
|
//! femtovg (the Linux/Windows renderer) shapes glyphs with `swash`, so a swash
|
||||||
|
//! advance pass at the same font + ppem + weight axis + letter-spacing matches
|
||||||
|
//! what is actually rendered. The four bundled variable lyric fonts plus the
|
||||||
|
//! Inter "System"/default bold are embedded here via `include_bytes!` (the
|
||||||
|
//! same TTFs `LyricsLinesView.slint` registers via `import`) and parsed once
|
||||||
|
//! into process-global `swash::FontRef`s (held as owned byte buffers in a
|
||||||
|
//! `OnceLock`).
|
||||||
|
//!
|
||||||
|
//! The active lyric line is rendered BOLD; for the variable fonts we set the
|
||||||
|
//! `wght` variation axis to 700 so the advances match the bold raster. (Inter
|
||||||
|
//! "System"/default is measured from `Inter_18pt-Bold.ttf` directly, which is
|
||||||
|
//! already a bold static instance — no axis needed. LINE Seed JP ships only a
|
||||||
|
//! Regular file with no weight axis, so it is measured at its single instance;
|
||||||
|
//! the split is still per-visual-line and self-consistent because we render
|
||||||
|
//! the segments ourselves.)
|
||||||
|
//!
|
||||||
|
//! Letter-spacing is added as a flat `LETTER_SPACING_PX` per glyph cluster, to
|
||||||
|
//! mirror the `letter-spacing: 0.2px` on the rendered Text items.
|
||||||
|
//!
|
||||||
|
//! # Wrapping algorithm
|
||||||
|
//!
|
||||||
|
//! Greedy word wrap, matching `wrap: word-wrap`: split the text on ASCII/Unicode
|
||||||
|
//! whitespace into words; keep appending words (with their separating space) to
|
||||||
|
//! the current visual line while they fit `max_width_px`; otherwise start a new
|
||||||
|
//! visual line. A single word longer than the budget is broken per grapheme
|
||||||
|
//! cluster (CJK / no-space runs also break per character this way). Each
|
||||||
|
//! emitted segment carries its measured pixel width.
|
||||||
|
|
||||||
|
use std::sync::OnceLock;
|
||||||
|
|
||||||
|
use swash::shape::ShapeContext;
|
||||||
|
use swash::FontRef;
|
||||||
|
|
||||||
|
/// Per-glyph-cluster letter spacing, mirroring `letter-spacing: 0.2px` on the
|
||||||
|
/// rendered Text items in `LyricsLinesView.slint`.
|
||||||
|
const LETTER_SPACING_PX: f32 = 0.2;
|
||||||
|
|
||||||
|
/// The weight axis value for the bold active line (`Typography.bold` ~= 700).
|
||||||
|
const BOLD_WGHT: f32 = 700.0;
|
||||||
|
|
||||||
|
/// One bundled lyric font, parsed lazily and kept alive for the process.
|
||||||
|
struct LoadedFont {
|
||||||
|
/// Owned font bytes — `FontRef` borrows from these, so they must outlive it.
|
||||||
|
data: &'static [u8],
|
||||||
|
/// Font collection index (0 for all single-face TTFs here).
|
||||||
|
index: usize,
|
||||||
|
/// Whether this font exposes a `wght` variation axis (set 700 when true).
|
||||||
|
variable: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Embedded copies of the EXACT TTFs `LyricsLinesView.slint` registers, plus
|
||||||
|
/// the Inter bold used for the "System"/default family. `include_bytes!` paths
|
||||||
|
/// are relative to THIS source file (`crates/qbz-slint/src/`).
|
||||||
|
static FONT_SYSTEM_INTER_BOLD: &[u8] =
|
||||||
|
include_bytes!("../ui/assets/fonts/Inter_18pt-Bold.ttf");
|
||||||
|
static FONT_LINE_SEED_JP: &[u8] =
|
||||||
|
include_bytes!("../../../static/fonts/LINESeedJP-Regular.ttf");
|
||||||
|
static FONT_MONTSERRAT: &[u8] =
|
||||||
|
include_bytes!("../../../static/fonts/Montserrat-VariableFont_wght.ttf");
|
||||||
|
static FONT_NOTO_SANS: &[u8] =
|
||||||
|
include_bytes!("../../../static/fonts/NotoSans-VariableFont_wdth,wght.ttf");
|
||||||
|
static FONT_SOURCE_SANS_3: &[u8] =
|
||||||
|
include_bytes!("../../../static/fonts/SourceSans3-VariableFont_wght.ttf");
|
||||||
|
|
||||||
|
/// Map a `font-index` (the same enum as `LyricsState.font-index` /
|
||||||
|
/// `LyricsSidebar.slint`: 0=System→Inter, 1=LINE Seed JP, 2=Montserrat,
|
||||||
|
/// 3=Noto Sans, 4=Source Sans 3) to its embedded bytes. Unknown indices fall
|
||||||
|
/// back to the System/Inter default, matching the `.slint` `font-name` default.
|
||||||
|
fn loaded_font(font_index: i32) -> &'static LoadedFont {
|
||||||
|
static FONTS: OnceLock<[LoadedFont; 5]> = OnceLock::new();
|
||||||
|
let fonts = FONTS.get_or_init(|| {
|
||||||
|
[
|
||||||
|
// 0 System (window default = Inter bold static instance).
|
||||||
|
LoadedFont { data: FONT_SYSTEM_INTER_BOLD, index: 0, variable: false },
|
||||||
|
// 1 LINE Seed JP (Regular only — no weight axis).
|
||||||
|
LoadedFont { data: FONT_LINE_SEED_JP, index: 0, variable: false },
|
||||||
|
// 2 Montserrat (variable wght).
|
||||||
|
LoadedFont { data: FONT_MONTSERRAT, index: 0, variable: true },
|
||||||
|
// 3 Noto Sans (variable wdth,wght).
|
||||||
|
LoadedFont { data: FONT_NOTO_SANS, index: 0, variable: true },
|
||||||
|
// 4 Source Sans 3 (variable wght).
|
||||||
|
LoadedFont { data: FONT_SOURCE_SANS_3, index: 0, variable: true },
|
||||||
|
]
|
||||||
|
});
|
||||||
|
let idx = match font_index {
|
||||||
|
1..=4 => font_index as usize,
|
||||||
|
_ => 0,
|
||||||
|
};
|
||||||
|
&fonts[idx]
|
||||||
|
}
|
||||||
|
|
||||||
|
/// One measured visual line of a wrapped logical lyric line.
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct Segment {
|
||||||
|
/// The text of this visual line (no leading/trailing wrap whitespace).
|
||||||
|
pub text: String,
|
||||||
|
/// The rendered pixel ink+advance width of this visual line (includes the
|
||||||
|
/// per-cluster letter spacing, so it matches the drawn Text width).
|
||||||
|
pub width_px: f32,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Measure the advance width (px) of `text` at `size_px` for `font`, including
|
||||||
|
/// the flat per-cluster letter spacing — the same metric Slint uses to size a
|
||||||
|
/// `no-wrap` Text. Returns the total advance.
|
||||||
|
fn measure_width(ctx: &mut ShapeContext, font: &LoadedFont, text: &str, size_px: f32) -> f32 {
|
||||||
|
if text.is_empty() {
|
||||||
|
return 0.0;
|
||||||
|
}
|
||||||
|
let Some(font_ref) = FontRef::from_index(font.data, font.index) else {
|
||||||
|
// Parse failure: fall back to a crude estimate so callers still split.
|
||||||
|
return text.chars().count() as f32 * size_px * 0.5;
|
||||||
|
};
|
||||||
|
let mut builder = ctx.builder(font_ref).size(size_px);
|
||||||
|
if font.variable {
|
||||||
|
builder = builder.variations(&[("wght", BOLD_WGHT)]);
|
||||||
|
}
|
||||||
|
let mut shaper = builder.build();
|
||||||
|
shaper.add_str(text);
|
||||||
|
let mut total = 0.0_f32;
|
||||||
|
let mut clusters = 0_u32;
|
||||||
|
shaper.shape_with(|cluster| {
|
||||||
|
for glyph in cluster.glyphs {
|
||||||
|
total += glyph.advance;
|
||||||
|
}
|
||||||
|
clusters += 1;
|
||||||
|
});
|
||||||
|
// Letter spacing is applied per cluster on the rendered side.
|
||||||
|
total + clusters as f32 * LETTER_SPACING_PX
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Split a single over-long word into per-grapheme-cluster pieces that each fit
|
||||||
|
/// `max_width_px` (CJK and other no-space runs land here too). Appends the
|
||||||
|
/// resulting segments to `out`.
|
||||||
|
fn break_long_word(
|
||||||
|
ctx: &mut ShapeContext,
|
||||||
|
font: &LoadedFont,
|
||||||
|
word: &str,
|
||||||
|
size_px: f32,
|
||||||
|
max_width_px: f32,
|
||||||
|
out: &mut Vec<Segment>,
|
||||||
|
) {
|
||||||
|
let mut current = String::new();
|
||||||
|
let mut current_w = 0.0_f32;
|
||||||
|
// Break per Unicode scalar (good enough for CJK; avoids pulling in a
|
||||||
|
// grapheme-segmentation dependency — lyric runs are short).
|
||||||
|
for ch in word.chars() {
|
||||||
|
let piece: String = ch.to_string();
|
||||||
|
let piece_w = measure_width(ctx, font, &piece, size_px);
|
||||||
|
if !current.is_empty() && current_w + piece_w > max_width_px {
|
||||||
|
out.push(Segment { text: std::mem::take(&mut current), width_px: current_w });
|
||||||
|
current_w = 0.0;
|
||||||
|
}
|
||||||
|
current.push(ch);
|
||||||
|
current_w += piece_w;
|
||||||
|
}
|
||||||
|
if !current.is_empty() {
|
||||||
|
out.push(Segment { text: current, width_px: current_w });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Compute the per-visual-line segmentation of `text` as it would word-wrap
|
||||||
|
/// inside `max_width_px`, at the given `font_index` + `size_px` (bold weight,
|
||||||
|
/// 0.2px letter spacing — matching the active lyric line render).
|
||||||
|
///
|
||||||
|
/// Returns one [`Segment`] per visual line, each with the segment text and its
|
||||||
|
/// rendered pixel width. An empty / whitespace-only input yields a single empty
|
||||||
|
/// segment so the caller always has at least one row to render.
|
||||||
|
pub fn wrap_segments(text: &str, font_index: i32, size_px: f32, max_width_px: f32) -> Vec<Segment> {
|
||||||
|
let font = loaded_font(font_index);
|
||||||
|
let mut ctx = ShapeContext::new();
|
||||||
|
let budget = max_width_px.max(1.0);
|
||||||
|
|
||||||
|
// Tokenize into words, preserving nothing of the original whitespace except
|
||||||
|
// that each inter-word gap becomes a single space when re-joined.
|
||||||
|
let words: Vec<&str> = text.split_whitespace().collect();
|
||||||
|
if words.is_empty() {
|
||||||
|
return vec![Segment { text: String::new(), width_px: 0.0 }];
|
||||||
|
}
|
||||||
|
|
||||||
|
let space_w = measure_width(&mut ctx, font, " ", size_px);
|
||||||
|
|
||||||
|
let mut out: Vec<Segment> = Vec::new();
|
||||||
|
let mut line = String::new();
|
||||||
|
let mut line_w = 0.0_f32;
|
||||||
|
|
||||||
|
for word in words {
|
||||||
|
let word_w = measure_width(&mut ctx, font, word, size_px);
|
||||||
|
|
||||||
|
if line.is_empty() {
|
||||||
|
// First word on a fresh line.
|
||||||
|
if word_w > budget {
|
||||||
|
// Word alone overflows — hard-break it into pieces.
|
||||||
|
break_long_word(&mut ctx, font, word, size_px, budget, &mut out);
|
||||||
|
// The tail piece (if any) becomes the start of the current line
|
||||||
|
// so following words can still pack onto it.
|
||||||
|
if let Some(last) = out.pop() {
|
||||||
|
line = last.text;
|
||||||
|
line_w = last.width_px;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
line.push_str(word);
|
||||||
|
line_w = word_w;
|
||||||
|
}
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Subsequent word: does it fit with a leading space?
|
||||||
|
let added = space_w + word_w;
|
||||||
|
if line_w + added <= budget {
|
||||||
|
line.push(' ');
|
||||||
|
line.push_str(word);
|
||||||
|
line_w += added;
|
||||||
|
} else {
|
||||||
|
// Flush the current line and start anew with this word.
|
||||||
|
out.push(Segment { text: std::mem::take(&mut line), width_px: line_w });
|
||||||
|
line_w = 0.0;
|
||||||
|
if word_w > budget {
|
||||||
|
break_long_word(&mut ctx, font, word, size_px, budget, &mut out);
|
||||||
|
if let Some(last) = out.pop() {
|
||||||
|
line = last.text;
|
||||||
|
line_w = last.width_px;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
line.push_str(word);
|
||||||
|
line_w = word_w;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if !line.is_empty() || out.is_empty() {
|
||||||
|
out.push(Segment { text: line, width_px: line_w });
|
||||||
|
}
|
||||||
|
out
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn single_short_line_one_segment() {
|
||||||
|
let segs = wrap_segments("hello world", 0, 15.0, 10_000.0);
|
||||||
|
assert_eq!(segs.len(), 1);
|
||||||
|
assert_eq!(segs[0].text, "hello world");
|
||||||
|
assert!(segs[0].width_px > 0.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn empty_input_yields_one_empty_segment() {
|
||||||
|
let segs = wrap_segments(" ", 0, 15.0, 100.0);
|
||||||
|
assert_eq!(segs.len(), 1);
|
||||||
|
assert_eq!(segs[0].text, "");
|
||||||
|
assert_eq!(segs[0].width_px, 0.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn narrow_budget_wraps_to_multiple_segments() {
|
||||||
|
// A budget too small for the whole phrase must split it.
|
||||||
|
let segs = wrap_segments("alpha beta gamma delta", 0, 15.0, 40.0);
|
||||||
|
assert!(segs.len() >= 2, "expected a wrap, got {} segs", segs.len());
|
||||||
|
// Every emitted segment must be non-empty (no dangling blank rows).
|
||||||
|
for seg in &segs {
|
||||||
|
assert!(!seg.text.is_empty());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn over_long_word_is_hard_broken() {
|
||||||
|
// One unbreakable token wider than the budget breaks per character.
|
||||||
|
let segs = wrap_segments("aaaaaaaaaaaaaaaaaaaa", 0, 15.0, 20.0);
|
||||||
|
assert!(segs.len() >= 2, "expected hard break, got {}", segs.len());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -47,12 +47,13 @@ use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
|
|||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
use slint::{ComponentHandle, Timer, TimerMode};
|
use slint::{ComponentHandle, Model, ModelRc, SharedString, Timer, TimerMode, VecModel};
|
||||||
|
|
||||||
use qbz_app::shell::AppRuntime;
|
use qbz_app::shell::AppRuntime;
|
||||||
|
|
||||||
use crate::adapter::SlintAdapter;
|
use crate::adapter::SlintAdapter;
|
||||||
use crate::{AppWindow, LyricsState, ShellState};
|
use crate::lyrics_measure;
|
||||||
|
use crate::{AppWindow, LyricsSegment, LyricsState, ShellState};
|
||||||
|
|
||||||
type Runtime = Arc<AppRuntime<SlintAdapter>>;
|
type Runtime = Arc<AppRuntime<SlintAdapter>>;
|
||||||
|
|
||||||
@@ -96,6 +97,33 @@ thread_local! {
|
|||||||
static FAST: Cell<bool> = const { Cell::new(false) };
|
static FAST: Cell<bool> = const { Cell::new(false) };
|
||||||
static CTX: RefCell<Option<(Runtime, slint::Weak<AppWindow>)>> =
|
static CTX: RefCell<Option<(Runtime, slint::Weak<AppWindow>)>> =
|
||||||
const { RefCell::new(None) };
|
const { RefCell::new(None) };
|
||||||
|
/// Last inputs the active-line segmentation was computed for, so the
|
||||||
|
/// (potentially font-shaping) recompute only runs on a real change —
|
||||||
|
/// active-line index, content-width budget, font family, size tier, or the
|
||||||
|
/// uppercase transform — never every 30Hz tick. `None` forces a recompute.
|
||||||
|
/// Separate cells per surface (the main window vs the miniplayer have
|
||||||
|
/// different content widths, hence different segmentations).
|
||||||
|
static SEG_KEY_MAIN: RefCell<Option<SegKey>> = const { RefCell::new(None) };
|
||||||
|
static SEG_KEY_MINI: RefCell<Option<SegKey>> = const { RefCell::new(None) };
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Which surface's segmentation cache to consult.
|
||||||
|
#[derive(Clone, Copy)]
|
||||||
|
pub(crate) enum SegSurface {
|
||||||
|
Main,
|
||||||
|
Mini,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Cache key for the active-line segmentation (see `SEG_KEY`).
|
||||||
|
#[derive(PartialEq, Clone)]
|
||||||
|
struct SegKey {
|
||||||
|
index: i32,
|
||||||
|
/// Content width in px, quantized to whole px so sub-pixel jitter from the
|
||||||
|
/// layout doesn't thrash the recompute.
|
||||||
|
width_px: i32,
|
||||||
|
font_index: i32,
|
||||||
|
size_index: i32,
|
||||||
|
uppercase: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Start the engine timer. UI-thread only (slint::Timer requirement);
|
/// Start the engine timer. UI-thread only (slint::Timer requirement);
|
||||||
@@ -178,7 +206,16 @@ fn compute_and_push(window: &AppWindow, runtime: &Runtime, require_playing: bool
|
|||||||
let (position_ms, playing) = resolve_position_ms(runtime);
|
let (position_ms, playing) = resolve_position_ms(runtime);
|
||||||
if require_playing && !playing {
|
if require_playing && !playing {
|
||||||
// Pause freezes the ladder + fill in place (Tauri parity: the rAF
|
// Pause freezes the ladder + fill in place (Tauri parity: the rAF
|
||||||
// gate stops, getCurrentTime holds — review §8 "Pause").
|
// gate stops, getCurrentTime holds — review §8 "Pause"). Still refresh
|
||||||
|
// the segmentation from the last-pushed active index so a resize or a
|
||||||
|
// font/size pref change WHILE PAUSED re-flows the wrapped highlight
|
||||||
|
// (cache-guarded — a no-op when nothing relevant changed).
|
||||||
|
refresh_active_segments(
|
||||||
|
&lyrics,
|
||||||
|
lyrics.get_active_index(),
|
||||||
|
main_render_prefs(&lyrics),
|
||||||
|
SegSurface::Main,
|
||||||
|
);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -217,6 +254,10 @@ fn compute_and_push(window: &AppWindow, runtime: &Runtime, require_playing: bool
|
|||||||
lyrics.set_line_progress(fraction);
|
lyrics.set_line_progress(fraction);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Per-visual-line karaoke segmentation: recompute (cache-guarded) when the
|
||||||
|
// active line, content-width, font, size tier, or uppercase pref changes.
|
||||||
|
refresh_active_segments(&lyrics, index, main_render_prefs(&lyrics), SegSurface::Main);
|
||||||
|
|
||||||
// REQ-1 fan-out: mirror the karaoke scalars to the miniplayer (30Hz; no-op
|
// REQ-1 fan-out: mirror the karaoke scalars to the miniplayer (30Hz; no-op
|
||||||
// when the mini is closed) so the mini lyrics highlight stays smooth.
|
// when the mini is closed) so the mini lyrics highlight stays smooth.
|
||||||
crate::miniplayer::mirror_lyrics_scalars(window);
|
crate::miniplayer::mirror_lyrics_scalars(window);
|
||||||
@@ -233,3 +274,126 @@ fn now_ms() -> u64 {
|
|||||||
.map(|d| d.as_millis() as u64)
|
.map(|d| d.as_millis() as u64)
|
||||||
.unwrap_or(0)
|
.unwrap_or(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Render prefs for the MAIN sidebar surface — driven by the persisted prefs
|
||||||
|
/// on `LyricsState`, exactly as `LyricsSidebar.slint` binds them into
|
||||||
|
/// `LyricsLinesView`.
|
||||||
|
fn main_render_prefs(lyrics: &LyricsState) -> RenderPrefs {
|
||||||
|
RenderPrefs {
|
||||||
|
font_index: lyrics.get_font_index(),
|
||||||
|
size_index: lyrics.get_size_index(),
|
||||||
|
uppercase: lyrics.get_uppercase(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Active-line font size in px (`size-active`), replicating the size-tier ->
|
||||||
|
/// px mapping in `LyricsSidebar.slint` (0 S = 13 · 1 M = 15 · 2 L = 19 ·
|
||||||
|
/// 3 XL = 24). Used as the ppem for the segmentation measure pass so segment
|
||||||
|
/// widths match the drawn active line.
|
||||||
|
fn size_active_px(size_index: i32) -> f32 {
|
||||||
|
match size_index {
|
||||||
|
0 => 13.0,
|
||||||
|
2 => 19.0,
|
||||||
|
3 => 24.0,
|
||||||
|
_ => 15.0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The font + size tier + uppercase the active line is RENDERED with — the
|
||||||
|
/// segmentation must measure against these so segment widths match the draw.
|
||||||
|
/// The main sidebar drives them from the persisted prefs on `LyricsState`; the
|
||||||
|
/// miniplayer renders with FIXED values (Inter / 15px / no uppercase — it
|
||||||
|
/// binds none of the prefs, see `MiniLyricsSurface.slint`), so it passes those.
|
||||||
|
#[derive(Clone, Copy)]
|
||||||
|
pub(crate) struct RenderPrefs {
|
||||||
|
pub font_index: i32,
|
||||||
|
pub size_index: i32,
|
||||||
|
pub uppercase: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Recompute (cache-guarded) the per-visual-line segmentation of the ACTIVE
|
||||||
|
/// line and push it onto `lyrics.active-segments`. Cheap no-op unless the
|
||||||
|
/// active index, content-width, or render prefs changed since the last push.
|
||||||
|
/// When there is no active line, clears the model. `surface` selects the
|
||||||
|
/// per-window cache cell (main window vs miniplayer).
|
||||||
|
pub(crate) fn refresh_active_segments(
|
||||||
|
lyrics: &LyricsState,
|
||||||
|
index: i32,
|
||||||
|
prefs: RenderPrefs,
|
||||||
|
surface: SegSurface,
|
||||||
|
) {
|
||||||
|
let content_width = lyrics.get_content_width();
|
||||||
|
let font_index = prefs.font_index;
|
||||||
|
let size_index = prefs.size_index;
|
||||||
|
let uppercase = prefs.uppercase;
|
||||||
|
|
||||||
|
let key = SegKey {
|
||||||
|
index,
|
||||||
|
// Quantize to whole px so layout sub-pixel jitter doesn't re-segment.
|
||||||
|
width_px: content_width.round() as i32,
|
||||||
|
font_index,
|
||||||
|
size_index,
|
||||||
|
uppercase,
|
||||||
|
};
|
||||||
|
let set_key = |k: Option<SegKey>| match surface {
|
||||||
|
SegSurface::Main => SEG_KEY_MAIN.with(|cell| *cell.borrow_mut() = k),
|
||||||
|
SegSurface::Mini => SEG_KEY_MINI.with(|cell| *cell.borrow_mut() = k),
|
||||||
|
};
|
||||||
|
let unchanged = match surface {
|
||||||
|
SegSurface::Main => SEG_KEY_MAIN.with(|cell| cell.borrow().as_ref() == Some(&key)),
|
||||||
|
SegSurface::Mini => SEG_KEY_MINI.with(|cell| cell.borrow().as_ref() == Some(&key)),
|
||||||
|
};
|
||||||
|
if unchanged {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// No active line, no measurable width, or no doc -> clear the model.
|
||||||
|
if index < 0 || content_width <= 0.0 {
|
||||||
|
if lyrics.get_active_segments().row_count() > 0 {
|
||||||
|
lyrics.set_active_segments(ModelRc::new(VecModel::<LyricsSegment>::default()));
|
||||||
|
}
|
||||||
|
set_key(Some(key));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pull the raw active-line text from the parsed doc (the UI model carries
|
||||||
|
// the same text, but the doc read is already the engine's source of truth).
|
||||||
|
let raw = crate::lyrics::with_current_doc(|doc| match doc {
|
||||||
|
Some(doc) if doc.synced => doc
|
||||||
|
.lines
|
||||||
|
.get(index as usize)
|
||||||
|
.map(|line| line.text.clone()),
|
||||||
|
_ => None,
|
||||||
|
});
|
||||||
|
let Some(raw) = raw else {
|
||||||
|
lyrics.set_active_segments(ModelRc::new(VecModel::<LyricsSegment>::default()));
|
||||||
|
set_key(Some(key));
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Match the rendered transform: the active line is uppercased when the pref
|
||||||
|
// is on, so segment widths must be measured on the uppercased string.
|
||||||
|
let text = if uppercase { raw.to_uppercase() } else { raw };
|
||||||
|
|
||||||
|
let size_px = size_active_px(size_index);
|
||||||
|
let segments = lyrics_measure::wrap_segments(&text, font_index, size_px, content_width);
|
||||||
|
|
||||||
|
// Convert measured widths into cumulative start-weight + per-segment
|
||||||
|
// width-weight (Tauri's getSegmentProgress partition) so the row can
|
||||||
|
// re-map the single global `line-progress` to a per-segment local fill.
|
||||||
|
let total: f32 = segments.iter().map(|seg| seg.width_px).sum();
|
||||||
|
let mut cumulative = 0.0_f32;
|
||||||
|
let mut out: Vec<LyricsSegment> = Vec::with_capacity(segments.len());
|
||||||
|
for seg in &segments {
|
||||||
|
let width_weight = if total > 0.0 { seg.width_px / total } else { 0.0 };
|
||||||
|
out.push(LyricsSegment {
|
||||||
|
text: SharedString::from(seg.text.as_str()),
|
||||||
|
start_weight: cumulative,
|
||||||
|
width_weight,
|
||||||
|
});
|
||||||
|
cumulative += width_weight;
|
||||||
|
}
|
||||||
|
|
||||||
|
lyrics.set_active_segments(ModelRc::new(VecModel::from(out)));
|
||||||
|
set_key(Some(key));
|
||||||
|
}
|
||||||
@@ -71,6 +71,7 @@ mod local_library;
|
|||||||
mod local_playlist;
|
mod local_playlist;
|
||||||
mod local_library_settings;
|
mod local_library_settings;
|
||||||
mod lyrics;
|
mod lyrics;
|
||||||
|
mod lyrics_measure;
|
||||||
mod lyrics_prefs;
|
mod lyrics_prefs;
|
||||||
mod lyrics_sync;
|
mod lyrics_sync;
|
||||||
mod media_controls;
|
mod media_controls;
|
||||||
|
|||||||
@@ -464,6 +464,25 @@ pub fn mirror_lyrics_scalars(main: &AppWindow) {
|
|||||||
d.set_active_index(s.get_active_index());
|
d.set_active_index(s.get_active_index());
|
||||||
d.set_line_progress(s.get_line_progress());
|
d.set_line_progress(s.get_line_progress());
|
||||||
d.set_fill_anim_ms(s.get_fill_anim_ms());
|
d.set_fill_anim_ms(s.get_fill_anim_ms());
|
||||||
|
refresh_mini_segments(&d);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Recompute the mini's per-visual-line karaoke segmentation against ITS OWN
|
||||||
|
/// content-width (the mini surface has a different width + padding than the
|
||||||
|
/// sidebar). The mini renders with FIXED render prefs — Inter / 15px / no
|
||||||
|
/// uppercase (`MiniLyricsSurface.slint` binds none of the display prefs) — so
|
||||||
|
/// it passes those rather than the persisted sidebar prefs. Cache-guarded.
|
||||||
|
fn refresh_mini_segments(d: &LyricsState) {
|
||||||
|
crate::lyrics_sync::refresh_active_segments(
|
||||||
|
d,
|
||||||
|
d.get_active_index(),
|
||||||
|
crate::lyrics_sync::RenderPrefs {
|
||||||
|
font_index: 0, // System / Inter (mini default font-name "")
|
||||||
|
size_index: 1, // 15px (mini's fixed size-active)
|
||||||
|
uppercase: false,
|
||||||
|
},
|
||||||
|
crate::lyrics_sync::SegSurface::Mini,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn force_full_mirror(main: &AppWindow, mini: &MiniPlayerWindow) {
|
fn force_full_mirror(main: &AppWindow, mini: &MiniPlayerWindow) {
|
||||||
@@ -533,6 +552,7 @@ fn mirror_lyrics_gated(main: &AppWindow, mini: &MiniPlayerWindow) {
|
|||||||
d.set_active_index(s.get_active_index());
|
d.set_active_index(s.get_active_index());
|
||||||
d.set_line_progress(s.get_line_progress());
|
d.set_line_progress(s.get_line_progress());
|
||||||
d.set_fill_anim_ms(s.get_fill_anim_ms());
|
d.set_fill_anim_ms(s.get_fill_anim_ms());
|
||||||
|
refresh_mini_segments(&d);
|
||||||
|
|
||||||
// Gate the lines-model copy on (status, line-count) so we don't thrash the
|
// Gate the lines-model copy on (status, line-count) so we don't thrash the
|
||||||
// lyrics view (which would reset its scroll) every tick.
|
// lyrics view (which would reset its scroll) every tick.
|
||||||
|
|||||||
@@ -44,7 +44,7 @@
|
|||||||
|
|
||||||
import { Theme } from "../foundation/semantic-colors.slint";
|
import { Theme } from "../foundation/semantic-colors.slint";
|
||||||
import { Typography } from "../foundation/typography.slint";
|
import { Typography } from "../foundation/typography.slint";
|
||||||
import { LyricsState } from "../state.slint";
|
import { LyricsState, LyricsSegment } from "../state.slint";
|
||||||
|
|
||||||
// Q6 (SHIPPED): Tauri's four non-system lyric font families, embedded into
|
// Q6 (SHIPPED): Tauri's four non-system lyric font families, embedded into
|
||||||
// the binary at compile time (slint-build default EmbedAllResources for Rust
|
// the binary at compile time (slint-build default EmbedAllResources for Rust
|
||||||
@@ -166,7 +166,9 @@ component LyricsLineRow inherits VerticalLayout {
|
|||||||
return 0.03;
|
return 0.03;
|
||||||
}
|
}
|
||||||
|
|
||||||
base := Text {
|
// INACTIVE / unsynced rows: a single wrapping Text, exactly as before.
|
||||||
|
// (Active rows render the per-visual-line karaoke split below instead.)
|
||||||
|
if !root.is-active: Text {
|
||||||
text: root.display-text;
|
text: root.display-text;
|
||||||
wrap: word-wrap;
|
wrap: word-wrap;
|
||||||
opacity: root.line-opacity();
|
opacity: root.line-opacity();
|
||||||
@@ -178,25 +180,52 @@ component LyricsLineRow inherits VerticalLayout {
|
|||||||
duration: 220ms;
|
duration: 220ms;
|
||||||
easing: ease-out;
|
easing: ease-out;
|
||||||
}
|
}
|
||||||
// Active base = the DIM (unsung) layer under the karaoke overlay
|
color: root.is-past ? Theme.text-muted : Theme.text-secondary;
|
||||||
// (Tauri gradient parity: unsung stays dim, sung sweeps in accent).
|
|
||||||
color: root.is-active
|
|
||||||
? Theme.text-secondary
|
|
||||||
: (root.is-past ? Theme.text-muted : Theme.text-secondary);
|
|
||||||
font-family: root.font-name;
|
font-family: root.font-name;
|
||||||
font-size: root.is-active ? root.size-active : root.size-inactive;
|
font-size: root.size-inactive;
|
||||||
font-weight: root.is-active ? Typography.bold : Typography.medium;
|
font-weight: Typography.medium;
|
||||||
// Friendly letter-spacing (Q3): slightly open, never cramped.
|
// Friendly letter-spacing (Q3): slightly open, never cramped.
|
||||||
letter-spacing: 0.2px;
|
letter-spacing: 0.2px;
|
||||||
|
}
|
||||||
|
|
||||||
// Karaoke fill overlay (active synced line only): bright copy of the
|
// ACTIVE row: per-VISUAL-line karaoke highlight. The engine measured the
|
||||||
// SAME text, clipped to the sung fraction. Identical width/font so
|
// active line's word-wrap (`lyrics_measure` / `lyrics_sync`) against this
|
||||||
// wrapping and glyph positions match the base exactly.
|
// view's content-width + font + size and pushed one LyricsSegment per
|
||||||
if root.is-active: Rectangle {
|
// visual row, each carrying `start-weight` (cumulative width fraction
|
||||||
|
// before it) and `width-weight` (its share of the line). We render one
|
||||||
|
// dim-base + clipped-bright-overlay row PER segment with `wrap: no-wrap`
|
||||||
|
// (segments are already single visual lines, Tauri's `white-space:nowrap`),
|
||||||
|
// and re-map the single global `line-progress` to a per-segment local
|
||||||
|
// fraction so row 1 fills to 100% BEFORE row 2 starts — instead of the old
|
||||||
|
// single full-height clip rect that lit every wrapped row at once.
|
||||||
|
if root.is-active: VerticalLayout {
|
||||||
|
for seg[i] in LyricsState.active-segments: HorizontalLayout {
|
||||||
|
// Left-align each visual line; the segment's own width drives the
|
||||||
|
// clip, the HorizontalLayout just keeps it flush-left.
|
||||||
|
alignment: start;
|
||||||
|
segbase := Text {
|
||||||
|
text: seg.text;
|
||||||
|
wrap: no-wrap;
|
||||||
|
// Active line is fully bright (opacity 1.0, dimming ladder
|
||||||
|
// returns 1.0 for the active row) — keep parity with the old
|
||||||
|
// base which animated opacity; active stays solid.
|
||||||
|
color: Theme.text-secondary;
|
||||||
|
font-family: root.font-name;
|
||||||
|
font-size: root.size-active;
|
||||||
|
font-weight: Typography.bold;
|
||||||
|
letter-spacing: 0.2px;
|
||||||
|
|
||||||
|
// Bright (sung) overlay, clipped to this segment's LOCAL fill.
|
||||||
|
// local = clamp((line-progress - start-weight) / width-weight, 0, 1)
|
||||||
|
Rectangle {
|
||||||
x: 0;
|
x: 0;
|
||||||
y: 0;
|
y: 0;
|
||||||
width: base.width * Math.clamp(LyricsState.line-progress, 0.0, 1.0);
|
width: segbase.width * Math.clamp(
|
||||||
height: base.height;
|
seg.width-weight > 0
|
||||||
|
? (LyricsState.line-progress - seg.start-weight) / seg.width-weight
|
||||||
|
: 0,
|
||||||
|
0.0, 1.0);
|
||||||
|
height: segbase.height;
|
||||||
clip: true;
|
clip: true;
|
||||||
animate width {
|
animate width {
|
||||||
duration: LyricsState.fill-anim-ms * 1ms;
|
duration: LyricsState.fill-anim-ms * 1ms;
|
||||||
@@ -205,10 +234,10 @@ component LyricsLineRow inherits VerticalLayout {
|
|||||||
Text {
|
Text {
|
||||||
x: 0;
|
x: 0;
|
||||||
y: 0;
|
y: 0;
|
||||||
width: base.width;
|
width: segbase.width;
|
||||||
height: base.height;
|
height: segbase.height;
|
||||||
text: root.display-text;
|
text: seg.text;
|
||||||
wrap: word-wrap;
|
wrap: no-wrap;
|
||||||
color: root.active-color;
|
color: root.active-color;
|
||||||
font-family: root.font-name;
|
font-family: root.font-name;
|
||||||
font-size: root.size-active;
|
font-size: root.size-active;
|
||||||
@@ -218,6 +247,8 @@ component LyricsLineRow inherits VerticalLayout {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export component LyricsLinesView inherits Rectangle {
|
export component LyricsLinesView inherits Rectangle {
|
||||||
// Owner Q3 layout knobs — 4-6px per side, NEVER removed; the miniplayer
|
// Owner Q3 layout knobs — 4-6px per side, NEVER removed; the miniplayer
|
||||||
@@ -239,6 +270,19 @@ export component LyricsLinesView inherits Rectangle {
|
|||||||
in property <string> font-name: ""; // "" = window default font
|
in property <string> font-name: ""; // "" = window default font
|
||||||
in property <color> active-color: Theme.accent;
|
in property <color> active-color: Theme.accent;
|
||||||
|
|
||||||
|
// Publish the text content width (column width minus the side padding) to
|
||||||
|
// the engine so it can segment the active line's word-wrap at the right
|
||||||
|
// budget for the per-visual-line karaoke highlight. The ROOT is NOT a
|
||||||
|
// repeated row, so reading `self.width` here is SAFE (the header FOOTGUN
|
||||||
|
// forbids geometry reads only in `changed` handlers on the repeated rows).
|
||||||
|
// `init` seeds it (width is set once at construction; the view remounts on
|
||||||
|
// every doc commit), `changed width` keeps it live on resize.
|
||||||
|
function publish-content-width() {
|
||||||
|
LyricsState.content-width = Math.max(0px, self.width - 2 * root.side-padding);
|
||||||
|
}
|
||||||
|
init => { root.publish-content-width(); }
|
||||||
|
changed width => { root.publish-content-width(); }
|
||||||
|
|
||||||
// ---- Auto-center scroll state ----
|
// ---- Auto-center scroll state ----
|
||||||
// Last index the view centered on; -1 = nothing yet -> next center is
|
// Last index the view centered on; -1 = nothing yet -> next center is
|
||||||
// INSTANT ("initial sync" rule). Reset whenever the engine drops the
|
// INSTANT ("initial sync" rule). Reset whenever the engine drops the
|
||||||
|
|||||||
@@ -3309,6 +3309,22 @@ export struct LyricsLineItem {
|
|||||||
has-words: bool,
|
has-words: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// One VISUAL line of the ACTIVE logical line, for the per-visual-line karaoke
|
||||||
|
// highlight. The engine (`lyrics_measure` + `lyrics_sync`) measures the active
|
||||||
|
// line's word-wrap against the current content width + font + size, and pushes
|
||||||
|
// one segment per visual row so the highlight fills row 1 to 100% BEFORE row 2
|
||||||
|
// starts (matching Tauri) instead of lighting every wrapped row at once.
|
||||||
|
//
|
||||||
|
// `start-weight` = cumulative width fraction (0..1) of all PRIOR segments;
|
||||||
|
// `width-weight` = this segment's width / total line width. The row re-maps the
|
||||||
|
// single global `line-progress` to a per-segment local fraction:
|
||||||
|
// local = clamp((line-progress - start-weight) / width-weight, 0, 1)
|
||||||
|
export struct LyricsSegment {
|
||||||
|
text: string,
|
||||||
|
start-weight: float,
|
||||||
|
width-weight: float,
|
||||||
|
}
|
||||||
|
|
||||||
// Lyrics panel state. The Rust glue (`crate::lyrics`) pushes fetch results
|
// Lyrics panel state. The Rust glue (`crate::lyrics`) pushes fetch results
|
||||||
// here on the de-duped track-change edge; the sidebar binds directly. The
|
// here on the de-duped track-change edge; the sidebar binds directly. The
|
||||||
// S4 sync engine drives `active-index` (placeholder -1 until then).
|
// S4 sync engine drives `active-index` (placeholder -1 until then).
|
||||||
@@ -3340,6 +3356,15 @@ export global LyricsState {
|
|||||||
// backward seek) so the fill first-paints exactly at the reported
|
// backward seek) so the fill first-paints exactly at the reported
|
||||||
// progress (Tauri parity, LyricsLines.svelte:114-138).
|
// progress (Tauri parity, LyricsLines.svelte:114-138).
|
||||||
in property <int> fill-anim-ms: 0;
|
in property <int> fill-anim-ms: 0;
|
||||||
|
// Per-visual-line segmentation of the ACTIVE line, for the karaoke
|
||||||
|
// highlight on wrapped lines. The engine recomputes it when the active
|
||||||
|
// index, content-width, font-index or size-index changes (low frequency)
|
||||||
|
// and pushes one entry per visual row. Empty until the first active line.
|
||||||
|
in property <[LyricsSegment]> active-segments: [];
|
||||||
|
// The lyrics view's current text content width (column width minus side
|
||||||
|
// padding). The view pushes it from its root `changed width`; the engine
|
||||||
|
// uses it as the wrap budget when segmenting the active line.
|
||||||
|
in property <length> content-width: 0px;
|
||||||
// Fired by the sidebar's `init` (conditional mount, ADR-010): re-requests
|
// Fired by the sidebar's `init` (conditional mount, ADR-010): re-requests
|
||||||
// lyrics for the current track (cache-served when already fetched).
|
// lyrics for the current track (cache-served when already fetched).
|
||||||
callback panel-opened();
|
callback panel-opened();
|
||||||
|
|||||||
Reference in new issue
Block a user