//! Clips: every clip the user has exported, newest first, in a grid of cards. //! //! A card is the clip file: its pictures as the main body once they load, and under it the //! clip's name and, when the recording is in the loaded history, the champion it stars and //! the game it came from. Share and the actions menu sit beside. use rune::tok::{radius, space}; use rune::{Color, El, col, el, row, text}; use crate::app::App; use crate::export::Clip; use crate::format; use crate::icons; use crate::ui::{self, Paint, body, heading}; use rune_kit::{Kit, Menu, MenuItem, Placement}; /// Cards across, at the window's current width. Reflowing at other widths comes later. const COLS: usize = 3; pub fn view(app: &App, p: Paint, layer: &mut Vec) -> El { let kit = rune_kit::Kit::neutral(app.dark); // The scroll bar gets its own strip at the end, clear of the cards. let mut grid = col().key("clips-grid").w_full().gap(space::S3).pr(14.0); if app.clips.is_empty() { grid = grid.child(empty( p, "No clips yet", "Export a clip from a recording's page and it lands here.", )); } else { for (row_index, row_clips) in app.clips.chunks(COLS).enumerate() { let mut cards = row() .key(format!("clips-row-{row_index}")) .w_full() .gap(space::S3) .items_start(); for (cell, clip) in row_clips.iter().enumerate() { let index = row_index * COLS + cell; cards = cards.child(clip_card(app, p, &kit, layer, clip, index)); } // Empty cells keep the row's cards at their column width. for cell in row_clips.len()..COLS { cards = cards.child(el().key(format!("filler-{row_index}-{cell}")).grow(1.0)); } grid = grid.child(cards); } } col() .key("clips") .w_full() .h_full() .padding(space::S6) .gap(space::S3) .child( col() .key("clips-title") .gap(2.0) .child( text("Clips") .key("title") .font(heading(20.0)) .color(p.t.text), ) .child( text(count_label(app)) .key("count") .font(body(11.5)) .color(p.muted()), ), ) .child( app.clips_scroll.follow( "clips-scroller", col() .w_full() .grow(1.0) .min_h(0.0) .scroll(rune::ScrollSpec { stick_bottom: false, }) .child(grid), ), ) } /// How many clips the grid holds and how much disk they take. fn count_label(app: &App) -> String { if app.clips.is_empty() { return "No clips".to_string(); } let bytes = app.clips.iter().map(|clip| clip.bytes).sum::(); format!("{} clips · {}", app.clips.len(), format::bytes(bytes)) } fn empty(p: Paint, title: &str, detail: &str) -> El { col() .key("empty") .w_full() .py(64.0) .gap(6.0) .center() .child( text(title) .key("empty-title") .font(heading(15.0)) .color(p.t.text), ) .child( text(detail) .key("empty-detail") .font(body(12.0)) .color(p.muted()), ) } /// What a card says under its stage: its position, the title, the line under it, and the /// champion it stars when its recording is in the loaded history. struct Card { index: usize, title: String, detail: String, champion: Option, } 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. fn of(app: &App, clip: &Clip, index: usize) -> Card { let game = app .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()); match game { Some(game) => Card { index, title: game.champion().unwrap_or_else(|| clip.name.clone()), detail: format!("{} · {}", game.queue_label(), when), champion: game.champion(), }, None => Card { index, title: clip.name.clone(), detail: format!("{} · {}", format::bytes(clip.bytes), when), champion: None, }, } } } fn clip_card(app: &App, p: Paint, k: &Kit, layer: &mut Vec, clip: &Clip, index: usize) -> El { let card = Card::of(app, clip, index); el().key(format!("clip-{index}")) .role("button") .label(format!("Clip {}", clip.name)) .grow(1.0) .min_w(0.0) .radius(radius::LG) .clip_rounded(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) .child( col() .key("card") .w_full() .child(stage(app, p, clip)) .child(footer(app, p, k, layer, clip, &card)), ) } /// The clip itself: the 16:9 box the card gives. The miniature the thumbnail pass cuts fills /// it once it has landed, and the play mark rides over it either way. fn stage(app: &App, p: Paint, clip: &Clip) -> El { let mut stage = col() .key("stage") .aspect_ratio(16.0 / 9.0) .w_full() .fill(Color::BLACK); if let (Some(path), Some(thumbs)) = (&clip.thumb, &app.thumbs) { let image = thumbs.file(path, 360.0, p.scale); stage = stage.child( el().key("thumb") .absolute(0.0, 0.0) .w_full() .h_full() .a11y_hidden() .pixels(image.pixels()), ); } stage.child( el().key("play-overlay") .absolute(0.0, 0.0) .w_full() .h_full() .center() .a11y_hidden() .child( el().key("play") .size(40.0, 40.0) .radius(20.0) .center() .fill(Color::WHITE.alpha(0.16)) .child( rune::icon(&icons::PLAY, 18.0) .color(Color::WHITE.alpha(0.9)) .a11y_hidden(), ), ), ) } /// 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 Card { index, title, detail, champion, } = card; let (index, title, champion) = (*index, title.as_str(), champion.as_deref()); row() .key("footer") .w_full() .px(space::S3) .py(space::S2) .gap(space::S2) .center_y() .child(ui::champion_portrait(app, p, champion, 32.0)) .child( col() .key("labels") .grow(1.0) .min_w(0.0) .gap(1.0) .child( text(title) .key("title") .font(heading(13.0)) .color(p.t.text) .shrink() .truncate(), ) .child( text(detail) .key("game") .font(body(11.0)) .color(p.muted()) .shrink() .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| {}, )) .child(clip_menu(app, k, layer, p, clip, index)) } /// The dots at the end of a card: the menu of actions a clip offers, `Delete` for now. The /// menu is drawn over this window, so it stays inside it. Keyed by the card's position, like /// the history rows, so rows stay distinct. fn clip_menu(app: &App, k: &Kit, layer: &mut Vec, p: Paint, clip: &Clip, index: usize) -> El { let name = clip.name.clone(); Menu::new(format!("clips-menu-{index}")) .trigger(ui::menu_button( p, format!("clips-menu-{index}"), &icons::DOTS, "Clip options", move |app: &mut App| app.clips_menu_open = Some(index), )) .placement(Placement::BottomEnd) .open(app.clips_menu_open == Some(index)) .on_close(|app: &mut App| app.clips_menu_open = None) .item( MenuItem::new("Delete") .danger(true) .on_select(move |app: &mut App| { app.clips_menu_open = None; app.confirm_clip_delete = Some(name.clone()); }), ) .build(k, layer) } /// The confirmation the menu's Delete opens: the clip leaves the grid, and the recording and /// its files stay. `app::view` draws it over the whole window, title bar included. pub fn delete_dialog(app: &App, p: Paint, layer: &mut Vec) { let panel = col() .key("clip-delete-dialog") .role("dialog") .label("Delete this clip") .w(380.0) .padding(space::S6) .gap(space::S3) .radius(radius::LG) .fill(p.t.card) .border(p.hair, p.t.card_border) .shadow(4.0, 12.0, p.t.shadow) .shadow(24.0, 48.0, p.t.shadow_soft) .modal(true, |app: &mut App| app.confirm_clip_delete = None) .child( text("Delete this clip?") .key("delete-question") .font(heading(15.0)) .color(p.t.text), ) .child( text(format!( "\"{}.mp4\" is deleted from disk. The recording stays.", delete_name(app) )) .key("delete-note") .font(body(12.0)) .color(p.muted()), ) .child( row() .key("delete-actions") .w_full() .gap(space::S2) .justify_end() .child(ui::button(p, "delete-no", "No", false, |app: &mut App| { app.confirm_clip_delete = None; })) .child(ui::button_danger( p, "delete-yes", "Delete", |app: &mut App| { delete_confirmed(app); }, )), ); layer.push( el().key("delete-scrim") .absolute(0.0, 0.0) .w_full() .h_full() .fill(Color::BLACK.alpha(if app.dark { 0.55 } else { 0.28 })) .a11y_hidden(), ); layer.push( el().key("delete-layer") .absolute(0.0, 0.0) .w_full() .h_full() .padding(16.0) .center() .child(panel), ); } /// Yes on the dialog: the file goes, and the card with it. The recording keeps its files. fn delete_confirmed(app: &mut App) { let Some(name) = app.confirm_clip_delete.take() else { return; }; let Some(index) = app.clips.iter().position(|clip| clip.name == name) else { return; }; let clip = app.clips.remove(index); let _ = std::fs::remove_file(crate::export::thumb_path(&clip)); match std::fs::remove_file(&clip.path) { Ok(()) => app.notify("Clip deleted"), Err(error) => { app.notify(format!("Could not delete the clip: {error}")); app.clips.insert(index, clip); } } } /// The clip the delete dialog is about, for the dialog's wording. fn delete_name(app: &App) -> String { app.confirm_clip_delete .as_ref() .and_then(|name| app.clips.iter().find(|clip| &clip.name == name)) .map(|clip| clip.name.clone()) .unwrap_or_default() }