使NSPanel中的NSView成为无关键窗口状态的第一响应者

发布于 2024-10-16 06:49:50 字数 91 浏览 0 评论 0原文

是否可以在不提供 NSPanel 关键窗口状态(使主应用程序窗口退出关键)的情况下,为 NSPanel 内的 NSView 提供第一响应者状态?

谢谢。

Is it possible to give an NSView inside an NSPanel first responder status without giving the NSPanel key window status (making the main application window resign key)?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

绻影浮沉 2024-10-23 06:49:50

好吧,我最终解决了这个问题,但是需要进行大量研究,因此我将在这里发布详细信息,以防其他人遇到同样的问题。首先,一些基础知识:

  1. 不可能同时有 2 个窗口实际上是关键
  2. 可以通过覆盖 -isKeyWindow 来伪造一个窗口,使其认为它是关键,但这不会给出视图包含在窗口第一响应者状态中。

我的场景

我在主应用程序窗口中添加了一个包含 NSTableView 的子窗口(原因无关)。子窗口是一个带有 NSBorderlessWindowMaskNSPanel。我想给 NSTableView 第一响应者状态而不使面板成为关键窗口,因为它夺走了主窗口的焦点(子窗口幻象的全部目的是使子窗口看起来像它是主窗口的一部分)。

我尝试的第一件事是通过覆盖 isKeyWindow 返回 YES 来欺骗表视图,使其认为它位于关键窗口内。这使得表视图绘制得好像它是第一响应者一样,但仍然没有赋予它第一响应者状态。

解决方案

因此默认情况下,NSBorderlessWindowMask 不允许窗口成为关键。为了使表视图成为第一响应者,窗口必须是关键,因此我重写了无边框窗口子类中的 canBecomeKeyWindow 以返回 YES。当然,这剥夺了主窗口的关键状态,这是我想避免的事情之一。为了解决这个问题,我对我的 ma​​in 窗口进行了子类化,并覆盖了以下方法:

- (BOOL)isMainWindow
{
    return YES;
}

- (BOOL)isKeyWindow
{
    return ([NSApp isActive]) ? YES : [super isKeyWindow];
}

该子类检查应用程序是否处于活动状态,如果是,则始终返回 YES,这样就不会无论应用程序中哪个窗口处于活动状态,主窗口始终会表现得好像它仍然是关键窗口。这种方式给人一种错觉,即您可以同时让多个窗口成为关键,并使您能够将关键窗口状态转移到另一个窗口,而不会在主窗口上丢失它。希望这有帮助!

Well, I ended up figuring this one out, but it took a lot of research so I'll post the details here in case anyone else runs into the same problem. First of all, a few basics:

  1. It's impossible to have 2 windows actually be key at the same time
  2. It's possible to fake a window into thinking it's key by overriding -isKeyWindow but that won't give the views contained in the window first responder status.

My Scenario:

I added a child window containing an NSTableView into my main application window (the reason is irrelavant). The child window was an NSPanel with NSBorderlessWindowMask. I wanted to give the NSTableView first responder status without making the panel the key window because it took away focus from the main window (and the whole point of the child window illusion was to make the child window look like it was part of the main window).

The first thing I tried was fooling the table view into thinking that it was inside the key window by overriding isKeyWindow to return YES. This made the table view draw as if it were the first responder, but still did not give it first responder status.

The Solution:

So by default, NSBorderlessWindowMask will not allow the window to become key. To make the table view first responder, the window had to be key so I overrode canBecomeKeyWindow in the borderless window subclass to return YES. This, of course, took away key status from the main window, which was one of the things I wanted to avoid. To fix this, I subclassed my main window and overrode the following methods:

- (BOOL)isMainWindow
{
    return YES;
}

- (BOOL)isKeyWindow
{
    return ([NSApp isActive]) ? YES : [super isKeyWindow];
}

This subclass checks if the application is active, and if it is, it always returns YES so that no matter what window is active in your application, the main window will always behave as if it is still key. This sort of gives the illusion that you can have multiple windows be key at the same time and enables you to shift key window status to another window without losing it on your main window. Hope this helps!

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