检测全屏菜单栏移动

时间:2012-08-09 03:58:20

标签: objective-c macos statusbar

所以现在我有一个NSWindow使用INAppStoreWindow像NSToolbar一样吸引自己,我想知道当应用程序全屏显示菜单栏移动时是否有任何类型的事件或消息发出,以便我可以移动窗口的标题栏作为响应(标准NSToolbar的工作方式)。显然,NSToolbar知道我不知道的事情,这样可以避免我在窗口的上半部分制作NSTrackingArea。

以下是它现在的作用:

Before

这就是我想做的事情:

enter image description here

不幸的是,KVO'ing fullScreenAccessoryView不起作用。帧事件仅在进入和退出全屏模式时生成,而不是在状态栏“向下移动”工具栏时生成。

3 个答案:

答案 0 :(得分:2)

这个怎么样?使用宽度为NSStatusBarItem的自定义NSView创建0,然后使用window跟踪其NSWindowWillMoveNotification的位置。

更新:我做了一个INAppStoreWindow的分叉,并在菜单栏上附加了一个自定义工具栏。 Check it out

答案 1 :(得分:0)

您可以尝试的一件事是在窗口上设置假(即空)工具栏,然后给它fullScreenAccessoryView。移动到全屏时,此视图从视图层次结构中删除并附加在工具栏下方。但是,Dunno在使用自定义窗口类时是如何工作的......:/

答案 2 :(得分:0)

我希望完成同样的事情。我能够通过在WAYAppStoreWindow(或INAppStoreWindow)中设置标题栏视图的超级视图来发布帧更改通知,然后在此视图上观察帧更改通知。在您的设置中添加以下观察者:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(windowTitleBarFrameDidChange:)
                                             name:NSViewFrameDidChangeNotification
                                           object:window.titleBarView.superview];

在进入/退出全屏时打开/关闭帧更改通知:

window.titleBarView.superview.postsFrameChangedNotifications = YES;

然后在windowTitleBarFrameWillChange:方法的实现中,通过与标题栏的超视图帧的最后y位置进行比较来检查标题栏的显示时间。这是代码:

// Window titlebar heights.
#define kWindowTitlebarHeightDefault    22.0
#define kWindowTitlebarHeightStandard   37.0
#define kWindowTitlebarHeightExtended   82.0

- (void) windowTitleBarFrameDidChange:(NSNotification *)notification;
{
    NSView * titleBarView = [notification object];  // view is actually the titlebar container view

    if (NSMinX(_lastTitleBarFrame) == NSMinX(titleBarView.frame) &&
        NSWidth(_lastTitleBarFrame) == NSWidth(titleBarView.frame) &&
        NSHeight(_lastTitleBarFrame) == NSHeight(titleBarView.frame) &&
        NSMinY(_lastTitleBarFrame) == -kWindowTitlebarHeightDefault &&
        NSMinY(titleBarView.frame) > -kWindowTitlebarHeightDefault)                     // titlebar will show
    {
        [self windowTitleBarWillShow];
    }
    else if (NSMinX(_lastTitleBarFrame) == NSMinX(titleBarView.frame) &&
             NSWidth(_lastTitleBarFrame) == NSWidth(titleBarView.frame) &&
             NSHeight(_lastTitleBarFrame) == NSHeight(titleBarView.frame) &&
             NSMinY(_lastTitleBarFrame) == 0.0 && NSMinY(titleBarView.frame) < 0.0)     // titlebar will hide
    {
        [self windowTitleBarWillHide:YES];
    }
    else if (NSWidth(_lastTitleBarFrame) != NSWidth([NSScreen mainScreen].frame) &&
             NSWidth(titleBarView.frame) == NSWidth([NSScreen mainScreen].frame))       // just went full-screen
    {
        [self windowTitleBarWillHide:NO];
    }

    _lastTitleBarFrame = titleBarView.frame;
}

- (void) windowTitleBarWillHide:(BOOL)animate
{
    WAYAppStoreWindow * window = (WAYAppStoreWindow *)[(NSWindowController *)[[self windowControllers] objectAtIndex:0] window];
    NSView * themeFrame = window.titleBarView.superview.superview;    
    if (animate)
        [themeFrame animator].alphaValue = 0.0;
    else
        themeFrame.alphaValue = 0.0;
}

- (void) windowTitleBarWillShow
{
    WAYAppStoreWindow * window = (WAYAppStoreWindow *)[(NSWindowController *)[[self windowControllers] objectAtIndex:0] window];
    NSView * themeFrame = window.titleBarView.superview.superview;
    [themeFrame animator].alphaValue = 1.0;
}

- (void) windowWillEnterFullScreen:(NSNotification *)notification
{
    WAYAppStoreWindow * window = [notification object];
    [self setUpFullScreenTitleBarForWindow:window];
    _lastTitleBarFrame = NSZeroRect;
}

- (void) windowDidEnterFullScreen:(NSNotification *)notification;
{
    WAYAppStoreWindow * window = [notification object];
    window.titleBarView.superview.postsFrameChangedNotifications = YES;
    _fullscreenToolbarView.hidden = NO;
}

- (void) windowWillExitFullScreen:(NSNotification *)notification;
{
    WAYAppStoreWindow * window = [notification object];
    window.titleBarView.superview.postsFrameChangedNotifications = NO;
    [self setUpStandardTitleBarForWindow:window];
}

- (void) setUpNormalTitleBarForWindow:(WAYAppStoreWindow *)window
{
    window.appearance = nil;
    window.showsTitle = NO;
    window.titleBarHeight = kWindowTitlebarHeightExtended;
    window.verticalTrafficLightButtons = YES;
    window.centerTrafficLightButtons = YES;
    window.trafficLightButtonsLeftMargin = 13.0;

    _fullscreenToolbarView.hidden = YES;
}

- (void) setUpFullScreenTitleBarForWindow:(WAYAppStoreWindow *)window
{
    window.appearance = [NSAppearance appearanceNamed:NSAppearanceNameVibrantDark];
    window.showsTitle = YES;
    window.titleBarHeight = kWindowTitlebarHeightStandard;
    window.verticalTrafficLightButtons = NO;
    window.centerTrafficLightButtons = YES;
    window.trafficLightButtonsLeftMargin = 13.0;
    window.verticallyCenterTitle = YES;

    _fullscreenToolbarView.hidden = YES;
    _lastTitleBarFrame = NSZeroRect;
}

注意:我正在使用WAYWindow的分支,它增加了对标题栏中垂直居中文档标题的支持。