diff --git a/app/src/icons.rs b/app/src/icons.rs index 63ef3ef..bf7de07 100644 --- a/app/src/icons.rs +++ b/app/src/icons.rs @@ -50,6 +50,20 @@ pub static SHARE: Icon = Icon { body: r#""#, }; +/// A speaker with sound waves: the clip plays its sound. +pub static VOLUME: Icon = Icon { + name: "app-volume", + view: 16.0, + body: r#""#, +}; + +/// The same speaker crossed out: the clip is muted. +pub static VOLUME_OFF: Icon = Icon { + name: "app-volume-off", + view: 16.0, + body: r#""#, +}; + /// Three vertical dots: the button that opens a card's menu of actions. pub static DOTS: Icon = Icon { name: "app-dots", diff --git a/app/src/screens/clip.rs b/app/src/screens/clip.rs index 6ce6ffa..b3faa47 100644 --- a/app/src/screens/clip.rs +++ b/app/src/screens/clip.rs @@ -3,6 +3,8 @@ //! 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 std::time::Duration; + use rune::tok::{radius, space, type_}; use rune::{ ClipboardContent, Color, El, Player, PlayerState, SurfaceFit, TextEdit, Tx, auto_grow, col, el, @@ -24,12 +26,17 @@ pub struct ClipView { 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. + /// Whether the clip plays without sound. A clip opens with its sound on. pub muted: bool, + /// The time bar width in points, from its layout, so a drag can read the pointer. + pub bar_width: f32, + /// Where the player is and how long the clip is, read in `tick` so the bar and clock move. + pub at: f64, + pub long: f64, } /// 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. +/// with sound 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(); @@ -46,7 +53,7 @@ pub fn open(app: &mut App, name: &str) { }; let player = Player::file(&video); player.set_looping(true); - player.set_muted(true); + player.set_muted(false); if let Some(waker) = &app.waker { player.set_waker(waker.clone()); } @@ -56,15 +63,43 @@ pub fn open(app: &mut App, name: &str) { title: TextEdit::new(clip.name.clone()), player: Some(player), error: None, - muted: true, + muted: false, + bar_width: 0.0, + at: 0.0, + long: 0.0, }); app.screen = Screen::Clip; } -/// Hand a pending share to the clipboard worker, and say so. +/// 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 { + let mut changed = false; + if let Some(view) = &mut app.clip { + let state = view + .player + .as_ref() + .map(|player| (player.time(), player.duration(), player.is_playing())); + if let Some((at, long, playing)) = state { + if (at - view.at).abs() > 0.001 { + view.at = at; + changed = true; + } + if let Some(long) = long + && long > 0.0 + && (long - view.long).abs() > 0.05 + { + view.long = long; + changed = true; + } + // The picture only moves when the window draws it, so keep drawing while it plays. + if playing { + tx.wake_after(Duration::from_millis(33)); + } + } + } let Some(path) = app.share_clip.take() else { - return false; + return changed; }; tx.copy_rich(ClipboardContent::default().with_files([path])); app.notify("Clip copied. Paste it anywhere."); @@ -190,9 +225,7 @@ pub fn view(app: &App, p: Paint) -> El { .grow(1.0) .min_w(0.0) .min_h(0.0) - .gap(space::S2) - .child(video_stage(app, p)) - .child(transport(app, p)), + .child(video_stage(app, p)), ) .child( col() @@ -289,11 +322,9 @@ pub fn view(app: &App, p: Paint) -> El { .child(body) } -/// The stage: the clip's video on black, 16 points of nine everywhere it fits. +/// 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 { - 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() @@ -302,118 +333,248 @@ fn video_stage(app: &App, p: Paint) -> El { .fill(Color::BLACK) .radius(radius::LG) .clip_soft(radius::LG) + .hover_group() .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( + let Some(view) = &app.clip else { + return stage; + }; + let Some(player) = &view.player else { + return stage.child( text(view.error.clone().unwrap_or_else(|| "No clip open.".into())) .key("stage-message") .font(body(13.0)) .color(p.muted()), - ), + ); + }; + if let PlayerState::Failed(message) = player.state() { + return stage.child( + text(format!("The clip does not play: {message}")) + .key("stage-error") + .font(body(13.0)) + .color(p.muted()), + ); } + stage.child( + el().key("player-frame") + .aspect_fit(16.0 / 9.0) + .radius(radius::LG) + .clip_soft(radius::LG) + .fill(Color::BLACK) + .child( + el().key("video") + .w_full() + .h_full() + .video(player, SurfaceFit::Fill), + ) + .child(controls(app, p, player)), + ) } -/// 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); +/// The controls over the bottom of the picture: the time bar, play and pause, a couple of +/// seconds either way, and the sound. They rest visible while the clip stands still and hide +/// while it plays, showing again when the pointer comes over the player. +fn controls(app: &App, p: Paint, player: &Player) -> El { + let playing = player.is_playing(); let muted = app.clip.as_ref().is_some_and(|view| view.muted); - row() - .key("transport") - .w_full() - .center_y() + let at = app.clip.as_ref().map_or(0.0, |view| view.at); + let long = app.clip.as_ref().map_or(0.0, |view| view.long); + col() + .key("controls") + .pin(Some(0.0), Some(0.0), Some(0.0), Some(0.0)) + .justify_end() .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), - )) + .px(16.0) + .pb(space::S3) + .gradient(Color::TRANSPARENT, Color::BLACK.alpha(0.68)) + .group_opacity(if playing { 0.0 } else { 1.0 }, 1.0) + .child(time_bar(app, p, at, long)) .child( - text(format!("{} / {}", format::clock(at), format::clock(long))) - .key("clock") - .font(type_::MONO_CAPTION) - .color(p.muted()), + row() + .key("transport") + .w_full() + .center_y() + .gap(space::S2) + .child(overlay_button( + p, + "rewind", + &icons::BACK, + "Back 2 seconds", + false, + |app: &mut App| seek(app, -2.0), + )) + .child(overlay_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(overlay_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(Color::WHITE.alpha(0.9)), + ) + .child(el().grow(1.0)) + .child(overlay_button( + p, + "mute", + if muted { + &icons::VOLUME_OFF + } else { + &icons::VOLUME + }, + 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); + } + } + }, + )), + ) +} + +/// An icon button for the dark picture: the theme's control colours are made for surfaces, +/// and the picture is black in either appearance. +fn overlay_button( + p: Paint, + key: &str, + icon: &'static rune::Icon, + label: &str, + primary: bool, + on_press: impl Fn(&mut App) + Send + Sync + 'static, +) -> El { + let ink = if primary { p.accent_ink } else { Color::WHITE }; + el().key(key) + .role("button") + .label(label) + .focusable(true) + .size(32.0, 32.0) + .center() + .radius(radius::MD) + .fill(if primary { + p.accent + } else { + Color::WHITE.alpha(0.16) + }) + .hover(Color::WHITE.alpha(0.1)) + .press(Color::WHITE.alpha(0.18)) + .on_press(on_press) + .child(rune::icon(icon, 16.0).color(ink).a11y_hidden()) +} + +/// The clip's progress, over the picture. A press or a drag anywhere on it seeks. +fn time_bar(app: &App, p: Paint, at: f64, long: f64) -> El { + // The width the bar was last given; a drag reads the pointer against it. + let width = app.clip.as_ref().map_or(0.0, |view| view.bar_width); + let w = if width > 0.0 { width } else { 600.0 }; + let progress = if long > 0.0 { + (at / long).clamp(0.0, 1.0) as f32 + } else { + 0.0 + }; + let (height, bar, knob) = (18.0, 4.0, 11.0); + el().key("time-bar") + .role("slider") + .label("Playback position") + .range_value(at, 0.0, long.max(0.001)) + .range_step(5.0) + .w_full() + .h(height) + .on_layout(|app: &mut App, [_, _, w, _]| { + if let Some(view) = &mut app.clip { + view.bar_width = w; + } + }) + .on_step(|app: &mut App, steps, _| seek(app, steps as f64 * 5.0)) + .on_drag(|app: &mut App, d: &mut rune::Drag, tx| { + let long = app.clip.as_ref().map_or(0.0, |view| view.long); + let width = app + .clip + .as_ref() + .map_or(0.0, |view| view.bar_width) + .max(1.0); + if long <= 0.0 { + return; + } + let at = (d.x / width).clamp(0.0, 1.0) as f64 * long; + if let Some(player) = app.clip.as_ref().and_then(|view| view.player.as_ref()) { + player.seek(at); + } + if let Some(view) = &mut app.clip { + view.at = at; + } + tx.instant(); + }) + .child( + el().key("track") + .w_full() + .h(bar) + .absolute(0.0, (height - bar) / 2.0) + .radius(bar / 2.0) + .fill(Color::WHITE.alpha(0.22)) + .a11y_hidden(), + ) + .child( + el().key("played") + .w(w * progress) + .h(bar) + .absolute(0.0, (height - bar) / 2.0) + .radius(bar / 2.0) + .fill(p.accent.alpha(0.9)) + .a11y_hidden(), + ) + .child( + el().key("knob") + .size(knob, knob) + .absolute( + (progress * (w - knob)).clamp(0.0, (w - knob).max(1.0)), + (height - knob) / 2.0, + ) + .radius(knob / 2.0) + .fill(Color::WHITE) + .shadow(0.0, 1.0, Color::BLACK.alpha(0.35)) + .a11y_hidden(), ) - .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)); + let at = app + .clip + .as_ref() + .and_then(|view| view.player.as_ref()) + .map(|player| { + let long = player.duration().unwrap_or(0.0); + let at = (player.time() + seconds).clamp(0.0, long); + player.seek(at); + at + }); + if let (Some(at), Some(view)) = (at, app.clip.as_mut()) { + view.at = at; } }