1use crate::capabilities::foundation::theme::use_theme;
2use gpui::{prelude::FluentBuilder as _, *};
3use std::rc::Rc;
4use std::time::{Duration, Instant};
5
6type WindowCallback = Rc<dyn Fn(&mut Window, &mut App) + 'static>;
7type ValueCallback<T> = Rc<dyn Fn(T, &mut Window, &mut App) + 'static>;
8
9#[derive(Clone, Copy, Debug, PartialEq, Default)]
10pub enum VideoPlaybackState {
11 #[default]
12 Stopped,
13 Playing,
14 Paused,
15 Buffering,
16}
17
18#[derive(Clone, Copy, Debug, PartialEq, Default)]
19pub enum VideoPlayerSize {
20 Sm,
21 #[default]
22 Md,
23 Lg,
24 Full,
25}
26
27impl VideoPlayerSize {
28 pub fn dimensions(&self) -> (Pixels, Pixels) {
29 match self {
30 Self::Sm => (px(400.0), px(225.0)),
31 Self::Md => (px(640.0), px(360.0)),
32 Self::Lg => (px(854.0), px(480.0)),
33 Self::Full => (px(1280.0), px(720.0)),
34 }
35 }
36
37 pub fn controls_height(&self) -> Pixels {
38 match self {
39 Self::Sm => px(36.0),
40 Self::Md => px(44.0),
41 Self::Lg => px(52.0),
42 Self::Full => px(56.0),
43 }
44 }
45
46 pub fn icon_size(&self) -> Pixels {
47 match self {
48 Self::Sm => px(16.0),
49 Self::Md => px(20.0),
50 Self::Lg => px(24.0),
51 Self::Full => px(28.0),
52 }
53 }
54}
55
56#[derive(Clone, Copy, Debug, PartialEq, Default)]
57pub enum VideoPlaybackSpeed {
58 Quarter,
59 Half,
60 ThreeQuarter,
61 #[default]
62 Normal,
63 OneAndQuarter,
64 OneAndHalf,
65 Double,
66}
67
68impl VideoPlaybackSpeed {
69 pub fn multiplier(&self) -> f32 {
70 match self {
71 Self::Quarter => 0.25,
72 Self::Half => 0.5,
73 Self::ThreeQuarter => 0.75,
74 Self::Normal => 1.0,
75 Self::OneAndQuarter => 1.25,
76 Self::OneAndHalf => 1.5,
77 Self::Double => 2.0,
78 }
79 }
80
81 pub fn label(&self) -> &'static str {
82 match self {
83 Self::Quarter => "0.25x",
84 Self::Half => "0.5x",
85 Self::ThreeQuarter => "0.75x",
86 Self::Normal => "1x",
87 Self::OneAndQuarter => "1.25x",
88 Self::OneAndHalf => "1.5x",
89 Self::Double => "2x",
90 }
91 }
92
93 pub fn next(&self) -> Self {
94 match self {
95 Self::Quarter => Self::Half,
96 Self::Half => Self::ThreeQuarter,
97 Self::ThreeQuarter => Self::Normal,
98 Self::Normal => Self::OneAndQuarter,
99 Self::OneAndQuarter => Self::OneAndHalf,
100 Self::OneAndHalf => Self::Double,
101 Self::Double => Self::Quarter,
102 }
103 }
104
105 pub fn all() -> &'static [VideoPlaybackSpeed] {
106 &[
107 Self::Quarter,
108 Self::Half,
109 Self::ThreeQuarter,
110 Self::Normal,
111 Self::OneAndQuarter,
112 Self::OneAndHalf,
113 Self::Double,
114 ]
115 }
116}
117
118pub struct VideoPlayerState {
119 playback_state: VideoPlaybackState,
120 current_time: f64,
121 duration: f64,
122 volume: f32,
123 is_muted: bool,
124 previous_volume: f32,
125 playback_speed: VideoPlaybackSpeed,
126 is_fullscreen: bool,
127 show_controls: bool,
128 last_interaction: Instant,
129 controls_timeout: Duration,
130 auto_hide_generation: u64,
131 auto_hide_task: Option<Task<()>>,
132 is_seeking: bool,
133 progress_bounds: Bounds<Pixels>,
134 volume_bounds: Bounds<Pixels>,
135 is_volume_dragging: bool,
136 show_speed_menu: bool,
137 focus_handle: FocusHandle,
138 current_frame: Option<SharedString>,
139 video_title: Option<SharedString>,
140}
141
142impl VideoPlayerState {
143 pub fn new(cx: &mut Context<Self>) -> Self {
144 Self {
145 playback_state: VideoPlaybackState::Stopped,
146 current_time: 0.0,
147 duration: 0.0,
148 volume: 1.0,
149 is_muted: false,
150 previous_volume: 1.0,
151 playback_speed: VideoPlaybackSpeed::Normal,
152 is_fullscreen: false,
153 show_controls: true,
154 last_interaction: Instant::now(),
155 controls_timeout: Duration::from_secs(3),
156 auto_hide_generation: 0,
157 auto_hide_task: None,
158 is_seeking: false,
159 progress_bounds: Bounds::default(),
160 volume_bounds: Bounds::default(),
161 is_volume_dragging: false,
162 show_speed_menu: false,
163 focus_handle: cx.focus_handle(),
164 current_frame: None,
165 video_title: None,
166 }
167 }
168
169 pub fn set_frame(&mut self, frame_path: impl Into<SharedString>, cx: &mut Context<Self>) {
170 self.current_frame = Some(frame_path.into());
171 cx.notify();
172 }
173
174 pub fn clear_frame(&mut self, cx: &mut Context<Self>) {
175 self.current_frame = None;
176 cx.notify();
177 }
178
179 pub fn current_frame(&self) -> Option<&SharedString> {
180 self.current_frame.as_ref()
181 }
182
183 pub fn set_title(&mut self, title: impl Into<SharedString>, cx: &mut Context<Self>) {
184 self.video_title = Some(title.into());
185 cx.notify();
186 }
187
188 pub fn title(&self) -> Option<&SharedString> {
189 self.video_title.as_ref()
190 }
191
192 pub fn playback_state(&self) -> VideoPlaybackState {
193 self.playback_state
194 }
195
196 pub fn is_playing(&self) -> bool {
197 self.playback_state == VideoPlaybackState::Playing
198 }
199
200 pub fn play(&mut self, cx: &mut Context<Self>) {
201 self.playback_state = VideoPlaybackState::Playing;
202 self.touch_controls(cx);
203 cx.notify();
204 }
205
206 pub fn pause(&mut self, cx: &mut Context<Self>) {
207 self.playback_state = VideoPlaybackState::Paused;
208 self.show_controls = true;
209 self.cancel_auto_hide();
210 cx.notify();
211 }
212
213 pub fn stop(&mut self, cx: &mut Context<Self>) {
214 self.playback_state = VideoPlaybackState::Stopped;
215 self.current_time = 0.0;
216 self.show_controls = true;
217 self.cancel_auto_hide();
218 cx.notify();
219 }
220
221 pub fn toggle_play(&mut self, cx: &mut Context<Self>) {
222 match self.playback_state {
223 VideoPlaybackState::Playing => self.pause(cx),
224 VideoPlaybackState::Paused | VideoPlaybackState::Stopped => self.play(cx),
225 VideoPlaybackState::Buffering => {}
226 }
227 }
228
229 pub fn current_time(&self) -> f64 {
230 self.current_time
231 }
232
233 pub fn set_current_time(&mut self, time: f64, cx: &mut Context<Self>) {
234 self.current_time = time.clamp(0.0, self.duration);
235 cx.notify();
236 }
237
238 pub fn duration(&self) -> f64 {
239 self.duration
240 }
241
242 pub fn set_duration(&mut self, duration: f64, cx: &mut Context<Self>) {
243 self.duration = duration.max(0.0);
244 cx.notify();
245 }
246
247 pub fn progress(&self) -> f64 {
248 if self.duration <= 0.0 {
249 return 0.0;
250 }
251 (self.current_time / self.duration).clamp(0.0, 1.0)
252 }
253
254 pub fn seek(&mut self, position: f64, cx: &mut Context<Self>) {
255 let clamped = position.clamp(0.0, 1.0);
256 self.current_time = clamped * self.duration;
257 self.touch_controls(cx);
258 cx.notify();
259 }
260
261 pub fn seek_relative(&mut self, delta: f64, cx: &mut Context<Self>) {
262 let new_time = (self.current_time + delta).clamp(0.0, self.duration);
263 self.current_time = new_time;
264 self.touch_controls(cx);
265 cx.notify();
266 }
267
268 pub fn volume(&self) -> f32 {
269 self.volume
270 }
271
272 pub fn effective_volume(&self) -> f32 {
273 if self.is_muted {
274 0.0
275 } else {
276 self.volume
277 }
278 }
279
280 pub fn set_volume(&mut self, volume: f32, cx: &mut Context<Self>) {
281 self.volume = volume.clamp(0.0, 1.0);
282 if self.volume > 0.0 {
283 self.is_muted = false;
284 }
285 self.touch_controls(cx);
286 cx.notify();
287 }
288
289 pub fn is_muted(&self) -> bool {
290 self.is_muted
291 }
292
293 pub fn toggle_mute(&mut self, cx: &mut Context<Self>) {
294 if self.is_muted {
295 self.is_muted = false;
296 if self.volume == 0.0 {
297 self.volume = self.previous_volume;
298 }
299 } else {
300 self.previous_volume = self.volume;
301 self.is_muted = true;
302 }
303 self.touch_controls(cx);
304 cx.notify();
305 }
306
307 pub fn playback_speed(&self) -> VideoPlaybackSpeed {
308 self.playback_speed
309 }
310
311 pub fn set_playback_speed(&mut self, speed: VideoPlaybackSpeed, cx: &mut Context<Self>) {
312 self.playback_speed = speed;
313 self.show_speed_menu = false;
314 self.touch_controls(cx);
315 cx.notify();
316 }
317
318 pub fn cycle_playback_speed(&mut self, cx: &mut Context<Self>) {
319 self.playback_speed = self.playback_speed.next();
320 self.touch_controls(cx);
321 cx.notify();
322 }
323
324 pub fn is_fullscreen(&self) -> bool {
325 self.is_fullscreen
326 }
327
328 pub fn toggle_fullscreen(&mut self, window: &Window, cx: &mut Context<Self>) {
329 self.is_fullscreen = !self.is_fullscreen;
330 window.toggle_fullscreen();
331 self.touch_controls(cx);
332 cx.notify();
333 }
334
335 pub fn show_controls(&self) -> bool {
336 self.show_controls
337 }
338
339 pub fn touch_controls(&mut self, cx: &mut Context<Self>) {
340 self.show_controls = true;
341 self.last_interaction = Instant::now();
342 self.auto_hide_generation = self.auto_hide_generation.wrapping_add(1);
343 let generation = self.auto_hide_generation;
344 let timeout = self.controls_timeout;
345 self.auto_hide_task = Some(cx.spawn(async move |this, cx| {
346 cx.background_executor().timer(timeout).await;
347 _ = this.update(cx, |state, cx| {
348 if state.auto_hide_generation == generation {
349 state.hide_controls(cx);
350 }
351 });
352 }));
353 cx.notify();
354 }
355
356 fn cancel_auto_hide(&mut self) {
357 self.auto_hide_generation = self.auto_hide_generation.wrapping_add(1);
358 self.auto_hide_task = None;
359 }
360
361 pub fn hide_controls(&mut self, cx: &mut Context<Self>) {
362 if self.playback_state == VideoPlaybackState::Playing
363 && !self.is_seeking
364 && !self.is_volume_dragging
365 && self.show_controls
366 {
367 self.show_controls = false;
368 cx.notify();
369 }
370 }
371
372 pub fn check_auto_hide(&mut self, cx: &mut Context<Self>) {
373 if self.last_interaction.elapsed() > self.controls_timeout {
374 self.hide_controls(cx);
375 }
376 }
377
378 pub fn toggle_speed_menu(&mut self, cx: &mut Context<Self>) {
379 self.show_speed_menu = !self.show_speed_menu;
380 self.touch_controls(cx);
381 cx.notify();
382 }
383
384 fn update_seek_from_position(&mut self, position: Point<Pixels>, cx: &mut Context<Self>) {
385 let track_width = self.progress_bounds.size.width;
386 if track_width <= px(0.0) {
387 return;
388 }
389
390 let relative_x = (position.x - self.progress_bounds.left()).clamp(px(0.0), track_width);
391 let percentage = (relative_x / track_width).clamp(0.0, 1.0);
392 self.seek(percentage as f64, cx);
393 }
394
395 fn update_volume_from_position(&mut self, position: Point<Pixels>, cx: &mut Context<Self>) {
396 let track_width = self.volume_bounds.size.width;
397 if track_width <= px(0.0) {
398 return;
399 }
400
401 let relative_x = (position.x - self.volume_bounds.left()).clamp(px(0.0), track_width);
402 let percentage = (relative_x / track_width).clamp(0.0, 1.0);
403 self.set_volume(percentage as f32, cx);
404 }
405}
406
407impl Focusable for VideoPlayerState {
408 fn focus_handle(&self, _: &App) -> FocusHandle {
409 self.focus_handle.clone()
410 }
411}
412
413impl Render for VideoPlayerState {
414 fn render(&mut self, _: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement {
415 div()
416 }
417}
418
419actions!(
420 video_player,
421 [
422 VideoPlayerTogglePlay,
423 VideoPlayerMute,
424 VideoPlayerFullscreen,
425 VideoPlayerSeekForward,
426 VideoPlayerSeekBackward,
427 VideoPlayerVolumeUp,
428 VideoPlayerVolumeDown,
429 ]
430);
431
432pub fn init_video_player(cx: &mut App) {
433 if !crate::capabilities::foundation::initialization::begin(cx, "video-player") {
434 return;
435 }
436 cx.bind_keys([
437 KeyBinding::new("space", VideoPlayerTogglePlay, Some("VideoPlayer")),
438 KeyBinding::new("m", VideoPlayerMute, Some("VideoPlayer")),
439 KeyBinding::new("f", VideoPlayerFullscreen, Some("VideoPlayer")),
440 KeyBinding::new("right", VideoPlayerSeekForward, Some("VideoPlayer")),
441 KeyBinding::new("left", VideoPlayerSeekBackward, Some("VideoPlayer")),
442 KeyBinding::new("up", VideoPlayerVolumeUp, Some("VideoPlayer")),
443 KeyBinding::new("down", VideoPlayerVolumeDown, Some("VideoPlayer")),
444 ]);
445}
446
447use crate::capabilities::media::format_time;
448
449#[derive(IntoElement)]
450pub struct VideoPlayer {
451 state: Entity<VideoPlayerState>,
452 size: VideoPlayerSize,
453 poster: Option<SharedString>,
454 show_poster: bool,
455 overlay_only: bool,
456 on_play: Option<WindowCallback>,
457 on_pause: Option<WindowCallback>,
458 on_seek: Option<ValueCallback<f64>>,
459 on_volume_change: Option<ValueCallback<f32>>,
460 on_fullscreen: Option<ValueCallback<bool>>,
461 on_playback_speed_change: Option<ValueCallback<VideoPlaybackSpeed>>,
462 style: StyleRefinement,
463}
464
465impl VideoPlayer {
466 pub fn new(state: Entity<VideoPlayerState>) -> Self {
467 Self {
468 state,
469 size: VideoPlayerSize::default(),
470 poster: None,
471 show_poster: true,
472 overlay_only: false,
473 on_play: None,
474 on_pause: None,
475 on_seek: None,
476 on_volume_change: None,
477 on_fullscreen: None,
478 on_playback_speed_change: None,
479 style: StyleRefinement::default(),
480 }
481 }
482
483 pub fn size(mut self, size: VideoPlayerSize) -> Self {
484 self.size = size;
485 self
486 }
487
488 pub fn poster(mut self, poster: impl Into<SharedString>) -> Self {
489 self.poster = Some(poster.into());
490 self
491 }
492
493 pub fn show_poster(mut self, show: bool) -> Self {
494 self.show_poster = show;
495 self
496 }
497
498 pub fn overlay_only(mut self) -> Self {
499 self.overlay_only = true;
500 self
501 }
502
503 pub fn on_play(mut self, handler: impl Fn(&mut Window, &mut App) + 'static) -> Self {
504 self.on_play = Some(Rc::new(handler));
505 self
506 }
507
508 pub fn on_pause(mut self, handler: impl Fn(&mut Window, &mut App) + 'static) -> Self {
509 self.on_pause = Some(Rc::new(handler));
510 self
511 }
512
513 pub fn on_seek(mut self, handler: impl Fn(f64, &mut Window, &mut App) + 'static) -> Self {
514 self.on_seek = Some(Rc::new(handler));
515 self
516 }
517
518 pub fn on_volume_change(
519 mut self,
520 handler: impl Fn(f32, &mut Window, &mut App) + 'static,
521 ) -> Self {
522 self.on_volume_change = Some(Rc::new(handler));
523 self
524 }
525
526 pub fn on_fullscreen(
527 mut self,
528 handler: impl Fn(bool, &mut Window, &mut App) + 'static,
529 ) -> Self {
530 self.on_fullscreen = Some(Rc::new(handler));
531 self
532 }
533
534 pub fn on_playback_speed_change(
535 mut self,
536 handler: impl Fn(VideoPlaybackSpeed, &mut Window, &mut App) + 'static,
537 ) -> Self {
538 self.on_playback_speed_change = Some(Rc::new(handler));
539 self
540 }
541}
542
543impl Styled for VideoPlayer {
544 fn style(&mut self) -> &mut StyleRefinement {
545 &mut self.style
546 }
547}
548
549impl RenderOnce for VideoPlayer {
550 fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
551 let theme = use_theme(cx);
552 let state = self.state.read(cx);
553 let focus_handle = state.focus_handle(cx);
554
555 let playback_state = state.playback_state();
556 let is_playing = state.is_playing();
557 let current_time = state.current_time();
558 let duration = state.duration();
559 let progress = state.progress();
560 let volume = state.volume();
561 let is_muted = state.is_muted();
562 let playback_speed = state.playback_speed();
563 let is_fullscreen = state.is_fullscreen();
564 let show_controls = state.show_controls();
565 let show_speed_menu = state.show_speed_menu;
566
567 let (width, height) = self.size.dimensions();
568 let controls_height = self.size.controls_height();
569 let icon_size = self.size.icon_size();
570
571 let show_poster = self.show_poster
572 && self.poster.is_some()
573 && playback_state == VideoPlaybackState::Stopped;
574
575 let current_frame = state.current_frame().cloned();
576 let overlay_only = self.overlay_only;
577
578 let user_style = self.style;
579
580 let volume_icon = if is_muted || volume == 0.0 {
581 "volume-x"
582 } else if volume < 0.5 {
583 "volume-1"
584 } else {
585 "volume-2"
586 };
587
588 let play_icon = if is_playing { "pause" } else { "play" };
589
590 let state_entity = self.state.clone();
591 let state_for_actions = self.state.clone();
592 let state_for_mouse = self.state.clone();
593
594 let on_play = self.on_play.clone();
595 let on_pause = self.on_pause.clone();
596 let on_seek = self.on_seek.clone();
597 let on_volume_change = self.on_volume_change.clone();
598 let on_fullscreen = self.on_fullscreen.clone();
599 let on_playback_speed_change = self.on_playback_speed_change.clone();
600
601 div()
602 .id("video-player")
603 .key_context("VideoPlayer")
604 .track_focus(&focus_handle)
605 .relative()
606 .w(width)
607 .h(height)
608 .bg(gpui::black())
609 .rounded(theme.tokens.radius_lg)
610 .overflow_hidden()
611 .cursor(CursorStyle::Arrow)
612 .map(|this| {
613 let mut div = this;
614 div.style().refine(&user_style);
615 div
616 })
617 .on_action({
618 let state = state_for_actions.clone();
619 let on_play = on_play.clone();
620 let on_pause = on_pause.clone();
621 move |_: &VideoPlayerTogglePlay, window, cx| {
622 let is_playing = state.read(cx).is_playing();
623 cx.update_entity(&state, |state, cx| state.toggle_play(cx));
624 if is_playing {
625 if let Some(handler) = &on_pause {
626 handler(window, cx);
627 }
628 } else if let Some(handler) = &on_play {
629 handler(window, cx);
630 }
631 }
632 })
633 .on_action({
634 let state = state_for_actions.clone();
635 move |_: &VideoPlayerMute, _, cx| {
636 cx.update_entity(&state, |state, cx| state.toggle_mute(cx));
637 }
638 })
639 .on_action({
640 let state = state_for_actions.clone();
641 let on_fullscreen = on_fullscreen.clone();
642 move |_: &VideoPlayerFullscreen, window, cx| {
643 cx.update_entity(&state, |state, cx| state.toggle_fullscreen(window, cx));
644 let is_fullscreen = state.read(cx).is_fullscreen();
645 if let Some(handler) = &on_fullscreen {
646 handler(is_fullscreen, window, cx);
647 }
648 }
649 })
650 .on_action({
651 let state = state_for_actions.clone();
652 let on_seek = on_seek.clone();
653 move |_: &VideoPlayerSeekForward, window, cx| {
654 cx.update_entity(&state, |state, cx| state.seek_relative(10.0, cx));
655 if let Some(handler) = &on_seek {
656 handler(state.read(cx).current_time(), window, cx);
657 }
658 }
659 })
660 .on_action({
661 let state = state_for_actions.clone();
662 let on_seek = on_seek.clone();
663 move |_: &VideoPlayerSeekBackward, window, cx| {
664 cx.update_entity(&state, |state, cx| state.seek_relative(-10.0, cx));
665 if let Some(handler) = &on_seek {
666 handler(state.read(cx).current_time(), window, cx);
667 }
668 }
669 })
670 .on_action({
671 let state = state_for_actions.clone();
672 let on_volume_change = on_volume_change.clone();
673 move |_: &VideoPlayerVolumeUp, window, cx| {
674 let current = state.read(cx).volume();
675 let new_volume = (current + 0.1).min(1.0);
676 cx.update_entity(&state, |state, cx| state.set_volume(new_volume, cx));
677 if let Some(handler) = &on_volume_change {
678 handler(new_volume, window, cx);
679 }
680 }
681 })
682 .on_action({
683 let state = state_for_actions.clone();
684 let on_volume_change = on_volume_change.clone();
685 move |_: &VideoPlayerVolumeDown, window, cx| {
686 let current = state.read(cx).volume();
687 let new_volume = (current - 0.1).max(0.0);
688 cx.update_entity(&state, |state, cx| state.set_volume(new_volume, cx));
689 if let Some(handler) = &on_volume_change {
690 handler(new_volume, window, cx);
691 }
692 }
693 })
694 .on_mouse_move({
695 let state = state_for_mouse.clone();
696 window.listener_for(&state, move |state, _: &MouseMoveEvent, _, cx| {
697 state.touch_controls(cx);
698 })
699 })
700 .when(show_poster, {
701 let poster = self.poster.clone();
702 move |this| {
703 this.child(
704 div()
705 .absolute()
706 .inset_0()
707 .flex()
708 .items_center()
709 .justify_center()
710 .when_some(poster, |this, poster_src| {
711 this.child(
712 img(poster_src)
713 .size_full()
714 .object_fit(ObjectFit::Cover)
715 )
716 })
717 )
718 }
719 })
720 .when(!show_poster && !overlay_only, |this| {
721 if let Some(ref frame) = current_frame {
722 this.child(
723 div()
724 .absolute()
725 .inset_0()
726 .child(
727 img(frame.clone())
728 .size_full()
729 .object_fit(ObjectFit::Contain)
730 )
731 )
732 } else {
733 this.child(
734 div()
735 .absolute()
736 .inset_0()
737 .bg(gpui::black())
738 .flex()
739 .items_center()
740 .justify_center()
741 .text_color(gpui::white().opacity(0.5))
742 .text_sm()
743 .font_family(theme.tokens.font_family.clone())
744 .child("Video content area")
745 )
746 }
747 })
748 .child({
749 let state_play = state_entity.clone();
750 let on_play_center = on_play.clone();
751 let on_pause_center = on_pause.clone();
752
753 div()
754 .id("center-play-button")
755 .absolute()
756 .inset_0()
757 .flex()
758 .items_center()
759 .justify_center()
760 .when(show_controls || !is_playing, |this| {
761 this.child(
762 div()
763 .id("play-overlay")
764 .size(px(72.0))
765 .rounded_full()
766 .bg(gpui::black().opacity(0.6))
767 .border_2()
768 .border_color(gpui::white().opacity(0.3))
769 .flex()
770 .items_center()
771 .justify_center()
772 .cursor(CursorStyle::PointingHand)
773 .hover(|style| style.bg(gpui::black().opacity(0.8)))
774 .on_mouse_down(MouseButton::Left, move |_, window, cx| {
775 let is_playing_now = state_play.read(cx).is_playing();
776 cx.update_entity(&state_play, |state, cx| state.toggle_play(cx));
777 if is_playing_now {
778 if let Some(handler) = &on_pause_center {
779 handler(window, cx);
780 }
781 } else if let Some(handler) = &on_play_center {
782 handler(window, cx);
783 }
784 })
785 .child(
786 svg()
787 .path(format!("icons/{}.svg", play_icon))
788 .size(px(32.0))
789 .text_color(gpui::white())
790 )
791 )
792 })
793 })
794 .when(show_controls, |this| {
795 let state_progress = state_entity.clone();
796 let state_progress_drag = state_entity.clone();
797 let state_progress_move = state_entity.clone();
798 let state_progress_up = state_entity.clone();
799 let on_seek_progress = on_seek.clone();
800 let on_seek_drag = on_seek.clone();
801
802 let state_volume = state_entity.clone();
803 let state_volume_icon = state_entity.clone();
804 let state_volume_drag = state_entity.clone();
805 let state_volume_move = state_entity.clone();
806 let state_volume_up = state_entity.clone();
807 let on_volume_slider = on_volume_change.clone();
808 let on_volume_drag_change = on_volume_change.clone();
809
810 let state_play_btn = state_entity.clone();
811 let on_play_btn = on_play.clone();
812 let on_pause_btn = on_pause.clone();
813
814 let state_skip_back = state_entity.clone();
815 let on_seek_back = on_seek.clone();
816
817 let state_skip_forward = state_entity.clone();
818 let on_seek_forward = on_seek.clone();
819
820 let state_speed = state_entity.clone();
821 let state_speed_item = state_entity.clone();
822 let on_speed_change = on_playback_speed_change.clone();
823
824 let state_fullscreen = state_entity.clone();
825 let on_fullscreen_btn = on_fullscreen.clone();
826
827 this.child(
828 div()
829 .id("controls-overlay")
830 .absolute()
831 .bottom_0()
832 .left_0()
833 .right_0()
834 .h(controls_height + px(40.0))
835 .bg(gpui::black().opacity(0.7))
836 .flex()
837 .flex_col()
838 .justify_end()
839 .child(
840 div()
841 .id("progress-bar-container")
842 .px(px(12.0))
843 .pb(px(4.0))
844 .child(
845 div()
846 .id("progress-bar")
847 .relative()
848 .h(px(6.0))
849 .w_full()
850 .bg(gpui::white().opacity(0.3))
851 .rounded_full()
852 .cursor(CursorStyle::PointingHand)
853 .child(
854 canvas(
855 {
856 let state = state_progress.clone();
857 move |bounds, _, cx| {
858 state.update(cx, |state, _| {
859 state.progress_bounds = bounds;
860 });
861 }
862 },
863 |_, _, _, _| {},
864 )
865 .absolute()
866 .size_full(),
867 )
868 .child(
869 div()
870 .absolute()
871 .left_0()
872 .top_0()
873 .h_full()
874 .w(relative(progress as f32))
875 .bg(theme.tokens.primary)
876 .rounded_full()
877 )
878 .child(
879 div()
880 .absolute()
881 .left(relative(progress as f32))
882 .top(px(-3.0))
883 .ml(px(-6.0))
884 .size(px(12.0))
885 .rounded_full()
886 .bg(theme.tokens.primary)
887 .border_2()
888 .border_color(gpui::white())
889 )
890 .on_mouse_down(
891 MouseButton::Left,
892 window.listener_for(
893 &state_progress_drag,
894 {
895 let on_seek = on_seek_progress.clone();
896 move |state, e: &MouseDownEvent, window, cx| {
897 state.is_seeking = true;
898 state.update_seek_from_position(e.position, cx);
899 if let Some(handler) = &on_seek {
900 handler(state.current_time, window, cx);
901 }
902 }
903 },
904 ),
905 )
906 .on_mouse_move(
907 window.listener_for(
908 &state_progress_move,
909 {
910 let on_seek = on_seek_drag.clone();
911 move |state, e: &MouseMoveEvent, window, cx| {
912 if state.is_seeking {
913 state.update_seek_from_position(e.position, cx);
914 if let Some(handler) = &on_seek {
915 handler(state.current_time, window, cx);
916 }
917 }
918 }
919 },
920 ),
921 )
922 .on_mouse_up(
923 MouseButton::Left,
924 window.listener_for(
925 &state_progress_up,
926 move |state, _: &MouseUpEvent, _, _| {
927 state.is_seeking = false;
928 },
929 ),
930 )
931 .on_mouse_up_out(
932 MouseButton::Left,
933 window.listener_for(
934 &state_progress_up,
935 move |state, _: &MouseUpEvent, _, _| {
936 state.is_seeking = false;
937 },
938 ),
939 )
940 )
941 )
942 .child(
943 div()
944 .id("controls-bar")
945 .h(controls_height)
946 .px(px(12.0))
947 .flex()
948 .items_center()
949 .justify_between()
950 .child(
951 div()
952 .flex()
953 .items_center()
954 .gap(px(8.0))
955 .child(
956 div()
957 .id("skip-back-btn")
958 .size(px(32.0))
959 .rounded(theme.tokens.radius_md)
960 .flex()
961 .items_center()
962 .justify_center()
963 .cursor(CursorStyle::PointingHand)
964 .hover(|style| style.bg(gpui::white().opacity(0.2)))
965 .on_mouse_down(MouseButton::Left, move |_, window, cx| {
966 cx.update_entity(&state_skip_back, |state, cx| {
967 state.seek_relative(-10.0, cx);
968 });
969 if let Some(handler) = &on_seek_back {
970 handler(state_skip_back.read(cx).current_time(), window, cx);
971 }
972 })
973 .child(
974 svg()
975 .path("icons/rewind.svg")
976 .size(icon_size)
977 .text_color(gpui::white())
978 )
979 )
980 .child(
981 div()
982 .id("play-pause-btn")
983 .size(px(40.0))
984 .rounded_full()
985 .bg(theme.tokens.primary)
986 .flex()
987 .items_center()
988 .justify_center()
989 .cursor(CursorStyle::PointingHand)
990 .hover(|style| style.opacity(0.9))
991 .on_mouse_down(MouseButton::Left, move |_, window, cx| {
992 let is_playing_now = state_play_btn.read(cx).is_playing();
993 cx.update_entity(&state_play_btn, |state, cx| state.toggle_play(cx));
994 if is_playing_now {
995 if let Some(handler) = &on_pause_btn {
996 handler(window, cx);
997 }
998 } else if let Some(handler) = &on_play_btn {
999 handler(window, cx);
1000 }
1001 })
1002 .child(
1003 svg()
1004 .path(format!("icons/{}.svg", play_icon))
1005 .size(icon_size)
1006 .text_color(theme.tokens.primary_foreground)
1007 )
1008 )
1009 .child(
1010 div()
1011 .id("skip-forward-btn")
1012 .size(px(32.0))
1013 .rounded(theme.tokens.radius_md)
1014 .flex()
1015 .items_center()
1016 .justify_center()
1017 .cursor(CursorStyle::PointingHand)
1018 .hover(|style| style.bg(gpui::white().opacity(0.2)))
1019 .on_mouse_down(MouseButton::Left, move |_, window, cx| {
1020 cx.update_entity(&state_skip_forward, |state, cx| {
1021 state.seek_relative(10.0, cx);
1022 });
1023 if let Some(handler) = &on_seek_forward {
1024 handler(state_skip_forward.read(cx).current_time(), window, cx);
1025 }
1026 })
1027 .child(
1028 svg()
1029 .path("icons/fast-forward.svg")
1030 .size(icon_size)
1031 .text_color(gpui::white())
1032 )
1033 )
1034 .child(
1035 div()
1036 .ml(px(8.0))
1037 .text_sm()
1038 .text_color(gpui::white())
1039 .font_family(theme.tokens.font_family.clone())
1040 .child(format!("{} / {}", format_time(current_time), format_time(duration)))
1041 )
1042 )
1043 .child(
1044 div()
1045 .flex()
1046 .items_center()
1047 .gap(px(8.0))
1048 .child(
1049 div()
1050 .flex()
1051 .items_center()
1052 .gap(px(4.0))
1053 .child(
1054 div()
1055 .id("volume-btn")
1056 .size(px(32.0))
1057 .rounded(theme.tokens.radius_md)
1058 .flex()
1059 .items_center()
1060 .justify_center()
1061 .cursor(CursorStyle::PointingHand)
1062 .hover(|style| style.bg(gpui::white().opacity(0.2)))
1063 .on_mouse_down(MouseButton::Left, move |_, _, cx| {
1064 cx.update_entity(&state_volume_icon, |state, cx| state.toggle_mute(cx));
1065 })
1066 .child(
1067 svg()
1068 .path(format!("icons/{}.svg", volume_icon))
1069 .size(icon_size)
1070 .text_color(gpui::white())
1071 )
1072 )
1073 .child(
1074 div()
1075 .id("volume-slider")
1076 .relative()
1077 .w(px(80.0))
1078 .h(px(4.0))
1079 .bg(gpui::white().opacity(0.3))
1080 .rounded_full()
1081 .cursor(CursorStyle::PointingHand)
1082 .child(
1083 canvas(
1084 {
1085 let state = state_volume.clone();
1086 move |bounds, _, cx| {
1087 state.update(cx, |state, _| {
1088 state.volume_bounds = bounds;
1089 });
1090 }
1091 },
1092 |_, _, _, _| {},
1093 )
1094 .absolute()
1095 .size_full(),
1096 )
1097 .child(
1098 div()
1099 .absolute()
1100 .left_0()
1101 .top_0()
1102 .h_full()
1103 .w(relative(volume))
1104 .bg(gpui::white())
1105 .rounded_full()
1106 )
1107 .child(
1108 div()
1109 .absolute()
1110 .left(relative(volume))
1111 .top(px(-4.0))
1112 .ml(px(-6.0))
1113 .size(px(12.0))
1114 .rounded_full()
1115 .bg(gpui::white())
1116 )
1117 .on_mouse_down(
1118 MouseButton::Left,
1119 window.listener_for(
1120 &state_volume_drag,
1121 {
1122 let on_volume = on_volume_slider.clone();
1123 move |state, e: &MouseDownEvent, window, cx| {
1124 state.is_volume_dragging = true;
1125 state.update_volume_from_position(e.position, cx);
1126 if let Some(handler) = &on_volume {
1127 handler(state.volume, window, cx);
1128 }
1129 }
1130 },
1131 ),
1132 )
1133 .on_mouse_move(
1134 window.listener_for(
1135 &state_volume_move,
1136 {
1137 let on_volume = on_volume_drag_change.clone();
1138 move |state, e: &MouseMoveEvent, window, cx| {
1139 if state.is_volume_dragging {
1140 state.update_volume_from_position(e.position, cx);
1141 if let Some(handler) = &on_volume {
1142 handler(state.volume, window, cx);
1143 }
1144 }
1145 }
1146 },
1147 ),
1148 )
1149 .on_mouse_up(
1150 MouseButton::Left,
1151 window.listener_for(
1152 &state_volume_up,
1153 move |state, _: &MouseUpEvent, _, _| {
1154 state.is_volume_dragging = false;
1155 },
1156 ),
1157 )
1158 .on_mouse_up_out(
1159 MouseButton::Left,
1160 window.listener_for(
1161 &state_volume_up,
1162 move |state, _: &MouseUpEvent, _, _| {
1163 state.is_volume_dragging = false;
1164 },
1165 ),
1166 )
1167 )
1168 )
1169 .child(
1170 div()
1171 .id("speed-btn")
1172 .relative()
1173 .child(
1174 div()
1175 .px(px(8.0))
1176 .py(px(4.0))
1177 .rounded(theme.tokens.radius_md)
1178 .text_xs()
1179 .text_color(gpui::white())
1180 .font_family(theme.tokens.font_family.clone())
1181 .cursor(CursorStyle::PointingHand)
1182 .hover(|style| style.bg(gpui::white().opacity(0.2)))
1183 .on_mouse_down(MouseButton::Left, move |_, _, cx| {
1184 cx.update_entity(&state_speed, |state, cx| state.toggle_speed_menu(cx));
1185 })
1186 .child(playback_speed.label())
1187 )
1188 .when(show_speed_menu, {
1189 let theme = theme.clone();
1190 move |this| {
1191 this.child(
1192 div()
1193 .absolute()
1194 .bottom(px(36.0))
1195 .right_0()
1196 .w(px(80.0))
1197 .bg(theme.tokens.popover)
1198 .rounded(theme.tokens.radius_md)
1199 .border_1()
1200 .border_color(theme.tokens.border)
1201 .shadow(smallvec::smallvec![theme.tokens.shadow_lg])
1202 .py(px(4.0))
1203 .children(
1204 VideoPlaybackSpeed::all().iter().map(|speed| {
1205 let state_item = state_speed_item.clone();
1206 let on_speed = on_speed_change.clone();
1207 let speed_val = *speed;
1208 let is_selected = speed_val == playback_speed;
1209
1210 div()
1211 .id(ElementId::Name(format!("speed-{}", speed_val.label()).into()))
1212 .px(px(12.0))
1213 .py(px(6.0))
1214 .text_xs()
1215 .text_color(if is_selected {
1216 theme.tokens.primary
1217 } else {
1218 theme.tokens.popover_foreground
1219 })
1220 .font_family(theme.tokens.font_family.clone())
1221 .cursor(CursorStyle::PointingHand)
1222 .hover(|style| style.bg(theme.tokens.accent))
1223 .on_mouse_down(MouseButton::Left, move |_, window, cx| {
1224 cx.update_entity(&state_item, |state, cx| {
1225 state.set_playback_speed(speed_val, cx);
1226 });
1227 if let Some(handler) = &on_speed {
1228 handler(speed_val, window, cx);
1229 }
1230 })
1231 .child(speed_val.label())
1232 })
1233 )
1234 )
1235 }
1236 })
1237 )
1238 .child(
1239 div()
1240 .id("fullscreen-btn")
1241 .size(px(32.0))
1242 .rounded(theme.tokens.radius_md)
1243 .flex()
1244 .items_center()
1245 .justify_center()
1246 .cursor(CursorStyle::PointingHand)
1247 .hover(|style| style.bg(gpui::white().opacity(0.2)))
1248 .on_mouse_down(MouseButton::Left, move |_, window, cx| {
1249 cx.update_entity(&state_fullscreen, |state, cx| state.toggle_fullscreen(window, cx));
1250 let is_fs = state_fullscreen.read(cx).is_fullscreen();
1251 if let Some(handler) = &on_fullscreen_btn {
1252 handler(is_fs, window, cx);
1253 }
1254 })
1255 .child(
1256 svg()
1257 .path(if is_fullscreen { "icons/minimize.svg" } else { "icons/maximize.svg" })
1258 .size(icon_size)
1259 .text_color(gpui::white())
1260 )
1261 )
1262 )
1263 )
1264 )
1265 })
1266 }
1267}