Skip to content

Commit

Permalink
feat: add drag-n-drop position (#7601)
Browse files Browse the repository at this point in the history
  • Loading branch information
amrbashir authored Aug 16, 2023
1 parent 8a67661 commit 6177150
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 12 deletions.
7 changes: 7 additions & 0 deletions .changes/dnd-position.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'tauri': 'minor:feat'
'tauri-runtime': 'minor'
'tauri-runtime-wry': 'minor'
---

Changed `FileDropEvent` to include drop and hover position.
14 changes: 8 additions & 6 deletions core/tauri-runtime-wry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -881,12 +881,14 @@ fn decode_path(path: PathBuf) -> PathBuf {
impl From<FileDropEventWrapper> for FileDropEvent {
fn from(event: FileDropEventWrapper) -> Self {
match event.0 {
WryFileDropEvent::Hovered { paths, position: _ } => {
FileDropEvent::Hovered(paths.into_iter().map(decode_path).collect())
}
WryFileDropEvent::Dropped { paths, position: _ } => {
FileDropEvent::Dropped(paths.into_iter().map(decode_path).collect())
}
WryFileDropEvent::Hovered { paths, position } => FileDropEvent::Hovered {
paths: paths.into_iter().map(decode_path).collect(),
position: PhysicalPositionWrapper(position).into(),
},
WryFileDropEvent::Dropped { paths, position } => FileDropEvent::Dropped {
paths: paths.into_iter().map(decode_path).collect(),
position: PhysicalPositionWrapper(position).into(),
},
// default to cancelled
// FIXME(maybe): Add `FileDropEvent::Unknown` event?
_ => FileDropEvent::Cancelled,
Expand Down
14 changes: 12 additions & 2 deletions core/tauri-runtime/src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ use std::{
sync::mpsc::Sender,
};

use self::dpi::PhysicalPosition;

type UriSchemeProtocol =
dyn Fn(&HttpRequest) -> Result<HttpResponse, Box<dyn std::error::Error>> + Send + Sync + 'static;

Expand Down Expand Up @@ -76,9 +78,17 @@ pub enum WindowEvent {
#[non_exhaustive]
pub enum FileDropEvent {
/// The file(s) have been dragged onto the window, but have not been dropped yet.
Hovered(Vec<PathBuf>),
Hovered {
paths: Vec<PathBuf>,
/// The position of the mouse cursor.
position: PhysicalPosition<f64>,
},
/// The file(s) have been dropped onto the window.
Dropped(Vec<PathBuf>),
Dropped {
paths: Vec<PathBuf>,
/// The position of the mouse cursor.
position: PhysicalPosition<f64>,
},
/// The file drop was aborted.
Cancelled,
}
Expand Down
22 changes: 18 additions & 4 deletions core/tauri/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::{
collections::{HashMap, HashSet},
fmt,
fs::create_dir_all,
path::PathBuf,
sync::{Arc, Mutex, MutexGuard},
};

Expand Down Expand Up @@ -38,7 +39,10 @@ use crate::{
ResponseBuilder as HttpResponseBuilder,
},
webview::WindowBuilder,
window::{dpi::PhysicalSize, DetachedWindow, FileDropEvent, PendingWindow},
window::{
dpi::{PhysicalPosition, PhysicalSize},
DetachedWindow, FileDropEvent, PendingWindow,
},
},
utils::{
assets::Assets,
Expand Down Expand Up @@ -1399,6 +1403,12 @@ impl<R: Runtime> WindowManager<R> {
}
}

#[derive(Serialize, Clone)]
struct FileDropPayload<'a> {
paths: &'a Vec<PathBuf>,
position: &'a PhysicalPosition<f64>,
}

fn on_window_event<R: Runtime>(
window: &Window<R>,
manager: &WindowManager<R>,
Expand Down Expand Up @@ -1444,8 +1454,11 @@ fn on_window_event<R: Runtime>(
},
)?,
WindowEvent::FileDrop(event) => match event {
FileDropEvent::Hovered(paths) => window.emit(WINDOW_FILE_DROP_HOVER_EVENT, paths)?,
FileDropEvent::Dropped(paths) => {
FileDropEvent::Hovered { paths, position } => {
let payload = FileDropPayload { paths, position };
window.emit(WINDOW_FILE_DROP_HOVER_EVENT, payload)?
}
FileDropEvent::Dropped { paths, position } => {
let scopes = window.state::<Scopes>();
for path in paths {
if path.is_file() {
Expand All @@ -1454,7 +1467,8 @@ fn on_window_event<R: Runtime>(
let _ = scopes.allow_directory(path, false);
}
}
window.emit(WINDOW_FILE_DROP_EVENT, paths)?
let payload = FileDropPayload { paths, position };
window.emit(WINDOW_FILE_DROP_EVENT, payload)?
}
FileDropEvent::Cancelled => window.emit(WINDOW_FILE_DROP_CANCELLED_EVENT, ())?,
_ => unimplemented!(),
Expand Down

0 comments on commit 6177150

Please sign in to comment.