|
| 1 | +//go:build darwin && !server |
| 2 | + |
| 3 | +#import <AppKit/AppKit.h> |
| 4 | +#include <stdlib.h> |
| 5 | +#include <string.h> |
| 6 | +#include "filepromise_darwin.h" |
| 7 | + |
| 8 | +// The Go-side receiver (filepromise_darwin.go's //export). |
| 9 | +extern void millFilePromiseDropped(char** paths, int count, int x, int y); |
| 10 | + |
| 11 | +// MillFilePromiseDropView receives FILE-PROMISE drags (the macOS |
| 12 | +// post-screenshot floating thumbnail, a browser image drag) that the |
| 13 | +// toolkit's own drag view structurally refuses -- it registers only |
| 14 | +// NSFilenamesPboardType, so a promise-only pasteboard never matches it |
| 15 | +// (docs/goals/0256). Registration here is promise-types-only, and the |
| 16 | +// view is inserted BELOW every existing subview: AppKit targets the |
| 17 | +// frontmost registered candidate under the pointer, so any drag that |
| 18 | +// carries real filenames keeps matching the toolkit's own topmost view |
| 19 | +// exactly as before -- existing drops are untouched by construction. |
| 20 | +@interface MillFilePromiseDropView : NSView |
| 21 | +@end |
| 22 | + |
| 23 | +@implementation MillFilePromiseDropView |
| 24 | + |
| 25 | +// Mouse events pass through to the webview underneath -- drag delivery |
| 26 | +// uses registerForDraggedTypes, not hitTest (same contract as the |
| 27 | +// toolkit's own drag view). |
| 28 | +- (NSView *)hitTest:(NSPoint)point { |
| 29 | + return nil; |
| 30 | +} |
| 31 | + |
| 32 | +- (NSDragOperation)draggingEntered:(id<NSDraggingInfo>)sender { |
| 33 | + return NSDragOperationCopy; |
| 34 | +} |
| 35 | + |
| 36 | +- (NSDragOperation)draggingUpdated:(id<NSDraggingInfo>)sender { |
| 37 | + return NSDragOperationCopy; |
| 38 | +} |
| 39 | + |
| 40 | +- (BOOL)performDragOperation:(id<NSDraggingInfo>)sender { |
| 41 | + NSPasteboard *pb = [sender draggingPasteboard]; |
| 42 | + NSArray<NSFilePromiseReceiver *> *receivers = |
| 43 | + [pb readObjectsForClasses:@[[NSFilePromiseReceiver class]] options:@{}]; |
| 44 | + if (receivers.count == 0) { |
| 45 | + return NO; |
| 46 | + } |
| 47 | + |
| 48 | + NSString *destDir = [NSTemporaryDirectory() stringByAppendingPathComponent: |
| 49 | + [NSString stringWithFormat:@"mill-promise-drop-%@", [[NSUUID UUID] UUIDString]]]; |
| 50 | + if (![[NSFileManager defaultManager] createDirectoryAtPath:destDir |
| 51 | + withIntermediateDirectories:YES |
| 52 | + attributes:nil |
| 53 | + error:nil]) { |
| 54 | + return NO; |
| 55 | + } |
| 56 | + NSURL *destURL = [NSURL fileURLWithPath:destDir isDirectory:YES]; |
| 57 | + |
| 58 | + // Same top-left coordinate math as the toolkit's own drag view -- |
| 59 | + // the frontend hit-tests these against document.elementFromPoint. |
| 60 | + NSPoint pWin = [sender draggingLocation]; |
| 61 | + NSPoint pView = [self convertPoint:pWin fromView:nil]; |
| 62 | + CGFloat contentHeight = self.window.contentView.frame.size.height; |
| 63 | + int x = (int)pView.x; |
| 64 | + int y = (int)(contentHeight - pView.y); |
| 65 | + |
| 66 | + // Receipt is asynchronous BY the API's own contract (the promise |
| 67 | + // source writes the file on its own schedule); the reader blocks |
| 68 | + // run on this background queue, never the main thread, and the |
| 69 | + // dispatch group fires the one Go callback once every promised |
| 70 | + // file has either landed or errored. |
| 71 | + NSOperationQueue *queue = [NSOperationQueue new]; |
| 72 | + dispatch_group_t group = dispatch_group_create(); |
| 73 | + NSMutableArray<NSString *> *landed = [NSMutableArray array]; |
| 74 | + NSLock *lock = [NSLock new]; |
| 75 | + for (NSFilePromiseReceiver *receiver in receivers) { |
| 76 | + dispatch_group_enter(group); |
| 77 | + [receiver receivePromisedFilesAtDestination:destURL |
| 78 | + options:@{} |
| 79 | + operationQueue:queue |
| 80 | + reader:^(NSURL *fileURL, NSError *error) { |
| 81 | + if (error == nil && fileURL != nil) { |
| 82 | + [lock lock]; |
| 83 | + [landed addObject:fileURL.path]; |
| 84 | + [lock unlock]; |
| 85 | + } |
| 86 | + dispatch_group_leave(group); |
| 87 | + }]; |
| 88 | + } |
| 89 | + dispatch_group_notify(group, dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{ |
| 90 | + NSUInteger count = landed.count; |
| 91 | + if (count == 0) { |
| 92 | + return; |
| 93 | + } |
| 94 | + char **cArr = (char **)malloc(sizeof(char *) * count); |
| 95 | + for (NSUInteger i = 0; i < count; i++) { |
| 96 | + cArr[i] = strdup([landed[i] UTF8String]); |
| 97 | + } |
| 98 | + millFilePromiseDropped(cArr, (int)count, x, y); |
| 99 | + for (NSUInteger i = 0; i < count; i++) { |
| 100 | + free(cArr[i]); |
| 101 | + } |
| 102 | + free(cArr); |
| 103 | + }); |
| 104 | + return YES; |
| 105 | +} |
| 106 | + |
| 107 | +@end |
| 108 | + |
| 109 | +void millAttachPromiseView(void *nsWindowPtr) { |
| 110 | + // Fail-safe, never fail-crash: a missing window/contentView means |
| 111 | + // no promise support this launch, not an abort at startup. |
| 112 | + if (nsWindowPtr == NULL) { |
| 113 | + return; |
| 114 | + } |
| 115 | + NSWindow *win = (__bridge NSWindow *)nsWindowPtr; |
| 116 | + NSView *content = win.contentView; |
| 117 | + if (content == nil) { |
| 118 | + return; |
| 119 | + } |
| 120 | + MillFilePromiseDropView *v = [[MillFilePromiseDropView alloc] initWithFrame:content.bounds]; |
| 121 | + v.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable; |
| 122 | + [v registerForDraggedTypes:[NSFilePromiseReceiver readableDraggedTypes]]; |
| 123 | + [content addSubview:v positioned:NSWindowBelow relativeTo:nil]; |
| 124 | +} |
0 commit comments