Add web bridge module

This commit is contained in:
2026-03-19 01:10:06 +08:00
parent 476c2ea18b
commit cf5128a081

100
src/web/bridge.rs Normal file
View File

@@ -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<String>),
Detections(Vec<DetectionResponse>),
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<BridgeMessage> {
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()
}
}