From 56e89bd4e0b98ab2c64cace884ac9c6b31409531 Mon Sep 17 00:00:00 2001 From: Surinder Singh Matoo Date: Fri, 23 Aug 2024 07:30:52 +0530 Subject: [PATCH] web_sys api change: navigator.clipboard() --- crates/eframe/src/web/mod.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/crates/eframe/src/web/mod.rs b/crates/eframe/src/web/mod.rs index 1a6fa6b447c..b42569a8c6d 100644 --- a/crates/eframe/src/web/mod.rs +++ b/crates/eframe/src/web/mod.rs @@ -167,11 +167,32 @@ fn set_cursor_icon(cursor: egui::CursorIcon) -> Option<()> { .ok() } +use web_sys::{Clipboard, Navigator}; +#[wasm_bindgen] +extern "C" { + # [wasm_bindgen (extends = Navigator , js_name = Navigator , typescript_type = "Navigator")] + #[derive(Debug, Clone, PartialEq, Eq)] + type NavigatorExt; + # [wasm_bindgen (structural , method , getter , js_class = "Navigator" , js_name = clipboard)] + fn clipboard(this: &NavigatorExt) -> Option; +} + +// pub fn clipboard() -> Option { +// window().map(|window| { +// window +// .navigator() +// .dyn_into::() +// .unwrap() +// .clipboard() +// }) +// } + /// Set the clipboard text. #[cfg(web_sys_unstable_apis)] fn set_clipboard_text(s: &str) { if let Some(window) = web_sys::window() { - if let Some(clipboard) = window.navigator().clipboard() { + let navigator = window.navigator().dyn_into::().unwrap(); + if let Some(clipboard) = navigator.clipboard() { let promise = clipboard.write_text(s); let future = wasm_bindgen_futures::JsFuture::from(promise); let future = async move {