11/* Native constructors exported by the AppKit module. */
22#import < QuartzCore/QuartzCore.h>
33
4+ /* A stack is the drag destination for both of its drag features: files
5+ * dropped on it (`onDrop`) and its own items dragged to reorder
6+ * (reorder_container.m). AppKit sends one set of NSDraggingDestination
7+ * messages per view, so the stack implements them once and routes each drag
8+ * by its payload. A category method with the same selector would silently
9+ * replace these, which is how file drops once stopped reaching `onDrop`. */
10+ @protocol LuaStackReorder <NSObject >
11+ - (NSDragOperation )validateDrag : (id <NSDraggingInfo >)info ;
12+ - (BOOL )acceptDrag : (id <NSDraggingInfo >)info ;
13+ @end
14+
415/* AppKit has no KVC property to exclude a container subtree from hit testing.
516 * Keep the native NSView traversal and opt out before it visits descendants. */
617@interface LuaStackView : NSView
@@ -12,6 +23,8 @@ @interface LuaStackView : NSView
1223// Accept only drags from other applications: a drag that starts in this
1324// app has a dragging source, one from the Finder has none.
1425@property (nonatomic ) BOOL dropExternalOnly;
26+ // Moves the stack's own items when one is dragged within it.
27+ @property (nonatomic , strong ) id <LuaStackReorder> reorder;
1528@end
1629@implementation LuaStackView
1730- (instancetype )initWithFrame : (NSRect )frame {
@@ -41,23 +54,41 @@ - (NSView *)hitTest:(NSPoint)point { return _allowsHitTesting ? [super hitTest:p
4154 for (NSURL *url in urls) if (url.path .length ) [paths addObject: url.path];
4255 return paths;
4356}
57+ // Types the stack takes: file URLs for `onDrop`, its item type for reorder.
58+ static void stack_register_drag_types (LuaStackView *view) {
59+ NSMutableArray *types = [NSMutableArray array ];
60+ if (view.dropReg ) [types addObject: NSPasteboardTypeFileURL ];
61+ if (view.reorder ) [types addObject: @" org.luaobjc.reorder-item" ];
62+ if (types.count ) [view registerForDraggedTypes: types];
63+ else [view unregisterDraggedTypes ];
64+ }
4465// While files hover over it, the stack shows the system focus indicator.
4566- (void )setDropTargeted : (BOOL )targeted {
4667 _dropTargeted = targeted;
4768 self.wantsLayer = YES ;
4869 self.layer .borderWidth = targeted ? kDropHighlightWidth : 0 ;
4970 self.layer .borderColor = NSColor .keyboardFocusIndicatorColor .CGColor ;
5071}
51- - (NSDragOperation ) draggingEntered : (id <NSDraggingInfo >)sender {
52- BOOL accepts = self.dropReg && (!self.dropExternalOnly || sender.draggingSource == nil )
72+ - (BOOL ) acceptsFileDrag : (id <NSDraggingInfo >)sender {
73+ return self.dropReg && (!self.dropExternalOnly || sender.draggingSource == nil )
5374 && stack_drop_paths (sender).count > 0 ;
54- self.dropTargeted = accepts;
55- return accepts ? NSDragOperationCopy : NSDragOperationNone;
75+ }
76+ - (NSDragOperation )draggingEntered : (id <NSDraggingInfo >)sender {
77+ NSDragOperation reorder = [self .reorder validateDrag: sender];
78+ if (reorder != NSDragOperationNone) return reorder;
79+ self.dropTargeted = [self acceptsFileDrag: sender];
80+ return self.dropTargeted ? NSDragOperationCopy : NSDragOperationNone;
81+ }
82+ - (NSDragOperation )draggingUpdated : (id <NSDraggingInfo >)sender {
83+ NSDragOperation reorder = [self .reorder validateDrag: sender];
84+ if (reorder != NSDragOperationNone) return reorder;
85+ return self.dropTargeted ? NSDragOperationCopy : NSDragOperationNone;
5686}
5787- (void )draggingExited : (id <NSDraggingInfo >)sender { (void )sender; self.dropTargeted = NO ; }
5888- (BOOL )performDragOperation : (id <NSDraggingInfo >)sender {
5989 self.dropTargeted = NO ;
60- if (self.dropExternalOnly && sender.draggingSource != nil ) return NO ;
90+ if ([self .reorder validateDrag: sender] != NSDragOperationNone) return [self .reorder acceptDrag: sender];
91+ if (![self acceptsFileDrag: sender]) return NO ;
6192 return stack_perform_drop (self, stack_drop_paths (sender));
6293}
6394static BOOL stack_perform_drop (LuaStackView *view, NSArray <NSString *> *paths) {
@@ -82,23 +113,41 @@ static int bridge_set_drop_handler(lua_State *L) {
82113 LuaStackView *view = lua_objc_check_object (L, 1 , [LuaStackView class ], " stack" );
83114 view.dropReg = lua_reg_opt (L, 2 );
84115 view.dropExternalOnly = lua_toboolean (L, 3 );
85- if (view.dropReg ) [view registerForDraggedTypes: @[NSPasteboardTypeFileURL ]];
86- else [view unregisterDraggedTypes ];
116+ stack_register_drag_types (view);
87117 return 0 ;
88118}
89119
90- // Test hook: _dropFiles(stack, paths) as if they were dropped on it.
120+ /* The drag a test hook hands to a stack: files on a private pasteboard, from
121+ * the Finder (no source) or from inside this app. Only the NSDraggingInfo
122+ * members the stack reads are implemented. */
123+ @interface LuaTestFileDrag : NSObject
124+ @property (nonatomic , strong ) NSPasteboard *draggingPasteboard;
125+ @property (nonatomic , strong ) id draggingSource;
126+ @end
127+ @implementation LuaTestFileDrag
128+ @end
129+
130+ // Test hook: _dropFiles(stack, paths[, fromThisApp]) delivers the drop
131+ // through the stack's own NSDraggingDestination methods, as AppKit does.
91132static int bridge_drop_files (lua_State *L) {
92133 LuaStackView *view = lua_objc_check_object (L, 1 , [LuaStackView class ], " stack" );
93134 luaL_checktype (L, 2 , LUA_TTABLE );
94- NSMutableArray *paths = [NSMutableArray array ];
135+ NSMutableArray < NSURL *> *urls = [NSMutableArray array ];
95136 for (lua_Integer i = 1 ; ; i++) {
96137 lua_rawgeti (L, 2 , i);
97138 if (lua_isnil (L, -1 )) { lua_pop (L, 1 ); break ; }
98- [paths addObject: [NSString stringWithUTF8String: luaL_checkstring (L, -1 )]];
139+ [urls addObject: [NSURL fileURLWithPath: [ NSString stringWithUTF8String: luaL_checkstring (L, -1 )] ]];
99140 lua_pop (L, 1 );
100141 }
101- lua_pushboolean (L, stack_perform_drop (view, paths));
142+ LuaTestFileDrag *drag = [LuaTestFileDrag new ];
143+ drag.draggingPasteboard = [NSPasteboard pasteboardWithUniqueName ];
144+ [drag.draggingPasteboard clearContents ];
145+ [drag.draggingPasteboard writeObjects: urls];
146+ if (lua_toboolean (L, 3 )) drag.draggingSource = view;
147+ id <NSDraggingInfo > info = (id <NSDraggingInfo >)drag;
148+ BOOL accepted = [view draggingEntered: info] != NSDragOperationNone && [view performDragOperation: info];
149+ [drag.draggingPasteboard releaseGlobally ];
150+ lua_pushboolean (L, accepted);
102151 return 1 ;
103152}
104153
0 commit comments