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();
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"))]
{
bridge_ok || open_external_url_desktop(target)
}
}