Thanks to the relentless work of @rozaimech and others in this thread, I was inspired and able to add the drag & drop gestures to the package.
The module now supports dragging and dropping both within a Pythonista app and between Pythonista and another app (latter only possible on iPads). These two cases are handled differently:
- For in-app drops, Apple method of relaying objects is skipped completely, and you can refer to any Python object to be dropped to the target view.
- For cross-app drops, we have to conform to Apple method of managing data. Currently only plain text and image drops are supported, in either direction.
- It is also good to note that
ui.TextFieldandui.TextViewviews natively act as receivers for both in-app and cross-app plain text drag and drop.
View is set to be a sender for a drap and drop operation with the drag function. Drag starts with a long press, and can end in any view that has been set as a receiver with the drop function. Views show the readiness to receive data with a green "plus" sign. You can accept only specific types of data; incompatible drop targets show a grey "forbidden" sign.
Following example covers setting up an in-app drag and drop operation between two labels. To repeat, in the in-app case, the simple string could replaced by any Python object of any complexity, passed by reference:
drag(sender_label, "Important data")
drop(receiver_label,
lambda data, sender, receiver: setattr(receiver, 'text', data),
accept=str)
See the module documentation for the two functions for details.