INAppStoreWindow标题栏上的NSSplitView分隔符

时间:2013-02-26 15:19:40

标签: objective-c macos cocoa nssplitview inappstorewindow

我正在尝试创建一个类似Reeder / Sparrow的UI来处理我的应用内容。目前我在内部使用带有两个NSView的NSSplitView(左边的那个是内容列表,另一个是右边的“检查器”)。

我想知道的是如何在标题栏上创建分隔符,它也可以作为分割视图的分隔符。 我已经在使用INAppStoreWindow子类了。

有什么想法吗? Thanx提前

1 个答案:

答案 0 :(得分:8)

我这样做的方法是添加一个NSSplitView子类作为INAppStoreWindow的tileBarView的子视图:

// This code comes from the INAppStoreWindow readme
INAppStoreWindow *appStoreWindow = (INAppStoreWindow *)[self window];

// self.titleView is a an IBOutlet to an NSView that has been configured in IB with everything you want in the title bar
self.windowTitleBarView.frame = appStoreWindow.titleBarView.bounds;
self.windowTitleBarView.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;
[appStoreWindow.titleBarView addSubview:self.windowTitleBarView];

两个棘手的部分是让这个分割视图表现得像标题栏(即仍然允许你拖动窗口),并将标题栏中的分割视图与窗口中的主分割视图同步,所以它们对用户来说似乎是一样的。

要解决第一个问题,您必须做的不仅仅是从标题栏NSSplitView子类中的-mouseDownCanMovewWindow返回YES。如果您这样做,则平铺栏的子视图都不会响应鼠标事件。相反,这样做:

@implementation MyTitleBarSplitView

- (BOOL)mouseDownCanMoveWindow
{
    return NO;
}

// Code below adapted from http://www.cocoabuilder.com/archive/cocoa/219261-conditional-mousedowncanmovewindow-for-nsview.html
- (void)mouseDown:(NSEvent*)theEvent 
{
    NSWindow *window = [self window];
    NSPoint mouseLocation = [theEvent locationInWindow];
    NSRect dividerRect = NSMakeRect(NSMaxX([[[self subviews] objectAtIndex:0] frame]), 
                                    NSMinY([self bounds]), 
                                    [self dividerThickness], 
                                    NSHeight([self bounds]));
    dividerRect = NSInsetRect(dividerRect, -2, 0);
    NSPoint mouseLocationInMyCoords = [self convertPoint:mouseLocation fromView:nil];
    if (![self mouse:mouseLocationInMyCoords inRect:dividerRect]) 
    {
        mouseLocation = [window convertBaseToScreen:mouseLocation];
        NSPoint origin = [window frame].origin;
        // Now we loop handling mouse events until we get a mouse up event.
        while ((theEvent = [NSApp nextEventMatchingMask:NSLeftMouseDownMask|NSLeftMouseDraggedMask|NSLeftMouseUpMask untilDate:[NSDate distantFuture] inMode:NSEventTrackingRunLoopMode dequeue:YES])&&([theEvent type]!=NSLeftMouseUp)) 
        {
            @autoreleasepool 
            {
                NSPoint currentLocation = [window convertBaseToScreen:[theEvent locationInWindow]];
                origin.x += currentLocation.x-mouseLocation.x;
                origin.y += currentLocation.y-mouseLocation.y;
                // Move the window by the mouse displacement since the last event.
                [window setFrameOrigin:origin];
                mouseLocation = currentLocation;
            }
        }
        [self mouseUp:theEvent];
        return;
    }

    [super mouseDown:theEvent];
}

@end

第二项业务是同步两个拆分视图。使您的控制器类(可能是窗口控制器,但在您的代码中有意义)代表您的主要内容拆分视图和标题栏拆分视图。然后,实现下面的两个NSSplitView委托方法:

@implementation MyController
{
    BOOL updatingLinkedSplitview;
}

- (CGFloat)splitView:(NSSplitView *)splitView constrainSplitPosition:(CGFloat)proposedPosition ofSubviewAt:(NSInteger)dividerIndex
{
    // If already updating a split view, return early to avoid infinite loop and stack overflow
    if (updatingLinkedSplitview) return proposedPosition;

    if (splitView == self.mainSplitView)
    {
        // Main splitview is being resized, so manually resize the title bar split view
        updatingLinkedSplitview = YES;
        [self.titleBarSplitView setPosition:proposedPosition ofDividerAtIndex:dividerIndex];
        updatingLinkedSplitview = NO;
    }
    else if (splitView == self.titleBarSplitView)
    {
        // Title bar splitview is being resized, so manually resize the main split view
        updatingLinkedSplitview = YES;
        [self.mainSplitView setPosition:proposedPosition ofDividerAtIndex:dividerIndex];
        updatingLinkedSplitview = NO;
    }

    return proposedPosition;
}

- (void)splitView:(NSSplitView *)splitView resizeSubviewsWithOldSize:(NSSize)oldSize
{
    // This is to synchronize the splitter positions when the window is first loaded
    if (splitView == self.titleBarSplitView)
    {
        NSRect leftFrame = NSMakeRect(NSMinX([self.leftTitleBarView frame]),
                                      NSMinY([self.leftTitleBarView frame]),
                                      NSWidth([self.leftMainSplitView frame]),
                                      NSHeight([self.leftTitleBarView frame]));
        NSRect rightFrame = NSMakeRect(NSMaxX(leftFrame) + [splitView dividerThickness],
                                      NSMinY([self.rightTitleBarView frame]),
                                      NSWidth([self.rightMainSplitView frame]),
                                      NSHeight([self.rightTitleBarView frame]));

        [self.leftTitleBarView setFrame:leftFrame];
        [self.rightTitleBarView setFrame:rightFrame];
    }
}