feat: add drag-n-drop position (#7601)

This commit is contained in:
Amr Bashir 2023-08-16 05:21:40 +03:00 committed by GitHub
parent 8a6766173b
commit 6177150b6f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 45 additions and 12 deletions

7
.changes/dnd-position.md Normal file
View File

@ -0,0 +1,7 @@
---
'tauri': 'minor:feat'
'tauri-runtime': 'minor'
'tauri-runtime-wry': 'minor'
---
Changed `FileDropEvent` to include drop and hover position.

View File

@ -881,12 +881,14 @@ fn decode_path(path: PathBuf) -> PathBuf {
impl From<FileDropEventWrapper> for FileDropEvent { impl From<FileDropEventWrapper> for FileDropEvent {
fn from(event: FileDropEventWrapper) -> Self { fn from(event: FileDropEventWrapper) -> Self {
match event.0 { match event.0 {
WryFileDropEvent::Hovered { paths, position: _ } => { WryFileDropEvent::Hovered { paths, position } => FileDropEvent::Hovered {
FileDropEvent::Hovered(paths.into_iter().map(decode_path).collect()) paths: paths.into_iter().map(decode_path).collect(),
} position: PhysicalPositionWrapper(position).into(),
WryFileDropEvent::Dropped { paths, position: _ } => { },
FileDropEvent::Dropped(paths.into_iter().map(decode_path).collect()) WryFileDropEvent::Dropped { paths, position } => FileDropEvent::Dropped {
} paths: paths.into_iter().map(decode_path).collect(),
position: PhysicalPositionWrapper(position).into(),
},
// default to cancelled // default to cancelled
// FIXME(maybe): Add `FileDropEvent::Unknown` event? // FIXME(maybe): Add `FileDropEvent::Unknown` event?
_ => FileDropEvent::Cancelled, _ => FileDropEvent::Cancelled,

View File

@ -22,6 +22,8 @@ use std::{
sync::mpsc::Sender, sync::mpsc::Sender,
}; };
use self::dpi::PhysicalPosition;
type UriSchemeProtocol = type UriSchemeProtocol =
dyn Fn(&HttpRequest) -> Result<HttpResponse, Box<dyn std::error::Error>> + Send + Sync + 'static; dyn Fn(&HttpRequest) -> Result<HttpResponse, Box<dyn std::error::Error>> + Send + Sync + 'static;
@ -76,9 +78,17 @@ pub enum WindowEvent {
#[non_exhaustive] #[non_exhaustive]
pub enum FileDropEvent { pub enum FileDropEvent {
/// The file(s) have been dragged onto the window, but have not been dropped yet. /// 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. /// 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. /// The file drop was aborted.
Cancelled, Cancelled,
} }

View File

@ -7,6 +7,7 @@ use std::{
collections::{HashMap, HashSet}, collections::{HashMap, HashSet},
fmt, fmt,
fs::create_dir_all, fs::create_dir_all,
path::PathBuf,
sync::{Arc, Mutex, MutexGuard}, sync::{Arc, Mutex, MutexGuard},
}; };
@ -38,7 +39,10 @@ use crate::{
ResponseBuilder as HttpResponseBuilder, ResponseBuilder as HttpResponseBuilder,
}, },
webview::WindowBuilder, webview::WindowBuilder,
window::{dpi::PhysicalSize, DetachedWindow, FileDropEvent, PendingWindow}, window::{
dpi::{PhysicalPosition, PhysicalSize},
DetachedWindow, FileDropEvent, PendingWindow,
},
}, },
utils::{ utils::{
assets::Assets, assets::Assets,
@ -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>( fn on_window_event<R: Runtime>(
window: &Window<R>, window: &Window<R>,
manager: &WindowManager<R>, manager: &WindowManager<R>,
@ -1444,8 +1454,11 @@ fn on_window_event<R: Runtime>(
}, },
)?, )?,
WindowEvent::FileDrop(event) => match event { WindowEvent::FileDrop(event) => match event {
FileDropEvent::Hovered(paths) => window.emit(WINDOW_FILE_DROP_HOVER_EVENT, paths)?, FileDropEvent::Hovered { paths, position } => {
FileDropEvent::Dropped(paths) => { let payload = FileDropPayload { paths, position };
window.emit(WINDOW_FILE_DROP_HOVER_EVENT, payload)?
}
FileDropEvent::Dropped { paths, position } => {
let scopes = window.state::<Scopes>(); let scopes = window.state::<Scopes>();
for path in paths { for path in paths {
if path.is_file() { if path.is_file() {
@ -1454,7 +1467,8 @@ fn on_window_event<R: Runtime>(
let _ = scopes.allow_directory(path, false); 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, ())?, FileDropEvent::Cancelled => window.emit(WINDOW_FILE_DROP_CANCELLED_EVENT, ())?,
_ => unimplemented!(), _ => unimplemented!(),