显示模态 NSWindow,而不激活其他应用程序窗口

发布于 2024-10-08 08:13:30 字数 541 浏览 8 评论 0原文

我有一个在菜单栏中正确显示的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

客…行舟 2024-10-15 08:13:30

不要创建 NSWindow,而是创建一个具有 NSNonactivatingPanelMask 样式的 NSPanel。然后,您可以执行通常的 makeKeyAndOrderFront:orderOut: 来根据需要显示/隐藏面板。

Instead of creating an NSWindow, create an NSPanel with the style NSNonactivatingPanelMask. You can then do the usual makeKeyAndOrderFront: and orderOut: to show/hide panel as needed.

半透明的墙 2024-10-15 08:13:30

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

梓梦 2024-10-15 08:13:30

几年前 Ken Thomases 在 cocoa-dev 列表上提出的解决方案看起来也适用于此:

[[NSApplication sharedApplication] hide:self];
[[NSApplication sharedApplication] performSelector:@selector(unhideWithoutActivation) 
                                        withObject:nil 
                                        afterDelay:0.05];

理论上,它告诉应用程序隐藏自身并在窗口堆栈的底部取消隐藏。

您还可以拦截鼠标单击事件并使用 [NSApp PreventWindowOrdering]

A solution by Ken Thomases on the cocoa-dev list a couple years ago looks applicable here too:

[[NSApplication sharedApplication] hide:self];
[[NSApplication sharedApplication] performSelector:@selector(unhideWithoutActivation) 
                                        withObject:nil 
                                        afterDelay:0.05];

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]

傾旎 2024-10-15 08:13:30

您可以尝试以下操作:

...
if ([NSApp isHidden])
    [myWindow makeKeyAndOrderFront:self];
else
    [NSApp runModalForWindow:myWindow];
... 

完成后:

...
if ([NSApp isHidden])
    [myWindow orderOut:self];
else
    [NSApp stopModal];
... 

You can try something like:

...
if ([NSApp isHidden])
    [myWindow makeKeyAndOrderFront:self];
else
    [NSApp runModalForWindow:myWindow];
... 

and when finish:

...
if ([NSApp isHidden])
    [myWindow orderOut:self];
else
    [NSApp stopModal];
... 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文