Skip to content

Commit 6177150

Browse files
authored
feat: add drag-n-drop position (#7601)
1 parent 8a67661 commit 6177150

File tree

4 files changed

+45
-12
lines changed

4 files changed

+45
-12
lines changed

.changes/dnd-position.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'tauri': 'minor:feat'
3+
'tauri-runtime': 'minor'
4+
'tauri-runtime-wry': 'minor'
5+
---
6+
7+
Changed `FileDropEvent` to include drop and hover position.

core/tauri-runtime-wry/src/lib.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -881,12 +881,14 @@ fn decode_path(path: PathBuf) -> PathBuf {
881881
impl From<FileDropEventWrapper> for FileDropEvent {
882882
fn from(event: FileDropEventWrapper) -> Self {
883883
match event.0 {
884-
WryFileDropEvent::Hovered { paths, position: _ } => {
885-
FileDropEvent::Hovered(paths.into_iter().map(decode_path).collect())
886-
}
887-
WryFileDropEvent::Dropped { paths, position: _ } => {
888-
FileDropEvent::Dropped(paths.into_iter().map(decode_path).collect())
889-
}
884+
WryFileDropEvent::Hovered { paths, position } => FileDropEvent::Hovered {
885+
paths: paths.into_iter().map(decode_path).collect(),
886+
position: PhysicalPositionWrapper(position).into(),
887+
},
888+
WryFileDropEvent::Dropped { paths, position } => FileDropEvent::Dropped {
889+
paths: paths.into_iter().map(decode_path).collect(),
890+
position: PhysicalPositionWrapper(position).into(),
891+
},
890892
// default to cancelled
891893
// FIXME(maybe): Add `FileDropEvent::Unknown` event?
892894
_ => FileDropEvent::Cancelled,

core/tauri-runtime/src/window.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ use std::{
2222
sync::mpsc::Sender,
2323
};
2424

25+
use self::dpi::PhysicalPosition;
26+
2527
type UriSchemeProtocol =
2628
dyn Fn(&HttpRequest) -> Result<HttpResponse, Box<dyn std::error::Error>> + Send + Sync + 'static;
2729

@@ -76,9 +78,17 @@ pub enum WindowEvent {
7678
#[non_exhaustive]
7779
pub enum FileDropEvent {
7880
/// The file(s) have been dragged onto the window, but have not been dropped yet.
79-
Hovered(Vec<PathBuf>),
81+
Hovered {
82+
paths: Vec<PathBuf>,
83+
/// The position of the mouse cursor.
84+
position: PhysicalPosition<f64>,
85+
},
8086
/// The file(s) have been dropped onto the window.
81-
Dropped(Vec<PathBuf>),
87+
Dropped {
88+
paths: Vec<PathBuf>,
89+
/// The position of the mouse cursor.
90+
position: PhysicalPosition<f64>,
91+
},
8292
/// The file drop was aborted.
8393
Cancelled,
8494
}

core/tauri/src/manager.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::{
77
collections::{HashMap, HashSet},
88
fmt,
99
fs::create_dir_all,
10+
path::PathBuf,
1011
sync::{Arc, Mutex, MutexGuard},
1112
};
1213

@@ -38,7 +39,10 @@ use crate::{
3839
ResponseBuilder as HttpResponseBuilder,
3940
},
4041
webview::WindowBuilder,
41-
window::{dpi::PhysicalSize, DetachedWindow, FileDropEvent, PendingWindow},
42+
window::{
43+
dpi::{PhysicalPosition, PhysicalSize},
44+
DetachedWindow, FileDropEvent, PendingWindow,
45+
},
4246
},
4347
utils::{
4448
assets::Assets,
@@ -1399,6 +1403,12 @@ impl<R: Runtime> WindowManager<R> {
13991403
}
14001404
}
14011405

1406+
#[derive(Serialize, Clone)]
1407+
struct FileDropPayload<'a> {
1408+
paths: &'a Vec<PathBuf>,
1409+
position: &'a PhysicalPosition<f64>,
1410+
}
1411+
14021412
fn on_window_event<R: Runtime>(
14031413
window: &Window<R>,
14041414
manager: &WindowManager<R>,
@@ -1444,8 +1454,11 @@ fn on_window_event<R: Runtime>(
14441454
},
14451455
)?,
14461456
WindowEvent::FileDrop(event) => match event {
1447-
FileDropEvent::Hovered(paths) => window.emit(WINDOW_FILE_DROP_HOVER_EVENT, paths)?,
1448-
FileDropEvent::Dropped(paths) => {
1457+
FileDropEvent::Hovered { paths, position } => {
1458+
let payload = FileDropPayload { paths, position };
1459+
window.emit(WINDOW_FILE_DROP_HOVER_EVENT, payload)?
1460+
}
1461+
FileDropEvent::Dropped { paths, position } => {
14491462
let scopes = window.state::<Scopes>();
14501463
for path in paths {
14511464
if path.is_file() {
@@ -1454,7 +1467,8 @@ fn on_window_event<R: Runtime>(
14541467
let _ = scopes.allow_directory(path, false);
14551468
}
14561469
}
1457-
window.emit(WINDOW_FILE_DROP_EVENT, paths)?
1470+
let payload = FileDropPayload { paths, position };
1471+
window.emit(WINDOW_FILE_DROP_EVENT, payload)?
14581472
}
14591473
FileDropEvent::Cancelled => window.emit(WINDOW_FILE_DROP_CANCELLED_EVENT, ())?,
14601474
_ => unimplemented!(),

0 commit comments

Comments
 (0)