There is a method -[NSStatusItem setView:]
. When you set a custom view for your status item, this view is automatically inserted into a special status bar window. And you can access that window using a 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)); // i.e. {{1159, 900}, {24, 22}}
}
You will receive the notification every time the status bar becomes visible or hidden and when your icon is moved. This is your chance to update a location of your popup panel.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…