#[cfg(not(all(
not(alloc_frugal),
any(target_os = "linux", target_os = "macos", target_os = "windows")
)))]
fn main() {
println!(
"probe_clipboard_custom: this profile has no platform clipboard (alloc-frugal or a \
non-desktop target), so there is nothing to stage."
);
}
#[cfg(all(
not(alloc_frugal),
any(target_os = "linux", target_os = "macos", target_os = "windows")
))]
fn main() {
#[cfg(any(
all(target_os = "linux", feature = "gtk-native"),
target_os = "windows",
all(target_os = "macos", any(feature = "cocoa-legacy", feature = "macos"))
))]
{
run_staging_probe();
}
#[cfg(not(any(
all(target_os = "linux", feature = "gtk-native"),
target_os = "windows",
all(target_os = "macos", any(feature = "cocoa-legacy", feature = "macos"))
)))]
{
println!(
"probe_clipboard_custom: this backend has no native custom-type clipboard \
(`--features desktop,gtk-native` on a host with a display session, or `desktop` on \
Windows/macOS, stages it); its write reports the honest refusal instead."
);
}
}
#[cfg(all(
not(alloc_frugal),
any(
all(target_os = "linux", feature = "gtk-native"),
target_os = "windows",
all(target_os = "macos", any(feature = "cocoa-legacy", feature = "macos"))
)
))]
fn gui_probe_allowed() -> bool {
if std::env::var("RW_GUI_PROBE_OK").ok().as_deref() == Some("1") {
return true;
}
println!(
"this probe replaces the clipboard on the session it runs on; set RW_GUI_PROBE_OK=1 to \
stage it (and prefer a machine whose clipboard nobody is using)"
);
false
}
#[cfg(all(
not(alloc_frugal),
any(
all(target_os = "linux", feature = "gtk-native"),
target_os = "windows",
all(target_os = "macos", any(feature = "cocoa-legacy", feature = "macos"))
)
))]
fn run_staging_probe() {
if !gui_probe_allowed() {
std::process::exit(2);
}
use rust_widgets::platform::clipboard::ClipboardContent;
const NODES: &str = "application/vnd.rustage.nodes+json";
let mut failures = 0usize;
let mut check = |held: bool, what: &str| {
if held {
println!(" OK {what}");
} else {
println!(" FAIL {what}");
failures += 1;
}
};
println!("=== clipboard custom-type probe ===");
println!("backend: {}", rust_widgets::platform::backend_name());
rust_widgets::init();
if !rust_widgets::platform::capabilities().clipboard {
println!("unsupported host: no clipboard is reachable in this session");
std::process::exit(2);
}
let Some(clip) = rust_widgets::platform_clipboard() else {
println!("unsupported host: the backend exposes no rich clipboard here");
std::process::exit(2);
};
let saved = rust_widgets::clipboard_text();
let payload = br#"{"vendor":"rustage","kind":"nodes","nodes":[]}"#.to_vec();
println!("\nA. set_contents_with_type / has_format");
let wrote = clip.set_contents_with_type(NODES, &payload);
println!(" set_contents_with_type -> {wrote}");
check(wrote, "the custom type was stored");
check(clip.has_format(NODES), "has_format answers true for the custom type");
println!("\nB. get_contents_for_type");
let read_back = clip.get_contents_for_type(NODES);
println!(" get_contents_for_type -> {} byte(s)", read_back.as_ref().map_or(0, Vec::len));
check(read_back.as_deref() == Some(payload.as_slice()), "the payload comes back byte-for-byte");
check(
clip.get_contents_for_type("text/plain").is_none(),
"a type the clipboard does not hold answers None",
);
println!("\nC. the structured Custom route lands in the same place");
let custom = ClipboardContent::Custom {
content_type: "application/vnd.rustage.resource+json".into(),
data: vec![1, 2, 3, 4],
};
let custom_type = custom.content_type().to_string();
let stored = clip.set_contents(custom);
println!(" set_contents(Custom) -> {stored}");
check(stored && clip.has_format(&custom_type), "a structured Custom write is readable by name");
check(
clip.get_contents_for_type(&custom_type).as_deref() == Some([1u8, 2, 3, 4].as_slice()),
"its bytes come back unchanged",
);
match saved {
Some(text) => {
let _ = rust_widgets::set_clipboard_text(&text);
}
None => {
let _ = rust_widgets::set_clipboard_text("");
}
}
println!("\n=== {} check(s) failed ===", failures);
std::process::exit(if failures == 0 { 0 } else { 1 });
}