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