状态栏窗口仅出现在主桌面中

时间:2015-11-10 10:37:07

标签: objective-c xcode macos cocoa

我正在按照本教程在我的cocoa应用程序中创建一个状态栏项:http://blog.shpakovski.com/2011/07/cocoa-popup-window-in-status-bar.html

在本教程中,Info.plist中的Application is agent (UIElement)键设置为YES,但我希望我的应用程序正常运行(即有一个停靠图标),所以我设置了这个键到NO

然而,这会导致我的状态栏窗口视图仅显示在应用程序处于活动状态的主桌面上。例如,当尝试在全屏应用程序中打开状态项时,会发生以下情况: enter image description here

但是当密钥设置为YES时,不会发生这种情况。我需要改变什么想法?以下是调用状态项窗口的代码:

NSWindow *panel = [self window];

NSRect screenRect = [[NSScreen mainScreen] visibleFrame];
NSRect statusRect = NSZeroRect;

StatusItemView *statusItemView = nil;
if ([self.delegate respondsToSelector:@selector(statusItemViewForPanelController:)])
{
    statusItemView = [self.delegate statusItemViewForPanelController:self];
}

if (statusItemView)
{
    statusRect = statusItemView.globalRect;
    statusRect.origin.y = NSMinY(statusRect) - NSHeight(statusRect);
}
else
{
    statusRect.size = NSMakeSize(STATUS_ITEM_VIEW_WIDTH, [[NSStatusBar systemStatusBar] thickness]);
    statusRect.origin.x = roundf((NSWidth(screenRect) - NSWidth(statusRect)) / 2);
    statusRect.origin.y = NSHeight(screenRect) - NSHeight(statusRect) * 2;
}


NSRect panelRect = [panel frame];
panelRect.size.width = PANEL_WIDTH;
panelRect.size.height = POPUP_HEIGHT;
panelRect.origin.x = roundf(NSMidX(statusRect) - NSWidth(panelRect) / 2);
panelRect.origin.y = NSMaxY(statusRect) - NSHeight(panelRect);

if (NSMaxX(panelRect) > (NSMaxX(screenRect) - ARROW_HEIGHT))
    panelRect.origin.x -= NSMaxX(panelRect) - (NSMaxX(screenRect) - ARROW_HEIGHT);

[NSApp activateIgnoringOtherApps:NO];
[panel setAlphaValue:0];
[panel setFrame:statusRect display:YES];
[panel makeKeyAndOrderFront:nil];

NSTimeInterval openDuration = OPEN_DURATION;

NSEvent *currentEvent = [NSApp currentEvent];
if ([currentEvent type] == NSLeftMouseDown)
{
    NSUInteger clearFlags = ([currentEvent modifierFlags] & NSDeviceIndependentModifierFlagsMask);
    BOOL shiftPressed = (clearFlags == NSShiftKeyMask);
    BOOL shiftOptionPressed = (clearFlags == (NSShiftKeyMask | NSAlternateKeyMask));
    if (shiftPressed || shiftOptionPressed)
    {
        openDuration *= 10;

        if (shiftOptionPressed)
            NSLog(@"Icon is at %@\n\tMenu is on screen %@\n\tWill be animated to %@",
                  NSStringFromRect(statusRect), NSStringFromRect(screenRect), NSStringFromRect(panelRect));
    }
}

[NSAnimationContext beginGrouping];
[[NSAnimationContext currentContext] setDuration:openDuration];
[[panel animator] setFrame:panelRect display:YES];
[[panel animator] setAlphaValue:1];
[NSAnimationContext endGrouping];

0 个答案:

没有答案