app: keep a renamed clip's game after the rename

The card that leads back to a clip's game found the recording by the id
the export writes into the file's name, and a title change takes that
name away. Saving a title now writes a small link file beside the clip,
`{name}.json`, naming the recording, and carries an existing one along,
so the card survives the rename, leaving the clip view, and a restart.
The clips folder reads the link back, which also keeps the champion on
a grid card. Deleting a clip deletes its link file too.
This commit is contained in:
vhaudiquet committed 2026-11-23 20:53:09 +01:00
1 parent 64b172649b
commit c452fab735
4 files changed
+132 -18

No files matched your search

+72 -12
View File
@@ -3,6 +3,7 @@
//! The grid opens it on a card, and an export opens it when the clip lands. The title is
//! the file's name: saving renames the clip, and the grid follows.
use std::path::Path;
use std::time::Duration;
use rune::tok::{radius, space, type_};
@@ -65,7 +66,7 @@ pub fn open(app: &mut App, name: &str) {
app.clip = Some(ClipView {
name: clip.name.clone(),
title: TextEdit::new(clip.name.clone()),
game: recording_of(app, &clip.name),
game: recording_of(app, &clip),
player: Some(player),
error: None,
muted: false,
@@ -78,17 +79,30 @@ pub fn open(app: &mut App, name: &str) {
/// The recording a clip was cut from.
///
/// The export puts the recording's id in the file's name, so the id is in the clip's name, and
/// the id names the timeline. The row comes from the loaded history when it is there, and from
/// the timeline itself when the history has not paged that far, so an old clip still leads back
/// to its game.
fn recording_of(app: &App, clip_name: &str) -> Option<Summary> {
/// A clip the app renamed carries a file naming its recording; one still named as the export
/// left it carries the id in the name. Either way the row comes from the loaded history when it
/// is there, and from the timeline itself when the history has not paged that far, so an old
/// clip still leads back to its game.
fn recording_of(app: &App, clip: &Clip) -> Option<Summary> {
let id = match &clip.recording_id {
Some(id) => id.clone(),
None => recording_id_in_export_name(app, &clip.name)?,
};
if let Some(game) = app.games.iter().find(|game| game.recording_id == id) {
return Some(game.clone());
}
history::load(&id).map(|game| game.summary())
}
/// The recording a clip's export name carries. The export writes the id into the name, so the
/// id is a substring of it; a clip renamed away from that name carries a link file instead.
fn recording_id_in_export_name(app: &App, clip_name: &str) -> Option<String> {
if let Some(game) = app
.games
.iter()
.find(|game| clip_name.contains(&game.recording_id))
{
return Some(game.clone());
return Some(game.recording_id.clone());
}
let paths = if app.games_paths.is_empty() {
history::timeline_paths()
@@ -98,12 +112,11 @@ fn recording_of(app: &App, clip_name: &str) -> Option<Summary> {
let ids = paths
.iter()
.filter_map(|path| path.file_stem().and_then(|stem| stem.to_str()));
let id = recording_id(clip_name, ids)?;
history::load(id).map(|game| game.summary())
recording_id(clip_name, ids).map(str::to_string)
}
/// The recording a clip's file name carries. The export writes the id into the name, so the id
/// is a substring of it; a clip renamed away from its export name carries none.
/// The recording a name carries, from ids the caller has. A substring check: the export appends
/// to the id, and no two ids share a prefix.
fn recording_id<'a>(clip_name: &str, ids: impl Iterator<Item = &'a str>) -> Option<&'a str> {
ids.into_iter().find(|id| clip_name.contains(id))
}
@@ -193,10 +206,19 @@ pub fn save_title(app: &mut App) {
};
let (from_thumb, to_thumb) = (export::thumb_path(&clip), export::thumb_path(&renamed));
let _ = std::fs::rename(from_thumb, &to_thumb);
// The link to the recording follows the file: a new name no longer names it.
let recording = app
.clip
.as_ref()
.and_then(|view| view.game.as_ref())
.map(|game| game.recording_id.clone())
.or_else(|| clip.recording_id.clone());
keep_link(&clip.path, &to, recording.as_deref());
let entry = &mut app.clips[index];
entry.name = wanted.clone();
entry.path = to;
entry.thumb = to_thumb.exists().then_some(to_thumb);
entry.recording_id = recording;
let view = app.clip.as_mut().expect("checked above");
view.name = wanted;
view.title = TextEdit::new(view.name.clone());
@@ -204,6 +226,16 @@ pub fn save_title(app: &mut App) {
app.spawn_thumbs();
}
/// Keep a clip's link to its recording through a move. One already beside the old file moves
/// with it; a clip that has none gets one from `recording`, while the view still knows it.
fn keep_link(from: &Path, to: &Path, recording: Option<&str>) {
if std::fs::rename(export::link_path(from), export::link_path(to)).is_err()
&& let Some(id) = recording
{
export::link_recording(to, id);
}
}
/// The file name for a typed title. Characters a Windows file cannot carry become dashes,
/// the edges lose their stray spaces and dots, and the whole stays short enough to show.
fn title_name(title: &str) -> String {
@@ -752,7 +784,35 @@ fn seek(app: &mut App, seconds: f64) {
#[cfg(test)]
mod tests {
use super::{recording_id, title_name};
use super::{keep_link, recording_id, title_name};
use crate::export::{link_path, link_recording, recording_link};
#[test]
fn a_renamed_clip_keeps_the_link_to_its_recording() {
let dir = std::env::temp_dir().join(format!("leaguerecorder-{}", uuid::Uuid::new_v4()));
std::fs::create_dir_all(&dir).unwrap();
let from = dir.join("demo-1-clip.mp4");
let to = dir.join("My Ace.mp4");
std::fs::write(&from, b"").unwrap();
std::fs::write(&to, b"").unwrap();
// A link from an earlier rename moves with the file.
link_recording(&from, "demo-1");
keep_link(&from, &to, Some("ignored"));
assert_eq!(recording_link(&to).as_deref(), Some("demo-1"));
// A clip exported before links existed gets one from the game the view knows.
std::fs::remove_file(link_path(&to)).unwrap();
keep_link(&from, &to, Some("demo-2"));
assert_eq!(recording_link(&to).as_deref(), Some("demo-2"));
// With no game known there is nothing to write.
std::fs::remove_file(link_path(&to)).unwrap();
keep_link(&from, &to, None);
assert_eq!(recording_link(&to), None);
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn a_clip_name_carries_the_recording_it_was_cut_from() {
+14 -6
View File
@@ -127,13 +127,18 @@ struct Card {
impl Card {
/// The card for one clip. The game the clip was cut from, when its recording is in the
/// loaded history, names the card; otherwise the file does. Export names carry the
/// recording's id somewhere in them, so a match is a contains check away.
/// loaded history, names the card; otherwise the file does. The recording comes from the
/// clip's link file, or from the export name, which carries the id.
fn of(app: &App, clip: &Clip, index: usize) -> Card {
let game = app
.games
.iter()
.find(|game| clip.name.contains(&game.recording_id));
let game = clip
.recording_id
.as_deref()
.and_then(|id| app.games.iter().find(|game| game.recording_id == id))
.or_else(|| {
app.games
.iter()
.find(|game| clip.name.contains(&game.recording_id))
});
let when = format::age(clip.exported);
match game {
Some(game) => Card {
@@ -353,6 +358,9 @@ fn delete_confirmed(app: &mut App) {
};
let clip = app.clips.remove(index);
let _ = std::fs::remove_file(crate::export::thumb_path(&clip));
if !clip.path.as_os_str().is_empty() {
let _ = std::fs::remove_file(crate::export::link_path(&clip.path));
}
match std::fs::remove_file(&clip.path) {
Ok(()) => app.notify("Clip deleted"),
Err(error) => {