diff --git a/app/src/screens/clip.rs b/app/src/screens/clip.rs index b3faa47..a1c2964 100644 --- a/app/src/screens/clip.rs +++ b/app/src/screens/clip.rs @@ -14,6 +14,7 @@ use rune::{ use crate::app::{App, Screen}; use crate::export::{self, Clip}; use crate::format; +use crate::history::{self, Outcome, Summary}; use crate::icons; use crate::ui::{self, Paint, body, heading}; @@ -23,6 +24,9 @@ pub struct ClipView { pub name: String, /// The title as it is being typed. Saving turns it into the file's name. pub title: TextEdit, + /// The recording the clip was cut from, once it is found: the card in the side panel shows + /// it, and the card leads back to it. Kept here so a renamed clip keeps the way back. + pub game: Option, pub player: Option, /// Shown over the stage when the file refuses to play. pub error: Option, @@ -61,6 +65,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), player: Some(player), error: None, muted: false, @@ -71,6 +76,38 @@ pub fn open(app: &mut App, name: &str) { app.screen = Screen::Clip; } +/// 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 { + if let Some(game) = app + .games + .iter() + .find(|game| clip_name.contains(&game.recording_id)) + { + return Some(game.clone()); + } + let paths = if app.games_paths.is_empty() { + history::timeline_paths() + } else { + app.games_paths.clone() + }; + 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()) +} + +/// 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. +fn recording_id<'a>(clip_name: &str, ids: impl Iterator) -> Option<&'a str> { + ids.into_iter().find(|id| clip_name.contains(id)) +} + /// Follow the clip's clock so the bar and the time move, and hand a pending share to the /// clipboard worker. pub fn tick(app: &mut App, tx: &mut Tx) -> bool { @@ -192,27 +229,23 @@ pub fn view(app: &App, p: Paint) -> El { ); }; let clip = app.clips.iter().find(|clip| clip.name == view.name); - let game = clip.and_then(|clip| { - app.games - .iter() - .find(|game| clip.name.contains(&game.recording_id)) - }); + let game = view.game.as_ref(); let title = game .map(|game| game.champion().unwrap_or_else(|| view.name.clone())) .unwrap_or_else(|| view.name.clone()); - let detail = match (clip, game) { - (Some(clip), Some(game)) => { - format!("{} · {}", game.queue_label(), format::age(clip.exported)) - } - (Some(clip), None) => format!( - "{} · {}", - format::bytes(clip.bytes), - format::age(clip.exported) - ), - _ => String::new(), - }; + // The card below says what the game was; this line is what the file is. + let detail = clip + .map(|clip| { + format!( + "{} · {}", + format::bytes(clip.bytes), + format::age(clip.exported) + ) + }) + .unwrap_or_default(); - // The player takes the window; the side panel holds the title, the share and the facts. + // The player takes the window; the side panel holds the title, the share, and the card + // that leads back to the game the clip was cut from. let body = row() .key("clip-body") .w_full() @@ -311,7 +344,8 @@ pub fn view(app: &App, p: Paint) -> El { .unwrap_or_default(); request_share(app, &name); })), - ), + ) + .children(game.map(|game| game_card(app, p, game))), ); col() .key("clip-view") @@ -322,6 +356,144 @@ pub fn view(app: &App, p: Paint) -> El { .child(body) } +/// The card at the foot of the side panel: the champion the clip stars, the scoreline, and the +/// game the clip was cut from. The whole card opens that recording; the arrow marks it as a link. +fn game_card(app: &App, p: Paint, game: &Summary) -> El { + let champion = game.champion().unwrap_or_else(|| "Unknown champion".into()); + let outcome = game.outcome(); + let ink = match outcome { + Outcome::Victory => p.success, + Outcome::Defeat => p.danger, + Outcome::Terminated => p.muted(), + }; + let stats = game.stats(); + let score = stats.as_ref().map_or_else( + || "0/0/0".to_string(), + |s| format!("{}/{}/{}", s.kills, s.deaths, s.assists), + ); + let ratio = stats.as_ref().map_or_else( + || "0.0".to_string(), + |s| format::kda_ratio(s.kills, s.deaths, s.assists), + ); + let queue = game.queue_label().to_string(); + let length = format::duration(game.duration_secs.max(0) as f64); + let id = game.recording_id.clone(); + let label = format!("Open the game this clip came from: {champion}, {score}, {queue}"); + col() + .key("game-card") + .role("button") + .label(label) + .focusable(true) + .w_full() + .padding(space::S3) + .gap(space::S2) + .radius(radius::LG) + .fill(p.t.card) + .border(p.hair, p.t.card_border) + .shadow(1.0, 2.0, p.t.shadow) + .shadow(8.0, 24.0, p.t.shadow_soft) + .hover(p.t.hover) + .press(p.t.press) + .on_press(move |app: &mut App| crate::screens::review::open(app, &id)) + .child( + row() + .key("card-head") + .w_full() + .gap(space::S2) + .center_y() + .child(ui::champion_portrait( + app, + p, + game.champion().as_deref(), + 40.0, + )) + .child( + col() + .key("card-who") + .grow(1.0) + .min_w(0.0) + .gap(1.0) + .child( + text(champion) + .key("card-champion") + .font(heading(14.0)) + .color(p.t.text) + .shrink() + .truncate(), + ) + .child( + text(format!("{queue} · {length}")) + .key("card-queue") + .font(body(11.0)) + .color(p.muted()) + .shrink() + .truncate(), + ), + ) + .child( + text(outcome.label()) + .key("card-outcome") + .font(type_::LABEL) + .color(ink), + ), + ) + .child( + row() + .key("card-score") + .w_full() + .center_y() + .gap(space::S2) + .child( + text(score) + .key("card-kda") + .font(heading(17.0)) + .color(p.t.text), + ) + .child( + text(format!("{ratio} KDA")) + .key("card-ratio") + .font(body(11.0)) + .color(p.muted()), + ) + .child(el().grow(1.0)) + .child( + text(format::relative_time(game.start_time)) + .key("card-when") + .font(body(11.0)) + .color(p.muted()) + .shrink() + .truncate(), + ), + ) + .child( + el().key("card-sep") + .w_full() + .h(p.hair) + .fill(p.t.separator) + .a11y_hidden(), + ) + .child( + row() + .key("card-open") + .w_full() + .center_y() + .justify_between() + .child( + text("Original game") + .key("card-open-label") + .font(type_::LABEL) + .color(p.accent), + ) + .child( + text("→") + .key("card-open-arrow") + .font(heading(14.0)) + .color(p.accent) + .a11y_hidden(), + ), + ) +} + /// The stage: the clip on black, the largest 16:9 box that fits, with the controls drawn over /// it. fn video_stage(app: &App, p: Paint) -> El { @@ -580,7 +752,27 @@ fn seek(app: &mut App, seconds: f64) { #[cfg(test)] mod tests { - use super::title_name; + use super::{recording_id, title_name}; + + #[test] + fn a_clip_name_carries_the_recording_it_was_cut_from() { + let ids = ["demo-1", "demo-2"].into_iter(); + assert_eq!( + recording_id("demo-1-clip", ids.clone()), + Some("demo-1"), + "the export appends to the id" + ); + assert_eq!( + recording_id("demo-2-highlight-3", ids.clone()), + Some("demo-2"), + "a highlight carries it too" + ); + assert_eq!( + recording_id("pentakill", ids), + None, + "a renamed clip names no recording" + ); + } #[test] fn a_title_keeps_its_letters_and_spaces() {