Drag and drop

Like the clipboard, drag and drop is a way to transfer data between different applications.

Most operating systems implement the drag and drop system in the same way with clipboard, and in Yue dragged data are represented as instances of the Clipboard::Data.

For code examples on handling drag and drop, please see the sample app in yue-sample-apps/drag_source.

Drag destination

A View can be made a drag destination by using the View.RegisterDraggedTypes API, which gives the view abilities to accept dropped data.


When users drag data with registered types to the view, drag related events will be emitted in sequence:

  1. The handle_drag_enter and on_drag_leave will be called when the cursor enters or leaves the view while dragging.
  2. The handle_drag_update will be called while cursor moves in the view while dragging.
  3. The handle_drop will be called user releases cursor and drops the data on the view.

To accept dropped data, the handle_drag_enter should be implemented first to decide which DragOperation to perform when cursor is dropped. Depending on the returned drag operation, the cursor may have different icons.

function view.handledragenter(self, info, point)
  isDragging = true
  return gui.DraggingInfo.dragoperationcopy

If you are implementing some visual effects when there are data being dragged to the view, you may want to do some cleanup in on_drag_leave, which is called when cursor moves out of the view and before users drops the data on the view.

function view.ondragleave(self)
  isDragging = true

If the view supports different drag operations depending on the cursor position, you may want to implement handle_drag_update, and return the drag operation according to the cursor position in the view.

If you don't implement handle_drag_update, the return value of previous handle_drag_enter call will be used.

function view.handledragupdate(self, info, point)
  if (pointInDragArea(point))
    return gui.DraggingInfo.dragoperationcopy
    return gui.DraggingInfo.dragoperationnone

Finally, by implementing handle_drop, you can read the dragged data from the passed DraggingInfo instance.

function view.handledrop(self, info, point)
  if (info:isdataavailable('image')) {
    return true
  return false

Drag source

To make it possible for users to drag some data from current applications to other destinations, you can listen to mouse down events and use the View.DoDrag API.


function source.onmousedown(self)
  local ret = self:doDrag({{type: 'file-paths', value: {filePath}}},
                          gui.DraggingInfo.dragoperationcopy | gui.DraggingInfo.dragoperationlink)
  if (ret == gui.DraggingInfo.dragoperationcopy)
  else if (ret == gui.DraggingInfo.dragoperationlink)
  return true

The API should be called with the data to provide, and the supported drag operations. The call will be blocked until the user cancels the dragging or drops the data, and it will return the drag operation used by the destination.