如何让我的应用从/向Finder图标展开/折叠?

时间:2018-01-24 15:29:11

标签: cocoa animation finder

您知道有时当您关闭Finder窗口或文档时,它会向Finder中的表示缩小。我希望我的应用程序能够做到这一点。这有API吗?我找不到任何。

Imgur

1 个答案:

答案 0 :(得分:1)

执行摘要:如果您需要此行为,请使用NSDocument系统。

详细说明:

看起来你在GIF中使用TextEdit。碰巧,Apple publishes the source code for TextEdit as sample code。因此,我们可以查看是否有任何特殊的事情来实现这一目标。

我在TextEdit源代码中找不到任何内容。我捅了一会儿并设置了一些断点,但没有找到任何证据表明TextEdit“手动”这样做。

我发现如果你使用File>打开文件打开(而不是双击Finder中的文件),即使文件在Finder中可见,也可以获取动画关闭窗口。

但是如果你使用File>打开文件打开,然后(不关闭该窗口)双击Finder中的文件,然后执行获取动画关闭窗口。

所以我更多地讨论,设置断点并查看反汇编列表,我在-[NSWindow _close]找到了我认为重要的位。它基本上是这样的:

- (void)_close {
    if (!_wFlags.windowDying) { return };
    if (_auxiliaryStorage->_auxWFlags.windowClosed) { return; }

    void (^actuallyCloseMyself)() = ^{ ... code to actually close the window ... };

    NSWindowController *controller = self.windowController;
    if (![controller respondsToSelector:@selector(document)]) { goto noCloser; }
    NSDocument *document = controller.document;
    if (![document respondsToSelector:@selector(fileURL)]) { goto noCloser; }
    QLSeamlessDocumentCloser *closer = [[NSDocumentController _seamlessDocumentCloserClass] seamlessDocumentCloserForURL:document.fileURL];
    if (closer == nil) { goto noCloser; }
    CGRect frame = NSRectZero;
    [closer closeWindow:self contentFrame:&frame withBlock:actuallyCloseMyself];
    goto done;

noCloser:
    actuallyCloseMyself();

done:
    _auxiliaryStorage->_auxWFlags.wantsHideOnDeactivate = YES;
}

基本上,如果您的窗口附加到NSWindowController,并且控制器有NSDocument,那么AppKit将尝试使用QLSeamlessDocumentCloser设置“无缝关闭”。 QL前缀意味着它是QuickLook的一部分(该类实际上位于QuickLookUI框架中,它是Quartz框架的一部分)。

我猜想发生的事情是,当您在Finder中打开文件时,Finder告诉QuickLook系统(可能是quicklookd进程),屏幕上显示文件的图标。当调用close时(在TextEdit中),如果QuickLook有一个要接近的帧,它会在实际关闭窗口之前将窗口向下动画到该帧。如果你没有通过Finder打开文件,QuickLook没有动画的帧,所以它可能只是立即调用actuallyCloseMyself块。