简体   繁体   English

获取NSStatusItem框架更改的通知?

[英]Get Notification of NSStatusItem frame change?

In an app that uses a NSStatusItem with a custom view like this: 在使用带有自定义视图的NSStatusItem的应用程序中,如下所示:

在此输入图像描述

... 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. 当项目更改位置时,两者都是将自定义视图移动到正确位置所必需的。

There is a method -[NSStatusItem setView:] . 有一个方法-[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 : 您可以使用方法-[NSView window]访问该窗口以观察其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. 这是您更新弹出式面板位置的机会。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM