Skip to content

Commit b63f685

Browse files
corepunchclaude
andcommitted
Stacks take dropped files again: one set of drag methods for drop and reorder
The ReorderContainer category on LuaStackView redefined draggingEntered:, draggingUpdated: and performDragOperation:, silently replacing the stack's own file-drop implementation, so onDrop never saw a real drag (Diskmap's "Drop a Folder Here" and the cleanup collector did nothing). The stack now implements the dragging-destination methods once and routes each drag to its reorder delegate or to onDrop, registering both types together. _dropFiles now delivers through draggingEntered:/performDragOperation: with a real file-URL pasteboard, so tests exercise AppKit's dispatch path. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent c4bb0a2 commit b63f685

3 files changed

Lines changed: 81 additions & 34 deletions

File tree

‎src/appkit/constructors.m‎

Lines changed: 60 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
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
}
6394
static 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.
91132
static 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

‎src/appkit/reorder_container.m‎

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#pragma mark - Native drag-to-reorder for AppKit stack containers
22

3-
@interface LuaContainerReorder : NSObject <NSDraggingSource>
3+
@interface LuaContainerReorder : NSObject <NSDraggingSource, LuaStackReorder>
44
@property(nonatomic, weak) NSView *container;
55
@property(nonatomic, copy) NSArray<NSView *> *items;
66
@property(nonatomic, strong) LuaReg *callback;
@@ -75,26 +75,11 @@ - (void)moveItemFrom:(NSUInteger)from to:(NSUInteger)to {
7575
}
7676
@end
7777

78-
static char kAppKitReorderContainerKey;
79-
@implementation LuaStackView (ReorderContainer)
80-
- (NSDragOperation)draggingEntered:(id<NSDraggingInfo>)sender {
81-
LuaContainerReorder *delegate = objc_getAssociatedObject(self, &kAppKitReorderContainerKey);
82-
return [delegate validateDrag:sender];
83-
}
84-
- (NSDragOperation)draggingUpdated:(id<NSDraggingInfo>)sender {
85-
LuaContainerReorder *delegate = objc_getAssociatedObject(self, &kAppKitReorderContainerKey);
86-
return [delegate validateDrag:sender];
87-
}
88-
- (BOOL)performDragOperation:(id<NSDraggingInfo>)sender {
89-
LuaContainerReorder *delegate = objc_getAssociatedObject(self, &kAppKitReorderContainerKey);
90-
return [delegate acceptDrag:sender];
91-
}
92-
@end
93-
9478
static int bridge_AppKitReorder_attach(lua_State *L) {
95-
NSView *container = check_view(L, 1);
96-
if (![container isKindOfClass:LuaStackView.class])
79+
NSView *view = check_view(L, 1);
80+
if (![view isKindOfClass:LuaStackView.class])
9781
return luaL_error(L, "reorder container requires a stack-style native layout");
82+
LuaStackView *container = (LuaStackView *)view;
9883
luaL_checktype(L, 2, LUA_TTABLE);
9984
LuaReg *callback = lua_reg_opt(L, 3);
10085
if (!callback) return luaL_error(L, "reorder container requires a callback");
@@ -114,15 +99,15 @@ static int bridge_AppKitReorder_attach(lua_State *L) {
11499
initWithTarget:delegate action:@selector(dragItem:)];
115100
[item addGestureRecognizer:gesture];
116101
}
117-
[container registerForDraggedTypes:@[@"org.luaobjc.reorder-item"]];
118-
objc_setAssociatedObject(container, &kAppKitReorderContainerKey, delegate,
119-
OBJC_ASSOCIATION_RETAIN);
102+
container.reorder = delegate;
103+
stack_register_drag_types(container);
120104
return 0;
121105
}
122106

123107
static int bridge_AppKitReorder_testMove(lua_State *L) {
124108
NSView *container = check_view(L, 1);
125-
LuaContainerReorder *delegate = objc_getAssociatedObject(container, &kAppKitReorderContainerKey);
109+
LuaContainerReorder *delegate = [container isKindOfClass:LuaStackView.class]
110+
? (LuaContainerReorder *)((LuaStackView *)container).reorder : nil;
126111
if (!delegate) return luaL_error(L, "view has no reorder container");
127112
lua_Integer from = luaL_checkinteger(L, 2);
128113
lua_Integer to = luaL_checkinteger(L, 3);

‎tests/drag_drop.test.lua‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,19 @@ local _, externalRefs = xml.render('<VStack id="zone" onDrop="drop" dropExternal
7474
t.expect(externalRefs.zone.dropExternalOnly, "dropExternalOnly reaches the native stack")
7575
t.expect(not targetRefs.zone.dropExternalOnly, "stacks take drags from anywhere by default")
7676
t.expect(ns._dropFiles(externalRefs.zone, {"/tmp/x"}), "files from the Finder still reach the handler")
77+
t.expect(not ns._dropFiles(externalRefs.zone, {"/tmp/x"}, true), "files dragged inside the app pass it by")
78+
79+
-- A reorderable stack is still a file drop target: both features share the
80+
-- stack's one set of dragging-destination methods.
81+
local bothDropped
82+
local _, bothRefs = xml.render([[<VStack id="zone" onDrop="drop" reorderable="true" reorderContainer="move">
83+
<Label text="A" /><Label text="B" />
84+
</VStack>]], {actions = {drop = function(paths) bothDropped = paths; return true end, move = function() end}}, ns)
85+
t.expect(ns._dropFiles(bothRefs.zone, {"/tmp/z"}), "a reorderable stack takes dropped files")
86+
t.assertEqual(bothDropped and bothDropped[1], "/tmp/z", "the file reaches onDrop, not the reorder")
87+
local types = {}
88+
for _, type in ipairs(bothRefs.zone.registeredDraggedTypes) do types[tostring(type)] = true end
89+
t.expect(types["public.file-url"] and types["org.luaobjc.reorder-item"], "the stack registers files and its items")
7790

7891
-- Folders and files opened with the app (Dock icon, Open With) wait for a
7992
-- handler and then reach it.

0 commit comments

Comments
 (0)