From cf5128a0814f0b36c46f764b7db339a85f7e4942 Mon Sep 17 00:00:00 2001 From: Warren Lo Date: Thu, 19 Mar 2026 01:10:06 +0800 Subject: [PATCH] Add web bridge module --- src/web/bridge.rs | 100 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 src/web/bridge.rs diff --git a/src/web/bridge.rs b/src/web/bridge.rs new file mode 100644 index 0000000..24a7667 --- /dev/null +++ b/src/web/bridge.rs @@ -0,0 +1,100 @@ +//! WebView bridge for JS <-> Rust communication + +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Clone, Serialize, Deserialize)] +#[serde(tag = "type", content = "data")] +pub enum BridgeMessage { + // Player commands + Play, + Pause, + Stop, + SeekFrame(u64), + SeekTime(f64), + StepForward, + StepBackward, + + // UI state + SetVolume(f32), + SetMuted(bool), + SetSpeed(f32), + + // Overlay toggles + ToggleSubtitle, + ToggleYolo, + ToggleChunks, + + // Zoom/Pan + Zoom(f32), + Pan(f32, f32), + ResetView, + + // File operations + OpenFile(String), + + // Queries + GetState, + GetSubtitle(f64), + GetDetections(u64), +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +#[serde(tag = "type", content = "data")] +pub enum BridgeResponse { + State(PlayerStateResponse), + Subtitle(Option), + Detections(Vec), + Error(String), + Ok, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct PlayerStateResponse { + pub playback: String, + pub current_frame: u64, + pub total_frames: u64, + pub current_time_ms: u64, + pub duration_ms: u64, + pub fps: f64, + pub show_subtitle: bool, + pub show_yolo: bool, + pub show_chunks: bool, + pub zoom: f32, + pub muted: bool, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct DetectionResponse { + pub class_name: String, + pub confidence: f64, + pub x1: f64, + pub y1: f64, + pub x2: f64, + pub y2: f64, +} + +pub struct WebBridge; + +impl WebBridge { + pub fn new() -> Self { + Self + } + + pub fn encode_message(msg: &BridgeMessage) -> String { + serde_json::to_string(msg).unwrap_or_default() + } + + pub fn decode_message(raw: &str) -> Option { + serde_json::from_str(raw).ok() + } + + pub fn encode_response(resp: &BridgeResponse) -> String { + serde_json::to_string(resp).unwrap_or_default() + } +} + +impl Default for WebBridge { + fn default() -> Self { + Self::new() + } +}