diff --git a/crates/rnote-ui/src/appwindow/actions.rs b/crates/rnote-ui/src/appwindow/actions.rs index 500a05ac7f..b915a5f5c8 100644 --- a/crates/rnote-ui/src/appwindow/actions.rs +++ b/crates/rnote-ui/src/appwindow/actions.rs @@ -1,6 +1,7 @@ // Imports use crate::{config, dialogs, RnAppWindow, RnCanvas}; use gettextrs::gettext; +use gtk4::gio::InputStream; use gtk4::graphene; use gtk4::{ gdk, gio, glib, glib::clone, prelude::*, PrintOperation, PrintOperationAction, Unit, @@ -1160,38 +1161,52 @@ impl RnAppWindow { async move { debug!("Recognized clipboard content format: files list"); - match appwindow.clipboard().read_text_future().await { - Ok(Some(text)) => { - let file_paths = text - .lines() - .filter_map(|line| { - let file_path = if let Ok(path_uri) = url::Url::parse(line) { - path_uri.to_file_path().ok()? - } else { - PathBuf::from(&line) - }; - - if file_path.exists() { - Some(file_path) - } else { - None + match appwindow + .clipboard() + .read_future(&["text/uri-list"], glib::source::Priority::DEFAULT) + .await + { + Ok((input_stream, _)) => { + let acc = collect_clipboard_data(input_stream).await; + if !acc.is_empty() { + match crate::utils::str_from_u8_nul_utf8(&acc) { + Ok(text) => { + debug!("files uri list : {:?}", text); + let file_paths = text + .lines() + .filter_map(|line| { + let file_path = if let Ok(path_uri) = url::Url::parse(line) { + path_uri.to_file_path().ok()? + } else { + PathBuf::from(&line) + }; + + if file_path.exists() { + Some(file_path) + } else { + None + } + }) + .collect::>(); + + for file_path in file_paths { + appwindow + .open_file_w_dialogs( + gio::File::for_path(&file_path), + target_pos, + true, + ) + .await; + } + } + Err(e) => error!("Failed to read `text/uri-list` from clipboard data, Err: {e:?}"), } - }) - .collect::>(); - - for file_path in file_paths { - appwindow - .open_file_w_dialogs( - gio::File::for_path(&file_path), - target_pos, - true, - ) - .await; } } - Ok(None) => {} Err(e) => { - error!("Reading clipboard text while pasting clipboard from path failed, Err: {e:?}"); + error!( + "Reading clipboard failed while pasting as `text/uri-list`, Err: {e:?}", + ); } } } @@ -1214,28 +1229,7 @@ impl RnAppWindow { .await { Ok((input_stream, _)) => { - let mut acc = Vec::new(); - loop { - match input_stream - .read_future( - vec![0; CLIPBOARD_INPUT_STREAM_BUFSIZE], - glib::source::Priority::DEFAULT, - ) - .await - { - Ok((mut bytes, n)) => { - if n == 0 { - break; - } - acc.append(&mut bytes); - } - Err(e) => { - error!("Failed to read clipboard input stream, Err: {e:?}"); - acc.clear(); - break; - } - } - } + let acc = collect_clipboard_data(input_stream).await; if !acc.is_empty() { match crate::utils::str_from_u8_nul_utf8(&acc) { @@ -1278,28 +1272,7 @@ impl RnAppWindow { .await { Ok((input_stream, _)) => { - let mut acc = Vec::new(); - loop { - match input_stream - .read_future( - vec![0; CLIPBOARD_INPUT_STREAM_BUFSIZE], - glib::source::Priority::DEFAULT, - ) - .await - { - Ok((mut bytes, n)) => { - if n == 0 { - break; - } - acc.append(&mut bytes); - } - Err(e) => { - error!("Failed to read clipboard input stream while pasting as Svg, Err: {e:?}"); - acc.clear(); - break; - } - } - } + let acc = collect_clipboard_data(input_stream).await; if !acc.is_empty() { match crate::utils::str_from_u8_nul_utf8(&acc) { @@ -1406,3 +1379,29 @@ impl RnAppWindow { } } } + +async fn collect_clipboard_data(input_stream: InputStream) -> Vec { + let mut acc = Vec::new(); + loop { + match input_stream + .read_future( + vec![0; CLIPBOARD_INPUT_STREAM_BUFSIZE], + glib::source::Priority::DEFAULT, + ) + .await + { + Ok((mut bytes, n)) => { + if n == 0 { + break; + } + acc.append(&mut bytes); + } + Err(e) => { + error!("Failed to read clipboard input stream, Err: {e:?}"); + acc.clear(); + break; + } + } + } + acc +}