Skip to content

Commit

Permalink
fix(core): percent decode file drop payloads, closes #4034 (#4035)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed May 3, 2022
1 parent 715cbde commit a0ecd81
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 18 deletions.
6 changes: 6 additions & 0 deletions .changes/file-drop-percent-decode.md
@@ -0,0 +1,6 @@
---
"tauri-runtime-wry": patch
"tauri": patch
---

The file drop event payloads are now percent-decoded.
1 change: 1 addition & 0 deletions core/tauri-runtime-wry/Cargo.toml
Expand Up @@ -28,6 +28,7 @@ webview2-com = "0.13.0"

[target."cfg(any(target_os = \"linux\", target_os = \"dragonfly\", target_os = \"freebsd\", target_os = \"openbsd\", target_os = \"netbsd\"))".dependencies]
gtk = { version = "0.15", features = [ "v3_20" ] }
percent-encoding = "2.1"

[features]
dox = [ "wry/dox" ]
Expand Down
35 changes: 33 additions & 2 deletions core/tauri-runtime-wry/src/lib.rs
Expand Up @@ -927,11 +927,42 @@ impl WindowBuilder for WindowBuilderWrapper {

pub struct FileDropEventWrapper(WryFileDropEvent);

// on Linux, the paths are percent-encoded
#[cfg(any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd"
))]
fn decode_path(path: PathBuf) -> PathBuf {
percent_encoding::percent_decode(path.display().to_string().as_bytes())
.decode_utf8_lossy()
.into_owned()
.into()
}

// on Windows and macOS, we do not need to decode the path
#[cfg(not(any(
target_os = "linux",
target_os = "dragonfly",
target_os = "freebsd",
target_os = "netbsd",
target_os = "openbsd"
)))]
fn decode_path(path: PathBuf) -> PathBuf {
path
}

impl From<FileDropEventWrapper> for FileDropEvent {
fn from(event: FileDropEventWrapper) -> Self {
match event.0 {
WryFileDropEvent::Hovered(paths) => FileDropEvent::Hovered(paths),
WryFileDropEvent::Dropped(paths) => FileDropEvent::Dropped(paths),
WryFileDropEvent::Hovered(paths) => {
FileDropEvent::Hovered(paths.into_iter().map(decode_path).collect())
}
WryFileDropEvent::Dropped(paths) => {
FileDropEvent::Dropped(paths.into_iter().map(decode_path).collect())
}
// default to cancelled
// FIXME(maybe): Add `FileDropEvent::Unknown` event?
_ => FileDropEvent::Cancelled,
Expand Down
24 changes: 12 additions & 12 deletions examples/api/dist/assets/index.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions examples/api/dist/assets/vendor.js

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions examples/api/src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions examples/api/src/App.svelte
Expand Up @@ -4,6 +4,7 @@
import hotkeys from "hotkeys-js";
import { open } from "@tauri-apps/api/shell";
import { invoke } from "@tauri-apps/api/tauri";
import { appWindow } from "@tauri-apps/api/window";
import Welcome from "./components/Welcome.svelte";
import Cli from "./components/Cli.svelte";
Expand All @@ -28,6 +29,10 @@
});
});
appWindow.listen('tauri://file-drop', function (event) {
onMessage(`File drop: ${event.payload}`);
});
const views = [
{
label: "Welcome",
Expand Down

0 comments on commit a0ecd81

Please sign in to comment.