use super::web_core::SimulationEngine;
use webkit2gtk::WebViewExt;
pub struct WebKitBackend {
webview: webkit2gtk::WebView,
}
unsafe impl Send for WebKitBackend {}
impl WebKitBackend {
pub fn new() -> Result<Self, String> {
let result =
std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| webkit2gtk::WebView::new()));
match result {
Ok(webview) => Ok(Self { webview }),
Err(_) => Err("GTK not initialized or no display available".to_string()),
}
}
pub fn load_url(&mut self, url: &str) -> Result<(), String> {
self.webview.load_uri(url);
Ok(())
}
pub fn load_html(&mut self, html: &str, base_url: Option<&str>) -> Result<(), String> {
self.webview.load_html(html, base_url);
Ok(())
}
pub fn go_back(&mut self) {
self.webview.go_back();
}
pub fn go_forward(&mut self) {
self.webview.go_forward();
}
pub fn reload(&mut self) {
self.webview.reload();
}
pub fn stop_loading(&mut self) {
self.webview.stop_loading();
}
}
impl SimulationEngine for WebKitBackend {
fn simulate_navigation(&mut self, url: &str) -> Result<String, String> {
self.load_url(url)?;
Ok(format!("WebKit navigation started to: {}", url))
}
}