显示模态 NSWindow,而不激活其他应用程序窗口
我有一个在菜单栏中正确显示的 NSStatusItem
。其中一个项目(单击时)显示我的应用程序中的模式 NSWindow,这意味着执行一次性任务,然后消失。 (例如,用户输入一小段文本,单击“保存”,模式 NSWindow 消失。)
当应用程序在后台运行时,会出现此问题。模式窗口正确地显示在前台运行的任何应用程序之上,但是当用户单击“保存”按钮时,应用程序的其余窗口也会被激活。这是不可取的,因为用户随后必须单击返回他们正在使用的任何应用程序。 (破坏了 NSStatusItem 的便利性。)我使用以下方式显示模式窗口:
[myWindow setFrame:finalRect display:YES animate:NO];
[myWindow setLevel:NSPopUpMenuWindowLevel];
[NSApp runModalForWindow:myWindow];
有什么方法可以防止弹出窗口中的单击/事件导致应用程序的其余部分变为活动状态?或者有一种方法让 NSApp 知道这个特定的面板不应自动激活应用程序的其余部分?谢谢!
I have an NSStatusItem
that is properly displaying in the MenuBar. One of the items (when clicked) displays a modal NSWindow from my application, which is meant to perform a one-off task, then disappear. (Eg. the user enters a small bit of text, clicks "Save", and the modal NSWindow goes away.)
The issue occurs when the application is running in the background. The modal window properly appears above whatever application is running in the foreground, but when the user clicks the "Save" button, the rest of the application's windows also are made active. This is undesirable, as the user then has to click back to whatever app they were using. (Destroying the convenience of the NSStatusItem.) I'm displaying the modal window using:
[myWindow setFrame:finalRect display:YES animate:NO];
[myWindow setLevel:NSPopUpMenuWindowLevel];
[NSApp runModalForWindow:myWindow];
Is there any way to prevent clicks/events in my popup window from causing the rest of the application to become active? Or a way to let NSApp know that this particular panel shouldn't automatically activate the rest of the app? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不要创建
NSWindow
,而是创建一个具有NSNonactivatingPanelMask
样式的NSPanel
。然后,您可以执行通常的makeKeyAndOrderFront:
和orderOut:
来根据需要显示/隐藏面板。Instead of creating an
NSWindow
, create anNSPanel
with the styleNSNonactivatingPanelMask
. You can then do the usualmakeKeyAndOrderFront:
andorderOut:
to show/hide panel as needed.NSApp 的 beginModalSessionForWindow、runModalSession、endModalSession 是您需要的方法。
看看这里的例子如何使用它:
创建完全自定义的 NSAlert
NSApp's beginModalSessionForWindow, runModalSession, endModalSession are methods you need.
Have a look here for example how to use it:
Creating a fully customized NSAlert
几年前 Ken Thomases 在 cocoa-dev 列表上提出的解决方案看起来也适用于此:
理论上,它告诉应用程序隐藏自身并在窗口堆栈的底部取消隐藏。
您还可以拦截鼠标单击事件并使用
[NSApp PreventWindowOrdering]
A solution by Ken Thomases on the cocoa-dev list a couple years ago looks applicable here too:
Which in theory tells the application to hide itself and unhide at the bottom of the window stack.
You could also intercept the mouse click event and use
[NSApp preventWindowOrdering]
您可以尝试以下操作:
完成后:
You can try something like:
and when finish: