Skip to content

Commit

Permalink
fix(windows): set WebView2 AllowExternalDrop to false (#1409)
Browse files Browse the repository at this point in the history
* fix(windows): set WebView2 `AllowExternalDrop` to false

closes tauri-apps/tauri#11274

* remove accidental code
  • Loading branch information
amrbashir authored Nov 5, 2024
1 parent 7221256 commit fa9875b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
5 changes: 5 additions & 0 deletions .changes/windows-drag-files.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wry": "patch"
---

On Windows, disable Webview2's file drop when using `WebViewBuilder::with_drag_drop_handler` which fix drag events for files from "Recent files" view.
25 changes: 17 additions & 8 deletions src/webview2/drag_drop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,20 +167,29 @@ impl IDropTarget_Impl for DragDropTarget_Impl {

let mut paths = Vec::new();
let hdrop = unsafe { DragDropTarget::iterate_filenames(pDataObj, |path| paths.push(path)) };

let enter_is_valid = hdrop.is_some();

if !enter_is_valid {
return Ok(());
};

unsafe {
*self.enter_is_valid.get() = enter_is_valid;
}

(self.listener)(DragDropEvent::Enter {
paths,
position: (pt.x as _, pt.y as _),
});

unsafe {
let enter_is_valid = hdrop.is_some();
*self.enter_is_valid.get() = enter_is_valid;
let cursor_effect = if enter_is_valid {
DROPEFFECT_COPY
} else {
DROPEFFECT_NONE
};

let cursor_effect = if enter_is_valid {
DROPEFFECT_COPY
} else {
DROPEFFECT_NONE
};
unsafe {
*pdwEffect = cursor_effect;
*self.cursor_effect.get() = cursor_effect;
}
Expand Down
10 changes: 9 additions & 1 deletion src/webview2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,15 @@ impl InnerWebView {
is_child,
)?;

let drag_drop_controller = drop_handler.map(|handler| DragDropController::new(hwnd, handler));
let drag_drop_controller = drop_handler.map(|handler| {
// Disable file drops, so our handler can capture it
unsafe {
let _ = controller
.cast::<ICoreWebView2Controller4>()
.and_then(|c| c.SetAllowExternalDrop(false));
}
DragDropController::new(hwnd, handler)
});

let w = Self {
id,
Expand Down

0 comments on commit fa9875b

Please sign in to comment.