diff --git a/app/README.md b/app/README.md index 149206b..ab3bd33 100644 --- a/app/README.md +++ b/app/README.md @@ -27,6 +27,7 @@ cargo run -- --snap recording.png --scene recording cargo run -- --snap audio.png --scene audio cargo run -- --snap review.png --scene review cargo run -- --snap clips.png --scene clips +cargo run -- --snap clip.png --scene clip cargo run -- --snap clips-many.png --scene clips-many --at 1 # 36 demo clips, to see the scrollbar cargo run -- --snap many.png --scene many --at 1 # 60 sample games, to see the scrollbar ``` diff --git a/app/src/app.rs b/app/src/app.rs index 1977246..e84a5d7 100644 --- a/app/src/app.rs +++ b/app/src/app.rs @@ -22,6 +22,7 @@ pub enum Screen { Settings, History, Clips, + Clip, Review, } @@ -142,6 +143,10 @@ pub struct App { /// Every clip the user exported, newest first. Read when the screen opens. pub clips: Vec, pub clips_loaded: bool, + /// The open clip: its view, with the title being set and its player. + pub clip: Option, + /// A clip waiting for the clipboard: its file. Handled off the handler, in poll. + pub share_clip: Option, /// The clips' miniatures, decoded off the render path and cached by file. pub thumbs: Option, /// A miniature on its way: the clip's name and the file that holds it. @@ -205,6 +210,8 @@ impl App { menu_open: None, clips: Vec::new(), clips_loaded: false, + clip: None, + share_clip: None, thumbs: None, thumbs_rx: None, clips_menu_open: None, @@ -938,9 +945,14 @@ impl App { // Keep the clock running so the status stays live even when nothing draws. tx.wake_after(Duration::from_millis(1000)); + // The clip view plays its file; leaving the screen behind stops it with the view. + if self.screen != Screen::Clip && self.clip.take().is_some() { + changed = true; + } if self.screen == Screen::Review { changed |= screens::review::tick(self, tx); } + changed |= screens::clip::tick(self, tx); changed } } @@ -990,6 +1002,11 @@ pub fn on_key(app: &mut App, event: &KeyEvent, _tx: &mut Tx) -> bool { app.clips_menu_open = None; true } + (Key::Escape, _) if app.screen == Screen::Clip => { + app.menu_open = None; + app.screen = Screen::Clips; + true + } (Key::Escape, _) if app.screen == Screen::Clips => { app.menu_open = None; app.screen = Screen::History; @@ -1080,18 +1097,27 @@ fn titlebar(p: Paint, app: &App) -> El { let mut leading = row().key("leading").gap(8.0).center_y(); if matches!( app.screen, - Screen::Settings | Screen::Clips | Screen::Review + Screen::Settings | Screen::Clips | Screen::Clip | Screen::Review ) { + let from_clip = app.screen == Screen::Clip; leading = leading.child(ui::button( p, "back", - "← Match history", + if from_clip { + "← Clips" + } else { + "← Match history" + }, false, |app: &mut App| { if app.screen == Screen::Review { screens::review::leave(app); } - app.screen = Screen::History; + app.screen = if app.screen == Screen::Clip { + Screen::Clips + } else { + Screen::History + }; }, )); } @@ -1105,7 +1131,7 @@ fn titlebar(p: Paint, app: &App) -> El { // On the settings screen the title bar has nothing to offer: refreshing and opening // settings again would only repeat what the screen already does. if app.screen != Screen::Settings { - if app.screen != Screen::Clips { + if !matches!(app.screen, Screen::Clips | Screen::Clip) { actions = actions.child(ui::button( p, "open-clips", @@ -1201,6 +1227,7 @@ pub fn view(app: &App, cx: &mut Cx) -> El { Screen::Settings => screens::settings::view(app, p), Screen::History => screens::history::view(app, p, &mut overlays), Screen::Clips => screens::clips::view(app, p, &mut overlays), + Screen::Clip => screens::clip::view(app, p), Screen::Review => screens::review::view(app, p), }; let mut main = col().key("main").grow(1.0).min_h(0.0).w_full().fill(p.t.bg); diff --git a/app/src/format.rs b/app/src/format.rs index fb4f9a0..fb391ae 100644 --- a/app/src/format.rs +++ b/app/src/format.rs @@ -12,6 +12,13 @@ pub fn clock(secs: f64) -> String { format!("{}:{:02}", total / 60, total % 60) } +/// How long ago an exported file's stamp was: `2 hours ago`. A stamp that could not be read +/// reads `earlier`. +pub fn age(at: Option) -> String { + at.map(|at| relative_time(at.into())) + .unwrap_or_else(|| "earlier".to_string()) +} + pub fn relative_time(when: DateTime) -> String { let now = Utc::now(); let delta = now.signed_duration_since(when); diff --git a/app/src/main.rs b/app/src/main.rs index 49814f8..7251737 100644 --- a/app/src/main.rs +++ b/app/src/main.rs @@ -120,6 +120,16 @@ fn main() { app.clips_menu_open = None; app.spawn_thumbs(); }), + ("clip", |app| { + // Demo clips come with the app; live ones need the folder listed first. + if app.clips.is_empty() { + app.refresh_clips(); + } + app.spawn_thumbs(); + if let Some(first) = app.clips.first().cloned() { + screens::clip::open(app, &first.name); + } + }), ("clips-many", |app| { app.screen = app::Screen::Clips; app.spawn_thumbs(); diff --git a/app/src/screens/clip.rs b/app/src/screens/clip.rs new file mode 100644 index 0000000..c1a1bdc --- /dev/null +++ b/app/src/screens/clip.rs @@ -0,0 +1,428 @@ +//! The clip view: one clip playing, its title being set, and its share. +//! +//! 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 rune::tok::{radius, space, type_}; +use rune::{ + ClipboardContent, Color, El, Player, PlayerState, SurfaceFit, TextEdit, Tx, col, el, row, text, + text_input, +}; + +use crate::app::{App, Screen}; +use crate::export::{self, Clip}; +use crate::format; +use crate::icons; +use crate::ui::{self, Paint, body, heading}; + +/// The open clip: which file it is, the title being edited for it, and its player. +pub struct ClipView { + /// The clip's name in the grid: its file stem, and its title until renamed. + pub name: String, + /// The title as it is being typed. Saving turns it into the file's name. + pub title: TextEdit, + pub player: Option, + /// Shown over the stage when the file refuses to play. + pub error: Option, + /// Whether the clip plays without sound; a clip opens muted. + pub muted: bool, +} + +/// Open a clip by name, from the grid's list. The screen switches here and the clip plays, +/// muted and looping, until the view is left. +pub fn open(app: &mut App, name: &str) { + if !app.clips.iter().any(|clip| clip.name == name) { + app.refresh_clips(); + } + let Some(clip) = app.clips.iter().find(|clip| clip.name == name).cloned() else { + app.notify_error(format!("No clip named {name} in the clips folder")); + return; + }; + // Demo clips name no file until their stand-in video is synthesized; real clips need + // only the file to be there. + let Some(video) = export::source_of(&clip) else { + app.notify_error("The clip's video is missing from the clips folder"); + return; + }; + let player = Player::file(&video); + player.set_looping(true); + player.set_muted(true); + if let Some(waker) = &app.waker { + player.set_waker(waker.clone()); + } + player.play(); + app.clip = Some(ClipView { + name: clip.name.clone(), + title: TextEdit::new(clip.name.clone()), + player: Some(player), + error: None, + muted: true, + }); + app.screen = Screen::Clip; +} + +/// Hand a pending share to the clipboard worker, and say so. +pub fn tick(app: &mut App, tx: &mut Tx) -> bool { + let Some(path) = app.share_clip.take() else { + return false; + }; + tx.copy_rich(ClipboardContent::default().with_files([path])); + app.notify("Clip copied. Paste it anywhere."); + true +} + +/// Ask for a clip's file on the clipboard. The worker gets it in `tick`, off the handler. +pub fn request_share(app: &mut App, name: &str) { + let Some(clip) = app.clips.iter().find(|clip| clip.name == name) else { + return; + }; + if clip.path.as_os_str().is_empty() { + app.notify_error("This demo clip has no file to share"); + return; + } + app.share_clip = Some(clip.path.clone()); +} + +/// Turn the edited title into the file's name. The grid, the miniature and the view follow. +pub fn save_title(app: &mut App) { + let Some(view) = app.clip.as_ref() else { + return; + }; + let wanted = title_name(&view.title.text); + let current = view.name.clone(); + if wanted.is_empty() { + app.notify_error("Give the clip a title first"); + return; + } + if wanted == current { + return; + } + let Some(index) = app.clips.iter().position(|clip| clip.name == current) else { + return; + }; + let clip = app.clips[index].clone(); + if clip.path.as_os_str().is_empty() { + app.notify_error("This demo clip has no file to rename"); + return; + } + let to = export::clips_dir().join(format!("{}.mp4", wanted)); + if to.exists() { + app.notify_error("A clip with that name exists already"); + return; + } + if let Err(error) = std::fs::rename(&clip.path, &to) { + app.notify_error(format!("Could not rename the clip: {error}")); + return; + } + // The miniature follows the file; without one the thumbnail pass cuts it fresh. + let renamed = Clip { + name: wanted.clone(), + ..clip.clone() + }; + let (from_thumb, to_thumb) = (export::thumb_path(&clip), export::thumb_path(&renamed)); + let _ = std::fs::rename(from_thumb, &to_thumb); + let entry = &mut app.clips[index]; + entry.name = wanted.clone(); + entry.path = to; + entry.thumb = to_thumb.exists().then_some(to_thumb); + let view = app.clip.as_mut().expect("checked above"); + view.name = wanted; + view.title = TextEdit::new(view.name.clone()); + app.notify("Clip renamed"); + app.spawn_thumbs(); +} + +/// 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 { + let cleaned: String = title + .chars() + .map(|c| match c { + '<' | '>' | ':' | '"' | '/' | '\\' | '|' | '?' | '*' => '-', + c if c.is_control() => '-', + c => c, + }) + .collect(); + let short: String = cleaned.chars().take(80).collect(); + short.trim_matches(|c| c == ' ' || c == '.').to_string() +} + +pub fn view(app: &App, p: Paint) -> El { + let Some(view) = &app.clip else { + return col().key("clip-view").padding(space::S6).child( + text("No clip open.") + .key("clip-none") + .font(body(13.0)) + .color(p.muted()), + ); + }; + 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 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 player takes the window; the side panel holds the title, the share and the facts. + let body = row() + .key("clip-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( + col() + .key("side") + .w(300.0) + .gap(space::S3) + .items_start() + .child( + col() + .key("who") + .w_full() + .gap(1.0) + .child( + text(title) + .key("clip-title") + .font(heading(15.0)) + .color(p.t.text) + .shrink() + .truncate(), + ) + .child( + text(detail) + .key("clip-detail") + .font(body(11.0)) + .color(p.muted()) + .shrink() + .truncate(), + ), + ) + .child( + text("Title") + .key("title-label") + .font(type_::LABEL) + .color(p.muted()), + ) + .child( + text_input(&view.title, type_::BODY) + .key("title-field") + .single_line() + .placeholder("Clip title", p.muted()) + .on_edit(|app: &mut App| { + &mut app + .clip + .as_mut() + .expect("the field lives on the open clip") + .title + }) + .on_submit(|app: &mut App, _tx: &mut Tx| save_title(app)), + ) + .child( + row() + .key("side-actions") + .gap(space::S2) + .child(ui::button( + p, + "save-title", + "Save title", + false, + |app: &mut App| { + save_title(app); + }, + )) + .child(ui::button(p, "share", "Share", true, |app: &mut App| { + let name = app + .clip + .as_ref() + .map(|view| view.name.clone()) + .unwrap_or_default(); + request_share(app, &name); + })), + ), + ); + col() + .key("clip-view") + .w_full() + .grow(1.0) + .min_h(0.0) + .padding(space::S6) + .child(body) +} + +/// The stage: the clip's video on black, 16 points of nine everywhere it fits. +fn video_stage(app: &App, p: Paint) -> El { + let Some(view) = &app.clip else { + return col().key("stage").w_full().grow(1.0).min_h(0.0); + }; + let stage = col() + .key("stage") + .w_full() + .grow(1.0) + .min_h(0.0) + .fill(Color::BLACK) + .radius(radius::LG) + .clip_soft(radius::LG) + .center(); + match &view.player { + Some(player) => { + if let PlayerState::Failed(message) = player.state() { + stage.child( + text(format!("The clip does not play: {message}")) + .key("stage-error") + .font(body(13.0)) + .color(p.muted()), + ) + } else { + stage.child( + el().key("video") + .aspect_fit(16.0 / 9.0) + .video(player, SurfaceFit::Fill), + ) + } + } + None => stage.child( + text(view.error.clone().unwrap_or_else(|| "No clip open.".into())) + .key("stage-message") + .font(body(13.0)) + .color(p.muted()), + ), + } +} + +/// Play and pause, a couple of seconds either way, and the sound. +fn transport(app: &App, p: Paint) -> El { + let playing = app + .clip + .as_ref() + .and_then(|view| view.player.as_ref()) + .is_some_and(Player::is_playing); + let at = app + .clip + .as_ref() + .and_then(|view| view.player.as_ref()) + .map(Player::time) + .unwrap_or(0.0); + let long = app + .clip + .as_ref() + .and_then(|view| view.player.as_ref()) + .and_then(Player::duration) + .unwrap_or(0.0); + let muted = app.clip.as_ref().is_some_and(|view| view.muted); + row() + .key("transport") + .w_full() + .center_y() + .gap(space::S2) + .child(ui::media_button( + p, + "rewind", + &icons::BACK, + "Back 2 seconds", + false, + |app: &mut App| seek(app, -2.0), + )) + .child(ui::media_button( + p, + "play-pause", + if playing { &icons::PAUSE } else { &icons::PLAY }, + if playing { "Pause" } else { "Play" }, + true, + |app: &mut App| { + if let Some(player) = app.clip.as_ref().and_then(|view| view.player.as_ref()) { + if player.is_playing() { + player.pause(); + } else { + player.play(); + } + } + }, + )) + .child(ui::media_button( + p, + "advance", + &icons::FORWARD, + "Forward 2 seconds", + false, + |app: &mut App| seek(app, 2.0), + )) + .child( + text(format!("{} / {}", format::clock(at), format::clock(long))) + .key("clock") + .font(type_::MONO_CAPTION) + .color(p.muted()), + ) + .child(el().grow(1.0)) + .child(ui::button( + p, + "mute", + if muted { "Unmute" } else { "Mute" }, + false, + |app: &mut App| { + if let Some(view) = &mut app.clip { + view.muted = !view.muted; + if let Some(player) = &view.player { + player.set_muted(view.muted); + } + } + }, + )) +} + +/// Move the clip's player by `seconds`, held to the clip's length. +fn seek(app: &mut App, seconds: f64) { + if let Some(player) = app.clip.as_ref().and_then(|view| view.player.as_ref()) { + let long = player.duration().unwrap_or(0.0); + player.seek((player.time() + seconds).clamp(0.0, long)); + } +} + +#[cfg(test)] +mod tests { + use super::title_name; + + #[test] + fn a_title_keeps_its_letters_and_spaces() { + assert_eq!(title_name("My Ace!"), "My Ace!"); + } + + #[test] + fn characters_a_file_cannot_carry_become_dashes() { + assert_eq!(title_name("Kai'Sa: pentakill?"), "Kai'Sa- pentakill-"); + } + + #[test] + fn edges_lose_their_stray_space_and_dots() { + assert_eq!(title_name(" .clip. "), "clip"); + } + + #[test] + fn a_long_title_stays_short_enough_to_show() { + assert_eq!(title_name(&"x".repeat(90)).len(), 80); + } +} diff --git a/app/src/screens/clips.rs b/app/src/screens/clips.rs index 6df79b9..5bd8b33 100644 --- a/app/src/screens/clips.rs +++ b/app/src/screens/clips.rs @@ -134,10 +134,7 @@ impl Card { .games .iter() .find(|game| clip.name.contains(&game.recording_id)); - let when = clip - .exported - .map(|at| format::relative_time(at.into())) - .unwrap_or_else(|| "earlier".to_string()); + let when = format::age(clip.exported); match game { Some(game) => Card { index, @@ -157,9 +154,11 @@ impl Card { fn clip_card(app: &App, p: Paint, k: &Kit, layer: &mut Vec, clip: &Clip, index: usize) -> El { let card = Card::of(app, clip, index); + let name = clip.name.clone(); el().key(format!("clip-{index}")) .role("button") .label(format!("Clip {}", clip.name)) + .on_press(move |app: &mut App| crate::screens::clip::open(app, &name)) .grow(1.0) .min_w(0.0) .radius(radius::LG) @@ -202,6 +201,7 @@ fn stage(app: &App, p: Paint, clip: &Clip) -> El { /// Who the clip stars and what game it came from, with share and the actions menu beside. fn footer(app: &App, p: Paint, k: &Kit, layer: &mut Vec, clip: &Clip, card: &Card) -> El { + let share_name = clip.name.clone(); let Card { index, title, @@ -240,13 +240,12 @@ fn footer(app: &App, p: Paint, k: &Kit, layer: &mut Vec, clip: &Clip, card: .truncate(), ), ) - // Share has nothing behind it yet; the layout holds its place. .child(ui::menu_button( p, format!("share-{index}"), &icons::SHARE, "Share clip", - |_app: &mut App| {}, + move |app: &mut App| crate::screens::clip::request_share(app, &share_name), )) .child(clip_menu(app, k, layer, p, clip, index)) } diff --git a/app/src/screens/mod.rs b/app/src/screens/mod.rs index 89334ce..15dbd84 100644 --- a/app/src/screens/mod.rs +++ b/app/src/screens/mod.rs @@ -1,5 +1,6 @@ -//! The four screens: settings, match history, the clips grid and the game review. +//! The screens: settings, match history, the clips grid, one open clip and the game review. +pub mod clip; pub mod clips; pub mod history; pub mod review; diff --git a/app/src/screens/review.rs b/app/src/screens/review.rs index c0d207b..4ae5fbc 100644 --- a/app/src/screens/review.rs +++ b/app/src/screens/review.rs @@ -557,9 +557,14 @@ pub fn tick(app: &mut App, tx: &mut Tx) -> bool { app.review.export_note = Some(format!("Clip saved to {path}")); app.review.export = None; changed = true; - // The clips grid lists the folder this clip just joined. - if app.screen == crate::app::Screen::Clips { + // The exported clip is what the user was after: open its view, with the + // folder listing refreshed so the view finds it. + if let Some(name) = std::path::Path::new(&path) + .file_stem() + .and_then(|stem| stem.to_str()) + { app.refresh_clips(); + crate::screens::clip::open(app, name); } } Ok(Err(e)) => {