Get Notification of NSStatusItem frame change?

放肆的年华 提交于 2019-12-29 05:35:08

问题


In an app that uses a NSStatusItem with a custom view like this:

... how can you get notifications when:

  1. The status bar gets hidden because of a full screen app
  2. The status item moves position because another item is added/removed/resized?

Both are necessary to move the custom view to the right position when the item changes places.


回答1:


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.



来源:https://stackoverflow.com/questions/11504696/get-notification-of-nsstatusitem-frame-change

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!