奇怪的行为:从Stacks拖动到状态项不起作用

Ale*_*dre 4 cocoa drag-and-drop nsstatusitem

我的应用程序允许拖动到主窗口和状态项.

  • 如果我将文件从Stacks拖到我的窗口,它可以很好地工作.
  • 如果我将文件从Finder拖到我的窗口,它可以很好地工作.
  • 如果我将文件从Finder拖到我的状态项,它可以很好地工作.
  • 如果我将文件从Stack拖到我的状态项,它不起作用.

窗口和状态项都使用完全相同的拖放处理代码.

有趣的是,当文件从Stacks拖到状态项上时,光标会按预期更改,因为- (NSDragOperation)draggingEntered:(id)sender {NSPasteboard*pboard; NSDragOperation sourceDragMask; 方法按预期调用.

但是,当文件被删除时,- (BOOL)performDragOperation:(id)sender {NSPasteboard*pboard; NSDragOperation sourceDragMask; 方法不被调用.

这是第一种方法的实现:

- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender {
    NSPasteboard *pboard;
    NSDragOperation sourceDragMask;

    sourceDragMask = [sender draggingSourceOperationMask];
    pboard = [sender draggingPasteboard];

    if ( [[pboard types] containsObject:NSColorPboardType] ) {
        if (sourceDragMask & NSDragOperationGeneric) {
            return NSDragOperationGeneric;
        }
    }
    if ( [[pboard types] containsObject:NSFilenamesPboardType] ) {
        if (sourceDragMask & NSDragOperationLink) {
            return NSDragOperationLink;
        } else if (sourceDragMask & NSDragOperationCopy) {
            return NSDragOperationCopy;
        }
    }

    return NSDragOperationNone;
}
Run Code Online (Sandbox Code Playgroud)

谢谢!

Lev*_*ink 5

这是一个合法的问题.我已经向Apple提交了一份错误报告.http://openradar.appspot.com/radar?id=1745403

与此同时,我找到了一个解决方法.即使从不调用performDragOperation:,draggingEnded:仍然是.您仍然可以通过检查"draggingLocation"点是否在NSView的rect内部来判断文件是否已放在NSStatusItem上.这是一个例子:

- (void)draggingEnded:(id<NSDraggingInfo>)sender
{
    if(NSPointInRect([sender draggingLocation],self.frame)){
        //The file was actually dropped on the view so call the performDrag manually
        [self performDragOperation:sender];
    }
}
Run Code Online (Sandbox Code Playgroud)

希望这有助于修复错误.