use crate::error::{BrowserError, Result};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::sync::Mutex;
use std::time::Instant;
pub const DESIGN_MODE_GESTURE_WINDOW_MS: u64 = 1_500;
const PICKER_TEMPLATE: &str = include_str!("design_mode_picker.js");
pub fn now_ms() -> u64 {
static BASE: std::sync::OnceLock<Instant> = std::sync::OnceLock::new();
BASE.get_or_init(Instant::now).elapsed().as_millis() as u64 + 1
}
#[derive(Debug, Default)]
pub struct NativeGestureLatch(Mutex<u64>);
impl NativeGestureLatch {
pub fn new() -> Self {
Self::default()
}
pub fn stamp_at(&self, now_ms: u64) {
*self.0.lock().unwrap() = now_ms;
}
pub fn stamp(&self) {
self.stamp_at(now_ms());
}
pub fn take(&self, now_ms: u64) -> bool {
let mut at = self.0.lock().unwrap();
let recent = gesture_is_recent(*at, now_ms);
*at = 0;
recent
}
}
#[derive(Debug, Clone, PartialEq)]
pub enum DesignModeMarker {
ElementSelected(Value),
PromptSubmitted(Value),
Dismissed,
}
pub struct DesignModeSession {
pub events: tokio::sync::mpsc::UnboundedReceiver<DesignModeMarker>,
}
#[derive(Debug, Clone)]
pub struct DesignModeBridge {
nonce: String,
last_submit_id: u64,
}
fn check_nonce(nonce: &str) -> Result<()> {
if nonce.is_empty() || !nonce.chars().all(|c| c.is_ascii_alphanumeric()) {
return Err(BrowserError::Invalid(
"design-mode nonce must be non-empty ASCII alphanumeric".into(),
));
}
Ok(())
}
pub fn binding_name_for_nonce(nonce: &str) -> String {
format!("__omni_{nonce}_bridge__")
}
pub fn world_name_for_nonce(nonce: &str) -> String {
format!("__omni_{nonce}_world__")
}
impl DesignModeBridge {
pub fn new(nonce: &str) -> Result<Self> {
check_nonce(nonce)?;
Ok(Self {
nonce: nonce.to_string(),
last_submit_id: 0,
})
}
pub fn binding_name(&self) -> String {
binding_name_for_nonce(&self.nonce)
}
pub fn world_name(&self) -> String {
world_name_for_nonce(&self.nonce)
}
pub fn accept(
&mut self,
binding: &str,
payload: &str,
trusted_context: bool,
gesture: &NativeGestureLatch,
now_ms: u64,
) -> Option<DesignModeMarker> {
if !trusted_context || binding != self.binding_name() {
return None;
}
let v: Value = serde_json::from_str(payload).ok()?;
if v.get("nonce").and_then(Value::as_str) != Some(self.nonce.as_str()) {
return None;
}
let data = v.get("data").cloned().unwrap_or(Value::Null);
match v.get("kind").and_then(Value::as_str)? {
"select" => Some(DesignModeMarker::ElementSelected(data)),
"dismiss" => Some(DesignModeMarker::Dismissed),
"submit" => {
let id = data.get("id").and_then(Value::as_u64)?;
if id <= self.last_submit_id || !gesture.take(now_ms) {
return None;
}
self.last_submit_id = id;
Some(DesignModeMarker::PromptSubmitted(data))
}
_ => None,
}
}
}
pub fn is_native_gesture_input(input_type: &str) -> bool {
matches!(input_type, "mouseDown" | "keyDown" | "rawKeyDown")
}
pub fn gesture_is_recent(last_gesture_at_ms: u64, now_ms: u64) -> bool {
last_gesture_at_ms != 0
&& now_ms.saturating_sub(last_gesture_at_ms) <= DESIGN_MODE_GESTURE_WINDOW_MS
}
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ElementRect {
pub x: f64,
pub y: f64,
pub width: f64,
pub height: f64,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CaptureRect {
pub x: i64,
pub y: i64,
pub width: u64,
pub height: u64,
}
pub fn normalize_capture_rect(rect: ElementRect, device_scale: f64) -> Option<CaptureRect> {
if !rect.x.is_finite()
|| !rect.y.is_finite()
|| !rect.width.is_finite()
|| !rect.height.is_finite()
|| rect.width <= 0.0
|| rect.height <= 0.0
{
return None;
}
let scale = if device_scale.is_finite() && device_scale > 0.0 {
device_scale
} else {
1.0
};
Some(CaptureRect {
x: (rect.x * scale).round() as i64,
y: (rect.y * scale).round() as i64,
width: (rect.width * scale).round().max(1.0) as u64,
height: (rect.height * scale).round().max(1.0) as u64,
})
}
pub fn build_design_mode_script(nonce: &str) -> Result<String> {
check_nonce(nonce)?;
Ok(PICKER_TEMPLATE
.replace(
"\"@@BINDING@@\"",
&js_string(&binding_name_for_nonce(nonce)),
)
.replace("\"@@NONCE@@\"", &js_string(nonce)))
}
fn js_string(s: &str) -> String {
serde_json::to_string(s).unwrap_or_else(|_| "\"\"".into())
}