From a7d41b3c17a789faeb8d2c27b92a06ecdaa23f6a Mon Sep 17 00:00:00 2001 From: accusys Date: Thu, 19 Mar 2026 01:55:44 +0800 Subject: [PATCH] feat(player): add drag-to-seek on progress bar --- src/main.rs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/main.rs b/src/main.rs index df32df7..53a3ff6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -65,6 +65,7 @@ fn run(config: &Config) -> Result<()> { let mut yolo: Option = None; let mut chunks: Option = None; let mut is_fullscreen = false; + let mut is_dragging = false; if let Some(ref video_path) = config.video { info!("Loading video: {:?}", video_path); @@ -281,6 +282,8 @@ fn run(config: &Config) -> Result<()> { let bar_x_start = 10i32; let bar_width = config.width as i32 - 20; if x >= bar_x_start && x <= bar_x_start + bar_width { + is_dragging = true; + player_state.playback = PlaybackState::Paused; let ratio = (x - bar_x_start) as f64 / bar_width as f64; let target_frame = (player_state.total_frames as f64 * ratio) as u64; if let Some(ref mut dec) = decoder { @@ -299,6 +302,36 @@ fn run(config: &Config) -> Result<()> { } } } + sdl2::event::Event::MouseButtonUp { .. } => { + if is_dragging { + is_dragging = false; + info!("Drag seek ended"); + } + } + sdl2::event::Event::MouseMotion { x, y, .. } => { + if is_dragging { + let progress_y = config.height as i32 - progress_height + 5; + let bar_height = 20i32; + if y >= progress_y - 30 + && y <= progress_y + bar_height + 30 + && player_state.total_frames > 0 + { + let bar_x_start = 10i32; + let bar_width = config.width as i32 - 20; + let clamped_x = x.clamp(bar_x_start, bar_x_start + bar_width); + let ratio = (clamped_x - bar_x_start) as f64 / bar_width as f64; + let target_frame = (player_state.total_frames as f64 * ratio) as u64; + if let Some(ref mut dec) = decoder { + let time_ms = + ((target_frame as f64 / player_state.fps) * 1000.0) as u64; + if dec.seek(time_ms).is_ok() { + player_state.current_frame = target_frame; + player_state.current_time_ms = time_ms; + } + } + } + } + } _ => {} } }