diff --git a/crates/app/src/keymap.rs b/crates/app/src/keymap.rs index ab82413..c2bf8bd 100644 --- a/crates/app/src/keymap.rs +++ b/crates/app/src/keymap.rs @@ -142,6 +142,10 @@ actions! { /// its own, because a card reachable only by a button in the panel is one a /// hand on the keyboard has to leave the keyboard for. ShowActions, + /// Saves the frame on screen to a PNG: whatever is showing, the timeline's + /// own picture or a library preview's, since both write through the one + /// `self.image` the atlas already holds. + Screenshot, } impl ActionId { @@ -203,6 +207,7 @@ impl ActionId { ActionId::Fullscreen => "Fullscreen on / off", ActionId::CancelExport => "Cancel export", ActionId::ShowActions => "All actions and their keys…", + ActionId::Screenshot => "Save the frame on screen as a PNG", } } @@ -262,6 +267,7 @@ impl ActionId { ActionId::Fullscreen => "fullscreen", ActionId::CancelExport => "cancel-export", ActionId::ShowActions => "show-actions", + ActionId::Screenshot => "screenshot", } } @@ -362,7 +368,11 @@ impl ActionId { ActionId::Save | ActionId::Export | ActionId::CancelExport - | ActionId::ShowActions => Category::File, + | ActionId::ShowActions + // It writes a file like the three above it, and touches no edit + // list -- what an export's own allow-list already means it stays + // out of, not what puts it in this heading. + | ActionId::Screenshot => Category::File, } } } @@ -907,6 +917,9 @@ impl Keymap { // (platform.rs:880, the keysym's own name lowercased) -- and // not a letter, so it takes nothing away from the clip keys. b(ActionId::ShowActions, "f1", false), + // "w" is free entirely, bare and ctrl both -- nothing else in + // this table answers to it. + b(ActionId::Screenshot, "w", false), ], } } @@ -1170,8 +1183,9 @@ mod tests { #[test] fn every_default_stroke_reaches_its_action() { let k = Keymap::defaults(); - assert_eq!(k.entries().len(), 54); + assert_eq!(k.entries().len(), 55); assert_eq!(k.lookup("f11", false), Some(ActionId::Fullscreen)); + assert_eq!(k.lookup("w", false), Some(ActionId::Screenshot)); assert_eq!(k.lookup("p", true), Some(ActionId::ToggleProxies)); assert_eq!(k.lookup("o", true), Some(ActionId::ToggleAutoProxies)); assert_eq!(k.lookup("f1", false), Some(ActionId::ShowActions)); diff --git a/crates/app/src/player/actions.rs b/crates/app/src/player/actions.rs index d979b5e..b467af0 100644 --- a/crates/app/src/player/actions.rs +++ b/crates/app/src/player/actions.rs @@ -105,6 +105,7 @@ impl Player { // the key handler is what answers this one while there is. ActionId::CancelExport => {} ActionId::ShowActions => self.show_actions(cx), + ActionId::Screenshot => self.take_screenshot(cx), } } diff --git a/crates/app/src/tests/media.rs b/crates/app/src/tests/media.rs index 8915b8f..8e62333 100644 --- a/crates/app/src/tests/media.rs +++ b/crates/app/src/tests/media.rs @@ -2,6 +2,7 @@ //! command line, the scans, the codecs offered, and the subtitles found. use super::*; +use crate::ui::preview::{bgra_to_rgba, screenshot_path}; /// The import line's own state machine, driven the way a repaint drives /// it: the worker writes a stage, the poll notices it changed and restarts @@ -1158,3 +1159,57 @@ fn a_second_session_playing_never_moves_the_first() { "the preview's play moved the wrong session" ); } + +/// [`bgra_to_rgba`] and [`save_screenshot`]'s own PNG write, round-tripped: a +/// synthetic 2x2 frame -- one pixel per corner, alpha and channel order all +/// distinct -- decodes back to the same pixels a viewer would see, in RGBA. +#[test] +fn a_screenshot_frame_round_trips_through_its_png() { + // BGRA, gpui's own layout: red top-left, green top-right, blue + // bottom-left, and a half-alpha white bottom-right. + #[rustfmt::skip] + let bgra: Vec = vec![ + 0, 0, 255, 255, 0, 255, 0, 255, + 255, 0, 0, 255, 255, 255, 255, 128, + ]; + let rgba = bgra_to_rgba(&bgra); + assert_eq!(rgba[0..4], [255, 0, 0, 255], "red survives the channel swap"); + assert_eq!(rgba[4..8], [0, 255, 0, 255], "green is unmoved by the swap"); + assert_eq!(rgba[8..12], [0, 0, 255, 255], "blue swaps into place"); + assert_eq!(rgba[12..16], [255, 255, 255, 128], "alpha is untouched"); + + let dir = std::env::temp_dir().join(format!("edith-screenshot-test-{}", std::process::id())); + std::fs::create_dir_all(&dir).expect("scratch dir"); + let path = dir.join("roundtrip.png"); + let buf = image::RgbaImage::from_raw(2, 2, rgba.clone()).expect("2x2 rgba buffer"); + buf.save(&path).expect("png write"); + let decoded = image::open(&path).expect("png read").to_rgba8(); + assert_eq!(decoded.into_raw(), rgba, "the PNG round-trips the pixels exactly"); + let _ = std::fs::remove_dir_all(&dir); +} + +/// [`screenshot_path`]'s no-clobber rule: the same stem and timecode taken +/// twice in a row lands on `-2`, and a third time on `-3` -- a screenshot +/// never overwrites the one before it, unlike an export's own path. +#[test] +fn a_repeated_screenshot_name_gets_a_numeric_suffix() { + let dir = std::env::temp_dir().join(format!("edith-screenshot-suffix-{}", std::process::id())); + std::fs::create_dir_all(&dir).expect("scratch dir"); + + let first = screenshot_path(&dir, "A Film", "00-01-02"); + assert_eq!(first, dir.join("A Film-00-01-02.png")); + std::fs::write(&first, b"stand-in for a PNG").expect("write the first"); + + let second = screenshot_path(&dir, "A Film", "00-01-02"); + assert_eq!(second, dir.join("A Film-00-01-02-2.png")); + std::fs::write(&second, b"stand-in for a PNG").expect("write the second"); + + let third = screenshot_path(&dir, "A Film", "00-01-02"); + assert_eq!(third, dir.join("A Film-00-01-02-3.png")); + + // A different timecode never collides with either of the above. + let elsewhere = screenshot_path(&dir, "A Film", "00-01-03"); + assert_eq!(elsewhere, dir.join("A Film-00-01-03.png")); + + let _ = std::fs::remove_dir_all(&dir); +} diff --git a/crates/app/src/ui/toolbar.rs b/crates/app/src/ui/toolbar.rs index 4e0274a..687bb21 100644 --- a/crates/app/src/ui/toolbar.rs +++ b/crates/app/src/ui/toolbar.rs @@ -178,6 +178,16 @@ impl Player { .bg(rgb(BG_PANEL())) .border_t_1() .border_color(rgb(STROKE_DIVIDER())) + .child(self.action_control( + "screenshot", + 0., + BG_RAISED(), + None, + "Shot", + "saves the frame on screen as a PNG in ~/Pictures/edith", + ActionId::Screenshot, + cx.listener(|this, _: &ClickEvent, _, cx| this.take_screenshot(cx)), + )) .child(self.action_control( "transport", 40.,