获取NSStatusItem框架更改的通知? [英] Get Notification of NSStatusItem frame change?
问题描述
在使用NSStatusItem
并具有如下自定义视图的应用中:
In an app that uses a NSStatusItem
with a custom view like this:
...在以下情况下如何获取通知
... how can you get notifications when:
- 由于全屏应用程序,状态栏被隐藏
- 状态项是否因为添加/删除/调整了其他项而移动位置?
当项目更改位置时,将自定义视图移动到正确位置都是必需的.
Both are necessary to move the custom view to the right position when the item changes places.
推荐答案
有一种方法-[NSStatusItem setView:]
.当您为状态项设置自定义视图时,该视图会自动插入到特殊的状态栏窗口中.您可以使用方法-[NSView window]
访问该窗口以观察其NSWindowDidMoveNotification
:
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.
这篇关于获取NSStatusItem框架更改的通知?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!