robotools / vanilla

A Pythonic wrapper around Cocoa.
MIT License
78 stars 28 forks source link

Drag and drop issue : how can I make "dropOnRow" work? #202

Closed nanife closed 9 months ago

nanife commented 9 months ago

Hello,

I want to merge texts between two List controls by drag and drop. Though I set "allowDropOnRow = True", I only see the 'between rows' indicator only. Please let me know how I can figure it out. Below is the sample code changed from https://github.com/robotools/vanilla/issues/123.

from vanilla import *
from AppKit import NSPasteboardTypeString

class DragAndDropListTest:

    list0 = ['A', 'B', 'C', 'D', 'E']
    list1 = ['1', '2', '3', '4', '5']

    def __init__(self):
        self.dropIndex = 0
        self.w = Window((320, 200), 'drag & drop demo')
        self.w.list0 = List((0, 0, 160, -0),
                self.list0,
                selfWindowDropSettings = dict(
                    type = NSPasteboardTypeString,
                    allowDropOnRow = True,
                    allowDropBetweenRows = False,
                    callback = self.dropCallback
                )
        )
        self.w.list1 = List((160, 0, 160, -0),
                self.list1,
                dragSettings = dict(
                    type = NSPasteboardTypeString,
                    **allowDropOnRow = True,**
                    allowDropBetweenRows = False,
                    callback = self.dragCallback
                ),
        )
        self.w.list0.setSelection([])
        self.w.list1.setSelection([])
        self.w.open()

    def dragCallback(self, sender, indexes):
        self.draggedIndexes = indexes

    def dropCallback(self, sender, dropInfo):
        isProposal = dropInfo['isProposal']
        if not isProposal:
            dropIndex = dropInfo['rowIndex']
            self.mergeText( self.list0, self.draggedIndexes, dropIndex, self.list1 )
            self.draggedIndexes = []
            self.w.list1.setSelection([])
        return True

    def mergeText(self, targetList, draggedIndexes, dropIndex, sourceList ):
        """
        merge list1 texts into the list0
        """
        strTemp = ''.join([sourceList[idx]  for idx in draggedIndexes])
        targetList[dropIndex] = targetList[dropIndex] + strTemp
        self.w.list0[dropIndex] = targetList[dropIndex]

DragAndDropListTest()
typemytype commented 9 months ago

you had some typos: allowsDropOnRows and allowsDropBetweenRows

good luck!

from vanilla import *
from AppKit import NSPasteboardTypeString

class DragAndDropListTest:

    list0 = ['A', 'B', 'C', 'D', 'E']
    list1 = ['1', '2', '3', 'foo', '5']

    def __init__(self):
        self.dropIndex = 0
        self.w = Window((320, 200), 'drag & drop demo')
        self.w.list0 = List((0, 0, 160, -0),
                self.list0,
                selfWindowDropSettings = dict(
                    type = NSPasteboardTypeString,
                    allowsDropOnRows = True,
                    allowsDropBetweenRows = False,
                    callback = self.dropCallback
                )
        )
        self.w.list1 = List((160, 0, 160, -0),
                self.list1,
                dragSettings = dict(
                    type = NSPasteboardTypeString,
                    callback = self.dragCallback
                ),
        )
        self.w.list0.setSelection([])
        self.w.list1.setSelection([])
        self.w.open()

    def dragCallback(self, sender, indexes):
        return indexes

    def dropCallback(self, sender, dropInfo):
        print(dropInfo)
        isProposal = dropInfo['isProposal']
        dropOnRow = dropInfo['dropOnRow']
        indexes = indexes = [int(i) for i in sorted(dropInfo["data"])]
        if dropOnRow and not isProposal:
            dropIndex = dropInfo['rowIndex']

            self.mergeText(self.list0, indexes, dropIndex, self.list1 )            
            self.w.list1.setSelection([])
        return True

    def mergeText(self, targetList, draggedIndexes, dropIndex, sourceList ):
        """
        merge list1 texts into the list0
        """
        strTemp = ''.join([sourceList[idx]  for idx in draggedIndexes])
        targetList[dropIndex] = targetList[dropIndex] + strTemp
        self.w.list0[dropIndex] = targetList[dropIndex]

DragAndDropListTest()
nanife commented 9 months ago

Wow, Thanks for your quick reply and help!!