New drag and drop API (#4571)

This commit implements a new API for drag and drop, with a `DataTransfer` type which abstracts over the various clipboard/drag and drop APIs across different platforms. I built this on top of #2429 although admittedly I ended up removing pretty much all of their work while I was reworking the design.

This is being built in order to help support [drag-and-drop work](https://github.com/slint-ui/slint/issues/1967) in Slint's winit backend. As part of that work, I did extensive research on how drag-and-drop and clipboard APIs are implemented across different platforms, and wrote a (still WIP) research document that can be found [here](https://gist.github.com/eira-fransham/06750cf8d25ade08d362a0ca8dfafe06).

The new API is inspired by the browser's [`DataTransfer`](https://developer.mozilla.org/en-US/docs/Web/API/DataTransfer) API.
This commit is contained in:
Eira Fransham
2026-07-16 14:48:12 +02:00
committed by GitHub
parent 066c091b56
commit 156433eb91
44 changed files with 5905 additions and 594 deletions

View File

@@ -61,8 +61,8 @@ use crate::dark_mode::try_theme;
use crate::definitions::{
CLSID_TaskbarList, IID_ITaskbarList, IID_ITaskbarList2, ITaskbarList, ITaskbarList2,
};
use crate::dnd::FileDropHandler;
use crate::dpi::{dpi_to_scale_factor, enable_non_client_dpi_scaling, hwnd_dpi};
use crate::drop_handler::FileDropHandler;
use crate::event_loop::{self, ActiveEventLoop, DESTROY_MSG_ID, Event, EventLoopRunner};
use crate::icon::{IconType, WinCursor};
use crate::ime::ImeContext;
@@ -1191,6 +1191,7 @@ impl InitData<'_> {
let window_state = {
let window_state = WindowState::new(
&self.attributes,
&self.win_attributes,
scale_factor,
current_theme,
self.attributes.preferred_theme,
@@ -1230,15 +1231,16 @@ impl InitData<'_> {
let file_drop_runner = self.runner.clone();
let window_id = win.id();
let file_drop_handler = FileDropHandler::new(
let mut file_drop_handler = FileDropHandler::new(
win.window.hwnd(),
self.runner.clone(),
Box::new(move |event| {
file_drop_runner.send_event(Event::Window { window_id, event })
}),
);
let handler_interface_ptr =
unsafe { &mut (*file_drop_handler.data).interface as *mut _ as *mut c_void };
unsafe { file_drop_handler.interface_unchecked_mut() as *mut _ as *mut c_void };
assert_eq!(unsafe { RegisterDragDrop(win.window.hwnd(), handler_interface_ptr) }, S_OK);
Some(file_drop_handler)