This issue covers native drag & drop support on several operating systems, including Windows, Linux, macOS, IOS and Android. It also contains suggestions for possible lightweight cross-platform libraries which can be used in Nabla to simplify this process.
Native Implementations
Windows
Drag&Drop on windows works in a very easy manner. To start with, you need a window that will accept files and enable Drag&Drop.
With X11 the sitution is much more complicated. Unlike on Windows, Linux has no drag & drop standart, so many organizations have implemented their own protocols, so the program, written for one protocol cannot communicate via Drag&Drop with those, written for different protocols.
Still there is one, most commonly used protocol - XDND.
To implement something minimal, lets make two processes:
You can view the full code of drag & drop handling example with X11 here.
Wayland
If you think that X11 is the only counter-intuitive part of cross-platform drag & drop implementation, you're totally wrong, because it also can be applied to wayland interface.
To get access to the clipboard and drag & drop interfaces, clients can bind to the wl_data_device_manager. We’ll also need to bind to a seat:
After that, you can perform event handling, covered in-depth here.
macOS
On macOS the d&d features are implemented pretty easily. Actually there are several ways to do that, but they're all pretty similar so I'm only gonna cover one of them.
First you need to create class, derived from NSView and specify the formats you want d&d to work with:
class TestView: NSView {
let supportedTypes: [NSPasteboard.PasteboardType] = [.tiff, .color, .string, .fileURL]
}
Then allow the view accept these formats by overriding the registerForDraggedTypes method:
Description
This issue covers native drag & drop support on several operating systems, including Windows, Linux, macOS, IOS and Android. It also contains suggestions for possible lightweight cross-platform libraries which can be used in Nabla to simplify this process.
Native Implementations
Windows
Drag&Drop on windows works in a very easy manner. To start with, you need a window that will accept files and enable Drag&Drop.
In WindowProc you also need to define proper drop handling:
Linux
X11
With X11 the sitution is much more complicated. Unlike on Windows, Linux has no drag & drop standart, so many organizations have implemented their own protocols, so the program, written for one protocol cannot communicate via Drag&Drop with those, written for different protocols. Still there is one, most commonly used protocol - XDND.
To implement something minimal, lets make two processes:
Where the createWindow function looks something like this:
You can view the full code of drag & drop handling example with X11 here.
Wayland
If you think that X11 is the only counter-intuitive part of cross-platform drag & drop implementation, you're totally wrong, because it also can be applied to wayland interface.
To get access to the clipboard and drag & drop interfaces, clients can bind to the
wl_data_device_manager
. We’ll also need to bind to a seat:After binding, we’ll need to create a
wl_data_device
object to interact with the clipboard and drag & drop on a particular seat:and a data source listener with the source itself:
Note that
wl_data_source_offer
allows you to specify the data format.Also create a surface (drag & drop area):
After that, you can perform event handling, covered in-depth here.
macOS
On macOS the d&d features are implemented pretty easily. Actually there are several ways to do that, but they're all pretty similar so I'm only gonna cover one of them. First you need to create class, derived from NSView and specify the formats you want d&d to work with:
Then allow the view accept these formats by overriding the registerForDraggedTypes method:
The next method will allow you to detect when the draggable object enters the dropping area (TestView):
To implement the actual drop operation, override the next method:
IOS
Android