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
447fn format_time(seconds: f64) -> String {
448 let total_seconds = seconds.floor() as i64;
449 let hours = total_seconds / 3600;
450 let minutes = (total_seconds % 3600) / 60;
451 let secs = total_seconds % 60;
452
453 if hours > 0 {
454 format!("{}:{:02}:{:02}", hours, minutes, secs)
455 } else {
456 format!("{}:{:02}", minutes, secs)
457 }
458}
459
460#[derive(IntoElement)]
461pub struct VideoPlayer {
462 state: Entity<VideoPlayerState>,
463 size: VideoPlayerSize,
464 poster: Option<SharedString>,
465 show_poster: bool,
466 overlay_only: bool,
467 on_play: Option<WindowCallback>,
468 on_pause: Option<WindowCallback>,
469 on_seek: Option<ValueCallback<f64>>,
470 on_volume_change: Option<ValueCallback<f32>>,
471 on_fullscreen: Option<ValueCallback<bool>>,
472 on_playback_speed_change: Option<ValueCallback<VideoPlaybackSpeed>>,
473 style: StyleRefinement,
474}
475
476impl VideoPlayer {
477 pub fn new(state: Entity<VideoPlayerState>) -> Self {
478 Self {
479 state,
480 size: VideoPlayerSize::default(),
481 poster: None,
482 show_poster: true,
483 overlay_only: false,
484 on_play: None,
485 on_pause: None,
486 on_seek: None,
487 on_volume_change: None,
488 on_fullscreen: None,
489 on_playback_speed_change: None,
490 style: StyleRefinement::default(),
491 }
492 }
493
494 pub fn size(mut self, size: VideoPlayerSize) -> Self {
495 self.size = size;
496 self
497 }
498
499 pub fn poster(mut self, poster: impl Into<SharedString>) -> Self {
500 self.poster = Some(poster.into());
501 self
502 }
503
504 pub fn show_poster(mut self, show: bool) -> Self {
505 self.show_poster = show;
506 self
507 }
508
509 pub fn overlay_only(mut self) -> Self {
510 self.overlay_only = true;
511 self
512 }
513
514 pub fn on_play(mut self, handler: impl Fn(&mut Window, &mut App) + 'static) -> Self {
515 self.on_play = Some(Rc::new(handler));
516 self
517 }
518
519 pub fn on_pause(mut self, handler: impl Fn(&mut Window, &mut App) + 'static) -> Self {
520 self.on_pause = Some(Rc::new(handler));
521 self
522 }
523
524 pub fn on_seek(mut self, handler: impl Fn(f64, &mut Window, &mut App) + 'static) -> Self {
525 self.on_seek = Some(Rc::new(handler));
526 self
527 }
528
529 pub fn on_volume_change(
530 mut self,
531 handler: impl Fn(f32, &mut Window, &mut App) + 'static,
532 ) -> Self {
533 self.on_volume_change = Some(Rc::new(handler));
534 self
535 }
536
537 pub fn on_fullscreen(
538 mut self,
539 handler: impl Fn(bool, &mut Window, &mut App) + 'static,
540 ) -> Self {
541 self.on_fullscreen = Some(Rc::new(handler));
542 self
543 }
544
545 pub fn on_playback_speed_change(
546 mut self,
547 handler: impl Fn(VideoPlaybackSpeed, &mut Window, &mut App) + 'static,
548 ) -> Self {
549 self.on_playback_speed_change = Some(Rc::new(handler));
550 self
551 }
552}
553
554impl Styled for VideoPlayer {
555 fn style(&mut self) -> &mut StyleRefinement {
556 &mut self.style
557 }
558}
559
560impl RenderOnce for VideoPlayer {
561 fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
562 let theme = use_theme(cx);
563 let state = self.state.read(cx);
564 let focus_handle = state.focus_handle(cx);
565
566 let playback_state = state.playback_state();
567 let is_playing = state.is_playing();
568 let current_time = state.current_time();
569 let duration = state.duration();
570 let progress = state.progress();
571 let volume = state.volume();
572 let is_muted = state.is_muted();
573 let playback_speed = state.playback_speed();
574 let is_fullscreen = state.is_fullscreen();
575 let show_controls = state.show_controls();
576 let show_speed_menu = state.show_speed_menu;
577
578 let (width, height) = self.size.dimensions();
579 let controls_height = self.size.controls_height();
580 let icon_size = self.size.icon_size();
581
582 let show_poster = self.show_poster
583 && self.poster.is_some()
584 && playback_state == VideoPlaybackState::Stopped;
585
586 let current_frame = state.current_frame().cloned();
587 let overlay_only = self.overlay_only;
588
589 let user_style = self.style;
590
591 let volume_icon = if is_muted || volume == 0.0 {
592 "volume-x"
593 } else if volume < 0.5 {
594 "volume-1"
595 } else {
596 "volume-2"
597 };
598
599 let play_icon = if is_playing { "pause" } else { "play" };
600
601 let state_entity = self.state.clone();
602 let state_for_actions = self.state.clone();
603 let state_for_mouse = self.state.clone();
604
605 let on_play = self.on_play.clone();
606 let on_pause = self.on_pause.clone();
607 let on_seek = self.on_seek.clone();
608 let on_volume_change = self.on_volume_change.clone();
609 let on_fullscreen = self.on_fullscreen.clone();
610 let on_playback_speed_change = self.on_playback_speed_change.clone();
611
612 div()
613 .id("video-player")
614 .key_context("VideoPlayer")
615 .track_focus(&focus_handle)
616 .relative()
617 .w(width)
618 .h(height)
619 .bg(gpui::black())
620 .rounded(theme.tokens.radius_lg)
621 .overflow_hidden()
622 .cursor(CursorStyle::Arrow)
623 .map(|this| {
624 let mut div = this;
625 div.style().refine(&user_style);
626 div
627 })
628 .on_action({
629 let state = state_for_actions.clone();
630 let on_play = on_play.clone();
631 let on_pause = on_pause.clone();
632 move |_: &VideoPlayerTogglePlay, window, cx| {
633 let is_playing = state.read(cx).is_playing();
634 cx.update_entity(&state, |state, cx| state.toggle_play(cx));
635 if is_playing {
636 if let Some(handler) = &on_pause {
637 handler(window, cx);
638 }
639 } else if let Some(handler) = &on_play {
640 handler(window, cx);
641 }
642 }
643 })
644 .on_action({
645 let state = state_for_actions.clone();
646 move |_: &VideoPlayerMute, _, cx| {
647 cx.update_entity(&state, |state, cx| state.toggle_mute(cx));
648 }
649 })
650 .on_action({
651 let state = state_for_actions.clone();
652 let on_fullscreen = on_fullscreen.clone();
653 move |_: &VideoPlayerFullscreen, window, cx| {
654 cx.update_entity(&state, |state, cx| state.toggle_fullscreen(window, cx));
655 let is_fullscreen = state.read(cx).is_fullscreen();
656 if let Some(handler) = &on_fullscreen {
657 handler(is_fullscreen, window, cx);
658 }
659 }
660 })
661 .on_action({
662 let state = state_for_actions.clone();
663 let on_seek = on_seek.clone();
664 move |_: &VideoPlayerSeekForward, window, cx| {
665 cx.update_entity(&state, |state, cx| state.seek_relative(10.0, cx));
666 if let Some(handler) = &on_seek {
667 handler(state.read(cx).current_time(), window, cx);
668 }
669 }
670 })
671 .on_action({
672 let state = state_for_actions.clone();
673 let on_seek = on_seek.clone();
674 move |_: &VideoPlayerSeekBackward, window, cx| {
675 cx.update_entity(&state, |state, cx| state.seek_relative(-10.0, cx));
676 if let Some(handler) = &on_seek {
677 handler(state.read(cx).current_time(), window, cx);
678 }
679 }
680 })
681 .on_action({
682 let state = state_for_actions.clone();
683 let on_volume_change = on_volume_change.clone();
684 move |_: &VideoPlayerVolumeUp, window, cx| {
685 let current = state.read(cx).volume();
686 let new_volume = (current + 0.1).min(1.0);
687 cx.update_entity(&state, |state, cx| state.set_volume(new_volume, cx));
688 if let Some(handler) = &on_volume_change {
689 handler(new_volume, window, cx);
690 }
691 }
692 })
693 .on_action({
694 let state = state_for_actions.clone();
695 let on_volume_change = on_volume_change.clone();
696 move |_: &VideoPlayerVolumeDown, window, cx| {
697 let current = state.read(cx).volume();
698 let new_volume = (current - 0.1).max(0.0);
699 cx.update_entity(&state, |state, cx| state.set_volume(new_volume, cx));
700 if let Some(handler) = &on_volume_change {
701 handler(new_volume, window, cx);
702 }
703 }
704 })
705 .on_mouse_move({
706 let state = state_for_mouse.clone();
707 window.listener_for(&state, move |state, _: &MouseMoveEvent, _, cx| {
708 state.touch_controls(cx);
709 })
710 })
711 .when(show_poster, {
712 let poster = self.poster.clone();
713 move |this| {
714 this.child(
715 div()
716 .absolute()
717 .inset_0()
718 .flex()
719 .items_center()
720 .justify_center()
721 .when_some(poster, |this, poster_src| {
722 this.child(
723 img(poster_src)
724 .size_full()
725 .object_fit(ObjectFit::Cover)
726 )
727 })
728 )
729 }
730 })
731 .when(!show_poster && !overlay_only, |this| {
732 if let Some(ref frame) = current_frame {
733 this.child(
734 div()
735 .absolute()
736 .inset_0()
737 .child(
738 img(frame.clone())
739 .size_full()
740 .object_fit(ObjectFit::Contain)
741 )
742 )
743 } else {
744 this.child(
745 div()
746 .absolute()
747 .inset_0()
748 .bg(gpui::black())
749 .flex()
750 .items_center()
751 .justify_center()
752 .text_color(gpui::white().opacity(0.5))
753 .text_sm()
754 .font_family(theme.tokens.font_family.clone())
755 .child("Video content area")
756 )
757 }
758 })
759 .child({
760 let state_play = state_entity.clone();
761 let on_play_center = on_play.clone();
762 let on_pause_center = on_pause.clone();
763
764 div()
765 .id("center-play-button")
766 .absolute()
767 .inset_0()
768 .flex()
769 .items_center()
770 .justify_center()
771 .when(show_controls || !is_playing, |this| {
772 this.child(
773 div()
774 .id("play-overlay")
775 .size(px(72.0))
776 .rounded_full()
777 .bg(gpui::black().opacity(0.6))
778 .border_2()
779 .border_color(gpui::white().opacity(0.3))
780 .flex()
781 .items_center()
782 .justify_center()
783 .cursor(CursorStyle::PointingHand)
784 .hover(|style| style.bg(gpui::black().opacity(0.8)))
785 .on_mouse_down(MouseButton::Left, move |_, window, cx| {
786 let is_playing_now = state_play.read(cx).is_playing();
787 cx.update_entity(&state_play, |state, cx| state.toggle_play(cx));
788 if is_playing_now {
789 if let Some(handler) = &on_pause_center {
790 handler(window, cx);
791 }
792 } else if let Some(handler) = &on_play_center {
793 handler(window, cx);
794 }
795 })
796 .child(
797 svg()
798 .path(format!("icons/{}.svg", play_icon))
799 .size(px(32.0))
800 .text_color(gpui::white())
801 )
802 )
803 })
804 })
805 .when(show_controls, |this| {
806 let state_progress = state_entity.clone();
807 let state_progress_drag = state_entity.clone();
808 let state_progress_move = state_entity.clone();
809 let state_progress_up = state_entity.clone();
810 let on_seek_progress = on_seek.clone();
811 let on_seek_drag = on_seek.clone();
812
813 let state_volume = state_entity.clone();
814 let state_volume_icon = state_entity.clone();
815 let state_volume_drag = state_entity.clone();
816 let state_volume_move = state_entity.clone();
817 let state_volume_up = state_entity.clone();
818 let on_volume_slider = on_volume_change.clone();
819 let on_volume_drag_change = on_volume_change.clone();
820
821 let state_play_btn = state_entity.clone();
822 let on_play_btn = on_play.clone();
823 let on_pause_btn = on_pause.clone();
824
825 let state_skip_back = state_entity.clone();
826 let on_seek_back = on_seek.clone();
827
828 let state_skip_forward = state_entity.clone();
829 let on_seek_forward = on_seek.clone();
830
831 let state_speed = state_entity.clone();
832 let state_speed_item = state_entity.clone();
833 let on_speed_change = on_playback_speed_change.clone();
834
835 let state_fullscreen = state_entity.clone();
836 let on_fullscreen_btn = on_fullscreen.clone();
837
838 this.child(
839 div()
840 .id("controls-overlay")
841 .absolute()
842 .bottom_0()
843 .left_0()
844 .right_0()
845 .h(controls_height + px(40.0))
846 .bg(gpui::black().opacity(0.7))
847 .flex()
848 .flex_col()
849 .justify_end()
850 .child(
851 div()
852 .id("progress-bar-container")
853 .px(px(12.0))
854 .pb(px(4.0))
855 .child(
856 div()
857 .id("progress-bar")
858 .relative()
859 .h(px(6.0))
860 .w_full()
861 .bg(gpui::white().opacity(0.3))
862 .rounded_full()
863 .cursor(CursorStyle::PointingHand)
864 .child(
865 canvas(
866 {
867 let state = state_progress.clone();
868 move |bounds, _, cx| {
869 state.update(cx, |state, _| {
870 state.progress_bounds = bounds;
871 });
872 }
873 },
874 |_, _, _, _| {},
875 )
876 .absolute()
877 .size_full(),
878 )
879 .child(
880 div()
881 .absolute()
882 .left_0()
883 .top_0()
884 .h_full()
885 .w(relative(progress as f32))
886 .bg(theme.tokens.primary)
887 .rounded_full()
888 )
889 .child(
890 div()
891 .absolute()
892 .left(relative(progress as f32))
893 .top(px(-3.0))
894 .ml(px(-6.0))
895 .size(px(12.0))
896 .rounded_full()
897 .bg(theme.tokens.primary)
898 .border_2()
899 .border_color(gpui::white())
900 )
901 .on_mouse_down(
902 MouseButton::Left,
903 window.listener_for(
904 &state_progress_drag,
905 {
906 let on_seek = on_seek_progress.clone();
907 move |state, e: &MouseDownEvent, window, cx| {
908 state.is_seeking = true;
909 state.update_seek_from_position(e.position, cx);
910 if let Some(handler) = &on_seek {
911 handler(state.current_time, window, cx);
912 }
913 }
914 },
915 ),
916 )
917 .on_mouse_move(
918 window.listener_for(
919 &state_progress_move,
920 {
921 let on_seek = on_seek_drag.clone();
922 move |state, e: &MouseMoveEvent, window, cx| {
923 if state.is_seeking {
924 state.update_seek_from_position(e.position, cx);
925 if let Some(handler) = &on_seek {
926 handler(state.current_time, window, cx);
927 }
928 }
929 }
930 },
931 ),
932 )
933 .on_mouse_up(
934 MouseButton::Left,
935 window.listener_for(
936 &state_progress_up,
937 move |state, _: &MouseUpEvent, _, _| {
938 state.is_seeking = false;
939 },
940 ),
941 )
942 .on_mouse_up_out(
943 MouseButton::Left,
944 window.listener_for(
945 &state_progress_up,
946 move |state, _: &MouseUpEvent, _, _| {
947 state.is_seeking = false;
948 },
949 ),
950 )
951 )
952 )
953 .child(
954 div()
955 .id("controls-bar")
956 .h(controls_height)
957 .px(px(12.0))
958 .flex()
959 .items_center()
960 .justify_between()
961 .child(
962 div()
963 .flex()
964 .items_center()
965 .gap(px(8.0))
966 .child(
967 div()
968 .id("skip-back-btn")
969 .size(px(32.0))
970 .rounded(theme.tokens.radius_md)
971 .flex()
972 .items_center()
973 .justify_center()
974 .cursor(CursorStyle::PointingHand)
975 .hover(|style| style.bg(gpui::white().opacity(0.2)))
976 .on_mouse_down(MouseButton::Left, move |_, window, cx| {
977 cx.update_entity(&state_skip_back, |state, cx| {
978 state.seek_relative(-10.0, cx);
979 });
980 if let Some(handler) = &on_seek_back {
981 handler(state_skip_back.read(cx).current_time(), window, cx);
982 }
983 })
984 .child(
985 svg()
986 .path("icons/rewind.svg")
987 .size(icon_size)
988 .text_color(gpui::white())
989 )
990 )
991 .child(
992 div()
993 .id("play-pause-btn")
994 .size(px(40.0))
995 .rounded_full()
996 .bg(theme.tokens.primary)
997 .flex()
998 .items_center()
999 .justify_center()
1000 .cursor(CursorStyle::PointingHand)
1001 .hover(|style| style.opacity(0.9))
1002 .on_mouse_down(MouseButton::Left, move |_, window, cx| {
1003 let is_playing_now = state_play_btn.read(cx).is_playing();
1004 cx.update_entity(&state_play_btn, |state, cx| state.toggle_play(cx));
1005 if is_playing_now {
1006 if let Some(handler) = &on_pause_btn {
1007 handler(window, cx);
1008 }
1009 } else if let Some(handler) = &on_play_btn {
1010 handler(window, cx);
1011 }
1012 })
1013 .child(
1014 svg()
1015 .path(format!("icons/{}.svg", play_icon))
1016 .size(icon_size)
1017 .text_color(theme.tokens.primary_foreground)
1018 )
1019 )
1020 .child(
1021 div()
1022 .id("skip-forward-btn")
1023 .size(px(32.0))
1024 .rounded(theme.tokens.radius_md)
1025 .flex()
1026 .items_center()
1027 .justify_center()
1028 .cursor(CursorStyle::PointingHand)
1029 .hover(|style| style.bg(gpui::white().opacity(0.2)))
1030 .on_mouse_down(MouseButton::Left, move |_, window, cx| {
1031 cx.update_entity(&state_skip_forward, |state, cx| {
1032 state.seek_relative(10.0, cx);
1033 });
1034 if let Some(handler) = &on_seek_forward {
1035 handler(state_skip_forward.read(cx).current_time(), window, cx);
1036 }
1037 })
1038 .child(
1039 svg()
1040 .path("icons/fast-forward.svg")
1041 .size(icon_size)
1042 .text_color(gpui::white())
1043 )
1044 )
1045 .child(
1046 div()
1047 .ml(px(8.0))
1048 .text_sm()
1049 .text_color(gpui::white())
1050 .font_family(theme.tokens.font_family.clone())
1051 .child(format!("{} / {}", format_time(current_time), format_time(duration)))
1052 )
1053 )
1054 .child(
1055 div()
1056 .flex()
1057 .items_center()
1058 .gap(px(8.0))
1059 .child(
1060 div()
1061 .flex()
1062 .items_center()
1063 .gap(px(4.0))
1064 .child(
1065 div()
1066 .id("volume-btn")
1067 .size(px(32.0))
1068 .rounded(theme.tokens.radius_md)
1069 .flex()
1070 .items_center()
1071 .justify_center()
1072 .cursor(CursorStyle::PointingHand)
1073 .hover(|style| style.bg(gpui::white().opacity(0.2)))
1074 .on_mouse_down(MouseButton::Left, move |_, _, cx| {
1075 cx.update_entity(&state_volume_icon, |state, cx| state.toggle_mute(cx));
1076 })
1077 .child(
1078 svg()
1079 .path(format!("icons/{}.svg", volume_icon))
1080 .size(icon_size)
1081 .text_color(gpui::white())
1082 )
1083 )
1084 .child(
1085 div()
1086 .id("volume-slider")
1087 .relative()
1088 .w(px(80.0))
1089 .h(px(4.0))
1090 .bg(gpui::white().opacity(0.3))
1091 .rounded_full()
1092 .cursor(CursorStyle::PointingHand)
1093 .child(
1094 canvas(
1095 {
1096 let state = state_volume.clone();
1097 move |bounds, _, cx| {
1098 state.update(cx, |state, _| {
1099 state.volume_bounds = bounds;
1100 });
1101 }
1102 },
1103 |_, _, _, _| {},
1104 )
1105 .absolute()
1106 .size_full(),
1107 )
1108 .child(
1109 div()
1110 .absolute()
1111 .left_0()
1112 .top_0()
1113 .h_full()
1114 .w(relative(volume))
1115 .bg(gpui::white())
1116 .rounded_full()
1117 )
1118 .child(
1119 div()
1120 .absolute()
1121 .left(relative(volume))
1122 .top(px(-4.0))
1123 .ml(px(-6.0))
1124 .size(px(12.0))
1125 .rounded_full()
1126 .bg(gpui::white())
1127 )
1128 .on_mouse_down(
1129 MouseButton::Left,
1130 window.listener_for(
1131 &state_volume_drag,
1132 {
1133 let on_volume = on_volume_slider.clone();
1134 move |state, e: &MouseDownEvent, window, cx| {
1135 state.is_volume_dragging = true;
1136 state.update_volume_from_position(e.position, cx);
1137 if let Some(handler) = &on_volume {
1138 handler(state.volume, window, cx);
1139 }
1140 }
1141 },
1142 ),
1143 )
1144 .on_mouse_move(
1145 window.listener_for(
1146 &state_volume_move,
1147 {
1148 let on_volume = on_volume_drag_change.clone();
1149 move |state, e: &MouseMoveEvent, window, cx| {
1150 if state.is_volume_dragging {
1151 state.update_volume_from_position(e.position, cx);
1152 if let Some(handler) = &on_volume {
1153 handler(state.volume, window, cx);
1154 }
1155 }
1156 }
1157 },
1158 ),
1159 )
1160 .on_mouse_up(
1161 MouseButton::Left,
1162 window.listener_for(
1163 &state_volume_up,
1164 move |state, _: &MouseUpEvent, _, _| {
1165 state.is_volume_dragging = false;
1166 },
1167 ),
1168 )
1169 .on_mouse_up_out(
1170 MouseButton::Left,
1171 window.listener_for(
1172 &state_volume_up,
1173 move |state, _: &MouseUpEvent, _, _| {
1174 state.is_volume_dragging = false;
1175 },
1176 ),
1177 )
1178 )
1179 )
1180 .child(
1181 div()
1182 .id("speed-btn")
1183 .relative()
1184 .child(
1185 div()
1186 .px(px(8.0))
1187 .py(px(4.0))
1188 .rounded(theme.tokens.radius_md)
1189 .text_xs()
1190 .text_color(gpui::white())
1191 .font_family(theme.tokens.font_family.clone())
1192 .cursor(CursorStyle::PointingHand)
1193 .hover(|style| style.bg(gpui::white().opacity(0.2)))
1194 .on_mouse_down(MouseButton::Left, move |_, _, cx| {
1195 cx.update_entity(&state_speed, |state, cx| state.toggle_speed_menu(cx));
1196 })
1197 .child(playback_speed.label())
1198 )
1199 .when(show_speed_menu, {
1200 let theme = theme.clone();
1201 move |this| {
1202 this.child(
1203 div()
1204 .absolute()
1205 .bottom(px(36.0))
1206 .right_0()
1207 .w(px(80.0))
1208 .bg(theme.tokens.popover)
1209 .rounded(theme.tokens.radius_md)
1210 .border_1()
1211 .border_color(theme.tokens.border)
1212 .shadow(smallvec::smallvec![theme.tokens.shadow_lg])
1213 .py(px(4.0))
1214 .children(
1215 VideoPlaybackSpeed::all().iter().map(|speed| {
1216 let state_item = state_speed_item.clone();
1217 let on_speed = on_speed_change.clone();
1218 let speed_val = *speed;
1219 let is_selected = speed_val == playback_speed;
1220
1221 div()
1222 .id(ElementId::Name(format!("speed-{}", speed_val.label()).into()))
1223 .px(px(12.0))
1224 .py(px(6.0))
1225 .text_xs()
1226 .text_color(if is_selected {
1227 theme.tokens.primary
1228 } else {
1229 theme.tokens.popover_foreground
1230 })
1231 .font_family(theme.tokens.font_family.clone())
1232 .cursor(CursorStyle::PointingHand)
1233 .hover(|style| style.bg(theme.tokens.accent))
1234 .on_mouse_down(MouseButton::Left, move |_, window, cx| {
1235 cx.update_entity(&state_item, |state, cx| {
1236 state.set_playback_speed(speed_val, cx);
1237 });
1238 if let Some(handler) = &on_speed {
1239 handler(speed_val, window, cx);
1240 }
1241 })
1242 .child(speed_val.label())
1243 })
1244 )
1245 )
1246 }
1247 })
1248 )
1249 .child(
1250 div()
1251 .id("fullscreen-btn")
1252 .size(px(32.0))
1253 .rounded(theme.tokens.radius_md)
1254 .flex()
1255 .items_center()
1256 .justify_center()
1257 .cursor(CursorStyle::PointingHand)
1258 .hover(|style| style.bg(gpui::white().opacity(0.2)))
1259 .on_mouse_down(MouseButton::Left, move |_, window, cx| {
1260 cx.update_entity(&state_fullscreen, |state, cx| state.toggle_fullscreen(window, cx));
1261 let is_fs = state_fullscreen.read(cx).is_fullscreen();
1262 if let Some(handler) = &on_fullscreen_btn {
1263 handler(is_fs, window, cx);
1264 }
1265 })
1266 .child(
1267 svg()
1268 .path(if is_fullscreen { "icons/minimize.svg" } else { "icons/maximize.svg" })
1269 .size(icon_size)
1270 .text_color(gpui::white())
1271 )
1272 )
1273 )
1274 )
1275 )
1276 })
1277 }
1278}