The current frame saves as a PNG with a keystroke
Browse files at this commit7a82c202e44855435141194fb00b153f012c2b11Parent: 5231711- feat
- the current frame saves as a PNG with a keystroke
4 changed files +82 -2
- crates/app/src/keymap.rs +16 -2
- crates/app/src/player/actions.rs +1 -0
- crates/app/src/tests/media.rs +55 -0
- crates/app/src/ui/toolbar.rs +10 -0
crates/app/src/keymap.rsmodified+16 -2
| @@ -142,6 +142,10 @@ actions! { | |||
| 142 | /// its own, because a card reachable only by a button in the panel is one a | 142 | /// its own, because a card reachable only by a button in the panel is one a |
| 143 | /// hand on the keyboard has to leave the keyboard for. | 143 | /// hand on the keyboard has to leave the keyboard for. |
| 144 | ShowActions, | 144 | ShowActions, |
| 145 | + /// Saves the frame on screen to a PNG: whatever is showing, the timeline's | ||
| 146 | + /// own picture or a library preview's, since both write through the one | ||
| 147 | + /// `self.image` the atlas already holds. | ||
| 148 | + Screenshot, | ||
| 145 | } | 149 | } |
| 146 | 150 | ||
| 147 | impl ActionId { | 151 | impl ActionId { |
| @@ -203,6 +207,7 @@ impl ActionId { | |||
| 203 | ActionId::Fullscreen => "Fullscreen on / off", | 207 | ActionId::Fullscreen => "Fullscreen on / off", |
| 204 | ActionId::CancelExport => "Cancel export", | 208 | ActionId::CancelExport => "Cancel export", |
| 205 | ActionId::ShowActions => "All actions and their keys…", | 209 | ActionId::ShowActions => "All actions and their keys…", |
| 210 | + ActionId::Screenshot => "Save the frame on screen as a PNG", | ||
| 206 | } | 211 | } |
| 207 | } | 212 | } |
| 208 | 213 | ||
| @@ -262,6 +267,7 @@ impl ActionId { | |||
| 262 | ActionId::Fullscreen => "fullscreen", | 267 | ActionId::Fullscreen => "fullscreen", |
| 263 | ActionId::CancelExport => "cancel-export", | 268 | ActionId::CancelExport => "cancel-export", |
| 264 | ActionId::ShowActions => "show-actions", | 269 | ActionId::ShowActions => "show-actions", |
| 270 | + ActionId::Screenshot => "screenshot", | ||
| 265 | } | 271 | } |
| 266 | } | 272 | } |
| 267 | 273 | ||
| @@ -362,7 +368,11 @@ impl ActionId { | |||
| 362 | ActionId::Save | 368 | ActionId::Save |
| 363 | | ActionId::Export | 369 | | ActionId::Export |
| 364 | | ActionId::CancelExport | 370 | | ActionId::CancelExport |
| 365 | - | ActionId::ShowActions => Category::File, | 371 | + | ActionId::ShowActions |
| 372 | + // It writes a file like the three above it, and touches no edit | ||
| 373 | + // list -- what an export's own allow-list already means it stays | ||
| 374 | + // out of, not what puts it in this heading. | ||
| 375 | + | ActionId::Screenshot => Category::File, | ||
| 366 | } | 376 | } |
| 367 | } | 377 | } |
| 368 | } | 378 | } |
| @@ -907,6 +917,9 @@ impl Keymap { | |||
| 907 | // (platform.rs:880, the keysym's own name lowercased) -- and | 917 | // (platform.rs:880, the keysym's own name lowercased) -- and |
| 908 | // not a letter, so it takes nothing away from the clip keys. | 918 | // not a letter, so it takes nothing away from the clip keys. |
| 909 | b(ActionId::ShowActions, "f1", false), | 919 | b(ActionId::ShowActions, "f1", false), |
| 920 | + // "w" is free entirely, bare and ctrl both -- nothing else in | ||
| 921 | + // this table answers to it. | ||
| 922 | + b(ActionId::Screenshot, "w", false), | ||
| 910 | ], | 923 | ], |
| 911 | } | 924 | } |
| 912 | } | 925 | } |
| @@ -1170,8 +1183,9 @@ mod tests { | |||
| 1170 | #[test] | 1183 | #[test] |
| 1171 | fn every_default_stroke_reaches_its_action() { | 1184 | fn every_default_stroke_reaches_its_action() { |
| 1172 | let k = Keymap::defaults(); | 1185 | let k = Keymap::defaults(); |
| 1173 | - assert_eq!(k.entries().len(), 54); | 1186 | + assert_eq!(k.entries().len(), 55); |
| 1174 | assert_eq!(k.lookup("f11", false), Some(ActionId::Fullscreen)); | 1187 | assert_eq!(k.lookup("f11", false), Some(ActionId::Fullscreen)); |
| 1188 | + assert_eq!(k.lookup("w", false), Some(ActionId::Screenshot)); | ||
| 1175 | assert_eq!(k.lookup("p", true), Some(ActionId::ToggleProxies)); | 1189 | assert_eq!(k.lookup("p", true), Some(ActionId::ToggleProxies)); |
| 1176 | assert_eq!(k.lookup("o", true), Some(ActionId::ToggleAutoProxies)); | 1190 | assert_eq!(k.lookup("o", true), Some(ActionId::ToggleAutoProxies)); |
| 1177 | assert_eq!(k.lookup("f1", false), Some(ActionId::ShowActions)); | 1191 | assert_eq!(k.lookup("f1", false), Some(ActionId::ShowActions)); |
crates/app/src/player/actions.rsmodified+1 -0
| @@ -105,6 +105,7 @@ impl Player { | |||
| 105 | // the key handler is what answers this one while there is. | 105 | // the key handler is what answers this one while there is. |
| 106 | ActionId::CancelExport => {} | 106 | ActionId::CancelExport => {} |
| 107 | ActionId::ShowActions => self.show_actions(cx), | 107 | ActionId::ShowActions => self.show_actions(cx), |
| 108 | + ActionId::Screenshot => self.take_screenshot(cx), | ||
| 108 | } | 109 | } |
| 109 | } | 110 | } |
| 110 | 111 | ||
crates/app/src/tests/media.rsmodified+55 -0
| @@ -2,6 +2,7 @@ | |||
| 2 | //! command line, the scans, the codecs offered, and the subtitles found. | 2 | //! command line, the scans, the codecs offered, and the subtitles found. |
| 3 | 3 | ||
| 4 | use super::*; | 4 | use super::*; |
| 5 | +use crate::ui::preview::{bgra_to_rgba, screenshot_path}; | ||
| 5 | 6 | ||
| 6 | /// The import line's own state machine, driven the way a repaint drives | 7 | /// The import line's own state machine, driven the way a repaint drives |
| 7 | /// it: the worker writes a stage, the poll notices it changed and restarts | 8 | /// 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() { | |||
| 1158 | "the preview's play moved the wrong session" | 1159 | "the preview's play moved the wrong session" |
| 1159 | ); | 1160 | ); |
| 1160 | } | 1161 | } |
| 1162 | + | ||
| 1163 | +/// [`bgra_to_rgba`] and [`save_screenshot`]'s own PNG write, round-tripped: a | ||
| 1164 | +/// synthetic 2x2 frame -- one pixel per corner, alpha and channel order all | ||
| 1165 | +/// distinct -- decodes back to the same pixels a viewer would see, in RGBA. | ||
| 1166 | +#[test] | ||
| 1167 | +fn a_screenshot_frame_round_trips_through_its_png() { | ||
| 1168 | + // BGRA, gpui's own layout: red top-left, green top-right, blue | ||
| 1169 | + // bottom-left, and a half-alpha white bottom-right. | ||
| 1170 | + #[rustfmt::skip] | ||
| 1171 | + let bgra: Vec<u8> = vec![ | ||
| 1172 | + 0, 0, 255, 255, 0, 255, 0, 255, | ||
| 1173 | + 255, 0, 0, 255, 255, 255, 255, 128, | ||
| 1174 | + ]; | ||
| 1175 | + let rgba = bgra_to_rgba(&bgra); | ||
| 1176 | + assert_eq!(rgba[0..4], [255, 0, 0, 255], "red survives the channel swap"); | ||
| 1177 | + assert_eq!(rgba[4..8], [0, 255, 0, 255], "green is unmoved by the swap"); | ||
| 1178 | + assert_eq!(rgba[8..12], [0, 0, 255, 255], "blue swaps into place"); | ||
| 1179 | + assert_eq!(rgba[12..16], [255, 255, 255, 128], "alpha is untouched"); | ||
| 1180 | + | ||
| 1181 | + let dir = std::env::temp_dir().join(format!("edith-screenshot-test-{}", std::process::id())); | ||
| 1182 | + std::fs::create_dir_all(&dir).expect("scratch dir"); | ||
| 1183 | + let path = dir.join("roundtrip.png"); | ||
| 1184 | + let buf = image::RgbaImage::from_raw(2, 2, rgba.clone()).expect("2x2 rgba buffer"); | ||
| 1185 | + buf.save(&path).expect("png write"); | ||
| 1186 | + let decoded = image::open(&path).expect("png read").to_rgba8(); | ||
| 1187 | + assert_eq!(decoded.into_raw(), rgba, "the PNG round-trips the pixels exactly"); | ||
| 1188 | + let _ = std::fs::remove_dir_all(&dir); | ||
| 1189 | +} | ||
| 1190 | + | ||
| 1191 | +/// [`screenshot_path`]'s no-clobber rule: the same stem and timecode taken | ||
| 1192 | +/// twice in a row lands on `-2`, and a third time on `-3` -- a screenshot | ||
| 1193 | +/// never overwrites the one before it, unlike an export's own path. | ||
| 1194 | +#[test] | ||
| 1195 | +fn a_repeated_screenshot_name_gets_a_numeric_suffix() { | ||
| 1196 | + let dir = std::env::temp_dir().join(format!("edith-screenshot-suffix-{}", std::process::id())); | ||
| 1197 | + std::fs::create_dir_all(&dir).expect("scratch dir"); | ||
| 1198 | + | ||
| 1199 | + let first = screenshot_path(&dir, "A Film", "00-01-02"); | ||
| 1200 | + assert_eq!(first, dir.join("A Film-00-01-02.png")); | ||
| 1201 | + std::fs::write(&first, b"stand-in for a PNG").expect("write the first"); | ||
| 1202 | + | ||
| 1203 | + let second = screenshot_path(&dir, "A Film", "00-01-02"); | ||
| 1204 | + assert_eq!(second, dir.join("A Film-00-01-02-2.png")); | ||
| 1205 | + std::fs::write(&second, b"stand-in for a PNG").expect("write the second"); | ||
| 1206 | + | ||
| 1207 | + let third = screenshot_path(&dir, "A Film", "00-01-02"); | ||
| 1208 | + assert_eq!(third, dir.join("A Film-00-01-02-3.png")); | ||
| 1209 | + | ||
| 1210 | + // A different timecode never collides with either of the above. | ||
| 1211 | + let elsewhere = screenshot_path(&dir, "A Film", "00-01-03"); | ||
| 1212 | + assert_eq!(elsewhere, dir.join("A Film-00-01-03.png")); | ||
| 1213 | + | ||
| 1214 | + let _ = std::fs::remove_dir_all(&dir); | ||
| 1215 | +} | ||
crates/app/src/ui/toolbar.rsmodified+10 -0
| @@ -178,6 +178,16 @@ impl Player { | |||
| 178 | .bg(rgb(BG_PANEL())) | 178 | .bg(rgb(BG_PANEL())) |
| 179 | .border_t_1() | 179 | .border_t_1() |
| 180 | .border_color(rgb(STROKE_DIVIDER())) | 180 | .border_color(rgb(STROKE_DIVIDER())) |
| 181 | + .child(self.action_control( | ||
| 182 | + "screenshot", | ||
| 183 | + 0., | ||
| 184 | + BG_RAISED(), | ||
| 185 | + None, | ||
| 186 | + "Shot", | ||
| 187 | + "saves the frame on screen as a PNG in ~/Pictures/edith", | ||
| 188 | + ActionId::Screenshot, | ||
| 189 | + cx.listener(|this, _: &ClickEvent, _, cx| this.take_screenshot(cx)), | ||
| 190 | + )) | ||
| 181 | .child(self.action_control( | 191 | .child(self.action_control( |
| 182 | "transport", | 192 | "transport", |
| 183 | 40., | 193 | 40., |