Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve the clipboard pasting of files for better compatibility #1281

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 63 additions & 28 deletions crates/rnote-ui/src/appwindow/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1160,38 +1160,73 @@ 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
}
})
.collect::<Vec<PathBuf>>();

for file_path in file_paths {
appwindow
.open_file_w_dialogs(
gio::File::for_path(&file_path),
target_pos,
true,
match appwindow
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a candidate for DRY - almost the same code is used in three places in this source file

.clipboard()
.read_future(&["text/uri-list"], glib::source::Priority::DEFAULT)
.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;
.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 files list, Err: {e:?}");
acc.clear();
break;
}
}
}
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::<Vec<PathBuf>>();

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:?}"),
}
}
}
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:?}",
);
}
}
}
Expand Down