Cocoa Dock触发NSApplicationDidChangeScreenParametersNotification [英] Cocoa Dock fires NSApplicationDidChangeScreenParametersNotification

查看:584
本文介绍了Cocoa Dock触发NSApplicationDidChangeScreenParametersNotification的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更改坞站位置时Cocoa正在触发NSApplicationDidChangeScreenParametersNotification:

When changing the dock position Cocoa is firing a NSApplicationDidChangeScreenParametersNotification:

问题是,对于 Apple Docs ,则只有在

The problem is that as for Apple Docs, it should be raised only when


更改连接到计算机
的显示器的配置时发布。可以以编程方式或者当用户更改Displays
控制面板中的设置时进行配置更改
。通知对象为sharedApplication。此
通知不包含userInfo字典。

Posted when the configuration of the displays attached to the computer is changed. The configuration change can be made either programmatically or when the user changes settings in the Displays control panel. The notification object is sharedApplication. This notification does not contain a userInfo dictionary.

因此,如果要在附加新显示时更新应用程序窗口(例如改变/移动一些HUD窗口的框架/等),你会有一个假的通知来到码头。
也没有附加到此通知的userInfo字典,所以我没有机会检查每当是dock或新的显示控制器。

So if you want to update your application windows when attach a new display (e.g. changing/moving the frame of some HUD window/etc), you will have a fake notification coming the dock. Also there's no userInfo dictionary attached to this notification, so I had no chance to check whenever was the dock or a new display controller.

那么如何处理

一种可能的解决方案是在通知激活时检查[NSScreen mainScreen]大小。
如果这个NSSize改变,那个通知来自一个新的显示器,而不是从dock:

A possible solution is to check the [NSScreen mainScreen] size when the notification si fired. If this NSSize changes, that notification comes from a new display attached, not from the dock:

static NSSize mainScreenSize;

-(void)handleApplicationDidChangeScreenParameters:(NSNotification *)notification {


    NSSize screenSize = [[NSScreen mainScreen] frame].size;


    if( screenSize.width != mainScreenSize.width || screenSize.height != mainScreenSize.height ) { // screen size changed

        mainScreenSize =  [[NSScreen mainScreen] frame].size;
        [myWindowController updateContent];
        [[myWindow contentView] setNeedsDisplay:YES]; // update custom window

}


推荐答案

通知是因为主画面的 visibleFrame (不包括Dock占用的空间)取决于Dock的位置。

The notification is fired because the main screen's visibleFrame (which excludes the space occupied by the Dock) depends on the position of the Dock.

因此,如果主屏幕的 visibleFrame 更改,您可以确定通知是Dock移动的结果。

So if the visibleFrame of the main screen changes, you can be sure that the notification is the result of the Dock being moved.

这篇关于Cocoa Dock触发NSApplicationDidChangeScreenParametersNotification的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆