Skip to content

Instantly share code, notes, and snippets.

@gferreira
Last active June 14, 2019 15:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gferreira/03a5647ab092c39496c4e8d718771ec6 to your computer and use it in GitHub Desktop.
Save gferreira/03a5647ab092c39496c4e8d718771ec6 to your computer and use it in GitHub Desktop.
from vanilla import *
from AppKit import NSDragOperationCopy
genericListPboardType = "genericListPboardType"
class DragAndDropListItemsDemo:
weekdays = {
'Mon' : [],
'Tue' : [],
'Wed' : [],
'Thu' : [],
'Fri' : [],
'Sat' : [],
'Sun' : [],
}
def __init__(self):
self.w = Window((400, 200), 'drag & drop demo')
self.w.list1 = List((0, 0, 200, -0),
list(self.weekdays.keys()),
allowsMultipleSelection=True,
selectionCallback=self.list1SelectionCallback,
selfWindowDropSettings=dict(type=genericListPboardType,
allowDropOnRow=True,
allowDropBetweenRows=False,
operation=NSDragOperationCopy,
callback=self.selfDropCallback))
self.w.list2 = List((200, 0, -0, -0),
['banana', 'apple', 'orange', 'melon'],
dragSettings=dict(type=genericListPboardType,
allowDropOnRow=True,
allowDropBetweenRows=False,
callback=self.dragCallback))
self.w.open()
def list1SelectionCallback(self, sender):
selection = sender.getSelection()
selectionKeys = [day for i, day in enumerate(sender.get()) if i in selection]
for k in selectionKeys:
print(k, self.weekdays[k])
def dragCallback(self, sender, indexes):
self.draggedItems = [self.w.list2.get()[i] for i in indexes]
def selfDropCallback(self, sender, dropInfo):
isProposal = dropInfo["isProposal"]
if not isProposal:
key = self.w.list1.get()[dropInfo['rowIndex']]
for item in self.draggedItems:
if item not in self.weekdays[key]:
self.weekdays[key] += [item]
return True
DragAndDropListItemsDemo()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment