//! Game review: the recorded clip, a scrubber with event markers, highlights and export. use std::sync::mpsc::{Receiver, channel}; use rune::tok::{radius, space, type_}; use rune::{El, Player, SurfaceFit, Tx, col, el, row, text}; use crate::app::{App, Screen}; use crate::format; use crate::highlight::{self, Highlight}; use crate::history::{Outcome, Timeline}; use crate::ui::{self, Paint, body, heading}; #[derive(Default)] pub struct Review { pub game: Option, pub player: Option, pub time: f64, pub duration: f64, pub scrubber_width: f32, pub show_details: bool, pub highlights: Vec, pub export: Option>>, pub export_note: Option, pub error: Option, } impl Review { fn playback(&mut self) { if let Some(player) = &self.player { if player.is_playing() { player.pause(); } else { player.play(); } } } fn seek(&mut self, seconds: f64) { let at = seconds.clamp(0.0, self.duration.max(0.0)); if let Some(player) = &self.player { player.seek(at); self.time = at; } } } /// Open a game in the review screen and load its clip. pub fn open(app: &mut App, index: usize) { leave(app); app.screen = Screen::Review; let Some(game) = app.games.get(index) else { return; }; // Keep the recording id, not the row number: a new recording can re-sort the list. app.review.game = Some(game.recording_id.clone()); let path = game.video_path(); let highlights = highlight::compute( game, game.summoner().as_deref(), highlight::Options::default(), ); let duration = game.duration_secs.max(0) as f64; app.review.highlights = highlights; app.review.duration = duration; let Some(path) = path else { app.review.error = Some(format!( "No video file found for this recording ({}).", game.video_file.as_deref().unwrap_or("unnamed") )); return; }; let player = Player::file(&path); player.set_looping(false); if let Some(waker) = &app.waker { player.set_waker(waker.clone()); } app.review.player = Some(player); } /// Leave the review screen and stop playback. pub fn leave(app: &mut App) { app.review.player = None; app.review.game = None; app.review.highlights.clear(); app.review.time = 0.0; app.review.duration = 0.0; app.review.error = None; app.review.export_note = None; app.review.export = None; } /// Called from `poll` while the review screen is up. pub fn tick(app: &mut App, tx: &mut Tx) -> bool { let mut changed = false; if let Some(player) = &app.review.player { let time = player.time(); if (time - app.review.time).abs() > 0.001 { app.review.time = time; changed = true; } if let Some(duration) = player.duration() && duration > 0.0 && (duration - app.review.duration).abs() > 0.05 { app.review.duration = duration; changed = true; } if app.review.error.is_none() && let rune::PlayerState::Failed(message) = player.state() { app.review.error = Some(message); changed = true; } if player.is_playing() { tx.wake_after(std::time::Duration::from_millis(33)); } } if let Some(rx) = &app.review.export { match rx.try_recv() { Ok(Ok(path)) => { app.review.export_note = Some(format!("Clip saved to {path}")); app.review.export = None; changed = true; } Ok(Err(e)) => { app.review.export_note = Some(format!("Export failed: {e}")); app.review.export = None; changed = true; } Err(_) => {} } } changed } pub fn view(app: &App, p: Paint) -> El { let Some(game) = app.reviewed() else { return col() .key("review") .padding(space::S6) .child(text("No game selected.").font(body(13.0)).color(p.muted())); }; let outcome = game.outcome(); let ink = match outcome { Outcome::Victory => p.success, Outcome::Defeat => p.danger, Outcome::Terminated => p.muted(), }; let header = row() .key("review-header") .w_full() .center_y() .justify_between() .child( row() .key("review-title") .gap(10.0) .center_y() .child(ui::button( p, "back", "← Match history", false, |app: &mut App| { leave(app); app.screen = Screen::History; }, )) .child(ui::champion_portrait( app, p, game.champion().as_deref(), 26.0, )) .child( text(game.champion().unwrap_or_else(|| "Unknown champion".into())) .key("champion") .font(heading(20.0)) .color(p.t.text), ) .child( text(outcome.label()) .key("outcome") .font(type_::LABEL) .color(ink), ) .child( text(format!( "{} · {}", game.queue_label(), format::duration(app.review.duration) )) .key("meta") .font(body(12.0)) .color(p.muted()), ), ) .child( text(format::game_start(game.start_time)) .key("started") .font(body(11.5)) .color(p.muted()), ); // The player takes the window: the clip is why the screen exists. The details column // opens beside it on demand, so a closed panel leaves the clip the whole width. let body = row() .key("review-body") .w_full() .grow(1.0) .min_h(0.0) .gap(space::S3) .child( col() .key("player") .grow(1.0) .min_w(0.0) .min_h(0.0) .gap(space::S2) .child(video_stage(app, p)) .child(transport(app, p)) .child(scrubber(app, p, game)), ); let body = if app.review.show_details { body.child( col() .key("details") .w(380.0) .min_w(320.0) .h_full() .min_h(0.0) .gap(space::S2) .child(highlights(app, p, game)) .child(events(app, p, game)), ) } else { body }; col() .key("review") .w_full() .h_full() .padding(space::S6) .gap(space::S3) .child(header) .child(body) } /// The clip itself: the largest 16:9 box that fits, on black, with a message when there is no /// file to play. fn video_stage(app: &App, p: Paint) -> El { let stage = el() .key("stage") .w_full() .grow(1.0) .min_h(0.0) .fill(rune::Color::BLACK) .radius(radius::LG) .clip_soft(radius::LG) .center(); match &app.review.player { Some(player) => stage.child( el().key("video") .aspect_fit(16.0 / 9.0) .video(player, SurfaceFit::Fill), ), None => stage.child( col().key("stage-empty").center().gap(6.0).child( text( app.review .error .clone() .unwrap_or_else(|| "Loading the clip…".into()), ) .key("stage-message") .font(body(13.0)) .color(p.muted()), ), ), } } /// Play/pause, the ten-second steps and the clock, with the highlights toggle on the other /// side: the list is one press away, not a column that always takes the clip's width. fn transport(app: &App, p: Paint) -> El { let count = app.review.highlights.len(); let bar = row() .key("transport") .w_full() .center_y() .gap(space::S2) .child(ui::button( p, "play-pause", if playing(app) { "Pause" } else { "Play" }, false, |app: &mut App| { app.review.playback(); }, )) .child(ui::button(p, "rewind", "-10 s", false, |app: &mut App| { let at = app.review.time - 10.0; app.review.seek(at); })) .child(ui::button(p, "advance", "+10 s", false, |app: &mut App| { let at = app.review.time + 10.0; app.review.seek(at); })) .child( text(format!( "{} / {}", format::clock(app.review.time), format::clock(app.review.duration) )) .key("time") .font(type_::MONO_CAPTION) .color(p.muted()), ) .child(el().grow(1.0)); let bar = match &app.review.export_note { Some(note) => bar.child( text(note.clone()) .key("export-note") .font(body(11.5)) .color(p.muted()), ), None => bar, }; bar.child(ui::button( p, "toggle-details", &if app.review.show_details { "Hide details".to_string() } else { format!("Highlights · {count}") }, false, |app: &mut App| { app.review.show_details = !app.review.show_details; }, )) } fn playing(app: &App) -> bool { app.review.player.as_ref().is_some_and(Player::is_playing) } fn scrubber(app: &App, p: Paint, game: &Timeline) -> El { let width = if app.review.scrubber_width > 0.0 { app.review.scrubber_width } else { 800.0 }; let duration = app.review.duration.max(0.001); let progress = (app.review.time / duration).clamp(0.0, 1.0) as f32; let knob = 14.0; let player = game.summoner(); let mut bar = el() .key("scrubber") .role("slider") .label("Playback position") .range_value(app.review.time, 0.0, duration) .w_full() .h(34.0) .on_layout(|app: &mut App, [_, _, w, _]| { app.review.scrubber_width = w; }) .on_step(|app: &mut App, steps, _| { let at = app.review.time + steps as f64 * 5.0; app.review.seek(at); }) .on_drag(move |app: &mut App, d: &mut rune::Drag, tx| { let w = app.review.scrubber_width.max(1.0); let at = (d.x / w).clamp(0.0, 1.0) as f64 * app.review.duration; app.review.seek(at); tx.instant(); }) .child( el().key("track") .w_full() .h(6.0) .absolute(0.0, 12.0) .radius(3.0) .fill(p.t.control_off) .a11y_hidden(), ); // Each highlight as a band on the track, so the clip it exports is visible at a glance. for (index, clip) in app.review.highlights.iter().enumerate() { let x = (clip.start / duration).clamp(0.0, 1.0) as f32 * width; let end = (clip.end / duration).clamp(0.0, 1.0) as f32 * width; let ink = match clip.kind { highlight::Kind::Death => p.danger, highlight::Kind::Assist => p.muted(), _ => p.success, }; bar = bar.child( el().key(format!("clip-{index}")) .w((end - x).max(4.0)) .h(6.0) .absolute(x, 12.0) .radius(3.0) .fill(ink.alpha(0.32)) .a11y_hidden(), ); } bar = bar.child( el().key("played") .w((width * progress).max(2.0)) .h(6.0) .absolute(0.0, 12.0) .radius(3.0) .fill(p.accent.alpha(0.55)) .a11y_hidden(), ); // One tick per timeline event, below the track: every kill, objective and change the // recording has, not only the player's own. for (index, event) in game.events.iter().enumerate() { let at = event.time(); if !at.is_finite() || at < 0.0 || at > duration { continue; } let x = (at / duration) as f32 * (width - 2.0); let involvement = player .as_deref() .and_then(|name| highlight::involvement(event, name)); let ink = match event.event_type.as_str() { "kill" => match involvement { Some(highlight::Kind::Death) => p.danger, Some(_) => p.success, None => p.muted().alpha(0.75), }, "objective" | "lp_change" => p.accent, "game_start" | "game_end" => p.muted(), _ => p.muted().alpha(0.5), }; let label = match involvement { Some(kind) => kind.label().to_string(), None if event.description.is_empty() => event.event_type.clone(), None => event.description.clone(), }; bar = bar.child( el().key(format!("event-tick-{index}")) .role("button") .label(format!("{label} at {}", format::clock(at))) .w(2.0) .h(9.0) .absolute(x, 21.0) .fill(ink) .on_press(move |app: &mut App| app.review.seek(at)) .a11y_hidden(), ); } bar.child( el().key("knob") .size(knob, knob) .absolute(progress * (width - knob), 8.0) .radius(knob / 2.0) .fill(p.t.knob) .shadow(1.0, 3.0, p.t.knob_shadow) .shadow(0.0, 0.5, p.t.edge) .a11y_hidden(), ) } fn highlights(app: &App, p: Paint, game: &Timeline) -> El { let mut list = col().key("highlight-rows").w_full().gap(4.0); if app.review.highlights.is_empty() { list = list.child( text(if game.events.is_empty() { "No timeline was saved for this recording." } else { "No kills or deaths in this recording." }) .key("no-highlights") .font(body(12.0)) .color(p.muted()) .padding(space::S2), ); } 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 name = format!("{}-highlight-{}", game.recording_id, index); let kind_ink = match clip.kind { highlight::Kind::Death => p.danger, highlight::Kind::Assist => p.muted(), highlight::Kind::MultiKill | highlight::Kind::TradeKill => p.accent, highlight::Kind::Kill => p.success, }; let mut actions = row().key("actions").gap(6.0).center_y().child(ui::button( p, &format!("play-{index}"), "Play", false, move |app: &mut App| { app.review.seek(start); if let Some(player) = &app.review.player { player.play(); } }, )); if video.is_some() { actions = actions.child(ui::button( p, &format!("export-{index}"), "Export", false, move |app: &mut App| { let (tx, rx) = channel(); let Some(video) = video.clone() else { return }; let name = name.clone(); std::thread::spawn(move || { let result = crate::export::export_clip(&video, start, end, &name, true, "high"); let _ = tx.send(result); }); app.review.export = Some(rx); app.review.export_note = Some("Exporting clip…".into()); }, )); } list = list.child( row() .key(format!("highlight-{index}")) .w_full() .h(44.0) .px(space::S3) .center_y() .justify_between() .radius(radius::MD) .hover(p.t.hover) .on_press({ let start = clip.start; move |app: &mut App| app.review.seek(start) }) .child( col() .key("labels") .gap(1.0) .child( row() .key("title-row") .gap(6.0) .center_y() .child(ui::dot(kind_ink)) .child( text(clip.title.clone()) .key("title") .font(type_::LABEL) .color(p.t.text), ) .child( text(format!( "{} to {}", format::clock(start), format::clock(end) )) .key("range") .font(type_::MONO_CAPTION) .color(p.muted()), ), ) .child( text(clip.subtitle.clone()) .key("subtitle") .font(body(11.0)) .color(p.muted()), ), ) .child(actions), ); } col() .key("highlights-panel") .w_full() .grow(1.0) .min_h(0.0) .gap(space::S2) .child( text("Highlights") .key("header") .font(type_::SECTION) .color(p.muted()), ) .child( app.highlights_scroll.follow( "highlight-scroller", col() .w_full() .grow(1.0) .min_h(0.0) .scroll(rune::ScrollSpec { stick_bottom: false, }) .child(list.pr(14.0)), ), ) } fn events(_app: &App, p: Paint, game: &Timeline) -> El { let interesting = |kind: &str| { matches!( kind, "kill" | "death" | "objective" | "game_start" | "game_end" | "lp_change" ) }; let mut list = col().key("event-rows").w_full().gap(1.0); let mut shown = 0; for event in game.events.iter().filter(|e| interesting(&e.event_type)) { let at = event.time(); let label = match event.event_type.as_str() { "kill" => "Kill", "death" => "Death", "objective" => "Objective", "game_start" => "Game start", "game_end" => "Game end", "lp_change" => "LP change", other => other, }; let detail = if event.description.is_empty() { label.to_string() } else { event.description.clone() }; list = list.child( row() .key(format!("event-{shown}")) .w_full() .h(30.0) .px(space::S2) .center_y() .gap(space::S2) .radius(radius::SM) .hover(p.t.hover) .on_press(move |app: &mut App| app.review.seek(at)) .child( text(format::clock(at)) .key("time") .font(type_::MONO_CAPTION) .color(p.muted()), ) .child( text(detail) .key("detail") .font(body(11.5)) .color(p.t.text) .grow(1.0) .shrink() .truncate(), ), ); shown += 1; } if shown == 0 { list = list.child( text("No timeline events.") .key("no-events") .font(body(12.0)) .color(p.muted()), ); } col() .key("events-panel") .w_full() .grow(1.5) .min_h(0.0) .gap(space::S2) .child( text("Timeline") .key("header") .font(type_::SECTION) .color(p.muted()), ) .child( _app.events_scroll.follow( "event-scroller", col() .w_full() .grow(1.0) .min_h(0.0) .scroll(rune::ScrollSpec { stick_bottom: false, }) .child(list.pr(14.0)), ), ) }