New drag and drop API#4571
Conversation
This changes the data transfer system to better match cross-platform semantics
|
I haven't read very much into impl. details, but from what I've seen:
In the current API, we have this URI list preloaded, but I think this is solved, we should only load with async-ish API once user asks, I think, if you want to follow Wayland. If the API is not lazy, I'm not sure how it should be done, so if yo can provide tl;dr it would help (haven't found clear wording in your research).
Well, because it's a lot of work, you need to negotiate what you want to read (image/text/html/whatever), then both ends should do non-blocking writing/reading to an FD, thus implying that you plug that FD into epoll based event loop or something. And ensure that you don't hang each other. And compositor does very little here actually, it just lets you exchange FDs with other end, but all this negotiation + writing the right mime type is on the client. So for example, when something drags object/pastes, you have a bunch of mime types you can use to query data, e.g. image/text/audio, then you ask for audio, and other end should provide audio. Then you want to initiate drag and drop, and you started dragging something, but this something is either text or image, and then depending on what the other end picks(e.g. you drag from winit to firefox, and firefox picks one of two mimetypes you gave to it), winit must reply with the right data. Note that you don't create buffer for both image and text before hand, you only create them once you get event what type of data other end wants. It can also ask you for both, or ask you something from time to time as long as you have advertised something. |
|
@kchibisov Thanks for your response. I appreciate you clarifying the way that data transfer (i.e. clipboard and drag-and-drop) works, but it might be worth reading through the PR since I have an extensive doc comment explaining the exact concerns that you bring up and how the API addresses them. In particular:
The implementation in this PR only addresses drag-and-drop, but it is specifically designed to support clipboard operations in the future. Clipboard operations are planned in a follow-up PR, and only left unimplemented for now for two reasons:
The type hierarchy is like so:
The API flow as-implemented by this PR is like so:
Note that we cannot just return a potentially-blocking Regarding moving |
But we can pass Also, X11 is nearly dead, so no point in design around it. |
|
Yeah, as of the latest few commits I’ve moved the whole drag-and-drop API to the event loop. Fetching still needs to be done asynchronously to support X11 though unfortunately, there’s not really a way around that without running the risk of deadlocking the event loop. The user can always immediately try to read the data without waiting for the I’ve also updated the dnd example to show how to use the new API. |
kchibisov
left a comment
There was a problem hiding this comment.
Generally using event will be fine, I guess. The API should certainly be async, sync won't work on Wayland as well.
This commit also adds deadlock detection to X11, since on that platform the event loop needs to be polled in order for the selection to be received.
ece1dfa to
98106b8
Compare
a0c95c8 to
ea394ea
Compare
Drop emitted DragDropped before computing the effect, so a target rejection via set_valid_actions(none()) left the source seeing DROPEFFECT_NONE while the target app had already seen DragDropped. Compute the effect first and route DROPEFFECT_NONE to DragLeft. Also trace effect_out on the source - cross-process drops have no in-process target callback to observe it through.
Standard Arc pattern: Release on decrement publishes, but the dropping thread needs an Acquire fence on the zero transition to see writes another thread made through the object. AddRef drops to Relaxed - a bump on a count you already reference doesn't synchronize anything. Latent today since our COM objects stay in the STA.
Plumb the icon through IDragSourceHelper::InitializeFromBitmap. The shell composites in premultiplied alpha, so convert RGBA to premultiplied BGRA in a top-down DIB or translucent edges get a halo. Helper failures are non-fatal.
The TYMED branch values in `duplicate_stgmedium` were each off by one bit shift: TYMED_FILE was 1 (= HGLOBAL) instead of 2, GDI was 32 instead of 16, MFPICT 64 instead of 32, ENHMF 128 instead of 64. Latent because the shell drag helper only uses HGLOBAL, but a HANDLE-bearing SetData of any other tymed would have either fallen through or aliased the wrong union arm. Use the named TYMED_* constants from windows-sys so we can't drift again.
win32: add support for starting drags
This PR implements a new API for drag and drop, with a
DataTransfertype which abstracts over the various clipboard/drag and drop APIs across different platforms. I built this on top of #2429 in order to ensure @SludgePhD gets credit if it gets merged, 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 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.
Some platforms (Wayland, X11) always transfer bytes with a MIME type, other platforms have a set of standardised transferrable types. However, all types that are supported cross-platform (images, RTF, HTML, plaintext, URIs/URI lists) are cleanly expressible using MIME types on all supported platforms. As part of this PR, I've written up a quick-and-dirty summary of which types are supported on different platforms.
Design
The new API is inspired by the browser's
DataTransferAPI. The main complexity comes from supporting both the common set of capabilities (the types and traits inwinit-core/src/data_transfer.rs) while also allowing a consumer to use the platform-specific APIs.The design may look somewhat complex, and I am open to suggestions for simplifying it, but OS drag-and-drop/clipboard/etc APIs are just fundamentally complex. Unfortunately, there's going to be a lot of complexity here no matter the implementation. This article by a Wayland maintainer describes it as "arguably one of the most complicated parts of the core Wayland protocol", and from researching the design for other platforms it seems like Wayland actually has the simplest API.
In general, the API is designed around the idea that the user should be able to supply/read both cross-platform types and platform-specific types. Not all of the platform-specific types are fully implemented, although many of them are.
Current state
Both receiving and initiating a drag operation are implemented on Windows, Wayland and macOS. X11 supports receiving dropped data, but initiating a drag on X11 is not planned as part of this PR.
Example
The drag-and-drop example has been updated, so both sending and receiving drag operations is now shown off.
macOS
Sending and receiving a dragging operation:
2026-06-03.17-28-00.mov
Dragging styled text (via the HTML clipboard type) into the notes app on macOS:
2026-06-03.17-38-15.mov
Wayland
Video_2026-06-04_17-29-06.mp4
Windows
2026-06-04.18-41-50.mp4
AI disclosure
Unsure if this is important to anyone reading this PR, but I thought I should mention just in case since I know that it's a common issue for OSS projects:
AI was NOT used for the development of the core winit API, nor the X11, Wayland, or macOS implementations, nor any other code, documentation, or comments contributed to this branch by me. It was also not used to write the PR description, nor any comments made by me. The Windows implementation was done by a colleague, so I do not know for sure if AI was used. I can ask him if the reviewer would like to know, but I can at least confirm that I've manually reviewed every line of his PR to my branch.