gpt4 book ai didi

objective-c - `[NSScreen mainscreen]` 误导文档

转载 作者:行者123 更新时间:2023-12-02 19:59:08 25 4
gpt4 key购买 nike

documentation [NSScreen mainscreen] 具有误导性。

The main screen is not necessarily the same screen that contains the menu bar or has its origin at (0, 0). The main screen refers to the screen containing the window that is currently receiving keyboard events. It is the main screen because it is the one with which the user is most likely interacting.

我尝试:

printf( "%p \n", [NSScreen mainScreen]);

for( NSScreen* sc in [NSScreen screens] ) {
NSRect f = [sc frame];
printf("%p %f %f %f %f \n", sc, f.origin.x, f.origin.y, f.size.width,f.size.height );
}

我将我的 Xcode 窗口拖到我的原生 MacBook 显示器上,确保它有键盘焦点,然后执行:

0x1006aaac0 
0x1006aaac0 0.000000 0.000000 2560.000000 1440.000000
0x1006ab3d0 0.000000 -800.000000 1280.000000 800.000000

现在我将它拖到我的 LCD 上,确保它有键盘焦点,然后执行:

0x1005a15e0 
0x1005a15e0 0.000000 0.000000 2560.000000 1440.000000
0x1005a64c0 0.000000 -800.000000 1280.000000 800.000000

所以 [NSScreen mainscreen] 每次都识别 LCD(带有菜单栏的那个)。

看来 [NSScreen mainscreen]与文档相反,实际上确实产生了带有菜单栏的屏幕。

有人愿意确认这是文档错误吗?

最佳答案

Xcode的窗口的位置跟它有什么关系呢?查询它的程序可能不是 Xcode 本身,而是您的应用程序。

无论如何,这受“系统偏好设置”>“任务控制”>“显示有单独的空间”的影响。另请参阅 10.9 AppKit release notes :

Spaces and Multiple Screens

In 10.9, we have added a feature where each screen gets its own set of spaces, and it is possible to switch between spaces on one screen without perturbing the spaces on the other screens. In this mode, a fullscreen window uses one screen, and leaves the contents of any other screens unchanged.

Each screen now has its own menu bar, and it is possible to show the Dock on any screen, provided you have the Dock set to “Position on Bottom”.

The menu bar has an active appearance on the active screen, which is typically the screen containing the key window. Menu bars on other screens have an inactive appearance.

In this mode, it is desirable for new windows to open on the active screen. In support of this model, +[NSScreen mainScreen] now returns the active screen, which is slightly different than its prior behavior of returning the screen containing the keyWindow, if any, and the zero screen otherwise.

A window restored at app launch through -restoreStateWithCoder: will return to its previous location, independent of active screen. A window positioned using -setFrameAutosaveName: will prefer the active display.

This feature can be disabled by unchecking the preference named “Displays have Separate Spaces” in the Mission Control preference pane in System Preferences. This setting only takes effect after logging out and back in, or restarting. NSScreen has API to query whether the separate space feature is enabled:

+ (BOOL)screensHaveSeparateSpaces NS_AVAILABLE_MAC(10_9);

When this feature is enabled, windows may not visibly span displays. A window will get assigned to the display containing the majority of its geometry if programmatically positioned in a spanning position. A window will get assigned to the display containing the mouse if the window is moved by the user. A window clips to the edge of the display, whether or not there is another adjacent display.

遗憾的是,许多重要信息仅在发行说明中。它并不总是包含在主要文档中。

请注意对 +mainScreen 的“旧”行为的稍微不同的解释:

… its prior behavior of returning the screen containing the keyWindow, if any, and the zero screen otherwise.

考虑到,对于一个不活动的应用程序或没有窗口的应用程序,-[NSApplication keyWindow] 将是 nil,因此 +mainScreen 将返回零屏幕。因此,引用您引用的文档:

The main screen refers to the screen containing the window that is currently receiving keyboard events.

没有说的是“在调用应用程序中”。

无论如何,如果您想要的是主显示器,即 (0, 0) 处的显示器,只需使用 NSScreen.screens[0]

关于objective-c - `[NSScreen mainscreen]` 误导文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56268640/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com