Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

macos - Get Notification of NSStatusItem frame change?

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

enter image description here

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...