I am following this tutorial to create a status bar item in my cocoa application: http://blog.shpakovski.com/2011/07/cocoa-popup-window-in-status-bar.html
In the tutorial, the Application is agent (UIElement)
key in the Info.plist is set to YES
, but I want my application to function normally (ie. have a dock icon) so I have set this key to NO
.
However this causes my status bar window view only show on the main desktop where the app is active. For example, when attempting to open the status item in a full screen app this occurs:
This does not happen however when the key is set to YES
. Any ideas what I need to change? Here is the code where the status item window is called:
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];