Skip to content

Commit

Permalink
fix(linux): percent-decode paths before firing FileDropEvent (#1168)
Browse files Browse the repository at this point in the history
  • Loading branch information
amrbashir authored Feb 15, 2024
1 parent 2ff8d9d commit 3bdccb7
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changes/linux-file-drop-event.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wry": "patch"
---

On Linux, decode `FilDropEvent` paths before emitting them to make it consistent across all platforms.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ gtk = { version = "0.18", optional = true }
soup3 = { version = "0.5", optional = true }
x11-dl = { version = "2.9", optional = true }
gdkx11 = { version = "0.18", optional = true }
percent-encoding = "2.1"

[target."cfg(target_os = \"windows\")".dependencies]
webview2-com = "0.28"
Expand Down
23 changes: 14 additions & 9 deletions src/webkitgtk/file_drop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,23 @@ pub(crate) fn connect_drag_event(webview: WebView, handler: Box<dyn Fn(FileDropE
let listener_ref = listener.clone();
webview.connect_drag_data_received(move |_, _, x, y, data, info, _| {
if info == 2 {
let uris = data
.uris()
let uris = data.uris();
let paths = uris
.iter()
.map(|gstr| {
let path = gstr.as_str();
PathBuf::from(path.to_string().strip_prefix("file://").unwrap_or(path))
let path = path.strip_prefix("file://").unwrap_or(path);
let path = percent_encoding::percent_decode(path.as_bytes())
.decode_utf8_lossy()
.to_string();
PathBuf::from(path)
})
.collect::<Vec<PathBuf>>();
listener_ref.1.set(Some(uris.clone()));
.collect::<Vec<_>>();

listener_ref.1.set(Some(paths.clone()));

listener_ref.0(FileDropEvent::Hovered {
paths: uris,
paths,
position: (x, y),
});
} else {
Expand All @@ -36,10 +41,10 @@ pub(crate) fn connect_drag_event(webview: WebView, handler: Box<dyn Fn(FileDropE

let listener_ref = listener.clone();
webview.connect_drag_drop(move |_, _, x, y, _| {
let uris = listener_ref.1.take();
if let Some(uris) = uris {
let paths = listener_ref.1.take();
if let Some(paths) = paths {
listener_ref.0(FileDropEvent::Dropped {
paths: uris,
paths,
position: (x, y),
})
} else {
Expand Down

0 comments on commit 3bdccb7

Please sign in to comment.