From 7894e70e578a8982e6ddc00680c5af82c799db16 Mon Sep 17 00:00:00 2001 From: Mehfuz Hossain Date: Mon, 16 Nov 2026 19:52:59 -0800 Subject: [PATCH] Recording markers start chapters A marker set from the recording bar was only written to recording-markers.json, which nothing read. The new project now gets Chapter 1 at the start and a chapter at each marker (markers in the first or last second, or within a second of the one before, add nothing), ready to rename on the Chapters page or by the agent. The flag button says Start a chapter here and counts the markers so far. --- apps/studio/src/recorder.rs | 11 ++++++++++- crates/sf-session/src/create.rs | 29 +++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/apps/studio/src/recorder.rs b/apps/studio/src/recorder.rs index 82ef90d..eda21e8 100644 --- a/apps/studio/src/recorder.rs +++ b/apps/studio/src/recorder.rs @@ -411,9 +411,18 @@ impl RecordingScreen { self.error = Some(e.to_string()); } } - if hud_button(ui, next(&mut x, BTN, 2.0), "marker", |p, r| icon_glyph(p, r, crate::widgets::Icon::Flag, 14.0), "Add a marker (a split point)").clicked() { + // a marker starts a chapter in the project; the count shows each press landed + let marked = rec.markers().len(); + let flag = next(&mut x, BTN, 2.0); + let tip = if marked == 0 { "Start a chapter here".to_string() } else { format!("Start a chapter here ({} marked)", marked) }; + if hud_button(ui, flag, "marker", |p, r| icon_glyph(p, r, crate::widgets::Icon::Flag, 14.0), &tip).clicked() { rec.add_marker(); } + if marked > 0 { + let c = pos2(flag.right() - 5.0, flag.top() + 5.0); + painter.circle_filled(c, 6.5, pal.accent); + painter.text(c, Align2::CENTER_CENTER, marked.min(99).to_string(), FontId::proportional(9.5), Color32::WHITE); + } if hud_button(ui, next(&mut x, BTN, 2.0), "restart", |p, r| icon_glyph(p, r, crate::widgets::Icon::Restart, 15.0), "Start over").clicked() { restart = true; } diff --git a/crates/sf-session/src/create.rs b/crates/sf-session/src/create.rs index 9c2e7ef..1d64bd5 100644 --- a/crates/sf-session/src/create.rs +++ b/crates/sf-session/src/create.rs @@ -113,6 +113,12 @@ pub fn create_project_from_recording(recording_dir: &Path, opts: CreateOptions) } } } + // markers set while recording start chapters (the agent or the Chapters page names them) + let chapters = chapters_from_markers(&opts.markers_ms, duration); + if !chapters.is_empty() { + use sf_project::SceneChaptersExt; + scene.set_chapters(chapters); + } let markers: Vec = opts .markers_ms .iter() @@ -126,6 +132,19 @@ pub fn create_project_from_recording(recording_dir: &Path, opts: CreateOptions) Ok(pkg) } +/// Chapters from markers set while recording (ms of the recording): one at the start, then one +/// at each marker. Markers in the first or last second, or within a second of the one before, +/// add nothing. No markers, no chapters. +pub fn chapters_from_markers(markers_ms: &[f64], duration_ms: f64) -> Vec { + let mut at: Vec = markers_ms.iter().copied().filter(|m| m.is_finite() && *m >= 1000.0 && *m <= duration_ms - 1000.0).collect(); + at.sort_by(f64::total_cmp); + at.dedup_by(|b, a| *b - *a < 1000.0); + if at.is_empty() { + return vec![]; + } + std::iter::once(0.0).chain(at).enumerate().map(|(i, ms)| sf_project::Chapter::new(format!("Chapter {}", i + 1), ms, None)).collect() +} + fn chrono_iso(unix_ms: f64) -> String { let secs = (unix_ms / 1000.0).floor() as i64; let nanos = ((unix_ms / 1000.0 - secs as f64) * 1e9) as u32; @@ -192,4 +211,14 @@ mod tests { assert_eq!(cfg.output.padding_ratio01, 0.1); assert_eq!(cfg.styles.screen_border_radius, 16.0); } + + #[test] + fn markers_start_chapters() { + let ch = chapters_from_markers(&[30_000.0, 500.0, 30_400.0, 90_000.0, 119_600.0], 120_000.0); + let at: Vec<(String, f64)> = ch.iter().map(|c| (c.title.clone(), c.source_ms)).collect(); + // the start, then 30 s and 90 s: too early, too close and too late markers add nothing + assert_eq!(at, vec![("Chapter 1".to_string(), 0.0), ("Chapter 2".into(), 30_000.0), ("Chapter 3".into(), 90_000.0)]); + assert!(chapters_from_markers(&[], 120_000.0).is_empty()); + assert!(chapters_from_markers(&[200.0], 120_000.0).is_empty()); + } }