Files
leaguerecorder/app/src/screens/review.rs
T
vhaudiquet 17c73925bf app: draw the whole timeline under the review scrubber
Every event in the recording gets a tick: kills and deaths coloured by
whether they were the player's, objectives and LP changes in the accent,
game start and end muted. Highlights sit on the track as bands.

The summoner carries a #TAG that the live client data's KillerName does
not, so involvement never matched and every game showed 0 highlights.
Names are compared without the tag now.
2026-11-15 23:34:23 +01:00

667 lines
21 KiB
Rust

//! 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<String>,
pub player: Option<Player>,
pub time: f64,
pub duration: f64,
pub scrubber_width: f32,
pub highlights: Vec<Highlight>,
pub export: Option<Receiver<Result<String, String>>>,
pub export_note: Option<String>,
pub error: Option<String>,
}
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(
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()),
);
col()
.key("review")
.w_full()
.h_full()
.padding(space::S6)
.gap(space::S3)
.child(header)
.child(video_stage(app, p))
.child(scrubber(app, p, game))
.child(
row()
.key("review-body")
.w_full()
.grow(1.0)
.min_h(0.0)
.gap(space::S3)
.child(highlights(app, p, game))
.child(events(app, p, game)),
)
}
fn video_stage(app: &App, p: Paint) -> El {
let mut stage = el()
.key("stage")
.w_full()
.grow(1.0)
.min_h(0.0)
.radius(radius::LG)
.fill(rune::Color::BLACK)
.clip_soft(radius::LG);
if let Some(player) = &app.review.player {
stage = stage.child(
el().key("video")
.w_full()
.h_full()
.video(player, SurfaceFit::Fit),
);
} else {
stage = stage.child(
col()
.key("stage-empty")
.w_full()
.h_full()
.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()),
),
);
}
col()
.key("stage-column")
.w_full()
.grow(1.0)
.min_h(0.0)
.gap(space::S2)
.child(stage)
.child(
row()
.key("transport")
.w_full()
.center_y()
.gap(space::S3)
.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))
.child(
text(
app.review.export_note.clone().unwrap_or_else(|| {
format!("{} highlights", app.review.highlights.len())
}),
)
.key("export-note")
.font(body(11.5))
.color(p.muted()),
),
)
}
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(360.0)
.h_full()
.gap(space::S2)
.child(
text("Highlights")
.key("header")
.font(type_::SECTION)
.color(p.muted()),
)
.child(
app.highlights_scroll.follow(
col()
.key("highlight-scroller")
.w_full()
.grow(1.0)
.min_h(0.0)
.scroll(rune::ScrollSpec {
stick_bottom: false,
})
.child(list),
),
)
}
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")
.grow(1.0)
.min_w(240.0)
.h_full()
.gap(space::S2)
.child(
text("Timeline")
.key("header")
.font(type_::SECTION)
.color(p.muted()),
)
.child(
app.events_scroll.follow(
col()
.key("event-scroller")
.w_full()
.grow(1.0)
.min_h(0.0)
.scroll(rune::ScrollSpec {
stick_bottom: false,
})
.child(list),
),
)
}