Receive frame change notification NSStatusItem?

In an application using NSStatusItem with a custom view, for example:

enter image description here

... how you can receive notifications when:

  • Status bar hiding due to full screen application
  • Does the status element move the position because another element was added / deleted / changed?

Both are needed to move the user view to the desired position when the item changes places.

+4
source share
1 answer

There is a method -[NSStatusItem setView:] . When you set a custom view for your status element, that view is automatically inserted into a special window on the status bar. And you can access this window using the method -[NSView window] to observe its NSWindowDidMoveNotification :

 - (void)applicationDidFinishLaunching:(NSNotification *)notification { NSStatusItem *statusItem = [self newStatusItem]; NSView *statusItemView = [self newStatusItemView]; statusItem.view = statusItemView; NSNotificationCenter *dnc = [NSNotificationCenter defaultCenter]; [dnc addObserver:self selector:@selector(statusBarDidMove:) name:NSWindowDidMoveNotification object:statusItemView.window]; } - (void)statusBarDidMove:(NSNotification *)note { NSWindow *window = note.object; NSLog(@"%@", NSStringFromRect(window.frame)); // ie {{1159, 900}, {24, 22}} } 

You will be notified every time the status bar becomes visible or hidden and when your icon is moved. This is your chance to update the location of the popup panel.

+9
source

All Articles