Fullscreen, library preview, and frame screenshots
Browse files at this commit- feat
- fullscreen, library preview, and frame screenshots
13 changed files +460 -34
- crates/app/src/keymap.rs +30 -3
- crates/app/src/main.rs +13 -0
- crates/app/src/oracle.rs +3 -1
- crates/app/src/player/actions.rs +7 -2
- crates/app/src/player/library.rs +64 -0
- crates/app/src/player/transport.rs +45 -16
- crates/app/src/render.rs +11 -3
- crates/app/src/tests/editing.rs +7 -3
- crates/app/src/tests/media.rs +83 -0
- crates/app/src/ui/library.rs +29 -1
- crates/app/src/ui/overlays.rs +4 -4
- crates/app/src/ui/preview.rs +134 -0
- crates/app/src/ui/toolbar.rs +30 -1
crates/app/src/keymap.rsmodified+30 -3
| @@ -134,11 +134,18 @@ actions! { | |||
| 134 | /// toolbar's Theme button. It was a build feature once, which made it a | 134 | /// toolbar's Theme button. It was a build feature once, which made it a |
| 135 | /// choice only whoever compiled the binary could make. | 135 | /// choice only whoever compiled the binary could make. |
| 136 | Theme, | 136 | Theme, |
| 137 | + /// The whole window, borderless over the screen: a player's own control, | ||
| 138 | + /// on the platform's own toggle ([`gpui::Window::toggle_fullscreen`]). | ||
| 139 | + Fullscreen, | ||
| 137 | CancelExport, | 140 | CancelExport, |
| 138 | /// Opens the actions card -- the list every other action is on. A door of | 141 | /// Opens the actions card -- the list every other action is on. A door of |
| 139 | /// 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 |
| 140 | /// hand on the keyboard has to leave the keyboard for. | 143 | /// hand on the keyboard has to leave the keyboard for. |
| 141 | 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, | ||
| 142 | } | 149 | } |
| 143 | 150 | ||
| 144 | impl ActionId { | 151 | impl ActionId { |
| @@ -197,8 +204,10 @@ impl ActionId { | |||
| 197 | ActionId::ToggleProxies => "Proxies on / off for the picture", | 204 | ActionId::ToggleProxies => "Proxies on / off for the picture", |
| 198 | ActionId::ToggleAutoProxies => "Make proxies on import: on / off", | 205 | ActionId::ToggleAutoProxies => "Make proxies on import: on / off", |
| 199 | ActionId::Theme => "Theme: the window's colours…", | 206 | ActionId::Theme => "Theme: the window's colours…", |
| 207 | + ActionId::Fullscreen => "Fullscreen on / off", | ||
| 200 | ActionId::CancelExport => "Cancel export", | 208 | ActionId::CancelExport => "Cancel export", |
| 201 | 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", | ||
| 202 | } | 211 | } |
| 203 | } | 212 | } |
| 204 | 213 | ||
| @@ -255,8 +264,10 @@ impl ActionId { | |||
| 255 | ActionId::ToggleProxies => "toggle-proxies", | 264 | ActionId::ToggleProxies => "toggle-proxies", |
| 256 | ActionId::ToggleAutoProxies => "toggle-auto-proxies", | 265 | ActionId::ToggleAutoProxies => "toggle-auto-proxies", |
| 257 | ActionId::Theme => "theme", | 266 | ActionId::Theme => "theme", |
| 267 | + ActionId::Fullscreen => "fullscreen", | ||
| 258 | ActionId::CancelExport => "cancel-export", | 268 | ActionId::CancelExport => "cancel-export", |
| 259 | ActionId::ShowActions => "show-actions", | 269 | ActionId::ShowActions => "show-actions", |
| 270 | + ActionId::Screenshot => "screenshot", | ||
| 260 | } | 271 | } |
| 261 | } | 272 | } |
| 262 | 273 | ||
| @@ -317,7 +328,10 @@ impl ActionId { | |||
| 317 | | ActionId::ToggleAutoProxies | 328 | | ActionId::ToggleAutoProxies |
| 318 | // ...and what the whole window is painted in: it edits nothing at | 329 | // ...and what the whole window is painted in: it edits nothing at |
| 319 | // all, it is what one is looking *with*. | 330 | // all, it is what one is looking *with*. |
| 320 | - | ActionId::Theme => Category::View, | 331 | + | ActionId::Theme |
| 332 | + // ...and whether that window fills the screen: a way of looking, | ||
| 333 | + // same as the zoom and the theme beside it. | ||
| 334 | + | ActionId::Fullscreen => Category::View, | ||
| 321 | ActionId::Cut | 335 | ActionId::Cut |
| 322 | | ActionId::Regroup | 336 | | ActionId::Regroup |
| 323 | | ActionId::Detach | 337 | | ActionId::Detach |
| @@ -354,7 +368,11 @@ impl ActionId { | |||
| 354 | ActionId::Save | 368 | ActionId::Save |
| 355 | | ActionId::Export | 369 | | ActionId::Export |
| 356 | | ActionId::CancelExport | 370 | | ActionId::CancelExport |
| 357 | - | 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, | ||
| 358 | } | 376 | } |
| 359 | } | 377 | } |
| 360 | } | 378 | } |
| @@ -908,6 +926,10 @@ impl Keymap { | |||
| 908 | // it is a preference set once, not a stroke wanted under a hand | 926 | // it is a preference set once, not a stroke wanted under a hand |
| 909 | // that is editing. | 927 | // that is editing. |
| 910 | b(ActionId::Theme, "h", true), | 928 | b(ActionId::Theme, "h", true), |
| 929 | + // Every other player's key for it, and free here: nothing in | ||
| 930 | + // this table answers to "f11" (gpui lowercases the keysym | ||
| 931 | + // name it gets from xkb, same as "f1" below). | ||
| 932 | + b(ActionId::Fullscreen, "f11", false), | ||
| 911 | // A chord, and the only escape in this table: bare `esc` is the | 933 | // A chord, and the only escape in this table: bare `esc` is the |
| 912 | // stroke a hand throws at anything on screen, and throwing it | 934 | // stroke a hand throws at anything on screen, and throwing it |
| 913 | // at a running export used to delete an hour of encoding. The | 935 | // at a running export used to delete an hour of encoding. The |
| @@ -919,6 +941,9 @@ impl Keymap { | |||
| 919 | // (platform.rs:880, the keysym's own name lowercased) -- and | 941 | // (platform.rs:880, the keysym's own name lowercased) -- and |
| 920 | // not a letter, so it takes nothing away from the clip keys. | 942 | // not a letter, so it takes nothing away from the clip keys. |
| 921 | b(ActionId::ShowActions, "f1", false), | 943 | b(ActionId::ShowActions, "f1", false), |
| 944 | + // "w" is free entirely, bare and ctrl both -- nothing else in | ||
| 945 | + // this table answers to it. | ||
| 946 | + b(ActionId::Screenshot, "w", false), | ||
| 922 | ], | 947 | ], |
| 923 | } | 948 | } |
| 924 | } | 949 | } |
| @@ -1182,7 +1207,9 @@ mod tests { | |||
| 1182 | #[test] | 1207 | #[test] |
| 1183 | fn every_default_stroke_reaches_its_action() { | 1208 | fn every_default_stroke_reaches_its_action() { |
| 1184 | let k = Keymap::defaults(); | 1209 | let k = Keymap::defaults(); |
| 1185 | - assert_eq!(k.entries().len(), 53); | 1210 | + assert_eq!(k.entries().len(), 55); |
| 1211 | + assert_eq!(k.lookup("f11", false), Some(ActionId::Fullscreen)); | ||
| 1212 | + assert_eq!(k.lookup("w", false), Some(ActionId::Screenshot)); | ||
| 1186 | assert_eq!(k.lookup("p", true), Some(ActionId::ToggleProxies)); | 1213 | assert_eq!(k.lookup("p", true), Some(ActionId::ToggleProxies)); |
| 1187 | assert_eq!(k.lookup("o", true), Some(ActionId::ToggleAutoProxies)); | 1214 | assert_eq!(k.lookup("o", true), Some(ActionId::ToggleAutoProxies)); |
| 1188 | assert_eq!(k.lookup("f1", false), Some(ActionId::ShowActions)); | 1215 | assert_eq!(k.lookup("f1", false), Some(ActionId::ShowActions)); |
crates/app/src/main.rsmodified+13 -0
| @@ -73,6 +73,17 @@ struct Player { | |||
| 73 | /// waits: the first media import or project load is what fills it, and | 73 | /// waits: the first media import or project load is what fills it, and |
| 74 | /// until then every action that needs a timeline says so instead of acting. | 74 | /// until then every action that needs a timeline says so instead of acting. |
| 75 | session: Option<PlaybackSession>, | 75 | session: Option<PlaybackSession>, |
| 76 | + /// A library row's own file, opened and playing in place of the | ||
| 77 | + /// timeline's picture, and never written to `session` or its undo stack: | ||
| 78 | + /// a preview is watched, not edited. `Some` is what [`Player::pump`] and | ||
| 79 | + /// [`Player::transport`] read first -- the timeline's own session and its | ||
| 80 | + /// silence scan sit untouched underneath until this clears again | ||
| 81 | + /// ([`Player::close_preview`]). | ||
| 82 | + preview_session: Option<PlaybackSession>, | ||
| 83 | + /// Whether the timeline was playing when a preview took it over | ||
| 84 | + /// ([`Player::open_preview`] pauses it so the two sounds never overlap): | ||
| 85 | + /// what [`Player::close_preview`] resumes, and only then. | ||
| 86 | + preview_playing: bool, | ||
| 76 | /// Timeline seconds -> frame index, so the clock can be compared to what | 87 | /// Timeline seconds -> frame index, so the clock can be compared to what |
| 77 | /// the decoder hands over. | 88 | /// the decoder hands over. |
| 78 | fps: f64, | 89 | fps: f64, |
| @@ -669,6 +680,8 @@ fn main() { | |||
| 669 | seek_since: None, | 680 | seek_since: None, |
| 670 | resynced: None, | 681 | resynced: None, |
| 671 | session: None, | 682 | session: None, |
| 683 | + preview_session: None, | ||
| 684 | + preview_playing: false, | ||
| 672 | // Full and unmuted, which is what the session it was just | 685 | // Full and unmuted, which is what the session it was just |
| 673 | // handed is already set to: nothing to push at startup. | 686 | // handed is already set to: nothing to push at startup. |
| 674 | volume: Volume::default(), | 687 | volume: Volume::default(), |
crates/app/src/oracle.rsmodified+3 -1
| @@ -98,7 +98,9 @@ pub(crate) fn enable(action: ActionId, ctx: Ctx) -> Enable { | |||
| 98 | // The window's own colours: there is always a window, and repainting it | 98 | // The window's own colours: there is always a window, and repainting it |
| 99 | // touches no timeline -- so this one is live even while an export is | 99 | // touches no timeline -- so this one is live even while an export is |
| 100 | // reading one, which is the only state that dims the list above. | 100 | // reading one, which is the only state that dims the list above. |
| 101 | - if action == ActionId::Theme { | 101 | + // ...and whether that window fills the screen: also always there, also |
| 102 | + // touching no timeline, live through an export the same way. | ||
| 103 | + if action == ActionId::Theme || action == ActionId::Fullscreen { | ||
| 102 | return Enable::Yes; | 104 | return Enable::Yes; |
| 103 | } | 105 | } |
| 104 | if action == ActionId::ShowActions { | 106 | if action == ActionId::ShowActions { |
crates/app/src/player/actions.rsmodified+7 -2
| @@ -7,7 +7,7 @@ impl Player { | |||
| 7 | /// What an action does, wherever it was asked for -- a stroke, or the clip | 7 | /// What an action does, wherever it was asked for -- a stroke, or the clip |
| 8 | /// menu item that names the same action. One table, so the two can never | 8 | /// menu item that names the same action. One table, so the two can never |
| 9 | /// come to mean different things. | 9 | /// come to mean different things. |
| 10 | - pub(crate) fn act(&mut self, action: ActionId, cx: &mut Context<Self>) { | 10 | + pub(crate) fn act(&mut self, action: ActionId, window: &mut Window, cx: &mut Context<Self>) { |
| 11 | // Two doors, one oracle. This used to be the asymmetry the whole | 11 | // Two doors, one oracle. This used to be the asymmetry the whole |
| 12 | // toolbar was built on: the buttons dimmed themselves off | 12 | // toolbar was built on: the buttons dimmed themselves off |
| 13 | // [`enable`] while the keyboard walked straight past it, so with no | 13 | // [`enable`] while the keyboard walked straight past it, so with no |
| @@ -97,10 +97,15 @@ impl Player { | |||
| 97 | // At the window's corner, since a stroke names no place -- and | 97 | // At the window's corner, since a stroke names no place -- and |
| 98 | // [`menu_at`] keeps it on screen from there. | 98 | // [`menu_at`] keeps it on screen from there. |
| 99 | ActionId::Theme => self.open_picker(Pick::Theme, Point::default(), cx), | 99 | ActionId::Theme => self.open_picker(Pick::Theme, Point::default(), cx), |
| 100 | + // The platform's own toggle (gpui::Window::toggle_fullscreen); the | ||
| 101 | + // window reports its own new state back through `is_fullscreen`, | ||
| 102 | + // there is no local bool to keep in step with it. | ||
| 103 | + ActionId::Fullscreen => window.toggle_fullscreen(), | ||
| 100 | // Nothing to cancel while nothing is exporting; the export guard in | 104 | // Nothing to cancel while nothing is exporting; the export guard in |
| 101 | // the key handler is what answers this one while there is. | 105 | // the key handler is what answers this one while there is. |
| 102 | ActionId::CancelExport => {} | 106 | ActionId::CancelExport => {} |
| 103 | ActionId::ShowActions => self.show_actions(cx), | 107 | ActionId::ShowActions => self.show_actions(cx), |
| 108 | + ActionId::Screenshot => self.take_screenshot(cx), | ||
| 104 | } | 109 | } |
| 105 | } | 110 | } |
| 106 | 111 | ||
| @@ -248,7 +253,7 @@ impl Player { | |||
| 248 | timeline: true, | 253 | timeline: true, |
| 249 | clipboard: self.clipboard.is_some(), | 254 | clipboard: self.clipboard.is_some(), |
| 250 | subtitles: !session.subtitles().is_empty(), | 255 | subtitles: !session.subtitles().is_empty(), |
| 251 | - playable: !nothing_to_play(Some(session)), | 256 | + playable: !nothing_to_play(self.active_session()), |
| 252 | exporting: self.exporting().is_some(), | 257 | exporting: self.exporting().is_some(), |
| 253 | picks: picks.len(), | 258 | picks: picks.len(), |
| 254 | pick_lanes, | 259 | pick_lanes, |
crates/app/src/player/library.rsmodified+64 -0
| @@ -207,6 +207,70 @@ impl Player { | |||
| 207 | self.seek_since = None; | 207 | self.seek_since = None; |
| 208 | } | 208 | } |
| 209 | 209 | ||
| 210 | + /// The row's own play button: opens `path` as a session of its own and | ||
| 211 | + /// shows it in place of the timeline's picture, without moving the | ||
| 212 | + /// timeline's playhead or touching its undo stack -- what a library | ||
| 213 | + /// preview is. [`PlaybackSession::open`] already builds exactly this (a | ||
| 214 | + /// one-clip session, ungraded, passthrough), so nothing new is asked of | ||
| 215 | + /// the engine. | ||
| 216 | + /// | ||
| 217 | + /// corner-cut: opens the file's own first audio stream rather than the | ||
| 218 | + /// row's particular one -- `PlaybackSession::open` takes no stream | ||
| 219 | + /// argument. Ceiling: a remux with several audio tracks previews on its | ||
| 220 | + /// first. Upgrade path: an engine door that opens a chosen stream. | ||
| 221 | + pub(crate) fn open_preview(&mut self, path: &std::path::Path, cx: &mut Context<Self>) { | ||
| 222 | + if self.exporting().is_some() { | ||
| 223 | + return; | ||
| 224 | + } | ||
| 225 | + // The timeline would otherwise keep playing under the preview -- two | ||
| 226 | + // soundtracks at once -- so it is paused here and resumed on the way | ||
| 227 | + // out only if this is what paused it ([`Player::close_preview`]). | ||
| 228 | + self.preview_playing = self.session.as_ref().is_some_and(PlaybackSession::is_playing); | ||
| 229 | + if let Some(session) = self.session.as_mut() { | ||
| 230 | + session.pause(); | ||
| 231 | + } | ||
| 232 | + match PlaybackSession::open(path) { | ||
| 233 | + Ok(mut session) => { | ||
| 234 | + session.drop_late_pictures(true); | ||
| 235 | + session.set_gain(self.volume.gain()); | ||
| 236 | + self.preview_session = Some(session); | ||
| 237 | + self.notify_user( | ||
| 238 | + format!("PREVIEWING {} — not on the timeline; esc stops it", file_name(path)) | ||
| 239 | + .into(), | ||
| 240 | + ); | ||
| 241 | + } | ||
| 242 | + Err(e) => self.notify_user(format!("PREVIEW FAILED: {e}").into()), | ||
| 243 | + } | ||
| 244 | + self.reset_after_reseek(); | ||
| 245 | + cx.notify(); | ||
| 246 | + } | ||
| 247 | + | ||
| 248 | + /// The way out of a preview: `esc`, or the banner's own button. The | ||
| 249 | + /// timeline was never pumped while the preview played, so its picture is | ||
| 250 | + /// stale where it left off -- a reseek to the position it is already at | ||
| 251 | + /// is what brings a fresh frame back, exactly as loading a project does. | ||
| 252 | + pub(crate) fn close_preview(&mut self, cx: &mut Context<Self>) { | ||
| 253 | + if self.preview_session.take().is_none() { | ||
| 254 | + return; | ||
| 255 | + } | ||
| 256 | + self.notify_user("PREVIEW STOPPED — back to the timeline".into()); | ||
| 257 | + match self.session.as_ref().map(PlaybackSession::now) { | ||
| 258 | + Some(now) => { | ||
| 259 | + self.seek(now, cx); | ||
| 260 | + if self.preview_playing { | ||
| 261 | + if let Some(session) = self.session.as_mut() { | ||
| 262 | + session.play(); | ||
| 263 | + } | ||
| 264 | + } | ||
| 265 | + } | ||
| 266 | + None => { | ||
| 267 | + self.image = None; | ||
| 268 | + cx.notify(); | ||
| 269 | + } | ||
| 270 | + } | ||
| 271 | + self.preview_playing = false; | ||
| 272 | + } | ||
| 273 | + | ||
| 210 | /// The rate and layout the whole timeline's audio is, taken from the stream | 274 | /// The rate and layout the whole timeline's audio is, taken from the stream |
| 211 | /// of the first source that could have one: what a library row has to match | 275 | /// of the first source that could have one: what a library row has to match |
| 212 | /// to be placeable. `None` until that file has been probed, and then nothing | 276 | /// to be placeable. `None` until that file has been probed, and then nothing |
crates/app/src/player/transport.rsmodified+45 -16
| @@ -4,6 +4,27 @@ | |||
| 4 | use crate::*; | 4 | use crate::*; |
| 5 | 5 | ||
| 6 | impl Player { | 6 | impl Player { |
| 7 | + /// The session the transport, the pump and the clock all read: the | ||
| 8 | + /// preview while one is showing, the timeline otherwise -- never both, so | ||
| 9 | + /// a preview is watched without moving the edit's own playhead. See | ||
| 10 | + /// [`preview_session`](Player) for what stays untouched underneath. | ||
| 11 | + pub(crate) fn active_session(&self) -> Option<&PlaybackSession> { | ||
| 12 | + self.preview_session.as_ref().or(self.session.as_ref()) | ||
| 13 | + } | ||
| 14 | + | ||
| 15 | + pub(crate) fn active_session_mut(&mut self) -> Option<&mut PlaybackSession> { | ||
| 16 | + self.preview_session.as_mut().or(self.session.as_mut()) | ||
| 17 | + } | ||
| 18 | + | ||
| 19 | + /// The active session's own frame rate rather than the timeline's: a | ||
| 20 | + /// preview of a file shot at another rate clocks and steps at *its* rate, | ||
| 21 | + /// not the edit's. | ||
| 22 | + pub(crate) fn active_fps(&self) -> f64 { | ||
| 23 | + self.preview_session | ||
| 24 | + .as_ref() | ||
| 25 | + .map_or(self.fps, |s| s.meta().frame_rate) | ||
| 26 | + } | ||
| 27 | + | ||
| 7 | /// Catches the display up to the clock: everything already due is taken off | 28 | /// Catches the display up to the clock: everything already due is taken off |
| 8 | /// the channel and only the last of them is shown, which *is* the | 29 | /// the channel and only the last of them is shown, which *is* the |
| 9 | /// drop-when-behind policy. A frame that is not due yet waits in `held`, and | 30 | /// drop-when-behind policy. A frame that is not due yet waits in `held`, and |
| @@ -13,9 +34,16 @@ impl Player { | |||
| 13 | // Where the transport was before this drain, so the crossing into | 34 | // Where the transport was before this drain, so the crossing into |
| 14 | // `Ended` can be recognised as the one transition it is. | 35 | // `Ended` can be recognised as the one transition it is. |
| 15 | let was = self.transport(); | 36 | let was = self.transport(); |
| 37 | + let fps = self.active_fps(); | ||
| 16 | // No timeline, nothing to catch up to: the window is showing its empty | 38 | // No timeline, nothing to catch up to: the window is showing its empty |
| 17 | // state and there is no decoder to drain. | 39 | // state and there is no decoder to drain. |
| 18 | - let Some(session) = &mut self.session else { | 40 | + // A direct field borrow rather than the `active_session_mut` helper: the |
| 41 | + // rest of this function reads and writes other fields on `self` | ||
| 42 | + // (`held`, `seek_since`, `dropped`, `displayed`, ...) alongside | ||
| 43 | + // `session`, and only a borrow of the two Option fields themselves -- | ||
| 44 | + // not a call through a `&mut self` method -- lets the borrow checker | ||
| 45 | + // see those as disjoint. | ||
| 46 | + let Some(session) = self.preview_session.as_mut().or(self.session.as_mut()) else { | ||
| 19 | return; | 47 | return; |
| 20 | }; | 48 | }; |
| 21 | // Whether the span now decoding has yet to hand over a picture, read | 49 | // Whether the span now decoding has yet to hand over a picture, read |
| @@ -24,7 +52,7 @@ impl Player { | |||
| 24 | // reopen took -- is exactly what the resync below must not answer with | 52 | // reopen took -- is exactly what the resync below must not answer with |
| 25 | // another reopen. | 53 | // another reopen. |
| 26 | let priming = session.picture_priming(); | 54 | let priming = session.picture_priming(); |
| 27 | - let target = session.now() * self.fps; | 55 | + let target = session.now() * fps; |
| 28 | let mut newest: Option<Frame> = None; | 56 | let mut newest: Option<Frame> = None; |
| 29 | // A frame the screen is owed: a seek's landing, and the one readiness | 57 | // A frame the screen is owed: a seek's landing, and the one readiness |
| 30 | // signal there is ([`Player::reset_after_reseek`]). | 58 | // signal there is ([`Player::reset_after_reseek`]). |
| @@ -66,7 +94,7 @@ impl Player { | |||
| 66 | // waiting on. | 94 | // waiting on. |
| 67 | let late = newest | 95 | let late = newest |
| 68 | .as_ref() | 96 | .as_ref() |
| 69 | - .map_or(0., |f| (target - f64::from(f.index)) / self.fps); | 97 | + .map_or(0., |f| (target - f64::from(f.index)) / fps); |
| 70 | 98 | ||
| 71 | if let Some(frame) = newest { | 99 | if let Some(frame) = newest { |
| 72 | self.displayed += 1; | 100 | self.displayed += 1; |
| @@ -137,7 +165,7 @@ impl Player { | |||
| 137 | // empty space (measured: a 5 s timeline recognised its end at | 165 | // empty space (measured: a 5 s timeline recognised its end at |
| 138 | // clock 17.5 s under a slow renderer). End of stream is left | 166 | // clock 17.5 s under a slow renderer). End of stream is left |
| 139 | // set, so this is still `Ended` and the next press restarts. | 167 | // set, so this is still `Ended` and the next press restarts. |
| 140 | - if let Some(session) = &mut self.session { | 168 | + if let Some(session) = self.active_session_mut() { |
| 141 | session.halt_at_end(); | 169 | session.halt_at_end(); |
| 142 | } | 170 | } |
| 143 | let elapsed = self.started.map_or(0.0, |t| t.elapsed().as_secs_f64()); | 171 | let elapsed = self.started.map_or(0.0, |t| t.elapsed().as_secs_f64()); |
| @@ -145,7 +173,7 @@ impl Player { | |||
| 145 | "eof after {elapsed:.3}s wall: {} frames displayed, {} dropped, clock {:.3}s", | 173 | "eof after {elapsed:.3}s wall: {} frames displayed, {} dropped, clock {:.3}s", |
| 146 | self.displayed, | 174 | self.displayed, |
| 147 | self.dropped, | 175 | self.dropped, |
| 148 | - self.session.as_ref().map_or(0., PlaybackSession::now) | 176 | + self.active_session().map_or(0., PlaybackSession::now) |
| 149 | ); | 177 | ); |
| 150 | } | 178 | } |
| 151 | } | 179 | } |
| @@ -156,7 +184,7 @@ impl Player { | |||
| 156 | /// an edit past the end revives the picture) and so is the clock. A held | 184 | /// an edit past the end revives the picture) and so is the clock. A held |
| 157 | /// frame is one still owed to the screen, so the end is not the end yet. | 185 | /// frame is one still owed to the screen, so the end is not the end yet. |
| 158 | pub(crate) fn transport(&self) -> Transport { | 186 | pub(crate) fn transport(&self) -> Transport { |
| 159 | - let Some(session) = &self.session else { | 187 | + let Some(session) = self.active_session() else { |
| 160 | return Transport::Stopped; | 188 | return Transport::Stopped; |
| 161 | }; | 189 | }; |
| 162 | transport( | 190 | transport( |
| @@ -193,7 +221,7 @@ impl Player { | |||
| 193 | if self.exporting().is_some() { | 221 | if self.exporting().is_some() { |
| 194 | return; | 222 | return; |
| 195 | } | 223 | } |
| 196 | - let Some(session) = &mut self.session else { | 224 | + let Some(session) = self.active_session_mut() else { |
| 197 | return; | 225 | return; |
| 198 | }; | 226 | }; |
| 199 | session.seek(t); | 227 | session.seek(t); |
| @@ -211,16 +239,17 @@ impl Player { | |||
| 211 | /// edit, and nothing it does moves a clip index. | 239 | /// edit, and nothing it does moves a clip index. |
| 212 | pub(crate) fn step(&mut self, frames: i64, cx: &mut Context<Self>) { | 240 | pub(crate) fn step(&mut self, frames: i64, cx: &mut Context<Self>) { |
| 213 | let ended = self.transport() == Transport::Ended; | 241 | let ended = self.transport() == Transport::Ended; |
| 214 | - let Some(session) = &self.session else { | 242 | + let fps = self.active_fps(); |
| 243 | + let Some(session) = self.active_session() else { | ||
| 215 | return; | 244 | return; |
| 216 | }; | 245 | }; |
| 217 | - let last = ((session.timeline_duration() * self.fps).round() as i64 - 1).max(0); | 246 | + let last = ((session.timeline_duration() * fps).round() as i64 - 1).max(0); |
| 218 | let now = match ended { | 247 | let now = match ended { |
| 219 | true => last, | 248 | true => last, |
| 220 | - false => i64::from(frame_at(session.now(), self.fps)), | 249 | + false => i64::from(frame_at(session.now(), fps)), |
| 221 | }; | 250 | }; |
| 222 | let target = now.saturating_add(frames).clamp(0, last); | 251 | let target = now.saturating_add(frames).clamp(0, last); |
| 223 | - self.seek(target as f64 / self.fps, cx); | 252 | + self.seek(target as f64 / fps, cx); |
| 224 | } | 253 | } |
| 225 | 254 | ||
| 226 | /// Seeks to where the pointer sits along the ruler. `commit` is the press | 255 | /// Seeks to where the pointer sits along the ruler. `commit` is the press |
| @@ -252,7 +281,7 @@ impl Player { | |||
| 252 | /// that is the whole reason this is not just called from the key handler. | 281 | /// that is the whole reason this is not just called from the key handler. |
| 253 | /// Silent no-op with no timeline, or with a run that has no audio device. | 282 | /// Silent no-op with no timeline, or with a run that has no audio device. |
| 254 | pub(crate) fn apply_volume(&self) { | 283 | pub(crate) fn apply_volume(&self) { |
| 255 | - if let Some(session) = &self.session { | 284 | + if let Some(session) = self.active_session() { |
| 256 | session.set_gain(self.volume.gain()); | 285 | session.set_gain(self.volume.gain()); |
| 257 | } | 286 | } |
| 258 | } | 287 | } |
| @@ -285,8 +314,8 @@ impl Player { | |||
| 285 | // -- and it is Ended again by the next repaint, so no later press could | 314 | // -- and it is Ended again by the next repaint, so no later press could |
| 286 | // ever stop it: the button would read "Pause" and never pause. A delete | 315 | // ever stop it: the button would read "Pause" and never pause. A delete |
| 287 | // can empty the timeline mid-play, and that press must still stop it. | 316 | // can empty the timeline mid-play, and that press must still stop it. |
| 288 | - if nothing_to_play(self.session.as_ref()) { | 317 | + if nothing_to_play(self.active_session()) { |
| 289 | - match self.session.as_mut().filter(|s| s.is_playing()) { | 318 | + match self.active_session_mut().filter(|s| s.is_playing()) { |
| 290 | Some(session) => session.pause(), | 319 | Some(session) => session.pause(), |
| 291 | None => self.notify_user(NOTHING_TO_PLAY.into()), | 320 | None => self.notify_user(NOTHING_TO_PLAY.into()), |
| 292 | } | 321 | } |
| @@ -303,12 +332,12 @@ impl Player { | |||
| 303 | // whichever asked, the transport was showing Play. | 332 | // whichever asked, the transport was showing Play. |
| 304 | state if state.restarts() => { | 333 | state if state.restarts() => { |
| 305 | self.seek(0., cx); | 334 | self.seek(0., cx); |
| 306 | - if let Some(session) = &mut self.session { | 335 | + if let Some(session) = self.active_session_mut() { |
| 307 | session.play(); | 336 | session.play(); |
| 308 | } | 337 | } |
| 309 | } | 338 | } |
| 310 | _ => { | 339 | _ => { |
| 311 | - if let Some(session) = &mut self.session { | 340 | + if let Some(session) = self.active_session_mut() { |
| 312 | session.toggle(); | 341 | session.toggle(); |
| 313 | // A paused timeline animates nothing; this is the repaint | 342 | // A paused timeline animates nothing; this is the repaint |
| 314 | // that puts the new glyph up. | 343 | // that puts the new glyph up. |
crates/app/src/render.rsmodified+11 -3
| @@ -95,7 +95,7 @@ impl Render for Player { | |||
| 95 | 95 | ||
| 96 | div() | 96 | div() |
| 97 | .track_focus(&self.focus) | 97 | .track_focus(&self.focus) |
| 98 | - .on_key_down(cx.listener(|this, event: &KeyDownEvent, _, cx| { | 98 | + .on_key_down(cx.listener(|this, event: &KeyDownEvent, window, cx| { |
| 99 | let key = event.keystroke.key.as_str(); | 99 | let key = event.keystroke.key.as_str(); |
| 100 | let ctrl = event.keystroke.modifiers.control; | 100 | let ctrl = event.keystroke.modifiers.control; |
| 101 | // `is_held` is the auto-repeat, and a value is the one thing | 101 | // `is_held` is the auto-repeat, and a value is the one thing |
| @@ -474,8 +474,15 @@ impl Render for Player { | |||
| 474 | return; | 474 | return; |
| 475 | } | 475 | } |
| 476 | } | 476 | } |
| 477 | + // The innermost thing left on screen once no menu is up: a | ||
| 478 | + // preview takes the picture over the timeline's own, and | ||
| 479 | + // escape is its own way out, same as every card above it. | ||
| 480 | + if key == ESCAPE && !ctrl && this.preview_session.is_some() { | ||
| 481 | + this.close_preview(cx); | ||
| 482 | + return; | ||
| 483 | + } | ||
| 477 | if let Some(action) = action { | 484 | if let Some(action) = action { |
| 478 | - this.act(action, cx); | 485 | + this.act(action, window, cx); |
| 479 | } | 486 | } |
| 480 | })) | 487 | })) |
| 481 | // The whole window is the drop target: gpui turns an external file | 488 | // The whole window is the drop target: gpui turns an external file |
| @@ -561,7 +568,7 @@ impl Render for Player { | |||
| 561 | // it. Nothing here moves when the state changes -- the regions are | 568 | // it. Nothing here moves when the state changes -- the regions are |
| 562 | // fixed and the panels keep their room whether or not anything is | 569 | // fixed and the panels keep their room whether or not anything is |
| 563 | // open in them. | 570 | // open in them. |
| 564 | - .child(self.topbar(cx)) | 571 | + .child(self.topbar(window, cx)) |
| 565 | .child( | 572 | .child( |
| 566 | div() | 573 | div() |
| 567 | .flex_1() | 574 | .flex_1() |
| @@ -619,6 +626,7 @@ impl Render for Player { | |||
| 619 | // After the picture, so the plate is drawn | 626 | // After the picture, so the plate is drawn |
| 620 | // over it rather than under. | 627 | // over it rather than under. |
| 621 | .children(self.subtitle_overlay(position, window)) | 628 | .children(self.subtitle_overlay(position, window)) |
| 629 | + .children(self.preview_badge(cx)) | ||
| 622 | // The three transient lines hang off the | 630 | // The three transient lines hang off the |
| 623 | // bottom of the picture rather than taking | 631 | // bottom of the picture rather than taking |
| 624 | // a row of the column: a notice that | 632 | // a row of the column: a notice that |
crates/app/src/tests/editing.rsmodified+7 -3
| @@ -884,9 +884,13 @@ fn the_clip_menu_dims_what_the_playhead_is_not_on_and_stays_in_the_window() { | |||
| 884 | for action in ActionId::ALL { | 884 | for action in ActionId::ALL { |
| 885 | // Nothing may touch the edit list while an export is reading it -- | 885 | // Nothing may touch the edit list while an export is reading it -- |
| 886 | // and the palette does not: it repaints this window and writes one | 886 | // and the palette does not: it repaints this window and writes one |
| 887 | - // word to the user's own config file, which is why it is the one | 887 | + // word to the user's own config file, which is why it is one of the |
| 888 | - // action besides the cancel that stays live here. | 888 | + // actions besides the cancel that stay live here. Fullscreen is the |
| 889 | - let allowed = action == ActionId::CancelExport || action == ActionId::Theme; | 889 | + // same kind of thing: the platform's own window toggle, touching no |
| 890 | + // edit list either. | ||
| 891 | + let allowed = action == ActionId::CancelExport | ||
| 892 | + || action == ActionId::Theme | ||
| 893 | + || action == ActionId::Fullscreen; | ||
| 890 | assert_eq!( | 894 | assert_eq!( |
| 891 | whole(action, busy).yes(), | 895 | whole(action, busy).yes(), |
| 892 | allowed, | 896 | allowed, |
crates/app/src/tests/media.rsmodified+83 -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 |
| @@ -1184,3 +1185,85 @@ fn exactly_one_subtitle_lane_is_shown_whatever_the_pick_and_whatever_is_left() { | |||
| 1184 | // A pick that is not a subtitle lane at all cannot name one either. | 1185 | // A pick that is not a subtitle lane at all cannot name one either. |
| 1185 | assert_eq!(active_lane(Some(Lane::V1), &[s1, s2]), Some(s1)); | 1186 | assert_eq!(active_lane(Some(Lane::V1), &[s1, s2]), Some(s1)); |
| 1186 | } | 1187 | } |
| 1188 | + | ||
| 1189 | +/// A library preview's whole point ([`Player::open_preview`], | ||
| 1190 | +/// [`Player::close_preview`]): the timeline's own session is untouched by | ||
| 1191 | +/// one showing over it. `Player` cannot be built here -- it takes a gpui | ||
| 1192 | +/// `Context`, which this test binary has no `TestAppContext` to hand it | ||
| 1193 | +/// (see the crate's other tests for the same limit) -- so this exercises | ||
| 1194 | +/// the two sessions `open_preview`/`close_preview` juggle directly: seeking | ||
| 1195 | +/// and playing one, exactly as a preview session's own life does, leaves | ||
| 1196 | +/// the other's position and clock exactly where they were. | ||
| 1197 | +#[test] | ||
| 1198 | +fn a_second_session_playing_never_moves_the_first() { | ||
| 1199 | + let mut timeline = PlaybackSession::open(asset("test_av.mp4")).expect("open the fixture"); | ||
| 1200 | + timeline.seek(0.5); | ||
| 1201 | + let before = timeline.now(); | ||
| 1202 | + let before_playing = timeline.is_playing(); | ||
| 1203 | + | ||
| 1204 | + // Stands in for the preview session `open_preview` opens over it. | ||
| 1205 | + let mut preview = PlaybackSession::open(asset("test_av.mp4")).expect("open the fixture"); | ||
| 1206 | + preview.seek(1.0); | ||
| 1207 | + preview.play(); | ||
| 1208 | + | ||
| 1209 | + assert_eq!(timeline.now(), before, "the preview's seek moved the wrong session"); | ||
| 1210 | + assert_eq!( | ||
| 1211 | + timeline.is_playing(), | ||
| 1212 | + before_playing, | ||
| 1213 | + "the preview's play moved the wrong session" | ||
| 1214 | + ); | ||
| 1215 | +} | ||
| 1216 | + | ||
| 1217 | +/// [`bgra_to_rgba`] and [`save_screenshot`]'s own PNG write, round-tripped: a | ||
| 1218 | +/// synthetic 2x2 frame -- one pixel per corner, alpha and channel order all | ||
| 1219 | +/// distinct -- decodes back to the same pixels a viewer would see, in RGBA. | ||
| 1220 | +#[test] | ||
| 1221 | +fn a_screenshot_frame_round_trips_through_its_png() { | ||
| 1222 | + // BGRA, gpui's own layout: red top-left, green top-right, blue | ||
| 1223 | + // bottom-left, and a half-alpha white bottom-right. | ||
| 1224 | + #[rustfmt::skip] | ||
| 1225 | + let bgra: Vec<u8> = vec![ | ||
| 1226 | + 0, 0, 255, 255, 0, 255, 0, 255, | ||
| 1227 | + 255, 0, 0, 255, 255, 255, 255, 128, | ||
| 1228 | + ]; | ||
| 1229 | + let rgba = bgra_to_rgba(&bgra); | ||
| 1230 | + assert_eq!(rgba[0..4], [255, 0, 0, 255], "red survives the channel swap"); | ||
| 1231 | + assert_eq!(rgba[4..8], [0, 255, 0, 255], "green is unmoved by the swap"); | ||
| 1232 | + assert_eq!(rgba[8..12], [0, 0, 255, 255], "blue swaps into place"); | ||
| 1233 | + assert_eq!(rgba[12..16], [255, 255, 255, 128], "alpha is untouched"); | ||
| 1234 | + | ||
| 1235 | + let dir = std::env::temp_dir().join(format!("edith-screenshot-test-{}", std::process::id())); | ||
| 1236 | + std::fs::create_dir_all(&dir).expect("scratch dir"); | ||
| 1237 | + let path = dir.join("roundtrip.png"); | ||
| 1238 | + let buf = image::RgbaImage::from_raw(2, 2, rgba.clone()).expect("2x2 rgba buffer"); | ||
| 1239 | + buf.save(&path).expect("png write"); | ||
| 1240 | + let decoded = image::open(&path).expect("png read").to_rgba8(); | ||
| 1241 | + assert_eq!(decoded.into_raw(), rgba, "the PNG round-trips the pixels exactly"); | ||
| 1242 | + let _ = std::fs::remove_dir_all(&dir); | ||
| 1243 | +} | ||
| 1244 | + | ||
| 1245 | +/// [`screenshot_path`]'s no-clobber rule: the same stem and timecode taken | ||
| 1246 | +/// twice in a row lands on `-2`, and a third time on `-3` -- a screenshot | ||
| 1247 | +/// never overwrites the one before it, unlike an export's own path. | ||
| 1248 | +#[test] | ||
| 1249 | +fn a_repeated_screenshot_name_gets_a_numeric_suffix() { | ||
| 1250 | + let dir = std::env::temp_dir().join(format!("edith-screenshot-suffix-{}", std::process::id())); | ||
| 1251 | + std::fs::create_dir_all(&dir).expect("scratch dir"); | ||
| 1252 | + | ||
| 1253 | + let first = screenshot_path(&dir, "A Film", "00-01-02"); | ||
| 1254 | + assert_eq!(first, dir.join("A Film-00-01-02.png")); | ||
| 1255 | + std::fs::write(&first, b"stand-in for a PNG").expect("write the first"); | ||
| 1256 | + | ||
| 1257 | + let second = screenshot_path(&dir, "A Film", "00-01-02"); | ||
| 1258 | + assert_eq!(second, dir.join("A Film-00-01-02-2.png")); | ||
| 1259 | + std::fs::write(&second, b"stand-in for a PNG").expect("write the second"); | ||
| 1260 | + | ||
| 1261 | + let third = screenshot_path(&dir, "A Film", "00-01-02"); | ||
| 1262 | + assert_eq!(third, dir.join("A Film-00-01-02-3.png")); | ||
| 1263 | + | ||
| 1264 | + // A different timecode never collides with either of the above. | ||
| 1265 | + let elsewhere = screenshot_path(&dir, "A Film", "00-01-03"); | ||
| 1266 | + assert_eq!(elsewhere, dir.join("A Film-00-01-03.png")); | ||
| 1267 | + | ||
| 1268 | + let _ = std::fs::remove_dir_all(&dir); | ||
| 1269 | +} | ||
crates/app/src/ui/library.rsmodified+29 -1
| @@ -170,14 +170,20 @@ impl Player { | |||
| 170 | // name alone was, and the detail line under it ran on under the | 170 | // name alone was, and the detail line under it ran on under the |
| 171 | // switch and was cut mid-word -- and an unusable row draws no switch | 171 | // switch and was cut mid-word -- and an unusable row draws no switch |
| 172 | // and so pays nothing for one. | 172 | // and so pays nothing for one. |
| 173 | + // Two switches now sit at the end of a usable row -- the preview | ||
| 174 | + // play button beside the stand-in toggle -- so the text gives up | ||
| 175 | + // twice the room it gave up for one. | ||
| 173 | let text_w = row_text_w(width) | 176 | let text_w = row_text_w(width) |
| 174 | - match usable { | 177 | - match usable { |
| 175 | - true => HIT_MIN + 6., | 178 | + true => 2. * (HIT_MIN + 6.), |
| 176 | false => 0., | 179 | false => 0., |
| 177 | }; | 180 | }; |
| 178 | let (path, stream) = (row.path.clone(), row.stream); | 181 | let (path, stream) = (row.path.clone(), row.stream); |
| 179 | let dragged = (path.clone(), stream); | 182 | let dragged = (path.clone(), stream); |
| 180 | let menu_path = path.clone(); | 183 | let menu_path = path.clone(); |
| 184 | + let preview_path = path.clone(); | ||
| 185 | + let preview_tip: SharedString = | ||
| 186 | + format!("Preview {} — plays it without touching the timeline", row.name).into(); | ||
| 181 | div() | 187 | div() |
| 182 | .id(("asset", i)) | 188 | .id(("asset", i)) |
| 183 | .flex_none() | 189 | .flex_none() |
| @@ -282,6 +288,28 @@ impl Player { | |||
| 282 | // the window speaks says which way it is set -- lit like a | 288 | // the window speaks says which way it is set -- lit like a |
| 283 | // picked row when there is one, dimmed like an unusable one | 289 | // picked row when there is one, dimmed like an unusable one |
| 284 | // when there is not. | 290 | // when there is not. |
| 291 | + .when(usable, |d| { | ||
| 292 | + d.child( | ||
| 293 | + div() | ||
| 294 | + .id(("preview-play", i)) | ||
| 295 | + .flex_none() | ||
| 296 | + .w(px(HIT_MIN)) | ||
| 297 | + .h_full() | ||
| 298 | + .flex() | ||
| 299 | + .items_center() | ||
| 300 | + .justify_center() | ||
| 301 | + .rounded(px(3.)) | ||
| 302 | + .text_size(px(11.)) | ||
| 303 | + .cursor_pointer() | ||
| 304 | + .hover(|s| s.bg(rgb(BG_HOVER()))) | ||
| 305 | + .tooltip(move |_, cx| cx.new(|_| Tip(preview_tip.clone())).into()) | ||
| 306 | + .on_click(cx.listener(move |this, _: &ClickEvent, _, cx| { | ||
| 307 | + cx.stop_propagation(); | ||
| 308 | + this.open_preview(&preview_path, cx); | ||
| 309 | + })) | ||
| 310 | + .child("▶"), | ||
| 311 | + ) | ||
| 312 | + }) | ||
| 285 | .when(usable, |d| { | 313 | .when(usable, |d| { |
| 286 | d.child( | 314 | d.child( |
| 287 | div() | 315 | div() |
crates/app/src/ui/overlays.rsmodified+4 -4
| @@ -304,14 +304,14 @@ impl Player { | |||
| 304 | .when(!refusal.yes(), |d| d.opacity(0.4).cursor_not_allowed()) | 304 | .when(!refusal.yes(), |d| d.opacity(0.4).cursor_not_allowed()) |
| 305 | .when(refusal.yes(), |d| { | 305 | .when(refusal.yes(), |d| { |
| 306 | d.cursor_pointer().hover(|s| s.bg(rgb(BG_HOVER()))).on_click( | 306 | d.cursor_pointer().hover(|s| s.bg(rgb(BG_HOVER()))).on_click( |
| 307 | - cx.listener(move |this, _: &ClickEvent, _, cx| { | 307 | + cx.listener(move |this, _: &ClickEvent, window, cx| { |
| 308 | // The card goes first: several of | 308 | // The card goes first: several of |
| 309 | // these open a card of their own, | 309 | // these open a card of their own, |
| 310 | // and every edit moves the indices | 310 | // and every edit moves the indices |
| 311 | // the menus are holding. | 311 | // the menus are holding. |
| 312 | this.keys_open = false; | 312 | this.keys_open = false; |
| 313 | this.rebinding = None; | 313 | this.rebinding = None; |
| 314 | - this.act(action, cx); | 314 | + this.act(action, window, cx); |
| 315 | cx.notify(); | 315 | cx.notify(); |
| 316 | }), | 316 | }), |
| 317 | ) | 317 | ) |
| @@ -578,7 +578,7 @@ impl Player { | |||
| 578 | .when(enabled, |d| { | 578 | .when(enabled, |d| { |
| 579 | d.cursor_pointer() | 579 | d.cursor_pointer() |
| 580 | .hover(|s| s.bg(rgb(BG_HOVER()))) | 580 | .hover(|s| s.bg(rgb(BG_HOVER()))) |
| 581 | - .on_click(cx.listener(move |this, event: &ClickEvent, _, cx| { | 581 | + .on_click(cx.listener(move |this, event: &ClickEvent, window, cx| { |
| 582 | // Closed first: the action moves the very | 582 | // Closed first: the action moves the very |
| 583 | // indices this menu is holding. | 583 | // indices this menu is holding. |
| 584 | this.context_menu = None; | 584 | this.context_menu = None; |
| @@ -594,7 +594,7 @@ impl Player { | |||
| 594 | cx, | 594 | cx, |
| 595 | ); | 595 | ); |
| 596 | } else { | 596 | } else { |
| 597 | - this.act(action, cx); | 597 | + this.act(action, window, cx); |
| 598 | } | 598 | } |
| 599 | })) | 599 | })) |
| 600 | }) | 600 | }) |
crates/app/src/ui/preview.rsmodified+134 -0
| @@ -1,8 +1,80 @@ | |||
| 1 | //! The picture: what is drawn over it and what is read off it. | 1 | //! The picture: what is drawn over it and what is read off it. |
| 2 | 2 | ||
| 3 | use crate::*; | 3 | use crate::*; |
| 4 | +use std::path::{Path, PathBuf}; | ||
| 4 | 5 | ||
| 5 | impl Player { | 6 | impl Player { |
| 7 | + /// The one thing that says a preview is up rather than the timeline: a | ||
| 8 | + /// banner over the top of the picture, and a click of its own -- the | ||
| 9 | + /// keyboard has `esc`, and a hand with only a mouse needs a way out too. | ||
| 10 | + pub(crate) fn preview_badge(&self, cx: &mut Context<Self>) -> Option<impl IntoElement> { | ||
| 11 | + self.preview_session.is_some().then(|| { | ||
| 12 | + div() | ||
| 13 | + .absolute() | ||
| 14 | + .top_0() | ||
| 15 | + .left_0() | ||
| 16 | + .right_0() | ||
| 17 | + .flex() | ||
| 18 | + .items_center() | ||
| 19 | + .justify_center() | ||
| 20 | + .gap(px(8.)) | ||
| 21 | + .p(px(4.)) | ||
| 22 | + .bg(rgba(SUB_SHADE())) | ||
| 23 | + .text_size(px(11.)) | ||
| 24 | + .text_color(rgb(FG_PRIMARY())) | ||
| 25 | + .child("PREVIEW — not on the timeline") | ||
| 26 | + .child( | ||
| 27 | + div() | ||
| 28 | + .id("preview-stop") | ||
| 29 | + .px(px(6.)) | ||
| 30 | + .rounded(px(3.)) | ||
| 31 | + .cursor_pointer() | ||
| 32 | + .bg(rgb(BG_RAISED())) | ||
| 33 | + .hover(|s| s.bg(rgb(BG_HOVER()))) | ||
| 34 | + .on_click(cx.listener(|this, _: &ClickEvent, _, cx| this.close_preview(cx))) | ||
| 35 | + .child("Stop (esc)"), | ||
| 36 | + ) | ||
| 37 | + }) | ||
| 38 | + } | ||
| 39 | + | ||
| 40 | + /// The frame on screen right now, written to a PNG: `image` and its | ||
| 41 | + /// `as_bytes` are gpui's own cached copy of what was last handed to the | ||
| 42 | + /// atlas ([`Player::pump`]), so this reads no decoder and races nothing -- | ||
| 43 | + /// works during preview or on the timeline, whichever is showing. | ||
| 44 | + pub(crate) fn take_screenshot(&mut self, cx: &mut Context<Self>) { | ||
| 45 | + let no_frame = || "NO FRAME TO SAVE — nothing is showing yet".to_string(); | ||
| 46 | + let Some(image) = self.image.clone() else { | ||
| 47 | + self.notify_user(no_frame().into()); | ||
| 48 | + cx.notify(); | ||
| 49 | + return; | ||
| 50 | + }; | ||
| 51 | + let Some(bytes) = image.as_bytes(0) else { | ||
| 52 | + self.notify_user(no_frame().into()); | ||
| 53 | + cx.notify(); | ||
| 54 | + return; | ||
| 55 | + }; | ||
| 56 | + let size = image.size(0); | ||
| 57 | + let stem = self | ||
| 58 | + .active_session() | ||
| 59 | + .and_then(|s| s.sources().first()) | ||
| 60 | + .map_or_else(|| self.name.to_string(), |s| file_name(&s.path)); | ||
| 61 | + let stem = std::path::Path::new(&stem) | ||
| 62 | + .file_stem() | ||
| 63 | + .map_or(stem.clone(), |s| s.to_string_lossy().into_owned()); | ||
| 64 | + let tc = timecode( | ||
| 65 | + self.active_session().map_or(0., PlaybackSession::now), | ||
| 66 | + self.active_fps(), | ||
| 67 | + ) | ||
| 68 | + .replace(':', "-"); | ||
| 69 | + let text = match save_screenshot(bytes, size.width.0 as u32, size.height.0 as u32, &stem, &tc) | ||
| 70 | + { | ||
| 71 | + Ok(path) => format!("SAVED {}", path.display()), | ||
| 72 | + Err(e) => format!("SCREENSHOT FAILED: {e}"), | ||
| 73 | + }; | ||
| 74 | + self.notify_user(text.into()); | ||
| 75 | + cx.notify(); | ||
| 76 | + } | ||
| 77 | + | ||
| 6 | /// The cues the *subtitle lanes* put on screen at `at`, over the picture and | 78 | /// The cues the *subtitle lanes* put on screen at `at`, over the picture and |
| 7 | /// nothing else: bottom-centred where every player puts them, white on a | 79 | /// nothing else: bottom-centred where every player puts them, white on a |
| 8 | /// plate so the film underneath cannot swallow them, and each cue its own | 80 | /// plate so the film underneath cannot swallow them, and each cue its own |
| @@ -201,3 +273,65 @@ impl Player { | |||
| 201 | } | 273 | } |
| 202 | } | 274 | } |
| 203 | } | 275 | } |
| 276 | + | ||
| 277 | +/// `~/Pictures/edith`, or `.` with no `HOME` -- [`keymap::config_path_in`]'s | ||
| 278 | +/// own fallback, for the same reason: a screenshot has to land somewhere even | ||
| 279 | +/// on a machine with no desktop environment set up. | ||
| 280 | +pub(crate) fn screenshots_dir() -> PathBuf { | ||
| 281 | + std::env::var_os("HOME") | ||
| 282 | + .map_or_else(|| PathBuf::from("."), PathBuf::from) | ||
| 283 | + .join("Pictures") | ||
| 284 | + .join("edith") | ||
| 285 | +} | ||
| 286 | + | ||
| 287 | +/// Where a screenshot goes: `stem-tc.png` in `dir`, or the same name with a | ||
| 288 | +/// numeric suffix if that one is already taken -- a screenshot never | ||
| 289 | +/// overwrites another the way an export overwrites its own path. | ||
| 290 | +pub(crate) fn screenshot_path(dir: &Path, stem: &str, tc: &str) -> PathBuf { | ||
| 291 | + let base = format!("{stem}-{tc}"); | ||
| 292 | + let first = dir.join(format!("{base}.png")); | ||
| 293 | + if !first.exists() { | ||
| 294 | + return first; | ||
| 295 | + } | ||
| 296 | + let mut n = 2; | ||
| 297 | + loop { | ||
| 298 | + let path = dir.join(format!("{base}-{n}.png")); | ||
| 299 | + if !path.exists() { | ||
| 300 | + return path; | ||
| 301 | + } | ||
| 302 | + n += 1; | ||
| 303 | + } | ||
| 304 | +} | ||
| 305 | + | ||
| 306 | +/// The swap every BGRA frame needs to become the RGBA a PNG writes: gpui's | ||
| 307 | +/// atlas is BGRA with straight alpha ([`Player::sub_picture`]'s own comment), | ||
| 308 | +/// and `image::save` writes RGBA. | ||
| 309 | +pub(crate) fn bgra_to_rgba(bgra: &[u8]) -> Vec<u8> { | ||
| 310 | + let mut out = bgra.to_vec(); | ||
| 311 | + for pixel in out.chunks_exact_mut(4) { | ||
| 312 | + pixel.swap(0, 2); | ||
| 313 | + } | ||
| 314 | + out | ||
| 315 | +} | ||
| 316 | + | ||
| 317 | +/// Writes `bgra` (`w`x`h`, gpui's own layout) to a fresh PNG under | ||
| 318 | +/// [`screenshots_dir`], creating the directory if this is the first one. | ||
| 319 | +/// Never called on the render thread's own budget for more than a channel | ||
| 320 | +/// swap and one `image::save` -- there is no decode here, only a copy already | ||
| 321 | +/// in memory. | ||
| 322 | +pub(crate) fn save_screenshot( | ||
| 323 | + bgra: &[u8], | ||
| 324 | + w: u32, | ||
| 325 | + h: u32, | ||
| 326 | + stem: &str, | ||
| 327 | + tc: &str, | ||
| 328 | +) -> Result<PathBuf, String> { | ||
| 329 | + let dir = screenshots_dir(); | ||
| 330 | + std::fs::create_dir_all(&dir).map_err(|e| format!("{}: {e}", dir.display()))?; | ||
| 331 | + let path = screenshot_path(&dir, stem, tc); | ||
| 332 | + let rgba = bgra_to_rgba(bgra); | ||
| 333 | + let buf = image::RgbaImage::from_raw(w, h, rgba) | ||
| 334 | + .ok_or_else(|| "frame buffer sized w*h*4".to_string())?; | ||
| 335 | + buf.save(&path).map_err(|e| e.to_string())?; | ||
| 336 | + Ok(path) | ||
| 337 | +} | ||
crates/app/src/ui/toolbar.rsmodified+30 -1
| @@ -49,8 +49,9 @@ impl Player { | |||
| 49 | /// write a file on the right. Export is the one accented button in the | 49 | /// write a file on the right. Export is the one accented button in the |
| 50 | /// window -- the primary action of an editor, where every consumer editor | 50 | /// window -- the primary action of an editor, where every consumer editor |
| 51 | /// puts it -- and it keeps its rect while it is a Cancel. | 51 | /// puts it -- and it keeps its rect while it is a Cancel. |
| 52 | - pub(crate) fn topbar(&self, cx: &mut Context<Self>) -> impl IntoElement { | 52 | + pub(crate) fn topbar(&self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement { |
| 53 | let exporting = self.exporting().is_some(); | 53 | let exporting = self.exporting().is_some(); |
| 54 | + let fullscreen = window.is_fullscreen(); | ||
| 54 | div() | 55 | div() |
| 55 | .flex_none() | 56 | .flex_none() |
| 56 | .h(px(TOPBAR_H)) | 57 | .h(px(TOPBAR_H)) |
| @@ -76,6 +77,24 @@ impl Player { | |||
| 76 | .truncate() | 77 | .truncate() |
| 77 | .child(self.name.clone()), | 78 | .child(self.name.clone()), |
| 78 | ) | 79 | ) |
| 80 | + // The window's own control, in the fixed rect the mute button uses | ||
| 81 | + // for the same reason (toolbar.rs:194-203): fullscreen swaps the | ||
| 82 | + // word and the colour, never the box. | ||
| 83 | + .child(self.action_control( | ||
| 84 | + "fullscreen", | ||
| 85 | + 0., | ||
| 86 | + BG_RAISED(), | ||
| 87 | + None, | ||
| 88 | + match fullscreen { | ||
| 89 | + true => "Exit fullscreen", | ||
| 90 | + false => "Fullscreen", | ||
| 91 | + }, | ||
| 92 | + "fills the screen with the window, or gives it back", | ||
| 93 | + ActionId::Fullscreen, | ||
| 94 | + cx.listener(|this, _: &ClickEvent, window, cx| { | ||
| 95 | + this.act(ActionId::Fullscreen, window, cx) | ||
| 96 | + }), | ||
| 97 | + )) | ||
| 79 | .child(self.action_control( | 98 | .child(self.action_control( |
| 80 | "save", | 99 | "save", |
| 81 | 0., | 100 | 0., |
| @@ -159,6 +178,16 @@ impl Player { | |||
| 159 | .bg(rgb(BG_PANEL())) | 178 | .bg(rgb(BG_PANEL())) |
| 160 | .border_t_1() | 179 | .border_t_1() |
| 161 | .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 | + )) | ||
| 162 | .child(self.action_control( | 191 | .child(self.action_control( |
| 163 | "transport", | 192 | "transport", |
| 164 | 40., | 193 | 40., |