Editor-shell UI — transport, timecode, playhead ruler
Browse files at this commit1c1f5e0152a695d21054a3b513613002899340b8Parent: 752c6a0- feat
- editor-shell UI — transport, timecode, playhead ruler
1 changed file +221 -11
- crates/app/src/main.rs +221 -11
crates/app/src/main.rsmodified+221 -11
| @@ -4,15 +4,37 @@ use std::time::Instant; | |||
| 4 | 4 | ||
| 5 | use engine::{Frame, PlaybackSession}; | 5 | use engine::{Frame, PlaybackSession}; |
| 6 | use gpui::{ | 6 | use gpui::{ |
| 7 | - App, Application, Bounds, Context, FocusHandle, KeyDownEvent, RenderImage, TitlebarOptions, | 7 | + App, Application, Bounds, Context, FocusHandle, KeyDownEvent, MouseButton, MouseDownEvent, |
| 8 | - Window, WindowBounds, WindowOptions, div, img, prelude::*, px, rgb, size, | 8 | + PathBuilder, RenderImage, SharedString, TitlebarOptions, Window, WindowBounds, WindowOptions, |
| 9 | + canvas, div, img, point, prelude::*, px, relative, rgb, size, | ||
| 9 | }; | 10 | }; |
| 10 | 11 | ||
| 12 | +/// Editor chrome: three grays and one accent, all darker than the picture so the | ||
| 13 | +/// frame is what the eye lands on. `LETTERBOX` stays the video's own bed. | ||
| 14 | +const LETTERBOX: u32 = 0x101010; | ||
| 15 | +const CHROME: u32 = 0x242424; | ||
| 16 | +const SURFACE: u32 = 0x333333; | ||
| 17 | +const INK: u32 = 0xc8c8c8; | ||
| 18 | +const ACCENT: u32 = 0x4a9eff; | ||
| 19 | + | ||
| 20 | +/// Fixed so the video region takes every pixel the window gains and the controls | ||
| 21 | +/// never clip at 640x360. | ||
| 22 | +const HEADER_H: f32 = 32.; | ||
| 23 | +const PANEL_H: f32 = 180.; | ||
| 24 | +const LANE_H: f32 = 48.; | ||
| 25 | +/// Wide enough for `HH:MM:SS:FF / HH:MM:SS:FF`, and fixed so changing digits | ||
| 26 | +/// cannot push the layout around. | ||
| 27 | +const TIME_W: f32 = 200.; | ||
| 28 | + | ||
| 11 | struct Player { | 29 | struct Player { |
| 12 | session: PlaybackSession, | 30 | session: PlaybackSession, |
| 13 | /// Timeline seconds -> frame index, so the clock can be compared to what | 31 | /// Timeline seconds -> frame index, so the clock can be compared to what |
| 14 | /// the decoder hands over. | 32 | /// the decoder hands over. |
| 15 | fps: f64, | 33 | fps: f64, |
| 34 | + /// `frame_count / frame_rate`: the timeline's end, and the clamp for a clock | ||
| 35 | + /// that keeps running past it. | ||
| 36 | + duration: f64, | ||
| 37 | + name: SharedString, | ||
| 16 | image: Option<Arc<RenderImage>>, | 38 | image: Option<Arc<RenderImage>>, |
| 17 | /// A frame that arrived before its time; shown on the tick it comes due. | 39 | /// A frame that arrived before its time; shown on the tick it comes due. |
| 18 | held: Option<Frame>, | 40 | held: Option<Frame>, |
| @@ -96,29 +118,211 @@ impl Render for Player { | |||
| 96 | } | 118 | } |
| 97 | self.session.tick(); | 119 | self.session.tick(); |
| 98 | self.pump(window); | 120 | self.pump(window); |
| 99 | - if !self.done { | 121 | + // No shadow flag: the clock is the only truth about play state. |
| 122 | + let playing = self.session.is_playing(); | ||
| 123 | + // A paused timeline has nothing to animate; the toggle handlers notify, | ||
| 124 | + // which is what starts the loop again. | ||
| 125 | + if playing && !self.done { | ||
| 100 | window.request_animation_frame(); | 126 | window.request_animation_frame(); |
| 101 | } | 127 | } |
| 102 | 128 | ||
| 129 | + // The clock keeps running after the last frame (wall time takes over at | ||
| 130 | + // audio EOF) while the picture is frozen, so the timeline the UI shows is | ||
| 131 | + // the clamped one, pinned to the out-point once playback is done. | ||
| 132 | + let position = if self.done { | ||
| 133 | + self.duration | ||
| 134 | + } else { | ||
| 135 | + self.session.now().clamp(0., self.duration) | ||
| 136 | + }; | ||
| 137 | + | ||
| 103 | div() | 138 | div() |
| 104 | .track_focus(&self.focus) | 139 | .track_focus(&self.focus) |
| 105 | - .on_key_down(cx.listener(|this, event: &KeyDownEvent, _, _| { | 140 | + .on_key_down(cx.listener(|this, event: &KeyDownEvent, _, cx| { |
| 106 | // `is_held` filters auto-repeat, which would otherwise toggle | 141 | // `is_held` filters auto-repeat, which would otherwise toggle |
| 107 | // playback many times a second. | 142 | // playback many times a second. |
| 108 | if event.keystroke.key == "space" && !event.is_held { | 143 | if event.keystroke.key == "space" && !event.is_held { |
| 109 | this.session.toggle(); | 144 | this.session.toggle(); |
| 145 | + // Past EOF nothing else asks for a repaint. | ||
| 146 | + cx.notify(); | ||
| 110 | } | 147 | } |
| 111 | })) | 148 | })) |
| 112 | .size_full() | 149 | .size_full() |
| 113 | - .bg(rgb(0x101010)) | ||
| 114 | .flex() | 150 | .flex() |
| 115 | - .justify_center() | 151 | + .flex_col() |
| 116 | - .items_center() | 152 | + .bg(rgb(LETTERBOX)) |
| 117 | - .children( | 153 | + .text_color(rgb(INK)) |
| 118 | - self.image | 154 | + .text_size(px(13.)) |
| 119 | - .clone() | 155 | + .child( |
| 120 | - .map(|i| img(i).size_full().object_fit(gpui::ObjectFit::Contain)), | 156 | + div() |
| 157 | + .flex_none() | ||
| 158 | + .h(px(HEADER_H)) | ||
| 159 | + .flex() | ||
| 160 | + .items_center() | ||
| 161 | + .px(px(12.)) | ||
| 162 | + .bg(rgb(CHROME)) | ||
| 163 | + .child(self.name.clone()), | ||
| 164 | + ) | ||
| 165 | + .child( | ||
| 166 | + div() | ||
| 167 | + .flex_1() | ||
| 168 | + .min_h(px(0.)) | ||
| 169 | + .overflow_hidden() | ||
| 170 | + .flex() | ||
| 171 | + .justify_center() | ||
| 172 | + .items_center() | ||
| 173 | + .children( | ||
| 174 | + self.image | ||
| 175 | + .clone() | ||
| 176 | + .map(|i| img(i).size_full().object_fit(gpui::ObjectFit::Contain)), | ||
| 177 | + ), | ||
| 178 | + ) | ||
| 179 | + .child(self.panel(position, playing, cx)) | ||
| 180 | + } | ||
| 181 | +} | ||
| 182 | + | ||
| 183 | +impl Player { | ||
| 184 | + /// Transport, timecode, playhead and the (still empty) track lanes. | ||
| 185 | + fn panel(&self, position: f64, playing: bool, cx: &mut Context<Self>) -> impl IntoElement { | ||
| 186 | + let filled = if self.duration > 0. { | ||
| 187 | + (position / self.duration) as f32 | ||
| 188 | + } else { | ||
| 189 | + 0. | ||
| 190 | + }; | ||
| 191 | + div() | ||
| 192 | + .flex_none() | ||
| 193 | + .h(px(PANEL_H)) | ||
| 194 | + .flex() | ||
| 195 | + .flex_col() | ||
| 196 | + .gap(px(8.)) | ||
| 197 | + .px(px(12.)) | ||
| 198 | + .py(px(8.)) | ||
| 199 | + .bg(rgb(CHROME)) | ||
| 200 | + .child( | ||
| 201 | + div() | ||
| 202 | + .flex() | ||
| 203 | + .items_center() | ||
| 204 | + .gap(px(12.)) | ||
| 205 | + .child( | ||
| 206 | + // A plain div: nothing here tracks focus, so the root's | ||
| 207 | + // own mouse-down handler is what wins the click and the | ||
| 208 | + // key listener keeps working after a press. | ||
| 209 | + div() | ||
| 210 | + .w(px(32.)) | ||
| 211 | + .h(px(28.)) | ||
| 212 | + .flex() | ||
| 213 | + .justify_center() | ||
| 214 | + .items_center() | ||
| 215 | + .rounded(px(3.)) | ||
| 216 | + .bg(rgb(SURFACE)) | ||
| 217 | + .on_mouse_down( | ||
| 218 | + MouseButton::Left, | ||
| 219 | + cx.listener(|this, _: &MouseDownEvent, _, cx| { | ||
| 220 | + this.session.toggle(); | ||
| 221 | + cx.notify(); | ||
| 222 | + }), | ||
| 223 | + ) | ||
| 224 | + .child(transport_glyph(playing)), | ||
| 225 | + ) | ||
| 226 | + .child( | ||
| 227 | + div().flex_none().w(px(TIME_W)).child(format!( | ||
| 228 | + "{} / {}", | ||
| 229 | + timecode(position, self.fps), | ||
| 230 | + timecode(self.duration, self.fps) | ||
| 231 | + )), | ||
| 232 | + ), | ||
| 233 | + ) | ||
| 234 | + // Read-only: no cursor, no hover, no listener -- a clip slice turns | ||
| 235 | + // this into a scrubber, today it only reports. | ||
| 236 | + .child( | ||
| 237 | + div() | ||
| 238 | + .flex_none() | ||
| 239 | + .h(px(6.)) | ||
| 240 | + .rounded(px(3.)) | ||
| 241 | + .bg(rgb(SURFACE)) | ||
| 242 | + .child( | ||
| 243 | + div() | ||
| 244 | + .h_full() | ||
| 245 | + .w(relative(filled)) | ||
| 246 | + .rounded(px(3.)) | ||
| 247 | + .bg(rgb(ACCENT)), | ||
| 248 | + ), | ||
| 121 | ) | 249 | ) |
| 250 | + .child(track_lane()) | ||
| 251 | + .child(track_lane()) | ||
| 252 | + } | ||
| 253 | +} | ||
| 254 | + | ||
| 255 | +/// Placeholder chrome for the clips slice: deliberately empty. | ||
| 256 | +fn track_lane() -> impl IntoElement { | ||
| 257 | + div() | ||
| 258 | + .flex_none() | ||
| 259 | + .h(px(LANE_H)) | ||
| 260 | + .rounded(px(3.)) | ||
| 261 | + .bg(rgb(SURFACE)) | ||
| 262 | +} | ||
| 263 | + | ||
| 264 | +/// Two bars while playing, a triangle while paused. Drawn, so there is no icon | ||
| 265 | +/// font and no glyph coverage to depend on. | ||
| 266 | +fn transport_glyph(playing: bool) -> impl IntoElement { | ||
| 267 | + div() | ||
| 268 | + .flex() | ||
| 269 | + .items_center() | ||
| 270 | + .gap(px(4.)) | ||
| 271 | + .when(playing, |d| { | ||
| 272 | + d.child(div().w(px(3.)).h(px(12.)).bg(rgb(INK))) | ||
| 273 | + .child(div().w(px(3.)).h(px(12.)).bg(rgb(INK))) | ||
| 274 | + }) | ||
| 275 | + .when(!playing, |d| { | ||
| 276 | + d.child( | ||
| 277 | + canvas( | ||
| 278 | + |_, _, _| (), | ||
| 279 | + |bounds, _, window, _| { | ||
| 280 | + let (o, s) = (bounds.origin, bounds.size); | ||
| 281 | + let mut path = PathBuilder::fill(); | ||
| 282 | + path.move_to(o); | ||
| 283 | + path.line_to(point(o.x + s.width, o.y + s.height / 2.)); | ||
| 284 | + path.line_to(point(o.x, o.y + s.height)); | ||
| 285 | + path.close(); | ||
| 286 | + if let Ok(path) = path.build() { | ||
| 287 | + window.paint_path(path, rgb(INK)); | ||
| 288 | + } | ||
| 289 | + }, | ||
| 290 | + ) | ||
| 291 | + .w(px(11.)) | ||
| 292 | + .h(px(13.)), | ||
| 293 | + ) | ||
| 294 | + }) | ||
| 295 | +} | ||
| 296 | + | ||
| 297 | +/// NLE timecode: `HH:MM:SS:FF`, the frame counted inside its own second. | ||
| 298 | +fn timecode(t: f64, fps: f64) -> String { | ||
| 299 | + let t = t.max(0.); | ||
| 300 | + let secs = t as u64; | ||
| 301 | + let last = (fps.ceil() as u64).saturating_sub(1); | ||
| 302 | + let frame = ((t - secs as f64) * fps) as u64; | ||
| 303 | + format!( | ||
| 304 | + "{:02}:{:02}:{:02}:{:02}", | ||
| 305 | + secs / 3600, | ||
| 306 | + secs / 60 % 60, | ||
| 307 | + secs % 60, | ||
| 308 | + frame.min(last) | ||
| 309 | + ) | ||
| 310 | +} | ||
| 311 | + | ||
| 312 | +#[cfg(test)] | ||
| 313 | +mod tests { | ||
| 314 | + use super::timecode; | ||
| 315 | + | ||
| 316 | + #[test] | ||
| 317 | + fn timecode_counts_frames_inside_the_second() { | ||
| 318 | + assert_eq!(timecode(0., 30.), "00:00:00:00"); | ||
| 319 | + assert_eq!(timecode(-1., 30.), "00:00:00:00"); // clamped, never negative | ||
| 320 | + assert_eq!(timecode(4.9667, 30.), "00:00:04:29"); // last frame of a 5 s clip | ||
| 321 | + assert_eq!(timecode(5., 30.), "00:00:05:00"); | ||
| 322 | + assert_eq!(timecode(3661.5, 30.), "01:01:01:15"); | ||
| 323 | + // Rounding must not spill into a frame the second does not have. | ||
| 324 | + assert_eq!(timecode(1. - f64::EPSILON, 30.), "00:00:00:29"); | ||
| 325 | + assert_eq!(timecode(0.999, 29.97), "00:00:00:29"); | ||
| 122 | } | 326 | } |
| 123 | } | 327 | } |
| 124 | 328 | ||
| @@ -135,6 +339,10 @@ fn main() { | |||
| 135 | } | 339 | } |
| 136 | }; | 340 | }; |
| 137 | let meta = *session.meta(); | 341 | let meta = *session.meta(); |
| 342 | + let name: SharedString = std::path::Path::new(&path) | ||
| 343 | + .file_name() | ||
| 344 | + .map_or_else(|| path.clone(), |n| n.to_string_lossy().into_owned()) | ||
| 345 | + .into(); | ||
| 138 | println!( | 346 | println!( |
| 139 | "{path}: {}x{} @ {:.2} fps, {} samples", | 347 | "{path}: {}x{} @ {:.2} fps, {} samples", |
| 140 | meta.width, meta.height, meta.frame_rate, meta.frame_count | 348 | meta.width, meta.height, meta.frame_rate, meta.frame_count |
| @@ -159,6 +367,8 @@ fn main() { | |||
| 159 | let player = cx.new(|cx| Player { | 367 | let player = cx.new(|cx| Player { |
| 160 | session, | 368 | session, |
| 161 | fps: meta.frame_rate, | 369 | fps: meta.frame_rate, |
| 370 | + duration: f64::from(meta.frame_count) / meta.frame_rate, | ||
| 371 | + name: name.clone(), | ||
| 162 | image: None, | 372 | image: None, |
| 163 | held: None, | 373 | held: None, |
| 164 | eos: false, | 374 | eos: false, |