2019-06-21 11:33:15 -04:00
|
|
|
use std::{
|
2022-03-07 22:58:12 +01:00
|
|
|
ffi::{c_void, OsString},
|
2019-06-21 11:33:15 -04:00
|
|
|
os::windows::ffi::OsStringExt,
|
|
|
|
|
path::PathBuf,
|
|
|
|
|
ptr,
|
|
|
|
|
sync::atomic::{AtomicUsize, Ordering},
|
|
|
|
|
};
|
|
|
|
|
|
2022-03-07 22:58:12 +01:00
|
|
|
use windows_sys::{
|
|
|
|
|
core::{IUnknown, GUID, HRESULT},
|
|
|
|
|
Win32::{
|
|
|
|
|
Foundation::{DV_E_FORMATETC, HWND, POINTL, S_OK},
|
|
|
|
|
System::{
|
|
|
|
|
Com::{IDataObject, DVASPECT_CONTENT, FORMATETC, TYMED_HGLOBAL},
|
2023-01-23 22:10:28 +01:00
|
|
|
Ole::{CF_HDROP, DROPEFFECT_COPY, DROPEFFECT_NONE},
|
2022-03-07 22:58:12 +01:00
|
|
|
},
|
|
|
|
|
UI::Shell::{DragFinish, DragQueryFileW, HDROP},
|
2019-06-21 11:33:15 -04:00
|
|
|
},
|
|
|
|
|
};
|
2018-10-24 20:40:12 +02:00
|
|
|
|
2023-12-25 09:25:09 +01:00
|
|
|
use log::debug;
|
|
|
|
|
|
2022-03-07 22:58:12 +01:00
|
|
|
use crate::platform_impl::platform::{
|
|
|
|
|
definitions::{IDataObjectVtbl, IDropTarget, IDropTargetVtbl, IUnknownVtbl},
|
|
|
|
|
WindowId,
|
|
|
|
|
};
|
2022-03-18 14:09:39 +01:00
|
|
|
|
|
|
|
|
use crate::{event::Event, window::WindowId as RootWindowId};
|
2018-10-24 20:40:12 +02:00
|
|
|
|
|
|
|
|
#[repr(C)]
|
|
|
|
|
pub struct FileDropHandlerData {
|
|
|
|
|
pub interface: IDropTarget,
|
|
|
|
|
refcount: AtomicUsize,
|
|
|
|
|
window: HWND,
|
2023-07-31 00:39:01 +04:00
|
|
|
send_event: Box<dyn Fn(Event<()>)>,
|
2022-03-07 22:58:12 +01:00
|
|
|
cursor_effect: u32,
|
2019-06-21 11:33:15 -04:00
|
|
|
hovered_is_valid: bool, /* If the currently hovered item is not valid there must not be any `HoveredFileCancelled` emitted */
|
2018-10-24 20:40:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct FileDropHandler {
|
|
|
|
|
pub data: *mut FileDropHandlerData,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
|
impl FileDropHandler {
|
2023-07-31 00:39:01 +04:00
|
|
|
pub fn new(window: HWND, send_event: Box<dyn Fn(Event<()>)>) -> FileDropHandler {
|
2018-10-24 20:40:12 +02:00
|
|
|
let data = Box::new(FileDropHandlerData {
|
|
|
|
|
interface: IDropTarget {
|
|
|
|
|
lpVtbl: &DROP_TARGET_VTBL as *const IDropTargetVtbl,
|
|
|
|
|
},
|
|
|
|
|
refcount: AtomicUsize::new(1),
|
|
|
|
|
window,
|
2019-02-05 10:30:33 -05:00
|
|
|
send_event,
|
2018-11-20 09:28:26 +01:00
|
|
|
cursor_effect: DROPEFFECT_NONE,
|
|
|
|
|
hovered_is_valid: false,
|
2018-10-24 20:40:12 +02:00
|
|
|
});
|
|
|
|
|
FileDropHandler {
|
|
|
|
|
data: Box::into_raw(data),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Implement IUnknown
|
|
|
|
|
pub unsafe extern "system" fn QueryInterface(
|
2022-03-07 22:58:12 +01:00
|
|
|
_this: *mut IUnknown,
|
|
|
|
|
_riid: *const GUID,
|
2018-10-24 20:40:12 +02:00
|
|
|
_ppvObject: *mut *mut c_void,
|
|
|
|
|
) -> HRESULT {
|
|
|
|
|
// This function doesn't appear to be required for an `IDropTarget`.
|
|
|
|
|
// An implementation would be nice however.
|
|
|
|
|
unimplemented!();
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-07 22:58:12 +01:00
|
|
|
pub unsafe extern "system" fn AddRef(this: *mut IUnknown) -> u32 {
|
2023-09-29 16:07:44 +02:00
|
|
|
let drop_handler_data = unsafe { Self::from_interface(this) };
|
2018-10-24 20:40:12 +02:00
|
|
|
let count = drop_handler_data.refcount.fetch_add(1, Ordering::Release) + 1;
|
2022-03-07 22:58:12 +01:00
|
|
|
count as u32
|
2018-10-24 20:40:12 +02:00
|
|
|
}
|
|
|
|
|
|
2022-03-07 22:58:12 +01:00
|
|
|
pub unsafe extern "system" fn Release(this: *mut IUnknown) -> u32 {
|
2023-09-29 16:07:44 +02:00
|
|
|
let drop_handler = unsafe { Self::from_interface(this) };
|
2018-10-24 20:40:12 +02:00
|
|
|
let count = drop_handler.refcount.fetch_sub(1, Ordering::Release) - 1;
|
|
|
|
|
if count == 0 {
|
|
|
|
|
// Destroy the underlying data
|
2023-09-29 16:07:44 +02:00
|
|
|
drop(unsafe { Box::from_raw(drop_handler as *mut FileDropHandlerData) });
|
2018-10-24 20:40:12 +02:00
|
|
|
}
|
2022-03-07 22:58:12 +01:00
|
|
|
count as u32
|
2018-10-24 20:40:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub unsafe extern "system" fn DragEnter(
|
|
|
|
|
this: *mut IDropTarget,
|
|
|
|
|
pDataObj: *const IDataObject,
|
2022-03-07 22:58:12 +01:00
|
|
|
_grfKeyState: u32,
|
2018-10-24 20:40:12 +02:00
|
|
|
_pt: *const POINTL,
|
2022-03-07 22:58:12 +01:00
|
|
|
pdwEffect: *mut u32,
|
2018-10-24 20:40:12 +02:00
|
|
|
) -> HRESULT {
|
2019-06-18 02:27:00 +08:00
|
|
|
use crate::event::WindowEvent::HoveredFile;
|
2023-09-29 16:07:44 +02:00
|
|
|
let drop_handler = unsafe { Self::from_interface(this) };
|
|
|
|
|
let hdrop = unsafe {
|
|
|
|
|
Self::iterate_filenames(pDataObj, |filename| {
|
|
|
|
|
drop_handler.send_event(Event::WindowEvent {
|
|
|
|
|
window_id: RootWindowId(WindowId(drop_handler.window)),
|
|
|
|
|
event: HoveredFile(filename),
|
|
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
};
|
2018-11-20 09:28:26 +01:00
|
|
|
drop_handler.hovered_is_valid = hdrop.is_some();
|
|
|
|
|
drop_handler.cursor_effect = if drop_handler.hovered_is_valid {
|
|
|
|
|
DROPEFFECT_COPY
|
|
|
|
|
} else {
|
|
|
|
|
DROPEFFECT_NONE
|
|
|
|
|
};
|
2023-09-29 16:07:44 +02:00
|
|
|
unsafe {
|
|
|
|
|
*pdwEffect = drop_handler.cursor_effect;
|
|
|
|
|
}
|
2018-10-24 20:40:12 +02:00
|
|
|
|
|
|
|
|
S_OK
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub unsafe extern "system" fn DragOver(
|
2018-11-20 09:28:26 +01:00
|
|
|
this: *mut IDropTarget,
|
2022-03-07 22:58:12 +01:00
|
|
|
_grfKeyState: u32,
|
2018-10-24 20:40:12 +02:00
|
|
|
_pt: *const POINTL,
|
2022-03-07 22:58:12 +01:00
|
|
|
pdwEffect: *mut u32,
|
2018-10-24 20:40:12 +02:00
|
|
|
) -> HRESULT {
|
2023-09-29 16:07:44 +02:00
|
|
|
let drop_handler = unsafe { Self::from_interface(this) };
|
|
|
|
|
unsafe {
|
|
|
|
|
*pdwEffect = drop_handler.cursor_effect;
|
|
|
|
|
}
|
2018-11-20 09:28:26 +01:00
|
|
|
|
2018-10-24 20:40:12 +02:00
|
|
|
S_OK
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub unsafe extern "system" fn DragLeave(this: *mut IDropTarget) -> HRESULT {
|
2019-06-18 02:27:00 +08:00
|
|
|
use crate::event::WindowEvent::HoveredFileCancelled;
|
2023-09-29 16:07:44 +02:00
|
|
|
let drop_handler = unsafe { Self::from_interface(this) };
|
2018-11-20 09:28:26 +01:00
|
|
|
if drop_handler.hovered_is_valid {
|
2019-02-05 10:30:33 -05:00
|
|
|
drop_handler.send_event(Event::WindowEvent {
|
2022-03-18 14:09:39 +01:00
|
|
|
window_id: RootWindowId(WindowId(drop_handler.window)),
|
2018-11-20 09:28:26 +01:00
|
|
|
event: HoveredFileCancelled,
|
|
|
|
|
});
|
|
|
|
|
}
|
2018-10-24 20:40:12 +02:00
|
|
|
|
|
|
|
|
S_OK
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub unsafe extern "system" fn Drop(
|
|
|
|
|
this: *mut IDropTarget,
|
|
|
|
|
pDataObj: *const IDataObject,
|
2022-03-07 22:58:12 +01:00
|
|
|
_grfKeyState: u32,
|
2018-10-24 20:40:12 +02:00
|
|
|
_pt: *const POINTL,
|
2022-03-07 22:58:12 +01:00
|
|
|
_pdwEffect: *mut u32,
|
2018-10-24 20:40:12 +02:00
|
|
|
) -> HRESULT {
|
2019-06-18 02:27:00 +08:00
|
|
|
use crate::event::WindowEvent::DroppedFile;
|
2023-09-29 16:07:44 +02:00
|
|
|
let drop_handler = unsafe { Self::from_interface(this) };
|
|
|
|
|
let hdrop = unsafe {
|
|
|
|
|
Self::iterate_filenames(pDataObj, |filename| {
|
|
|
|
|
drop_handler.send_event(Event::WindowEvent {
|
|
|
|
|
window_id: RootWindowId(WindowId(drop_handler.window)),
|
|
|
|
|
event: DroppedFile(filename),
|
|
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
};
|
2018-11-20 09:28:26 +01:00
|
|
|
if let Some(hdrop) = hdrop {
|
2023-09-29 16:07:44 +02:00
|
|
|
unsafe { DragFinish(hdrop) };
|
2018-11-20 09:28:26 +01:00
|
|
|
}
|
2018-10-24 20:40:12 +02:00
|
|
|
|
|
|
|
|
S_OK
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unsafe fn from_interface<'a, InterfaceT>(this: *mut InterfaceT) -> &'a mut FileDropHandlerData {
|
2023-09-29 16:07:44 +02:00
|
|
|
unsafe { &mut *(this as *mut _) }
|
2018-10-24 20:40:12 +02:00
|
|
|
}
|
|
|
|
|
|
2022-03-07 22:58:12 +01:00
|
|
|
unsafe fn iterate_filenames<F>(data_obj: *const IDataObject, callback: F) -> Option<HDROP>
|
2018-10-24 20:40:12 +02:00
|
|
|
where
|
|
|
|
|
F: Fn(PathBuf),
|
|
|
|
|
{
|
2021-08-30 19:40:02 +02:00
|
|
|
let drop_format = FORMATETC {
|
2023-01-23 22:10:28 +01:00
|
|
|
cfFormat: CF_HDROP,
|
2022-03-07 22:58:12 +01:00
|
|
|
ptd: ptr::null_mut(),
|
2022-12-22 21:35:33 +02:00
|
|
|
dwAspect: DVASPECT_CONTENT,
|
2018-10-24 20:40:12 +02:00
|
|
|
lindex: -1,
|
2022-03-07 22:58:12 +01:00
|
|
|
tymed: TYMED_HGLOBAL as u32,
|
2018-10-24 20:40:12 +02:00
|
|
|
};
|
|
|
|
|
|
2023-09-29 16:07:44 +02:00
|
|
|
let mut medium = unsafe { std::mem::zeroed() };
|
|
|
|
|
let get_data_fn = unsafe { (*(*data_obj).cast::<IDataObjectVtbl>()).GetData };
|
|
|
|
|
let get_data_result = unsafe { get_data_fn(data_obj as *mut _, &drop_format, &mut medium) };
|
2022-03-07 22:58:12 +01:00
|
|
|
if get_data_result >= 0 {
|
2023-09-29 16:07:44 +02:00
|
|
|
let hdrop = unsafe { medium.Anonymous.hGlobal };
|
2018-10-24 20:40:12 +02:00
|
|
|
|
|
|
|
|
// The second parameter (0xFFFFFFFF) instructs the function to return the item count
|
2023-09-29 16:07:44 +02:00
|
|
|
let item_count = unsafe { DragQueryFileW(hdrop, 0xFFFFFFFF, ptr::null_mut(), 0) };
|
2018-10-24 20:40:12 +02:00
|
|
|
|
|
|
|
|
for i in 0..item_count {
|
2019-06-29 00:07:36 +02:00
|
|
|
// Get the length of the path string NOT including the terminating null character.
|
|
|
|
|
// Previously, this was using a fixed size array of MAX_PATH length, but the
|
|
|
|
|
// Windows API allows longer paths under certain circumstances.
|
2023-09-29 16:07:44 +02:00
|
|
|
let character_count =
|
|
|
|
|
unsafe { DragQueryFileW(hdrop, i, ptr::null_mut(), 0) as usize };
|
2019-06-29 00:07:36 +02:00
|
|
|
let str_len = character_count + 1;
|
|
|
|
|
|
|
|
|
|
// Fill path_buf with the null-terminated file name
|
|
|
|
|
let mut path_buf = Vec::with_capacity(str_len);
|
2023-09-29 16:07:44 +02:00
|
|
|
unsafe {
|
|
|
|
|
DragQueryFileW(hdrop, i, path_buf.as_mut_ptr(), str_len as u32);
|
|
|
|
|
path_buf.set_len(str_len);
|
|
|
|
|
}
|
2019-06-29 00:07:36 +02:00
|
|
|
|
|
|
|
|
callback(OsString::from_wide(&path_buf[0..character_count]).into());
|
2018-10-24 20:40:12 +02:00
|
|
|
}
|
|
|
|
|
|
2021-08-30 19:40:02 +02:00
|
|
|
Some(hdrop)
|
2018-11-20 09:28:26 +01:00
|
|
|
} else if get_data_result == DV_E_FORMATETC {
|
|
|
|
|
// If the dropped item is not a file this error will occur.
|
|
|
|
|
// In this case it is OK to return without taking further action.
|
|
|
|
|
debug!("Error occured while processing dropped/hovered item: item is not a file.");
|
2021-08-30 19:40:02 +02:00
|
|
|
None
|
2018-11-20 09:28:26 +01:00
|
|
|
} else {
|
|
|
|
|
debug!("Unexpected error occured while processing dropped/hovered item.");
|
2021-08-30 19:40:02 +02:00
|
|
|
None
|
2018-10-24 20:40:12 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-02-05 10:30:33 -05:00
|
|
|
impl FileDropHandlerData {
|
2023-07-31 00:39:01 +04:00
|
|
|
fn send_event(&self, event: Event<()>) {
|
2019-02-05 10:30:33 -05:00
|
|
|
(self.send_event)(event);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-24 20:40:12 +02:00
|
|
|
impl Drop for FileDropHandler {
|
|
|
|
|
fn drop(&mut self) {
|
|
|
|
|
unsafe {
|
2022-03-07 22:58:12 +01:00
|
|
|
FileDropHandler::Release(self.data as *mut IUnknown);
|
2018-10-24 20:40:12 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static DROP_TARGET_VTBL: IDropTargetVtbl = IDropTargetVtbl {
|
2022-03-07 22:58:12 +01:00
|
|
|
parent: IUnknownVtbl {
|
2018-10-24 20:40:12 +02:00
|
|
|
QueryInterface: FileDropHandler::QueryInterface,
|
|
|
|
|
AddRef: FileDropHandler::AddRef,
|
|
|
|
|
Release: FileDropHandler::Release,
|
|
|
|
|
},
|
|
|
|
|
DragEnter: FileDropHandler::DragEnter,
|
|
|
|
|
DragOver: FileDropHandler::DragOver,
|
|
|
|
|
DragLeave: FileDropHandler::DragLeave,
|
|
|
|
|
Drop: FileDropHandler::Drop,
|
|
|
|
|
};
|