@@ -294,6 +294,11 @@ def tableView_acceptDrop_row_dropOperation_(
294
294
295
295
class VanillaList2TableViewSubclass (AppKit .NSTableView ):
296
296
297
+ def keyDown_ (self , event ):
298
+ didSomething = self .vanillaWrapper ()._keyDown (event )
299
+ if not didSomething :
300
+ super ().keyDown_ (event )
301
+
297
302
def draggingEntered_ (self , draggingInfo ):
298
303
super ().draggingEntered_ (draggingInfo )
299
304
return self .vanillaWrapper ()._dropCandidateEntered (draggingInfo )
@@ -405,7 +410,7 @@ class List2(ScrollView, DropTargetProtocolMixIn):
405
410
406
411
**menuCallback** Callback to be called when a contextual menu is requested.
407
412
408
- # **enableDelete** A boolean representing if items in the list can be deleted via the interface.
413
+ **enableDelete** A boolean representing if items in the list can be deleted via the interface.
409
414
410
415
**enableTypingSensitivity** A boolean representing if typing in the list will jump to the
411
416
closest match as the entered keystrokes.
@@ -508,6 +513,7 @@ def __init__(self,
508
513
allowsEmptySelection = True ,
509
514
allowsSorting = True ,
510
515
allowColumnReordering = True ,
516
+ enableDelete = False ,
511
517
enableTypingSensitivity = False ,
512
518
showColumnTitles = True ,
513
519
drawFocusRing = True ,
@@ -561,6 +567,7 @@ def __init__(self,
561
567
self ._tableView .setAllowsMultipleSelection_ (allowsMultipleSelection )
562
568
self ._tableView .setAllowsColumnReordering_ (allowColumnReordering )
563
569
self ._allowsSorting = allowsSorting
570
+ self ._enableDelete = enableDelete
564
571
self ._tableView .setAllowsTypeSelect_ (enableTypingSensitivity )
565
572
# visual attributes
566
573
if not showColumnTitles :
@@ -860,6 +867,18 @@ def scrollToIndex(self, row):
860
867
"""
861
868
self ._tableView .scrollRowToVisible_ (row )
862
869
870
+ def removeSelection (self ):
871
+ """
872
+ Remove selected items.
873
+ """
874
+ selection = self .getSelectedIndexes ()
875
+ if not selection :
876
+ return
877
+ items = self .get ()
878
+ for index in reversed (sorted (selection )):
879
+ del items [index ]
880
+ self .set (items )
881
+
863
882
# Drag
864
883
865
884
_dragCandidateCallback = None
@@ -882,6 +901,33 @@ def _getPasteboardDataForIndex(self, index):
882
901
return None
883
902
return makePasteboardItem (typesAndValues )
884
903
904
+ # key down
905
+
906
+ def _keyDown (self , event ):
907
+ # this method is called by the NSTableView subclass after a key down
908
+ # has occurred. the subclass expects that a boolean will be returned
909
+ # that indicates if this method has done something (delete an item or
910
+ # select an item). if False is returned, the delegate calls the super
911
+ # method to insure standard key down behavior.
912
+ #
913
+ # get the characters
914
+ characters = event .characters ()
915
+ # get the field editor
916
+ #
917
+ deleteCharacters = [
918
+ AppKit .NSBackspaceCharacter ,
919
+ AppKit .NSDeleteFunctionKey ,
920
+ AppKit .NSDeleteCharacter ,
921
+ chr (0x007F ),
922
+ ]
923
+ if characters in deleteCharacters :
924
+ if self ._enableDelete :
925
+ self .removeSelection ()
926
+ return True
927
+
928
+ return False
929
+
930
+
885
931
# contextual menu
886
932
887
933
def _menuForEvent (self , event ):
0 commit comments