diff --git a/crates/rundown-converter/src/gui/review.rs b/crates/rundown-converter/src/gui/review.rs index 044cac7..27ec9e4 100644 --- a/crates/rundown-converter/src/gui/review.rs +++ b/crates/rundown-converter/src/gui/review.rs @@ -128,6 +128,73 @@ struct PreviewRow { gap_key: Option, } +struct OverlapShortening { + previous_row: usize, + previous_duration_ms: i64, + shortened_duration_ms: i64, + end_ms: i64, +} + +fn overlap_shortening(analysis: &Analysis, index: usize) -> Option { + let diagnostic = analysis.diagnostics.get(index)?; + if diagnostic.code != "OVERLAP" { + return None; + } + let current_row = diagnostic_row(diagnostic)?; + let pair = + analysis.rundown.as_ref()?.entries.windows(2).find(|pair| { + pair[1].source_rows.first().map(|source| source.row) == Some(current_row) + })?; + let previous = &pair[0]; + let current = &pair[1]; + let shortened_duration_ms = current.start_ms.checked_sub(previous.start_ms)?; + (shortened_duration_ms > 0 && shortened_duration_ms < previous.duration_ms).then_some( + OverlapShortening { + previous_row: previous.source_rows.first()?.row, + previous_duration_ms: previous.duration_ms, + shortened_duration_ms, + end_ms: current.start_ms, + }, + ) +} + +fn overlap_shortening_control( + ui: &mut egui::Ui, + profile: &mut Profile, + shortening: &OverlapShortening, +) { + egui::Frame::group(ui.style()).show(ui, |ui| { + ui.strong("Suggested overlap resolution"); + ui.horizontal_wrapped(|ui| { + if ui + .button(format!( + "Shorten row {} duration", + shortening.previous_row + )) + .on_hover_text(format!( + "Set row {} to end when this overlap begins. The source workbook remains unchanged.", + shortening.previous_row + )) + .clicked() + { + profile.durations.insert( + shortening.previous_row, + display(shortening.shortened_duration_ms), + ); + profile + .ends + .insert(shortening.previous_row, display(shortening.end_ms)); + } + ui.small(format!( + "{} → {} · end {}", + display(shortening.previous_duration_ms), + display(shortening.shortened_duration_ms), + display(shortening.end_ms), + )); + }); + }); +} + fn row_diagnostic_message(row: &PreviewRow, index: usize, diagnostic: &Diagnostic) -> String { if row.related_issues.contains(&index) && diagnostic.code == "OVERLAP" { "This cue is the predecessor in the overlap. Its end conflicts with the following cue's start; either row may need a timing correction or a separate track." @@ -145,10 +212,10 @@ fn row_diagnostic_message(row: &PreviewRow, index: usize, diagnostic: &Diagnosti fn diagnostic_resolution(row: &PreviewRow, index: usize, diagnostic: &Diagnostic) -> &'static str { match diagnostic.code.as_str() { "OVERLAP" if row.related_issues.contains(&index) => { - "Edit this cue's end or duration, or the following cue's start or duration. If both are correct, place them on separate parallel tracks, then apply and recheck." + "Use Shorten duration in Details, edit this cue's end or duration, or edit the following cue's start or duration. If both cues are correct, place them on separate parallel tracks, then apply and recheck." } "OVERLAP" => { - "Edit this cue's start or duration, or the highlighted predecessor's end or duration. If both are correct, place them on separate parallel tracks, then apply and recheck." + "Use Shorten duration in Details, edit this cue's start or duration, or edit the highlighted predecessor's end or duration. If both cues are correct, place them on separate parallel tracks, then apply and recheck." } "BACKWARD_TIME" => { "Correct this cue's start time, or place simultaneous cues on separate tracks, then apply and recheck." @@ -1066,6 +1133,14 @@ impl App { }); ui.small("Leave a field empty to use or calculate it from the source values. Apply & recheck after closing the editor."); } + if let Some(shortening) = self.analysis.as_ref().and_then(|analysis| { + preview + .issues + .iter() + .find_map(|index| overlap_shortening(analysis, *index)) + }) { + overlap_shortening_control(ui, &mut profile, &shortening); + } ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| { if ui.button("Close").clicked() { close = true; @@ -3371,8 +3446,14 @@ mod tests { row_diagnostic_message(previous, overlap, &analysis.diagnostics[overlap]) .contains("either row may need") ); + let shortening = overlap_shortening(&analysis, overlap).unwrap(); + assert_eq!(shortening.previous_row, 2); + assert_eq!(shortening.previous_duration_ms, 5 * 60_000); + assert_eq!(shortening.shortened_duration_ms, 4 * 60_000); + assert_eq!(shortening.end_ms, 9 * 3_600_000 + 4 * 60_000); app.analysis = Some(analysis); + app.analyzed_profile = Some(profile.clone()); app.row_filter = RowFilter::Included; let ctx = egui::Context::default(); configure_style(&ctx); @@ -3407,6 +3488,52 @@ mod tests { && text.contains("How to resolve:") && text.contains("following cue's start or duration")) ); + + let editor_size = [860.0, 900.0]; + for row in [2, 15] { + app.selected_row = Some(row); + app.row_details = true; + render(&mut app, &ctx, editor_size, vec![]); + let labels = render(&mut app, &ctx, editor_size, vec![]); + assert!( + labels + .iter() + .any(|(text, _)| text == "Shorten row 2 duration"), + "overlap resolution should be proposed while editing row {row}" + ); + assert!( + labels + .iter() + .any(|(text, _)| { text == "00:05:00 → 00:04:00 · end 09:04:00" }) + ); + press_enter(&mut app, &ctx, editor_size); + assert!(!app.row_details); + } + app.selected_row = Some(15); + app.row_details = true; + let labels = render(&mut app, &ctx, editor_size, vec![]); + let shorten = labels + .iter() + .find(|(text, _)| text == "Shorten row 2 duration") + .unwrap() + .1 + .center(); + click_size(&mut app, &ctx, shorten, editor_size); + let corrected_profile: Profile = serde_json::from_str(&app.profile).unwrap(); + assert_eq!(corrected_profile.durations[&2], "00:04:00"); + assert_eq!(corrected_profile.ends[&2], "09:04:00"); + let corrected = pipeline::analyze( + &app.tables[0], + &corrected_profile, + &AtomicBool::new(false), + |_, _| {}, + ); + assert!( + !corrected + .diagnostics + .iter() + .any(|diagnostic| diagnostic.code == "OVERLAP") + ); } #[test]