diff --git a/.changes/windows-drag-files.md b/.changes/windows-drag-files.md new file mode 100644 index 000000000..2d3a1c736 --- /dev/null +++ b/.changes/windows-drag-files.md @@ -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. diff --git a/src/webview2/drag_drop.rs b/src/webview2/drag_drop.rs index 0bdc3f59d..cf3efe8c0 100644 --- a/src/webview2/drag_drop.rs +++ b/src/webview2/drag_drop.rs @@ -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; } diff --git a/src/webview2/mod.rs b/src/webview2/mod.rs index f80c3ed57..6e92c18ba 100644 --- a/src/webview2/mod.rs +++ b/src/webview2/mod.rs @@ -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::() + .and_then(|c| c.SetAllowExternalDrop(false)); + } + DragDropController::new(hwnd, handler) + }); let w = Self { id,