【问题标题】:Status bar window only appearing in main desktop状态栏窗口只出现在主桌面
【发布时间】:2015-11-10 10:37:06
【问题描述】:

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

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

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

但是,当密钥设置为 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];

【问题讨论】:

    标签: objective-c xcode macos cocoa


    【解决方案1】:

    尝试将面板的level 设置为NSTornOffMenuWindowLevel 和/或将它的collectionBehavior 设置为NSWindowCollectionBehaviorCanJoinAllSpaces

    【讨论】:

      猜你喜欢
      • 2013-01-11
      • 2011-02-15
      • 1970-01-01
      • 1970-01-01
      • 2016-11-26
      • 2015-09-25
      • 1970-01-01
      • 2013-09-06
      • 1970-01-01
      相关资源
      最近更新 更多