diff --git a/app/src/app.rs b/app/src/app.rs index 5a1734c..bbf1eae 100644 --- a/app/src/app.rs +++ b/app/src/app.rs @@ -473,6 +473,35 @@ impl App { self.processes_rx = Some(rx); } + /// Where the daemon writes new recordings, as the settings it holds say. + /// + /// The draft stands in until the daemon has answered, so a first launch points at the + /// default folder the daemon would use anyway. + pub fn output_dir(&self) -> PathBuf { + let path = self + .settings + .as_ref() + .map(|settings| settings.output.path.as_str()) + .unwrap_or(self.form.draft.output.path.as_str()); + if path.is_empty() { + crate::daemon::recordings_dir() + } else { + PathBuf::from(path) + } + } + + /// The folders a recording's clip may be in: the configured output folder first, then the + /// default one, where timelines and the clips folder live. + pub fn video_roots(&self) -> Vec { + let default = crate::daemon::recordings_dir(); + let output = self.output_dir(); + if output == default { + vec![output] + } else { + vec![output, default] + } + } + /// List the timelines on disk and read the first page of rows on a thread of their own. /// `poll` picks them up. pub fn refresh_games(&mut self) { @@ -700,14 +729,15 @@ impl App { Err(e) => self.notify_error(e), } } - // The daemon answers a save with `{"success": true}`; the draft is already what - // it holds, so there is nothing to read back. + // The daemon answers a save with `{"success": true}`. Read the settings back: + // it may have resolved the recordings folder to an absolute path. (ipc::Request::SaveSettings, Ok(_)) => { self.settings = Some(self.form.settings()); self.form.saving = false; self.form.dirty = false; self.form.loaded = true; self.notify("Settings saved"); + self.daemon.get_settings(); } (ipc::Request::SaveSettings, Err(e)) => { self.form.saving = false; @@ -966,12 +996,11 @@ pub fn poll(app: &mut App, t: f64, tx: &mut Tx) -> bool { if let Some(at) = std::env::var("LR_DEMO_PAUSE_AT") .ok() .and_then(|v| v.parse::().ok()) + && let Some(player) = &app.review.player + && player.is_playing() + && player.time() >= at { - if let Some(player) = &app.review.player { - if player.is_playing() && player.time() >= at { - app.review.playback(); - } - } + app.review.playback(); } app.handle_poll(t, tx) } diff --git a/app/src/history.rs b/app/src/history.rs index 48b3eb8..ef42e69 100644 --- a/app/src/history.rs +++ b/app/src/history.rs @@ -38,6 +38,13 @@ pub struct Timeline { pub raw_end_game_stats: Option, #[serde(default)] pub video_file: Option, + /// The absolute path the recorder wrote the video to, when it recorded one. + /// + /// The recordings folder can change while older clips stay where they were, so the path + /// travels with the timeline. A recording made before the recorder wrote it has only + /// `video_file`, and is found by name in the folders the app searches. + #[serde(default)] + pub video_path: Option, /// What each audio track of the video carries, in file order. Empty for recordings made /// before the recorder wrote one track per source. #[serde(default)] @@ -188,6 +195,9 @@ pub struct Summary { pub duration_secs: i64, /// The video file the row names, kept so deleting a row does not re-read its timeline. pub video_file: Option, + /// The absolute path the recorder stored with the timeline, when it did, so a row still + /// finds its clip after the recordings folder changes. + pub video_path: Option, champion: Option, summoner: Option, queue_label: String, @@ -226,6 +236,15 @@ impl Summary { pub fn lp_change(&self) -> Option { self.lp.clone() } + + /// The video file of this row: its stored path, or its name in `roots`. + pub fn video_path(&self, roots: &[PathBuf]) -> Option { + video_path_of( + self.video_path.as_deref(), + self.video_file.as_deref(), + roots, + ) + } } impl Timeline { @@ -236,6 +255,7 @@ impl Timeline { start_time: self.start_time, duration_secs: self.duration_secs, video_file: self.video_file.clone(), + video_path: self.video_path.clone(), champion: self.champion(), summoner: self.summoner(), queue_label: self.queue_label(), @@ -256,19 +276,16 @@ pub fn load(recording_id: &str) -> Option { /// Remove a recording: its timeline JSON and its video file. /// -/// The video is found the way review finds it, so a clip renamed but keeping the timestamp -/// prefix goes too. A file that is already gone is fine: the list shows what was read -/// earlier, and a tidy-up elsewhere should not turn into an error here. -pub fn delete(recording_id: &str, video_file: Option<&str>) -> Result<(), String> { - delete_in(recording_id, video_file, &daemon::recordings_dir()) +/// The caller resolves the video, searching the folders the app records to; the path stored +/// with the timeline keeps a clip reachable after the recordings folder changes. A file that +/// is already gone is fine: the list shows what was read earlier, and a tidy-up elsewhere +/// should not turn into an error here. +pub fn delete(recording_id: &str, video: Option<&Path>) -> Result<(), String> { + delete_in(recording_id, video, &daemon::recordings_dir()) } /// [`delete`] inside one recordings folder, so a test can point it at a temporary one. -fn delete_in( - recording_id: &str, - video_file: Option<&str>, - recordings: &Path, -) -> Result<(), String> { +fn delete_in(recording_id: &str, video: Option<&Path>, recordings: &Path) -> Result<(), String> { let json = recordings .join("timelines") .join(format!("{recording_id}.json")); @@ -276,8 +293,8 @@ fn delete_in( std::fs::remove_file(&json) .map_err(|e| format!("could not delete {}: {e}", json.display()))?; } - if let Some(video) = video_file.and_then(|name| find_video_file(recordings, name)) { - std::fs::remove_file(&video) + if let Some(video) = video.filter(|path| path.exists()) { + std::fs::remove_file(video) .map_err(|e| format!("could not delete {}: {e}", video.display()))?; } Ok(()) @@ -495,13 +512,33 @@ impl Timeline { }) } - /// The recording's video file, by name or by the timestamp prefix. - pub fn video_path(&self) -> Option { - let dir = daemon::recordings_dir(); - find_video_file(&dir, self.video_file.as_deref()?) + /// The recording's video file: the path stored with the timeline, or its name in `roots`. + pub fn video_path(&self, roots: &[PathBuf]) -> Option { + video_path_of( + self.video_path.as_deref(), + self.video_file.as_deref(), + roots, + ) } } +/// The video file of a recording, as the stored path or a name in one of `roots`. +/// +/// The stored path is what the recorder wrote, so a clip stays with its timeline after the +/// recordings folder changes. A timeline from before the recorder wrote the path has only the +/// name, and is looked up in every folder the app records to. +fn video_path_of( + stored: Option<&Path>, + video_file: Option<&str>, + roots: &[PathBuf], +) -> Option { + if let Some(path) = stored.filter(|path| path.exists()) { + return Some(path.to_path_buf()); + } + let name = video_file?; + roots.iter().find_map(|dir| find_video_file(dir, name)) +} + /// Exact name first, then a file sharing the timestamp prefix. pub fn find_video_file(dir: &Path, filename: &str) -> Option { let exact = dir.join(filename); @@ -617,6 +654,7 @@ pub fn sample() -> Vec { } })), video_file: None, + video_path: None, audio_tracks: Vec::new(), } }; @@ -662,6 +700,20 @@ mod tests { assert!((timeline.events[1].time() - 1500.5).abs() < 1e-6); } + #[test] + fn a_timeline_reads_the_path_the_recorder_stored() { + let json = TIMELINE.replacen( + "\"video_file\"", + r#""video_path": "C:/clips/game.mp4", "video_file""#, + 1, + ); + let timeline: Timeline = serde_json::from_str(&json).unwrap(); + assert_eq!( + timeline.video_path.as_deref(), + Some(Path::new("C:/clips/game.mp4")) + ); + } + #[test] fn the_game_values_come_out_of_the_raw_responses() { let timeline = parsed(); @@ -721,13 +773,56 @@ mod tests { std::fs::create_dir_all(dir.join("timelines")).unwrap(); std::fs::write(dir.join("timelines").join("game.json"), TIMELINE).unwrap(); std::fs::write(dir.join("2026-03-27_16-42-52_kai_sa.mp4"), b"x").unwrap(); + let video = dir.join("2026-03-27_16-42-52_kai_sa.mp4"); - delete_in("game", Some("2026-03-27_16-42-52_unknown.mp4"), &dir).unwrap(); + delete_in("game", Some(&video), &dir).unwrap(); assert!(!dir.join("timelines").join("game.json").exists()); assert!(!dir.join("2026-03-27_16-42-52_kai_sa.mp4").exists()); // What is already gone is not an error. - delete_in("game", Some("2026-03-27_16-42-52_unknown.mp4"), &dir).unwrap(); + delete_in("game", Some(&video), &dir).unwrap(); + let _ = std::fs::remove_dir_all(dir); + } + + #[test] + fn a_clip_is_found_in_the_folder_it_was_recorded_to() { + let default = std::env::temp_dir().join(format!("leaguerecorder-{}", uuid::Uuid::new_v4())); + let output = std::env::temp_dir().join(format!("leaguerecorder-{}", uuid::Uuid::new_v4())); + std::fs::create_dir_all(&default).unwrap(); + std::fs::create_dir_all(&output).unwrap(); + let video = output.join("2026-03-27_16-42-52_kai_sa.mp4"); + std::fs::write(&video, b"x").unwrap(); + + let timeline = parsed(); + let roots = [output, default]; + assert_eq!(timeline.video_path(&roots), Some(video.clone())); + assert_eq!(timeline.summary().video_path(&roots), Some(video)); + + let _ = std::fs::remove_dir_all(&roots[0]); + let _ = std::fs::remove_dir_all(&roots[1]); + } + + #[test] + fn a_finished_timeline_keeps_the_path_of_its_clip() { + let dir = std::env::temp_dir().join(format!("leaguerecorder-{}", uuid::Uuid::new_v4())); + std::fs::create_dir_all(&dir).unwrap(); + let video = dir.join("old-folder").join("game.mp4"); + std::fs::create_dir_all(video.parent().unwrap()).unwrap(); + std::fs::write(&video, b"x").unwrap(); + + let mut timeline = parsed(); + timeline.video_path = Some(video.clone()); + // The folders the app knows now hold nothing: the stored path still finds the clip. + assert_eq!(timeline.video_path(&[]), Some(video.clone())); + + // A stored path that is gone falls back to the name in the folders it is given. + timeline.video_path = Some(dir.join("moved.mp4")); + std::fs::write(dir.join("2026-03-27_16-42-52_kai_sa.mp4"), b"x").unwrap(); + assert_eq!( + timeline.video_path(std::slice::from_ref(&dir)), + Some(dir.join("2026-03-27_16-42-52_kai_sa.mp4")) + ); + let _ = std::fs::remove_dir_all(dir); } } diff --git a/app/src/screens/history.rs b/app/src/screens/history.rs index 10e621e..bbb8b31 100644 --- a/app/src/screens/history.rs +++ b/app/src/screens/history.rs @@ -457,7 +457,8 @@ fn delete_confirmed(app: &mut App) { let Some(summary) = app.games.iter().find(|g| g.recording_id == id).cloned() else { return; }; - if let Err(e) = history::delete(&id, summary.video_file.as_deref()) { + let video = summary.video_path(&app.video_roots()); + if let Err(e) = history::delete(&id, video.as_deref()) { app.notify_error(e); return; } diff --git a/app/src/screens/review.rs b/app/src/screens/review.rs index cf6865f..1d7d50b 100644 --- a/app/src/screens/review.rs +++ b/app/src/screens/review.rs @@ -1,5 +1,6 @@ //! Game review: the recorded clip, a scrubber with event markers, highlights and export. +use std::path::PathBuf; use std::sync::mpsc::{Receiver, TryRecvError, channel}; use rune::tok::{radius, space, type_}; @@ -15,6 +16,8 @@ use crate::ui::{self, Paint, body, heading}; pub struct Review { /// The opened recording, once its file has been read. pub game: Option, + /// The folders the clip may be in, as the settings named them when the row opened. + pub video_roots: Vec, /// Set while that file is on its way to the screen. pub loading: bool, pub player: Option, @@ -348,6 +351,7 @@ impl Default for Review { fn default() -> Self { Self { game: None, + video_roots: Vec::new(), loading: false, player: None, sounds: Vec::new(), @@ -396,7 +400,8 @@ pub fn open(app: &mut App, recording_id: &str) { /// Fill the review screen with a timeline that has been read, and load its clip. pub fn show(app: &mut App, game: Timeline) { - let path = game.video_path(); + let roots = app.video_roots(); + let path = game.video_path(&roots); let highlights = highlight::compute( &game, game.summoner().as_deref(), @@ -405,6 +410,7 @@ pub fn show(app: &mut App, game: Timeline) { app.review.highlights = highlights; app.review.duration = game.duration_secs.max(0) as f64; let video_file = game.video_file.clone(); + app.review.video_roots = roots; app.review.game = Some(game); let Some(path) = path else { @@ -459,7 +465,7 @@ fn follow_sound(app: &mut App) { .review .game .as_ref() - .and_then(history::Timeline::video_path) + .and_then(|game| game.video_path(&app.review.video_roots)) else { return; }; @@ -912,7 +918,7 @@ fn channels(app: &App, p: Paint) -> Option { fn clip_row(app: &App, p: Paint, clip: Clip) -> El { let video = app .reviewed() - .and_then(|game| game.video_path()) + .and_then(|game| game.video_path(&app.review.video_roots)) .map(|path| path.display().to_string()); let name = app .reviewed() @@ -1203,7 +1209,9 @@ fn highlights(app: &App, p: Paint, game: &Timeline) -> El { for (index, clip) in app.review.highlights.iter().enumerate() { let start = clip.start; let end = clip.end; - let video = game.video_path().map(|path| path.display().to_string()); + let video = game + .video_path(&app.review.video_roots) + .map(|path| path.display().to_string()); let name = format!("{}-highlight-{}", game.recording_id, index); let kind_ink = match clip.kind { highlight::Kind::Death => p.danger, diff --git a/app/src/screens/settings.rs b/app/src/screens/settings.rs index bbc80e5..4a9f0d7 100644 --- a/app/src/screens/settings.rs +++ b/app/src/screens/settings.rs @@ -397,8 +397,8 @@ fn quick(app: &App, p: Paint, kit: &rune_kit::Kit, layer: &mut Vec, content: "open-recordings", "Open folder", false, - |_app: &mut App| { - crate::daemon::open_folder(&crate::daemon::recordings_dir()); + |app: &mut App| { + crate::daemon::open_folder(&app.output_dir()); }, )), vec![ui::setting_row( @@ -479,8 +479,8 @@ fn recording(app: &App, p: Paint, kit: &rune_kit::Kit, layer: &mut Vec, cont "open-output", "Open folder", false, - |_app: &mut App| { - crate::daemon::open_folder(&crate::daemon::recordings_dir()); + |app: &mut App| { + crate::daemon::open_folder(&app.output_dir()); }, )), vec![