简体   繁体   English

如何制作类似 Finder 的 Dock 菜单?

[英]How to make a Finder-like Dock menu?

When a user clicks on a Dock icon with the right mouse button (Or a command button + mouse click) on a running application - he can see a Dock menu.当用户在正在运行的应用程序上使用鼠标右键(或命令按钮 + 鼠标单击)单击 Dock 图标时 - 他可以看到 Dock 菜单。 Which usually consists of 3 parts:它通常由 3 个部分组成:

http://i55.tinypic.com/i1for6.png

a list of all opened documents by this application (red), a custom application's part of the menu (yellow) and default items that are added implicitly to all the items (blue).此应用程序打开的所有文档的列表(红色)、自定义应用程序的菜单部分(黄色)和隐式添加到所有项目的默认项目(蓝色)。

Here apple explains that we can define these custom items (yellow) by implementing an application delegate's method ‑(NSMenu *)applicationDockMenu:(NSApplication *)sender , which should return a valid menu (or by defining this Dock menu in Interface Builder).苹果在这里解释说,我们可以通过实现应用程序委托的方法‑(NSMenu *)applicationDockMenu:(NSApplication *)sender来定义这些自定义项(黄色),该方法应返回有效菜单(或通过在 Interface Builder 中定义此 Dock 菜单)。

If you try to open a dock menu for Finder, you can see an unusual menu:如果您尝试打开 Finder 的停靠菜单,您会看到一个不寻常的菜单:

不寻常的菜单

It is unusual because it does not have ordinary items for quitting and "Options" submenu - as any other launched application would have had by default.这是不寻常的,因为它没有普通的退出项目和“选项”子菜单 - 就像任何其他启动的应用程序默认情况下一样。 Instead it has only "Hide".相反,它只有“隐藏”。

I am pretty sure an application or a nib file can override this default "system" (blue) part of a Dock menu.我很确定应用程序或 nib 文件可以覆盖 Dock 菜单的这个默认“系统”(蓝色)部分。 Maybe anyone has any idea how that can be achieved?也许有人知道如何实现这一目标?

I am developing a security application that should not be allowed to quit or selected to be launched "at Login", but it should run all the time when the user is logged in (just like Finder, it may be halted only using Force application to Quit or Activity monitor).我正在开发一个不应允许退出或选择在“登录”时启动的安全应用程序,但它应该在用户登录时一直运行(就像 Finder 一样,它可能只使用强制应用程序来停止退出或活动监视器)。 I have good reasons to want to get rid of it.我有充分的理由想要摆脱它。 I am aware that this is not exactly a Mac OS way, but this is not exactly an ordinary application.我知道这不完全是 Mac OS 的方式,但这不完全是一个普通的应用程序。 Have anyone done anything similar?有没有人做过类似的事情?

Thank you谢谢

The Dock menus are actually put up by the Dock process itself and it just proxies each application's dockMenus, so you can't do anything directly about it. Dock 菜单实际上是由 Dock 进程本身建立的,它只是代理每个应用程序的 dockMenus,所以你不能直接对它做任何事情。

What would work is: install a Quartz Event Tap, intercept clicks routed to the Dock process, check if it's on your own app's icon, and then put up your own custom menu.可行的是:安装 Quartz Event Tap,拦截路由到 Dock 进程的点击,检查它是否在您自己的应用程序图标上,然后放置您自己的自定义菜单。 Tricky, and not possible at all for a sandboxed app.棘手,对于沙盒应用程序来说根本不可能。

It's pretty simple: create a menu in the nib file, and then connect NSApplication 's dockMenu outlet to that menu.这很简单:在 nib 文件中创建一个菜单,然后将NSApplicationdockMenu出口连接到该菜单。 You can find more information here .您可以在此处找到更多信息。

Or you can add menu items to the existing menu via code.或者您可以通过代码将菜单项添加到现有菜单。 You can use -[NSApplication applicationDockMenu:] to get the existing dock menu and add items programmatically.您可以使用-[NSApplication applicationDockMenu:]来获取现有的停靠菜单并以编程方式添加项目。

The Finder's menu is special because the Finder itself is special: it cannot be quit, it must remain in the Dock at all times, it must be opened on login, and you cannot reveal the location of the Finder in the Finder. Finder 的菜单很特别,因为 Finder 本身很特别:它不能退出,必须始终保留在 Dock 中,必须在登录时打开,并且您不能在 Finder 中显示 Finder 的位置。

What you're looking for simply does not exist.你要找的东西根本不存在。 There is no way to do this.没有办法做到这一点。 No application can override this area.没有应用程序可以覆盖该区域。

Instead of messing with the dock menu, can you just add the LSUIElement key set to YES to your app's Info.plist file?您可以将设置为YESLSUIElement键添加到应用程序的Info.plist文件中,而不是弄乱停靠菜单吗? This will stop your app from appearing in the dock at all.这将完全阻止您的应用程序出现在 Dock 中。

It will, however, also prevent it from having a menu bar.但是,它也会阻止它具有菜单栏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM