use super::*;
pub fn register_native_action_bridge(bridge: fn(&str, &str, &str) -> bool) {
EXTERNAL_NATIVE_ACTION_BRIDGE.store(bridge as *mut (), Ordering::Release);
}
pub fn dispatch_native_action(kind: &str, arg1: &str, arg2: &str) -> bool {
let raw: *mut () = EXTERNAL_NATIVE_ACTION_BRIDGE.load(Ordering::Acquire);
if raw.is_null() {
return false;
}
let func: fn(&str, &str, &str) -> bool =
unsafe { transmute::<*mut (), fn(&str, &str, &str) -> bool>(raw) };
func(kind, arg1, arg2)
}
pub fn write_system_clipboard(text: &str) -> bool {
let bridge_ok: bool =
dispatch_native_action(NATIVE_ACTION_CLIPBOARD, CLIPBOARD_ACTION_WRITE, text);
#[cfg(target_os = "macos")]
{
macos_write_pasteboard(text) || bridge_ok
}
#[cfg(target_arch = "wasm32")]
{
web_write_clipboard(text) || bridge_ok
}
#[cfg(not(any(target_os = "macos", target_arch = "wasm32")))]
{
bridge_ok
}
}
#[must_use]
pub fn is_valid_external_url(url: &str) -> bool {
let trimmed: &str = url.trim();
if trimmed.is_empty() {
return false;
}
let rest: &str = if let Some(after_https) = trimmed.strip_prefix(URL_SCHEME_HTTPS) {
after_https
} else if let Some(after_http) = trimmed.strip_prefix(URL_SCHEME_HTTP) {
after_http
} else {
return false;
};
if rest.is_empty() || rest.starts_with('/') {
return false;
}
!trimmed
.chars()
.any(|ch: char| ch.is_control() || ch.is_whitespace())
}
#[cfg(not(target_arch = "wasm32"))]
fn open_external_url_desktop(target: &str) -> bool {
if cfg!(test) || env::var_os(ENV_EUV_HEADLESS).is_some() {
return false;
}
#[cfg(target_os = "macos")]
{
Command::new(OS_CMD_OPEN)
.arg(target)
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null())
.spawn()
.is_ok()
}
#[cfg(target_os = "linux")]
{
Command::new(OS_CMD_XDG_OPEN)
.arg(target)
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null())
.spawn()
.is_ok()
}
#[cfg(target_os = "windows")]
{
Command::new(OS_CMD_WIN_SHELL)
.args([OS_CMD_WIN_EXEC, OS_CMD_WIN_START, "", target])
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null())
.spawn()
.is_ok()
}
#[cfg(not(any(target_os = "macos", target_os = "linux", target_os = "windows")))]
{
let _: &str = target;
false
}
}
pub fn open_external_url(url: &str) -> bool {
if !is_valid_external_url(url) {
return false;
}
let target: &str = url.trim();
if let Ok(mut guard) = LAST_EXTERNAL_URL.lock() {
*guard = Some(target.to_string());
}
let bridge_ok: bool = dispatch_native_action(NATIVE_ACTION_OPEN_URL, target, "");
#[cfg(target_arch = "wasm32")]
{
bridge_ok || web_open_external_url(target)
}
#[cfg(not(target_arch = "wasm32"))]
{
let desktop_ok: bool = open_external_url_desktop(target);
let headless_recorded: bool = cfg!(test) || env::var_os(ENV_EUV_HEADLESS).is_some();
let host_ok: bool = bridge_ok || desktop_ok;
host_ok || headless_recorded
}
}
pub fn open_webview_sheet_bridge(url: &str, title: &str) -> bool {
if !is_valid_external_url(url) {
return false;
}
let target: &str = url.trim();
if let Ok(mut guard) = LAST_WEBVIEW_SHEET.lock() {
*guard = Some((target.to_string(), title.to_string()));
}
let _: bool = dispatch_native_action(NATIVE_ACTION_WEBVIEW, target, title);
true
}
pub fn close_webview_sheet_bridge() -> bool {
if let Ok(mut guard) = LAST_WEBVIEW_SHEET.lock() {
*guard = None;
}
dispatch_native_action(NATIVE_ACTION_WEBVIEW, WEBVIEW_ACTION_CLOSE, "")
}
#[must_use]
pub fn last_opened_external_url() -> Option<String> {
LAST_EXTERNAL_URL
.lock()
.ok()
.and_then(|guard: MutexGuard<'_, Option<String>>| guard.clone())
}
#[must_use]
pub fn active_webview_sheet_target() -> Option<(String, String)> {
LAST_WEBVIEW_SHEET
.lock()
.ok()
.and_then(|guard: MutexGuard<'_, Option<(String, String)>>| guard.clone())
}
pub fn clear_link_action_history() {
if let Ok(mut ext_guard) = LAST_EXTERNAL_URL.lock() {
*ext_guard = None;
}
if let Ok(mut wv_guard) = LAST_WEBVIEW_SHEET.lock() {
*wv_guard = None;
}
}