use super::*;
use day_toolkit_dom::{Dom, DomHandle};
use day_spec::{NodeId, Proposal, Size};
use std::cell::RefCell;
use std::collections::HashMap;
day_core::tls_group! {
static LAST_SRC: RefCell<HashMap<DomHandle, String>> = RefCell::new(HashMap::new());
}
fn load(backend: &mut Dom, h: &DomHandle, url: &str) {
backend.set_attr(h, "src", url);
LAST_SRC.with(|m| m.borrow_mut().insert(*h, url.to_string()));
}
fn make(backend: &mut Dom, p: &WebProps, _id: NodeId) -> DomHandle {
if p.profile.private {
return backend.element("div");
}
let h = backend.element("iframe");
super::browser::observe_load(h.0 as i32);
if let Some(labels) = &p.tabs {
super::browser::tabs(h.0 as i32, labels);
}
backend.set_attr(&h, "allow", "autoplay; fullscreen; gamepad");
backend.set_attr(&h, "allowfullscreen", "");
if let Some(provider) = &p.resources {
super::browser::resources(h.0 as i32, &provider.base_url(), &p.url);
LAST_SRC.with(|m| m.borrow_mut().insert(h, p.url.clone()));
} else if !p.inline_root.is_empty() {
let base = format!("assets/data/{}/", p.inline_root);
super::browser::attach(h.0 as i32, &base);
load(backend, &h, &format!("{base}{}", p.inline_start));
} else if !p.url.is_empty() {
load(backend, &h, &p.url);
}
let style = if p.transparent {
"width:100%;height:100%;border:0;background:transparent;color-scheme:normal"
} else {
"width:100%;height:100%;border:0"
};
backend.set_attr(&h, "style", style);
if p.transparent {
backend.set_attr(&h, "allowtransparency", "true");
}
h
}
fn update(backend: &mut Dom, h: &DomHandle, patch: &WebPatch) {
match patch {
WebPatch::Load(url) => load(backend, h, url),
WebPatch::ForceReload => {} WebPatch::Reload => {
if let Some(url) = LAST_SRC.with(|m| m.borrow().get(h).cloned()) {
backend.set_attr(h, "src", &url);
}
}
WebPatch::Back | WebPatch::Forward | WebPatch::Stop => {}
WebPatch::Eval { req, script } => super::browser::eval(h.0 as i32, *req as f64, script),
}
}
fn measure(_backend: &mut Dom, _h: &DomHandle, p: Proposal) -> Size {
Size::new(p.width.unwrap_or(320.0), p.height.unwrap_or(240.0))
}
fn release(_backend: &mut Dom, h: &DomHandle) {
LAST_SRC.with(|m| m.borrow_mut().remove(h));
}
day_pieces::dom_renderer!(day_toolkit_dom::register_renderer, Dom,
kind: KIND, props: WebProps, patch: WebPatch,
make: make, update: update, measure: measure, release: release);