Search code examples
cocoadocknsapplication

Cocoa Dock fires NSApplicationDidChangeScreenParametersNotification


When changing the dock position Cocoa is firing a NSApplicationDidChangeScreenParametersNotification:

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

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.

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.

So how to handle this?

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

}

Solution

  • 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.

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